1 /* $OpenBSD: ssh-rsa.c,v 1.52 2014/06/24 01:13:21 djm 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.
20 #include <sys/types.h>
22 #include <openssl/evp.h>
23 #include <openssl/err.h>
31 #define SSHKEY_INTERNAL
35 static int openssh_RSA_verify(int, u_char
*, size_t, u_char
*, size_t, RSA
*);
37 /* RSASSA-PKCS1-v1_5 (PKCS #1 v2.0 signature) with SHA1 */
39 ssh_rsa_sign(const struct sshkey
*key
, u_char
**sigp
, size_t *lenp
,
40 const u_char
*data
, size_t datalen
, u_int compat
)
43 u_char digest
[SSH_DIGEST_MAX_LENGTH
], *sig
= NULL
;
46 int nid
, ret
= SSH_ERR_INTERNAL_ERROR
;
47 struct sshbuf
*b
= NULL
;
54 if (key
== NULL
|| key
->rsa
== NULL
||
55 sshkey_type_plain(key
->type
) != KEY_RSA
)
56 return SSH_ERR_INVALID_ARGUMENT
;
57 slen
= RSA_size(key
->rsa
);
58 if (slen
<= 0 || slen
> SSHBUF_MAX_BIGNUM
)
59 return SSH_ERR_INVALID_ARGUMENT
;
62 hash_alg
= SSH_DIGEST_SHA1
;
64 if ((dlen
= ssh_digest_bytes(hash_alg
)) == 0)
65 return SSH_ERR_INTERNAL_ERROR
;
66 if ((ret
= ssh_digest_memory(hash_alg
, data
, datalen
,
67 digest
, sizeof(digest
))) != 0)
70 if ((sig
= malloc(slen
)) == NULL
) {
71 ret
= SSH_ERR_ALLOC_FAIL
;
75 if (RSA_sign(nid
, digest
, dlen
, sig
, &len
, key
->rsa
) != 1) {
76 ret
= SSH_ERR_LIBCRYPTO_ERROR
;
80 size_t diff
= slen
- len
;
81 memmove(sig
+ diff
, sig
, len
);
82 explicit_bzero(sig
, diff
);
83 } else if (len
> slen
) {
84 ret
= SSH_ERR_INTERNAL_ERROR
;
87 /* encode signature */
88 if ((b
= sshbuf_new()) == NULL
) {
89 ret
= SSH_ERR_ALLOC_FAIL
;
92 if ((ret
= sshbuf_put_cstring(b
, "ssh-rsa")) != 0 ||
93 (ret
= sshbuf_put_string(b
, sig
, slen
)) != 0)
97 if ((*sigp
= malloc(len
)) == NULL
) {
98 ret
= SSH_ERR_ALLOC_FAIL
;
101 memcpy(*sigp
, sshbuf_ptr(b
), len
);
107 explicit_bzero(digest
, sizeof(digest
));
109 explicit_bzero(sig
, slen
);
118 ssh_rsa_verify(const struct sshkey
*key
,
119 const u_char
*signature
, size_t signaturelen
,
120 const u_char
*data
, size_t datalen
, u_int compat
)
123 int hash_alg
, ret
= SSH_ERR_INTERNAL_ERROR
;
124 size_t len
, diff
, modlen
, dlen
;
125 struct sshbuf
*b
= NULL
;
126 u_char digest
[SSH_DIGEST_MAX_LENGTH
], *osigblob
, *sigblob
= NULL
;
128 if (key
== NULL
|| key
->rsa
== NULL
||
129 sshkey_type_plain(key
->type
) != KEY_RSA
||
130 BN_num_bits(key
->rsa
->n
) < SSH_RSA_MINIMUM_MODULUS_SIZE
)
131 return SSH_ERR_INVALID_ARGUMENT
;
133 if ((b
= sshbuf_from(signature
, signaturelen
)) == NULL
)
134 return SSH_ERR_ALLOC_FAIL
;
135 if (sshbuf_get_cstring(b
, &ktype
, NULL
) != 0) {
136 ret
= SSH_ERR_INVALID_FORMAT
;
139 if (strcmp("ssh-rsa", ktype
) != 0) {
140 ret
= SSH_ERR_KEY_TYPE_MISMATCH
;
143 if (sshbuf_get_string(b
, &sigblob
, &len
) != 0) {
144 ret
= SSH_ERR_INVALID_FORMAT
;
147 if (sshbuf_len(b
) != 0) {
148 ret
= SSH_ERR_UNEXPECTED_TRAILING_DATA
;
151 /* RSA_verify expects a signature of RSA_size */
152 modlen
= RSA_size(key
->rsa
);
154 ret
= SSH_ERR_KEY_BITS_MISMATCH
;
156 } else if (len
< modlen
) {
159 if ((sigblob
= realloc(sigblob
, modlen
)) == NULL
) {
160 sigblob
= osigblob
; /* put it back for clear/free */
161 ret
= SSH_ERR_ALLOC_FAIL
;
164 memmove(sigblob
+ diff
, sigblob
, len
);
165 explicit_bzero(sigblob
, diff
);
168 hash_alg
= SSH_DIGEST_SHA1
;
169 if ((dlen
= ssh_digest_bytes(hash_alg
)) == 0) {
170 ret
= SSH_ERR_INTERNAL_ERROR
;
173 if ((ret
= ssh_digest_memory(hash_alg
, data
, datalen
,
174 digest
, sizeof(digest
))) != 0)
177 ret
= openssh_RSA_verify(hash_alg
, digest
, dlen
, sigblob
, len
,
180 if (sigblob
!= NULL
) {
181 explicit_bzero(sigblob
, len
);
188 explicit_bzero(digest
, sizeof(digest
));
194 * http://www.rsasecurity.com/rsalabs/pkcs/pkcs-1/
195 * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1.asn
198 * id-sha1 OBJECT IDENTIFIER ::= { iso(1) identified-organization(3)
199 * oiw(14) secsig(3) algorithms(2) 26 }
201 static const u_char id_sha1
[] = {
202 0x30, 0x21, /* type Sequence, length 0x21 (33) */
203 0x30, 0x09, /* type Sequence, length 0x09 */
204 0x06, 0x05, /* type OID, length 0x05 */
205 0x2b, 0x0e, 0x03, 0x02, 0x1a, /* id-sha1 OID */
206 0x05, 0x00, /* NULL */
207 0x04, 0x14 /* Octet string, length 0x14 (20), followed by sha1 hash */
211 openssh_RSA_verify(int hash_alg
, u_char
*hash
, size_t hashlen
,
212 u_char
*sigbuf
, size_t siglen
, RSA
*rsa
)
214 size_t ret
, rsasize
= 0, oidlen
= 0, hlen
= 0;
215 int len
, oidmatch
, hashmatch
;
216 const u_char
*oid
= NULL
;
217 u_char
*decrypted
= NULL
;
219 ret
= SSH_ERR_INTERNAL_ERROR
;
221 case SSH_DIGEST_SHA1
:
223 oidlen
= sizeof(id_sha1
);
229 if (hashlen
!= hlen
) {
230 ret
= SSH_ERR_INVALID_ARGUMENT
;
233 rsasize
= RSA_size(rsa
);
234 if (rsasize
<= 0 || rsasize
> SSHBUF_MAX_BIGNUM
||
235 siglen
== 0 || siglen
> rsasize
) {
236 ret
= SSH_ERR_INVALID_ARGUMENT
;
239 if ((decrypted
= malloc(rsasize
)) == NULL
) {
240 ret
= SSH_ERR_ALLOC_FAIL
;
243 if ((len
= RSA_public_decrypt(siglen
, sigbuf
, decrypted
, rsa
,
244 RSA_PKCS1_PADDING
)) < 0) {
245 ret
= SSH_ERR_LIBCRYPTO_ERROR
;
248 if (len
< 0 || (size_t)len
!= hlen
+ oidlen
) {
249 ret
= SSH_ERR_INVALID_FORMAT
;
252 oidmatch
= timingsafe_bcmp(decrypted
, oid
, oidlen
) == 0;
253 hashmatch
= timingsafe_bcmp(decrypted
+ oidlen
, hash
, hlen
) == 0;
254 if (!oidmatch
|| !hashmatch
) {
255 ret
= SSH_ERR_SIGNATURE_INVALID
;
261 explicit_bzero(decrypted
, rsasize
);