jsing@openbsd.org | d2b3db2 | 2018-02-14 16:27:24 +0000 | [diff] [blame] | 1 | /* $OpenBSD: ssh-rsa.c,v 1.66 2018/02/14 16:27:24 jsing Exp $ */ |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 2 | /* |
Darren Tucker | a251f80 | 2003-06-22 20:45:15 +1000 | [diff] [blame] | 3 | * Copyright (c) 2000, 2003 Markus Friedl <markus@openbsd.org> |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 4 | * |
Darren Tucker | a251f80 | 2003-06-22 20:45:15 +1000 | [diff] [blame] | 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. |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 8 | * |
Darren Tucker | a251f80 | 2003-06-22 20:45:15 +1000 | [diff] [blame] | 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. |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 16 | */ |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 17 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 18 | #include "includes.h" |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 19 | |
Damien Miller | 72ef7c1 | 2015-01-15 02:21:31 +1100 | [diff] [blame] | 20 | #ifdef WITH_OPENSSL |
| 21 | |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 22 | #include <sys/types.h> |
| 23 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 24 | #include <openssl/evp.h> |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 25 | #include <openssl/err.h> |
| 26 | |
Damien Miller | ded319c | 2006-09-01 15:38:36 +1000 | [diff] [blame] | 27 | #include <stdarg.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 28 | #include <string.h> |
| 29 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 30 | #include "sshbuf.h" |
Ben Lindstrom | 60a4381 | 2001-03-29 00:32:56 +0000 | [diff] [blame] | 31 | #include "compat.h" |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 32 | #include "ssherr.h" |
| 33 | #define SSHKEY_INTERNAL |
| 34 | #include "sshkey.h" |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 35 | #include "digest.h" |
djm@openbsd.org | 966ef47 | 2017-12-18 23:14:34 +0000 | [diff] [blame] | 36 | #include "log.h" |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 37 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 38 | static int openssh_RSA_verify(int, u_char *, size_t, u_char *, size_t, RSA *); |
Ben Lindstrom | 0deb5d9 | 2002-08-20 18:40:03 +0000 | [diff] [blame] | 39 | |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 40 | static const char * |
| 41 | rsa_hash_alg_ident(int hash_alg) |
| 42 | { |
| 43 | switch (hash_alg) { |
| 44 | case SSH_DIGEST_SHA1: |
| 45 | return "ssh-rsa"; |
| 46 | case SSH_DIGEST_SHA256: |
| 47 | return "rsa-sha2-256"; |
| 48 | case SSH_DIGEST_SHA512: |
| 49 | return "rsa-sha2-512"; |
| 50 | } |
| 51 | return NULL; |
| 52 | } |
| 53 | |
| 54 | static int |
| 55 | rsa_hash_alg_from_ident(const char *ident) |
| 56 | { |
djm@openbsd.org | 445e218 | 2016-09-12 23:39:34 +0000 | [diff] [blame] | 57 | if (strcmp(ident, "ssh-rsa") == 0 || |
| 58 | strcmp(ident, "ssh-rsa-cert-v01@openssh.com") == 0) |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 59 | return SSH_DIGEST_SHA1; |
| 60 | if (strcmp(ident, "rsa-sha2-256") == 0) |
| 61 | return SSH_DIGEST_SHA256; |
| 62 | if (strcmp(ident, "rsa-sha2-512") == 0) |
| 63 | return SSH_DIGEST_SHA512; |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 64 | return -1; |
| 65 | } |
| 66 | |
| 67 | static int |
| 68 | rsa_hash_alg_nid(int type) |
| 69 | { |
| 70 | switch (type) { |
| 71 | case SSH_DIGEST_SHA1: |
| 72 | return NID_sha1; |
| 73 | case SSH_DIGEST_SHA256: |
| 74 | return NID_sha256; |
| 75 | case SSH_DIGEST_SHA512: |
| 76 | return NID_sha512; |
| 77 | default: |
| 78 | return -1; |
| 79 | } |
| 80 | } |
| 81 | |
djm@openbsd.org | 83fa3a0 | 2017-07-01 13:50:45 +0000 | [diff] [blame] | 82 | int |
| 83 | ssh_rsa_generate_additional_parameters(struct sshkey *key) |
| 84 | { |
djm@openbsd.org | 83fa3a0 | 2017-07-01 13:50:45 +0000 | [diff] [blame] | 85 | BIGNUM *aux = NULL; |
| 86 | BN_CTX *ctx = NULL; |
jsing@openbsd.org | d2b3db2 | 2018-02-14 16:27:24 +0000 | [diff] [blame] | 87 | BIGNUM d; |
djm@openbsd.org | 83fa3a0 | 2017-07-01 13:50:45 +0000 | [diff] [blame] | 88 | int r; |
| 89 | |
| 90 | if (key == NULL || key->rsa == NULL || |
| 91 | sshkey_type_plain(key->type) != KEY_RSA) |
| 92 | return SSH_ERR_INVALID_ARGUMENT; |
| 93 | |
| 94 | if ((ctx = BN_CTX_new()) == NULL) |
| 95 | return SSH_ERR_ALLOC_FAIL; |
| 96 | if ((aux = BN_new()) == NULL) { |
| 97 | r = SSH_ERR_ALLOC_FAIL; |
| 98 | goto out; |
| 99 | } |
jsing@openbsd.org | d2b3db2 | 2018-02-14 16:27:24 +0000 | [diff] [blame] | 100 | BN_set_flags(aux, BN_FLG_CONSTTIME); |
djm@openbsd.org | 83fa3a0 | 2017-07-01 13:50:45 +0000 | [diff] [blame] | 101 | |
jsing@openbsd.org | d2b3db2 | 2018-02-14 16:27:24 +0000 | [diff] [blame] | 102 | BN_init(&d); |
| 103 | BN_with_flags(&d, key->rsa->d, BN_FLG_CONSTTIME); |
| 104 | |
| 105 | if ((BN_sub(aux, key->rsa->q, BN_value_one()) == 0) || |
| 106 | (BN_mod(key->rsa->dmq1, &d, aux, ctx) == 0) || |
| 107 | (BN_sub(aux, key->rsa->p, BN_value_one()) == 0) || |
| 108 | (BN_mod(key->rsa->dmp1, &d, aux, ctx) == 0)) { |
djm@openbsd.org | 83fa3a0 | 2017-07-01 13:50:45 +0000 | [diff] [blame] | 109 | r = SSH_ERR_LIBCRYPTO_ERROR; |
| 110 | goto out; |
| 111 | } |
| 112 | r = 0; |
| 113 | out: |
| 114 | BN_clear_free(aux); |
| 115 | BN_CTX_free(ctx); |
| 116 | return r; |
| 117 | } |
| 118 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 119 | /* RSASSA-PKCS1-v1_5 (PKCS #1 v2.0 signature) with SHA1 */ |
| 120 | int |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 121 | ssh_rsa_sign(const struct sshkey *key, u_char **sigp, size_t *lenp, |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 122 | const u_char *data, size_t datalen, const char *alg_ident) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 123 | { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 124 | u_char digest[SSH_DIGEST_MAX_LENGTH], *sig = NULL; |
jsing@openbsd.org | 1749991 | 2018-02-07 05:17:56 +0000 | [diff] [blame] | 125 | size_t slen = 0; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 126 | u_int dlen, len; |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 127 | int nid, hash_alg, ret = SSH_ERR_INTERNAL_ERROR; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 128 | struct sshbuf *b = NULL; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 129 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 130 | if (lenp != NULL) |
| 131 | *lenp = 0; |
| 132 | if (sigp != NULL) |
| 133 | *sigp = NULL; |
| 134 | |
djm@openbsd.org | 445e218 | 2016-09-12 23:39:34 +0000 | [diff] [blame] | 135 | if (alg_ident == NULL || strlen(alg_ident) == 0) |
markus@openbsd.org | 6262a05 | 2015-12-07 20:04:09 +0000 | [diff] [blame] | 136 | hash_alg = SSH_DIGEST_SHA1; |
| 137 | else |
| 138 | hash_alg = rsa_hash_alg_from_ident(alg_ident); |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 139 | if (key == NULL || key->rsa == NULL || hash_alg == -1 || |
djm@openbsd.org | bd636f4 | 2017-05-07 23:15:59 +0000 | [diff] [blame] | 140 | sshkey_type_plain(key->type) != KEY_RSA) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 141 | return SSH_ERR_INVALID_ARGUMENT; |
djm@openbsd.org | bd636f4 | 2017-05-07 23:15:59 +0000 | [diff] [blame] | 142 | if (BN_num_bits(key->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE) |
| 143 | return SSH_ERR_KEY_LENGTH; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 144 | slen = RSA_size(key->rsa); |
| 145 | if (slen <= 0 || slen > SSHBUF_MAX_BIGNUM) |
| 146 | return SSH_ERR_INVALID_ARGUMENT; |
Damien Miller | 3e19295 | 2013-12-29 17:47:50 +1100 | [diff] [blame] | 147 | |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 148 | /* hash the data */ |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 149 | nid = rsa_hash_alg_nid(hash_alg); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 150 | if ((dlen = ssh_digest_bytes(hash_alg)) == 0) |
| 151 | return SSH_ERR_INTERNAL_ERROR; |
| 152 | if ((ret = ssh_digest_memory(hash_alg, data, datalen, |
| 153 | digest, sizeof(digest))) != 0) |
| 154 | goto out; |
| 155 | |
| 156 | if ((sig = malloc(slen)) == NULL) { |
| 157 | ret = SSH_ERR_ALLOC_FAIL; |
| 158 | goto out; |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 159 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 160 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 161 | if (RSA_sign(nid, digest, dlen, sig, &len, key->rsa) != 1) { |
| 162 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
| 163 | goto out; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 164 | } |
| 165 | if (len < slen) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 166 | size_t diff = slen - len; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 167 | memmove(sig + diff, sig, len); |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 168 | explicit_bzero(sig, diff); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 169 | } else if (len > slen) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 170 | ret = SSH_ERR_INTERNAL_ERROR; |
| 171 | goto out; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 172 | } |
| 173 | /* encode signature */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 174 | if ((b = sshbuf_new()) == NULL) { |
| 175 | ret = SSH_ERR_ALLOC_FAIL; |
| 176 | goto out; |
| 177 | } |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 178 | if ((ret = sshbuf_put_cstring(b, rsa_hash_alg_ident(hash_alg))) != 0 || |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 179 | (ret = sshbuf_put_string(b, sig, slen)) != 0) |
| 180 | goto out; |
| 181 | len = sshbuf_len(b); |
| 182 | if (sigp != NULL) { |
| 183 | if ((*sigp = malloc(len)) == NULL) { |
| 184 | ret = SSH_ERR_ALLOC_FAIL; |
| 185 | goto out; |
| 186 | } |
| 187 | memcpy(*sigp, sshbuf_ptr(b), len); |
| 188 | } |
Ben Lindstrom | 2bf759c | 2002-07-07 22:13:31 +0000 | [diff] [blame] | 189 | if (lenp != NULL) |
| 190 | *lenp = len; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 191 | ret = 0; |
| 192 | out: |
| 193 | explicit_bzero(digest, sizeof(digest)); |
jsing@openbsd.org | 1749991 | 2018-02-07 05:17:56 +0000 | [diff] [blame] | 194 | freezero(sig, slen); |
mmcc@openbsd.org | 52d7078 | 2015-12-11 04:21:11 +0000 | [diff] [blame] | 195 | sshbuf_free(b); |
djm@openbsd.org | 4ef702e | 2015-06-15 01:32:50 +0000 | [diff] [blame] | 196 | return ret; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | int |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 200 | ssh_rsa_verify(const struct sshkey *key, |
djm@openbsd.org | 04c7e28 | 2017-12-18 02:25:15 +0000 | [diff] [blame] | 201 | const u_char *sig, size_t siglen, const u_char *data, size_t datalen, |
| 202 | const char *alg) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 203 | { |
djm@openbsd.org | 04c7e28 | 2017-12-18 02:25:15 +0000 | [diff] [blame] | 204 | char *sigtype = NULL; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 205 | int hash_alg, ret = SSH_ERR_INTERNAL_ERROR; |
jsing@openbsd.org | 1749991 | 2018-02-07 05:17:56 +0000 | [diff] [blame] | 206 | size_t len = 0, diff, modlen, dlen; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 207 | struct sshbuf *b = NULL; |
| 208 | u_char digest[SSH_DIGEST_MAX_LENGTH], *osigblob, *sigblob = NULL; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 209 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 210 | if (key == NULL || key->rsa == NULL || |
| 211 | sshkey_type_plain(key->type) != KEY_RSA || |
djm@openbsd.org | b6e0140 | 2016-04-21 06:08:02 +0000 | [diff] [blame] | 212 | sig == NULL || siglen == 0) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 213 | return SSH_ERR_INVALID_ARGUMENT; |
djm@openbsd.org | bd636f4 | 2017-05-07 23:15:59 +0000 | [diff] [blame] | 214 | if (BN_num_bits(key->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE) |
| 215 | return SSH_ERR_KEY_LENGTH; |
Damien Miller | 3e19295 | 2013-12-29 17:47:50 +1100 | [diff] [blame] | 216 | |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 217 | if ((b = sshbuf_from(sig, siglen)) == NULL) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 218 | return SSH_ERR_ALLOC_FAIL; |
djm@openbsd.org | 04c7e28 | 2017-12-18 02:25:15 +0000 | [diff] [blame] | 219 | if (sshbuf_get_cstring(b, &sigtype, NULL) != 0) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 220 | ret = SSH_ERR_INVALID_FORMAT; |
| 221 | goto out; |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 222 | } |
djm@openbsd.org | 04c7e28 | 2017-12-18 02:25:15 +0000 | [diff] [blame] | 223 | /* XXX djm: need cert types that reliably yield SHA-2 signatures */ |
| 224 | if (alg != NULL && strcmp(alg, sigtype) != 0 && |
| 225 | strcmp(alg, "ssh-rsa-cert-v01@openssh.com") != 0) { |
djm@openbsd.org | 966ef47 | 2017-12-18 23:14:34 +0000 | [diff] [blame] | 226 | error("%s: RSA signature type mismatch: " |
| 227 | "expected %s received %s", __func__, alg, sigtype); |
djm@openbsd.org | 04c7e28 | 2017-12-18 02:25:15 +0000 | [diff] [blame] | 228 | ret = SSH_ERR_SIGNATURE_INVALID; |
| 229 | goto out; |
| 230 | } |
| 231 | if ((hash_alg = rsa_hash_alg_from_ident(sigtype)) == -1) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 232 | ret = SSH_ERR_KEY_TYPE_MISMATCH; |
| 233 | goto out; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 234 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 235 | if (sshbuf_get_string(b, &sigblob, &len) != 0) { |
| 236 | ret = SSH_ERR_INVALID_FORMAT; |
| 237 | goto out; |
| 238 | } |
| 239 | if (sshbuf_len(b) != 0) { |
| 240 | ret = SSH_ERR_UNEXPECTED_TRAILING_DATA; |
| 241 | goto out; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 242 | } |
Ben Lindstrom | ceae9d1 | 2002-06-06 20:55:04 +0000 | [diff] [blame] | 243 | /* RSA_verify expects a signature of RSA_size */ |
| 244 | modlen = RSA_size(key->rsa); |
| 245 | if (len > modlen) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 246 | ret = SSH_ERR_KEY_BITS_MISMATCH; |
| 247 | goto out; |
Ben Lindstrom | ceae9d1 | 2002-06-06 20:55:04 +0000 | [diff] [blame] | 248 | } else if (len < modlen) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 249 | diff = modlen - len; |
| 250 | osigblob = sigblob; |
| 251 | if ((sigblob = realloc(sigblob, modlen)) == NULL) { |
| 252 | sigblob = osigblob; /* put it back for clear/free */ |
| 253 | ret = SSH_ERR_ALLOC_FAIL; |
| 254 | goto out; |
| 255 | } |
Ben Lindstrom | ceae9d1 | 2002-06-06 20:55:04 +0000 | [diff] [blame] | 256 | memmove(sigblob + diff, sigblob, len); |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 257 | explicit_bzero(sigblob, diff); |
Ben Lindstrom | ceae9d1 | 2002-06-06 20:55:04 +0000 | [diff] [blame] | 258 | len = modlen; |
| 259 | } |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 260 | if ((dlen = ssh_digest_bytes(hash_alg)) == 0) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 261 | ret = SSH_ERR_INTERNAL_ERROR; |
| 262 | goto out; |
Ben Lindstrom | 425fb02 | 2001-03-29 00:31:20 +0000 | [diff] [blame] | 263 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 264 | if ((ret = ssh_digest_memory(hash_alg, data, datalen, |
| 265 | digest, sizeof(digest))) != 0) |
| 266 | goto out; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 267 | |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 268 | ret = openssh_RSA_verify(hash_alg, digest, dlen, sigblob, len, |
| 269 | key->rsa); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 270 | out: |
jsing@openbsd.org | 1749991 | 2018-02-07 05:17:56 +0000 | [diff] [blame] | 271 | freezero(sigblob, len); |
djm@openbsd.org | 04c7e28 | 2017-12-18 02:25:15 +0000 | [diff] [blame] | 272 | free(sigtype); |
mmcc@openbsd.org | 52d7078 | 2015-12-11 04:21:11 +0000 | [diff] [blame] | 273 | sshbuf_free(b); |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 274 | explicit_bzero(digest, sizeof(digest)); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 275 | return ret; |
| 276 | } |
Ben Lindstrom | 0deb5d9 | 2002-08-20 18:40:03 +0000 | [diff] [blame] | 277 | |
| 278 | /* |
| 279 | * See: |
| 280 | * http://www.rsasecurity.com/rsalabs/pkcs/pkcs-1/ |
| 281 | * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1.asn |
| 282 | */ |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 283 | |
Ben Lindstrom | 0deb5d9 | 2002-08-20 18:40:03 +0000 | [diff] [blame] | 284 | /* |
| 285 | * id-sha1 OBJECT IDENTIFIER ::= { iso(1) identified-organization(3) |
| 286 | * oiw(14) secsig(3) algorithms(2) 26 } |
| 287 | */ |
| 288 | static const u_char id_sha1[] = { |
| 289 | 0x30, 0x21, /* type Sequence, length 0x21 (33) */ |
| 290 | 0x30, 0x09, /* type Sequence, length 0x09 */ |
| 291 | 0x06, 0x05, /* type OID, length 0x05 */ |
| 292 | 0x2b, 0x0e, 0x03, 0x02, 0x1a, /* id-sha1 OID */ |
| 293 | 0x05, 0x00, /* NULL */ |
| 294 | 0x04, 0x14 /* Octet string, length 0x14 (20), followed by sha1 hash */ |
| 295 | }; |
Ben Lindstrom | 0deb5d9 | 2002-08-20 18:40:03 +0000 | [diff] [blame] | 296 | |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 297 | /* |
| 298 | * See http://csrc.nist.gov/groups/ST/crypto_apps_infra/csor/algorithms.html |
| 299 | * id-sha256 OBJECT IDENTIFIER ::= { joint-iso-itu-t(2) country(16) us(840) |
| 300 | * organization(1) gov(101) csor(3) nistAlgorithm(4) hashAlgs(2) |
| 301 | * id-sha256(1) } |
| 302 | */ |
| 303 | static const u_char id_sha256[] = { |
| 304 | 0x30, 0x31, /* type Sequence, length 0x31 (49) */ |
| 305 | 0x30, 0x0d, /* type Sequence, length 0x0d (13) */ |
| 306 | 0x06, 0x09, /* type OID, length 0x09 */ |
| 307 | 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, /* id-sha256 */ |
| 308 | 0x05, 0x00, /* NULL */ |
| 309 | 0x04, 0x20 /* Octet string, length 0x20 (32), followed by sha256 hash */ |
| 310 | }; |
| 311 | |
| 312 | /* |
| 313 | * See http://csrc.nist.gov/groups/ST/crypto_apps_infra/csor/algorithms.html |
| 314 | * id-sha512 OBJECT IDENTIFIER ::= { joint-iso-itu-t(2) country(16) us(840) |
| 315 | * organization(1) gov(101) csor(3) nistAlgorithm(4) hashAlgs(2) |
| 316 | * id-sha256(3) } |
| 317 | */ |
| 318 | static const u_char id_sha512[] = { |
| 319 | 0x30, 0x51, /* type Sequence, length 0x51 (81) */ |
| 320 | 0x30, 0x0d, /* type Sequence, length 0x0d (13) */ |
| 321 | 0x06, 0x09, /* type OID, length 0x09 */ |
| 322 | 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, /* id-sha512 */ |
| 323 | 0x05, 0x00, /* NULL */ |
| 324 | 0x04, 0x40 /* Octet string, length 0x40 (64), followed by sha512 hash */ |
| 325 | }; |
| 326 | |
| 327 | static int |
| 328 | rsa_hash_alg_oid(int hash_alg, const u_char **oidp, size_t *oidlenp) |
| 329 | { |
| 330 | switch (hash_alg) { |
| 331 | case SSH_DIGEST_SHA1: |
| 332 | *oidp = id_sha1; |
| 333 | *oidlenp = sizeof(id_sha1); |
| 334 | break; |
| 335 | case SSH_DIGEST_SHA256: |
| 336 | *oidp = id_sha256; |
| 337 | *oidlenp = sizeof(id_sha256); |
| 338 | break; |
| 339 | case SSH_DIGEST_SHA512: |
| 340 | *oidp = id_sha512; |
| 341 | *oidlenp = sizeof(id_sha512); |
| 342 | break; |
| 343 | default: |
| 344 | return SSH_ERR_INVALID_ARGUMENT; |
| 345 | } |
| 346 | return 0; |
| 347 | } |
| 348 | |
Ben Lindstrom | 0deb5d9 | 2002-08-20 18:40:03 +0000 | [diff] [blame] | 349 | static int |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 350 | openssh_RSA_verify(int hash_alg, u_char *hash, size_t hashlen, |
| 351 | u_char *sigbuf, size_t siglen, RSA *rsa) |
Ben Lindstrom | 0deb5d9 | 2002-08-20 18:40:03 +0000 | [diff] [blame] | 352 | { |
djm@openbsd.org | 61942ea | 2015-09-09 00:52:44 +0000 | [diff] [blame] | 353 | size_t rsasize = 0, oidlen = 0, hlen = 0; |
| 354 | int ret, len, oidmatch, hashmatch; |
Ben Lindstrom | 0deb5d9 | 2002-08-20 18:40:03 +0000 | [diff] [blame] | 355 | const u_char *oid = NULL; |
| 356 | u_char *decrypted = NULL; |
| 357 | |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 358 | if ((ret = rsa_hash_alg_oid(hash_alg, &oid, &oidlen)) != 0) |
| 359 | return ret; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 360 | ret = SSH_ERR_INTERNAL_ERROR; |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 361 | hlen = ssh_digest_bytes(hash_alg); |
Ben Lindstrom | 0deb5d9 | 2002-08-20 18:40:03 +0000 | [diff] [blame] | 362 | if (hashlen != hlen) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 363 | ret = SSH_ERR_INVALID_ARGUMENT; |
Ben Lindstrom | 0deb5d9 | 2002-08-20 18:40:03 +0000 | [diff] [blame] | 364 | goto done; |
| 365 | } |
| 366 | rsasize = RSA_size(rsa); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 367 | if (rsasize <= 0 || rsasize > SSHBUF_MAX_BIGNUM || |
| 368 | siglen == 0 || siglen > rsasize) { |
| 369 | ret = SSH_ERR_INVALID_ARGUMENT; |
Ben Lindstrom | 0deb5d9 | 2002-08-20 18:40:03 +0000 | [diff] [blame] | 370 | goto done; |
| 371 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 372 | if ((decrypted = malloc(rsasize)) == NULL) { |
| 373 | ret = SSH_ERR_ALLOC_FAIL; |
| 374 | goto done; |
| 375 | } |
Ben Lindstrom | 0deb5d9 | 2002-08-20 18:40:03 +0000 | [diff] [blame] | 376 | if ((len = RSA_public_decrypt(siglen, sigbuf, decrypted, rsa, |
| 377 | RSA_PKCS1_PADDING)) < 0) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 378 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
Ben Lindstrom | 0deb5d9 | 2002-08-20 18:40:03 +0000 | [diff] [blame] | 379 | goto done; |
| 380 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 381 | if (len < 0 || (size_t)len != hlen + oidlen) { |
| 382 | ret = SSH_ERR_INVALID_FORMAT; |
Ben Lindstrom | 0deb5d9 | 2002-08-20 18:40:03 +0000 | [diff] [blame] | 383 | goto done; |
| 384 | } |
Damien Miller | 4e8285e | 2010-08-03 16:04:03 +1000 | [diff] [blame] | 385 | oidmatch = timingsafe_bcmp(decrypted, oid, oidlen) == 0; |
| 386 | hashmatch = timingsafe_bcmp(decrypted + oidlen, hash, hlen) == 0; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 387 | if (!oidmatch || !hashmatch) { |
| 388 | ret = SSH_ERR_SIGNATURE_INVALID; |
Ben Lindstrom | 0deb5d9 | 2002-08-20 18:40:03 +0000 | [diff] [blame] | 389 | goto done; |
| 390 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 391 | ret = 0; |
Ben Lindstrom | 0deb5d9 | 2002-08-20 18:40:03 +0000 | [diff] [blame] | 392 | done: |
jsing@openbsd.org | 1749991 | 2018-02-07 05:17:56 +0000 | [diff] [blame] | 393 | freezero(decrypted, rsasize); |
Ben Lindstrom | 0deb5d9 | 2002-08-20 18:40:03 +0000 | [diff] [blame] | 394 | return ret; |
| 395 | } |
Damien Miller | 72ef7c1 | 2015-01-15 02:21:31 +1100 | [diff] [blame] | 396 | #endif /* WITH_OPENSSL */ |