xref: /netbsd-src/crypto/external/bsd/openssh/dist/sshbuf-misc.c (revision 53b02e147d4ed531c0d2a5ca9b3e8026ba3e99b5)
1 /*	$OpenBSD: sshbuf-misc.c,v 1.17 2021/08/11 05:21:32 djm Exp $	*/
2 /*
3  * Copyright (c) 2011 Damien Miller
4  *
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.
8  *
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.
16  */
17 #include "includes.h"
18 __RCSID("$NetBSD: sshbuf-misc.c,v 1.13 2021/09/02 11:26:18 christos Exp $");
19 
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <netinet/in.h>
23 #include <errno.h>
24 #include <stdlib.h>
25 #include <stdint.h>
26 #include <stdio.h>
27 #include <limits.h>
28 #include <string.h>
29 #include <resolv.h>
30 #include <ctype.h>
31 
32 #include "ssherr.h"
33 #define SSHBUF_INTERNAL
34 #include "sshbuf.h"
35 
36 void
37 sshbuf_dump_data(const void *s, size_t len, FILE *f)
38 {
39 	size_t i, j;
40 	const u_char *p = (const u_char *)s;
41 
42 	for (i = 0; i < len; i += 16) {
43 		fprintf(f, "%.4zu: ", i);
44 		for (j = i; j < i + 16; j++) {
45 			if (j < len)
46 				fprintf(f, "%02x ", p[j]);
47 			else
48 				fprintf(f, "   ");
49 		}
50 		fprintf(f, " ");
51 		for (j = i; j < i + 16; j++) {
52 			if (j < len) {
53 				if  (isascii(p[j]) && isprint(p[j]))
54 					fprintf(f, "%c", p[j]);
55 				else
56 					fprintf(f, ".");
57 			}
58 		}
59 		fprintf(f, "\n");
60 	}
61 }
62 
63 void
64 sshbuf_dump(const struct sshbuf *buf, FILE *f)
65 {
66 	fprintf(f, "buffer len = %zu\n", sshbuf_len(buf));
67 	sshbuf_dump_data(sshbuf_ptr(buf), sshbuf_len(buf), f);
68 }
69 
70 char *
71 sshbuf_dtob16(struct sshbuf *buf)
72 {
73 	size_t i, j, len = sshbuf_len(buf);
74 	const u_char *p = sshbuf_ptr(buf);
75 	char *ret;
76 	const char hex[] = "0123456789abcdef";
77 
78 	if (len == 0)
79 		return strdup("");
80 	if (SIZE_MAX / 2 <= len || (ret = malloc(len * 2 + 1)) == NULL)
81 		return NULL;
82 	for (i = j = 0; i < len; i++) {
83 		ret[j++] = hex[(p[i] >> 4) & 0xf];
84 		ret[j++] = hex[p[i] & 0xf];
85 	}
86 	ret[j] = '\0';
87 	return ret;
88 }
89 
90 int
91 sshbuf_dtob64(const struct sshbuf *d, struct sshbuf *b64, int wrap)
92 {
93 	size_t i, slen = 0;
94 	char *s = NULL;
95 	int r;
96 
97 	if (d == NULL || b64 == NULL || sshbuf_len(d) >= SIZE_MAX / 2)
98 		return SSH_ERR_INVALID_ARGUMENT;
99 	if (sshbuf_len(d) == 0)
100 		return 0;
101 	slen = ((sshbuf_len(d) + 2) / 3) * 4 + 1;
102 	if ((s = malloc(slen)) == NULL)
103 		return SSH_ERR_ALLOC_FAIL;
104 	if (b64_ntop(sshbuf_ptr(d), sshbuf_len(d), s, slen) == -1) {
105 		r = SSH_ERR_INTERNAL_ERROR;
106 		goto fail;
107 	}
108 	if (wrap) {
109 		for (i = 0; s[i] != '\0'; i++) {
110 			if ((r = sshbuf_put_u8(b64, s[i])) != 0)
111 				goto fail;
112 			if (i % 70 == 69 && (r = sshbuf_put_u8(b64, '\n')) != 0)
113 				goto fail;
114 		}
115 		if ((i - 1) % 70 != 69 && (r = sshbuf_put_u8(b64, '\n')) != 0)
116 			goto fail;
117 	} else {
118 		if ((r = sshbuf_put(b64, s, strlen(s))) != 0)
119 			goto fail;
120 	}
121 	/* Success */
122 	r = 0;
123  fail:
124 	freezero(s, slen);
125 	return r;
126 }
127 
128 char *
129 sshbuf_dtob64_string(const struct sshbuf *buf, int wrap)
130 {
131 	struct sshbuf *tmp;
132 	char *ret;
133 
134 	if ((tmp = sshbuf_new()) == NULL)
135 		return NULL;
136 	if (sshbuf_dtob64(buf, tmp, wrap) != 0) {
137 		sshbuf_free(tmp);
138 		return NULL;
139 	}
140 	ret = sshbuf_dup_string(tmp);
141 	sshbuf_free(tmp);
142 	return ret;
143 }
144 
145 int
146 sshbuf_b64tod(struct sshbuf *buf, const char *b64)
147 {
148 	size_t plen = strlen(b64);
149 	int nlen, r;
150 	u_char *p;
151 
152 	if (plen == 0)
153 		return 0;
154 	if ((p = malloc(plen)) == NULL)
155 		return SSH_ERR_ALLOC_FAIL;
156 	if ((nlen = b64_pton(b64, p, plen)) < 0) {
157 		freezero(p, plen);
158 		return SSH_ERR_INVALID_FORMAT;
159 	}
160 	if ((r = sshbuf_put(buf, p, nlen)) < 0) {
161 		freezero(p, plen);
162 		return r;
163 	}
164 	freezero(p, plen);
165 	return 0;
166 }
167 
168 int
169 sshbuf_dtourlb64(const struct sshbuf *d, struct sshbuf *b64, int wrap)
170 {
171 	int r = SSH_ERR_INTERNAL_ERROR;
172 	u_char *p;
173 	struct sshbuf *b = NULL;
174 	size_t i, l;
175 
176 	if ((b = sshbuf_new()) == NULL)
177 		return SSH_ERR_ALLOC_FAIL;
178 	/* Encode using regular base64; we'll transform it once done */
179 	if ((r = sshbuf_dtob64(d, b, wrap)) != 0)
180 		goto out;
181 	/* remove padding from end of encoded string*/
182 	for (;;) {
183 		l = sshbuf_len(b);
184 		if (l <= 1 || sshbuf_ptr(b) == NULL) {
185 			r = SSH_ERR_INTERNAL_ERROR;
186 			goto out;
187 		}
188 		if (sshbuf_ptr(b)[l - 1] != '=')
189 			break;
190 		if ((r = sshbuf_consume_end(b, 1)) != 0)
191 			goto out;
192 	}
193 	/* Replace characters with rfc4648 equivalents */
194 	l = sshbuf_len(b);
195 	if ((p = sshbuf_mutable_ptr(b)) == NULL) {
196 		r = SSH_ERR_INTERNAL_ERROR;
197 		goto out;
198 	}
199 	for (i = 0; i < l; i++) {
200 		if (p[i] == '+')
201 			p[i] = '-';
202 		else if (p[i] == '/')
203 			p[i] = '_';
204 	}
205 	r = sshbuf_putb(b64, b);
206  out:
207 	sshbuf_free(b);
208 	return r;
209 }
210 
211 char *
212 sshbuf_dup_string(struct sshbuf *buf)
213 {
214 	const u_char *p = NULL, *s = sshbuf_ptr(buf);
215 	size_t l = sshbuf_len(buf);
216 	char *r;
217 
218 	if (s == NULL || l > SIZE_MAX)
219 		return NULL;
220 	/* accept a nul only as the last character in the buffer */
221 	if (l > 0 && (p = memchr(s, '\0', l)) != NULL) {
222 		if (p != s + l - 1)
223 			return NULL;
224 		l--; /* the nul is put back below */
225 	}
226 	if ((r = malloc(l + 1)) == NULL)
227 		return NULL;
228 	if (l > 0)
229 		memcpy(r, s, l);
230 	r[l] = '\0';
231 	return r;
232 }
233 
234 int
235 sshbuf_cmp(const struct sshbuf *b, size_t offset,
236     const void *s, size_t len)
237 {
238 	if (sshbuf_ptr(b) == NULL)
239 		return SSH_ERR_INTERNAL_ERROR;
240 	if (offset > SSHBUF_SIZE_MAX || len > SSHBUF_SIZE_MAX || len == 0)
241 		return SSH_ERR_INVALID_ARGUMENT;
242 	if (offset + len > sshbuf_len(b))
243 		return SSH_ERR_MESSAGE_INCOMPLETE;
244 	if (timingsafe_bcmp(sshbuf_ptr(b) + offset, s, len) != 0)
245 		return SSH_ERR_INVALID_FORMAT;
246 	return 0;
247 }
248 
249 int
250 sshbuf_find(const struct sshbuf *b, size_t start_offset,
251     const void *s, size_t len, size_t *offsetp)
252 {
253 	void *p;
254 
255 	if (offsetp != NULL)
256 		*offsetp = 0;
257 	if (sshbuf_ptr(b) == NULL)
258 		return SSH_ERR_INTERNAL_ERROR;
259 	if (start_offset > SSHBUF_SIZE_MAX || len > SSHBUF_SIZE_MAX || len == 0)
260 		return SSH_ERR_INVALID_ARGUMENT;
261 	if (start_offset > sshbuf_len(b) || start_offset + len > sshbuf_len(b))
262 		return SSH_ERR_MESSAGE_INCOMPLETE;
263 	if ((p = memmem(sshbuf_ptr(b) + start_offset,
264 	    sshbuf_len(b) - start_offset, s, len)) == NULL)
265 		return SSH_ERR_INVALID_FORMAT;
266 	if (offsetp != NULL)
267 		*offsetp = (const u_char *)p - sshbuf_ptr(b);
268 	return 0;
269 }
270