1 /* $OpenBSD: ssh-rsa.c,v 1.38 2006/07/22 20:48:23 stevesk Exp $ */
3 * Copyright (c) 2000, 2003 Markus Friedl <markus@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <openssl/evp.h>
20 #include <openssl/err.h>
32 static int openssh_RSA_verify(int, u_char
*, u_int
, u_char
*, u_int
, RSA
*);
34 /* RSASSA-PKCS1-v1_5 (PKCS #1 v2.0 signature) with SHA1 */
36 ssh_rsa_sign(const Key
*key
, u_char
**sigp
, u_int
*lenp
,
37 const u_char
*data
, u_int datalen
)
41 u_char digest
[EVP_MAX_MD_SIZE
], *sig
;
42 u_int slen
, dlen
, len
;
46 if (key
== NULL
|| key
->type
!= KEY_RSA
|| key
->rsa
== NULL
) {
47 error("ssh_rsa_sign: no RSA key");
50 nid
= (datafellows
& SSH_BUG_RSASIGMD5
) ? NID_md5
: NID_sha1
;
51 if ((evp_md
= EVP_get_digestbynid(nid
)) == NULL
) {
52 error("ssh_rsa_sign: EVP_get_digestbynid %d failed", nid
);
55 EVP_DigestInit(&md
, evp_md
);
56 EVP_DigestUpdate(&md
, data
, datalen
);
57 EVP_DigestFinal(&md
, digest
, &dlen
);
59 slen
= RSA_size(key
->rsa
);
62 ok
= RSA_sign(nid
, digest
, dlen
, sig
, &len
, key
->rsa
);
63 memset(digest
, 'd', sizeof(digest
));
66 int ecode
= ERR_get_error();
68 error("ssh_rsa_sign: RSA_sign failed: %s",
69 ERR_error_string(ecode
, NULL
));
74 u_int diff
= slen
- len
;
75 debug("slen %u > len %u", slen
, len
);
76 memmove(sig
+ diff
, sig
, len
);
78 } else if (len
> slen
) {
79 error("ssh_rsa_sign: slen %u slen2 %u", slen
, len
);
83 /* encode signature */
85 buffer_put_cstring(&b
, "ssh-rsa");
86 buffer_put_string(&b
, sig
, slen
);
92 memcpy(*sigp
, buffer_ptr(&b
), len
);
95 memset(sig
, 's', slen
);
102 ssh_rsa_verify(const Key
*key
, const u_char
*signature
, u_int signaturelen
,
103 const u_char
*data
, u_int datalen
)
106 const EVP_MD
*evp_md
;
109 u_char digest
[EVP_MAX_MD_SIZE
], *sigblob
;
110 u_int len
, dlen
, modlen
;
113 if (key
== NULL
|| key
->type
!= KEY_RSA
|| key
->rsa
== NULL
) {
114 error("ssh_rsa_verify: no RSA key");
117 if (BN_num_bits(key
->rsa
->n
) < SSH_RSA_MINIMUM_MODULUS_SIZE
) {
118 error("ssh_rsa_verify: RSA modulus too small: %d < minimum %d bits",
119 BN_num_bits(key
->rsa
->n
), SSH_RSA_MINIMUM_MODULUS_SIZE
);
123 buffer_append(&b
, signature
, signaturelen
);
124 ktype
= buffer_get_string(&b
, NULL
);
125 if (strcmp("ssh-rsa", ktype
) != 0) {
126 error("ssh_rsa_verify: cannot handle type %s", ktype
);
132 sigblob
= buffer_get_string(&b
, &len
);
133 rlen
= buffer_len(&b
);
136 error("ssh_rsa_verify: remaining bytes in signature %d", rlen
);
140 /* RSA_verify expects a signature of RSA_size */
141 modlen
= RSA_size(key
->rsa
);
143 error("ssh_rsa_verify: len %u > modlen %u", len
, modlen
);
146 } else if (len
< modlen
) {
147 u_int diff
= modlen
- len
;
148 debug("ssh_rsa_verify: add padding: modlen %u > len %u",
150 sigblob
= xrealloc(sigblob
, 1, modlen
);
151 memmove(sigblob
+ diff
, sigblob
, len
);
152 memset(sigblob
, 0, diff
);
155 nid
= (datafellows
& SSH_BUG_RSASIGMD5
) ? NID_md5
: NID_sha1
;
156 if ((evp_md
= EVP_get_digestbynid(nid
)) == NULL
) {
157 error("ssh_rsa_verify: EVP_get_digestbynid %d failed", nid
);
161 EVP_DigestInit(&md
, evp_md
);
162 EVP_DigestUpdate(&md
, data
, datalen
);
163 EVP_DigestFinal(&md
, digest
, &dlen
);
165 ret
= openssh_RSA_verify(nid
, digest
, dlen
, sigblob
, len
, key
->rsa
);
166 memset(digest
, 'd', sizeof(digest
));
167 memset(sigblob
, 's', len
);
169 debug("ssh_rsa_verify: signature %scorrect", (ret
==0) ? "in" : "");
175 * http://www.rsasecurity.com/rsalabs/pkcs/pkcs-1/
176 * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1.asn
179 * id-sha1 OBJECT IDENTIFIER ::= { iso(1) identified-organization(3)
180 * oiw(14) secsig(3) algorithms(2) 26 }
182 static const u_char id_sha1
[] = {
183 0x30, 0x21, /* type Sequence, length 0x21 (33) */
184 0x30, 0x09, /* type Sequence, length 0x09 */
185 0x06, 0x05, /* type OID, length 0x05 */
186 0x2b, 0x0e, 0x03, 0x02, 0x1a, /* id-sha1 OID */
187 0x05, 0x00, /* NULL */
188 0x04, 0x14 /* Octet string, length 0x14 (20), followed by sha1 hash */
191 * id-md5 OBJECT IDENTIFIER ::= { iso(1) member-body(2) us(840)
192 * rsadsi(113549) digestAlgorithm(2) 5 }
194 static const u_char id_md5
[] = {
195 0x30, 0x20, /* type Sequence, length 0x20 (32) */
196 0x30, 0x0c, /* type Sequence, length 0x09 */
197 0x06, 0x08, /* type OID, length 0x05 */
198 0x2a, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x02, 0x05, /* id-md5 */
199 0x05, 0x00, /* NULL */
200 0x04, 0x10 /* Octet string, length 0x10 (16), followed by md5 hash */
204 openssh_RSA_verify(int type
, u_char
*hash
, u_int hashlen
,
205 u_char
*sigbuf
, u_int siglen
, RSA
*rsa
)
207 u_int ret
, rsasize
, oidlen
= 0, hlen
= 0;
209 const u_char
*oid
= NULL
;
210 u_char
*decrypted
= NULL
;
216 oidlen
= sizeof(id_sha1
);
221 oidlen
= sizeof(id_md5
);
227 if (hashlen
!= hlen
) {
228 error("bad hashlen");
231 rsasize
= RSA_size(rsa
);
232 if (siglen
== 0 || siglen
> rsasize
) {
236 decrypted
= xmalloc(rsasize
);
237 if ((len
= RSA_public_decrypt(siglen
, sigbuf
, decrypted
, rsa
,
238 RSA_PKCS1_PADDING
)) < 0) {
239 error("RSA_public_decrypt failed: %s",
240 ERR_error_string(ERR_get_error(), NULL
));
243 if (len
< 0 || (u_int
)len
!= hlen
+ oidlen
) {
244 error("bad decrypted len: %d != %d + %d", len
, hlen
, oidlen
);
247 if (memcmp(decrypted
, oid
, oidlen
) != 0) {
248 error("oid mismatch");
251 if (memcmp(decrypted
+ oidlen
, hash
, hlen
) != 0) {
252 error("hash mismatch");