xref: /netbsd-src/crypto/external/bsd/openssh/dist/sshbuf-misc.c (revision e6c7e151de239c49d2e38720a061ed9d1fa99309)
1 /*	$OpenBSD: sshbuf-misc.c,v 1.13 2020/01/25 23:28:06 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.10 2020/02/27 00:24:40 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 		explicit_bzero(p, plen);
158 		free(p);
159 		return SSH_ERR_INVALID_FORMAT;
160 	}
161 	if ((r = sshbuf_put(buf, p, nlen)) < 0) {
162 		explicit_bzero(p, plen);
163 		free(p);
164 		return r;
165 	}
166 	explicit_bzero(p, plen);
167 	free(p);
168 	return 0;
169 }
170 
171 char *
172 sshbuf_dup_string(struct sshbuf *buf)
173 {
174 	const u_char *p = NULL, *s = sshbuf_ptr(buf);
175 	size_t l = sshbuf_len(buf);
176 	char *r;
177 
178 	if (s == NULL || l > SIZE_MAX)
179 		return NULL;
180 	/* accept a nul only as the last character in the buffer */
181 	if (l > 0 && (p = memchr(s, '\0', l)) != NULL) {
182 		if (p != s + l - 1)
183 			return NULL;
184 		l--; /* the nul is put back below */
185 	}
186 	if ((r = malloc(l + 1)) == NULL)
187 		return NULL;
188 	if (l > 0)
189 		memcpy(r, s, l);
190 	r[l] = '\0';
191 	return r;
192 }
193 
194 int
195 sshbuf_cmp(const struct sshbuf *b, size_t offset,
196     const void *s, size_t len)
197 {
198 	if (sshbuf_ptr(b) == NULL)
199 		return SSH_ERR_INTERNAL_ERROR;
200 	if (offset > SSHBUF_SIZE_MAX || len > SSHBUF_SIZE_MAX || len == 0)
201 		return SSH_ERR_INVALID_ARGUMENT;
202 	if (offset + len > sshbuf_len(b))
203 		return SSH_ERR_MESSAGE_INCOMPLETE;
204 	if (timingsafe_bcmp(sshbuf_ptr(b) + offset, s, len) != 0)
205 		return SSH_ERR_INVALID_FORMAT;
206 	return 0;
207 }
208 
209 int
210 sshbuf_find(const struct sshbuf *b, size_t start_offset,
211     const void *s, size_t len, size_t *offsetp)
212 {
213 	void *p;
214 
215 	if (offsetp != NULL)
216 		*offsetp = 0;
217 	if (sshbuf_ptr(b) == NULL)
218 		return SSH_ERR_INTERNAL_ERROR;
219 	if (start_offset > SSHBUF_SIZE_MAX || len > SSHBUF_SIZE_MAX || len == 0)
220 		return SSH_ERR_INVALID_ARGUMENT;
221 	if (start_offset > sshbuf_len(b) || start_offset + len > sshbuf_len(b))
222 		return SSH_ERR_MESSAGE_INCOMPLETE;
223 	if ((p = memmem(sshbuf_ptr(b) + start_offset,
224 	    sshbuf_len(b) - start_offset, s, len)) == NULL)
225 		return SSH_ERR_INVALID_FORMAT;
226 	if (offsetp != NULL)
227 		*offsetp = (const u_char *)p - sshbuf_ptr(b);
228 	return 0;
229 }
230