jsing@openbsd.org | 7cd3163 | 2018-02-07 02:06:50 +0000 | [diff] [blame] | 1 | /* $OpenBSD: ssh-ecdsa.c,v 1.14 2018/02/07 02:06:51 jsing Exp $ */ |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2000 Markus Friedl. All rights reserved. |
| 4 | * Copyright (c) 2010 Damien Miller. All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 16 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 17 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 18 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 19 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 20 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 21 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 22 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 24 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | */ |
| 26 | |
Damien Miller | c79ff07 | 2010-08-31 22:50:48 +1000 | [diff] [blame] | 27 | #include "includes.h" |
| 28 | |
Damien Miller | 72ef7c1 | 2015-01-15 02:21:31 +1100 | [diff] [blame] | 29 | #if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC) |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 30 | |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 31 | #include <sys/types.h> |
| 32 | |
| 33 | #include <openssl/bn.h> |
| 34 | #include <openssl/ec.h> |
| 35 | #include <openssl/ecdsa.h> |
| 36 | #include <openssl/evp.h> |
| 37 | |
| 38 | #include <string.h> |
| 39 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 40 | #include "sshbuf.h" |
| 41 | #include "ssherr.h" |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 42 | #include "digest.h" |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 43 | #define SSHKEY_INTERNAL |
| 44 | #include "sshkey.h" |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 45 | |
Damien Miller | 48f54b9 | 2018-09-13 12:13:50 +1000 | [diff] [blame] | 46 | #include "openbsd-compat/openssl-compat.h" |
| 47 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 48 | /* ARGSUSED */ |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 49 | int |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 50 | ssh_ecdsa_sign(const struct sshkey *key, u_char **sigp, size_t *lenp, |
| 51 | const u_char *data, size_t datalen, u_int compat) |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 52 | { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 53 | ECDSA_SIG *sig = NULL; |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 54 | const BIGNUM *sig_r, *sig_s; |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 55 | int hash_alg; |
| 56 | u_char digest[SSH_DIGEST_MAX_LENGTH]; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 57 | size_t len, dlen; |
| 58 | struct sshbuf *b = NULL, *bb = NULL; |
| 59 | int ret = SSH_ERR_INTERNAL_ERROR; |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 60 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 61 | if (lenp != NULL) |
| 62 | *lenp = 0; |
| 63 | if (sigp != NULL) |
| 64 | *sigp = NULL; |
| 65 | |
| 66 | if (key == NULL || key->ecdsa == NULL || |
| 67 | sshkey_type_plain(key->type) != KEY_ECDSA) |
| 68 | return SSH_ERR_INVALID_ARGUMENT; |
| 69 | |
| 70 | if ((hash_alg = sshkey_ec_nid_to_hash_alg(key->ecdsa_nid)) == -1 || |
| 71 | (dlen = ssh_digest_bytes(hash_alg)) == 0) |
| 72 | return SSH_ERR_INTERNAL_ERROR; |
| 73 | if ((ret = ssh_digest_memory(hash_alg, data, datalen, |
| 74 | digest, sizeof(digest))) != 0) |
| 75 | goto out; |
| 76 | |
| 77 | if ((sig = ECDSA_do_sign(digest, dlen, key->ecdsa)) == NULL) { |
| 78 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
| 79 | goto out; |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 80 | } |
Damien Miller | 3e19295 | 2013-12-29 17:47:50 +1100 | [diff] [blame] | 81 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 82 | if ((bb = sshbuf_new()) == NULL || (b = sshbuf_new()) == NULL) { |
| 83 | ret = SSH_ERR_ALLOC_FAIL; |
| 84 | goto out; |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 85 | } |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 86 | ECDSA_SIG_get0(sig, &sig_r, &sig_s); |
| 87 | if ((ret = sshbuf_put_bignum2(bb, sig_r)) != 0 || |
| 88 | (ret = sshbuf_put_bignum2(bb, sig_s)) != 0) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 89 | goto out; |
| 90 | if ((ret = sshbuf_put_cstring(b, sshkey_ssh_name_plain(key))) != 0 || |
| 91 | (ret = sshbuf_put_stringb(b, bb)) != 0) |
| 92 | goto out; |
| 93 | len = sshbuf_len(b); |
| 94 | if (sigp != NULL) { |
| 95 | if ((*sigp = malloc(len)) == NULL) { |
| 96 | ret = SSH_ERR_ALLOC_FAIL; |
| 97 | goto out; |
| 98 | } |
| 99 | memcpy(*sigp, sshbuf_ptr(b), len); |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 100 | } |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 101 | if (lenp != NULL) |
| 102 | *lenp = len; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 103 | ret = 0; |
| 104 | out: |
| 105 | explicit_bzero(digest, sizeof(digest)); |
mmcc@openbsd.org | 52d7078 | 2015-12-11 04:21:11 +0000 | [diff] [blame] | 106 | sshbuf_free(b); |
| 107 | sshbuf_free(bb); |
jsing@openbsd.org | 7cd3163 | 2018-02-07 02:06:50 +0000 | [diff] [blame] | 108 | ECDSA_SIG_free(sig); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 109 | return ret; |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 110 | } |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 111 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 112 | /* ARGSUSED */ |
| 113 | int |
| 114 | ssh_ecdsa_verify(const struct sshkey *key, |
| 115 | const u_char *signature, size_t signaturelen, |
| 116 | const u_char *data, size_t datalen, u_int compat) |
| 117 | { |
| 118 | ECDSA_SIG *sig = NULL; |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 119 | BIGNUM *sig_r = NULL, *sig_s = NULL; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 120 | int hash_alg; |
| 121 | u_char digest[SSH_DIGEST_MAX_LENGTH]; |
| 122 | size_t dlen; |
| 123 | int ret = SSH_ERR_INTERNAL_ERROR; |
| 124 | struct sshbuf *b = NULL, *sigbuf = NULL; |
| 125 | char *ktype = NULL; |
| 126 | |
| 127 | if (key == NULL || key->ecdsa == NULL || |
djm@openbsd.org | b6e0140 | 2016-04-21 06:08:02 +0000 | [diff] [blame] | 128 | sshkey_type_plain(key->type) != KEY_ECDSA || |
| 129 | signature == NULL || signaturelen == 0) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 130 | return SSH_ERR_INVALID_ARGUMENT; |
| 131 | |
| 132 | if ((hash_alg = sshkey_ec_nid_to_hash_alg(key->ecdsa_nid)) == -1 || |
| 133 | (dlen = ssh_digest_bytes(hash_alg)) == 0) |
| 134 | return SSH_ERR_INTERNAL_ERROR; |
Damien Miller | 3e19295 | 2013-12-29 17:47:50 +1100 | [diff] [blame] | 135 | |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 136 | /* fetch signature */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 137 | if ((b = sshbuf_from(signature, signaturelen)) == NULL) |
| 138 | return SSH_ERR_ALLOC_FAIL; |
| 139 | if (sshbuf_get_cstring(b, &ktype, NULL) != 0 || |
| 140 | sshbuf_froms(b, &sigbuf) != 0) { |
| 141 | ret = SSH_ERR_INVALID_FORMAT; |
| 142 | goto out; |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 143 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 144 | if (strcmp(sshkey_ssh_name_plain(key), ktype) != 0) { |
| 145 | ret = SSH_ERR_KEY_TYPE_MISMATCH; |
| 146 | goto out; |
| 147 | } |
| 148 | if (sshbuf_len(b) != 0) { |
| 149 | ret = SSH_ERR_UNEXPECTED_TRAILING_DATA; |
| 150 | goto out; |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | /* parse signature */ |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 154 | if ((sig = ECDSA_SIG_new()) == NULL || |
| 155 | (sig_r = BN_new()) == NULL || |
| 156 | (sig_s = BN_new()) == NULL) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 157 | ret = SSH_ERR_ALLOC_FAIL; |
| 158 | goto out; |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 159 | } |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 160 | if (sshbuf_get_bignum2(sigbuf, sig_r) != 0 || |
| 161 | sshbuf_get_bignum2(sigbuf, sig_s) != 0) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 162 | ret = SSH_ERR_INVALID_FORMAT; |
| 163 | goto out; |
| 164 | } |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 165 | if (!ECDSA_SIG_set0(sig, sig_r, sig_s)) { |
| 166 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
| 167 | goto out; |
| 168 | } |
| 169 | sig_r = sig_s = NULL; /* transferred */ |
| 170 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 171 | if (sshbuf_len(sigbuf) != 0) { |
| 172 | ret = SSH_ERR_UNEXPECTED_TRAILING_DATA; |
| 173 | goto out; |
| 174 | } |
| 175 | if ((ret = ssh_digest_memory(hash_alg, data, datalen, |
| 176 | digest, sizeof(digest))) != 0) |
| 177 | goto out; |
| 178 | |
| 179 | switch (ECDSA_do_verify(digest, dlen, sig, key->ecdsa)) { |
| 180 | case 1: |
| 181 | ret = 0; |
| 182 | break; |
| 183 | case 0: |
| 184 | ret = SSH_ERR_SIGNATURE_INVALID; |
| 185 | goto out; |
| 186 | default: |
| 187 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
| 188 | goto out; |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 189 | } |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 190 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 191 | out: |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 192 | explicit_bzero(digest, sizeof(digest)); |
mmcc@openbsd.org | 52d7078 | 2015-12-11 04:21:11 +0000 | [diff] [blame] | 193 | sshbuf_free(sigbuf); |
| 194 | sshbuf_free(b); |
jsing@openbsd.org | 7cd3163 | 2018-02-07 02:06:50 +0000 | [diff] [blame] | 195 | ECDSA_SIG_free(sig); |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 196 | BN_clear_free(sig_r); |
| 197 | BN_clear_free(sig_s); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 198 | free(ktype); |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 199 | return ret; |
| 200 | } |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 201 | |
Damien Miller | 72ef7c1 | 2015-01-15 02:21:31 +1100 | [diff] [blame] | 202 | #endif /* WITH_OPENSSL && OPENSSL_HAS_ECC */ |