diff options
Diffstat (limited to 'bufaux.c')
-rw-r--r-- | bufaux.c | 215 |
1 files changed, 171 insertions, 44 deletions
@@ -37,7 +37,7 @@ | |||
37 | */ | 37 | */ |
38 | 38 | ||
39 | #include "includes.h" | 39 | #include "includes.h" |
40 | RCSID("$OpenBSD: bufaux.c,v 1.32 2004/02/23 15:12:46 markus Exp $"); | 40 | RCSID("$OpenBSD: bufaux.c,v 1.34 2004/12/06 16:00:43 markus Exp $"); |
41 | 41 | ||
42 | #include <openssl/bn.h> | 42 | #include <openssl/bn.h> |
43 | #include "bufaux.h" | 43 | #include "bufaux.h" |
@@ -49,8 +49,8 @@ RCSID("$OpenBSD: bufaux.c,v 1.32 2004/02/23 15:12:46 markus Exp $"); | |||
49 | * Stores an BIGNUM in the buffer with a 2-byte msb first bit count, followed | 49 | * Stores an BIGNUM in the buffer with a 2-byte msb first bit count, followed |
50 | * by (bits+7)/8 bytes of binary data, msb first. | 50 | * by (bits+7)/8 bytes of binary data, msb first. |
51 | */ | 51 | */ |
52 | void | 52 | int |
53 | buffer_put_bignum(Buffer *buffer, const BIGNUM *value) | 53 | buffer_put_bignum_ret(Buffer *buffer, const BIGNUM *value) |
54 | { | 54 | { |
55 | int bits = BN_num_bits(value); | 55 | int bits = BN_num_bits(value); |
56 | int bin_size = (bits + 7) / 8; | 56 | int bin_size = (bits + 7) / 8; |
@@ -60,9 +60,11 @@ buffer_put_bignum(Buffer *buffer, const BIGNUM *value) | |||
60 | 60 | ||
61 | /* Get the value of in binary */ | 61 | /* Get the value of in binary */ |
62 | oi = BN_bn2bin(value, buf); | 62 | oi = BN_bn2bin(value, buf); |
63 | if (oi != bin_size) | 63 | if (oi != bin_size) { |
64 | fatal("buffer_put_bignum: BN_bn2bin() failed: oi %d != bin_size %d", | 64 | error("buffer_put_bignum_ret: BN_bn2bin() failed: oi %d != bin_size %d", |
65 | oi, bin_size); | 65 | oi, bin_size); |
66 | return (-1); | ||
67 | } | ||
66 | 68 | ||
67 | /* Store the number of bits in the buffer in two bytes, msb first. */ | 69 | /* Store the number of bits in the buffer in two bytes, msb first. */ |
68 | PUT_16BIT(msg, bits); | 70 | PUT_16BIT(msg, bits); |
@@ -72,36 +74,63 @@ buffer_put_bignum(Buffer *buffer, const BIGNUM *value) | |||
72 | 74 | ||
73 | memset(buf, 0, bin_size); | 75 | memset(buf, 0, bin_size); |
74 | xfree(buf); | 76 | xfree(buf); |
77 | |||
78 | return (0); | ||
79 | } | ||
80 | |||
81 | void | ||
82 | buffer_put_bignum(Buffer *buffer, const BIGNUM *value) | ||
83 | { | ||
84 | if (buffer_put_bignum_ret(buffer, value) == -1) | ||
85 | fatal("buffer_put_bignum: buffer error"); | ||
75 | } | 86 | } |
76 | 87 | ||
77 | /* | 88 | /* |
78 | * Retrieves an BIGNUM from the buffer. | 89 | * Retrieves an BIGNUM from the buffer. |
79 | */ | 90 | */ |
80 | void | 91 | int |
81 | buffer_get_bignum(Buffer *buffer, BIGNUM *value) | 92 | buffer_get_bignum_ret(Buffer *buffer, BIGNUM *value) |
82 | { | 93 | { |
83 | u_int bits, bytes; | 94 | u_int bits, bytes; |
84 | u_char buf[2], *bin; | 95 | u_char buf[2], *bin; |
85 | 96 | ||
86 | /* Get the number for bits. */ | 97 | /* Get the number for bits. */ |
87 | buffer_get(buffer, (char *) buf, 2); | 98 | if (buffer_get_ret(buffer, (char *) buf, 2) == -1) { |
99 | error("buffer_get_bignum_ret: invalid length"); | ||
100 | return (-1); | ||
101 | } | ||
88 | bits = GET_16BIT(buf); | 102 | bits = GET_16BIT(buf); |
89 | /* Compute the number of binary bytes that follow. */ | 103 | /* Compute the number of binary bytes that follow. */ |
90 | bytes = (bits + 7) / 8; | 104 | bytes = (bits + 7) / 8; |
91 | if (bytes > 8 * 1024) | 105 | if (bytes > 8 * 1024) { |
92 | fatal("buffer_get_bignum: cannot handle BN of size %d", bytes); | 106 | error("buffer_get_bignum_ret: cannot handle BN of size %d", bytes); |
93 | if (buffer_len(buffer) < bytes) | 107 | return (-1); |
94 | fatal("buffer_get_bignum: input buffer too small"); | 108 | } |
109 | if (buffer_len(buffer) < bytes) { | ||
110 | error("buffer_get_bignum_ret: input buffer too small"); | ||
111 | return (-1); | ||
112 | } | ||
95 | bin = buffer_ptr(buffer); | 113 | bin = buffer_ptr(buffer); |
96 | BN_bin2bn(bin, bytes, value); | 114 | BN_bin2bn(bin, bytes, value); |
97 | buffer_consume(buffer, bytes); | 115 | if (buffer_consume_ret(buffer, bytes) == -1) { |
116 | error("buffer_get_bignum_ret: buffer_consume failed"); | ||
117 | return (-1); | ||
118 | } | ||
119 | return (0); | ||
120 | } | ||
121 | |||
122 | void | ||
123 | buffer_get_bignum(Buffer *buffer, BIGNUM *value) | ||
124 | { | ||
125 | if (buffer_get_bignum_ret(buffer, value) == -1) | ||
126 | fatal("buffer_get_bignum: buffer error"); | ||
98 | } | 127 | } |
99 | 128 | ||
100 | /* | 129 | /* |
101 | * Stores an BIGNUM in the buffer in SSH2 format. | 130 | * Stores an BIGNUM in the buffer in SSH2 format. |
102 | */ | 131 | */ |
103 | void | 132 | int |
104 | buffer_put_bignum2(Buffer *buffer, const BIGNUM *value) | 133 | buffer_put_bignum2_ret(Buffer *buffer, const BIGNUM *value) |
105 | { | 134 | { |
106 | u_int bytes; | 135 | u_int bytes; |
107 | u_char *buf; | 136 | u_char *buf; |
@@ -110,69 +139,140 @@ buffer_put_bignum2(Buffer *buffer, const BIGNUM *value) | |||
110 | 139 | ||
111 | if (BN_is_zero(value)) { | 140 | if (BN_is_zero(value)) { |
112 | buffer_put_int(buffer, 0); | 141 | buffer_put_int(buffer, 0); |
113 | return; | 142 | return 0; |
143 | } | ||
144 | if (value->neg) { | ||
145 | error("buffer_put_bignum2_ret: negative numbers not supported"); | ||
146 | return (-1); | ||
114 | } | 147 | } |
115 | if (value->neg) | ||
116 | fatal("buffer_put_bignum2: negative numbers not supported"); | ||
117 | bytes = BN_num_bytes(value) + 1; /* extra padding byte */ | 148 | bytes = BN_num_bytes(value) + 1; /* extra padding byte */ |
118 | if (bytes < 2) | 149 | if (bytes < 2) { |
119 | fatal("buffer_put_bignum2: BN too small"); | 150 | error("buffer_put_bignum2_ret: BN too small"); |
151 | return (-1); | ||
152 | } | ||
120 | buf = xmalloc(bytes); | 153 | buf = xmalloc(bytes); |
121 | buf[0] = '\0'; | 154 | buf[0] = 0x00; |
122 | /* Get the value of in binary */ | 155 | /* Get the value of in binary */ |
123 | oi = BN_bn2bin(value, buf+1); | 156 | oi = BN_bn2bin(value, buf+1); |
124 | if (oi != bytes-1) | 157 | if (oi != bytes-1) { |
125 | fatal("buffer_put_bignum2: BN_bn2bin() failed: " | 158 | error("buffer_put_bignum2_ret: BN_bn2bin() failed: " |
126 | "oi %d != bin_size %d", oi, bytes); | 159 | "oi %d != bin_size %d", oi, bytes); |
160 | xfree(buf); | ||
161 | return (-1); | ||
162 | } | ||
127 | hasnohigh = (buf[1] & 0x80) ? 0 : 1; | 163 | hasnohigh = (buf[1] & 0x80) ? 0 : 1; |
128 | buffer_put_string(buffer, buf+hasnohigh, bytes-hasnohigh); | 164 | buffer_put_string(buffer, buf+hasnohigh, bytes-hasnohigh); |
129 | memset(buf, 0, bytes); | 165 | memset(buf, 0, bytes); |
130 | xfree(buf); | 166 | xfree(buf); |
167 | return (0); | ||
131 | } | 168 | } |
132 | 169 | ||
133 | void | 170 | void |
134 | buffer_get_bignum2(Buffer *buffer, BIGNUM *value) | 171 | buffer_put_bignum2(Buffer *buffer, const BIGNUM *value) |
172 | { | ||
173 | if (buffer_put_bignum2_ret(buffer, value) == -1) | ||
174 | fatal("buffer_put_bignum2: buffer error"); | ||
175 | } | ||
176 | |||
177 | int | ||
178 | buffer_get_bignum2_ret(Buffer *buffer, BIGNUM *value) | ||
135 | { | 179 | { |
136 | u_int len; | 180 | u_int len; |
137 | u_char *bin = buffer_get_string(buffer, &len); | 181 | u_char *bin; |
182 | |||
183 | if ((bin = buffer_get_string_ret(buffer, &len)) == NULL) { | ||
184 | error("buffer_get_bignum2_ret: invalid bignum"); | ||
185 | return (-1); | ||
186 | } | ||
138 | 187 | ||
139 | if (len > 0 && (bin[0] & 0x80)) | 188 | if (len > 0 && (bin[0] & 0x80)) { |
140 | fatal("buffer_get_bignum2: negative numbers not supported"); | 189 | error("buffer_get_bignum2_ret: negative numbers not supported"); |
141 | if (len > 8 * 1024) | 190 | return (-1); |
142 | fatal("buffer_get_bignum2: cannot handle BN of size %d", len); | 191 | } |
192 | if (len > 8 * 1024) { | ||
193 | error("buffer_get_bignum2_ret: cannot handle BN of size %d", len); | ||
194 | return (-1); | ||
195 | } | ||
143 | BN_bin2bn(bin, len, value); | 196 | BN_bin2bn(bin, len, value); |
144 | xfree(bin); | 197 | xfree(bin); |
198 | return (0); | ||
199 | } | ||
200 | |||
201 | void | ||
202 | buffer_get_bignum2(Buffer *buffer, BIGNUM *value) | ||
203 | { | ||
204 | if (buffer_get_bignum2_ret(buffer, value) == -1) | ||
205 | fatal("buffer_get_bignum2: buffer error"); | ||
145 | } | 206 | } |
146 | 207 | ||
147 | /* | 208 | /* |
148 | * Returns integers from the buffer (msb first). | 209 | * Returns integers from the buffer (msb first). |
149 | */ | 210 | */ |
150 | 211 | ||
212 | int | ||
213 | buffer_get_short_ret(u_short *ret, Buffer *buffer) | ||
214 | { | ||
215 | u_char buf[2]; | ||
216 | |||
217 | if (buffer_get_ret(buffer, (char *) buf, 2) == -1) | ||
218 | return (-1); | ||
219 | *ret = GET_16BIT(buf); | ||
220 | return (0); | ||
221 | } | ||
222 | |||
151 | u_short | 223 | u_short |
152 | buffer_get_short(Buffer *buffer) | 224 | buffer_get_short(Buffer *buffer) |
153 | { | 225 | { |
154 | u_char buf[2]; | 226 | u_short ret; |
227 | |||
228 | if (buffer_get_short_ret(&ret, buffer) == -1) | ||
229 | fatal("buffer_get_short: buffer error"); | ||
155 | 230 | ||
156 | buffer_get(buffer, (char *) buf, 2); | 231 | return (ret); |
157 | return GET_16BIT(buf); | 232 | } |
233 | |||
234 | int | ||
235 | buffer_get_int_ret(u_int *ret, Buffer *buffer) | ||
236 | { | ||
237 | u_char buf[4]; | ||
238 | |||
239 | if (buffer_get_ret(buffer, (char *) buf, 4) == -1) | ||
240 | return (-1); | ||
241 | *ret = GET_32BIT(buf); | ||
242 | return (0); | ||
158 | } | 243 | } |
159 | 244 | ||
160 | u_int | 245 | u_int |
161 | buffer_get_int(Buffer *buffer) | 246 | buffer_get_int(Buffer *buffer) |
162 | { | 247 | { |
163 | u_char buf[4]; | 248 | u_int ret; |
249 | |||
250 | if (buffer_get_int_ret(&ret, buffer) == -1) | ||
251 | fatal("buffer_get_int: buffer error"); | ||
252 | |||
253 | return (ret); | ||
254 | } | ||
164 | 255 | ||
165 | buffer_get(buffer, (char *) buf, 4); | 256 | int |
166 | return GET_32BIT(buf); | 257 | buffer_get_int64_ret(u_int64_t *ret, Buffer *buffer) |
258 | { | ||
259 | u_char buf[8]; | ||
260 | |||
261 | if (buffer_get_ret(buffer, (char *) buf, 8) == -1) | ||
262 | return (-1); | ||
263 | *ret = GET_64BIT(buf); | ||
264 | return (0); | ||
167 | } | 265 | } |
168 | 266 | ||
169 | u_int64_t | 267 | u_int64_t |
170 | buffer_get_int64(Buffer *buffer) | 268 | buffer_get_int64(Buffer *buffer) |
171 | { | 269 | { |
172 | u_char buf[8]; | 270 | u_int64_t ret; |
173 | 271 | ||
174 | buffer_get(buffer, (char *) buf, 8); | 272 | if (buffer_get_int64_ret(&ret, buffer) == -1) |
175 | return GET_64BIT(buf); | 273 | fatal("buffer_get_int: buffer error"); |
274 | |||
275 | return (ret); | ||
176 | } | 276 | } |
177 | 277 | ||
178 | /* | 278 | /* |
@@ -214,25 +314,41 @@ buffer_put_int64(Buffer *buffer, u_int64_t value) | |||
214 | * to the returned string, and is not counted in length. | 314 | * to the returned string, and is not counted in length. |
215 | */ | 315 | */ |
216 | void * | 316 | void * |
217 | buffer_get_string(Buffer *buffer, u_int *length_ptr) | 317 | buffer_get_string_ret(Buffer *buffer, u_int *length_ptr) |
218 | { | 318 | { |
219 | u_char *value; | 319 | u_char *value; |
220 | u_int len; | 320 | u_int len; |
221 | 321 | ||
222 | /* Get the length. */ | 322 | /* Get the length. */ |
223 | len = buffer_get_int(buffer); | 323 | len = buffer_get_int(buffer); |
224 | if (len > 256 * 1024) | 324 | if (len > 256 * 1024) { |
225 | fatal("buffer_get_string: bad string length %u", len); | 325 | error("buffer_get_string_ret: bad string length %u", len); |
326 | return (NULL); | ||
327 | } | ||
226 | /* Allocate space for the string. Add one byte for a null character. */ | 328 | /* Allocate space for the string. Add one byte for a null character. */ |
227 | value = xmalloc(len + 1); | 329 | value = xmalloc(len + 1); |
228 | /* Get the string. */ | 330 | /* Get the string. */ |
229 | buffer_get(buffer, value, len); | 331 | if (buffer_get_ret(buffer, value, len) == -1) { |
332 | error("buffer_get_string_ret: buffer_get failed"); | ||
333 | xfree(value); | ||
334 | return (NULL); | ||
335 | } | ||
230 | /* Append a null character to make processing easier. */ | 336 | /* Append a null character to make processing easier. */ |
231 | value[len] = 0; | 337 | value[len] = 0; |
232 | /* Optionally return the length of the string. */ | 338 | /* Optionally return the length of the string. */ |
233 | if (length_ptr) | 339 | if (length_ptr) |
234 | *length_ptr = len; | 340 | *length_ptr = len; |
235 | return value; | 341 | return (value); |
342 | } | ||
343 | |||
344 | void * | ||
345 | buffer_get_string(Buffer *buffer, u_int *length_ptr) | ||
346 | { | ||
347 | void *ret; | ||
348 | |||
349 | if ((ret = buffer_get_string_ret(buffer, length_ptr)) == NULL) | ||
350 | fatal("buffer_get_string: buffer error"); | ||
351 | return (ret); | ||
236 | } | 352 | } |
237 | 353 | ||
238 | /* | 354 | /* |
@@ -256,11 +372,22 @@ buffer_put_cstring(Buffer *buffer, const char *s) | |||
256 | * Returns a character from the buffer (0 - 255). | 372 | * Returns a character from the buffer (0 - 255). |
257 | */ | 373 | */ |
258 | int | 374 | int |
375 | buffer_get_char_ret(char *ret, Buffer *buffer) | ||
376 | { | ||
377 | if (buffer_get_ret(buffer, ret, 1) == -1) { | ||
378 | error("buffer_get_char_ret: buffer_get_ret failed"); | ||
379 | return (-1); | ||
380 | } | ||
381 | return (0); | ||
382 | } | ||
383 | |||
384 | int | ||
259 | buffer_get_char(Buffer *buffer) | 385 | buffer_get_char(Buffer *buffer) |
260 | { | 386 | { |
261 | char ch; | 387 | char ch; |
262 | 388 | ||
263 | buffer_get(buffer, &ch, 1); | 389 | if (buffer_get_char_ret(&ch, buffer) == -1) |
390 | fatal("buffer_get_char: buffer error"); | ||
264 | return (u_char) ch; | 391 | return (u_char) ch; |
265 | } | 392 | } |
266 | 393 | ||