djm@openbsd.org | e18a27e | 2019-07-15 13:11:38 +0000 | [diff] [blame^] | 1 | /* $OpenBSD: sshbuf-misc.c,v 1.8 2019/07/15 13:11:38 djm Exp $ */ |
Damien Miller | 05e82c3 | 2014-05-15 14:33:43 +1000 | [diff] [blame] | 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> |
Darren Tucker | 37f9220 | 2015-02-23 03:07:24 +1100 | [diff] [blame] | 25 | #ifdef HAVE_STDINT_H |
millert@openbsd.org | 1910a28 | 2015-02-05 12:59:57 +0000 | [diff] [blame] | 26 | #include <stdint.h> |
Darren Tucker | 37f9220 | 2015-02-23 03:07:24 +1100 | [diff] [blame] | 27 | #endif |
Damien Miller | 05e82c3 | 2014-05-15 14:33:43 +1000 | [diff] [blame] | 28 | #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 | |
| 38 | void |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 39 | sshbuf_dump_data(const void *s, size_t len, FILE *f) |
Damien Miller | 05e82c3 | 2014-05-15 14:33:43 +1000 | [diff] [blame] | 40 | { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 41 | size_t i, j; |
| 42 | const u_char *p = (const u_char *)s; |
Damien Miller | 05e82c3 | 2014-05-15 14:33:43 +1000 | [diff] [blame] | 43 | |
Damien Miller | 05e82c3 | 2014-05-15 14:33:43 +1000 | [diff] [blame] | 44 | for (i = 0; i < len; i += 16) { |
markus@openbsd.org | df100be | 2015-03-24 20:03:44 +0000 | [diff] [blame] | 45 | fprintf(f, "%.4zu: ", i); |
Damien Miller | 05e82c3 | 2014-05-15 14:33:43 +1000 | [diff] [blame] | 46 | 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 Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 65 | void |
| 66 | sshbuf_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 Miller | 05e82c3 | 2014-05-15 14:33:43 +1000 | [diff] [blame] | 72 | char * |
| 73 | sshbuf_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 | |
| 92 | char * |
| 93 | sshbuf_dtob64(struct sshbuf *buf) |
| 94 | { |
| 95 | size_t len = sshbuf_len(buf), plen; |
| 96 | const u_char *p = sshbuf_ptr(buf); |
| 97 | char *ret; |
Damien Miller | 05e82c3 | 2014-05-15 14:33:43 +1000 | [diff] [blame] | 98 | |
| 99 | if (len == 0) |
| 100 | return strdup(""); |
| 101 | plen = ((len + 2) / 3) * 4 + 1; |
| 102 | if (SIZE_MAX / 2 <= len || (ret = malloc(plen)) == NULL) |
| 103 | return NULL; |
dtucker@openbsd.org | 696fb42 | 2019-07-07 01:05:00 +0000 | [diff] [blame] | 104 | if (b64_ntop(p, len, ret, plen) == -1) { |
djm@openbsd.org | 905b054 | 2015-10-05 17:11:21 +0000 | [diff] [blame] | 105 | explicit_bzero(ret, plen); |
Damien Miller | 05e82c3 | 2014-05-15 14:33:43 +1000 | [diff] [blame] | 106 | free(ret); |
| 107 | return NULL; |
| 108 | } |
| 109 | return ret; |
| 110 | } |
| 111 | |
| 112 | int |
| 113 | sshbuf_b64tod(struct sshbuf *buf, const char *b64) |
| 114 | { |
| 115 | size_t plen = strlen(b64); |
| 116 | int nlen, r; |
| 117 | u_char *p; |
| 118 | |
| 119 | if (plen == 0) |
| 120 | return 0; |
| 121 | if ((p = malloc(plen)) == NULL) |
| 122 | return SSH_ERR_ALLOC_FAIL; |
| 123 | if ((nlen = b64_pton(b64, p, plen)) < 0) { |
djm@openbsd.org | 905b054 | 2015-10-05 17:11:21 +0000 | [diff] [blame] | 124 | explicit_bzero(p, plen); |
Damien Miller | 05e82c3 | 2014-05-15 14:33:43 +1000 | [diff] [blame] | 125 | free(p); |
| 126 | return SSH_ERR_INVALID_FORMAT; |
| 127 | } |
| 128 | if ((r = sshbuf_put(buf, p, nlen)) < 0) { |
djm@openbsd.org | 905b054 | 2015-10-05 17:11:21 +0000 | [diff] [blame] | 129 | explicit_bzero(p, plen); |
Damien Miller | 05e82c3 | 2014-05-15 14:33:43 +1000 | [diff] [blame] | 130 | free(p); |
| 131 | return r; |
| 132 | } |
djm@openbsd.org | 905b054 | 2015-10-05 17:11:21 +0000 | [diff] [blame] | 133 | explicit_bzero(p, plen); |
Damien Miller | 05e82c3 | 2014-05-15 14:33:43 +1000 | [diff] [blame] | 134 | free(p); |
| 135 | return 0; |
| 136 | } |
| 137 | |
djm@openbsd.org | 1a31d02 | 2016-05-02 08:49:03 +0000 | [diff] [blame] | 138 | char * |
| 139 | sshbuf_dup_string(struct sshbuf *buf) |
| 140 | { |
| 141 | const u_char *p = NULL, *s = sshbuf_ptr(buf); |
| 142 | size_t l = sshbuf_len(buf); |
| 143 | char *r; |
| 144 | |
| 145 | if (s == NULL || l > SIZE_MAX) |
| 146 | return NULL; |
| 147 | /* accept a nul only as the last character in the buffer */ |
| 148 | if (l > 0 && (p = memchr(s, '\0', l)) != NULL) { |
| 149 | if (p != s + l - 1) |
| 150 | return NULL; |
| 151 | l--; /* the nul is put back below */ |
| 152 | } |
| 153 | if ((r = malloc(l + 1)) == NULL) |
| 154 | return NULL; |
| 155 | if (l > 0) |
| 156 | memcpy(r, s, l); |
| 157 | r[l] = '\0'; |
| 158 | return r; |
| 159 | } |
| 160 | |
djm@openbsd.org | e18a27e | 2019-07-15 13:11:38 +0000 | [diff] [blame^] | 161 | int |
| 162 | sshbuf_cmp(const struct sshbuf *b, size_t offset, |
| 163 | const u_char *s, size_t len) |
| 164 | { |
| 165 | if (sshbuf_ptr(b) == NULL) |
| 166 | return SSH_ERR_INTERNAL_ERROR; |
| 167 | if (offset > SSHBUF_SIZE_MAX || len > SSHBUF_SIZE_MAX || len == 0) |
| 168 | return SSH_ERR_INVALID_ARGUMENT; |
| 169 | if (offset + len > sshbuf_len(b)) |
| 170 | return SSH_ERR_MESSAGE_INCOMPLETE; |
| 171 | if (timingsafe_bcmp(sshbuf_ptr(b) + offset, s, len) != 0) |
| 172 | return SSH_ERR_INVALID_FORMAT; |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | int |
| 177 | sshbuf_find(const struct sshbuf *b, size_t start_offset, |
| 178 | const u_char *s, size_t len, size_t *offsetp) |
| 179 | { |
| 180 | void *p; |
| 181 | |
| 182 | if (offsetp != NULL) |
| 183 | *offsetp = 0; |
| 184 | |
| 185 | if (sshbuf_ptr(b) == NULL) |
| 186 | return SSH_ERR_INTERNAL_ERROR; |
| 187 | if (start_offset > SSHBUF_SIZE_MAX || len > SSHBUF_SIZE_MAX || len == 0) |
| 188 | return SSH_ERR_INVALID_ARGUMENT; |
| 189 | if (start_offset > sshbuf_len(b) || start_offset + len > sshbuf_len(b)) |
| 190 | return SSH_ERR_MESSAGE_INCOMPLETE; |
| 191 | if ((p = memmem(sshbuf_ptr(b) + start_offset, |
| 192 | sshbuf_len(b) - start_offset, s, len)) == NULL) |
| 193 | return SSH_ERR_INVALID_FORMAT; |
| 194 | if (offsetp != NULL) |
| 195 | *offsetp = (const u_char *)p - sshbuf_ptr(b); |
| 196 | return 0; |
| 197 | } |