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 | 36e603d | 2001-11-12 11:03:35 +1100 | [diff] [blame] | 26 | RCSID("$OpenBSD: ssh-rsa.c,v 1.12 2001/11/07 22:10:28 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 | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 36 | #include "ssh-rsa.h" |
Ben Lindstrom | 60a4381 | 2001-03-29 00:32:56 +0000 | [diff] [blame] | 37 | #include "compat.h" |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 38 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 39 | /* RSASSA-PKCS1-v1_5 (PKCS #1 v2.0 signature) with SHA1 */ |
| 40 | int |
| 41 | ssh_rsa_sign( |
| 42 | Key *key, |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 43 | u_char **sigp, int *lenp, |
| 44 | u_char *data, 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 | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 48 | u_char *digest, *sig, *ret; |
| 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 | } |
Damien Miller | 9af8c3c | 2001-11-12 11:03:16 +1100 | [diff] [blame] | 57 | if (datafellows & SSH_BUG_SIGBLOB) { |
| 58 | error("ssh_rsa_sign: SSH_BUG_SIGBLOB not supported"); |
| 59 | return -1; |
| 60 | } |
Ben Lindstrom | 60a4381 | 2001-03-29 00:32:56 +0000 | [diff] [blame] | 61 | nid = (datafellows & SSH_BUG_RSASIGMD5) ? NID_md5 : NID_sha1; |
Ben Lindstrom | 425fb02 | 2001-03-29 00:31:20 +0000 | [diff] [blame] | 62 | if ((evp_md = EVP_get_digestbynid(nid)) == NULL) { |
| 63 | error("ssh_rsa_sign: EVP_get_digestbynid %d failed", nid); |
| 64 | return -1; |
| 65 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 66 | dlen = evp_md->md_size; |
| 67 | digest = xmalloc(dlen); |
| 68 | EVP_DigestInit(&md, evp_md); |
| 69 | EVP_DigestUpdate(&md, data, datalen); |
| 70 | EVP_DigestFinal(&md, digest, NULL); |
| 71 | |
Ben Lindstrom | 425fb02 | 2001-03-29 00:31:20 +0000 | [diff] [blame] | 72 | slen = RSA_size(key->rsa); |
| 73 | sig = xmalloc(slen); |
| 74 | |
| 75 | ok = RSA_sign(nid, digest, dlen, sig, &len, key->rsa); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 76 | memset(digest, 'd', dlen); |
| 77 | xfree(digest); |
| 78 | |
| 79 | if (ok != 1) { |
| 80 | int ecode = ERR_get_error(); |
| 81 | error("ssh_rsa_sign: RSA_sign failed: %s", ERR_error_string(ecode, NULL)); |
| 82 | xfree(sig); |
| 83 | return -1; |
| 84 | } |
| 85 | if (len < slen) { |
| 86 | int diff = slen - len; |
| 87 | debug("slen %d > len %d", slen, len); |
| 88 | memmove(sig + diff, sig, len); |
| 89 | memset(sig, 0, diff); |
| 90 | } else if (len > slen) { |
| 91 | error("ssh_rsa_sign: slen %d slen2 %d", slen, len); |
| 92 | xfree(sig); |
| 93 | return -1; |
| 94 | } |
| 95 | /* encode signature */ |
| 96 | buffer_init(&b); |
| 97 | buffer_put_cstring(&b, "ssh-rsa"); |
| 98 | buffer_put_string(&b, sig, slen); |
| 99 | len = buffer_len(&b); |
| 100 | ret = xmalloc(len); |
| 101 | memcpy(ret, buffer_ptr(&b), len); |
| 102 | buffer_free(&b); |
| 103 | memset(sig, 's', slen); |
| 104 | xfree(sig); |
| 105 | |
| 106 | if (lenp != NULL) |
| 107 | *lenp = len; |
| 108 | if (sigp != NULL) |
| 109 | *sigp = ret; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | int |
| 114 | ssh_rsa_verify( |
| 115 | Key *key, |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 116 | u_char *signature, int signaturelen, |
| 117 | u_char *data, int datalen) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 118 | { |
| 119 | Buffer b; |
Ben Lindstrom | 425fb02 | 2001-03-29 00:31:20 +0000 | [diff] [blame] | 120 | const EVP_MD *evp_md; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 121 | EVP_MD_CTX md; |
| 122 | char *ktype; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 123 | u_char *sigblob, *digest; |
| 124 | u_int len, dlen; |
Ben Lindstrom | 425fb02 | 2001-03-29 00:31:20 +0000 | [diff] [blame] | 125 | int rlen, ret, nid; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 126 | |
| 127 | if (key == NULL || key->type != KEY_RSA || key->rsa == NULL) { |
| 128 | error("ssh_rsa_verify: no RSA key"); |
| 129 | return -1; |
| 130 | } |
Damien Miller | 9af8c3c | 2001-11-12 11:03:16 +1100 | [diff] [blame] | 131 | if (datafellows & SSH_BUG_SIGBLOB) { |
| 132 | error("ssh_rsa_verify: SSH_BUG_SIGBLOB not supported"); |
| 133 | return -1; |
| 134 | } |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 135 | if (BN_num_bits(key->rsa->n) < 768) { |
| 136 | error("ssh_rsa_verify: n too small: %d bits", |
| 137 | BN_num_bits(key->rsa->n)); |
| 138 | return -1; |
| 139 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 140 | buffer_init(&b); |
Ben Lindstrom | 9e0ddd4 | 2001-09-18 05:41:19 +0000 | [diff] [blame] | 141 | buffer_append(&b, signature, signaturelen); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 142 | ktype = buffer_get_string(&b, NULL); |
| 143 | if (strcmp("ssh-rsa", ktype) != 0) { |
| 144 | error("ssh_rsa_verify: cannot handle type %s", ktype); |
| 145 | buffer_free(&b); |
| 146 | xfree(ktype); |
| 147 | return -1; |
| 148 | } |
| 149 | xfree(ktype); |
Ben Lindstrom | 9e0ddd4 | 2001-09-18 05:41:19 +0000 | [diff] [blame] | 150 | sigblob = buffer_get_string(&b, &len); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 151 | rlen = buffer_len(&b); |
| 152 | buffer_free(&b); |
| 153 | if(rlen != 0) { |
| 154 | error("ssh_rsa_verify: remaining bytes in signature %d", rlen); |
Damien Miller | 36e603d | 2001-11-12 11:03:35 +1100 | [diff] [blame] | 155 | xfree(sigblob); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 156 | return -1; |
| 157 | } |
Ben Lindstrom | 60a4381 | 2001-03-29 00:32:56 +0000 | [diff] [blame] | 158 | nid = (datafellows & SSH_BUG_RSASIGMD5) ? NID_md5 : NID_sha1; |
Ben Lindstrom | 425fb02 | 2001-03-29 00:31:20 +0000 | [diff] [blame] | 159 | if ((evp_md = EVP_get_digestbynid(nid)) == NULL) { |
Ben Lindstrom | 425fb02 | 2001-03-29 00:31:20 +0000 | [diff] [blame] | 160 | error("ssh_rsa_verify: EVP_get_digestbynid %d failed", nid); |
Damien Miller | 36e603d | 2001-11-12 11:03:35 +1100 | [diff] [blame] | 161 | xfree(sigblob); |
Ben Lindstrom | 425fb02 | 2001-03-29 00:31:20 +0000 | [diff] [blame] | 162 | return -1; |
| 163 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 164 | dlen = evp_md->md_size; |
| 165 | digest = xmalloc(dlen); |
| 166 | EVP_DigestInit(&md, evp_md); |
| 167 | EVP_DigestUpdate(&md, data, datalen); |
| 168 | EVP_DigestFinal(&md, digest, NULL); |
| 169 | |
Ben Lindstrom | 425fb02 | 2001-03-29 00:31:20 +0000 | [diff] [blame] | 170 | ret = RSA_verify(nid, digest, dlen, sigblob, len, key->rsa); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 171 | memset(digest, 'd', dlen); |
| 172 | xfree(digest); |
| 173 | memset(sigblob, 's', len); |
| 174 | xfree(sigblob); |
| 175 | if (ret == 0) { |
| 176 | int ecode = ERR_get_error(); |
| 177 | error("ssh_rsa_verify: RSA_verify failed: %s", ERR_error_string(ecode, NULL)); |
| 178 | } |
| 179 | debug("ssh_rsa_verify: signature %scorrect", (ret==0) ? "in" : ""); |
| 180 | return ret; |
| 181 | } |