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