xref: /netbsd-src/crypto/external/bsd/openssh/dist/sshbuf-getput-basic.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /*	$OpenBSD: sshbuf-getput-basic.c,v 1.10 2019/12/13 19:09:37 djm Exp $	*/
2 /*
3  * Copyright (c) 2011 Damien Miller
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 #include "includes.h"
18 __RCSID("$NetBSD: sshbuf-getput-basic.c,v 1.10 2020/02/27 00:24:40 christos Exp $");
19 
20 #include <sys/types.h>
21 
22 #include <stdarg.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdint.h>
27 
28 #include "ssherr.h"
29 #define SSHBUF_INTERNAL
30 #include "sshbuf.h"
31 
32 int
33 sshbuf_get(struct sshbuf *buf, void *v, size_t len)
34 {
35 	const u_char *p = sshbuf_ptr(buf);
36 	int r;
37 
38 	if ((r = sshbuf_consume(buf, len)) < 0)
39 		return r;
40 	if (v != NULL && len != 0)
41 		memcpy(v, p, len);
42 	return 0;
43 }
44 
45 int
46 sshbuf_get_u64(struct sshbuf *buf, u_int64_t *valp)
47 {
48 	const u_char *p = sshbuf_ptr(buf);
49 	int r;
50 
51 	if ((r = sshbuf_consume(buf, 8)) < 0)
52 		return r;
53 	if (valp != NULL)
54 		*valp = PEEK_U64(p);
55 	return 0;
56 }
57 
58 int
59 sshbuf_get_u32(struct sshbuf *buf, u_int32_t *valp)
60 {
61 	const u_char *p = sshbuf_ptr(buf);
62 	int r;
63 
64 	if ((r = sshbuf_consume(buf, 4)) < 0)
65 		return r;
66 	if (valp != NULL)
67 		*valp = PEEK_U32(p);
68 	return 0;
69 }
70 
71 int
72 sshbuf_get_u16(struct sshbuf *buf, u_int16_t *valp)
73 {
74 	const u_char *p = sshbuf_ptr(buf);
75 	int r;
76 
77 	if ((r = sshbuf_consume(buf, 2)) < 0)
78 		return r;
79 	if (valp != NULL)
80 		*valp = PEEK_U16(p);
81 	return 0;
82 }
83 
84 int
85 sshbuf_get_u8(struct sshbuf *buf, u_char *valp)
86 {
87 	const u_char *p = sshbuf_ptr(buf);
88 	int r;
89 
90 	if ((r = sshbuf_consume(buf, 1)) < 0)
91 		return r;
92 	if (valp != NULL)
93 		*valp = (u_int8_t)*p;
94 	return 0;
95 }
96 
97 static int
98 check_offset(const struct sshbuf *buf, int wr, size_t offset, size_t len)
99 {
100 	if (sshbuf_ptr(buf) == NULL) /* calls sshbuf_check_sanity() */
101 		return SSH_ERR_INTERNAL_ERROR;
102 	if (offset >= SIZE_MAX - len)
103 		return SSH_ERR_INVALID_ARGUMENT;
104 	if (offset + len > sshbuf_len(buf)) {
105 		return wr ?
106 		    SSH_ERR_NO_BUFFER_SPACE : SSH_ERR_MESSAGE_INCOMPLETE;
107 	}
108 	return 0;
109 }
110 
111 static int
112 check_roffset(const struct sshbuf *buf, size_t offset, size_t len,
113     const u_char **p)
114 {
115 	int r;
116 
117 	*p = NULL;
118 	if ((r = check_offset(buf, 0, offset, len)) != 0)
119 		return r;
120 	*p = sshbuf_ptr(buf) + offset;
121 	return 0;
122 }
123 
124 int
125 sshbuf_peek_u64(const struct sshbuf *buf, size_t offset, u_int64_t *valp)
126 {
127 	const u_char *p = NULL;
128 	int r;
129 
130 	if (valp != NULL)
131 		*valp = 0;
132 	if ((r = check_roffset(buf, offset, 8, &p)) != 0)
133 		return r;
134 	if (valp != NULL)
135 		*valp = PEEK_U64(p);
136 	return 0;
137 }
138 
139 int
140 sshbuf_peek_u32(const struct sshbuf *buf, size_t offset, u_int32_t *valp)
141 {
142 	const u_char *p = NULL;
143 	int r;
144 
145 	if (valp != NULL)
146 		*valp = 0;
147 	if ((r = check_roffset(buf, offset, 4, &p)) != 0)
148 		return r;
149 	if (valp != NULL)
150 		*valp = PEEK_U32(p);
151 	return 0;
152 }
153 
154 int
155 sshbuf_peek_u16(const struct sshbuf *buf, size_t offset, u_int16_t *valp)
156 {
157 	const u_char *p = NULL;
158 	int r;
159 
160 	if (valp != NULL)
161 		*valp = 0;
162 	if ((r = check_roffset(buf, offset, 2, &p)) != 0)
163 		return r;
164 	if (valp != NULL)
165 		*valp = PEEK_U16(p);
166 	return 0;
167 }
168 
169 int
170 sshbuf_peek_u8(const struct sshbuf *buf, size_t offset, u_char *valp)
171 {
172 	const u_char *p = NULL;
173 	int r;
174 
175 	if (valp != NULL)
176 		*valp = 0;
177 	if ((r = check_roffset(buf, offset, 1, &p)) != 0)
178 		return r;
179 	if (valp != NULL)
180 		*valp = *p;
181 	return 0;
182 }
183 
184 int
185 sshbuf_get_string(struct sshbuf *buf, u_char **valp, size_t *lenp)
186 {
187 	const u_char *val;
188 	size_t len;
189 	int r;
190 
191 	if (valp != NULL)
192 		*valp = NULL;
193 	if (lenp != NULL)
194 		*lenp = 0;
195 	if ((r = sshbuf_get_string_direct(buf, &val, &len)) < 0)
196 		return r;
197 	if (valp != NULL) {
198 		if ((*valp = malloc(len + 1)) == NULL) {
199 			SSHBUF_DBG(("SSH_ERR_ALLOC_FAIL"));
200 			return SSH_ERR_ALLOC_FAIL;
201 		}
202 		if (len != 0)
203 			memcpy(*valp, val, len);
204 		(*valp)[len] = '\0';
205 	}
206 	if (lenp != NULL)
207 		*lenp = len;
208 	return 0;
209 }
210 
211 int
212 sshbuf_get_string_direct(struct sshbuf *buf, const u_char **valp, size_t *lenp)
213 {
214 	size_t len;
215 	const u_char *p;
216 	int r;
217 
218 	if (valp != NULL)
219 		*valp = NULL;
220 	if (lenp != NULL)
221 		*lenp = 0;
222 	if ((r = sshbuf_peek_string_direct(buf, &p, &len)) < 0)
223 		return r;
224 	if (valp != NULL)
225 		*valp = p;
226 	if (lenp != NULL)
227 		*lenp = len;
228 	if (sshbuf_consume(buf, len + 4) != 0) {
229 		/* Shouldn't happen */
230 		SSHBUF_DBG(("SSH_ERR_INTERNAL_ERROR"));
231 		SSHBUF_ABORT();
232 		return SSH_ERR_INTERNAL_ERROR;
233 	}
234 	return 0;
235 }
236 
237 int
238 sshbuf_peek_string_direct(const struct sshbuf *buf, const u_char **valp,
239     size_t *lenp)
240 {
241 	u_int32_t len;
242 	const u_char *p = sshbuf_ptr(buf);
243 
244 	if (valp != NULL)
245 		*valp = NULL;
246 	if (lenp != NULL)
247 		*lenp = 0;
248 	if (sshbuf_len(buf) < 4) {
249 		SSHBUF_DBG(("SSH_ERR_MESSAGE_INCOMPLETE"));
250 		return SSH_ERR_MESSAGE_INCOMPLETE;
251 	}
252 	len = PEEK_U32(p);
253 	if (len > SSHBUF_SIZE_MAX - 4) {
254 		SSHBUF_DBG(("SSH_ERR_STRING_TOO_LARGE"));
255 		return SSH_ERR_STRING_TOO_LARGE;
256 	}
257 	if (sshbuf_len(buf) - 4 < len) {
258 		SSHBUF_DBG(("SSH_ERR_MESSAGE_INCOMPLETE"));
259 		return SSH_ERR_MESSAGE_INCOMPLETE;
260 	}
261 	if (valp != NULL)
262 		*valp = p + 4;
263 	if (lenp != NULL)
264 		*lenp = len;
265 	return 0;
266 }
267 
268 int
269 sshbuf_get_cstring(struct sshbuf *buf, char **valp, size_t *lenp)
270 {
271 	size_t len;
272 	const u_char *p, *z;
273 	int r;
274 
275 	if (valp != NULL)
276 		*valp = NULL;
277 	if (lenp != NULL)
278 		*lenp = 0;
279 	if ((r = sshbuf_peek_string_direct(buf, &p, &len)) != 0)
280 		return r;
281 	/* Allow a \0 only at the end of the string */
282 	if (len > 0 &&
283 	    (z = memchr(p , '\0', len)) != NULL && z < p + len - 1) {
284 		SSHBUF_DBG(("SSH_ERR_INVALID_FORMAT"));
285 		return SSH_ERR_INVALID_FORMAT;
286 	}
287 	if ((r = sshbuf_skip_string(buf)) != 0)
288 		return -1;
289 	if (valp != NULL) {
290 		if ((*valp = malloc(len + 1)) == NULL) {
291 			SSHBUF_DBG(("SSH_ERR_ALLOC_FAIL"));
292 			return SSH_ERR_ALLOC_FAIL;
293 		}
294 		if (len != 0)
295 			memcpy(*valp, p, len);
296 		(*valp)[len] = '\0';
297 	}
298 	if (lenp != NULL)
299 		*lenp = (size_t)len;
300 	return 0;
301 }
302 
303 int
304 sshbuf_get_stringb(struct sshbuf *buf, struct sshbuf *v)
305 {
306 	u_int32_t len;
307 	u_char *p;
308 	int r;
309 
310 	/*
311 	 * Use sshbuf_peek_string_direct() to figure out if there is
312 	 * a complete string in 'buf' and copy the string directly
313 	 * into 'v'.
314 	 */
315 	if ((r = sshbuf_peek_string_direct(buf, NULL, NULL)) != 0 ||
316 	    (r = sshbuf_get_u32(buf, &len)) != 0 ||
317 	    (r = sshbuf_reserve(v, len, &p)) != 0 ||
318 	    (r = sshbuf_get(buf, p, len)) != 0)
319 		return r;
320 	return 0;
321 }
322 
323 int
324 sshbuf_put(struct sshbuf *buf, const void *v, size_t len)
325 {
326 	u_char *p;
327 	int r;
328 
329 	if ((r = sshbuf_reserve(buf, len, &p)) < 0)
330 		return r;
331 	if (len != 0)
332 		memcpy(p, v, len);
333 	return 0;
334 }
335 
336 int
337 sshbuf_putb(struct sshbuf *buf, const struct sshbuf *v)
338 {
339 	return sshbuf_put(buf, sshbuf_ptr(v), sshbuf_len(v));
340 }
341 
342 int
343 sshbuf_putf(struct sshbuf *buf, const char *fmt, ...)
344 {
345 	va_list ap;
346 	int r;
347 
348 	va_start(ap, fmt);
349 	r = sshbuf_putfv(buf, fmt, ap);
350 	va_end(ap);
351 	return r;
352 }
353 
354 int
355 sshbuf_putfv(struct sshbuf *buf, const char *fmt, va_list ap)
356 {
357 	va_list ap2;
358 	int r, len;
359 	u_char *p;
360 
361 	va_copy(ap2, ap);
362 	if ((len = vsnprintf(NULL, 0, fmt, ap2)) < 0) {
363 		r = SSH_ERR_INVALID_ARGUMENT;
364 		goto out;
365 	}
366 	if (len == 0) {
367 		r = 0;
368 		goto out; /* Nothing to do */
369 	}
370 	va_end(ap2);
371 	va_copy(ap2, ap);
372 	if ((r = sshbuf_reserve(buf, (size_t)len + 1, &p)) < 0)
373 		goto out;
374 	if ((r = vsnprintf((char *)p, len + 1, fmt, ap2)) != len) {
375 		r = SSH_ERR_INTERNAL_ERROR;
376 		goto out; /* Shouldn't happen */
377 	}
378 	/* Consume terminating \0 */
379 	if ((r = sshbuf_consume_end(buf, 1)) != 0)
380 		goto out;
381 	r = 0;
382  out:
383 	va_end(ap2);
384 	return r;
385 }
386 
387 int
388 sshbuf_put_u64(struct sshbuf *buf, u_int64_t val)
389 {
390 	u_char *p;
391 	int r;
392 
393 	if ((r = sshbuf_reserve(buf, 8, &p)) < 0)
394 		return r;
395 	POKE_U64(p, val);
396 	return 0;
397 }
398 
399 int
400 sshbuf_put_u32(struct sshbuf *buf, u_int32_t val)
401 {
402 	u_char *p;
403 	int r;
404 
405 	if ((r = sshbuf_reserve(buf, 4, &p)) < 0)
406 		return r;
407 	POKE_U32(p, val);
408 	return 0;
409 }
410 
411 int
412 sshbuf_put_u16(struct sshbuf *buf, u_int16_t val)
413 {
414 	u_char *p;
415 	int r;
416 
417 	if ((r = sshbuf_reserve(buf, 2, &p)) < 0)
418 		return r;
419 	POKE_U16(p, val);
420 	return 0;
421 }
422 
423 int
424 sshbuf_put_u8(struct sshbuf *buf, u_char val)
425 {
426 	u_char *p;
427 	int r;
428 
429 	if ((r = sshbuf_reserve(buf, 1, &p)) < 0)
430 		return r;
431 	p[0] = val;
432 	return 0;
433 }
434 
435 static int
436 check_woffset(struct sshbuf *buf, size_t offset, size_t len, u_char **p)
437 {
438 	int r;
439 
440 	*p = NULL;
441 	if ((r = check_offset(buf, 1, offset, len)) != 0)
442 		return r;
443 	if (sshbuf_mutable_ptr(buf) == NULL)
444 		return SSH_ERR_BUFFER_READ_ONLY;
445 	*p = sshbuf_mutable_ptr(buf) + offset;
446 	return 0;
447 }
448 
449 int
450 sshbuf_poke_u64(struct sshbuf *buf, size_t offset, u_int64_t val)
451 {
452 	u_char *p = NULL;
453 	int r;
454 
455 	if ((r = check_woffset(buf, offset, 8, &p)) != 0)
456 		return r;
457 	POKE_U64(p, val);
458 	return 0;
459 }
460 
461 int
462 sshbuf_poke_u32(struct sshbuf *buf, size_t offset, u_int32_t val)
463 {
464 	u_char *p = NULL;
465 	int r;
466 
467 	if ((r = check_woffset(buf, offset, 4, &p)) != 0)
468 		return r;
469 	POKE_U32(p, val);
470 	return 0;
471 }
472 
473 int
474 sshbuf_poke_u16(struct sshbuf *buf, size_t offset, u_int16_t val)
475 {
476 	u_char *p = NULL;
477 	int r;
478 
479 	if ((r = check_woffset(buf, offset, 2, &p)) != 0)
480 		return r;
481 	POKE_U16(p, val);
482 	return 0;
483 }
484 
485 int
486 sshbuf_poke_u8(struct sshbuf *buf, size_t offset, u_char val)
487 {
488 	u_char *p = NULL;
489 	int r;
490 
491 	if ((r = check_woffset(buf, offset, 1, &p)) != 0)
492 		return r;
493 	*p = val;
494 	return 0;
495 }
496 
497 int
498 sshbuf_poke(struct sshbuf *buf, size_t offset, void *v, size_t len)
499 {
500 	u_char *p = NULL;
501 	int r;
502 
503 	if ((r = check_woffset(buf, offset, len, &p)) != 0)
504 		return r;
505 	memcpy(p, v, len);
506 	return 0;
507 }
508 
509 int
510 sshbuf_put_string(struct sshbuf *buf, const void *v, size_t len)
511 {
512 	u_char *d;
513 	int r;
514 
515 	if (len > SSHBUF_SIZE_MAX - 4) {
516 		SSHBUF_DBG(("SSH_ERR_NO_BUFFER_SPACE"));
517 		return SSH_ERR_NO_BUFFER_SPACE;
518 	}
519 	if ((r = sshbuf_reserve(buf, len + 4, &d)) < 0)
520 		return r;
521 	POKE_U32(d, len);
522 	if (len != 0)
523 		memcpy(d + 4, v, len);
524 	return 0;
525 }
526 
527 int
528 sshbuf_put_cstring(struct sshbuf *buf, const char *v)
529 {
530 	return sshbuf_put_string(buf, __UNCONST(v), v == NULL ? 0 : strlen(v));
531 }
532 
533 int
534 sshbuf_put_stringb(struct sshbuf *buf, const struct sshbuf *v)
535 {
536 	if (v == NULL)
537 		return sshbuf_put_string(buf, NULL, 0);
538 
539 	return sshbuf_put_string(buf, sshbuf_ptr(v), sshbuf_len(v));
540 }
541 
542 int
543 sshbuf_froms(struct sshbuf *buf, struct sshbuf **bufp)
544 {
545 	const u_char *p;
546 	size_t len;
547 	struct sshbuf *ret;
548 	int r;
549 
550 	if (buf == NULL || bufp == NULL)
551 		return SSH_ERR_INVALID_ARGUMENT;
552 	*bufp = NULL;
553 	if ((r = sshbuf_peek_string_direct(buf, &p, &len)) != 0)
554 		return r;
555 	if ((ret = sshbuf_from(p, len)) == NULL)
556 		return SSH_ERR_ALLOC_FAIL;
557 	if ((r = sshbuf_consume(buf, len + 4)) != 0 ||  /* Shouldn't happen */
558 	    (r = sshbuf_set_parent(ret, buf)) != 0) {
559 		sshbuf_free(ret);
560 		return r;
561 	}
562 	*bufp = ret;
563 	return 0;
564 }
565 
566 int
567 sshbuf_put_bignum2_bytes(struct sshbuf *buf, const void *v, size_t len)
568 {
569 	u_char *d;
570 	const u_char *s = (const u_char *)v;
571 	int r, prepend;
572 
573 	if (len > SSHBUF_SIZE_MAX - 5) {
574 		SSHBUF_DBG(("SSH_ERR_NO_BUFFER_SPACE"));
575 		return SSH_ERR_NO_BUFFER_SPACE;
576 	}
577 	/* Skip leading zero bytes */
578 	for (; len > 0 && *s == 0; len--, s++)
579 		;
580 	/*
581 	 * If most significant bit is set then prepend a zero byte to
582 	 * avoid interpretation as a negative number.
583 	 */
584 	prepend = len > 0 && (s[0] & 0x80) != 0;
585 	if ((r = sshbuf_reserve(buf, len + 4 + prepend, &d)) < 0)
586 		return r;
587 	POKE_U32(d, len + prepend);
588 	if (prepend)
589 		d[4] = 0;
590 	if (len != 0)
591 		memcpy(d + 4 + prepend, s, len);
592 	return 0;
593 }
594 
595 int
596 sshbuf_get_bignum2_bytes_direct(struct sshbuf *buf,
597     const u_char **valp, size_t *lenp)
598 {
599 	const u_char *d;
600 	size_t len, olen;
601 	int r;
602 
603 	if ((r = sshbuf_peek_string_direct(buf, &d, &olen)) < 0)
604 		return r;
605 	len = olen;
606 	/* Refuse negative (MSB set) bignums */
607 	if ((len != 0 && (*d & 0x80) != 0))
608 		return SSH_ERR_BIGNUM_IS_NEGATIVE;
609 	/* Refuse overlong bignums, allow prepended \0 to avoid MSB set */
610 	if (len > SSHBUF_MAX_BIGNUM + 1 ||
611 	    (len == SSHBUF_MAX_BIGNUM + 1 && *d != 0))
612 		return SSH_ERR_BIGNUM_TOO_LARGE;
613 	/* Trim leading zeros */
614 	while (len > 0 && *d == 0x00) {
615 		d++;
616 		len--;
617 	}
618 	if (valp != NULL)
619 		*valp = d;
620 	if (lenp != NULL)
621 		*lenp = len;
622 	if (sshbuf_consume(buf, olen + 4) != 0) {
623 		/* Shouldn't happen */
624 		SSHBUF_DBG(("SSH_ERR_INTERNAL_ERROR"));
625 		SSHBUF_ABORT();
626 		return SSH_ERR_INTERNAL_ERROR;
627 	}
628 	return 0;
629 }
630