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