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" 26 RCSID("$OpenBSD: ssh-rsa.c,v 1.18 2002/04/02 20:11:38 markus Exp $"); 27 28 #include <openssl/evp.h> 29 #include <openssl/err.h> 30 31 #include "xmalloc.h" 32 #include "log.h" 33 #include "buffer.h" 34 #include "bufaux.h" 35 #include "key.h" 36 #include "ssh-rsa.h" 37 #include "compat.h" 38 #include "ssh.h" 39 40 /* RSASSA-PKCS1-v1_5 (PKCS #1 v2.0 signature) with SHA1 */ 41 int 42 ssh_rsa_sign( 43 Key *key, 44 u_char **sigp, u_int *lenp, 45 u_char *data, u_int datalen) 46 { 47 const EVP_MD *evp_md; 48 EVP_MD_CTX md; 49 u_char digest[EVP_MAX_MD_SIZE], *sig, *ret; 50 u_int slen, dlen, len; 51 int ok, nid; 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 nid = (datafellows & SSH_BUG_RSASIGMD5) ? NID_md5 : NID_sha1; 59 if ((evp_md = EVP_get_digestbynid(nid)) == NULL) { 60 error("ssh_rsa_sign: EVP_get_digestbynid %d failed", nid); 61 return -1; 62 } 63 EVP_DigestInit(&md, evp_md); 64 EVP_DigestUpdate(&md, data, datalen); 65 EVP_DigestFinal(&md, digest, &dlen); 66 67 slen = RSA_size(key->rsa); 68 sig = xmalloc(slen); 69 70 ok = RSA_sign(nid, digest, dlen, sig, &len, key->rsa); 71 memset(digest, 'd', sizeof(digest)); 72 73 if (ok != 1) { 74 int ecode = ERR_get_error(); 75 error("ssh_rsa_sign: RSA_sign failed: %s", ERR_error_string(ecode, NULL)); 76 xfree(sig); 77 return -1; 78 } 79 if (len < slen) { 80 int diff = slen - len; 81 debug("slen %d > len %d", slen, len); 82 memmove(sig + diff, sig, len); 83 memset(sig, 0, diff); 84 } else if (len > slen) { 85 error("ssh_rsa_sign: slen %d slen2 %d", slen, len); 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); 94 ret = xmalloc(len); 95 memcpy(ret, buffer_ptr(&b), len); 96 buffer_free(&b); 97 memset(sig, 's', slen); 98 xfree(sig); 99 100 if (lenp != NULL) 101 *lenp = len; 102 if (sigp != NULL) 103 *sigp = ret; 104 return 0; 105 } 106 107 int 108 ssh_rsa_verify( 109 Key *key, 110 u_char *signature, u_int signaturelen, 111 u_char *data, u_int datalen) 112 { 113 Buffer b; 114 const EVP_MD *evp_md; 115 EVP_MD_CTX md; 116 char *ktype; 117 u_char digest[EVP_MAX_MD_SIZE], *sigblob; 118 u_int len, dlen; 119 int rlen, ret, nid; 120 121 if (key == NULL || key->type != KEY_RSA || key->rsa == NULL) { 122 error("ssh_rsa_verify: no RSA key"); 123 return -1; 124 } 125 if (BN_num_bits(key->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE) { 126 error("ssh_rsa_verify: n too small: %d bits", 127 BN_num_bits(key->rsa->n)); 128 return -1; 129 } 130 buffer_init(&b); 131 buffer_append(&b, 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); 140 sigblob = buffer_get_string(&b, &len); 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 xfree(sigblob); 146 return -1; 147 } 148 nid = (datafellows & SSH_BUG_RSASIGMD5) ? NID_md5 : NID_sha1; 149 if ((evp_md = EVP_get_digestbynid(nid)) == NULL) { 150 error("ssh_rsa_verify: EVP_get_digestbynid %d failed", nid); 151 xfree(sigblob); 152 return -1; 153 } 154 EVP_DigestInit(&md, evp_md); 155 EVP_DigestUpdate(&md, data, datalen); 156 EVP_DigestFinal(&md, digest, &dlen); 157 158 ret = RSA_verify(nid, digest, dlen, sigblob, len, key->rsa); 159 memset(digest, 'd', sizeof(digest)); 160 memset(sigblob, 's', len); 161 xfree(sigblob); 162 if (ret == 0) { 163 int ecode = ERR_get_error(); 164 error("ssh_rsa_verify: RSA_verify failed: %s", ERR_error_string(ecode, NULL)); 165 } 166 debug("ssh_rsa_verify: signature %scorrect", (ret==0) ? "in" : ""); 167 return ret; 168 } 169