1 /* $OpenBSD: sshbuf-getput-basic.c,v 1.7 2017/06/01 04:51:58 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>
32 sshbuf_get(struct sshbuf *buf, void *v, size_t len)
34 const u_char *p = sshbuf_ptr(buf);
37 if ((r = sshbuf_consume(buf, len)) < 0)
39 if (v != NULL && len != 0)
45 sshbuf_get_u64(struct sshbuf *buf, u_int64_t *valp)
47 const u_char *p = sshbuf_ptr(buf);
50 if ((r = sshbuf_consume(buf, 8)) < 0)
58 sshbuf_get_u32(struct sshbuf *buf, u_int32_t *valp)
60 const u_char *p = sshbuf_ptr(buf);
63 if ((r = sshbuf_consume(buf, 4)) < 0)
71 sshbuf_get_u16(struct sshbuf *buf, u_int16_t *valp)
73 const u_char *p = sshbuf_ptr(buf);
76 if ((r = sshbuf_consume(buf, 2)) < 0)
84 sshbuf_get_u8(struct sshbuf *buf, u_char *valp)
86 const u_char *p = sshbuf_ptr(buf);
89 if ((r = sshbuf_consume(buf, 1)) < 0)
97 sshbuf_get_string(struct sshbuf *buf, u_char **valp, size_t *lenp)
107 if ((r = sshbuf_get_string_direct(buf, &val, &len)) < 0)
110 if ((*valp = malloc(len + 1)) == NULL) {
111 SSHBUF_DBG(("SSH_ERR_ALLOC_FAIL"));
112 return SSH_ERR_ALLOC_FAIL;
115 memcpy(*valp, val, len);
124 sshbuf_get_string_direct(struct sshbuf *buf, const u_char **valp, size_t *lenp)
134 if ((r = sshbuf_peek_string_direct(buf, &p, &len)) < 0)
140 if (sshbuf_consume(buf, len + 4) != 0) {
141 /* Shouldn't happen */
142 SSHBUF_DBG(("SSH_ERR_INTERNAL_ERROR"));
144 return SSH_ERR_INTERNAL_ERROR;
150 sshbuf_peek_string_direct(const struct sshbuf *buf, const u_char **valp,
154 const u_char *p = sshbuf_ptr(buf);
160 if (sshbuf_len(buf) < 4) {
161 SSHBUF_DBG(("SSH_ERR_MESSAGE_INCOMPLETE"));
162 return SSH_ERR_MESSAGE_INCOMPLETE;
165 if (len > SSHBUF_SIZE_MAX - 4) {
166 SSHBUF_DBG(("SSH_ERR_STRING_TOO_LARGE"));
167 return SSH_ERR_STRING_TOO_LARGE;
169 if (sshbuf_len(buf) - 4 < len) {
170 SSHBUF_DBG(("SSH_ERR_MESSAGE_INCOMPLETE"));
171 return SSH_ERR_MESSAGE_INCOMPLETE;
181 sshbuf_get_cstring(struct sshbuf *buf, char **valp, size_t *lenp)
191 if ((r = sshbuf_peek_string_direct(buf, &p, &len)) != 0)
193 /* Allow a \0 only at the end of the string */
195 (z = memchr(p , '\0', len)) != NULL && z < p + len - 1) {
196 SSHBUF_DBG(("SSH_ERR_INVALID_FORMAT"));
197 return SSH_ERR_INVALID_FORMAT;
199 if ((r = sshbuf_skip_string(buf)) != 0)
202 if ((*valp = malloc(len + 1)) == NULL) {
203 SSHBUF_DBG(("SSH_ERR_ALLOC_FAIL"));
204 return SSH_ERR_ALLOC_FAIL;
207 memcpy(*valp, p, len);
216 sshbuf_get_stringb(struct sshbuf *buf, struct sshbuf *v)
223 * Use sshbuf_peek_string_direct() to figure out if there is
224 * a complete string in 'buf' and copy the string directly
227 if ((r = sshbuf_peek_string_direct(buf, NULL, NULL)) != 0 ||
228 (r = sshbuf_get_u32(buf, &len)) != 0 ||
229 (r = sshbuf_reserve(v, len, &p)) != 0 ||
230 (r = sshbuf_get(buf, p, len)) != 0)
236 sshbuf_put(struct sshbuf *buf, const void *v, size_t len)
241 if ((r = sshbuf_reserve(buf, len, &p)) < 0)
249 sshbuf_putb(struct sshbuf *buf, const struct sshbuf *v)
251 return sshbuf_put(buf, sshbuf_ptr(v), sshbuf_len(v));
255 sshbuf_putf(struct sshbuf *buf, const char *fmt, ...)
261 r = sshbuf_putfv(buf, fmt, ap);
267 sshbuf_putfv(struct sshbuf *buf, const char *fmt, va_list ap)
274 if ((len = vsnprintf(NULL, 0, fmt, ap2)) < 0) {
275 r = SSH_ERR_INVALID_ARGUMENT;
280 goto out; /* Nothing to do */
284 if ((r = sshbuf_reserve(buf, (size_t)len + 1, &p)) < 0)
286 if ((r = vsnprintf((char *)p, len + 1, fmt, ap2)) != len) {
287 r = SSH_ERR_INTERNAL_ERROR;
288 goto out; /* Shouldn't happen */
290 /* Consume terminating \0 */
291 if ((r = sshbuf_consume_end(buf, 1)) != 0)
300 sshbuf_put_u64(struct sshbuf *buf, u_int64_t val)
305 if ((r = sshbuf_reserve(buf, 8, &p)) < 0)
312 sshbuf_put_u32(struct sshbuf *buf, u_int32_t val)
317 if ((r = sshbuf_reserve(buf, 4, &p)) < 0)
324 sshbuf_put_u16(struct sshbuf *buf, u_int16_t val)
329 if ((r = sshbuf_reserve(buf, 2, &p)) < 0)
336 sshbuf_put_u8(struct sshbuf *buf, u_char val)
341 if ((r = sshbuf_reserve(buf, 1, &p)) < 0)
348 sshbuf_put_string(struct sshbuf *buf, const void *v, size_t len)
353 if (len > SSHBUF_SIZE_MAX - 4) {
354 SSHBUF_DBG(("SSH_ERR_NO_BUFFER_SPACE"));
355 return SSH_ERR_NO_BUFFER_SPACE;
357 if ((r = sshbuf_reserve(buf, len + 4, &d)) < 0)
361 memcpy(d + 4, v, len);
366 sshbuf_put_cstring(struct sshbuf *buf, const char *v)
368 return sshbuf_put_string(buf, v, v == NULL ? 0 : strlen(v));
372 sshbuf_put_stringb(struct sshbuf *buf, const struct sshbuf *v)
374 return sshbuf_put_string(buf, sshbuf_ptr(v), sshbuf_len(v));
378 sshbuf_froms(struct sshbuf *buf, struct sshbuf **bufp)
385 if (buf == NULL || bufp == NULL)
386 return SSH_ERR_INVALID_ARGUMENT;
388 if ((r = sshbuf_peek_string_direct(buf, &p, &len)) != 0)
390 if ((ret = sshbuf_from(p, len)) == NULL)
391 return SSH_ERR_ALLOC_FAIL;
392 if ((r = sshbuf_consume(buf, len + 4)) != 0 || /* Shouldn't happen */
393 (r = sshbuf_set_parent(ret, buf)) != 0) {
402 sshbuf_put_bignum2_bytes(struct sshbuf *buf, const void *v, size_t len)
405 const u_char *s = (const u_char *)v;
408 if (len > SSHBUF_SIZE_MAX - 5) {
409 SSHBUF_DBG(("SSH_ERR_NO_BUFFER_SPACE"));
410 return SSH_ERR_NO_BUFFER_SPACE;
412 /* Skip leading zero bytes */
413 for (; len > 0 && *s == 0; len--, s++)
416 * If most significant bit is set then prepend a zero byte to
417 * avoid interpretation as a negative number.
419 prepend = len > 0 && (s[0] & 0x80) != 0;
420 if ((r = sshbuf_reserve(buf, len + 4 + prepend, &d)) < 0)
422 POKE_U32(d, len + prepend);
426 memcpy(d + 4 + prepend, s, len);
431 sshbuf_get_bignum2_bytes_direct(struct sshbuf *buf,
432 const u_char **valp, size_t *lenp)
438 if ((r = sshbuf_peek_string_direct(buf, &d, &olen)) < 0)
441 /* Refuse negative (MSB set) bignums */
442 if ((len != 0 && (*d & 0x80) != 0))
443 return SSH_ERR_BIGNUM_IS_NEGATIVE;
444 /* Refuse overlong bignums, allow prepended \0 to avoid MSB set */
445 if (len > SSHBUF_MAX_BIGNUM + 1 ||
446 (len == SSHBUF_MAX_BIGNUM + 1 && *d != 0))
447 return SSH_ERR_BIGNUM_TOO_LARGE;
448 /* Trim leading zeros */
449 while (len > 0 && *d == 0x00) {
457 if (sshbuf_consume(buf, olen + 4) != 0) {
458 /* Shouldn't happen */
459 SSHBUF_DBG(("SSH_ERR_INTERNAL_ERROR"));
461 return SSH_ERR_INTERNAL_ERROR;