blob: a73f008b0c82af8bf1f88fbd17d1abd346d97149 [file] [log] [blame]
djm@openbsd.org49fa0652019-07-30 05:04:49 +00001/* $OpenBSD: sshbuf-misc.c,v 1.11 2019/07/30 05:04:49 djm Exp $ */
Damien Miller05e82c32014-05-15 14:33:43 +10002/*
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>
Darren Tucker37f92202015-02-23 03:07:24 +110025#ifdef HAVE_STDINT_H
Damien Millercfc18972019-10-09 09:06:35 +110026# include <stdint.h>
Darren Tucker37f92202015-02-23 03:07:24 +110027#endif
Damien Miller05e82c32014-05-15 14:33:43 +100028#include <stdio.h>
29#include <limits.h>
30#include <string.h>
31#include <resolv.h>
32#include <ctype.h>
33
34#include "ssherr.h"
35#define SSHBUF_INTERNAL
36#include "sshbuf.h"
37
38void
Damien Miller86687062014-07-02 15:28:02 +100039sshbuf_dump_data(const void *s, size_t len, FILE *f)
Damien Miller05e82c32014-05-15 14:33:43 +100040{
Damien Miller86687062014-07-02 15:28:02 +100041 size_t i, j;
42 const u_char *p = (const u_char *)s;
Damien Miller05e82c32014-05-15 14:33:43 +100043
Damien Miller05e82c32014-05-15 14:33:43 +100044 for (i = 0; i < len; i += 16) {
markus@openbsd.orgdf100be2015-03-24 20:03:44 +000045 fprintf(f, "%.4zu: ", i);
Damien Miller05e82c32014-05-15 14:33:43 +100046 for (j = i; j < i + 16; j++) {
47 if (j < len)
48 fprintf(f, "%02x ", p[j]);
49 else
50 fprintf(f, " ");
51 }
52 fprintf(f, " ");
53 for (j = i; j < i + 16; j++) {
54 if (j < len) {
55 if (isascii(p[j]) && isprint(p[j]))
56 fprintf(f, "%c", p[j]);
57 else
58 fprintf(f, ".");
59 }
60 }
61 fprintf(f, "\n");
62 }
63}
64
Damien Miller86687062014-07-02 15:28:02 +100065void
66sshbuf_dump(struct sshbuf *buf, FILE *f)
67{
68 fprintf(f, "buffer %p len = %zu\n", buf, sshbuf_len(buf));
69 sshbuf_dump_data(sshbuf_ptr(buf), sshbuf_len(buf), f);
70}
71
Damien Miller05e82c32014-05-15 14:33:43 +100072char *
73sshbuf_dtob16(struct sshbuf *buf)
74{
75 size_t i, j, len = sshbuf_len(buf);
76 const u_char *p = sshbuf_ptr(buf);
77 char *ret;
78 const char hex[] = "0123456789abcdef";
79
80 if (len == 0)
81 return strdup("");
82 if (SIZE_MAX / 2 <= len || (ret = malloc(len * 2 + 1)) == NULL)
83 return NULL;
84 for (i = j = 0; i < len; i++) {
85 ret[j++] = hex[(p[i] >> 4) & 0xf];
86 ret[j++] = hex[p[i] & 0xf];
87 }
88 ret[j] = '\0';
89 return ret;
90}
91
djm@openbsd.org16dd8b22019-07-16 13:18:39 +000092int
93sshbuf_dtob64(const struct sshbuf *d, struct sshbuf *b64, int wrap)
Damien Miller05e82c32014-05-15 14:33:43 +100094{
djm@openbsd.org16dd8b22019-07-16 13:18:39 +000095 size_t i, slen = 0;
96 char *s = NULL;
97 int r;
98
99 if (d == NULL || b64 == NULL || sshbuf_len(d) >= SIZE_MAX / 2)
100 return SSH_ERR_INVALID_ARGUMENT;
101 if (sshbuf_len(d) == 0)
102 return 0;
103 slen = ((sshbuf_len(d) + 2) / 3) * 4 + 1;
104 if ((s = malloc(slen)) == NULL)
105 return SSH_ERR_ALLOC_FAIL;
106 if (b64_ntop(sshbuf_ptr(d), sshbuf_len(d), s, slen) == -1) {
107 r = SSH_ERR_INTERNAL_ERROR;
108 goto fail;
109 }
110 if (wrap) {
111 for (i = 0; s[i] != '\0'; i++) {
112 if ((r = sshbuf_put_u8(b64, s[i])) != 0)
113 goto fail;
114 if (i % 70 == 69 && (r = sshbuf_put_u8(b64, '\n')) != 0)
115 goto fail;
116 }
djm@openbsd.orged46a0c2019-07-18 13:26:00 +0000117 if ((i - 1) % 70 != 69 && (r = sshbuf_put_u8(b64, '\n')) != 0)
djm@openbsd.org16dd8b22019-07-16 13:18:39 +0000118 goto fail;
119 } else {
120 if ((r = sshbuf_put(b64, s, strlen(s))) != 0)
121 goto fail;
122 }
123 /* Success */
124 r = 0;
125 fail:
126 freezero(s, slen);
127 return r;
128}
129
130char *
131sshbuf_dtob64_string(const struct sshbuf *buf, int wrap)
132{
133 struct sshbuf *tmp;
Damien Miller05e82c32014-05-15 14:33:43 +1000134 char *ret;
Damien Miller05e82c32014-05-15 14:33:43 +1000135
djm@openbsd.org16dd8b22019-07-16 13:18:39 +0000136 if ((tmp = sshbuf_new()) == NULL)
Damien Miller05e82c32014-05-15 14:33:43 +1000137 return NULL;
djm@openbsd.org16dd8b22019-07-16 13:18:39 +0000138 if (sshbuf_dtob64(buf, tmp, wrap) != 0) {
139 sshbuf_free(tmp);
Damien Miller05e82c32014-05-15 14:33:43 +1000140 return NULL;
141 }
djm@openbsd.org16dd8b22019-07-16 13:18:39 +0000142 ret = sshbuf_dup_string(tmp);
143 sshbuf_free(tmp);
Damien Miller05e82c32014-05-15 14:33:43 +1000144 return ret;
145}
146
147int
148sshbuf_b64tod(struct sshbuf *buf, const char *b64)
149{
150 size_t plen = strlen(b64);
151 int nlen, r;
152 u_char *p;
153
154 if (plen == 0)
155 return 0;
156 if ((p = malloc(plen)) == NULL)
157 return SSH_ERR_ALLOC_FAIL;
158 if ((nlen = b64_pton(b64, p, plen)) < 0) {
djm@openbsd.org905b0542015-10-05 17:11:21 +0000159 explicit_bzero(p, plen);
Damien Miller05e82c32014-05-15 14:33:43 +1000160 free(p);
161 return SSH_ERR_INVALID_FORMAT;
162 }
163 if ((r = sshbuf_put(buf, p, nlen)) < 0) {
djm@openbsd.org905b0542015-10-05 17:11:21 +0000164 explicit_bzero(p, plen);
Damien Miller05e82c32014-05-15 14:33:43 +1000165 free(p);
166 return r;
167 }
djm@openbsd.org905b0542015-10-05 17:11:21 +0000168 explicit_bzero(p, plen);
Damien Miller05e82c32014-05-15 14:33:43 +1000169 free(p);
170 return 0;
171}
172
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000173char *
174sshbuf_dup_string(struct sshbuf *buf)
175{
176 const u_char *p = NULL, *s = sshbuf_ptr(buf);
177 size_t l = sshbuf_len(buf);
178 char *r;
179
180 if (s == NULL || l > SIZE_MAX)
181 return NULL;
182 /* accept a nul only as the last character in the buffer */
183 if (l > 0 && (p = memchr(s, '\0', l)) != NULL) {
184 if (p != s + l - 1)
185 return NULL;
186 l--; /* the nul is put back below */
187 }
188 if ((r = malloc(l + 1)) == NULL)
189 return NULL;
190 if (l > 0)
191 memcpy(r, s, l);
192 r[l] = '\0';
193 return r;
194}
195
djm@openbsd.orge18a27e2019-07-15 13:11:38 +0000196int
197sshbuf_cmp(const struct sshbuf *b, size_t offset,
djm@openbsd.org49fa0652019-07-30 05:04:49 +0000198 const void *s, size_t len)
djm@openbsd.orge18a27e2019-07-15 13:11:38 +0000199{
200 if (sshbuf_ptr(b) == NULL)
201 return SSH_ERR_INTERNAL_ERROR;
202 if (offset > SSHBUF_SIZE_MAX || len > SSHBUF_SIZE_MAX || len == 0)
203 return SSH_ERR_INVALID_ARGUMENT;
204 if (offset + len > sshbuf_len(b))
205 return SSH_ERR_MESSAGE_INCOMPLETE;
206 if (timingsafe_bcmp(sshbuf_ptr(b) + offset, s, len) != 0)
207 return SSH_ERR_INVALID_FORMAT;
208 return 0;
209}
210
211int
212sshbuf_find(const struct sshbuf *b, size_t start_offset,
djm@openbsd.org49fa0652019-07-30 05:04:49 +0000213 const void *s, size_t len, size_t *offsetp)
djm@openbsd.orge18a27e2019-07-15 13:11:38 +0000214{
215 void *p;
216
217 if (offsetp != NULL)
218 *offsetp = 0;
djm@openbsd.orge18a27e2019-07-15 13:11:38 +0000219 if (sshbuf_ptr(b) == NULL)
220 return SSH_ERR_INTERNAL_ERROR;
221 if (start_offset > SSHBUF_SIZE_MAX || len > SSHBUF_SIZE_MAX || len == 0)
222 return SSH_ERR_INVALID_ARGUMENT;
223 if (start_offset > sshbuf_len(b) || start_offset + len > sshbuf_len(b))
224 return SSH_ERR_MESSAGE_INCOMPLETE;
225 if ((p = memmem(sshbuf_ptr(b) + start_offset,
226 sshbuf_len(b) - start_offset, s, len)) == NULL)
227 return SSH_ERR_INVALID_FORMAT;
228 if (offsetp != NULL)
229 *offsetp = (const u_char *)p - sshbuf_ptr(b);
230 return 0;
231}