[BACK]Return to sshbuf-misc.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/sshbuf-misc.c, Revision 1.15

1.15    ! djm         1: /*     $OpenBSD: sshbuf-misc.c,v 1.14 2020/02/26 13:40:09 jsg Exp $    */
1.1       djm         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 <sys/types.h>
                     19: #include <sys/socket.h>
                     20: #include <netinet/in.h>
                     21: #include <errno.h>
1.13      djm        22: #include <stdlib.h>
1.3       millert    23: #include <stdint.h>
1.1       djm        24: #include <stdio.h>
1.13      djm        25: #include <limits.h>
1.1       djm        26: #include <string.h>
1.13      djm        27: #include <resolv.h>
                     28: #include <ctype.h>
1.1       djm        29:
                     30: #include "ssherr.h"
                     31: #define SSHBUF_INTERNAL
                     32: #include "sshbuf.h"
                     33:
                     34: void
1.2       djm        35: sshbuf_dump_data(const void *s, size_t len, FILE *f)
1.1       djm        36: {
1.2       djm        37:        size_t i, j;
                     38:        const u_char *p = (const u_char *)s;
1.1       djm        39:
                     40:        for (i = 0; i < len; i += 16) {
1.4       markus     41:                fprintf(f, "%.4zu: ", i);
1.1       djm        42:                for (j = i; j < i + 16; j++) {
                     43:                        if (j < len)
                     44:                                fprintf(f, "%02x ", p[j]);
                     45:                        else
                     46:                                fprintf(f, "   ");
                     47:                }
                     48:                fprintf(f, " ");
                     49:                for (j = i; j < i + 16; j++) {
                     50:                        if (j < len) {
                     51:                                if  (isascii(p[j]) && isprint(p[j]))
                     52:                                        fprintf(f, "%c", p[j]);
                     53:                                else
                     54:                                        fprintf(f, ".");
                     55:                        }
                     56:                }
                     57:                fprintf(f, "\n");
                     58:        }
1.2       djm        59: }
                     60:
                     61: void
1.15    ! djm        62: sshbuf_dump(const struct sshbuf *buf, FILE *f)
1.2       djm        63: {
                     64:        fprintf(f, "buffer %p len = %zu\n", buf, sshbuf_len(buf));
                     65:        sshbuf_dump_data(sshbuf_ptr(buf), sshbuf_len(buf), f);
1.1       djm        66: }
                     67:
                     68: char *
                     69: sshbuf_dtob16(struct sshbuf *buf)
                     70: {
                     71:        size_t i, j, len = sshbuf_len(buf);
                     72:        const u_char *p = sshbuf_ptr(buf);
                     73:        char *ret;
                     74:        const char hex[] = "0123456789abcdef";
                     75:
                     76:        if (len == 0)
                     77:                return strdup("");
                     78:        if (SIZE_MAX / 2 <= len || (ret = malloc(len * 2 + 1)) == NULL)
                     79:                return NULL;
                     80:        for (i = j = 0; i < len; i++) {
                     81:                ret[j++] = hex[(p[i] >> 4) & 0xf];
                     82:                ret[j++] = hex[p[i] & 0xf];
                     83:        }
                     84:        ret[j] = '\0';
                     85:        return ret;
                     86: }
                     87:
1.9       djm        88: int
                     89: sshbuf_dtob64(const struct sshbuf *d, struct sshbuf *b64, int wrap)
                     90: {
                     91:        size_t i, slen = 0;
                     92:        char *s = NULL;
                     93:        int r;
                     94:
                     95:        if (d == NULL || b64 == NULL || sshbuf_len(d) >= SIZE_MAX / 2)
                     96:                return SSH_ERR_INVALID_ARGUMENT;
                     97:        if (sshbuf_len(d) == 0)
                     98:                return 0;
                     99:        slen = ((sshbuf_len(d) + 2) / 3) * 4 + 1;
                    100:        if ((s = malloc(slen)) == NULL)
                    101:                return SSH_ERR_ALLOC_FAIL;
                    102:        if (b64_ntop(sshbuf_ptr(d), sshbuf_len(d), s, slen) == -1) {
                    103:                r = SSH_ERR_INTERNAL_ERROR;
                    104:                goto fail;
                    105:        }
                    106:        if (wrap) {
                    107:                for (i = 0; s[i] != '\0'; i++) {
                    108:                        if ((r = sshbuf_put_u8(b64, s[i])) != 0)
                    109:                                goto fail;
                    110:                        if (i % 70 == 69 && (r = sshbuf_put_u8(b64, '\n')) != 0)
                    111:                                goto fail;
                    112:                }
1.10      djm       113:                if ((i - 1) % 70 != 69 && (r = sshbuf_put_u8(b64, '\n')) != 0)
1.9       djm       114:                        goto fail;
                    115:        } else {
                    116:                if ((r = sshbuf_put(b64, s, strlen(s))) != 0)
                    117:                        goto fail;
                    118:        }
                    119:        /* Success */
                    120:        r = 0;
                    121:  fail:
                    122:        freezero(s, slen);
                    123:        return r;
                    124: }
                    125:
1.1       djm       126: char *
1.9       djm       127: sshbuf_dtob64_string(const struct sshbuf *buf, int wrap)
1.1       djm       128: {
1.9       djm       129:        struct sshbuf *tmp;
1.1       djm       130:        char *ret;
                    131:
1.9       djm       132:        if ((tmp = sshbuf_new()) == NULL)
1.1       djm       133:                return NULL;
1.9       djm       134:        if (sshbuf_dtob64(buf, tmp, wrap) != 0) {
                    135:                sshbuf_free(tmp);
1.1       djm       136:                return NULL;
                    137:        }
1.9       djm       138:        ret = sshbuf_dup_string(tmp);
                    139:        sshbuf_free(tmp);
1.1       djm       140:        return ret;
                    141: }
                    142:
                    143: int
                    144: sshbuf_b64tod(struct sshbuf *buf, const char *b64)
                    145: {
                    146:        size_t plen = strlen(b64);
                    147:        int nlen, r;
                    148:        u_char *p;
                    149:
                    150:        if (plen == 0)
                    151:                return 0;
                    152:        if ((p = malloc(plen)) == NULL)
                    153:                return SSH_ERR_ALLOC_FAIL;
                    154:        if ((nlen = b64_pton(b64, p, plen)) < 0) {
1.14      jsg       155:                freezero(p, plen);
1.1       djm       156:                return SSH_ERR_INVALID_FORMAT;
                    157:        }
                    158:        if ((r = sshbuf_put(buf, p, nlen)) < 0) {
1.14      jsg       159:                freezero(p, plen);
1.1       djm       160:                return r;
                    161:        }
1.14      jsg       162:        freezero(p, plen);
1.1       djm       163:        return 0;
1.6       djm       164: }
                    165:
                    166: char *
                    167: sshbuf_dup_string(struct sshbuf *buf)
                    168: {
                    169:        const u_char *p = NULL, *s = sshbuf_ptr(buf);
                    170:        size_t l = sshbuf_len(buf);
                    171:        char *r;
                    172:
                    173:        if (s == NULL || l > SIZE_MAX)
                    174:                return NULL;
                    175:        /* accept a nul only as the last character in the buffer */
                    176:        if (l > 0 && (p = memchr(s, '\0', l)) != NULL) {
                    177:                if (p != s + l - 1)
                    178:                        return NULL;
                    179:                l--; /* the nul is put back below */
                    180:        }
                    181:        if ((r = malloc(l + 1)) == NULL)
                    182:                return NULL;
                    183:        if (l > 0)
                    184:                memcpy(r, s, l);
                    185:        r[l] = '\0';
                    186:        return r;
1.1       djm       187: }
                    188:
1.8       djm       189: int
                    190: sshbuf_cmp(const struct sshbuf *b, size_t offset,
1.11      djm       191:     const void *s, size_t len)
1.8       djm       192: {
                    193:        if (sshbuf_ptr(b) == NULL)
                    194:                return SSH_ERR_INTERNAL_ERROR;
                    195:        if (offset > SSHBUF_SIZE_MAX || len > SSHBUF_SIZE_MAX || len == 0)
                    196:                return SSH_ERR_INVALID_ARGUMENT;
                    197:        if (offset + len > sshbuf_len(b))
                    198:                return SSH_ERR_MESSAGE_INCOMPLETE;
                    199:        if (timingsafe_bcmp(sshbuf_ptr(b) + offset, s, len) != 0)
                    200:                return SSH_ERR_INVALID_FORMAT;
                    201:        return 0;
                    202: }
                    203:
                    204: int
                    205: sshbuf_find(const struct sshbuf *b, size_t start_offset,
1.11      djm       206:     const void *s, size_t len, size_t *offsetp)
1.8       djm       207: {
                    208:        void *p;
                    209:
                    210:        if (offsetp != NULL)
                    211:                *offsetp = 0;
                    212:        if (sshbuf_ptr(b) == NULL)
                    213:                return SSH_ERR_INTERNAL_ERROR;
                    214:        if (start_offset > SSHBUF_SIZE_MAX || len > SSHBUF_SIZE_MAX || len == 0)
                    215:                return SSH_ERR_INVALID_ARGUMENT;
                    216:        if (start_offset > sshbuf_len(b) || start_offset + len > sshbuf_len(b))
                    217:                return SSH_ERR_MESSAGE_INCOMPLETE;
                    218:        if ((p = memmem(sshbuf_ptr(b) + start_offset,
                    219:            sshbuf_len(b) - start_offset, s, len)) == NULL)
                    220:                return SSH_ERR_INVALID_FORMAT;
                    221:        if (offsetp != NULL)
                    222:                *offsetp = (const u_char *)p - sshbuf_ptr(b);
                    223:        return 0;
                    224: }