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