2 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 RCSID("$OpenBSD: ssh-rsa.c,v 1.26 2002/08/27 17:13:56 stevesk Exp $");
28 #include <openssl/evp.h>
29 #include <openssl/err.h>
40 static int openssh_RSA_verify(int, u_char *, u_int, u_char *, u_int , RSA *);
42 /* RSASSA-PKCS1-v1_5 (PKCS #1 v2.0 signature) with SHA1 */
44 ssh_rsa_sign(Key *key, u_char **sigp, u_int *lenp,
45 u_char *data, u_int datalen)
49 u_char digest[EVP_MAX_MD_SIZE], *sig;
50 u_int slen, dlen, len;
54 if (key == NULL || key->type != KEY_RSA || key->rsa == NULL) {
55 error("ssh_rsa_sign: no RSA key");
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);
63 EVP_DigestInit(&md, evp_md);
64 EVP_DigestUpdate(&md, data, datalen);
65 EVP_DigestFinal(&md, digest, &dlen);
67 slen = RSA_size(key->rsa);
70 ok = RSA_sign(nid, digest, dlen, sig, &len, key->rsa);
71 memset(digest, 'd', sizeof(digest));
74 int ecode = ERR_get_error();
75 error("ssh_rsa_sign: RSA_sign failed: %s",
76 ERR_error_string(ecode, NULL));
81 u_int diff = slen - len;
82 debug("slen %u > len %u", slen, len);
83 memmove(sig + diff, sig, len);
85 } else if (len > slen) {
86 error("ssh_rsa_sign: slen %u slen2 %u", slen, len);
90 /* encode signature */
92 buffer_put_cstring(&b, "ssh-rsa");
93 buffer_put_string(&b, sig, slen);
99 memcpy(*sigp, buffer_ptr(&b), len);
102 memset(sig, 's', slen);
109 ssh_rsa_verify(Key *key, u_char *signature, u_int signaturelen,
110 u_char *data, u_int datalen)
113 const EVP_MD *evp_md;
116 u_char digest[EVP_MAX_MD_SIZE], *sigblob;
117 u_int len, dlen, modlen;
120 if (key == NULL || key->type != KEY_RSA || key->rsa == NULL) {
121 error("ssh_rsa_verify: no RSA key");
124 if (BN_num_bits(key->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE) {
125 error("ssh_rsa_verify: RSA modulus too small: %d < minimum %d bits",
126 BN_num_bits(key->rsa->n), SSH_RSA_MINIMUM_MODULUS_SIZE);
130 buffer_append(&b, signature, signaturelen);
131 ktype = buffer_get_string(&b, NULL);
132 if (strcmp("ssh-rsa", ktype) != 0) {
133 error("ssh_rsa_verify: cannot handle type %s", ktype);
139 sigblob = buffer_get_string(&b, &len);
140 rlen = buffer_len(&b);
143 error("ssh_rsa_verify: remaining bytes in signature %d", rlen);
147 /* RSA_verify expects a signature of RSA_size */
148 modlen = RSA_size(key->rsa);
150 error("ssh_rsa_verify: len %u > modlen %u", len, modlen);
153 } else if (len < modlen) {
154 u_int diff = modlen - len;
155 debug("ssh_rsa_verify: add padding: modlen %u > len %u",
157 sigblob = xrealloc(sigblob, modlen);
158 memmove(sigblob + diff, sigblob, len);
159 memset(sigblob, 0, diff);
162 nid = (datafellows & SSH_BUG_RSASIGMD5) ? NID_md5 : NID_sha1;
163 if ((evp_md = EVP_get_digestbynid(nid)) == NULL) {
164 error("ssh_rsa_verify: EVP_get_digestbynid %d failed", nid);
168 EVP_DigestInit(&md, evp_md);
169 EVP_DigestUpdate(&md, data, datalen);
170 EVP_DigestFinal(&md, digest, &dlen);
172 ret = openssh_RSA_verify(nid, digest, dlen, sigblob, len, key->rsa);
173 memset(digest, 'd', sizeof(digest));
174 memset(sigblob, 's', len);
176 debug("ssh_rsa_verify: signature %scorrect", (ret==0) ? "in" : "");
182 * http://www.rsasecurity.com/rsalabs/pkcs/pkcs-1/
183 * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1.asn
186 * id-sha1 OBJECT IDENTIFIER ::= { iso(1) identified-organization(3)
187 * oiw(14) secsig(3) algorithms(2) 26 }
189 static const u_char id_sha1[] = {
190 0x30, 0x21, /* type Sequence, length 0x21 (33) */
191 0x30, 0x09, /* type Sequence, length 0x09 */
192 0x06, 0x05, /* type OID, length 0x05 */
193 0x2b, 0x0e, 0x03, 0x02, 0x1a, /* id-sha1 OID */
194 0x05, 0x00, /* NULL */
195 0x04, 0x14 /* Octet string, length 0x14 (20), followed by sha1 hash */
198 * id-md5 OBJECT IDENTIFIER ::= { iso(1) member-body(2) us(840)
199 * rsadsi(113549) digestAlgorithm(2) 5 }
201 static const u_char id_md5[] = {
202 0x30, 0x20, /* type Sequence, length 0x20 (32) */
203 0x30, 0x0c, /* type Sequence, length 0x09 */
204 0x06, 0x08, /* type OID, length 0x05 */
205 0x2a, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x02, 0x05, /* id-md5 */
206 0x05, 0x00, /* NULL */
207 0x04, 0x10 /* Octet string, length 0x10 (16), followed by md5 hash */
211 openssh_RSA_verify(int type, u_char *hash, u_int hashlen,
212 u_char *sigbuf, u_int siglen, RSA *rsa)
214 u_int ret, rsasize, oidlen = 0, hlen = 0;
216 const u_char *oid = NULL;
217 u_char *decrypted = NULL;
223 oidlen = sizeof(id_sha1);
228 oidlen = sizeof(id_md5);
235 if (hashlen != hlen) {
236 error("bad hashlen");
239 rsasize = RSA_size(rsa);
240 if (siglen == 0 || siglen > rsasize) {
244 decrypted = xmalloc(rsasize);
245 if ((len = RSA_public_decrypt(siglen, sigbuf, decrypted, rsa,
246 RSA_PKCS1_PADDING)) < 0) {
247 error("RSA_public_decrypt failed: %s",
248 ERR_error_string(ERR_get_error(), NULL));
251 if (len != hlen + oidlen) {
252 error("bad decrypted len: %d != %d + %d", len, hlen, oidlen);
255 if (memcmp(decrypted, oid, oidlen) != 0) {
256 error("oid mismatch");
259 if (memcmp(decrypted + oidlen, hash, hlen) != 0) {
260 error("hash mismatch");