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