/src/openssh/sshbuf-misc.c
Line | Count | Source |
1 | | /* $OpenBSD: sshbuf-misc.c,v 1.22 2025/09/04 00:32:31 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 | | |
18 | | #include "includes.h" |
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 | | #include <unistd.h> |
32 | | |
33 | | #include "ssherr.h" |
34 | | #define SSHBUF_INTERNAL |
35 | | #include "sshbuf.h" |
36 | | |
37 | | void |
38 | | sshbuf_dump_data(const void *s, size_t len, FILE *f) |
39 | 0 | { |
40 | 0 | size_t i, j; |
41 | 0 | const u_char *p = (const u_char *)s; |
42 | |
|
43 | 0 | for (i = 0; i < len; i += 16) { |
44 | 0 | fprintf(f, "%.4zu: ", i); |
45 | 0 | for (j = i; j < i + 16; j++) { |
46 | 0 | if (j < len) |
47 | 0 | fprintf(f, "%02x ", p[j]); |
48 | 0 | else |
49 | 0 | fprintf(f, " "); |
50 | 0 | } |
51 | 0 | fprintf(f, " "); |
52 | 0 | for (j = i; j < i + 16; j++) { |
53 | 0 | if (j < len) { |
54 | 0 | if (isascii(p[j]) && isprint(p[j])) |
55 | 0 | fprintf(f, "%c", p[j]); |
56 | 0 | else |
57 | 0 | fprintf(f, "."); |
58 | 0 | } |
59 | 0 | } |
60 | 0 | fprintf(f, "\n"); |
61 | 0 | } |
62 | 0 | } |
63 | | |
64 | | void |
65 | | sshbuf_dump(const struct sshbuf *buf, FILE *f) |
66 | 0 | { |
67 | 0 | fprintf(f, "buffer len = %zu\n", sshbuf_len(buf)); |
68 | 0 | sshbuf_dump_data(sshbuf_ptr(buf), sshbuf_len(buf), f); |
69 | 0 | } |
70 | | |
71 | | char * |
72 | | sshbuf_dtob16(const struct sshbuf *buf) |
73 | 0 | { |
74 | 0 | size_t i, j, len = sshbuf_len(buf); |
75 | 0 | const u_char *p = sshbuf_ptr(buf); |
76 | 0 | char *ret; |
77 | 0 | const char hex[] = "0123456789abcdef"; |
78 | |
|
79 | 0 | if (len == 0) |
80 | 0 | return strdup(""); |
81 | 0 | if (SIZE_MAX / 2 <= len || (ret = malloc(len * 2 + 1)) == NULL) |
82 | 0 | return NULL; |
83 | 0 | for (i = j = 0; i < len; i++) { |
84 | 0 | ret[j++] = hex[(p[i] >> 4) & 0xf]; |
85 | 0 | ret[j++] = hex[p[i] & 0xf]; |
86 | 0 | } |
87 | 0 | ret[j] = '\0'; |
88 | 0 | return ret; |
89 | 0 | } |
90 | | |
91 | | static int |
92 | | b16tod(const char v) |
93 | 0 | { |
94 | 0 | if (v >= '0' && v <= '9') |
95 | 0 | return v - '0'; |
96 | 0 | if (v >= 'a' && v <= 'f') |
97 | 0 | return 10 + v - 'a'; |
98 | 0 | if (v >= 'A' && v <= 'A') |
99 | 0 | return 10 + v - 'A'; |
100 | 0 | return -1; |
101 | 0 | } |
102 | | |
103 | | struct sshbuf * |
104 | | sshbuf_b16tod(const char *b16) |
105 | 0 | { |
106 | 0 | struct sshbuf *ret; |
107 | 0 | size_t o; |
108 | 0 | int r, v1, v2; |
109 | |
|
110 | 0 | if ((ret = sshbuf_new()) == NULL) |
111 | 0 | return NULL; |
112 | 0 | for (o = 0; b16[o] != '\0'; o += 2) { |
113 | 0 | if ((v1 = b16tod(b16[o])) == -1 || |
114 | 0 | (v2 = b16tod(b16[o + 1])) == -1) { |
115 | 0 | sshbuf_free(ret); |
116 | 0 | return NULL; |
117 | 0 | } |
118 | 0 | if ((r = sshbuf_put_u8(ret, (u_char)((v1 << 4) | v2))) != 0) { |
119 | 0 | sshbuf_free(ret); |
120 | 0 | return NULL; |
121 | 0 | } |
122 | 0 | } |
123 | | /* success */ |
124 | 0 | return ret; |
125 | 0 | } |
126 | | |
127 | | int |
128 | | sshbuf_dtob64(const struct sshbuf *d, struct sshbuf *b64, int wrap) |
129 | 0 | { |
130 | 0 | size_t i, slen = 0; |
131 | 0 | char *s = NULL; |
132 | 0 | int r; |
133 | |
|
134 | 0 | if (d == NULL || b64 == NULL || sshbuf_len(d) >= SIZE_MAX / 2) |
135 | 0 | return SSH_ERR_INVALID_ARGUMENT; |
136 | 0 | if (sshbuf_len(d) == 0) |
137 | 0 | return 0; |
138 | 0 | slen = ((sshbuf_len(d) + 2) / 3) * 4 + 1; |
139 | 0 | if ((s = malloc(slen)) == NULL) |
140 | 0 | return SSH_ERR_ALLOC_FAIL; |
141 | 0 | if (b64_ntop(sshbuf_ptr(d), sshbuf_len(d), s, slen) == -1) { |
142 | 0 | r = SSH_ERR_INTERNAL_ERROR; |
143 | 0 | goto fail; |
144 | 0 | } |
145 | 0 | if (wrap) { |
146 | 0 | for (i = 0; s[i] != '\0'; i++) { |
147 | 0 | if ((r = sshbuf_put_u8(b64, s[i])) != 0) |
148 | 0 | goto fail; |
149 | 0 | if (i % 70 == 69 && (r = sshbuf_put_u8(b64, '\n')) != 0) |
150 | 0 | goto fail; |
151 | 0 | } |
152 | 0 | if ((i - 1) % 70 != 69 && (r = sshbuf_put_u8(b64, '\n')) != 0) |
153 | 0 | goto fail; |
154 | 0 | } else { |
155 | 0 | if ((r = sshbuf_put(b64, s, strlen(s))) != 0) |
156 | 0 | goto fail; |
157 | 0 | } |
158 | | /* Success */ |
159 | 0 | r = 0; |
160 | 0 | fail: |
161 | 0 | freezero(s, slen); |
162 | 0 | return r; |
163 | 0 | } |
164 | | |
165 | | char * |
166 | | sshbuf_dtob64_string(const struct sshbuf *buf, int wrap) |
167 | 0 | { |
168 | 0 | struct sshbuf *tmp; |
169 | 0 | char *ret; |
170 | |
|
171 | 0 | if ((tmp = sshbuf_new()) == NULL) |
172 | 0 | return NULL; |
173 | 0 | if (sshbuf_dtob64(buf, tmp, wrap) != 0) { |
174 | 0 | sshbuf_free(tmp); |
175 | 0 | return NULL; |
176 | 0 | } |
177 | 0 | ret = sshbuf_dup_string(tmp); |
178 | 0 | sshbuf_free(tmp); |
179 | 0 | return ret; |
180 | 0 | } |
181 | | |
182 | | int |
183 | | sshbuf_b64tod(struct sshbuf *buf, const char *b64) |
184 | 6 | { |
185 | 6 | size_t plen = strlen(b64); |
186 | 6 | int nlen, r; |
187 | 6 | u_char *p; |
188 | | |
189 | 6 | if (plen == 0) |
190 | 0 | return 0; |
191 | 6 | if ((p = malloc(plen)) == NULL) |
192 | 0 | return SSH_ERR_ALLOC_FAIL; |
193 | 6 | if ((nlen = b64_pton(b64, p, plen)) < 0) { |
194 | 0 | freezero(p, plen); |
195 | 0 | return SSH_ERR_INVALID_FORMAT; |
196 | 0 | } |
197 | 6 | if ((r = sshbuf_put(buf, p, nlen)) < 0) { |
198 | 0 | freezero(p, plen); |
199 | 0 | return r; |
200 | 0 | } |
201 | 6 | freezero(p, plen); |
202 | 6 | return 0; |
203 | 6 | } |
204 | | |
205 | | int |
206 | | sshbuf_dtourlb64(const struct sshbuf *d, struct sshbuf *b64, int wrap) |
207 | 0 | { |
208 | 0 | int r = SSH_ERR_INTERNAL_ERROR; |
209 | 0 | u_char *p; |
210 | 0 | struct sshbuf *b = NULL; |
211 | 0 | size_t i, l; |
212 | |
|
213 | 0 | if (sshbuf_len(d) == 0) |
214 | 0 | return 0; |
215 | | |
216 | 0 | if ((b = sshbuf_new()) == NULL) |
217 | 0 | return SSH_ERR_ALLOC_FAIL; |
218 | | /* Encode using regular base64; we'll transform it once done */ |
219 | 0 | if ((r = sshbuf_dtob64(d, b, wrap)) != 0) |
220 | 0 | goto out; |
221 | | /* remove padding from end of encoded string*/ |
222 | 0 | for (;;) { |
223 | 0 | l = sshbuf_len(b); |
224 | 0 | if (l <= 1 || sshbuf_ptr(b) == NULL) { |
225 | 0 | r = SSH_ERR_INTERNAL_ERROR; |
226 | 0 | goto out; |
227 | 0 | } |
228 | 0 | if (sshbuf_ptr(b)[l - 1] != '=') |
229 | 0 | break; |
230 | 0 | if ((r = sshbuf_consume_end(b, 1)) != 0) |
231 | 0 | goto out; |
232 | 0 | } |
233 | | /* Replace characters with rfc4648 equivalents */ |
234 | 0 | l = sshbuf_len(b); |
235 | 0 | if ((p = sshbuf_mutable_ptr(b)) == NULL) { |
236 | 0 | r = SSH_ERR_INTERNAL_ERROR; |
237 | 0 | goto out; |
238 | 0 | } |
239 | 0 | for (i = 0; i < l; i++) { |
240 | 0 | if (p[i] == '+') |
241 | 0 | p[i] = '-'; |
242 | 0 | else if (p[i] == '/') |
243 | 0 | p[i] = '_'; |
244 | 0 | } |
245 | 0 | r = sshbuf_putb(b64, b); |
246 | 0 | out: |
247 | 0 | sshbuf_free(b); |
248 | 0 | return r; |
249 | 0 | } |
250 | | |
251 | | char * |
252 | | sshbuf_dup_string(struct sshbuf *buf) |
253 | 225k | { |
254 | 225k | const u_char *p = NULL, *s = sshbuf_ptr(buf); |
255 | 225k | size_t l = sshbuf_len(buf); |
256 | 225k | char *r; |
257 | | |
258 | 225k | if (s == NULL || l >= SIZE_MAX) |
259 | 0 | return NULL; |
260 | | /* accept a nul only as the last character in the buffer */ |
261 | 225k | if (l > 0 && (p = memchr(s, '\0', l)) != NULL) { |
262 | 555 | if (p != s + l - 1) |
263 | 195 | return NULL; |
264 | 360 | l--; /* the nul is put back below */ |
265 | 360 | } |
266 | 225k | if ((r = malloc(l + 1)) == NULL) |
267 | 0 | return NULL; |
268 | 225k | if (l > 0) |
269 | 225k | memcpy(r, s, l); |
270 | 225k | r[l] = '\0'; |
271 | 225k | return r; |
272 | 225k | } |
273 | | |
274 | | int |
275 | | sshbuf_cmp(const struct sshbuf *b, size_t offset, |
276 | | const void *s, size_t len) |
277 | 0 | { |
278 | 0 | if (sshbuf_ptr(b) == NULL) |
279 | 0 | return SSH_ERR_INTERNAL_ERROR; |
280 | 0 | if (offset > SSHBUF_SIZE_MAX || len > SSHBUF_SIZE_MAX || len == 0) |
281 | 0 | return SSH_ERR_INVALID_ARGUMENT; |
282 | 0 | if (offset + len > sshbuf_len(b)) |
283 | 0 | return SSH_ERR_MESSAGE_INCOMPLETE; |
284 | 0 | if (timingsafe_bcmp(sshbuf_ptr(b) + offset, s, len) != 0) |
285 | 0 | return SSH_ERR_INVALID_FORMAT; |
286 | 0 | return 0; |
287 | 0 | } |
288 | | |
289 | | int |
290 | | sshbuf_equals(const struct sshbuf *a, const struct sshbuf *b) |
291 | 0 | { |
292 | 0 | if (sshbuf_ptr(a) == NULL || sshbuf_ptr(b) == NULL) |
293 | 0 | return SSH_ERR_INTERNAL_ERROR; |
294 | 0 | if (sshbuf_len(a) != sshbuf_len(b)) |
295 | 0 | return SSH_ERR_MESSAGE_INCOMPLETE; |
296 | 0 | if (sshbuf_len(a) == 0) |
297 | 0 | return 0; |
298 | 0 | if (memcmp(sshbuf_ptr(a), sshbuf_ptr(b), sshbuf_len(a)) != 0) |
299 | 0 | return SSH_ERR_INVALID_FORMAT; |
300 | 0 | return 0; |
301 | 0 | } |
302 | | |
303 | | int |
304 | | sshbuf_find(const struct sshbuf *b, size_t start_offset, |
305 | | const void *s, size_t len, size_t *offsetp) |
306 | 0 | { |
307 | 0 | void *p; |
308 | |
|
309 | 0 | if (offsetp != NULL) |
310 | 0 | *offsetp = 0; |
311 | 0 | if (sshbuf_ptr(b) == NULL) |
312 | 0 | return SSH_ERR_INTERNAL_ERROR; |
313 | 0 | if (start_offset > SSHBUF_SIZE_MAX || len > SSHBUF_SIZE_MAX || len == 0) |
314 | 0 | return SSH_ERR_INVALID_ARGUMENT; |
315 | 0 | if (start_offset > sshbuf_len(b) || start_offset + len > sshbuf_len(b)) |
316 | 0 | return SSH_ERR_MESSAGE_INCOMPLETE; |
317 | 0 | if ((p = memmem(sshbuf_ptr(b) + start_offset, |
318 | 0 | sshbuf_len(b) - start_offset, s, len)) == NULL) |
319 | 0 | return SSH_ERR_INVALID_FORMAT; |
320 | 0 | if (offsetp != NULL) |
321 | 0 | *offsetp = (const u_char *)p - sshbuf_ptr(b); |
322 | 0 | return 0; |
323 | 0 | } |
324 | | |
325 | | int |
326 | | sshbuf_read(int fd, struct sshbuf *buf, size_t maxlen, size_t *rlen) |
327 | 0 | { |
328 | 0 | int r, oerrno; |
329 | 0 | size_t adjust; |
330 | 0 | ssize_t rr; |
331 | 0 | u_char *d; |
332 | |
|
333 | 0 | if (rlen != NULL) |
334 | 0 | *rlen = 0; |
335 | 0 | if ((r = sshbuf_reserve(buf, maxlen, &d)) != 0) |
336 | 0 | return r; |
337 | 0 | rr = read(fd, d, maxlen); |
338 | 0 | oerrno = errno; |
339 | | |
340 | | /* Adjust the buffer to include only what was actually read */ |
341 | 0 | if ((adjust = maxlen - (rr > 0 ? rr : 0)) != 0) { |
342 | 0 | if ((r = sshbuf_consume_end(buf, adjust)) != 0) { |
343 | | /* avoid returning uninitialised data to caller */ |
344 | 0 | memset(d + rr, '\0', adjust); |
345 | 0 | return SSH_ERR_INTERNAL_ERROR; /* shouldn't happen */ |
346 | 0 | } |
347 | 0 | } |
348 | 0 | if (rr < 0) { |
349 | 0 | errno = oerrno; |
350 | 0 | return SSH_ERR_SYSTEM_ERROR; |
351 | 0 | } else if (rr == 0) { |
352 | 0 | errno = EPIPE; |
353 | 0 | return SSH_ERR_SYSTEM_ERROR; |
354 | 0 | } |
355 | | /* success */ |
356 | 0 | if (rlen != NULL) |
357 | 0 | *rlen = (size_t)rr; |
358 | 0 | return 0; |
359 | 0 | } |