xref: /openssh-portable/sshbuf-misc.c (revision 12848191)
1*12848191Sdjm@openbsd.org /*	$OpenBSD: sshbuf-misc.c,v 1.16 2020/06/22 05:54:10 djm Exp $	*/
205e82c3bSDamien Miller /*
305e82c3bSDamien Miller  * Copyright (c) 2011 Damien Miller
405e82c3bSDamien Miller  *
505e82c3bSDamien Miller  * Permission to use, copy, modify, and distribute this software for any
605e82c3bSDamien Miller  * purpose with or without fee is hereby granted, provided that the above
705e82c3bSDamien Miller  * copyright notice and this permission notice appear in all copies.
805e82c3bSDamien Miller  *
905e82c3bSDamien Miller  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1005e82c3bSDamien Miller  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1105e82c3bSDamien Miller  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1205e82c3bSDamien Miller  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1305e82c3bSDamien Miller  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1405e82c3bSDamien Miller  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1505e82c3bSDamien Miller  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1605e82c3bSDamien Miller  */
1705e82c3bSDamien Miller 
1805e82c3bSDamien Miller #include "includes.h"
1905e82c3bSDamien Miller 
2005e82c3bSDamien Miller #include <sys/types.h>
2105e82c3bSDamien Miller #include <sys/socket.h>
2205e82c3bSDamien Miller #include <netinet/in.h>
2305e82c3bSDamien Miller #include <errno.h>
2405e82c3bSDamien Miller #include <stdlib.h>
2537f9220dSDarren Tucker #ifdef HAVE_STDINT_H
261910a286Smillert@openbsd.org # include <stdint.h>
2737f9220dSDarren Tucker #endif
2805e82c3bSDamien Miller #include <stdio.h>
2905e82c3bSDamien Miller #include <limits.h>
3005e82c3bSDamien Miller #include <string.h>
3105e82c3bSDamien Miller #include <resolv.h>
3205e82c3bSDamien Miller #include <ctype.h>
3305e82c3bSDamien Miller 
3405e82c3bSDamien Miller #include "ssherr.h"
3505e82c3bSDamien Miller #define SSHBUF_INTERNAL
3605e82c3bSDamien Miller #include "sshbuf.h"
3705e82c3bSDamien Miller 
3805e82c3bSDamien Miller void
sshbuf_dump_data(const void * s,size_t len,FILE * f)398668706dSDamien Miller sshbuf_dump_data(const void *s, size_t len, FILE *f)
4005e82c3bSDamien Miller {
418668706dSDamien Miller 	size_t i, j;
428668706dSDamien Miller 	const u_char *p = (const u_char *)s;
4305e82c3bSDamien Miller 
4405e82c3bSDamien Miller 	for (i = 0; i < len; i += 16) {
45df100be5Smarkus@openbsd.org 		fprintf(f, "%.4zu: ", i);
4605e82c3bSDamien Miller 		for (j = i; j < i + 16; j++) {
4705e82c3bSDamien Miller 			if (j < len)
4805e82c3bSDamien Miller 				fprintf(f, "%02x ", p[j]);
4905e82c3bSDamien Miller 			else
5005e82c3bSDamien Miller 				fprintf(f, "   ");
5105e82c3bSDamien Miller 		}
5205e82c3bSDamien Miller 		fprintf(f, " ");
5305e82c3bSDamien Miller 		for (j = i; j < i + 16; j++) {
5405e82c3bSDamien Miller 			if (j < len) {
5505e82c3bSDamien Miller 				if  (isascii(p[j]) && isprint(p[j]))
5605e82c3bSDamien Miller 					fprintf(f, "%c", p[j]);
5705e82c3bSDamien Miller 				else
5805e82c3bSDamien Miller 					fprintf(f, ".");
5905e82c3bSDamien Miller 			}
6005e82c3bSDamien Miller 		}
6105e82c3bSDamien Miller 		fprintf(f, "\n");
6205e82c3bSDamien Miller 	}
6305e82c3bSDamien Miller }
6405e82c3bSDamien Miller 
658668706dSDamien Miller void
sshbuf_dump(const struct sshbuf * buf,FILE * f)6669796297Sdjm@openbsd.org sshbuf_dump(const struct sshbuf *buf, FILE *f)
678668706dSDamien Miller {
688668706dSDamien Miller 	fprintf(f, "buffer %p len = %zu\n", buf, sshbuf_len(buf));
698668706dSDamien Miller 	sshbuf_dump_data(sshbuf_ptr(buf), sshbuf_len(buf), f);
708668706dSDamien Miller }
718668706dSDamien Miller 
7205e82c3bSDamien Miller char *
sshbuf_dtob16(struct sshbuf * buf)7305e82c3bSDamien Miller sshbuf_dtob16(struct sshbuf *buf)
7405e82c3bSDamien Miller {
7505e82c3bSDamien Miller 	size_t i, j, len = sshbuf_len(buf);
7605e82c3bSDamien Miller 	const u_char *p = sshbuf_ptr(buf);
7705e82c3bSDamien Miller 	char *ret;
7805e82c3bSDamien Miller 	const char hex[] = "0123456789abcdef";
7905e82c3bSDamien Miller 
8005e82c3bSDamien Miller 	if (len == 0)
8105e82c3bSDamien Miller 		return strdup("");
8205e82c3bSDamien Miller 	if (SIZE_MAX / 2 <= len || (ret = malloc(len * 2 + 1)) == NULL)
8305e82c3bSDamien Miller 		return NULL;
8405e82c3bSDamien Miller 	for (i = j = 0; i < len; i++) {
8505e82c3bSDamien Miller 		ret[j++] = hex[(p[i] >> 4) & 0xf];
8605e82c3bSDamien Miller 		ret[j++] = hex[p[i] & 0xf];
8705e82c3bSDamien Miller 	}
8805e82c3bSDamien Miller 	ret[j] = '\0';
8905e82c3bSDamien Miller 	return ret;
9005e82c3bSDamien Miller }
9105e82c3bSDamien Miller 
9216dd8b2cSdjm@openbsd.org int
sshbuf_dtob64(const struct sshbuf * d,struct sshbuf * b64,int wrap)9316dd8b2cSdjm@openbsd.org sshbuf_dtob64(const struct sshbuf *d, struct sshbuf *b64, int wrap)
9405e82c3bSDamien Miller {
9516dd8b2cSdjm@openbsd.org 	size_t i, slen = 0;
9616dd8b2cSdjm@openbsd.org 	char *s = NULL;
9716dd8b2cSdjm@openbsd.org 	int r;
9816dd8b2cSdjm@openbsd.org 
9916dd8b2cSdjm@openbsd.org 	if (d == NULL || b64 == NULL || sshbuf_len(d) >= SIZE_MAX / 2)
10016dd8b2cSdjm@openbsd.org 		return SSH_ERR_INVALID_ARGUMENT;
10116dd8b2cSdjm@openbsd.org 	if (sshbuf_len(d) == 0)
10216dd8b2cSdjm@openbsd.org 		return 0;
10316dd8b2cSdjm@openbsd.org 	slen = ((sshbuf_len(d) + 2) / 3) * 4 + 1;
10416dd8b2cSdjm@openbsd.org 	if ((s = malloc(slen)) == NULL)
10516dd8b2cSdjm@openbsd.org 		return SSH_ERR_ALLOC_FAIL;
10616dd8b2cSdjm@openbsd.org 	if (b64_ntop(sshbuf_ptr(d), sshbuf_len(d), s, slen) == -1) {
10716dd8b2cSdjm@openbsd.org 		r = SSH_ERR_INTERNAL_ERROR;
10816dd8b2cSdjm@openbsd.org 		goto fail;
10916dd8b2cSdjm@openbsd.org 	}
11016dd8b2cSdjm@openbsd.org 	if (wrap) {
11116dd8b2cSdjm@openbsd.org 		for (i = 0; s[i] != '\0'; i++) {
11216dd8b2cSdjm@openbsd.org 			if ((r = sshbuf_put_u8(b64, s[i])) != 0)
11316dd8b2cSdjm@openbsd.org 				goto fail;
11416dd8b2cSdjm@openbsd.org 			if (i % 70 == 69 && (r = sshbuf_put_u8(b64, '\n')) != 0)
11516dd8b2cSdjm@openbsd.org 				goto fail;
11616dd8b2cSdjm@openbsd.org 		}
117ed46a0c0Sdjm@openbsd.org 		if ((i - 1) % 70 != 69 && (r = sshbuf_put_u8(b64, '\n')) != 0)
11816dd8b2cSdjm@openbsd.org 			goto fail;
11916dd8b2cSdjm@openbsd.org 	} else {
12016dd8b2cSdjm@openbsd.org 		if ((r = sshbuf_put(b64, s, strlen(s))) != 0)
12116dd8b2cSdjm@openbsd.org 			goto fail;
12216dd8b2cSdjm@openbsd.org 	}
12316dd8b2cSdjm@openbsd.org 	/* Success */
12416dd8b2cSdjm@openbsd.org 	r = 0;
12516dd8b2cSdjm@openbsd.org  fail:
12616dd8b2cSdjm@openbsd.org 	freezero(s, slen);
12716dd8b2cSdjm@openbsd.org 	return r;
12816dd8b2cSdjm@openbsd.org }
12916dd8b2cSdjm@openbsd.org 
13016dd8b2cSdjm@openbsd.org char *
sshbuf_dtob64_string(const struct sshbuf * buf,int wrap)13116dd8b2cSdjm@openbsd.org sshbuf_dtob64_string(const struct sshbuf *buf, int wrap)
13216dd8b2cSdjm@openbsd.org {
13316dd8b2cSdjm@openbsd.org 	struct sshbuf *tmp;
13405e82c3bSDamien Miller 	char *ret;
13505e82c3bSDamien Miller 
13616dd8b2cSdjm@openbsd.org 	if ((tmp = sshbuf_new()) == NULL)
13705e82c3bSDamien Miller 		return NULL;
13816dd8b2cSdjm@openbsd.org 	if (sshbuf_dtob64(buf, tmp, wrap) != 0) {
13916dd8b2cSdjm@openbsd.org 		sshbuf_free(tmp);
14005e82c3bSDamien Miller 		return NULL;
14105e82c3bSDamien Miller 	}
14216dd8b2cSdjm@openbsd.org 	ret = sshbuf_dup_string(tmp);
14316dd8b2cSdjm@openbsd.org 	sshbuf_free(tmp);
14405e82c3bSDamien Miller 	return ret;
14505e82c3bSDamien Miller }
14605e82c3bSDamien Miller 
14705e82c3bSDamien Miller int
sshbuf_b64tod(struct sshbuf * buf,const char * b64)14805e82c3bSDamien Miller sshbuf_b64tod(struct sshbuf *buf, const char *b64)
14905e82c3bSDamien Miller {
15005e82c3bSDamien Miller 	size_t plen = strlen(b64);
15105e82c3bSDamien Miller 	int nlen, r;
15205e82c3bSDamien Miller 	u_char *p;
15305e82c3bSDamien Miller 
15405e82c3bSDamien Miller 	if (plen == 0)
15505e82c3bSDamien Miller 		return 0;
15605e82c3bSDamien Miller 	if ((p = malloc(plen)) == NULL)
15705e82c3bSDamien Miller 		return SSH_ERR_ALLOC_FAIL;
15805e82c3bSDamien Miller 	if ((nlen = b64_pton(b64, p, plen)) < 0) {
159d5ba1c03Sjsg@openbsd.org 		freezero(p, plen);
16005e82c3bSDamien Miller 		return SSH_ERR_INVALID_FORMAT;
16105e82c3bSDamien Miller 	}
16205e82c3bSDamien Miller 	if ((r = sshbuf_put(buf, p, nlen)) < 0) {
163d5ba1c03Sjsg@openbsd.org 		freezero(p, plen);
16405e82c3bSDamien Miller 		return r;
16505e82c3bSDamien Miller 	}
166d5ba1c03Sjsg@openbsd.org 	freezero(p, plen);
16705e82c3bSDamien Miller 	return 0;
16805e82c3bSDamien Miller }
16905e82c3bSDamien Miller 
170*12848191Sdjm@openbsd.org int
sshbuf_dtourlb64(const struct sshbuf * d,struct sshbuf * b64,int wrap)171*12848191Sdjm@openbsd.org sshbuf_dtourlb64(const struct sshbuf *d, struct sshbuf *b64, int wrap)
172*12848191Sdjm@openbsd.org {
173*12848191Sdjm@openbsd.org 	int r = SSH_ERR_INTERNAL_ERROR;
174*12848191Sdjm@openbsd.org 	u_char *p;
175*12848191Sdjm@openbsd.org 	struct sshbuf *b = NULL;
176*12848191Sdjm@openbsd.org 	size_t i, l;
177*12848191Sdjm@openbsd.org 
178*12848191Sdjm@openbsd.org 	if ((b = sshbuf_new()) == NULL)
179*12848191Sdjm@openbsd.org 		return SSH_ERR_ALLOC_FAIL;
180*12848191Sdjm@openbsd.org 	/* Encode using regular base64; we'll transform it once done */
181*12848191Sdjm@openbsd.org 	if ((r = sshbuf_dtob64(d, b, wrap)) != 0)
182*12848191Sdjm@openbsd.org 		goto out;
183*12848191Sdjm@openbsd.org 	/* remove padding from end of encoded string*/
184*12848191Sdjm@openbsd.org 	for (;;) {
185*12848191Sdjm@openbsd.org 		l = sshbuf_len(b);
186*12848191Sdjm@openbsd.org 		if (l <= 1 || sshbuf_ptr(b) == NULL) {
187*12848191Sdjm@openbsd.org 			r = SSH_ERR_INTERNAL_ERROR;
188*12848191Sdjm@openbsd.org 			goto out;
189*12848191Sdjm@openbsd.org 		}
190*12848191Sdjm@openbsd.org 		if (sshbuf_ptr(b)[l - 1] != '=')
191*12848191Sdjm@openbsd.org 			break;
192*12848191Sdjm@openbsd.org 		if ((r = sshbuf_consume_end(b, 1)) != 0)
193*12848191Sdjm@openbsd.org 			goto out;
194*12848191Sdjm@openbsd.org 	}
195*12848191Sdjm@openbsd.org 	/* Replace characters with rfc4648 equivalents */
196*12848191Sdjm@openbsd.org 	l = sshbuf_len(b);
197*12848191Sdjm@openbsd.org 	if ((p = sshbuf_mutable_ptr(b)) == NULL) {
198*12848191Sdjm@openbsd.org 		r = SSH_ERR_INTERNAL_ERROR;
199*12848191Sdjm@openbsd.org 		goto out;
200*12848191Sdjm@openbsd.org 	}
201*12848191Sdjm@openbsd.org 	for (i = 0; i < l; i++) {
202*12848191Sdjm@openbsd.org 		if (p[i] == '+')
203*12848191Sdjm@openbsd.org 			p[i] = '-';
204*12848191Sdjm@openbsd.org 		else if (p[i] == '/')
205*12848191Sdjm@openbsd.org 			p[i] = '_';
206*12848191Sdjm@openbsd.org 	}
207*12848191Sdjm@openbsd.org 	r = sshbuf_putb(b64, b);
208*12848191Sdjm@openbsd.org  out:
209*12848191Sdjm@openbsd.org 	sshbuf_free(b);
210*12848191Sdjm@openbsd.org 	return r;
211*12848191Sdjm@openbsd.org }
212*12848191Sdjm@openbsd.org 
2131a31d02bSdjm@openbsd.org char *
sshbuf_dup_string(struct sshbuf * buf)2141a31d02bSdjm@openbsd.org sshbuf_dup_string(struct sshbuf *buf)
2151a31d02bSdjm@openbsd.org {
2161a31d02bSdjm@openbsd.org 	const u_char *p = NULL, *s = sshbuf_ptr(buf);
2171a31d02bSdjm@openbsd.org 	size_t l = sshbuf_len(buf);
2181a31d02bSdjm@openbsd.org 	char *r;
2191a31d02bSdjm@openbsd.org 
2201a31d02bSdjm@openbsd.org 	if (s == NULL || l > SIZE_MAX)
2211a31d02bSdjm@openbsd.org 		return NULL;
2221a31d02bSdjm@openbsd.org 	/* accept a nul only as the last character in the buffer */
2231a31d02bSdjm@openbsd.org 	if (l > 0 && (p = memchr(s, '\0', l)) != NULL) {
2241a31d02bSdjm@openbsd.org 		if (p != s + l - 1)
2251a31d02bSdjm@openbsd.org 			return NULL;
2261a31d02bSdjm@openbsd.org 		l--; /* the nul is put back below */
2271a31d02bSdjm@openbsd.org 	}
2281a31d02bSdjm@openbsd.org 	if ((r = malloc(l + 1)) == NULL)
2291a31d02bSdjm@openbsd.org 		return NULL;
2301a31d02bSdjm@openbsd.org 	if (l > 0)
2311a31d02bSdjm@openbsd.org 		memcpy(r, s, l);
2321a31d02bSdjm@openbsd.org 	r[l] = '\0';
2331a31d02bSdjm@openbsd.org 	return r;
2341a31d02bSdjm@openbsd.org }
2351a31d02bSdjm@openbsd.org 
236e18a27eeSdjm@openbsd.org int
sshbuf_cmp(const struct sshbuf * b,size_t offset,const void * s,size_t len)237e18a27eeSdjm@openbsd.org sshbuf_cmp(const struct sshbuf *b, size_t offset,
23849fa065aSdjm@openbsd.org     const void *s, size_t len)
239e18a27eeSdjm@openbsd.org {
240e18a27eeSdjm@openbsd.org 	if (sshbuf_ptr(b) == NULL)
241e18a27eeSdjm@openbsd.org 		return SSH_ERR_INTERNAL_ERROR;
242e18a27eeSdjm@openbsd.org 	if (offset > SSHBUF_SIZE_MAX || len > SSHBUF_SIZE_MAX || len == 0)
243e18a27eeSdjm@openbsd.org 		return SSH_ERR_INVALID_ARGUMENT;
244e18a27eeSdjm@openbsd.org 	if (offset + len > sshbuf_len(b))
245e18a27eeSdjm@openbsd.org 		return SSH_ERR_MESSAGE_INCOMPLETE;
246e18a27eeSdjm@openbsd.org 	if (timingsafe_bcmp(sshbuf_ptr(b) + offset, s, len) != 0)
247e18a27eeSdjm@openbsd.org 		return SSH_ERR_INVALID_FORMAT;
248e18a27eeSdjm@openbsd.org 	return 0;
249e18a27eeSdjm@openbsd.org }
250e18a27eeSdjm@openbsd.org 
251e18a27eeSdjm@openbsd.org int
sshbuf_find(const struct sshbuf * b,size_t start_offset,const void * s,size_t len,size_t * offsetp)252e18a27eeSdjm@openbsd.org sshbuf_find(const struct sshbuf *b, size_t start_offset,
25349fa065aSdjm@openbsd.org     const void *s, size_t len, size_t *offsetp)
254e18a27eeSdjm@openbsd.org {
255e18a27eeSdjm@openbsd.org 	void *p;
256e18a27eeSdjm@openbsd.org 
257e18a27eeSdjm@openbsd.org 	if (offsetp != NULL)
258e18a27eeSdjm@openbsd.org 		*offsetp = 0;
259e18a27eeSdjm@openbsd.org 	if (sshbuf_ptr(b) == NULL)
260e18a27eeSdjm@openbsd.org 		return SSH_ERR_INTERNAL_ERROR;
261e18a27eeSdjm@openbsd.org 	if (start_offset > SSHBUF_SIZE_MAX || len > SSHBUF_SIZE_MAX || len == 0)
262e18a27eeSdjm@openbsd.org 		return SSH_ERR_INVALID_ARGUMENT;
263e18a27eeSdjm@openbsd.org 	if (start_offset > sshbuf_len(b) || start_offset + len > sshbuf_len(b))
264e18a27eeSdjm@openbsd.org 		return SSH_ERR_MESSAGE_INCOMPLETE;
265e18a27eeSdjm@openbsd.org 	if ((p = memmem(sshbuf_ptr(b) + start_offset,
266e18a27eeSdjm@openbsd.org 	    sshbuf_len(b) - start_offset, s, len)) == NULL)
267e18a27eeSdjm@openbsd.org 		return SSH_ERR_INVALID_FORMAT;
268e18a27eeSdjm@openbsd.org 	if (offsetp != NULL)
269e18a27eeSdjm@openbsd.org 		*offsetp = (const u_char *)p - sshbuf_ptr(b);
270e18a27eeSdjm@openbsd.org 	return 0;
271e18a27eeSdjm@openbsd.org }
272