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