1*1323ec57SEd Maste /* $OpenBSD: sshbuf-misc.c,v 1.18 2022/01/22 00:43:43 djm Exp $ */
2a0ee8cc6SDag-Erling Smørgrav /*
3a0ee8cc6SDag-Erling Smørgrav * Copyright (c) 2011 Damien Miller
4a0ee8cc6SDag-Erling Smørgrav *
5a0ee8cc6SDag-Erling Smørgrav * Permission to use, copy, modify, and distribute this software for any
6a0ee8cc6SDag-Erling Smørgrav * purpose with or without fee is hereby granted, provided that the above
7a0ee8cc6SDag-Erling Smørgrav * copyright notice and this permission notice appear in all copies.
8a0ee8cc6SDag-Erling Smørgrav *
9a0ee8cc6SDag-Erling Smørgrav * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10a0ee8cc6SDag-Erling Smørgrav * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11a0ee8cc6SDag-Erling Smørgrav * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12a0ee8cc6SDag-Erling Smørgrav * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13a0ee8cc6SDag-Erling Smørgrav * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14a0ee8cc6SDag-Erling Smørgrav * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15a0ee8cc6SDag-Erling Smørgrav * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16a0ee8cc6SDag-Erling Smørgrav */
17a0ee8cc6SDag-Erling Smørgrav
18a0ee8cc6SDag-Erling Smørgrav #include "includes.h"
19a0ee8cc6SDag-Erling Smørgrav
20a0ee8cc6SDag-Erling Smørgrav #include <sys/types.h>
21a0ee8cc6SDag-Erling Smørgrav #include <sys/socket.h>
22a0ee8cc6SDag-Erling Smørgrav #include <netinet/in.h>
23a0ee8cc6SDag-Erling Smørgrav #include <errno.h>
24a0ee8cc6SDag-Erling Smørgrav #include <stdlib.h>
25bc5531deSDag-Erling Smørgrav #ifdef HAVE_STDINT_H
26bc5531deSDag-Erling Smørgrav # include <stdint.h>
27bc5531deSDag-Erling Smørgrav #endif
28a0ee8cc6SDag-Erling Smørgrav #include <stdio.h>
29a0ee8cc6SDag-Erling Smørgrav #include <limits.h>
30a0ee8cc6SDag-Erling Smørgrav #include <string.h>
31a0ee8cc6SDag-Erling Smørgrav #include <resolv.h>
32a0ee8cc6SDag-Erling Smørgrav #include <ctype.h>
33*1323ec57SEd Maste #include <unistd.h>
34a0ee8cc6SDag-Erling Smørgrav
35a0ee8cc6SDag-Erling Smørgrav #include "ssherr.h"
36a0ee8cc6SDag-Erling Smørgrav #define SSHBUF_INTERNAL
37a0ee8cc6SDag-Erling Smørgrav #include "sshbuf.h"
38a0ee8cc6SDag-Erling Smørgrav
39a0ee8cc6SDag-Erling Smørgrav void
sshbuf_dump_data(const void * s,size_t len,FILE * f)40a0ee8cc6SDag-Erling Smørgrav sshbuf_dump_data(const void *s, size_t len, FILE *f)
41a0ee8cc6SDag-Erling Smørgrav {
42a0ee8cc6SDag-Erling Smørgrav size_t i, j;
43a0ee8cc6SDag-Erling Smørgrav const u_char *p = (const u_char *)s;
44a0ee8cc6SDag-Erling Smørgrav
45a0ee8cc6SDag-Erling Smørgrav for (i = 0; i < len; i += 16) {
46557f75e5SDag-Erling Smørgrav fprintf(f, "%.4zu: ", i);
47a0ee8cc6SDag-Erling Smørgrav for (j = i; j < i + 16; j++) {
48a0ee8cc6SDag-Erling Smørgrav if (j < len)
49a0ee8cc6SDag-Erling Smørgrav fprintf(f, "%02x ", p[j]);
50a0ee8cc6SDag-Erling Smørgrav else
51a0ee8cc6SDag-Erling Smørgrav fprintf(f, " ");
52a0ee8cc6SDag-Erling Smørgrav }
53a0ee8cc6SDag-Erling Smørgrav fprintf(f, " ");
54a0ee8cc6SDag-Erling Smørgrav for (j = i; j < i + 16; j++) {
55a0ee8cc6SDag-Erling Smørgrav if (j < len) {
56a0ee8cc6SDag-Erling Smørgrav if (isascii(p[j]) && isprint(p[j]))
57a0ee8cc6SDag-Erling Smørgrav fprintf(f, "%c", p[j]);
58a0ee8cc6SDag-Erling Smørgrav else
59a0ee8cc6SDag-Erling Smørgrav fprintf(f, ".");
60a0ee8cc6SDag-Erling Smørgrav }
61a0ee8cc6SDag-Erling Smørgrav }
62a0ee8cc6SDag-Erling Smørgrav fprintf(f, "\n");
63a0ee8cc6SDag-Erling Smørgrav }
64a0ee8cc6SDag-Erling Smørgrav }
65a0ee8cc6SDag-Erling Smørgrav
66a0ee8cc6SDag-Erling Smørgrav void
sshbuf_dump(const struct sshbuf * buf,FILE * f)6719261079SEd Maste sshbuf_dump(const struct sshbuf *buf, FILE *f)
68a0ee8cc6SDag-Erling Smørgrav {
6919261079SEd Maste fprintf(f, "buffer len = %zu\n", sshbuf_len(buf));
70a0ee8cc6SDag-Erling Smørgrav sshbuf_dump_data(sshbuf_ptr(buf), sshbuf_len(buf), f);
71a0ee8cc6SDag-Erling Smørgrav }
72a0ee8cc6SDag-Erling Smørgrav
73a0ee8cc6SDag-Erling Smørgrav char *
sshbuf_dtob16(struct sshbuf * buf)74a0ee8cc6SDag-Erling Smørgrav sshbuf_dtob16(struct sshbuf *buf)
75a0ee8cc6SDag-Erling Smørgrav {
76a0ee8cc6SDag-Erling Smørgrav size_t i, j, len = sshbuf_len(buf);
77a0ee8cc6SDag-Erling Smørgrav const u_char *p = sshbuf_ptr(buf);
78a0ee8cc6SDag-Erling Smørgrav char *ret;
79a0ee8cc6SDag-Erling Smørgrav const char hex[] = "0123456789abcdef";
80a0ee8cc6SDag-Erling Smørgrav
81a0ee8cc6SDag-Erling Smørgrav if (len == 0)
82a0ee8cc6SDag-Erling Smørgrav return strdup("");
83a0ee8cc6SDag-Erling Smørgrav if (SIZE_MAX / 2 <= len || (ret = malloc(len * 2 + 1)) == NULL)
84a0ee8cc6SDag-Erling Smørgrav return NULL;
85a0ee8cc6SDag-Erling Smørgrav for (i = j = 0; i < len; i++) {
86a0ee8cc6SDag-Erling Smørgrav ret[j++] = hex[(p[i] >> 4) & 0xf];
87a0ee8cc6SDag-Erling Smørgrav ret[j++] = hex[p[i] & 0xf];
88a0ee8cc6SDag-Erling Smørgrav }
89a0ee8cc6SDag-Erling Smørgrav ret[j] = '\0';
90a0ee8cc6SDag-Erling Smørgrav return ret;
91a0ee8cc6SDag-Erling Smørgrav }
92a0ee8cc6SDag-Erling Smørgrav
9319261079SEd Maste int
sshbuf_dtob64(const struct sshbuf * d,struct sshbuf * b64,int wrap)9419261079SEd Maste sshbuf_dtob64(const struct sshbuf *d, struct sshbuf *b64, int wrap)
95a0ee8cc6SDag-Erling Smørgrav {
9619261079SEd Maste size_t i, slen = 0;
9719261079SEd Maste char *s = NULL;
98a0ee8cc6SDag-Erling Smørgrav int r;
99a0ee8cc6SDag-Erling Smørgrav
10019261079SEd Maste if (d == NULL || b64 == NULL || sshbuf_len(d) >= SIZE_MAX / 2)
10119261079SEd Maste return SSH_ERR_INVALID_ARGUMENT;
10219261079SEd Maste if (sshbuf_len(d) == 0)
10319261079SEd Maste return 0;
10419261079SEd Maste slen = ((sshbuf_len(d) + 2) / 3) * 4 + 1;
10519261079SEd Maste if ((s = malloc(slen)) == NULL)
10619261079SEd Maste return SSH_ERR_ALLOC_FAIL;
10719261079SEd Maste if (b64_ntop(sshbuf_ptr(d), sshbuf_len(d), s, slen) == -1) {
10819261079SEd Maste r = SSH_ERR_INTERNAL_ERROR;
10919261079SEd Maste goto fail;
11019261079SEd Maste }
11119261079SEd Maste if (wrap) {
11219261079SEd Maste for (i = 0; s[i] != '\0'; i++) {
11319261079SEd Maste if ((r = sshbuf_put_u8(b64, s[i])) != 0)
11419261079SEd Maste goto fail;
11519261079SEd Maste if (i % 70 == 69 && (r = sshbuf_put_u8(b64, '\n')) != 0)
11619261079SEd Maste goto fail;
11719261079SEd Maste }
11819261079SEd Maste if ((i - 1) % 70 != 69 && (r = sshbuf_put_u8(b64, '\n')) != 0)
11919261079SEd Maste goto fail;
12019261079SEd Maste } else {
12119261079SEd Maste if ((r = sshbuf_put(b64, s, strlen(s))) != 0)
12219261079SEd Maste goto fail;
12319261079SEd Maste }
12419261079SEd Maste /* Success */
12519261079SEd Maste r = 0;
12619261079SEd Maste fail:
12719261079SEd Maste freezero(s, slen);
12819261079SEd Maste return r;
12919261079SEd Maste }
13019261079SEd Maste
13119261079SEd Maste char *
sshbuf_dtob64_string(const struct sshbuf * buf,int wrap)13219261079SEd Maste sshbuf_dtob64_string(const struct sshbuf *buf, int wrap)
13319261079SEd Maste {
13419261079SEd Maste struct sshbuf *tmp;
13519261079SEd Maste char *ret;
13619261079SEd Maste
13719261079SEd Maste if ((tmp = sshbuf_new()) == NULL)
138a0ee8cc6SDag-Erling Smørgrav return NULL;
13919261079SEd Maste if (sshbuf_dtob64(buf, tmp, wrap) != 0) {
14019261079SEd Maste sshbuf_free(tmp);
141a0ee8cc6SDag-Erling Smørgrav return NULL;
142a0ee8cc6SDag-Erling Smørgrav }
14319261079SEd Maste ret = sshbuf_dup_string(tmp);
14419261079SEd Maste sshbuf_free(tmp);
145a0ee8cc6SDag-Erling Smørgrav return ret;
146a0ee8cc6SDag-Erling Smørgrav }
147a0ee8cc6SDag-Erling Smørgrav
148a0ee8cc6SDag-Erling Smørgrav int
sshbuf_b64tod(struct sshbuf * buf,const char * b64)149a0ee8cc6SDag-Erling Smørgrav sshbuf_b64tod(struct sshbuf *buf, const char *b64)
150a0ee8cc6SDag-Erling Smørgrav {
151a0ee8cc6SDag-Erling Smørgrav size_t plen = strlen(b64);
152a0ee8cc6SDag-Erling Smørgrav int nlen, r;
153a0ee8cc6SDag-Erling Smørgrav u_char *p;
154a0ee8cc6SDag-Erling Smørgrav
155a0ee8cc6SDag-Erling Smørgrav if (plen == 0)
156a0ee8cc6SDag-Erling Smørgrav return 0;
157a0ee8cc6SDag-Erling Smørgrav if ((p = malloc(plen)) == NULL)
158a0ee8cc6SDag-Erling Smørgrav return SSH_ERR_ALLOC_FAIL;
159a0ee8cc6SDag-Erling Smørgrav if ((nlen = b64_pton(b64, p, plen)) < 0) {
16019261079SEd Maste freezero(p, plen);
161a0ee8cc6SDag-Erling Smørgrav return SSH_ERR_INVALID_FORMAT;
162a0ee8cc6SDag-Erling Smørgrav }
163a0ee8cc6SDag-Erling Smørgrav if ((r = sshbuf_put(buf, p, nlen)) < 0) {
16419261079SEd Maste freezero(p, plen);
165a0ee8cc6SDag-Erling Smørgrav return r;
166a0ee8cc6SDag-Erling Smørgrav }
16719261079SEd Maste freezero(p, plen);
168a0ee8cc6SDag-Erling Smørgrav return 0;
169a0ee8cc6SDag-Erling Smørgrav }
170a0ee8cc6SDag-Erling Smørgrav
17119261079SEd Maste int
sshbuf_dtourlb64(const struct sshbuf * d,struct sshbuf * b64,int wrap)17219261079SEd Maste sshbuf_dtourlb64(const struct sshbuf *d, struct sshbuf *b64, int wrap)
17319261079SEd Maste {
17419261079SEd Maste int r = SSH_ERR_INTERNAL_ERROR;
17519261079SEd Maste u_char *p;
17619261079SEd Maste struct sshbuf *b = NULL;
17719261079SEd Maste size_t i, l;
17819261079SEd Maste
17919261079SEd Maste if ((b = sshbuf_new()) == NULL)
18019261079SEd Maste return SSH_ERR_ALLOC_FAIL;
18119261079SEd Maste /* Encode using regular base64; we'll transform it once done */
18219261079SEd Maste if ((r = sshbuf_dtob64(d, b, wrap)) != 0)
18319261079SEd Maste goto out;
18419261079SEd Maste /* remove padding from end of encoded string*/
18519261079SEd Maste for (;;) {
18619261079SEd Maste l = sshbuf_len(b);
18719261079SEd Maste if (l <= 1 || sshbuf_ptr(b) == NULL) {
18819261079SEd Maste r = SSH_ERR_INTERNAL_ERROR;
18919261079SEd Maste goto out;
19019261079SEd Maste }
19119261079SEd Maste if (sshbuf_ptr(b)[l - 1] != '=')
19219261079SEd Maste break;
19319261079SEd Maste if ((r = sshbuf_consume_end(b, 1)) != 0)
19419261079SEd Maste goto out;
19519261079SEd Maste }
19619261079SEd Maste /* Replace characters with rfc4648 equivalents */
19719261079SEd Maste l = sshbuf_len(b);
19819261079SEd Maste if ((p = sshbuf_mutable_ptr(b)) == NULL) {
19919261079SEd Maste r = SSH_ERR_INTERNAL_ERROR;
20019261079SEd Maste goto out;
20119261079SEd Maste }
20219261079SEd Maste for (i = 0; i < l; i++) {
20319261079SEd Maste if (p[i] == '+')
20419261079SEd Maste p[i] = '-';
20519261079SEd Maste else if (p[i] == '/')
20619261079SEd Maste p[i] = '_';
20719261079SEd Maste }
20819261079SEd Maste r = sshbuf_putb(b64, b);
20919261079SEd Maste out:
21019261079SEd Maste sshbuf_free(b);
21119261079SEd Maste return r;
21219261079SEd Maste }
21319261079SEd Maste
214076ad2f8SDag-Erling Smørgrav char *
sshbuf_dup_string(struct sshbuf * buf)215076ad2f8SDag-Erling Smørgrav sshbuf_dup_string(struct sshbuf *buf)
216076ad2f8SDag-Erling Smørgrav {
217076ad2f8SDag-Erling Smørgrav const u_char *p = NULL, *s = sshbuf_ptr(buf);
218076ad2f8SDag-Erling Smørgrav size_t l = sshbuf_len(buf);
219076ad2f8SDag-Erling Smørgrav char *r;
220076ad2f8SDag-Erling Smørgrav
221076ad2f8SDag-Erling Smørgrav if (s == NULL || l > SIZE_MAX)
222076ad2f8SDag-Erling Smørgrav return NULL;
223076ad2f8SDag-Erling Smørgrav /* accept a nul only as the last character in the buffer */
224076ad2f8SDag-Erling Smørgrav if (l > 0 && (p = memchr(s, '\0', l)) != NULL) {
225076ad2f8SDag-Erling Smørgrav if (p != s + l - 1)
226076ad2f8SDag-Erling Smørgrav return NULL;
227076ad2f8SDag-Erling Smørgrav l--; /* the nul is put back below */
228076ad2f8SDag-Erling Smørgrav }
229076ad2f8SDag-Erling Smørgrav if ((r = malloc(l + 1)) == NULL)
230076ad2f8SDag-Erling Smørgrav return NULL;
231076ad2f8SDag-Erling Smørgrav if (l > 0)
232076ad2f8SDag-Erling Smørgrav memcpy(r, s, l);
233076ad2f8SDag-Erling Smørgrav r[l] = '\0';
234076ad2f8SDag-Erling Smørgrav return r;
235076ad2f8SDag-Erling Smørgrav }
236076ad2f8SDag-Erling Smørgrav
23719261079SEd Maste int
sshbuf_cmp(const struct sshbuf * b,size_t offset,const void * s,size_t len)23819261079SEd Maste sshbuf_cmp(const struct sshbuf *b, size_t offset,
23919261079SEd Maste const void *s, size_t len)
24019261079SEd Maste {
24119261079SEd Maste if (sshbuf_ptr(b) == NULL)
24219261079SEd Maste return SSH_ERR_INTERNAL_ERROR;
24319261079SEd Maste if (offset > SSHBUF_SIZE_MAX || len > SSHBUF_SIZE_MAX || len == 0)
24419261079SEd Maste return SSH_ERR_INVALID_ARGUMENT;
24519261079SEd Maste if (offset + len > sshbuf_len(b))
24619261079SEd Maste return SSH_ERR_MESSAGE_INCOMPLETE;
24719261079SEd Maste if (timingsafe_bcmp(sshbuf_ptr(b) + offset, s, len) != 0)
24819261079SEd Maste return SSH_ERR_INVALID_FORMAT;
24919261079SEd Maste return 0;
25019261079SEd Maste }
25119261079SEd Maste
25219261079SEd Maste int
sshbuf_find(const struct sshbuf * b,size_t start_offset,const void * s,size_t len,size_t * offsetp)25319261079SEd Maste sshbuf_find(const struct sshbuf *b, size_t start_offset,
25419261079SEd Maste const void *s, size_t len, size_t *offsetp)
25519261079SEd Maste {
25619261079SEd Maste void *p;
25719261079SEd Maste
25819261079SEd Maste if (offsetp != NULL)
25919261079SEd Maste *offsetp = 0;
26019261079SEd Maste if (sshbuf_ptr(b) == NULL)
26119261079SEd Maste return SSH_ERR_INTERNAL_ERROR;
26219261079SEd Maste if (start_offset > SSHBUF_SIZE_MAX || len > SSHBUF_SIZE_MAX || len == 0)
26319261079SEd Maste return SSH_ERR_INVALID_ARGUMENT;
26419261079SEd Maste if (start_offset > sshbuf_len(b) || start_offset + len > sshbuf_len(b))
26519261079SEd Maste return SSH_ERR_MESSAGE_INCOMPLETE;
26619261079SEd Maste if ((p = memmem(sshbuf_ptr(b) + start_offset,
26719261079SEd Maste sshbuf_len(b) - start_offset, s, len)) == NULL)
26819261079SEd Maste return SSH_ERR_INVALID_FORMAT;
26919261079SEd Maste if (offsetp != NULL)
27019261079SEd Maste *offsetp = (const u_char *)p - sshbuf_ptr(b);
27119261079SEd Maste return 0;
27219261079SEd Maste }
273*1323ec57SEd Maste
274*1323ec57SEd Maste int
sshbuf_read(int fd,struct sshbuf * buf,size_t maxlen,size_t * rlen)275*1323ec57SEd Maste sshbuf_read(int fd, struct sshbuf *buf, size_t maxlen, size_t *rlen)
276*1323ec57SEd Maste {
277*1323ec57SEd Maste int r, oerrno;
278*1323ec57SEd Maste size_t adjust;
279*1323ec57SEd Maste ssize_t rr;
280*1323ec57SEd Maste u_char *d;
281*1323ec57SEd Maste
282*1323ec57SEd Maste if (rlen != NULL)
283*1323ec57SEd Maste *rlen = 0;
284*1323ec57SEd Maste if ((r = sshbuf_reserve(buf, maxlen, &d)) != 0)
285*1323ec57SEd Maste return r;
286*1323ec57SEd Maste rr = read(fd, d, maxlen);
287*1323ec57SEd Maste oerrno = errno;
288*1323ec57SEd Maste
289*1323ec57SEd Maste /* Adjust the buffer to include only what was actually read */
290*1323ec57SEd Maste if ((adjust = maxlen - (rr > 0 ? rr : 0)) != 0) {
291*1323ec57SEd Maste if ((r = sshbuf_consume_end(buf, adjust)) != 0) {
292*1323ec57SEd Maste /* avoid returning uninitialised data to caller */
293*1323ec57SEd Maste memset(d + rr, '\0', adjust);
294*1323ec57SEd Maste return SSH_ERR_INTERNAL_ERROR; /* shouldn't happen */
295*1323ec57SEd Maste }
296*1323ec57SEd Maste }
297*1323ec57SEd Maste if (rr < 0) {
298*1323ec57SEd Maste errno = oerrno;
299*1323ec57SEd Maste return SSH_ERR_SYSTEM_ERROR;
300*1323ec57SEd Maste } else if (rr == 0) {
301*1323ec57SEd Maste errno = EPIPE;
302*1323ec57SEd Maste return SSH_ERR_SYSTEM_ERROR;
303*1323ec57SEd Maste }
304*1323ec57SEd Maste /* success */
305*1323ec57SEd Maste if (rlen != NULL)
306*1323ec57SEd Maste *rlen = (size_t)rr;
307*1323ec57SEd Maste return 0;
308*1323ec57SEd Maste }
309