1 /* $OpenBSD: sshbuf-misc.c,v 1.18 2022/01/22 00:43:43 djm Exp $ */
3 * Copyright (c) 2011 Damien Miller
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.
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.
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <netinet/in.h>
36 #define SSHBUF_INTERNAL
40 sshbuf_dump_data(const void *s
, size_t len
, FILE *f
)
43 const u_char
*p
= (const u_char
*)s
;
45 for (i
= 0; i
< len
; i
+= 16) {
46 fprintf(f
, "%.4zu: ", i
);
47 for (j
= i
; j
< i
+ 16; j
++) {
49 fprintf(f
, "%02x ", p
[j
]);
54 for (j
= i
; j
< i
+ 16; j
++) {
56 if (isascii(p
[j
]) && isprint(p
[j
]))
57 fprintf(f
, "%c", p
[j
]);
67 sshbuf_dump(const struct sshbuf
*buf
, FILE *f
)
69 fprintf(f
, "buffer len = %zu\n", sshbuf_len(buf
));
70 sshbuf_dump_data(sshbuf_ptr(buf
), sshbuf_len(buf
), f
);
74 sshbuf_dtob16(struct sshbuf
*buf
)
76 size_t i
, j
, len
= sshbuf_len(buf
);
77 const u_char
*p
= sshbuf_ptr(buf
);
79 const char hex
[] = "0123456789abcdef";
83 if (SIZE_MAX
/ 2 <= len
|| (ret
= malloc(len
* 2 + 1)) == NULL
)
85 for (i
= j
= 0; i
< len
; i
++) {
86 ret
[j
++] = hex
[(p
[i
] >> 4) & 0xf];
87 ret
[j
++] = hex
[p
[i
] & 0xf];
94 sshbuf_dtob64(const struct sshbuf
*d
, struct sshbuf
*b64
, int wrap
)
100 if (d
== NULL
|| b64
== NULL
|| sshbuf_len(d
) >= SIZE_MAX
/ 2)
101 return SSH_ERR_INVALID_ARGUMENT
;
102 if (sshbuf_len(d
) == 0)
104 slen
= ((sshbuf_len(d
) + 2) / 3) * 4 + 1;
105 if ((s
= malloc(slen
)) == NULL
)
106 return SSH_ERR_ALLOC_FAIL
;
107 if (b64_ntop(sshbuf_ptr(d
), sshbuf_len(d
), s
, slen
) == -1) {
108 r
= SSH_ERR_INTERNAL_ERROR
;
112 for (i
= 0; s
[i
] != '\0'; i
++) {
113 if ((r
= sshbuf_put_u8(b64
, s
[i
])) != 0)
115 if (i
% 70 == 69 && (r
= sshbuf_put_u8(b64
, '\n')) != 0)
118 if ((i
- 1) % 70 != 69 && (r
= sshbuf_put_u8(b64
, '\n')) != 0)
121 if ((r
= sshbuf_put(b64
, s
, strlen(s
))) != 0)
132 sshbuf_dtob64_string(const struct sshbuf
*buf
, int wrap
)
137 if ((tmp
= sshbuf_new()) == NULL
)
139 if (sshbuf_dtob64(buf
, tmp
, wrap
) != 0) {
143 ret
= sshbuf_dup_string(tmp
);
149 sshbuf_b64tod(struct sshbuf
*buf
, const char *b64
)
151 size_t plen
= strlen(b64
);
157 if ((p
= malloc(plen
)) == NULL
)
158 return SSH_ERR_ALLOC_FAIL
;
159 if ((nlen
= b64_pton(b64
, p
, plen
)) < 0) {
161 return SSH_ERR_INVALID_FORMAT
;
163 if ((r
= sshbuf_put(buf
, p
, nlen
)) < 0) {
172 sshbuf_dtourlb64(const struct sshbuf
*d
, struct sshbuf
*b64
, int wrap
)
174 int r
= SSH_ERR_INTERNAL_ERROR
;
176 struct sshbuf
*b
= NULL
;
179 if ((b
= sshbuf_new()) == NULL
)
180 return SSH_ERR_ALLOC_FAIL
;
181 /* Encode using regular base64; we'll transform it once done */
182 if ((r
= sshbuf_dtob64(d
, b
, wrap
)) != 0)
184 /* remove padding from end of encoded string*/
187 if (l
<= 1 || sshbuf_ptr(b
) == NULL
) {
188 r
= SSH_ERR_INTERNAL_ERROR
;
191 if (sshbuf_ptr(b
)[l
- 1] != '=')
193 if ((r
= sshbuf_consume_end(b
, 1)) != 0)
196 /* Replace characters with rfc4648 equivalents */
198 if ((p
= sshbuf_mutable_ptr(b
)) == NULL
) {
199 r
= SSH_ERR_INTERNAL_ERROR
;
202 for (i
= 0; i
< l
; i
++) {
205 else if (p
[i
] == '/')
208 r
= sshbuf_putb(b64
, b
);
215 sshbuf_dup_string(struct sshbuf
*buf
)
217 const u_char
*p
= NULL
, *s
= sshbuf_ptr(buf
);
218 size_t l
= sshbuf_len(buf
);
221 if (s
== NULL
|| l
> SIZE_MAX
)
223 /* accept a nul only as the last character in the buffer */
224 if (l
> 0 && (p
= memchr(s
, '\0', l
)) != NULL
) {
227 l
--; /* the nul is put back below */
229 if ((r
= malloc(l
+ 1)) == NULL
)
238 sshbuf_cmp(const struct sshbuf
*b
, size_t offset
,
239 const void *s
, size_t len
)
241 if (sshbuf_ptr(b
) == NULL
)
242 return SSH_ERR_INTERNAL_ERROR
;
243 if (offset
> SSHBUF_SIZE_MAX
|| len
> SSHBUF_SIZE_MAX
|| len
== 0)
244 return SSH_ERR_INVALID_ARGUMENT
;
245 if (offset
+ len
> sshbuf_len(b
))
246 return SSH_ERR_MESSAGE_INCOMPLETE
;
247 if (timingsafe_bcmp(sshbuf_ptr(b
) + offset
, s
, len
) != 0)
248 return SSH_ERR_INVALID_FORMAT
;
253 sshbuf_find(const struct sshbuf
*b
, size_t start_offset
,
254 const void *s
, size_t len
, size_t *offsetp
)
260 if (sshbuf_ptr(b
) == NULL
)
261 return SSH_ERR_INTERNAL_ERROR
;
262 if (start_offset
> SSHBUF_SIZE_MAX
|| len
> SSHBUF_SIZE_MAX
|| len
== 0)
263 return SSH_ERR_INVALID_ARGUMENT
;
264 if (start_offset
> sshbuf_len(b
) || start_offset
+ len
> sshbuf_len(b
))
265 return SSH_ERR_MESSAGE_INCOMPLETE
;
266 if ((p
= memmem(sshbuf_ptr(b
) + start_offset
,
267 sshbuf_len(b
) - start_offset
, s
, len
)) == NULL
)
268 return SSH_ERR_INVALID_FORMAT
;
270 *offsetp
= (const u_char
*)p
- sshbuf_ptr(b
);
275 sshbuf_read(int fd
, struct sshbuf
*buf
, size_t maxlen
, size_t *rlen
)
284 if ((r
= sshbuf_reserve(buf
, maxlen
, &d
)) != 0)
286 rr
= read(fd
, d
, maxlen
);
289 /* Adjust the buffer to include only what was actually read */
290 if ((adjust
= maxlen
- (rr
> 0 ? rr
: 0)) != 0) {
291 if ((r
= sshbuf_consume_end(buf
, adjust
)) != 0) {
292 /* avoid returning uninitialised data to caller */
293 memset(d
+ rr
, '\0', adjust
);
294 return SSH_ERR_INTERNAL_ERROR
; /* shouldn't happen */
299 return SSH_ERR_SYSTEM_ERROR
;
300 } else if (rr
== 0) {
302 return SSH_ERR_SYSTEM_ERROR
;