1 /* $OpenBSD: sshbuf-getput-basic.c,v 1.5 2015/10/20 23:24:25 mmcc 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>
30 sshbuf_get(struct sshbuf *buf, void *v, size_t len)
32 const u_char *p = sshbuf_ptr(buf);
35 if ((r = sshbuf_consume(buf, len)) < 0)
37 if (v != NULL && len != 0)
43 sshbuf_get_u64(struct sshbuf *buf, u_int64_t *valp)
45 const u_char *p = sshbuf_ptr(buf);
48 if ((r = sshbuf_consume(buf, 8)) < 0)
56 sshbuf_get_u32(struct sshbuf *buf, u_int32_t *valp)
58 const u_char *p = sshbuf_ptr(buf);
61 if ((r = sshbuf_consume(buf, 4)) < 0)
69 sshbuf_get_u16(struct sshbuf *buf, u_int16_t *valp)
71 const u_char *p = sshbuf_ptr(buf);
74 if ((r = sshbuf_consume(buf, 2)) < 0)
82 sshbuf_get_u8(struct sshbuf *buf, u_char *valp)
84 const u_char *p = sshbuf_ptr(buf);
87 if ((r = sshbuf_consume(buf, 1)) < 0)
95 sshbuf_get_string(struct sshbuf *buf, u_char **valp, size_t *lenp)
105 if ((r = sshbuf_get_string_direct(buf, &val, &len)) < 0)
108 if ((*valp = malloc(len + 1)) == NULL) {
109 SSHBUF_DBG(("SSH_ERR_ALLOC_FAIL"));
110 return SSH_ERR_ALLOC_FAIL;
113 memcpy(*valp, val, len);
122 sshbuf_get_string_direct(struct sshbuf *buf, const u_char **valp, size_t *lenp)
132 if ((r = sshbuf_peek_string_direct(buf, &p, &len)) < 0)
138 if (sshbuf_consume(buf, len + 4) != 0) {
139 /* Shouldn't happen */
140 SSHBUF_DBG(("SSH_ERR_INTERNAL_ERROR"));
142 return SSH_ERR_INTERNAL_ERROR;
148 sshbuf_peek_string_direct(const struct sshbuf *buf, const u_char **valp,
152 const u_char *p = sshbuf_ptr(buf);
158 if (sshbuf_len(buf) < 4) {
159 SSHBUF_DBG(("SSH_ERR_MESSAGE_INCOMPLETE"));
160 return SSH_ERR_MESSAGE_INCOMPLETE;
163 if (len > SSHBUF_SIZE_MAX - 4) {
164 SSHBUF_DBG(("SSH_ERR_STRING_TOO_LARGE"));
165 return SSH_ERR_STRING_TOO_LARGE;
167 if (sshbuf_len(buf) - 4 < len) {
168 SSHBUF_DBG(("SSH_ERR_MESSAGE_INCOMPLETE"));
169 return SSH_ERR_MESSAGE_INCOMPLETE;
179 sshbuf_get_cstring(struct sshbuf *buf, char **valp, size_t *lenp)
189 if ((r = sshbuf_peek_string_direct(buf, &p, &len)) != 0)
191 /* Allow a \0 only at the end of the string */
193 (z = memchr(p , '\0', len)) != NULL && z < p + len - 1) {
194 SSHBUF_DBG(("SSH_ERR_INVALID_FORMAT"));
195 return SSH_ERR_INVALID_FORMAT;
197 if ((r = sshbuf_skip_string(buf)) != 0)
200 if ((*valp = malloc(len + 1)) == NULL) {
201 SSHBUF_DBG(("SSH_ERR_ALLOC_FAIL"));
202 return SSH_ERR_ALLOC_FAIL;
205 memcpy(*valp, p, len);
214 sshbuf_get_stringb(struct sshbuf *buf, struct sshbuf *v)
221 * Use sshbuf_peek_string_direct() to figure out if there is
222 * a complete string in 'buf' and copy the string directly
225 if ((r = sshbuf_peek_string_direct(buf, NULL, NULL)) != 0 ||
226 (r = sshbuf_get_u32(buf, &len)) != 0 ||
227 (r = sshbuf_reserve(v, len, &p)) != 0 ||
228 (r = sshbuf_get(buf, p, len)) != 0)
234 sshbuf_put(struct sshbuf *buf, const void *v, size_t len)
239 if ((r = sshbuf_reserve(buf, len, &p)) < 0)
247 sshbuf_putb(struct sshbuf *buf, const struct sshbuf *v)
249 return sshbuf_put(buf, sshbuf_ptr(v), sshbuf_len(v));
253 sshbuf_putf(struct sshbuf *buf, const char *fmt, ...)
259 r = sshbuf_putfv(buf, fmt, ap);
265 sshbuf_putfv(struct sshbuf *buf, const char *fmt, va_list ap)
272 if ((len = vsnprintf(NULL, 0, fmt, ap2)) < 0) {
273 r = SSH_ERR_INVALID_ARGUMENT;
278 goto out; /* Nothing to do */
282 if ((r = sshbuf_reserve(buf, (size_t)len + 1, &p)) < 0)
284 if ((r = vsnprintf((char *)p, len + 1, fmt, ap2)) != len) {
285 r = SSH_ERR_INTERNAL_ERROR;
286 goto out; /* Shouldn't happen */
288 /* Consume terminating \0 */
289 if ((r = sshbuf_consume_end(buf, 1)) != 0)
298 sshbuf_put_u64(struct sshbuf *buf, u_int64_t val)
303 if ((r = sshbuf_reserve(buf, 8, &p)) < 0)
310 sshbuf_put_u32(struct sshbuf *buf, u_int32_t val)
315 if ((r = sshbuf_reserve(buf, 4, &p)) < 0)
322 sshbuf_put_u16(struct sshbuf *buf, u_int16_t val)
327 if ((r = sshbuf_reserve(buf, 2, &p)) < 0)
334 sshbuf_put_u8(struct sshbuf *buf, u_char val)
339 if ((r = sshbuf_reserve(buf, 1, &p)) < 0)
346 sshbuf_put_string(struct sshbuf *buf, const void *v, size_t len)
351 if (len > SSHBUF_SIZE_MAX - 4) {
352 SSHBUF_DBG(("SSH_ERR_NO_BUFFER_SPACE"));
353 return SSH_ERR_NO_BUFFER_SPACE;
355 if ((r = sshbuf_reserve(buf, len + 4, &d)) < 0)
359 memcpy(d + 4, v, len);
364 sshbuf_put_cstring(struct sshbuf *buf, const char *v)
366 return sshbuf_put_string(buf, (u_char *)v, v == NULL ? 0 : strlen(v));
370 sshbuf_put_stringb(struct sshbuf *buf, const struct sshbuf *v)
372 return sshbuf_put_string(buf, sshbuf_ptr(v), sshbuf_len(v));
376 sshbuf_froms(struct sshbuf *buf, struct sshbuf **bufp)
383 if (buf == NULL || bufp == NULL)
384 return SSH_ERR_INVALID_ARGUMENT;
386 if ((r = sshbuf_peek_string_direct(buf, &p, &len)) != 0)
388 if ((ret = sshbuf_from(p, len)) == NULL)
389 return SSH_ERR_ALLOC_FAIL;
390 if ((r = sshbuf_consume(buf, len + 4)) != 0 || /* Shouldn't happen */
391 (r = sshbuf_set_parent(ret, buf)) != 0) {
400 sshbuf_put_bignum2_bytes(struct sshbuf *buf, const void *v, size_t len)
403 const u_char *s = (const u_char *)v;
406 if (len > SSHBUF_SIZE_MAX - 5) {
407 SSHBUF_DBG(("SSH_ERR_NO_BUFFER_SPACE"));
408 return SSH_ERR_NO_BUFFER_SPACE;
410 /* Skip leading zero bytes */
411 for (; len > 0 && *s == 0; len--, s++)
414 * If most significant bit is set then prepend a zero byte to
415 * avoid interpretation as a negative number.
417 prepend = len > 0 && (s[0] & 0x80) != 0;
418 if ((r = sshbuf_reserve(buf, len + 4 + prepend, &d)) < 0)
420 POKE_U32(d, len + prepend);
424 memcpy(d + 4 + prepend, s, len);
429 sshbuf_get_bignum2_bytes_direct(struct sshbuf *buf,
430 const u_char **valp, size_t *lenp)
436 if ((r = sshbuf_peek_string_direct(buf, &d, &olen)) < 0)
439 /* Refuse negative (MSB set) bignums */
440 if ((len != 0 && (*d & 0x80) != 0))
441 return SSH_ERR_BIGNUM_IS_NEGATIVE;
442 /* Refuse overlong bignums, allow prepended \0 to avoid MSB set */
443 if (len > SSHBUF_MAX_BIGNUM + 1 ||
444 (len == SSHBUF_MAX_BIGNUM + 1 && *d != 0))
445 return SSH_ERR_BIGNUM_TOO_LARGE;
446 /* Trim leading zeros */
447 while (len > 0 && *d == 0x00) {
455 if (sshbuf_consume(buf, olen + 4) != 0) {
456 /* Shouldn't happen */
457 SSHBUF_DBG(("SSH_ERR_INTERNAL_ERROR"));
459 return SSH_ERR_INTERNAL_ERROR;