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