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

Annotation of src/usr.bin/ssh/sshbuf-getput-crypto.c, Revision 1.6

1.6     ! djm         1: /*     $OpenBSD: sshbuf-getput-crypto.c,v 1.5 2016/01/12 23:42:54 djm 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 <stdlib.h>
                     20: #include <stdio.h>
                     21: #include <string.h>
                     22:
                     23: #include <openssl/bn.h>
                     24: #include <openssl/ec.h>
                     25:
                     26: #include "ssherr.h"
                     27: #define SSHBUF_INTERNAL
                     28: #include "sshbuf.h"
                     29:
                     30: int
                     31: sshbuf_get_bignum2(struct sshbuf *buf, BIGNUM *v)
                     32: {
                     33:        const u_char *d;
                     34:        size_t len;
                     35:        int r;
                     36:
1.4       djm        37:        if ((r = sshbuf_get_bignum2_bytes_direct(buf, &d, &len)) != 0)
1.1       djm        38:                return r;
                     39:        if (v != NULL && BN_bin2bn(d, len, v) == NULL)
                     40:                return SSH_ERR_ALLOC_FAIL;
                     41:        return 0;
                     42: }
                     43:
                     44: static int
                     45: get_ec(const u_char *d, size_t len, EC_POINT *v, const EC_GROUP *g)
                     46: {
                     47:        /* Refuse overlong bignums */
                     48:        if (len == 0 || len > SSHBUF_MAX_ECPOINT)
                     49:                return SSH_ERR_ECPOINT_TOO_LARGE;
                     50:        /* Only handle uncompressed points */
                     51:        if (*d != POINT_CONVERSION_UNCOMPRESSED)
                     52:                return SSH_ERR_INVALID_FORMAT;
                     53:        if (v != NULL && EC_POINT_oct2point(g, v, d, len, NULL) != 1)
                     54:                return SSH_ERR_INVALID_FORMAT; /* XXX assumption */
                     55:        return 0;
                     56: }
                     57:
                     58: int
                     59: sshbuf_get_ec(struct sshbuf *buf, EC_POINT *v, const EC_GROUP *g)
                     60: {
                     61:        const u_char *d;
                     62:        size_t len;
                     63:        int r;
                     64:
                     65:        if ((r = sshbuf_peek_string_direct(buf, &d, &len)) < 0)
                     66:                return r;
                     67:        if ((r = get_ec(d, len, v, g)) != 0)
                     68:                return r;
                     69:        /* Skip string */
                     70:        if (sshbuf_get_string_direct(buf, NULL, NULL) != 0) {
                     71:                /* Shouldn't happen */
                     72:                SSHBUF_DBG(("SSH_ERR_INTERNAL_ERROR"));
                     73:                SSHBUF_ABORT();
                     74:                return SSH_ERR_INTERNAL_ERROR;
                     75:        }
                     76:        return 0;
                     77: }
                     78:
                     79: int
                     80: sshbuf_get_eckey(struct sshbuf *buf, EC_KEY *v)
                     81: {
                     82:        EC_POINT *pt = EC_POINT_new(EC_KEY_get0_group(v));
                     83:        int r;
                     84:        const u_char *d;
                     85:        size_t len;
                     86:
                     87:        if (pt == NULL) {
                     88:                SSHBUF_DBG(("SSH_ERR_ALLOC_FAIL"));
                     89:                return SSH_ERR_ALLOC_FAIL;
                     90:        }
                     91:        if ((r = sshbuf_peek_string_direct(buf, &d, &len)) < 0) {
                     92:                EC_POINT_free(pt);
                     93:                return r;
                     94:        }
                     95:        if ((r = get_ec(d, len, pt, EC_KEY_get0_group(v))) != 0) {
                     96:                EC_POINT_free(pt);
                     97:                return r;
                     98:        }
                     99:        if (EC_KEY_set_public_key(v, pt) != 1) {
                    100:                EC_POINT_free(pt);
                    101:                return SSH_ERR_ALLOC_FAIL; /* XXX assumption */
                    102:        }
                    103:        EC_POINT_free(pt);
                    104:        /* Skip string */
                    105:        if (sshbuf_get_string_direct(buf, NULL, NULL) != 0) {
                    106:                /* Shouldn't happen */
                    107:                SSHBUF_DBG(("SSH_ERR_INTERNAL_ERROR"));
                    108:                SSHBUF_ABORT();
                    109:                return SSH_ERR_INTERNAL_ERROR;
                    110:        }
                    111:        return 0;
                    112: }
                    113:
                    114: int
                    115: sshbuf_put_bignum2(struct sshbuf *buf, const BIGNUM *v)
                    116: {
                    117:        u_char d[SSHBUF_MAX_BIGNUM + 1];
                    118:        int len = BN_num_bytes(v), prepend = 0, r;
                    119:
                    120:        if (len < 0 || len > SSHBUF_MAX_BIGNUM)
                    121:                return SSH_ERR_INVALID_ARGUMENT;
                    122:        *d = '\0';
                    123:        if (BN_bn2bin(v, d + 1) != len)
                    124:                return SSH_ERR_INTERNAL_ERROR; /* Shouldn't happen */
                    125:        /* If MSB is set, prepend a \0 */
                    126:        if (len > 0 && (d[1] & 0x80) != 0)
                    127:                prepend = 1;
                    128:        if ((r = sshbuf_put_string(buf, d + 1 - prepend, len + prepend)) < 0) {
1.5       djm       129:                explicit_bzero(d, sizeof(d));
1.1       djm       130:                return r;
                    131:        }
1.5       djm       132:        explicit_bzero(d, sizeof(d));
1.1       djm       133:        return 0;
                    134: }
                    135:
                    136: int
                    137: sshbuf_put_ec(struct sshbuf *buf, const EC_POINT *v, const EC_GROUP *g)
                    138: {
                    139:        u_char d[SSHBUF_MAX_ECPOINT];
                    140:        BN_CTX *bn_ctx;
                    141:        size_t len;
                    142:        int ret;
                    143:
                    144:        if ((bn_ctx = BN_CTX_new()) == NULL)
                    145:                return SSH_ERR_ALLOC_FAIL;
                    146:        if ((len = EC_POINT_point2oct(g, v, POINT_CONVERSION_UNCOMPRESSED,
                    147:            NULL, 0, bn_ctx)) > SSHBUF_MAX_ECPOINT) {
                    148:                BN_CTX_free(bn_ctx);
                    149:                return SSH_ERR_INVALID_ARGUMENT;
                    150:        }
                    151:        if (EC_POINT_point2oct(g, v, POINT_CONVERSION_UNCOMPRESSED,
                    152:            d, len, bn_ctx) != len) {
                    153:                BN_CTX_free(bn_ctx);
                    154:                return SSH_ERR_INTERNAL_ERROR; /* Shouldn't happen */
                    155:        }
                    156:        BN_CTX_free(bn_ctx);
                    157:        ret = sshbuf_put_string(buf, d, len);
1.5       djm       158:        explicit_bzero(d, len);
1.1       djm       159:        return ret;
                    160: }
                    161:
                    162: int
                    163: sshbuf_put_eckey(struct sshbuf *buf, const EC_KEY *v)
                    164: {
                    165:        return sshbuf_put_ec(buf, EC_KEY_get0_public_key(v),
                    166:            EC_KEY_get0_group(v));
                    167: }
                    168: