Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 1 | /* $OpenBSD: ssh-rsa.c,v 1.38 2006/07/22 20:48:23 stevesk 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 | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 17 | #include "includes.h" |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 18 | |
| 19 | #include <openssl/evp.h> |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 20 | #include <openssl/err.h> |
| 21 | |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 22 | #include <string.h> |
| 23 | |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 24 | #include "xmalloc.h" |
| 25 | #include "log.h" |
| 26 | #include "buffer.h" |
| 27 | #include "bufaux.h" |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 28 | #include "key.h" |
Ben Lindstrom | 60a4381 | 2001-03-29 00:32:56 +0000 | [diff] [blame] | 29 | #include "compat.h" |
Ben Lindstrom | 03f3932 | 2002-04-02 20:43:11 +0000 | [diff] [blame] | 30 | #include "ssh.h" |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 31 | |
Ben Lindstrom | 93576d9 | 2002-12-23 02:06:19 +0000 | [diff] [blame] | 32 | static int openssh_RSA_verify(int, u_char *, u_int, u_char *, u_int, RSA *); |
Ben Lindstrom | 0deb5d9 | 2002-08-20 18:40:03 +0000 | [diff] [blame] | 33 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 34 | /* RSASSA-PKCS1-v1_5 (PKCS #1 v2.0 signature) with SHA1 */ |
| 35 | int |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 36 | ssh_rsa_sign(const Key *key, u_char **sigp, u_int *lenp, |
| 37 | const u_char *data, u_int datalen) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 38 | { |
Ben Lindstrom | 425fb02 | 2001-03-29 00:31:20 +0000 | [diff] [blame] | 39 | const EVP_MD *evp_md; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 40 | EVP_MD_CTX md; |
Ben Lindstrom | 2bf759c | 2002-07-07 22:13:31 +0000 | [diff] [blame] | 41 | u_char digest[EVP_MAX_MD_SIZE], *sig; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 42 | u_int slen, dlen, len; |
Ben Lindstrom | 425fb02 | 2001-03-29 00:31:20 +0000 | [diff] [blame] | 43 | int ok, nid; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 44 | Buffer b; |
| 45 | |
| 46 | if (key == NULL || key->type != KEY_RSA || key->rsa == NULL) { |
| 47 | error("ssh_rsa_sign: no RSA key"); |
| 48 | return -1; |
| 49 | } |
Ben Lindstrom | 60a4381 | 2001-03-29 00:32:56 +0000 | [diff] [blame] | 50 | nid = (datafellows & SSH_BUG_RSASIGMD5) ? NID_md5 : NID_sha1; |
Ben Lindstrom | 425fb02 | 2001-03-29 00:31:20 +0000 | [diff] [blame] | 51 | if ((evp_md = EVP_get_digestbynid(nid)) == NULL) { |
| 52 | error("ssh_rsa_sign: EVP_get_digestbynid %d failed", nid); |
| 53 | return -1; |
| 54 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 55 | EVP_DigestInit(&md, evp_md); |
| 56 | EVP_DigestUpdate(&md, data, datalen); |
Damien Miller | c516e92 | 2002-02-05 11:53:43 +1100 | [diff] [blame] | 57 | EVP_DigestFinal(&md, digest, &dlen); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 58 | |
Ben Lindstrom | 425fb02 | 2001-03-29 00:31:20 +0000 | [diff] [blame] | 59 | slen = RSA_size(key->rsa); |
| 60 | sig = xmalloc(slen); |
| 61 | |
| 62 | ok = RSA_sign(nid, digest, dlen, sig, &len, key->rsa); |
Damien Miller | c516e92 | 2002-02-05 11:53:43 +1100 | [diff] [blame] | 63 | memset(digest, 'd', sizeof(digest)); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 64 | |
| 65 | if (ok != 1) { |
| 66 | int ecode = ERR_get_error(); |
Damien Miller | 9096740 | 2006-03-26 14:07:26 +1100 | [diff] [blame] | 67 | |
Ben Lindstrom | 5c38552 | 2002-06-23 21:23:20 +0000 | [diff] [blame] | 68 | error("ssh_rsa_sign: RSA_sign failed: %s", |
| 69 | ERR_error_string(ecode, NULL)); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 70 | xfree(sig); |
| 71 | return -1; |
| 72 | } |
| 73 | if (len < slen) { |
Ben Lindstrom | 0e50d84 | 2002-08-20 18:39:14 +0000 | [diff] [blame] | 74 | u_int diff = slen - len; |
Ben Lindstrom | 5c38552 | 2002-06-23 21:23:20 +0000 | [diff] [blame] | 75 | debug("slen %u > len %u", slen, len); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 76 | memmove(sig + diff, sig, len); |
| 77 | memset(sig, 0, diff); |
| 78 | } else if (len > slen) { |
Ben Lindstrom | 5c38552 | 2002-06-23 21:23:20 +0000 | [diff] [blame] | 79 | error("ssh_rsa_sign: slen %u slen2 %u", slen, len); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 80 | xfree(sig); |
| 81 | return -1; |
| 82 | } |
| 83 | /* encode signature */ |
| 84 | buffer_init(&b); |
| 85 | buffer_put_cstring(&b, "ssh-rsa"); |
| 86 | buffer_put_string(&b, sig, slen); |
| 87 | len = buffer_len(&b); |
Ben Lindstrom | 2bf759c | 2002-07-07 22:13:31 +0000 | [diff] [blame] | 88 | if (lenp != NULL) |
| 89 | *lenp = len; |
| 90 | if (sigp != NULL) { |
| 91 | *sigp = xmalloc(len); |
| 92 | memcpy(*sigp, buffer_ptr(&b), len); |
| 93 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 94 | buffer_free(&b); |
| 95 | memset(sig, 's', slen); |
| 96 | xfree(sig); |
| 97 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | int |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 102 | ssh_rsa_verify(const Key *key, const u_char *signature, u_int signaturelen, |
| 103 | const u_char *data, u_int datalen) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 104 | { |
| 105 | Buffer b; |
Ben Lindstrom | 425fb02 | 2001-03-29 00:31:20 +0000 | [diff] [blame] | 106 | const EVP_MD *evp_md; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 107 | EVP_MD_CTX md; |
| 108 | char *ktype; |
Damien Miller | c516e92 | 2002-02-05 11:53:43 +1100 | [diff] [blame] | 109 | u_char digest[EVP_MAX_MD_SIZE], *sigblob; |
Ben Lindstrom | ceae9d1 | 2002-06-06 20:55:04 +0000 | [diff] [blame] | 110 | u_int len, dlen, modlen; |
Ben Lindstrom | 425fb02 | 2001-03-29 00:31:20 +0000 | [diff] [blame] | 111 | int rlen, ret, nid; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 112 | |
| 113 | if (key == NULL || key->type != KEY_RSA || key->rsa == NULL) { |
| 114 | error("ssh_rsa_verify: no RSA key"); |
| 115 | return -1; |
| 116 | } |
Ben Lindstrom | 03f3932 | 2002-04-02 20:43:11 +0000 | [diff] [blame] | 117 | if (BN_num_bits(key->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE) { |
Ben Lindstrom | 2779d28 | 2002-06-11 15:47:42 +0000 | [diff] [blame] | 118 | error("ssh_rsa_verify: RSA modulus too small: %d < minimum %d bits", |
| 119 | BN_num_bits(key->rsa->n), SSH_RSA_MINIMUM_MODULUS_SIZE); |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 120 | return -1; |
| 121 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 122 | buffer_init(&b); |
Ben Lindstrom | 9e0ddd4 | 2001-09-18 05:41:19 +0000 | [diff] [blame] | 123 | buffer_append(&b, signature, signaturelen); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 124 | ktype = buffer_get_string(&b, NULL); |
| 125 | if (strcmp("ssh-rsa", ktype) != 0) { |
| 126 | error("ssh_rsa_verify: cannot handle type %s", ktype); |
| 127 | buffer_free(&b); |
| 128 | xfree(ktype); |
| 129 | return -1; |
| 130 | } |
| 131 | xfree(ktype); |
Ben Lindstrom | 9e0ddd4 | 2001-09-18 05:41:19 +0000 | [diff] [blame] | 132 | sigblob = buffer_get_string(&b, &len); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 133 | rlen = buffer_len(&b); |
| 134 | buffer_free(&b); |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 135 | if (rlen != 0) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 136 | error("ssh_rsa_verify: remaining bytes in signature %d", rlen); |
Damien Miller | 36e603d | 2001-11-12 11:03:35 +1100 | [diff] [blame] | 137 | xfree(sigblob); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 138 | return -1; |
| 139 | } |
Ben Lindstrom | ceae9d1 | 2002-06-06 20:55:04 +0000 | [diff] [blame] | 140 | /* RSA_verify expects a signature of RSA_size */ |
| 141 | modlen = RSA_size(key->rsa); |
| 142 | if (len > modlen) { |
Ben Lindstrom | 5c38552 | 2002-06-23 21:23:20 +0000 | [diff] [blame] | 143 | error("ssh_rsa_verify: len %u > modlen %u", len, modlen); |
Ben Lindstrom | ceae9d1 | 2002-06-06 20:55:04 +0000 | [diff] [blame] | 144 | xfree(sigblob); |
| 145 | return -1; |
| 146 | } else if (len < modlen) { |
Ben Lindstrom | 0e50d84 | 2002-08-20 18:39:14 +0000 | [diff] [blame] | 147 | u_int diff = modlen - len; |
Ben Lindstrom | 5c38552 | 2002-06-23 21:23:20 +0000 | [diff] [blame] | 148 | debug("ssh_rsa_verify: add padding: modlen %u > len %u", |
Ben Lindstrom | ceae9d1 | 2002-06-06 20:55:04 +0000 | [diff] [blame] | 149 | modlen, len); |
Damien Miller | 3681209 | 2006-03-26 14:22:47 +1100 | [diff] [blame] | 150 | sigblob = xrealloc(sigblob, 1, modlen); |
Ben Lindstrom | ceae9d1 | 2002-06-06 20:55:04 +0000 | [diff] [blame] | 151 | memmove(sigblob + diff, sigblob, len); |
| 152 | memset(sigblob, 0, diff); |
| 153 | len = modlen; |
| 154 | } |
Ben Lindstrom | 60a4381 | 2001-03-29 00:32:56 +0000 | [diff] [blame] | 155 | nid = (datafellows & SSH_BUG_RSASIGMD5) ? NID_md5 : NID_sha1; |
Ben Lindstrom | 425fb02 | 2001-03-29 00:31:20 +0000 | [diff] [blame] | 156 | if ((evp_md = EVP_get_digestbynid(nid)) == NULL) { |
Ben Lindstrom | 425fb02 | 2001-03-29 00:31:20 +0000 | [diff] [blame] | 157 | error("ssh_rsa_verify: EVP_get_digestbynid %d failed", nid); |
Damien Miller | 36e603d | 2001-11-12 11:03:35 +1100 | [diff] [blame] | 158 | xfree(sigblob); |
Ben Lindstrom | 425fb02 | 2001-03-29 00:31:20 +0000 | [diff] [blame] | 159 | return -1; |
| 160 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 161 | EVP_DigestInit(&md, evp_md); |
| 162 | EVP_DigestUpdate(&md, data, datalen); |
Damien Miller | c516e92 | 2002-02-05 11:53:43 +1100 | [diff] [blame] | 163 | EVP_DigestFinal(&md, digest, &dlen); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 164 | |
Ben Lindstrom | 0deb5d9 | 2002-08-20 18:40:03 +0000 | [diff] [blame] | 165 | ret = openssh_RSA_verify(nid, digest, dlen, sigblob, len, key->rsa); |
Damien Miller | c516e92 | 2002-02-05 11:53:43 +1100 | [diff] [blame] | 166 | memset(digest, 'd', sizeof(digest)); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 167 | memset(sigblob, 's', len); |
| 168 | xfree(sigblob); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 169 | debug("ssh_rsa_verify: signature %scorrect", (ret==0) ? "in" : ""); |
| 170 | return ret; |
| 171 | } |
Ben Lindstrom | 0deb5d9 | 2002-08-20 18:40:03 +0000 | [diff] [blame] | 172 | |
| 173 | /* |
| 174 | * See: |
| 175 | * http://www.rsasecurity.com/rsalabs/pkcs/pkcs-1/ |
| 176 | * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1.asn |
| 177 | */ |
| 178 | /* |
| 179 | * id-sha1 OBJECT IDENTIFIER ::= { iso(1) identified-organization(3) |
| 180 | * oiw(14) secsig(3) algorithms(2) 26 } |
| 181 | */ |
| 182 | static const u_char id_sha1[] = { |
| 183 | 0x30, 0x21, /* type Sequence, length 0x21 (33) */ |
| 184 | 0x30, 0x09, /* type Sequence, length 0x09 */ |
| 185 | 0x06, 0x05, /* type OID, length 0x05 */ |
| 186 | 0x2b, 0x0e, 0x03, 0x02, 0x1a, /* id-sha1 OID */ |
| 187 | 0x05, 0x00, /* NULL */ |
| 188 | 0x04, 0x14 /* Octet string, length 0x14 (20), followed by sha1 hash */ |
| 189 | }; |
| 190 | /* |
| 191 | * id-md5 OBJECT IDENTIFIER ::= { iso(1) member-body(2) us(840) |
| 192 | * rsadsi(113549) digestAlgorithm(2) 5 } |
| 193 | */ |
| 194 | static const u_char id_md5[] = { |
| 195 | 0x30, 0x20, /* type Sequence, length 0x20 (32) */ |
| 196 | 0x30, 0x0c, /* type Sequence, length 0x09 */ |
| 197 | 0x06, 0x08, /* type OID, length 0x05 */ |
| 198 | 0x2a, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x02, 0x05, /* id-md5 */ |
| 199 | 0x05, 0x00, /* NULL */ |
| 200 | 0x04, 0x10 /* Octet string, length 0x10 (16), followed by md5 hash */ |
| 201 | }; |
| 202 | |
| 203 | static int |
| 204 | openssh_RSA_verify(int type, u_char *hash, u_int hashlen, |
| 205 | u_char *sigbuf, u_int siglen, RSA *rsa) |
| 206 | { |
Damien Miller | f7c2391 | 2002-09-04 16:39:48 +1000 | [diff] [blame] | 207 | u_int ret, rsasize, oidlen = 0, hlen = 0; |
| 208 | int len; |
Ben Lindstrom | 0deb5d9 | 2002-08-20 18:40:03 +0000 | [diff] [blame] | 209 | const u_char *oid = NULL; |
| 210 | u_char *decrypted = NULL; |
| 211 | |
| 212 | ret = 0; |
| 213 | switch (type) { |
| 214 | case NID_sha1: |
| 215 | oid = id_sha1; |
| 216 | oidlen = sizeof(id_sha1); |
| 217 | hlen = 20; |
| 218 | break; |
| 219 | case NID_md5: |
| 220 | oid = id_md5; |
| 221 | oidlen = sizeof(id_md5); |
| 222 | hlen = 16; |
| 223 | break; |
| 224 | default: |
| 225 | goto done; |
Ben Lindstrom | 0deb5d9 | 2002-08-20 18:40:03 +0000 | [diff] [blame] | 226 | } |
| 227 | if (hashlen != hlen) { |
| 228 | error("bad hashlen"); |
| 229 | goto done; |
| 230 | } |
| 231 | rsasize = RSA_size(rsa); |
| 232 | if (siglen == 0 || siglen > rsasize) { |
| 233 | error("bad siglen"); |
| 234 | goto done; |
| 235 | } |
| 236 | decrypted = xmalloc(rsasize); |
| 237 | if ((len = RSA_public_decrypt(siglen, sigbuf, decrypted, rsa, |
| 238 | RSA_PKCS1_PADDING)) < 0) { |
| 239 | error("RSA_public_decrypt failed: %s", |
| 240 | ERR_error_string(ERR_get_error(), NULL)); |
| 241 | goto done; |
| 242 | } |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 243 | if (len < 0 || (u_int)len != hlen + oidlen) { |
Darren Tucker | a251f80 | 2003-06-22 20:45:15 +1000 | [diff] [blame] | 244 | error("bad decrypted len: %d != %d + %d", len, hlen, oidlen); |
Ben Lindstrom | 0deb5d9 | 2002-08-20 18:40:03 +0000 | [diff] [blame] | 245 | goto done; |
| 246 | } |
| 247 | if (memcmp(decrypted, oid, oidlen) != 0) { |
| 248 | error("oid mismatch"); |
| 249 | goto done; |
| 250 | } |
| 251 | if (memcmp(decrypted + oidlen, hash, hlen) != 0) { |
| 252 | error("hash mismatch"); |
| 253 | goto done; |
| 254 | } |
| 255 | ret = 1; |
| 256 | done: |
| 257 | if (decrypted) |
| 258 | xfree(decrypted); |
| 259 | return ret; |
| 260 | } |