xref: /netbsd-src/crypto/external/bsd/openssh/dist/sshbuf-misc.c (revision 154bfe8e089c1a0a4e9ed8414f08d3da90949162)
1 /*	$OpenBSD: sshbuf-misc.c,v 1.14 2020/02/26 13:40:09 jsg 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.11 2020/05/28 17:05:49 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(struct sshbuf *buf, FILE *f)
65 {
66 	fprintf(f, "buffer %p len = %zu\n", buf, 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 char *
169 sshbuf_dup_string(struct sshbuf *buf)
170 {
171 	const u_char *p = NULL, *s = sshbuf_ptr(buf);
172 	size_t l = sshbuf_len(buf);
173 	char *r;
174 
175 	if (s == NULL || l > SIZE_MAX)
176 		return NULL;
177 	/* accept a nul only as the last character in the buffer */
178 	if (l > 0 && (p = memchr(s, '\0', l)) != NULL) {
179 		if (p != s + l - 1)
180 			return NULL;
181 		l--; /* the nul is put back below */
182 	}
183 	if ((r = malloc(l + 1)) == NULL)
184 		return NULL;
185 	if (l > 0)
186 		memcpy(r, s, l);
187 	r[l] = '\0';
188 	return r;
189 }
190 
191 int
192 sshbuf_cmp(const struct sshbuf *b, size_t offset,
193     const void *s, size_t len)
194 {
195 	if (sshbuf_ptr(b) == NULL)
196 		return SSH_ERR_INTERNAL_ERROR;
197 	if (offset > SSHBUF_SIZE_MAX || len > SSHBUF_SIZE_MAX || len == 0)
198 		return SSH_ERR_INVALID_ARGUMENT;
199 	if (offset + len > sshbuf_len(b))
200 		return SSH_ERR_MESSAGE_INCOMPLETE;
201 	if (timingsafe_bcmp(sshbuf_ptr(b) + offset, s, len) != 0)
202 		return SSH_ERR_INVALID_FORMAT;
203 	return 0;
204 }
205 
206 int
207 sshbuf_find(const struct sshbuf *b, size_t start_offset,
208     const void *s, size_t len, size_t *offsetp)
209 {
210 	void *p;
211 
212 	if (offsetp != NULL)
213 		*offsetp = 0;
214 	if (sshbuf_ptr(b) == NULL)
215 		return SSH_ERR_INTERNAL_ERROR;
216 	if (start_offset > SSHBUF_SIZE_MAX || len > SSHBUF_SIZE_MAX || len == 0)
217 		return SSH_ERR_INVALID_ARGUMENT;
218 	if (start_offset > sshbuf_len(b) || start_offset + len > sshbuf_len(b))
219 		return SSH_ERR_MESSAGE_INCOMPLETE;
220 	if ((p = memmem(sshbuf_ptr(b) + start_offset,
221 	    sshbuf_len(b) - start_offset, s, len)) == NULL)
222 		return SSH_ERR_INVALID_FORMAT;
223 	if (offsetp != NULL)
224 		*offsetp = (const u_char *)p - sshbuf_ptr(b);
225 	return 0;
226 }
227