summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog6
-rw-r--r--bufaux.c213
-rw-r--r--bufaux.h12
-rw-r--r--buffer.c48
-rw-r--r--buffer.h6
5 files changed, 229 insertions, 56 deletions
diff --git a/ChangeLog b/ChangeLog
index 3847553dc..f991fe7eb 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -54,6 +54,10 @@
54 - djm@cvs.openbsd.org 2004/10/29 22:53:56 54 - djm@cvs.openbsd.org 2004/10/29 22:53:56
55 [clientloop.c misc.h readpass.c ssh-agent.c] 55 [clientloop.c misc.h readpass.c ssh-agent.c]
56 factor out common permission-asking code to separate function; ok markus@ 56 factor out common permission-asking code to separate function; ok markus@
57 - djm@cvs.openbsd.org 2004/10/29 23:56:17
58 [bufaux.c bufaux.h buffer.c buffer.h]
59 introduce a new buffer API that returns an error rather than fatal()ing
60 when presented with bad data; ok markus@
57 61
5820041102 6220041102
59 - (dtucker) [configure.ac includes.h] Bug #947: Fix compile error on HP-UX 63 - (dtucker) [configure.ac includes.h] Bug #947: Fix compile error on HP-UX
@@ -1833,4 +1837,4 @@
1833 - (djm) Trim deprecated options from INSTALL. Mention UsePAM 1837 - (djm) Trim deprecated options from INSTALL. Mention UsePAM
1834 - (djm) Fix quote handling in sftp; Patch from admorten AT umich.edu 1838 - (djm) Fix quote handling in sftp; Patch from admorten AT umich.edu
1835 1839
1836$Id: ChangeLog,v 1.3576 2004/11/05 09:38:03 dtucker Exp $ 1840$Id: ChangeLog,v 1.3577 2004/11/05 09:41:24 dtucker Exp $
diff --git a/bufaux.c b/bufaux.c
index bf148316d..cbe77d5ae 100644
--- a/bufaux.c
+++ b/bufaux.c
@@ -37,7 +37,7 @@
37 */ 37 */
38 38
39#include "includes.h" 39#include "includes.h"
40RCSID("$OpenBSD: bufaux.c,v 1.32 2004/02/23 15:12:46 markus Exp $"); 40RCSID("$OpenBSD: bufaux.c,v 1.33 2004/10/29 23:56:17 djm 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 */
52void 52int
53buffer_put_bignum(Buffer *buffer, const BIGNUM *value) 53buffer_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
81void
82buffer_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 */
80void 91int
81buffer_get_bignum(Buffer *buffer, BIGNUM *value) 92buffer_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
122void
123buffer_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 */
103void 132int
104buffer_put_bignum2(Buffer *buffer, const BIGNUM *value) 133buffer_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] = '\0';
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
133void 170void
134buffer_get_bignum2(Buffer *buffer, BIGNUM *value) 171buffer_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
177int
178buffer_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
201void
202buffer_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
212int
213buffer_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
151u_short 223u_short
152buffer_get_short(Buffer *buffer) 224buffer_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
234int
235buffer_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
160u_int 245u_int
161buffer_get_int(Buffer *buffer) 246buffer_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); 256int
166 return GET_32BIT(buf); 257buffer_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
169u_int64_t 267u_int64_t
170buffer_get_int64(Buffer *buffer) 268buffer_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 */
216void * 316void *
217buffer_get_string(Buffer *buffer, u_int *length_ptr) 317buffer_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
344void *
345buffer_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 */
258int 374int
375buffer_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
384int
259buffer_get_char(Buffer *buffer) 385buffer_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
diff --git a/bufaux.h b/bufaux.h
index 61c72e353..e30911ddc 100644
--- a/bufaux.h
+++ b/bufaux.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: bufaux.h,v 1.19 2003/11/10 16:23:41 jakob Exp $ */ 1/* $OpenBSD: bufaux.h,v 1.20 2004/10/29 23:56:17 djm Exp $ */
2 2
3/* 3/*
4 * Author: Tatu Ylonen <ylo@cs.hut.fi> 4 * Author: Tatu Ylonen <ylo@cs.hut.fi>
@@ -42,4 +42,14 @@ void buffer_put_cstring(Buffer *, const char *);
42#define buffer_skip_string(b) \ 42#define buffer_skip_string(b) \
43 do { u_int l = buffer_get_int(b); buffer_consume(b, l); } while(0) 43 do { u_int l = buffer_get_int(b); buffer_consume(b, l); } while(0)
44 44
45int buffer_put_bignum_ret(Buffer *, const BIGNUM *);
46int buffer_get_bignum_ret(Buffer *, BIGNUM *);
47int buffer_put_bignum2_ret(Buffer *, const BIGNUM *);
48int buffer_get_bignum2_ret(Buffer *, BIGNUM *);
49int buffer_get_short_ret(u_short *, Buffer *);
50int buffer_get_int_ret(u_int *, Buffer *);
51int buffer_get_int64_ret(u_int64_t *, Buffer *);
52void *buffer_get_string_ret(Buffer *, u_int *);
53int buffer_get_char_ret(char *, Buffer *);
54
45#endif /* BUFAUX_H */ 55#endif /* BUFAUX_H */
diff --git a/buffer.c b/buffer.c
index 9217cb269..1a25004ba 100644
--- a/buffer.c
+++ b/buffer.c
@@ -12,7 +12,7 @@
12 */ 12 */
13 13
14#include "includes.h" 14#include "includes.h"
15RCSID("$OpenBSD: buffer.c,v 1.21 2003/11/21 11:57:03 djm Exp $"); 15RCSID("$OpenBSD: buffer.c,v 1.22 2004/10/29 23:56:17 djm Exp $");
16 16
17#include "xmalloc.h" 17#include "xmalloc.h"
18#include "buffer.h" 18#include "buffer.h"
@@ -126,34 +126,62 @@ buffer_len(Buffer *buffer)
126 126
127/* Gets data from the beginning of the buffer. */ 127/* Gets data from the beginning of the buffer. */
128 128
129void 129int
130buffer_get(Buffer *buffer, void *buf, u_int len) 130buffer_get_ret(Buffer *buffer, void *buf, u_int len)
131{ 131{
132 if (len > buffer->end - buffer->offset) 132 if (len > buffer->end - buffer->offset) {
133 fatal("buffer_get: trying to get more bytes %d than in buffer %d", 133 error("buffer_get_ret: trying to get more bytes %d than in buffer %d",
134 len, buffer->end - buffer->offset); 134 len, buffer->end - buffer->offset);
135 return (-1);
136 }
135 memcpy(buf, buffer->buf + buffer->offset, len); 137 memcpy(buf, buffer->buf + buffer->offset, len);
136 buffer->offset += len; 138 buffer->offset += len;
139 return (0);
140}
141
142void
143buffer_get(Buffer *buffer, void *buf, u_int len)
144{
145 if (buffer_get_ret(buffer, buf, len) == -1)
146 fatal("buffer_get: buffer error");
137} 147}
138 148
139/* Consumes the given number of bytes from the beginning of the buffer. */ 149/* Consumes the given number of bytes from the beginning of the buffer. */
140 150
151int
152buffer_consume_ret(Buffer *buffer, u_int bytes)
153{
154 if (bytes > buffer->end - buffer->offset) {
155 error("buffer_consume_ret: trying to get more bytes than in buffer");
156 return (-1);
157 }
158 buffer->offset += bytes;
159 return (0);
160}
161
141void 162void
142buffer_consume(Buffer *buffer, u_int bytes) 163buffer_consume(Buffer *buffer, u_int bytes)
143{ 164{
144 if (bytes > buffer->end - buffer->offset) 165 if (buffer_consume_ret(buffer, bytes) == -1)
145 fatal("buffer_consume: trying to get more bytes than in buffer"); 166 fatal("buffer_consume: buffer error");
146 buffer->offset += bytes;
147} 167}
148 168
149/* Consumes the given number of bytes from the end of the buffer. */ 169/* Consumes the given number of bytes from the end of the buffer. */
150 170
171int
172buffer_consume_end_ret(Buffer *buffer, u_int bytes)
173{
174 if (bytes > buffer->end - buffer->offset)
175 return (-1);
176 buffer->end -= bytes;
177 return (0);
178}
179
151void 180void
152buffer_consume_end(Buffer *buffer, u_int bytes) 181buffer_consume_end(Buffer *buffer, u_int bytes)
153{ 182{
154 if (bytes > buffer->end - buffer->offset) 183 if (buffer_consume_end_ret(buffer, bytes) == -1)
155 fatal("buffer_consume_end: trying to get more bytes than in buffer"); 184 fatal("buffer_consume_end: trying to get more bytes than in buffer");
156 buffer->end -= bytes;
157} 185}
158 186
159/* Returns a pointer to the first used byte in the buffer. */ 187/* Returns a pointer to the first used byte in the buffer. */
diff --git a/buffer.h b/buffer.h
index 5e4c41244..9c09d4f43 100644
--- a/buffer.h
+++ b/buffer.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: buffer.h,v 1.11 2002/03/04 17:27:39 stevesk Exp $ */ 1/* $OpenBSD: buffer.h,v 1.12 2004/10/29 23:56:17 djm Exp $ */
2 2
3/* 3/*
4 * Author: Tatu Ylonen <ylo@cs.hut.fi> 4 * Author: Tatu Ylonen <ylo@cs.hut.fi>
@@ -40,4 +40,8 @@ void buffer_consume_end(Buffer *, u_int);
40 40
41void buffer_dump(Buffer *); 41void buffer_dump(Buffer *);
42 42
43int buffer_get_ret(Buffer *, void *, u_int);
44int buffer_consume_ret(Buffer *, u_int);
45int buffer_consume_end_ret(Buffer *, u_int);
46
43#endif /* BUFFER_H */ 47#endif /* BUFFER_H */