blob: 0e16ff85f1d6f024273e22db89e04dbac6e52f1c [file] [log] [blame]
Damien Millerd7834352006-08-05 12:39:39 +10001/* $OpenBSD: ssh-rsa.c,v 1.39 2006/08/03 03:34:42 deraadt Exp $ */
Damien Miller0bc1bd82000-11-13 22:57:25 +11002/*
Darren Tuckera251f802003-06-22 20:45:15 +10003 * Copyright (c) 2000, 2003 Markus Friedl <markus@openbsd.org>
Damien Miller0bc1bd82000-11-13 22:57:25 +11004 *
Darren Tuckera251f802003-06-22 20:45:15 +10005 * 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.
Damien Miller0bc1bd82000-11-13 22:57:25 +11008 *
Darren Tuckera251f802003-06-22 20:45:15 +10009 * 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.
Damien Miller0bc1bd82000-11-13 22:57:25 +110016 */
Damien Millerd7834352006-08-05 12:39:39 +100017
Damien Miller0bc1bd82000-11-13 22:57:25 +110018#include "includes.h"
Damien Miller0bc1bd82000-11-13 22:57:25 +110019
Damien Millerd7834352006-08-05 12:39:39 +100020#include <sys/types.h>
21
Damien Miller0bc1bd82000-11-13 22:57:25 +110022#include <openssl/evp.h>
Damien Miller0bc1bd82000-11-13 22:57:25 +110023#include <openssl/err.h>
24
Damien Millerded319c2006-09-01 15:38:36 +100025#include <stdarg.h>
Damien Millere3476ed2006-07-24 14:13:33 +100026#include <string.h>
27
Ben Lindstrom226cfa02001-01-22 05:34:40 +000028#include "xmalloc.h"
29#include "log.h"
30#include "buffer.h"
Damien Miller0bc1bd82000-11-13 22:57:25 +110031#include "key.h"
Ben Lindstrom60a43812001-03-29 00:32:56 +000032#include "compat.h"
Ben Lindstrom03f39322002-04-02 20:43:11 +000033#include "ssh.h"
Damien Miller0bc1bd82000-11-13 22:57:25 +110034
Ben Lindstrom93576d92002-12-23 02:06:19 +000035static int openssh_RSA_verify(int, u_char *, u_int, u_char *, u_int, RSA *);
Ben Lindstrom0deb5d92002-08-20 18:40:03 +000036
Damien Miller0bc1bd82000-11-13 22:57:25 +110037/* RSASSA-PKCS1-v1_5 (PKCS #1 v2.0 signature) with SHA1 */
38int
Damien Millerf58b58c2003-11-17 21:18:23 +110039ssh_rsa_sign(const Key *key, u_char **sigp, u_int *lenp,
40 const u_char *data, u_int datalen)
Damien Miller0bc1bd82000-11-13 22:57:25 +110041{
Ben Lindstrom425fb022001-03-29 00:31:20 +000042 const EVP_MD *evp_md;
Damien Miller0bc1bd82000-11-13 22:57:25 +110043 EVP_MD_CTX md;
Ben Lindstrom2bf759c2002-07-07 22:13:31 +000044 u_char digest[EVP_MAX_MD_SIZE], *sig;
Ben Lindstrom46c16222000-12-22 01:43:59 +000045 u_int slen, dlen, len;
Ben Lindstrom425fb022001-03-29 00:31:20 +000046 int ok, nid;
Damien Miller0bc1bd82000-11-13 22:57:25 +110047 Buffer b;
48
49 if (key == NULL || key->type != KEY_RSA || key->rsa == NULL) {
50 error("ssh_rsa_sign: no RSA key");
51 return -1;
52 }
Ben Lindstrom60a43812001-03-29 00:32:56 +000053 nid = (datafellows & SSH_BUG_RSASIGMD5) ? NID_md5 : NID_sha1;
Ben Lindstrom425fb022001-03-29 00:31:20 +000054 if ((evp_md = EVP_get_digestbynid(nid)) == NULL) {
55 error("ssh_rsa_sign: EVP_get_digestbynid %d failed", nid);
56 return -1;
57 }
Damien Miller0bc1bd82000-11-13 22:57:25 +110058 EVP_DigestInit(&md, evp_md);
59 EVP_DigestUpdate(&md, data, datalen);
Damien Millerc516e922002-02-05 11:53:43 +110060 EVP_DigestFinal(&md, digest, &dlen);
Damien Miller0bc1bd82000-11-13 22:57:25 +110061
Ben Lindstrom425fb022001-03-29 00:31:20 +000062 slen = RSA_size(key->rsa);
63 sig = xmalloc(slen);
64
65 ok = RSA_sign(nid, digest, dlen, sig, &len, key->rsa);
Damien Millerc516e922002-02-05 11:53:43 +110066 memset(digest, 'd', sizeof(digest));
Damien Miller0bc1bd82000-11-13 22:57:25 +110067
68 if (ok != 1) {
69 int ecode = ERR_get_error();
Damien Miller90967402006-03-26 14:07:26 +110070
Ben Lindstrom5c385522002-06-23 21:23:20 +000071 error("ssh_rsa_sign: RSA_sign failed: %s",
72 ERR_error_string(ecode, NULL));
Damien Miller0bc1bd82000-11-13 22:57:25 +110073 xfree(sig);
74 return -1;
75 }
76 if (len < slen) {
Ben Lindstrom0e50d842002-08-20 18:39:14 +000077 u_int diff = slen - len;
Ben Lindstrom5c385522002-06-23 21:23:20 +000078 debug("slen %u > len %u", slen, len);
Damien Miller0bc1bd82000-11-13 22:57:25 +110079 memmove(sig + diff, sig, len);
80 memset(sig, 0, diff);
81 } else if (len > slen) {
Ben Lindstrom5c385522002-06-23 21:23:20 +000082 error("ssh_rsa_sign: slen %u slen2 %u", slen, len);
Damien Miller0bc1bd82000-11-13 22:57:25 +110083 xfree(sig);
84 return -1;
85 }
86 /* encode signature */
87 buffer_init(&b);
88 buffer_put_cstring(&b, "ssh-rsa");
89 buffer_put_string(&b, sig, slen);
90 len = buffer_len(&b);
Ben Lindstrom2bf759c2002-07-07 22:13:31 +000091 if (lenp != NULL)
92 *lenp = len;
93 if (sigp != NULL) {
94 *sigp = xmalloc(len);
95 memcpy(*sigp, buffer_ptr(&b), len);
96 }
Damien Miller0bc1bd82000-11-13 22:57:25 +110097 buffer_free(&b);
98 memset(sig, 's', slen);
99 xfree(sig);
100
Damien Miller0bc1bd82000-11-13 22:57:25 +1100101 return 0;
102}
103
104int
Damien Millerf58b58c2003-11-17 21:18:23 +1100105ssh_rsa_verify(const Key *key, const u_char *signature, u_int signaturelen,
106 const u_char *data, u_int datalen)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100107{
108 Buffer b;
Ben Lindstrom425fb022001-03-29 00:31:20 +0000109 const EVP_MD *evp_md;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100110 EVP_MD_CTX md;
111 char *ktype;
Damien Millerc516e922002-02-05 11:53:43 +1100112 u_char digest[EVP_MAX_MD_SIZE], *sigblob;
Ben Lindstromceae9d12002-06-06 20:55:04 +0000113 u_int len, dlen, modlen;
Ben Lindstrom425fb022001-03-29 00:31:20 +0000114 int rlen, ret, nid;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100115
116 if (key == NULL || key->type != KEY_RSA || key->rsa == NULL) {
117 error("ssh_rsa_verify: no RSA key");
118 return -1;
119 }
Ben Lindstrom03f39322002-04-02 20:43:11 +0000120 if (BN_num_bits(key->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE) {
Ben Lindstrom2779d282002-06-11 15:47:42 +0000121 error("ssh_rsa_verify: RSA modulus too small: %d < minimum %d bits",
122 BN_num_bits(key->rsa->n), SSH_RSA_MINIMUM_MODULUS_SIZE);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000123 return -1;
124 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100125 buffer_init(&b);
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000126 buffer_append(&b, signature, signaturelen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100127 ktype = buffer_get_string(&b, NULL);
128 if (strcmp("ssh-rsa", ktype) != 0) {
129 error("ssh_rsa_verify: cannot handle type %s", ktype);
130 buffer_free(&b);
131 xfree(ktype);
132 return -1;
133 }
134 xfree(ktype);
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000135 sigblob = buffer_get_string(&b, &len);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100136 rlen = buffer_len(&b);
137 buffer_free(&b);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000138 if (rlen != 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100139 error("ssh_rsa_verify: remaining bytes in signature %d", rlen);
Damien Miller36e603d2001-11-12 11:03:35 +1100140 xfree(sigblob);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100141 return -1;
142 }
Ben Lindstromceae9d12002-06-06 20:55:04 +0000143 /* RSA_verify expects a signature of RSA_size */
144 modlen = RSA_size(key->rsa);
145 if (len > modlen) {
Ben Lindstrom5c385522002-06-23 21:23:20 +0000146 error("ssh_rsa_verify: len %u > modlen %u", len, modlen);
Ben Lindstromceae9d12002-06-06 20:55:04 +0000147 xfree(sigblob);
148 return -1;
149 } else if (len < modlen) {
Ben Lindstrom0e50d842002-08-20 18:39:14 +0000150 u_int diff = modlen - len;
Ben Lindstrom5c385522002-06-23 21:23:20 +0000151 debug("ssh_rsa_verify: add padding: modlen %u > len %u",
Ben Lindstromceae9d12002-06-06 20:55:04 +0000152 modlen, len);
Damien Miller36812092006-03-26 14:22:47 +1100153 sigblob = xrealloc(sigblob, 1, modlen);
Ben Lindstromceae9d12002-06-06 20:55:04 +0000154 memmove(sigblob + diff, sigblob, len);
155 memset(sigblob, 0, diff);
156 len = modlen;
157 }
Ben Lindstrom60a43812001-03-29 00:32:56 +0000158 nid = (datafellows & SSH_BUG_RSASIGMD5) ? NID_md5 : NID_sha1;
Ben Lindstrom425fb022001-03-29 00:31:20 +0000159 if ((evp_md = EVP_get_digestbynid(nid)) == NULL) {
Ben Lindstrom425fb022001-03-29 00:31:20 +0000160 error("ssh_rsa_verify: EVP_get_digestbynid %d failed", nid);
Damien Miller36e603d2001-11-12 11:03:35 +1100161 xfree(sigblob);
Ben Lindstrom425fb022001-03-29 00:31:20 +0000162 return -1;
163 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100164 EVP_DigestInit(&md, evp_md);
165 EVP_DigestUpdate(&md, data, datalen);
Damien Millerc516e922002-02-05 11:53:43 +1100166 EVP_DigestFinal(&md, digest, &dlen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100167
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000168 ret = openssh_RSA_verify(nid, digest, dlen, sigblob, len, key->rsa);
Damien Millerc516e922002-02-05 11:53:43 +1100169 memset(digest, 'd', sizeof(digest));
Damien Miller0bc1bd82000-11-13 22:57:25 +1100170 memset(sigblob, 's', len);
171 xfree(sigblob);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100172 debug("ssh_rsa_verify: signature %scorrect", (ret==0) ? "in" : "");
173 return ret;
174}
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000175
176/*
177 * See:
178 * http://www.rsasecurity.com/rsalabs/pkcs/pkcs-1/
179 * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1.asn
180 */
181/*
182 * id-sha1 OBJECT IDENTIFIER ::= { iso(1) identified-organization(3)
183 * oiw(14) secsig(3) algorithms(2) 26 }
184 */
185static const u_char id_sha1[] = {
186 0x30, 0x21, /* type Sequence, length 0x21 (33) */
187 0x30, 0x09, /* type Sequence, length 0x09 */
188 0x06, 0x05, /* type OID, length 0x05 */
189 0x2b, 0x0e, 0x03, 0x02, 0x1a, /* id-sha1 OID */
190 0x05, 0x00, /* NULL */
191 0x04, 0x14 /* Octet string, length 0x14 (20), followed by sha1 hash */
192};
193/*
194 * id-md5 OBJECT IDENTIFIER ::= { iso(1) member-body(2) us(840)
195 * rsadsi(113549) digestAlgorithm(2) 5 }
196 */
197static const u_char id_md5[] = {
198 0x30, 0x20, /* type Sequence, length 0x20 (32) */
199 0x30, 0x0c, /* type Sequence, length 0x09 */
200 0x06, 0x08, /* type OID, length 0x05 */
201 0x2a, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x02, 0x05, /* id-md5 */
202 0x05, 0x00, /* NULL */
203 0x04, 0x10 /* Octet string, length 0x10 (16), followed by md5 hash */
204};
205
206static int
207openssh_RSA_verify(int type, u_char *hash, u_int hashlen,
208 u_char *sigbuf, u_int siglen, RSA *rsa)
209{
Damien Millerf7c23912002-09-04 16:39:48 +1000210 u_int ret, rsasize, oidlen = 0, hlen = 0;
211 int len;
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000212 const u_char *oid = NULL;
213 u_char *decrypted = NULL;
214
215 ret = 0;
216 switch (type) {
217 case NID_sha1:
218 oid = id_sha1;
219 oidlen = sizeof(id_sha1);
220 hlen = 20;
221 break;
222 case NID_md5:
223 oid = id_md5;
224 oidlen = sizeof(id_md5);
225 hlen = 16;
226 break;
227 default:
228 goto done;
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000229 }
230 if (hashlen != hlen) {
231 error("bad hashlen");
232 goto done;
233 }
234 rsasize = RSA_size(rsa);
235 if (siglen == 0 || siglen > rsasize) {
236 error("bad siglen");
237 goto done;
238 }
239 decrypted = xmalloc(rsasize);
240 if ((len = RSA_public_decrypt(siglen, sigbuf, decrypted, rsa,
241 RSA_PKCS1_PADDING)) < 0) {
242 error("RSA_public_decrypt failed: %s",
243 ERR_error_string(ERR_get_error(), NULL));
244 goto done;
245 }
Damien Millereccb9de2005-06-17 12:59:34 +1000246 if (len < 0 || (u_int)len != hlen + oidlen) {
Darren Tuckera251f802003-06-22 20:45:15 +1000247 error("bad decrypted len: %d != %d + %d", len, hlen, oidlen);
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000248 goto done;
249 }
250 if (memcmp(decrypted, oid, oidlen) != 0) {
251 error("oid mismatch");
252 goto done;
253 }
254 if (memcmp(decrypted + oidlen, hash, hlen) != 0) {
255 error("hash mismatch");
256 goto done;
257 }
258 ret = 1;
259done:
260 if (decrypted)
261 xfree(decrypted);
262 return ret;
263}