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