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