- (dtucker) [contrib/aix/buildbff.sh] Fix creation of ssh_prng_cmds.default
[openssh-git.git] / ssh-rsa.c
blobbb9cc8e201638ea894125f46d9e19154434aca4f
1 /* $OpenBSD: ssh-rsa.c,v 1.41 2010/04/16 01:47:26 djm Exp $ */
2 /*
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.
18 #include "includes.h"
20 #include <sys/types.h>
22 #include <openssl/evp.h>
23 #include <openssl/err.h>
25 #include <stdarg.h>
26 #include <string.h>
28 #include "xmalloc.h"
29 #include "log.h"
30 #include "buffer.h"
31 #include "key.h"
32 #include "compat.h"
33 #include "ssh.h"
35 static int openssh_RSA_verify(int, u_char *, u_int, u_char *, u_int, RSA *);
37 /* RSASSA-PKCS1-v1_5 (PKCS #1 v2.0 signature) with SHA1 */
38 int
39 ssh_rsa_sign(const Key *key, u_char **sigp, u_int *lenp,
40 const u_char *data, u_int datalen)
42 const EVP_MD *evp_md;
43 EVP_MD_CTX md;
44 u_char digest[EVP_MAX_MD_SIZE], *sig;
45 u_int slen, dlen, len;
46 int ok, nid;
47 Buffer b;
49 if (key == NULL || key->rsa == NULL || (key->type != KEY_RSA &&
50 key->type != KEY_RSA_CERT && key->type != KEY_RSA_CERT_V00)) {
51 error("ssh_rsa_sign: no RSA key");
52 return -1;
54 nid = (datafellows & SSH_BUG_RSASIGMD5) ? NID_md5 : NID_sha1;
55 if ((evp_md = EVP_get_digestbynid(nid)) == NULL) {
56 error("ssh_rsa_sign: EVP_get_digestbynid %d failed", nid);
57 return -1;
59 EVP_DigestInit(&md, evp_md);
60 EVP_DigestUpdate(&md, data, datalen);
61 EVP_DigestFinal(&md, digest, &dlen);
63 slen = RSA_size(key->rsa);
64 sig = xmalloc(slen);
66 ok = RSA_sign(nid, digest, dlen, sig, &len, key->rsa);
67 memset(digest, 'd', sizeof(digest));
69 if (ok != 1) {
70 int ecode = ERR_get_error();
72 error("ssh_rsa_sign: RSA_sign failed: %s",
73 ERR_error_string(ecode, NULL));
74 xfree(sig);
75 return -1;
77 if (len < slen) {
78 u_int diff = slen - len;
79 debug("slen %u > len %u", slen, len);
80 memmove(sig + diff, sig, len);
81 memset(sig, 0, diff);
82 } else if (len > slen) {
83 error("ssh_rsa_sign: slen %u slen2 %u", slen, len);
84 xfree(sig);
85 return -1;
87 /* encode signature */
88 buffer_init(&b);
89 buffer_put_cstring(&b, "ssh-rsa");
90 buffer_put_string(&b, sig, slen);
91 len = buffer_len(&b);
92 if (lenp != NULL)
93 *lenp = len;
94 if (sigp != NULL) {
95 *sigp = xmalloc(len);
96 memcpy(*sigp, buffer_ptr(&b), len);
98 buffer_free(&b);
99 memset(sig, 's', slen);
100 xfree(sig);
102 return 0;
106 ssh_rsa_verify(const Key *key, const u_char *signature, u_int signaturelen,
107 const u_char *data, u_int datalen)
109 Buffer b;
110 const EVP_MD *evp_md;
111 EVP_MD_CTX md;
112 char *ktype;
113 u_char digest[EVP_MAX_MD_SIZE], *sigblob;
114 u_int len, dlen, modlen;
115 int rlen, ret, nid;
117 if (key == NULL || key->rsa == NULL || (key->type != KEY_RSA &&
118 key->type != KEY_RSA_CERT && key->type != KEY_RSA_CERT_V00)) {
119 error("ssh_rsa_verify: no RSA key");
120 return -1;
122 if (BN_num_bits(key->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE) {
123 error("ssh_rsa_verify: RSA modulus too small: %d < minimum %d bits",
124 BN_num_bits(key->rsa->n), SSH_RSA_MINIMUM_MODULUS_SIZE);
125 return -1;
127 buffer_init(&b);
128 buffer_append(&b, signature, signaturelen);
129 ktype = buffer_get_string(&b, NULL);
130 if (strcmp("ssh-rsa", ktype) != 0) {
131 error("ssh_rsa_verify: cannot handle type %s", ktype);
132 buffer_free(&b);
133 xfree(ktype);
134 return -1;
136 xfree(ktype);
137 sigblob = buffer_get_string(&b, &len);
138 rlen = buffer_len(&b);
139 buffer_free(&b);
140 if (rlen != 0) {
141 error("ssh_rsa_verify: remaining bytes in signature %d", rlen);
142 xfree(sigblob);
143 return -1;
145 /* RSA_verify expects a signature of RSA_size */
146 modlen = RSA_size(key->rsa);
147 if (len > modlen) {
148 error("ssh_rsa_verify: len %u > modlen %u", len, modlen);
149 xfree(sigblob);
150 return -1;
151 } else if (len < modlen) {
152 u_int diff = modlen - len;
153 debug("ssh_rsa_verify: add padding: modlen %u > len %u",
154 modlen, len);
155 sigblob = xrealloc(sigblob, 1, modlen);
156 memmove(sigblob + diff, sigblob, len);
157 memset(sigblob, 0, diff);
158 len = modlen;
160 nid = (datafellows & SSH_BUG_RSASIGMD5) ? NID_md5 : NID_sha1;
161 if ((evp_md = EVP_get_digestbynid(nid)) == NULL) {
162 error("ssh_rsa_verify: EVP_get_digestbynid %d failed", nid);
163 xfree(sigblob);
164 return -1;
166 EVP_DigestInit(&md, evp_md);
167 EVP_DigestUpdate(&md, data, datalen);
168 EVP_DigestFinal(&md, digest, &dlen);
170 ret = openssh_RSA_verify(nid, digest, dlen, sigblob, len, key->rsa);
171 memset(digest, 'd', sizeof(digest));
172 memset(sigblob, 's', len);
173 xfree(sigblob);
174 debug("ssh_rsa_verify: signature %scorrect", (ret==0) ? "in" : "");
175 return ret;
179 * See:
180 * http://www.rsasecurity.com/rsalabs/pkcs/pkcs-1/
181 * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1.asn
184 * id-sha1 OBJECT IDENTIFIER ::= { iso(1) identified-organization(3)
185 * oiw(14) secsig(3) algorithms(2) 26 }
187 static const u_char id_sha1[] = {
188 0x30, 0x21, /* type Sequence, length 0x21 (33) */
189 0x30, 0x09, /* type Sequence, length 0x09 */
190 0x06, 0x05, /* type OID, length 0x05 */
191 0x2b, 0x0e, 0x03, 0x02, 0x1a, /* id-sha1 OID */
192 0x05, 0x00, /* NULL */
193 0x04, 0x14 /* Octet string, length 0x14 (20), followed by sha1 hash */
196 * id-md5 OBJECT IDENTIFIER ::= { iso(1) member-body(2) us(840)
197 * rsadsi(113549) digestAlgorithm(2) 5 }
199 static const u_char id_md5[] = {
200 0x30, 0x20, /* type Sequence, length 0x20 (32) */
201 0x30, 0x0c, /* type Sequence, length 0x09 */
202 0x06, 0x08, /* type OID, length 0x05 */
203 0x2a, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x02, 0x05, /* id-md5 */
204 0x05, 0x00, /* NULL */
205 0x04, 0x10 /* Octet string, length 0x10 (16), followed by md5 hash */
208 static int
209 openssh_RSA_verify(int type, u_char *hash, u_int hashlen,
210 u_char *sigbuf, u_int siglen, RSA *rsa)
212 u_int ret, rsasize, oidlen = 0, hlen = 0;
213 int len;
214 const u_char *oid = NULL;
215 u_char *decrypted = NULL;
217 ret = 0;
218 switch (type) {
219 case NID_sha1:
220 oid = id_sha1;
221 oidlen = sizeof(id_sha1);
222 hlen = 20;
223 break;
224 case NID_md5:
225 oid = id_md5;
226 oidlen = sizeof(id_md5);
227 hlen = 16;
228 break;
229 default:
230 goto done;
232 if (hashlen != hlen) {
233 error("bad hashlen");
234 goto done;
236 rsasize = RSA_size(rsa);
237 if (siglen == 0 || siglen > rsasize) {
238 error("bad siglen");
239 goto done;
241 decrypted = xmalloc(rsasize);
242 if ((len = RSA_public_decrypt(siglen, sigbuf, decrypted, rsa,
243 RSA_PKCS1_PADDING)) < 0) {
244 error("RSA_public_decrypt failed: %s",
245 ERR_error_string(ERR_get_error(), NULL));
246 goto done;
248 if (len < 0 || (u_int)len != hlen + oidlen) {
249 error("bad decrypted len: %d != %d + %d", len, hlen, oidlen);
250 goto done;
252 if (memcmp(decrypted, oid, oidlen) != 0) {
253 error("oid mismatch");
254 goto done;
256 if (memcmp(decrypted + oidlen, hash, hlen) != 0) {
257 error("hash mismatch");
258 goto done;
260 ret = 1;
261 done:
262 if (decrypted)
263 xfree(decrypted);
264 return ret;