xref: /netbsd-src/crypto/external/bsd/openssh/dist/sshbuf-misc.c (revision a03ec00c12395ed074b9ed551943c1b2d7b6c4a5)
1*a03ec00cSchristos /*	$OpenBSD: sshbuf-misc.c,v 1.18 2022/01/22 00:43:43 djm Exp $	*/
25484a5efSchristos /*
35484a5efSchristos  * Copyright (c) 2011 Damien Miller
45484a5efSchristos  *
55484a5efSchristos  * Permission to use, copy, modify, and distribute this software for any
65484a5efSchristos  * purpose with or without fee is hereby granted, provided that the above
75484a5efSchristos  * copyright notice and this permission notice appear in all copies.
85484a5efSchristos  *
95484a5efSchristos  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
105484a5efSchristos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
115484a5efSchristos  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
125484a5efSchristos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
135484a5efSchristos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
145484a5efSchristos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
155484a5efSchristos  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
165484a5efSchristos  */
178a4530f9Schristos #include "includes.h"
18*a03ec00cSchristos __RCSID("$NetBSD: sshbuf-misc.c,v 1.14 2022/02/23 19:07:20 christos Exp $");
195484a5efSchristos 
205484a5efSchristos #include <sys/types.h>
215484a5efSchristos #include <sys/socket.h>
225484a5efSchristos #include <netinet/in.h>
235484a5efSchristos #include <errno.h>
245484a5efSchristos #include <stdlib.h>
25e4d43b82Schristos #include <stdint.h>
265484a5efSchristos #include <stdio.h>
275484a5efSchristos #include <limits.h>
285484a5efSchristos #include <string.h>
295484a5efSchristos #include <resolv.h>
305484a5efSchristos #include <ctype.h>
31*a03ec00cSchristos #include <unistd.h>
325484a5efSchristos 
335484a5efSchristos #include "ssherr.h"
345484a5efSchristos #define SSHBUF_INTERNAL
355484a5efSchristos #include "sshbuf.h"
365484a5efSchristos 
375484a5efSchristos void
sshbuf_dump_data(const void * s,size_t len,FILE * f)385484a5efSchristos sshbuf_dump_data(const void *s, size_t len, FILE *f)
395484a5efSchristos {
405484a5efSchristos 	size_t i, j;
415484a5efSchristos 	const u_char *p = (const u_char *)s;
425484a5efSchristos 
435484a5efSchristos 	for (i = 0; i < len; i += 16) {
444054ffb0Schristos 		fprintf(f, "%.4zu: ", i);
455484a5efSchristos 		for (j = i; j < i + 16; j++) {
465484a5efSchristos 			if (j < len)
475484a5efSchristos 				fprintf(f, "%02x ", p[j]);
485484a5efSchristos 			else
495484a5efSchristos 				fprintf(f, "   ");
505484a5efSchristos 		}
515484a5efSchristos 		fprintf(f, " ");
525484a5efSchristos 		for (j = i; j < i + 16; j++) {
535484a5efSchristos 			if (j < len) {
545484a5efSchristos 				if  (isascii(p[j]) && isprint(p[j]))
555484a5efSchristos 					fprintf(f, "%c", p[j]);
565484a5efSchristos 				else
575484a5efSchristos 					fprintf(f, ".");
585484a5efSchristos 			}
595484a5efSchristos 		}
605484a5efSchristos 		fprintf(f, "\n");
615484a5efSchristos 	}
625484a5efSchristos }
635484a5efSchristos 
645484a5efSchristos void
sshbuf_dump(const struct sshbuf * buf,FILE * f)652d3b0f52Schristos sshbuf_dump(const struct sshbuf *buf, FILE *f)
665484a5efSchristos {
67b592f463Schristos 	fprintf(f, "buffer len = %zu\n", sshbuf_len(buf));
685484a5efSchristos 	sshbuf_dump_data(sshbuf_ptr(buf), sshbuf_len(buf), f);
695484a5efSchristos }
705484a5efSchristos 
715484a5efSchristos char *
sshbuf_dtob16(struct sshbuf * buf)725484a5efSchristos sshbuf_dtob16(struct sshbuf *buf)
735484a5efSchristos {
745484a5efSchristos 	size_t i, j, len = sshbuf_len(buf);
755484a5efSchristos 	const u_char *p = sshbuf_ptr(buf);
765484a5efSchristos 	char *ret;
775484a5efSchristos 	const char hex[] = "0123456789abcdef";
785484a5efSchristos 
795484a5efSchristos 	if (len == 0)
805484a5efSchristos 		return strdup("");
815484a5efSchristos 	if (SIZE_MAX / 2 <= len || (ret = malloc(len * 2 + 1)) == NULL)
825484a5efSchristos 		return NULL;
835484a5efSchristos 	for (i = j = 0; i < len; i++) {
845484a5efSchristos 		ret[j++] = hex[(p[i] >> 4) & 0xf];
855484a5efSchristos 		ret[j++] = hex[p[i] & 0xf];
865484a5efSchristos 	}
875484a5efSchristos 	ret[j] = '\0';
885484a5efSchristos 	return ret;
895484a5efSchristos }
905484a5efSchristos 
91cd4ada6aSchristos int
sshbuf_dtob64(const struct sshbuf * d,struct sshbuf * b64,int wrap)92cd4ada6aSchristos sshbuf_dtob64(const struct sshbuf *d, struct sshbuf *b64, int wrap)
935484a5efSchristos {
94cd4ada6aSchristos 	size_t i, slen = 0;
95cd4ada6aSchristos 	char *s = NULL;
965484a5efSchristos 	int r;
975484a5efSchristos 
98cd4ada6aSchristos 	if (d == NULL || b64 == NULL || sshbuf_len(d) >= SIZE_MAX / 2)
99cd4ada6aSchristos 		return SSH_ERR_INVALID_ARGUMENT;
100cd4ada6aSchristos 	if (sshbuf_len(d) == 0)
101cd4ada6aSchristos 		return 0;
102cd4ada6aSchristos 	slen = ((sshbuf_len(d) + 2) / 3) * 4 + 1;
103cd4ada6aSchristos 	if ((s = malloc(slen)) == NULL)
104cd4ada6aSchristos 		return SSH_ERR_ALLOC_FAIL;
105cd4ada6aSchristos 	if (b64_ntop(sshbuf_ptr(d), sshbuf_len(d), s, slen) == -1) {
106cd4ada6aSchristos 		r = SSH_ERR_INTERNAL_ERROR;
107cd4ada6aSchristos 		goto fail;
108cd4ada6aSchristos 	}
109cd4ada6aSchristos 	if (wrap) {
110cd4ada6aSchristos 		for (i = 0; s[i] != '\0'; i++) {
111cd4ada6aSchristos 			if ((r = sshbuf_put_u8(b64, s[i])) != 0)
112cd4ada6aSchristos 				goto fail;
113cd4ada6aSchristos 			if (i % 70 == 69 && (r = sshbuf_put_u8(b64, '\n')) != 0)
114cd4ada6aSchristos 				goto fail;
115cd4ada6aSchristos 		}
116cd4ada6aSchristos 		if ((i - 1) % 70 != 69 && (r = sshbuf_put_u8(b64, '\n')) != 0)
117cd4ada6aSchristos 			goto fail;
118cd4ada6aSchristos 	} else {
119cd4ada6aSchristos 		if ((r = sshbuf_put(b64, s, strlen(s))) != 0)
120cd4ada6aSchristos 			goto fail;
121cd4ada6aSchristos 	}
122cd4ada6aSchristos 	/* Success */
123cd4ada6aSchristos 	r = 0;
124cd4ada6aSchristos  fail:
125cd4ada6aSchristos 	freezero(s, slen);
126cd4ada6aSchristos 	return r;
127cd4ada6aSchristos }
128cd4ada6aSchristos 
129cd4ada6aSchristos char *
sshbuf_dtob64_string(const struct sshbuf * buf,int wrap)130cd4ada6aSchristos sshbuf_dtob64_string(const struct sshbuf *buf, int wrap)
131cd4ada6aSchristos {
132cd4ada6aSchristos 	struct sshbuf *tmp;
133cd4ada6aSchristos 	char *ret;
134cd4ada6aSchristos 
135cd4ada6aSchristos 	if ((tmp = sshbuf_new()) == NULL)
1365484a5efSchristos 		return NULL;
137cd4ada6aSchristos 	if (sshbuf_dtob64(buf, tmp, wrap) != 0) {
138cd4ada6aSchristos 		sshbuf_free(tmp);
1395484a5efSchristos 		return NULL;
1405484a5efSchristos 	}
141cd4ada6aSchristos 	ret = sshbuf_dup_string(tmp);
142cd4ada6aSchristos 	sshbuf_free(tmp);
1435484a5efSchristos 	return ret;
1445484a5efSchristos }
1455484a5efSchristos 
1465484a5efSchristos int
sshbuf_b64tod(struct sshbuf * buf,const char * b64)1475484a5efSchristos sshbuf_b64tod(struct sshbuf *buf, const char *b64)
1485484a5efSchristos {
1495484a5efSchristos 	size_t plen = strlen(b64);
1505484a5efSchristos 	int nlen, r;
1515484a5efSchristos 	u_char *p;
1525484a5efSchristos 
1535484a5efSchristos 	if (plen == 0)
1545484a5efSchristos 		return 0;
1555484a5efSchristos 	if ((p = malloc(plen)) == NULL)
1565484a5efSchristos 		return SSH_ERR_ALLOC_FAIL;
1575484a5efSchristos 	if ((nlen = b64_pton(b64, p, plen)) < 0) {
1588db691beSchristos 		freezero(p, plen);
1595484a5efSchristos 		return SSH_ERR_INVALID_FORMAT;
1605484a5efSchristos 	}
1615484a5efSchristos 	if ((r = sshbuf_put(buf, p, nlen)) < 0) {
1628db691beSchristos 		freezero(p, plen);
1635484a5efSchristos 		return r;
1645484a5efSchristos 	}
1658db691beSchristos 	freezero(p, plen);
1665484a5efSchristos 	return 0;
1675484a5efSchristos }
1685484a5efSchristos 
1692d3b0f52Schristos int
sshbuf_dtourlb64(const struct sshbuf * d,struct sshbuf * b64,int wrap)1702d3b0f52Schristos sshbuf_dtourlb64(const struct sshbuf *d, struct sshbuf *b64, int wrap)
1712d3b0f52Schristos {
1722d3b0f52Schristos 	int r = SSH_ERR_INTERNAL_ERROR;
1732d3b0f52Schristos 	u_char *p;
1742d3b0f52Schristos 	struct sshbuf *b = NULL;
1752d3b0f52Schristos 	size_t i, l;
1762d3b0f52Schristos 
1772d3b0f52Schristos 	if ((b = sshbuf_new()) == NULL)
1782d3b0f52Schristos 		return SSH_ERR_ALLOC_FAIL;
1792d3b0f52Schristos 	/* Encode using regular base64; we'll transform it once done */
1802d3b0f52Schristos 	if ((r = sshbuf_dtob64(d, b, wrap)) != 0)
1812d3b0f52Schristos 		goto out;
1822d3b0f52Schristos 	/* remove padding from end of encoded string*/
1832d3b0f52Schristos 	for (;;) {
1842d3b0f52Schristos 		l = sshbuf_len(b);
1852d3b0f52Schristos 		if (l <= 1 || sshbuf_ptr(b) == NULL) {
1862d3b0f52Schristos 			r = SSH_ERR_INTERNAL_ERROR;
1872d3b0f52Schristos 			goto out;
1882d3b0f52Schristos 		}
1892d3b0f52Schristos 		if (sshbuf_ptr(b)[l - 1] != '=')
1902d3b0f52Schristos 			break;
1912d3b0f52Schristos 		if ((r = sshbuf_consume_end(b, 1)) != 0)
1922d3b0f52Schristos 			goto out;
1932d3b0f52Schristos 	}
1942d3b0f52Schristos 	/* Replace characters with rfc4648 equivalents */
1952d3b0f52Schristos 	l = sshbuf_len(b);
1962d3b0f52Schristos 	if ((p = sshbuf_mutable_ptr(b)) == NULL) {
1972d3b0f52Schristos 		r = SSH_ERR_INTERNAL_ERROR;
1982d3b0f52Schristos 		goto out;
1992d3b0f52Schristos 	}
2002d3b0f52Schristos 	for (i = 0; i < l; i++) {
2012d3b0f52Schristos 		if (p[i] == '+')
2022d3b0f52Schristos 			p[i] = '-';
2032d3b0f52Schristos 		else if (p[i] == '/')
2042d3b0f52Schristos 			p[i] = '_';
2052d3b0f52Schristos 	}
2062d3b0f52Schristos 	r = sshbuf_putb(b64, b);
2072d3b0f52Schristos  out:
2082d3b0f52Schristos 	sshbuf_free(b);
2092d3b0f52Schristos 	return r;
2102d3b0f52Schristos }
2112d3b0f52Schristos 
2125101d403Schristos char *
sshbuf_dup_string(struct sshbuf * buf)2135101d403Schristos sshbuf_dup_string(struct sshbuf *buf)
2145101d403Schristos {
2155101d403Schristos 	const u_char *p = NULL, *s = sshbuf_ptr(buf);
2165101d403Schristos 	size_t l = sshbuf_len(buf);
2175101d403Schristos 	char *r;
2185101d403Schristos 
2195101d403Schristos 	if (s == NULL || l > SIZE_MAX)
2205101d403Schristos 		return NULL;
2215101d403Schristos 	/* accept a nul only as the last character in the buffer */
2225101d403Schristos 	if (l > 0 && (p = memchr(s, '\0', l)) != NULL) {
2235101d403Schristos 		if (p != s + l - 1)
2245101d403Schristos 			return NULL;
2255101d403Schristos 		l--; /* the nul is put back below */
2265101d403Schristos 	}
2275101d403Schristos 	if ((r = malloc(l + 1)) == NULL)
2285101d403Schristos 		return NULL;
2295101d403Schristos 	if (l > 0)
2305101d403Schristos 		memcpy(r, s, l);
2315101d403Schristos 	r[l] = '\0';
2325101d403Schristos 	return r;
2335101d403Schristos }
2345101d403Schristos 
235cd4ada6aSchristos int
sshbuf_cmp(const struct sshbuf * b,size_t offset,const void * s,size_t len)236cd4ada6aSchristos sshbuf_cmp(const struct sshbuf *b, size_t offset,
237cd4ada6aSchristos     const void *s, size_t len)
238cd4ada6aSchristos {
239cd4ada6aSchristos 	if (sshbuf_ptr(b) == NULL)
240cd4ada6aSchristos 		return SSH_ERR_INTERNAL_ERROR;
241cd4ada6aSchristos 	if (offset > SSHBUF_SIZE_MAX || len > SSHBUF_SIZE_MAX || len == 0)
242cd4ada6aSchristos 		return SSH_ERR_INVALID_ARGUMENT;
243cd4ada6aSchristos 	if (offset + len > sshbuf_len(b))
244cd4ada6aSchristos 		return SSH_ERR_MESSAGE_INCOMPLETE;
245cd4ada6aSchristos 	if (timingsafe_bcmp(sshbuf_ptr(b) + offset, s, len) != 0)
246cd4ada6aSchristos 		return SSH_ERR_INVALID_FORMAT;
247cd4ada6aSchristos 	return 0;
248cd4ada6aSchristos }
249cd4ada6aSchristos 
250cd4ada6aSchristos int
sshbuf_find(const struct sshbuf * b,size_t start_offset,const void * s,size_t len,size_t * offsetp)251cd4ada6aSchristos sshbuf_find(const struct sshbuf *b, size_t start_offset,
252cd4ada6aSchristos     const void *s, size_t len, size_t *offsetp)
253cd4ada6aSchristos {
254cd4ada6aSchristos 	void *p;
255cd4ada6aSchristos 
256cd4ada6aSchristos 	if (offsetp != NULL)
257cd4ada6aSchristos 		*offsetp = 0;
258cd4ada6aSchristos 	if (sshbuf_ptr(b) == NULL)
259cd4ada6aSchristos 		return SSH_ERR_INTERNAL_ERROR;
260cd4ada6aSchristos 	if (start_offset > SSHBUF_SIZE_MAX || len > SSHBUF_SIZE_MAX || len == 0)
261cd4ada6aSchristos 		return SSH_ERR_INVALID_ARGUMENT;
262cd4ada6aSchristos 	if (start_offset > sshbuf_len(b) || start_offset + len > sshbuf_len(b))
263cd4ada6aSchristos 		return SSH_ERR_MESSAGE_INCOMPLETE;
264cd4ada6aSchristos 	if ((p = memmem(sshbuf_ptr(b) + start_offset,
265cd4ada6aSchristos 	    sshbuf_len(b) - start_offset, s, len)) == NULL)
266cd4ada6aSchristos 		return SSH_ERR_INVALID_FORMAT;
267cd4ada6aSchristos 	if (offsetp != NULL)
268cd4ada6aSchristos 		*offsetp = (const u_char *)p - sshbuf_ptr(b);
269cd4ada6aSchristos 	return 0;
270cd4ada6aSchristos }
271*a03ec00cSchristos 
272*a03ec00cSchristos int
sshbuf_read(int fd,struct sshbuf * buf,size_t maxlen,size_t * rlen)273*a03ec00cSchristos sshbuf_read(int fd, struct sshbuf *buf, size_t maxlen, size_t *rlen)
274*a03ec00cSchristos {
275*a03ec00cSchristos 	int r, oerrno;
276*a03ec00cSchristos 	size_t adjust;
277*a03ec00cSchristos 	ssize_t rr;
278*a03ec00cSchristos 	u_char *d;
279*a03ec00cSchristos 
280*a03ec00cSchristos 	if (rlen != NULL)
281*a03ec00cSchristos 		*rlen = 0;
282*a03ec00cSchristos 	if ((r = sshbuf_reserve(buf, maxlen, &d)) != 0)
283*a03ec00cSchristos 		return r;
284*a03ec00cSchristos 	rr = read(fd, d, maxlen);
285*a03ec00cSchristos 	oerrno = errno;
286*a03ec00cSchristos 
287*a03ec00cSchristos 	/* Adjust the buffer to include only what was actually read */
288*a03ec00cSchristos 	if ((adjust = maxlen - (rr > 0 ? rr : 0)) != 0) {
289*a03ec00cSchristos 		if ((r = sshbuf_consume_end(buf, adjust)) != 0) {
290*a03ec00cSchristos 			/* avoid returning uninitialised data to caller */
291*a03ec00cSchristos 			memset(d + rr, '\0', adjust);
292*a03ec00cSchristos 			return SSH_ERR_INTERNAL_ERROR; /* shouldn't happen */
293*a03ec00cSchristos 		}
294*a03ec00cSchristos 	}
295*a03ec00cSchristos 	if (rr < 0) {
296*a03ec00cSchristos 		errno = oerrno;
297*a03ec00cSchristos 		return SSH_ERR_SYSTEM_ERROR;
298*a03ec00cSchristos 	} else if (rr == 0) {
299*a03ec00cSchristos 		errno = EPIPE;
300*a03ec00cSchristos 		return SSH_ERR_SYSTEM_ERROR;
301*a03ec00cSchristos 	}
302*a03ec00cSchristos 	/* success */
303*a03ec00cSchristos 	if (rlen != NULL)
304*a03ec00cSchristos 		*rlen = (size_t)rr;
305*a03ec00cSchristos 	return 0;
306*a03ec00cSchristos }
307