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