1 /* $OpenBSD: sshbuf-getput-basic.c,v 1.1 2014/04/30 05:29:56 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>
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)
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;
112 memcpy(*valp, val, len);
121 sshbuf_get_string_direct(struct sshbuf *buf, const u_char **valp, size_t *lenp)
131 if ((r = sshbuf_peek_string_direct(buf, &p, &len)) < 0)
137 if (sshbuf_consume(buf, len + 4) != 0) {
138 /* Shouldn't happen */
139 SSHBUF_DBG(("SSH_ERR_INTERNAL_ERROR"));
141 return SSH_ERR_INTERNAL_ERROR;
147 sshbuf_peek_string_direct(const struct sshbuf *buf, const u_char **valp,
151 const u_char *p = sshbuf_ptr(buf);
157 if (sshbuf_len(buf) < 4) {
158 SSHBUF_DBG(("SSH_ERR_MESSAGE_INCOMPLETE"));
159 return SSH_ERR_MESSAGE_INCOMPLETE;
162 if (len > SSHBUF_SIZE_MAX - 4) {
163 SSHBUF_DBG(("SSH_ERR_STRING_TOO_LARGE"));
164 return SSH_ERR_STRING_TOO_LARGE;
166 if (sshbuf_len(buf) - 4 < len) {
167 SSHBUF_DBG(("SSH_ERR_MESSAGE_INCOMPLETE"));
168 return SSH_ERR_MESSAGE_INCOMPLETE;
178 sshbuf_get_cstring(struct sshbuf *buf, char **valp, size_t *lenp)
188 if ((r = sshbuf_peek_string_direct(buf, &p, &len)) != 0)
190 /* Allow a \0 only at the end of the string */
192 (z = memchr(p , '\0', len)) != NULL && z < p + len - 1) {
193 SSHBUF_DBG(("SSH_ERR_INVALID_FORMAT"));
194 return SSH_ERR_INVALID_FORMAT;
196 if ((r = sshbuf_skip_string(buf)) != 0)
199 if ((*valp = malloc(len + 1)) == NULL) {
200 SSHBUF_DBG(("SSH_ERR_ALLOC_FAIL"));
201 return SSH_ERR_ALLOC_FAIL;
203 memcpy(*valp, p, len);
212 sshbuf_get_stringb(struct sshbuf *buf, struct sshbuf *v)
219 * Use sshbuf_peek_string_direct() to figure out if there is
220 * a complete string in 'buf' and copy the string directly
223 if ((r = sshbuf_peek_string_direct(buf, NULL, NULL)) != 0 ||
224 (r = sshbuf_get_u32(buf, &len)) != 0 ||
225 (r = sshbuf_reserve(v, len, &p)) != 0 ||
226 (r = sshbuf_get(buf, p, len)) != 0)
232 sshbuf_put(struct sshbuf *buf, const void *v, size_t len)
237 if ((r = sshbuf_reserve(buf, len, &p)) < 0)
244 sshbuf_putb(struct sshbuf *buf, const struct sshbuf *v)
246 return sshbuf_put(buf, sshbuf_ptr(v), sshbuf_len(v));
250 sshbuf_putf(struct sshbuf *buf, const char *fmt, ...)
256 r = sshbuf_putfv(buf, fmt, ap);
262 sshbuf_putfv(struct sshbuf *buf, const char *fmt, va_list ap)
269 if ((len = vsnprintf(NULL, 0, fmt, ap2)) < 0) {
270 r = SSH_ERR_INVALID_ARGUMENT;
275 goto out; /* Nothing to do */
279 if ((r = sshbuf_reserve(buf, (size_t)len + 1, &p)) < 0)
281 if ((r = vsnprintf((char *)p, len + 1, fmt, ap2)) != len) {
282 r = SSH_ERR_INTERNAL_ERROR;
283 goto out; /* Shouldn't happen */
285 /* Consume terminating \0 */
286 if ((r = sshbuf_consume_end(buf, 1)) != 0)
295 sshbuf_put_u64(struct sshbuf *buf, u_int64_t val)
300 if ((r = sshbuf_reserve(buf, 8, &p)) < 0)
307 sshbuf_put_u32(struct sshbuf *buf, u_int32_t val)
312 if ((r = sshbuf_reserve(buf, 4, &p)) < 0)
319 sshbuf_put_u16(struct sshbuf *buf, u_int16_t val)
324 if ((r = sshbuf_reserve(buf, 2, &p)) < 0)
331 sshbuf_put_u8(struct sshbuf *buf, u_char val)
336 if ((r = sshbuf_reserve(buf, 1, &p)) < 0)
343 sshbuf_put_string(struct sshbuf *buf, const void *v, size_t len)
348 if (len > SSHBUF_SIZE_MAX - 4) {
349 SSHBUF_DBG(("SSH_ERR_NO_BUFFER_SPACE"));
350 return SSH_ERR_NO_BUFFER_SPACE;
352 if ((r = sshbuf_reserve(buf, len + 4, &d)) < 0)
355 memcpy(d + 4, v, len);
360 sshbuf_put_cstring(struct sshbuf *buf, const char *v)
362 return sshbuf_put_string(buf, (u_char *)v, strlen(v));
366 sshbuf_put_stringb(struct sshbuf *buf, const struct sshbuf *v)
368 return sshbuf_put_string(buf, sshbuf_ptr(v), sshbuf_len(v));
372 sshbuf_froms(struct sshbuf *buf, struct sshbuf **bufp)
379 if (buf == NULL || bufp == NULL)
380 return SSH_ERR_INVALID_ARGUMENT;
382 if ((r = sshbuf_peek_string_direct(buf, &p, &len)) != 0)
384 if ((ret = sshbuf_from(p, len)) == NULL)
385 return SSH_ERR_ALLOC_FAIL;
386 if ((r = sshbuf_consume(buf, len + 4)) != 0 || /* Shouldn't happen */
387 (r = sshbuf_set_parent(ret, buf)) != 0) {
396 sshbuf_put_bignum2_bytes(struct sshbuf *buf, const void *v, size_t len)
399 const u_char *s = (const u_char *)v;
402 if (len > SSHBUF_SIZE_MAX - 5) {
403 SSHBUF_DBG(("SSH_ERR_NO_BUFFER_SPACE"));
404 return SSH_ERR_NO_BUFFER_SPACE;
406 /* Skip leading zero bytes */
407 for (; len > 0 && *s == 0; len--, s++)
410 * If most significant bit is set then prepend a zero byte to
411 * avoid interpretation as a negative number.
413 prepend = len > 0 && (s[0] & 0x80) != 0;
414 if ((r = sshbuf_reserve(buf, len + 4 + prepend, &d)) < 0)
416 POKE_U32(d, len + prepend);
419 memcpy(d + 4 + prepend, s, len);