xref: /netbsd-src/crypto/external/bsd/openssh/dist/ssh-rsa.c (revision ca32bd8de95214bf91f355ab0f8ff886cb5e8a34)
1 /*	$NetBSD: ssh-rsa.c,v 1.1.1.1 2009/06/07 22:19:24 christos Exp $	*/
2 /* $OpenBSD: ssh-rsa.c,v 1.39 2006/08/03 03:34:42 deraadt Exp $ */
3 /*
4  * Copyright (c) 2000, 2003 Markus Friedl <markus@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/types.h>
20 
21 #include <openssl/evp.h>
22 #include <openssl/err.h>
23 
24 #include <string.h>
25 
26 #include "xmalloc.h"
27 #include "log.h"
28 #include "buffer.h"
29 #include "key.h"
30 #include "compat.h"
31 #include "ssh.h"
32 
33 static int openssh_RSA_verify(int, u_char *, u_int, u_char *, u_int, RSA *);
34 
35 /* RSASSA-PKCS1-v1_5 (PKCS #1 v2.0 signature) with SHA1 */
36 int
37 ssh_rsa_sign(const Key *key, u_char **sigp, u_int *lenp,
38     const u_char *data, u_int datalen)
39 {
40 	const EVP_MD *evp_md;
41 	EVP_MD_CTX md;
42 	u_char digest[EVP_MAX_MD_SIZE], *sig;
43 	u_int slen, dlen, len;
44 	int ok, nid;
45 	Buffer b;
46 
47 	if (key == NULL || key->type != KEY_RSA || key->rsa == NULL) {
48 		error("ssh_rsa_sign: no RSA key");
49 		return -1;
50 	}
51 	nid = (datafellows & SSH_BUG_RSASIGMD5) ? NID_md5 : NID_sha1;
52 	if ((evp_md = EVP_get_digestbynid(nid)) == NULL) {
53 		error("ssh_rsa_sign: EVP_get_digestbynid %d failed", nid);
54 		return -1;
55 	}
56 	EVP_DigestInit(&md, evp_md);
57 	EVP_DigestUpdate(&md, data, datalen);
58 	EVP_DigestFinal(&md, digest, &dlen);
59 
60 	slen = RSA_size(key->rsa);
61 	sig = xmalloc(slen);
62 
63 	ok = RSA_sign(nid, digest, dlen, sig, &len, key->rsa);
64 	memset(digest, 'd', sizeof(digest));
65 
66 	if (ok != 1) {
67 		int ecode = ERR_get_error();
68 
69 		error("ssh_rsa_sign: RSA_sign failed: %s",
70 		    ERR_error_string(ecode, NULL));
71 		xfree(sig);
72 		return -1;
73 	}
74 	if (len < slen) {
75 		u_int diff = slen - len;
76 		debug("slen %u > len %u", slen, len);
77 		memmove(sig + diff, sig, len);
78 		memset(sig, 0, diff);
79 	} else if (len > slen) {
80 		error("ssh_rsa_sign: slen %u slen2 %u", slen, len);
81 		xfree(sig);
82 		return -1;
83 	}
84 	/* encode signature */
85 	buffer_init(&b);
86 	buffer_put_cstring(&b, "ssh-rsa");
87 	buffer_put_string(&b, sig, slen);
88 	len = buffer_len(&b);
89 	if (lenp != NULL)
90 		*lenp = len;
91 	if (sigp != NULL) {
92 		*sigp = xmalloc(len);
93 		memcpy(*sigp, buffer_ptr(&b), len);
94 	}
95 	buffer_free(&b);
96 	memset(sig, 's', slen);
97 	xfree(sig);
98 
99 	return 0;
100 }
101 
102 int
103 ssh_rsa_verify(const Key *key, const u_char *signature, u_int signaturelen,
104     const u_char *data, u_int datalen)
105 {
106 	Buffer b;
107 	const EVP_MD *evp_md;
108 	EVP_MD_CTX md;
109 	char *ktype;
110 	u_char digest[EVP_MAX_MD_SIZE], *sigblob;
111 	u_int len, dlen, modlen;
112 	int rlen, ret, nid;
113 
114 	if (key == NULL || key->type != KEY_RSA || key->rsa == NULL) {
115 		error("ssh_rsa_verify: no RSA key");
116 		return -1;
117 	}
118 	if (BN_num_bits(key->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE) {
119 		error("ssh_rsa_verify: RSA modulus too small: %d < minimum %d bits",
120 		    BN_num_bits(key->rsa->n), SSH_RSA_MINIMUM_MODULUS_SIZE);
121 		return -1;
122 	}
123 	buffer_init(&b);
124 	buffer_append(&b, signature, signaturelen);
125 	ktype = buffer_get_string(&b, NULL);
126 	if (strcmp("ssh-rsa", ktype) != 0) {
127 		error("ssh_rsa_verify: cannot handle type %s", ktype);
128 		buffer_free(&b);
129 		xfree(ktype);
130 		return -1;
131 	}
132 	xfree(ktype);
133 	sigblob = buffer_get_string(&b, &len);
134 	rlen = buffer_len(&b);
135 	buffer_free(&b);
136 	if (rlen != 0) {
137 		error("ssh_rsa_verify: remaining bytes in signature %d", rlen);
138 		xfree(sigblob);
139 		return -1;
140 	}
141 	/* RSA_verify expects a signature of RSA_size */
142 	modlen = RSA_size(key->rsa);
143 	if (len > modlen) {
144 		error("ssh_rsa_verify: len %u > modlen %u", len, modlen);
145 		xfree(sigblob);
146 		return -1;
147 	} else if (len < modlen) {
148 		u_int diff = modlen - len;
149 		debug("ssh_rsa_verify: add padding: modlen %u > len %u",
150 		    modlen, len);
151 		sigblob = xrealloc(sigblob, 1, modlen);
152 		memmove(sigblob + diff, sigblob, len);
153 		memset(sigblob, 0, diff);
154 		len = modlen;
155 	}
156 	nid = (datafellows & SSH_BUG_RSASIGMD5) ? NID_md5 : NID_sha1;
157 	if ((evp_md = EVP_get_digestbynid(nid)) == NULL) {
158 		error("ssh_rsa_verify: EVP_get_digestbynid %d failed", nid);
159 		xfree(sigblob);
160 		return -1;
161 	}
162 	EVP_DigestInit(&md, evp_md);
163 	EVP_DigestUpdate(&md, data, datalen);
164 	EVP_DigestFinal(&md, digest, &dlen);
165 
166 	ret = openssh_RSA_verify(nid, digest, dlen, sigblob, len, key->rsa);
167 	memset(digest, 'd', sizeof(digest));
168 	memset(sigblob, 's', len);
169 	xfree(sigblob);
170 	debug("ssh_rsa_verify: signature %scorrect", (ret==0) ? "in" : "");
171 	return ret;
172 }
173 
174 /*
175  * See:
176  * http://www.rsasecurity.com/rsalabs/pkcs/pkcs-1/
177  * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1.asn
178  */
179 /*
180  * id-sha1 OBJECT IDENTIFIER ::= { iso(1) identified-organization(3)
181  *	oiw(14) secsig(3) algorithms(2) 26 }
182  */
183 static const u_char id_sha1[] = {
184 	0x30, 0x21, /* type Sequence, length 0x21 (33) */
185 	0x30, 0x09, /* type Sequence, length 0x09 */
186 	0x06, 0x05, /* type OID, length 0x05 */
187 	0x2b, 0x0e, 0x03, 0x02, 0x1a, /* id-sha1 OID */
188 	0x05, 0x00, /* NULL */
189 	0x04, 0x14  /* Octet string, length 0x14 (20), followed by sha1 hash */
190 };
191 /*
192  * id-md5 OBJECT IDENTIFIER ::= { iso(1) member-body(2) us(840)
193  *	rsadsi(113549) digestAlgorithm(2) 5 }
194  */
195 static const u_char id_md5[] = {
196 	0x30, 0x20, /* type Sequence, length 0x20 (32) */
197 	0x30, 0x0c, /* type Sequence, length 0x09 */
198 	0x06, 0x08, /* type OID, length 0x05 */
199 	0x2a, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x02, 0x05, /* id-md5 */
200 	0x05, 0x00, /* NULL */
201 	0x04, 0x10  /* Octet string, length 0x10 (16), followed by md5 hash */
202 };
203 
204 static int
205 openssh_RSA_verify(int type, u_char *hash, u_int hashlen,
206     u_char *sigbuf, u_int siglen, RSA *rsa)
207 {
208 	u_int ret, rsasize, oidlen = 0, hlen = 0;
209 	int len;
210 	const u_char *oid = NULL;
211 	u_char *decrypted = NULL;
212 
213 	ret = 0;
214 	switch (type) {
215 	case NID_sha1:
216 		oid = id_sha1;
217 		oidlen = sizeof(id_sha1);
218 		hlen = 20;
219 		break;
220 	case NID_md5:
221 		oid = id_md5;
222 		oidlen = sizeof(id_md5);
223 		hlen = 16;
224 		break;
225 	default:
226 		goto done;
227 	}
228 	if (hashlen != hlen) {
229 		error("bad hashlen");
230 		goto done;
231 	}
232 	rsasize = RSA_size(rsa);
233 	if (siglen == 0 || siglen > rsasize) {
234 		error("bad siglen");
235 		goto done;
236 	}
237 	decrypted = xmalloc(rsasize);
238 	if ((len = RSA_public_decrypt(siglen, sigbuf, decrypted, rsa,
239 	    RSA_PKCS1_PADDING)) < 0) {
240 		error("RSA_public_decrypt failed: %s",
241 		    ERR_error_string(ERR_get_error(), NULL));
242 		goto done;
243 	}
244 	if (len < 0 || (u_int)len != hlen + oidlen) {
245 		error("bad decrypted len: %d != %d + %d", len, hlen, oidlen);
246 		goto done;
247 	}
248 	if (memcmp(decrypted, oid, oidlen) != 0) {
249 		error("oid mismatch");
250 		goto done;
251 	}
252 	if (memcmp(decrypted + oidlen, hash, hlen) != 0) {
253 		error("hash mismatch");
254 		goto done;
255 	}
256 	ret = 1;
257 done:
258 	if (decrypted)
259 		xfree(decrypted);
260 	return ret;
261 }
262