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