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