1 /* $OpenBSD: sshbuf-getput-basic.c,v 1.13 2022/05/25 06:03:44 djm Exp $ */
3 * Copyright (c) 2011 Damien Miller
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.
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.
18 #define SSHBUF_INTERNAL
21 #include <sys/types.h>
35 sshbuf_get(struct sshbuf
*buf
, void *v
, size_t len
)
37 const u_char
*p
= sshbuf_ptr(buf
);
40 if ((r
= sshbuf_consume(buf
, len
)) < 0)
42 if (v
!= NULL
&& len
!= 0)
48 sshbuf_get_u64(struct sshbuf
*buf
, u_int64_t
*valp
)
50 const u_char
*p
= sshbuf_ptr(buf
);
53 if ((r
= sshbuf_consume(buf
, 8)) < 0)
61 sshbuf_get_u32(struct sshbuf
*buf
, u_int32_t
*valp
)
63 const u_char
*p
= sshbuf_ptr(buf
);
66 if ((r
= sshbuf_consume(buf
, 4)) < 0)
74 sshbuf_get_u16(struct sshbuf
*buf
, u_int16_t
*valp
)
76 const u_char
*p
= sshbuf_ptr(buf
);
79 if ((r
= sshbuf_consume(buf
, 2)) < 0)
87 sshbuf_get_u8(struct sshbuf
*buf
, u_char
*valp
)
89 const u_char
*p
= sshbuf_ptr(buf
);
92 if ((r
= sshbuf_consume(buf
, 1)) < 0)
100 check_offset(const struct sshbuf
*buf
, int wr
, size_t offset
, size_t len
)
102 if (sshbuf_ptr(buf
) == NULL
) /* calls sshbuf_check_sanity() */
103 return SSH_ERR_INTERNAL_ERROR
;
104 if (offset
>= SIZE_MAX
- len
)
105 return SSH_ERR_INVALID_ARGUMENT
;
106 if (offset
+ len
> sshbuf_len(buf
)) {
108 SSH_ERR_NO_BUFFER_SPACE
: SSH_ERR_MESSAGE_INCOMPLETE
;
114 check_roffset(const struct sshbuf
*buf
, size_t offset
, size_t len
,
120 if ((r
= check_offset(buf
, 0, offset
, len
)) != 0)
122 *p
= sshbuf_ptr(buf
) + offset
;
127 sshbuf_peek_u64(const struct sshbuf
*buf
, size_t offset
, u_int64_t
*valp
)
129 const u_char
*p
= NULL
;
134 if ((r
= check_roffset(buf
, offset
, 8, &p
)) != 0)
142 sshbuf_peek_u32(const struct sshbuf
*buf
, size_t offset
, u_int32_t
*valp
)
144 const u_char
*p
= NULL
;
149 if ((r
= check_roffset(buf
, offset
, 4, &p
)) != 0)
157 sshbuf_peek_u16(const struct sshbuf
*buf
, size_t offset
, u_int16_t
*valp
)
159 const u_char
*p
= NULL
;
164 if ((r
= check_roffset(buf
, offset
, 2, &p
)) != 0)
172 sshbuf_peek_u8(const struct sshbuf
*buf
, size_t offset
, u_char
*valp
)
174 const u_char
*p
= NULL
;
179 if ((r
= check_roffset(buf
, offset
, 1, &p
)) != 0)
187 sshbuf_get_string(struct sshbuf
*buf
, u_char
**valp
, size_t *lenp
)
197 if ((r
= sshbuf_get_string_direct(buf
, &val
, &len
)) < 0)
200 if ((*valp
= malloc(len
+ 1)) == NULL
) {
201 SSHBUF_DBG(("SSH_ERR_ALLOC_FAIL"));
202 return SSH_ERR_ALLOC_FAIL
;
205 memcpy(*valp
, val
, len
);
214 sshbuf_get_string_direct(struct sshbuf
*buf
, const u_char
**valp
, size_t *lenp
)
224 if ((r
= sshbuf_peek_string_direct(buf
, &p
, &len
)) < 0)
230 if (sshbuf_consume(buf
, len
+ 4) != 0) {
231 /* Shouldn't happen */
232 SSHBUF_DBG(("SSH_ERR_INTERNAL_ERROR"));
234 return SSH_ERR_INTERNAL_ERROR
;
240 sshbuf_peek_string_direct(const struct sshbuf
*buf
, const u_char
**valp
,
244 const u_char
*p
= sshbuf_ptr(buf
);
250 if (sshbuf_len(buf
) < 4) {
251 SSHBUF_DBG(("SSH_ERR_MESSAGE_INCOMPLETE"));
252 return SSH_ERR_MESSAGE_INCOMPLETE
;
255 if (len
> SSHBUF_SIZE_MAX
- 4) {
256 SSHBUF_DBG(("SSH_ERR_STRING_TOO_LARGE"));
257 return SSH_ERR_STRING_TOO_LARGE
;
259 if (sshbuf_len(buf
) - 4 < len
) {
260 SSHBUF_DBG(("SSH_ERR_MESSAGE_INCOMPLETE"));
261 return SSH_ERR_MESSAGE_INCOMPLETE
;
271 sshbuf_get_cstring(struct sshbuf
*buf
, char **valp
, size_t *lenp
)
281 if ((r
= sshbuf_peek_string_direct(buf
, &p
, &len
)) != 0)
283 /* Allow a \0 only at the end of the string */
285 (z
= memchr(p
, '\0', len
)) != NULL
&& z
< p
+ len
- 1) {
286 SSHBUF_DBG(("SSH_ERR_INVALID_FORMAT"));
287 return SSH_ERR_INVALID_FORMAT
;
289 if ((r
= sshbuf_skip_string(buf
)) != 0)
292 if ((*valp
= malloc(len
+ 1)) == NULL
) {
293 SSHBUF_DBG(("SSH_ERR_ALLOC_FAIL"));
294 return SSH_ERR_ALLOC_FAIL
;
297 memcpy(*valp
, p
, len
);
306 sshbuf_get_stringb(struct sshbuf
*buf
, struct sshbuf
*v
)
313 * Use sshbuf_peek_string_direct() to figure out if there is
314 * a complete string in 'buf' and copy the string directly
317 if ((r
= sshbuf_peek_string_direct(buf
, NULL
, NULL
)) != 0 ||
318 (r
= sshbuf_get_u32(buf
, &len
)) != 0 ||
319 (r
= sshbuf_reserve(v
, len
, &p
)) != 0 ||
320 (r
= sshbuf_get(buf
, p
, len
)) != 0)
326 sshbuf_put(struct sshbuf
*buf
, const void *v
, size_t len
)
331 if ((r
= sshbuf_reserve(buf
, len
, &p
)) < 0)
339 sshbuf_putb(struct sshbuf
*buf
, const struct sshbuf
*v
)
343 return sshbuf_put(buf
, sshbuf_ptr(v
), sshbuf_len(v
));
347 sshbuf_putf(struct sshbuf
*buf
, const char *fmt
, ...)
353 r
= sshbuf_putfv(buf
, fmt
, ap
);
359 sshbuf_putfv(struct sshbuf
*buf
, const char *fmt
, va_list ap
)
366 if ((len
= vsnprintf(NULL
, 0, fmt
, ap2
)) < 0) {
367 r
= SSH_ERR_INVALID_ARGUMENT
;
372 goto out
; /* Nothing to do */
376 if ((r
= sshbuf_reserve(buf
, (size_t)len
+ 1, &p
)) < 0)
378 if ((r
= vsnprintf((char *)p
, len
+ 1, fmt
, ap2
)) != len
) {
379 r
= SSH_ERR_INTERNAL_ERROR
;
380 goto out
; /* Shouldn't happen */
382 /* Consume terminating \0 */
383 if ((r
= sshbuf_consume_end(buf
, 1)) != 0)
392 sshbuf_put_u64(struct sshbuf
*buf
, u_int64_t val
)
397 if ((r
= sshbuf_reserve(buf
, 8, &p
)) < 0)
404 sshbuf_put_u32(struct sshbuf
*buf
, u_int32_t val
)
409 if ((r
= sshbuf_reserve(buf
, 4, &p
)) < 0)
416 sshbuf_put_u16(struct sshbuf
*buf
, u_int16_t val
)
421 if ((r
= sshbuf_reserve(buf
, 2, &p
)) < 0)
428 sshbuf_put_u8(struct sshbuf
*buf
, u_char val
)
433 if ((r
= sshbuf_reserve(buf
, 1, &p
)) < 0)
440 check_woffset(struct sshbuf
*buf
, size_t offset
, size_t len
, u_char
**p
)
445 if ((r
= check_offset(buf
, 1, offset
, len
)) != 0)
447 if (sshbuf_mutable_ptr(buf
) == NULL
)
448 return SSH_ERR_BUFFER_READ_ONLY
;
449 *p
= sshbuf_mutable_ptr(buf
) + offset
;
454 sshbuf_poke_u64(struct sshbuf
*buf
, size_t offset
, u_int64_t val
)
459 if ((r
= check_woffset(buf
, offset
, 8, &p
)) != 0)
466 sshbuf_poke_u32(struct sshbuf
*buf
, size_t offset
, u_int32_t val
)
471 if ((r
= check_woffset(buf
, offset
, 4, &p
)) != 0)
478 sshbuf_poke_u16(struct sshbuf
*buf
, size_t offset
, u_int16_t val
)
483 if ((r
= check_woffset(buf
, offset
, 2, &p
)) != 0)
490 sshbuf_poke_u8(struct sshbuf
*buf
, size_t offset
, u_char val
)
495 if ((r
= check_woffset(buf
, offset
, 1, &p
)) != 0)
502 sshbuf_poke(struct sshbuf
*buf
, size_t offset
, void *v
, size_t len
)
507 if ((r
= check_woffset(buf
, offset
, len
, &p
)) != 0)
514 sshbuf_put_string(struct sshbuf
*buf
, const void *v
, size_t len
)
519 if (len
> SSHBUF_SIZE_MAX
- 4) {
520 SSHBUF_DBG(("SSH_ERR_NO_BUFFER_SPACE"));
521 return SSH_ERR_NO_BUFFER_SPACE
;
523 if ((r
= sshbuf_reserve(buf
, len
+ 4, &d
)) < 0)
527 memcpy(d
+ 4, v
, len
);
532 sshbuf_put_cstring(struct sshbuf
*buf
, const char *v
)
534 return sshbuf_put_string(buf
, v
, v
== NULL
? 0 : strlen(v
));
538 sshbuf_put_stringb(struct sshbuf
*buf
, const struct sshbuf
*v
)
541 return sshbuf_put_string(buf
, NULL
, 0);
543 return sshbuf_put_string(buf
, sshbuf_ptr(v
), sshbuf_len(v
));
547 sshbuf_froms(struct sshbuf
*buf
, struct sshbuf
**bufp
)
554 if (buf
== NULL
|| bufp
== NULL
)
555 return SSH_ERR_INVALID_ARGUMENT
;
557 if ((r
= sshbuf_peek_string_direct(buf
, &p
, &len
)) != 0)
559 if ((ret
= sshbuf_from(p
, len
)) == NULL
)
560 return SSH_ERR_ALLOC_FAIL
;
561 if ((r
= sshbuf_consume(buf
, len
+ 4)) != 0 || /* Shouldn't happen */
562 (r
= sshbuf_set_parent(ret
, buf
)) != 0) {
571 sshbuf_put_bignum2_bytes(struct sshbuf
*buf
, const void *v
, size_t len
)
574 const u_char
*s
= (const u_char
*)v
;
577 if (len
> SSHBUF_SIZE_MAX
- 5) {
578 SSHBUF_DBG(("SSH_ERR_NO_BUFFER_SPACE"));
579 return SSH_ERR_NO_BUFFER_SPACE
;
581 /* Skip leading zero bytes */
582 for (; len
> 0 && *s
== 0; len
--, s
++)
585 * If most significant bit is set then prepend a zero byte to
586 * avoid interpretation as a negative number.
588 prepend
= len
> 0 && (s
[0] & 0x80) != 0;
589 if ((r
= sshbuf_reserve(buf
, len
+ 4 + prepend
, &d
)) < 0)
591 POKE_U32(d
, len
+ prepend
);
595 memcpy(d
+ 4 + prepend
, s
, len
);
600 sshbuf_get_bignum2_bytes_direct(struct sshbuf
*buf
,
601 const u_char
**valp
, size_t *lenp
)
607 if ((r
= sshbuf_peek_string_direct(buf
, &d
, &olen
)) < 0)
610 /* Refuse negative (MSB set) bignums */
611 if ((len
!= 0 && (*d
& 0x80) != 0))
612 return SSH_ERR_BIGNUM_IS_NEGATIVE
;
613 /* Refuse overlong bignums, allow prepended \0 to avoid MSB set */
614 if (len
> SSHBUF_MAX_BIGNUM
+ 1 ||
615 (len
== SSHBUF_MAX_BIGNUM
+ 1 && *d
!= 0))
616 return SSH_ERR_BIGNUM_TOO_LARGE
;
617 /* Trim leading zeros */
618 while (len
> 0 && *d
== 0x00) {
626 if (sshbuf_consume(buf
, olen
+ 4) != 0) {
627 /* Shouldn't happen */
628 SSHBUF_DBG(("SSH_ERR_INTERNAL_ERROR"));
630 return SSH_ERR_INTERNAL_ERROR
;