blob: c599ce65c8cc5e52f70fa055dd5572801dea0a8e [file] [log] [blame]
Damien Miller0bc1bd82000-11-13 22:57:25 +11001/*
2 * Copyright (c) 2000 Markus Friedl. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25#include "includes.h"
Ben Lindstrom93576d92002-12-23 02:06:19 +000026RCSID("$OpenBSD: ssh-rsa.c,v 1.27 2002/11/21 23:03:51 deraadt Exp $");
Damien Miller0bc1bd82000-11-13 22:57:25 +110027
28#include <openssl/evp.h>
Damien Miller0bc1bd82000-11-13 22:57:25 +110029#include <openssl/err.h>
30
Ben Lindstrom226cfa02001-01-22 05:34:40 +000031#include "xmalloc.h"
32#include "log.h"
33#include "buffer.h"
34#include "bufaux.h"
Damien Miller0bc1bd82000-11-13 22:57:25 +110035#include "key.h"
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000036#include "ssh-rsa.h"
Ben Lindstrom60a43812001-03-29 00:32:56 +000037#include "compat.h"
Ben Lindstrom03f39322002-04-02 20:43:11 +000038#include "ssh.h"
Damien Miller0bc1bd82000-11-13 22:57:25 +110039
Ben Lindstrom93576d92002-12-23 02:06:19 +000040static int openssh_RSA_verify(int, u_char *, u_int, u_char *, u_int, RSA *);
Ben Lindstrom0deb5d92002-08-20 18:40:03 +000041
Damien Miller0bc1bd82000-11-13 22:57:25 +110042/* RSASSA-PKCS1-v1_5 (PKCS #1 v2.0 signature) with SHA1 */
43int
Ben Lindstrom5c385522002-06-23 21:23:20 +000044ssh_rsa_sign(Key *key, u_char **sigp, u_int *lenp,
Ben Lindstrom90fd8142002-02-26 18:09:42 +000045 u_char *data, u_int datalen)
Damien Miller0bc1bd82000-11-13 22:57:25 +110046{
Ben Lindstrom425fb022001-03-29 00:31:20 +000047 const EVP_MD *evp_md;
Damien Miller0bc1bd82000-11-13 22:57:25 +110048 EVP_MD_CTX md;
Ben Lindstrom2bf759c2002-07-07 22:13:31 +000049 u_char digest[EVP_MAX_MD_SIZE], *sig;
Ben Lindstrom46c16222000-12-22 01:43:59 +000050 u_int slen, dlen, len;
Ben Lindstrom425fb022001-03-29 00:31:20 +000051 int ok, nid;
Damien Miller0bc1bd82000-11-13 22:57:25 +110052 Buffer b;
53
54 if (key == NULL || key->type != KEY_RSA || key->rsa == NULL) {
55 error("ssh_rsa_sign: no RSA key");
56 return -1;
57 }
Ben Lindstrom60a43812001-03-29 00:32:56 +000058 nid = (datafellows & SSH_BUG_RSASIGMD5) ? NID_md5 : NID_sha1;
Ben Lindstrom425fb022001-03-29 00:31:20 +000059 if ((evp_md = EVP_get_digestbynid(nid)) == NULL) {
60 error("ssh_rsa_sign: EVP_get_digestbynid %d failed", nid);
61 return -1;
62 }
Damien Miller0bc1bd82000-11-13 22:57:25 +110063 EVP_DigestInit(&md, evp_md);
64 EVP_DigestUpdate(&md, data, datalen);
Damien Millerc516e922002-02-05 11:53:43 +110065 EVP_DigestFinal(&md, digest, &dlen);
Damien Miller0bc1bd82000-11-13 22:57:25 +110066
Ben Lindstrom425fb022001-03-29 00:31:20 +000067 slen = RSA_size(key->rsa);
68 sig = xmalloc(slen);
69
70 ok = RSA_sign(nid, digest, dlen, sig, &len, key->rsa);
Damien Millerc516e922002-02-05 11:53:43 +110071 memset(digest, 'd', sizeof(digest));
Damien Miller0bc1bd82000-11-13 22:57:25 +110072
73 if (ok != 1) {
74 int ecode = ERR_get_error();
Ben Lindstrom5c385522002-06-23 21:23:20 +000075 error("ssh_rsa_sign: RSA_sign failed: %s",
76 ERR_error_string(ecode, NULL));
Damien Miller0bc1bd82000-11-13 22:57:25 +110077 xfree(sig);
78 return -1;
79 }
80 if (len < slen) {
Ben Lindstrom0e50d842002-08-20 18:39:14 +000081 u_int diff = slen - len;
Ben Lindstrom5c385522002-06-23 21:23:20 +000082 debug("slen %u > len %u", slen, len);
Damien Miller0bc1bd82000-11-13 22:57:25 +110083 memmove(sig + diff, sig, len);
84 memset(sig, 0, diff);
85 } else if (len > slen) {
Ben Lindstrom5c385522002-06-23 21:23:20 +000086 error("ssh_rsa_sign: slen %u slen2 %u", slen, len);
Damien Miller0bc1bd82000-11-13 22:57:25 +110087 xfree(sig);
88 return -1;
89 }
90 /* encode signature */
91 buffer_init(&b);
92 buffer_put_cstring(&b, "ssh-rsa");
93 buffer_put_string(&b, sig, slen);
94 len = buffer_len(&b);
Ben Lindstrom2bf759c2002-07-07 22:13:31 +000095 if (lenp != NULL)
96 *lenp = len;
97 if (sigp != NULL) {
98 *sigp = xmalloc(len);
99 memcpy(*sigp, buffer_ptr(&b), len);
100 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100101 buffer_free(&b);
102 memset(sig, 's', slen);
103 xfree(sig);
104
Damien Miller0bc1bd82000-11-13 22:57:25 +1100105 return 0;
106}
107
108int
Ben Lindstrom5c385522002-06-23 21:23:20 +0000109ssh_rsa_verify(Key *key, u_char *signature, u_int signaturelen,
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000110 u_char *data, u_int datalen)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100111{
112 Buffer b;
Ben Lindstrom425fb022001-03-29 00:31:20 +0000113 const EVP_MD *evp_md;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100114 EVP_MD_CTX md;
115 char *ktype;
Damien Millerc516e922002-02-05 11:53:43 +1100116 u_char digest[EVP_MAX_MD_SIZE], *sigblob;
Ben Lindstromceae9d12002-06-06 20:55:04 +0000117 u_int len, dlen, modlen;
Ben Lindstrom425fb022001-03-29 00:31:20 +0000118 int rlen, ret, nid;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100119
120 if (key == NULL || key->type != KEY_RSA || key->rsa == NULL) {
121 error("ssh_rsa_verify: no RSA key");
122 return -1;
123 }
Ben Lindstrom03f39322002-04-02 20:43:11 +0000124 if (BN_num_bits(key->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE) {
Ben Lindstrom2779d282002-06-11 15:47:42 +0000125 error("ssh_rsa_verify: RSA modulus too small: %d < minimum %d bits",
126 BN_num_bits(key->rsa->n), SSH_RSA_MINIMUM_MODULUS_SIZE);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000127 return -1;
128 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100129 buffer_init(&b);
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000130 buffer_append(&b, signature, signaturelen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100131 ktype = buffer_get_string(&b, NULL);
132 if (strcmp("ssh-rsa", ktype) != 0) {
133 error("ssh_rsa_verify: cannot handle type %s", ktype);
134 buffer_free(&b);
135 xfree(ktype);
136 return -1;
137 }
138 xfree(ktype);
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000139 sigblob = buffer_get_string(&b, &len);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100140 rlen = buffer_len(&b);
141 buffer_free(&b);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000142 if (rlen != 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100143 error("ssh_rsa_verify: remaining bytes in signature %d", rlen);
Damien Miller36e603d2001-11-12 11:03:35 +1100144 xfree(sigblob);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100145 return -1;
146 }
Ben Lindstromceae9d12002-06-06 20:55:04 +0000147 /* RSA_verify expects a signature of RSA_size */
148 modlen = RSA_size(key->rsa);
149 if (len > modlen) {
Ben Lindstrom5c385522002-06-23 21:23:20 +0000150 error("ssh_rsa_verify: len %u > modlen %u", len, modlen);
Ben Lindstromceae9d12002-06-06 20:55:04 +0000151 xfree(sigblob);
152 return -1;
153 } else if (len < modlen) {
Ben Lindstrom0e50d842002-08-20 18:39:14 +0000154 u_int diff = modlen - len;
Ben Lindstrom5c385522002-06-23 21:23:20 +0000155 debug("ssh_rsa_verify: add padding: modlen %u > len %u",
Ben Lindstromceae9d12002-06-06 20:55:04 +0000156 modlen, len);
157 sigblob = xrealloc(sigblob, modlen);
158 memmove(sigblob + diff, sigblob, len);
159 memset(sigblob, 0, diff);
160 len = modlen;
161 }
Ben Lindstrom60a43812001-03-29 00:32:56 +0000162 nid = (datafellows & SSH_BUG_RSASIGMD5) ? NID_md5 : NID_sha1;
Ben Lindstrom425fb022001-03-29 00:31:20 +0000163 if ((evp_md = EVP_get_digestbynid(nid)) == NULL) {
Ben Lindstrom425fb022001-03-29 00:31:20 +0000164 error("ssh_rsa_verify: EVP_get_digestbynid %d failed", nid);
Damien Miller36e603d2001-11-12 11:03:35 +1100165 xfree(sigblob);
Ben Lindstrom425fb022001-03-29 00:31:20 +0000166 return -1;
167 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100168 EVP_DigestInit(&md, evp_md);
169 EVP_DigestUpdate(&md, data, datalen);
Damien Millerc516e922002-02-05 11:53:43 +1100170 EVP_DigestFinal(&md, digest, &dlen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100171
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000172 ret = openssh_RSA_verify(nid, digest, dlen, sigblob, len, key->rsa);
Damien Millerc516e922002-02-05 11:53:43 +1100173 memset(digest, 'd', sizeof(digest));
Damien Miller0bc1bd82000-11-13 22:57:25 +1100174 memset(sigblob, 's', len);
175 xfree(sigblob);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100176 debug("ssh_rsa_verify: signature %scorrect", (ret==0) ? "in" : "");
177 return ret;
178}
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000179
180/*
181 * See:
182 * http://www.rsasecurity.com/rsalabs/pkcs/pkcs-1/
183 * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1.asn
184 */
185/*
186 * id-sha1 OBJECT IDENTIFIER ::= { iso(1) identified-organization(3)
187 * oiw(14) secsig(3) algorithms(2) 26 }
188 */
189static const u_char id_sha1[] = {
190 0x30, 0x21, /* type Sequence, length 0x21 (33) */
191 0x30, 0x09, /* type Sequence, length 0x09 */
192 0x06, 0x05, /* type OID, length 0x05 */
193 0x2b, 0x0e, 0x03, 0x02, 0x1a, /* id-sha1 OID */
194 0x05, 0x00, /* NULL */
195 0x04, 0x14 /* Octet string, length 0x14 (20), followed by sha1 hash */
196};
197/*
198 * id-md5 OBJECT IDENTIFIER ::= { iso(1) member-body(2) us(840)
199 * rsadsi(113549) digestAlgorithm(2) 5 }
200 */
201static const u_char id_md5[] = {
202 0x30, 0x20, /* type Sequence, length 0x20 (32) */
203 0x30, 0x0c, /* type Sequence, length 0x09 */
204 0x06, 0x08, /* type OID, length 0x05 */
205 0x2a, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x02, 0x05, /* id-md5 */
206 0x05, 0x00, /* NULL */
207 0x04, 0x10 /* Octet string, length 0x10 (16), followed by md5 hash */
208};
209
210static int
211openssh_RSA_verify(int type, u_char *hash, u_int hashlen,
212 u_char *sigbuf, u_int siglen, RSA *rsa)
213{
Damien Millerf7c23912002-09-04 16:39:48 +1000214 u_int ret, rsasize, oidlen = 0, hlen = 0;
215 int len;
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000216 const u_char *oid = NULL;
217 u_char *decrypted = NULL;
218
219 ret = 0;
220 switch (type) {
221 case NID_sha1:
222 oid = id_sha1;
223 oidlen = sizeof(id_sha1);
224 hlen = 20;
225 break;
226 case NID_md5:
227 oid = id_md5;
228 oidlen = sizeof(id_md5);
229 hlen = 16;
230 break;
231 default:
232 goto done;
233 break;
234 }
235 if (hashlen != hlen) {
236 error("bad hashlen");
237 goto done;
238 }
239 rsasize = RSA_size(rsa);
240 if (siglen == 0 || siglen > rsasize) {
241 error("bad siglen");
242 goto done;
243 }
244 decrypted = xmalloc(rsasize);
245 if ((len = RSA_public_decrypt(siglen, sigbuf, decrypted, rsa,
246 RSA_PKCS1_PADDING)) < 0) {
247 error("RSA_public_decrypt failed: %s",
248 ERR_error_string(ERR_get_error(), NULL));
249 goto done;
250 }
251 if (len != hlen + oidlen) {
252 error("bad decrypted len: %d != %d + %d", len, hlen, oidlen);
253 goto done;
254 }
255 if (memcmp(decrypted, oid, oidlen) != 0) {
256 error("oid mismatch");
257 goto done;
258 }
259 if (memcmp(decrypted + oidlen, hash, hlen) != 0) {
260 error("hash mismatch");
261 goto done;
262 }
263 ret = 1;
264done:
265 if (decrypted)
266 xfree(decrypted);
267 return ret;
268}