1*84f89c72Schristos /*	$OpenBSD: sshbuf-misc.c,v 1.18 2022/01/22 00:43:43 djm Exp $	*/
259288a41Schristos /*
359288a41Schristos  * Copyright (c) 2011 Damien Miller
459288a41Schristos  *
559288a41Schristos  * Permission to use, copy, modify, and distribute this software for any
659288a41Schristos  * purpose with or without fee is hereby granted, provided that the above
759288a41Schristos  * copyright notice and this permission notice appear in all copies.
859288a41Schristos  *
959288a41Schristos  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1059288a41Schristos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1159288a41Schristos  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1259288a41Schristos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1359288a41Schristos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1459288a41Schristos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1559288a41Schristos  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1659288a41Schristos  */
17e8f89eebSchristos #include "includes.h"
18*84f89c72Schristos __RCSID("$NetBSD: sshbuf-misc.c,v 1.14 2022/02/23 19:07:20 christos Exp $");
1959288a41Schristos 
2059288a41Schristos #include <sys/types.h>
2159288a41Schristos #include <sys/socket.h>
2259288a41Schristos #include <netinet/in.h>
2359288a41Schristos #include <errno.h>
2459288a41Schristos #include <stdlib.h>
2551acf88eSchristos #include <stdint.h>
2659288a41Schristos #include <stdio.h>
2759288a41Schristos #include <limits.h>
2859288a41Schristos #include <string.h>
2959288a41Schristos #include <resolv.h>
3059288a41Schristos #include <ctype.h>
31*84f89c72Schristos #include <unistd.h>
3259288a41Schristos 
3359288a41Schristos #include "ssherr.h"
3459288a41Schristos #define SSHBUF_INTERNAL
3559288a41Schristos #include "sshbuf.h"
3659288a41Schristos 
3759288a41Schristos void
sshbuf_dump_data(const void * s,size_t len,FILE * f)3859288a41Schristos sshbuf_dump_data(const void *s, size_t len, FILE *f)
3959288a41Schristos {
4059288a41Schristos 	size_t i, j;
4159288a41Schristos 	const u_char *p = (const u_char *)s;
4259288a41Schristos 
4359288a41Schristos 	for (i = 0; i < len; i += 16) {
44c6a11926Schristos 		fprintf(f, "%.4zu: ", i);
4559288a41Schristos 		for (j = i; j < i + 16; j++) {
4659288a41Schristos 			if (j < len)
4759288a41Schristos 				fprintf(f, "%02x ", p[j]);
4859288a41Schristos 			else
4959288a41Schristos 				fprintf(f, "   ");
5059288a41Schristos 		}
5159288a41Schristos 		fprintf(f, " ");
5259288a41Schristos 		for (j = i; j < i + 16; j++) {
5359288a41Schristos 			if (j < len) {
5459288a41Schristos 				if  (isascii(p[j]) && isprint(p[j]))
5559288a41Schristos 					fprintf(f, "%c", p[j]);
5659288a41Schristos 				else
5759288a41Schristos 					fprintf(f, ".");
5859288a41Schristos 			}
5959288a41Schristos 		}
6059288a41Schristos 		fprintf(f, "\n");
6159288a41Schristos 	}
6259288a41Schristos }
6359288a41Schristos 
6459288a41Schristos void
sshbuf_dump(const struct sshbuf * buf,FILE * f)657d35d687Schristos sshbuf_dump(const struct sshbuf *buf, FILE *f)
6659288a41Schristos {
6719a1dfedSchristos 	fprintf(f, "buffer len = %zu\n", sshbuf_len(buf));
6859288a41Schristos 	sshbuf_dump_data(sshbuf_ptr(buf), sshbuf_len(buf), f);
6959288a41Schristos }
7059288a41Schristos 
7159288a41Schristos char *
sshbuf_dtob16(struct sshbuf * buf)7259288a41Schristos sshbuf_dtob16(struct sshbuf *buf)
7359288a41Schristos {
7459288a41Schristos 	size_t i, j, len = sshbuf_len(buf);
7559288a41Schristos 	const u_char *p = sshbuf_ptr(buf);
7659288a41Schristos 	char *ret;
7759288a41Schristos 	const char hex[] = "0123456789abcdef";
7859288a41Schristos 
7959288a41Schristos 	if (len == 0)
8059288a41Schristos 		return strdup("");
8159288a41Schristos 	if (SIZE_MAX / 2 <= len || (ret = malloc(len * 2 + 1)) == NULL)
8259288a41Schristos 		return NULL;
8359288a41Schristos 	for (i = j = 0; i < len; i++) {
8459288a41Schristos 		ret[j++] = hex[(p[i] >> 4) & 0xf];
8559288a41Schristos 		ret[j++] = hex[p[i] & 0xf];
8659288a41Schristos 	}
8759288a41Schristos 	ret[j] = '\0';
8859288a41Schristos 	return ret;
8959288a41Schristos }
9059288a41Schristos 
913f0e61bfSchristos int
sshbuf_dtob64(const struct sshbuf * d,struct sshbuf * b64,int wrap)923f0e61bfSchristos sshbuf_dtob64(const struct sshbuf *d, struct sshbuf *b64, int wrap)
9359288a41Schristos {
943f0e61bfSchristos 	size_t i, slen = 0;
953f0e61bfSchristos 	char *s = NULL;
9659288a41Schristos 	int r;
9759288a41Schristos 
983f0e61bfSchristos 	if (d == NULL || b64 == NULL || sshbuf_len(d) >= SIZE_MAX / 2)
993f0e61bfSchristos 		return SSH_ERR_INVALID_ARGUMENT;
1003f0e61bfSchristos 	if (sshbuf_len(d) == 0)
1013f0e61bfSchristos 		return 0;
1023f0e61bfSchristos 	slen = ((sshbuf_len(d) + 2) / 3) * 4 + 1;
1033f0e61bfSchristos 	if ((s = malloc(slen)) == NULL)
1043f0e61bfSchristos 		return SSH_ERR_ALLOC_FAIL;
1053f0e61bfSchristos 	if (b64_ntop(sshbuf_ptr(d), sshbuf_len(d), s, slen) == -1) {
1063f0e61bfSchristos 		r = SSH_ERR_INTERNAL_ERROR;
1073f0e61bfSchristos 		goto fail;
1083f0e61bfSchristos 	}
1093f0e61bfSchristos 	if (wrap) {
1103f0e61bfSchristos 		for (i = 0; s[i] != '\0'; i++) {
1113f0e61bfSchristos 			if ((r = sshbuf_put_u8(b64, s[i])) != 0)
1123f0e61bfSchristos 				goto fail;
1133f0e61bfSchristos 			if (i % 70 == 69 && (r = sshbuf_put_u8(b64, '\n')) != 0)
1143f0e61bfSchristos 				goto fail;
1153f0e61bfSchristos 		}
1163f0e61bfSchristos 		if ((i - 1) % 70 != 69 && (r = sshbuf_put_u8(b64, '\n')) != 0)
1173f0e61bfSchristos 			goto fail;
1183f0e61bfSchristos 	} else {
1193f0e61bfSchristos 		if ((r = sshbuf_put(b64, s, strlen(s))) != 0)
1203f0e61bfSchristos 			goto fail;
1213f0e61bfSchristos 	}
1223f0e61bfSchristos 	/* Success */
1233f0e61bfSchristos 	r = 0;
1243f0e61bfSchristos  fail:
1253f0e61bfSchristos 	freezero(s, slen);
1263f0e61bfSchristos 	return r;
1273f0e61bfSchristos }
1283f0e61bfSchristos 
1293f0e61bfSchristos char *
sshbuf_dtob64_string(const struct sshbuf * buf,int wrap)1303f0e61bfSchristos sshbuf_dtob64_string(const struct sshbuf *buf, int wrap)
1313f0e61bfSchristos {
1323f0e61bfSchristos 	struct sshbuf *tmp;
1333f0e61bfSchristos 	char *ret;
1343f0e61bfSchristos 
1353f0e61bfSchristos 	if ((tmp = sshbuf_new()) == NULL)
13659288a41Schristos 		return NULL;
1373f0e61bfSchristos 	if (sshbuf_dtob64(buf, tmp, wrap) != 0) {
1383f0e61bfSchristos 		sshbuf_free(tmp);
13959288a41Schristos 		return NULL;
14059288a41Schristos 	}
1413f0e61bfSchristos 	ret = sshbuf_dup_string(tmp);
1423f0e61bfSchristos 	sshbuf_free(tmp);
14359288a41Schristos 	return ret;
14459288a41Schristos }
14559288a41Schristos 
14659288a41Schristos int
sshbuf_b64tod(struct sshbuf * buf,const char * b64)14759288a41Schristos sshbuf_b64tod(struct sshbuf *buf, const char *b64)
14859288a41Schristos {
14959288a41Schristos 	size_t plen = strlen(b64);
15059288a41Schristos 	int nlen, r;
15159288a41Schristos 	u_char *p;
15259288a41Schristos 
15359288a41Schristos 	if (plen == 0)
15459288a41Schristos 		return 0;
15559288a41Schristos 	if ((p = malloc(plen)) == NULL)
15659288a41Schristos 		return SSH_ERR_ALLOC_FAIL;
15759288a41Schristos 	if ((nlen = b64_pton(b64, p, plen)) < 0) {
158e29d081dSchristos 		freezero(p, plen);
15959288a41Schristos 		return SSH_ERR_INVALID_FORMAT;
16059288a41Schristos 	}
16159288a41Schristos 	if ((r = sshbuf_put(buf, p, nlen)) < 0) {
162e29d081dSchristos 		freezero(p, plen);
16359288a41Schristos 		return r;
16459288a41Schristos 	}
165e29d081dSchristos 	freezero(p, plen);
16659288a41Schristos 	return 0;
16759288a41Schristos }
16859288a41Schristos 
1697d35d687Schristos int
sshbuf_dtourlb64(const struct sshbuf * d,struct sshbuf * b64,int wrap)1707d35d687Schristos sshbuf_dtourlb64(const struct sshbuf *d, struct sshbuf *b64, int wrap)
1717d35d687Schristos {
1727d35d687Schristos 	int r = SSH_ERR_INTERNAL_ERROR;
1737d35d687Schristos 	u_char *p;
1747d35d687Schristos 	struct sshbuf *b = NULL;
1757d35d687Schristos 	size_t i, l;
1767d35d687Schristos 
1777d35d687Schristos 	if ((b = sshbuf_new()) == NULL)
1787d35d687Schristos 		return SSH_ERR_ALLOC_FAIL;
1797d35d687Schristos 	/* Encode using regular base64; we'll transform it once done */
1807d35d687Schristos 	if ((r = sshbuf_dtob64(d, b, wrap)) != 0)
1817d35d687Schristos 		goto out;
1827d35d687Schristos 	/* remove padding from end of encoded string*/
1837d35d687Schristos 	for (;;) {
1847d35d687Schristos 		l = sshbuf_len(b);
1857d35d687Schristos 		if (l <= 1 || sshbuf_ptr(b) == NULL) {
1867d35d687Schristos 			r = SSH_ERR_INTERNAL_ERROR;
1877d35d687Schristos 			goto out;
1887d35d687Schristos 		}
1897d35d687Schristos 		if (sshbuf_ptr(b)[l - 1] != '=')
1907d35d687Schristos 			break;
1917d35d687Schristos 		if ((r = sshbuf_consume_end(b, 1)) != 0)
1927d35d687Schristos 			goto out;
1937d35d687Schristos 	}
1947d35d687Schristos 	/* Replace characters with rfc4648 equivalents */
1957d35d687Schristos 	l = sshbuf_len(b);
1967d35d687Schristos 	if ((p = sshbuf_mutable_ptr(b)) == NULL) {
1977d35d687Schristos 		r = SSH_ERR_INTERNAL_ERROR;
1987d35d687Schristos 		goto out;
1997d35d687Schristos 	}
2007d35d687Schristos 	for (i = 0; i < l; i++) {
2017d35d687Schristos 		if (p[i] == '+')
2027d35d687Schristos 			p[i] = '-';
2037d35d687Schristos 		else if (p[i] == '/')
2047d35d687Schristos 			p[i] = '_';
2057d35d687Schristos 	}
2067d35d687Schristos 	r = sshbuf_putb(b64, b);
2077d35d687Schristos  out:
2087d35d687Schristos 	sshbuf_free(b);
2097d35d687Schristos 	return r;
2107d35d687Schristos }
2117d35d687Schristos 
2125a7cf56cSchristos char *
sshbuf_dup_string(struct sshbuf * buf)2135a7cf56cSchristos sshbuf_dup_string(struct sshbuf *buf)
2145a7cf56cSchristos {
2155a7cf56cSchristos 	const u_char *p = NULL, *s = sshbuf_ptr(buf);
2165a7cf56cSchristos 	size_t l = sshbuf_len(buf);
2175a7cf56cSchristos 	char *r;
2185a7cf56cSchristos 
2195a7cf56cSchristos 	if (s == NULL || l > SIZE_MAX)
2205a7cf56cSchristos 		return NULL;
2215a7cf56cSchristos 	/* accept a nul only as the last character in the buffer */
2225a7cf56cSchristos 	if (l > 0 && (p = memchr(s, '\0', l)) != NULL) {
2235a7cf56cSchristos 		if (p != s + l - 1)
2245a7cf56cSchristos 			return NULL;
2255a7cf56cSchristos 		l--; /* the nul is put back below */
2265a7cf56cSchristos 	}
2275a7cf56cSchristos 	if ((r = malloc(l + 1)) == NULL)
2285a7cf56cSchristos 		return NULL;
2295a7cf56cSchristos 	if (l > 0)
2305a7cf56cSchristos 		memcpy(r, s, l);
2315a7cf56cSchristos 	r[l] = '\0';
2325a7cf56cSchristos 	return r;
2335a7cf56cSchristos }
2345a7cf56cSchristos 
2353f0e61bfSchristos int
sshbuf_cmp(const struct sshbuf * b,size_t offset,const void * s,size_t len)2363f0e61bfSchristos sshbuf_cmp(const struct sshbuf *b, size_t offset,
2373f0e61bfSchristos     const void *s, size_t len)
2383f0e61bfSchristos {
2393f0e61bfSchristos 	if (sshbuf_ptr(b) == NULL)
2403f0e61bfSchristos 		return SSH_ERR_INTERNAL_ERROR;
2413f0e61bfSchristos 	if (offset > SSHBUF_SIZE_MAX || len > SSHBUF_SIZE_MAX || len == 0)
2423f0e61bfSchristos 		return SSH_ERR_INVALID_ARGUMENT;
2433f0e61bfSchristos 	if (offset + len > sshbuf_len(b))
2443f0e61bfSchristos 		return SSH_ERR_MESSAGE_INCOMPLETE;
2453f0e61bfSchristos 	if (timingsafe_bcmp(sshbuf_ptr(b) + offset, s, len) != 0)
2463f0e61bfSchristos 		return SSH_ERR_INVALID_FORMAT;
2473f0e61bfSchristos 	return 0;
2483f0e61bfSchristos }
2493f0e61bfSchristos 
2503f0e61bfSchristos int
sshbuf_find(const struct sshbuf * b,size_t start_offset,const void * s,size_t len,size_t * offsetp)2513f0e61bfSchristos sshbuf_find(const struct sshbuf *b, size_t start_offset,
2523f0e61bfSchristos     const void *s, size_t len, size_t *offsetp)
2533f0e61bfSchristos {
2543f0e61bfSchristos 	void *p;
2553f0e61bfSchristos 
2563f0e61bfSchristos 	if (offsetp != NULL)
2573f0e61bfSchristos 		*offsetp = 0;
2583f0e61bfSchristos 	if (sshbuf_ptr(b) == NULL)
2593f0e61bfSchristos 		return SSH_ERR_INTERNAL_ERROR;
2603f0e61bfSchristos 	if (start_offset > SSHBUF_SIZE_MAX || len > SSHBUF_SIZE_MAX || len == 0)
2613f0e61bfSchristos 		return SSH_ERR_INVALID_ARGUMENT;
2623f0e61bfSchristos 	if (start_offset > sshbuf_len(b) || start_offset + len > sshbuf_len(b))
2633f0e61bfSchristos 		return SSH_ERR_MESSAGE_INCOMPLETE;
2643f0e61bfSchristos 	if ((p = memmem(sshbuf_ptr(b) + start_offset,
2653f0e61bfSchristos 	    sshbuf_len(b) - start_offset, s, len)) == NULL)
2663f0e61bfSchristos 		return SSH_ERR_INVALID_FORMAT;
2673f0e61bfSchristos 	if (offsetp != NULL)
2683f0e61bfSchristos 		*offsetp = (const u_char *)p - sshbuf_ptr(b);
2693f0e61bfSchristos 	return 0;
2703f0e61bfSchristos }
271*84f89c72Schristos 
272*84f89c72Schristos int
sshbuf_read(int fd,struct sshbuf * buf,size_t maxlen,size_t * rlen)273*84f89c72Schristos sshbuf_read(int fd, struct sshbuf *buf, size_t maxlen, size_t *rlen)
274*84f89c72Schristos {
275*84f89c72Schristos 	int r, oerrno;
276*84f89c72Schristos 	size_t adjust;
277*84f89c72Schristos 	ssize_t rr;
278*84f89c72Schristos 	u_char *d;
279*84f89c72Schristos 
280*84f89c72Schristos 	if (rlen != NULL)
281*84f89c72Schristos 		*rlen = 0;
282*84f89c72Schristos 	if ((r = sshbuf_reserve(buf, maxlen, &d)) != 0)
283*84f89c72Schristos 		return r;
284*84f89c72Schristos 	rr = read(fd, d, maxlen);
285*84f89c72Schristos 	oerrno = errno;
286*84f89c72Schristos 
287*84f89c72Schristos 	/* Adjust the buffer to include only what was actually read */
288*84f89c72Schristos 	if ((adjust = maxlen - (rr > 0 ? rr : 0)) != 0) {
289*84f89c72Schristos 		if ((r = sshbuf_consume_end(buf, adjust)) != 0) {
290*84f89c72Schristos 			/* avoid returning uninitialised data to caller */
291*84f89c72Schristos 			memset(d + rr, '\0', adjust);
292*84f89c72Schristos 			return SSH_ERR_INTERNAL_ERROR; /* shouldn't happen */
293*84f89c72Schristos 		}
294*84f89c72Schristos 	}
295*84f89c72Schristos 	if (rr < 0) {
296*84f89c72Schristos 		errno = oerrno;
297*84f89c72Schristos 		return SSH_ERR_SYSTEM_ERROR;
298*84f89c72Schristos 	} else if (rr == 0) {
299*84f89c72Schristos 		errno = EPIPE;
300*84f89c72Schristos 		return SSH_ERR_SYSTEM_ERROR;
301*84f89c72Schristos 	}
302*84f89c72Schristos 	/* success */
303*84f89c72Schristos 	if (rlen != NULL)
304*84f89c72Schristos 		*rlen = (size_t)rr;
305*84f89c72Schristos 	return 0;
306*84f89c72Schristos }
307