blob: 782c855730081e0a24950adcdb416a6525866bea [file] [log] [blame]
Damien Miller3e192952013-12-29 17:47:50 +11001/* $OpenBSD: ssh-rsa.c,v 1.47 2013/12/27 22:30:17 djm 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"
Damien Miller8a0268f2010-07-16 13:57:51 +100033#include "misc.h"
Ben Lindstrom03f39322002-04-02 20:43:11 +000034#include "ssh.h"
Damien Miller0bc1bd82000-11-13 22:57:25 +110035
Ben Lindstrom93576d92002-12-23 02:06:19 +000036static int openssh_RSA_verify(int, u_char *, u_int, u_char *, u_int, RSA *);
Ben Lindstrom0deb5d92002-08-20 18:40:03 +000037
Damien Miller0bc1bd82000-11-13 22:57:25 +110038/* RSASSA-PKCS1-v1_5 (PKCS #1 v2.0 signature) with SHA1 */
39int
Damien Millerf58b58c2003-11-17 21:18:23 +110040ssh_rsa_sign(const Key *key, u_char **sigp, u_int *lenp,
41 const u_char *data, u_int datalen)
Damien Miller0bc1bd82000-11-13 22:57:25 +110042{
Ben Lindstrom425fb022001-03-29 00:31:20 +000043 const EVP_MD *evp_md;
Damien Miller0bc1bd82000-11-13 22:57:25 +110044 EVP_MD_CTX md;
Ben Lindstrom2bf759c2002-07-07 22:13:31 +000045 u_char digest[EVP_MAX_MD_SIZE], *sig;
Ben Lindstrom46c16222000-12-22 01:43:59 +000046 u_int slen, dlen, len;
Ben Lindstrom425fb022001-03-29 00:31:20 +000047 int ok, nid;
Damien Miller0bc1bd82000-11-13 22:57:25 +110048 Buffer b;
49
Damien Miller3e192952013-12-29 17:47:50 +110050 if (key == NULL || key_type_plain(key->type) != KEY_RSA ||
51 key->rsa == NULL) {
52 error("%s: no RSA key", __func__);
Damien Miller0bc1bd82000-11-13 22:57:25 +110053 return -1;
54 }
Damien Miller3e192952013-12-29 17:47:50 +110055
Ben Lindstrom60a43812001-03-29 00:32:56 +000056 nid = (datafellows & SSH_BUG_RSASIGMD5) ? NID_md5 : NID_sha1;
Ben Lindstrom425fb022001-03-29 00:31:20 +000057 if ((evp_md = EVP_get_digestbynid(nid)) == NULL) {
Damien Miller3e192952013-12-29 17:47:50 +110058 error("%s: EVP_get_digestbynid %d failed", __func__, nid);
Ben Lindstrom425fb022001-03-29 00:31:20 +000059 return -1;
60 }
Damien Miller0bc1bd82000-11-13 22:57:25 +110061 EVP_DigestInit(&md, evp_md);
62 EVP_DigestUpdate(&md, data, datalen);
Damien Millerc516e922002-02-05 11:53:43 +110063 EVP_DigestFinal(&md, digest, &dlen);
Damien Miller0bc1bd82000-11-13 22:57:25 +110064
Ben Lindstrom425fb022001-03-29 00:31:20 +000065 slen = RSA_size(key->rsa);
66 sig = xmalloc(slen);
67
68 ok = RSA_sign(nid, digest, dlen, sig, &len, key->rsa);
Damien Millerc516e922002-02-05 11:53:43 +110069 memset(digest, 'd', sizeof(digest));
Damien Miller0bc1bd82000-11-13 22:57:25 +110070
71 if (ok != 1) {
72 int ecode = ERR_get_error();
Damien Miller90967402006-03-26 14:07:26 +110073
Damien Miller3e192952013-12-29 17:47:50 +110074 error("%s: RSA_sign failed: %s", __func__,
Ben Lindstrom5c385522002-06-23 21:23:20 +000075 ERR_error_string(ecode, NULL));
Darren Tuckera627d422013-06-02 07:31:17 +100076 free(sig);
Damien Miller0bc1bd82000-11-13 22:57:25 +110077 return -1;
78 }
79 if (len < slen) {
Ben Lindstrom0e50d842002-08-20 18:39:14 +000080 u_int diff = slen - len;
Ben Lindstrom5c385522002-06-23 21:23:20 +000081 debug("slen %u > len %u", slen, len);
Damien Miller0bc1bd82000-11-13 22:57:25 +110082 memmove(sig + diff, sig, len);
83 memset(sig, 0, diff);
84 } else if (len > slen) {
Damien Miller3e192952013-12-29 17:47:50 +110085 error("%s: slen %u slen2 %u", __func__, slen, len);
Darren Tuckera627d422013-06-02 07:31:17 +100086 free(sig);
Damien Miller0bc1bd82000-11-13 22:57:25 +110087 return -1;
88 }
89 /* encode signature */
90 buffer_init(&b);
91 buffer_put_cstring(&b, "ssh-rsa");
92 buffer_put_string(&b, sig, slen);
93 len = buffer_len(&b);
Ben Lindstrom2bf759c2002-07-07 22:13:31 +000094 if (lenp != NULL)
95 *lenp = len;
96 if (sigp != NULL) {
97 *sigp = xmalloc(len);
98 memcpy(*sigp, buffer_ptr(&b), len);
99 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100100 buffer_free(&b);
101 memset(sig, 's', slen);
Darren Tuckera627d422013-06-02 07:31:17 +1000102 free(sig);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100103
Damien Miller0bc1bd82000-11-13 22:57:25 +1100104 return 0;
105}
106
107int
Damien Millerf58b58c2003-11-17 21:18:23 +1100108ssh_rsa_verify(const Key *key, const u_char *signature, u_int signaturelen,
109 const u_char *data, u_int datalen)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100110{
111 Buffer b;
Ben Lindstrom425fb022001-03-29 00:31:20 +0000112 const EVP_MD *evp_md;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100113 EVP_MD_CTX md;
114 char *ktype;
Damien Millerc516e922002-02-05 11:53:43 +1100115 u_char digest[EVP_MAX_MD_SIZE], *sigblob;
Ben Lindstromceae9d12002-06-06 20:55:04 +0000116 u_int len, dlen, modlen;
Ben Lindstrom425fb022001-03-29 00:31:20 +0000117 int rlen, ret, nid;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100118
Damien Miller3e192952013-12-29 17:47:50 +1100119 if (key == NULL || key_type_plain(key->type) != KEY_RSA ||
120 key->rsa == NULL) {
121 error("%s: no RSA key", __func__);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100122 return -1;
123 }
Damien Miller3e192952013-12-29 17:47:50 +1100124
Ben Lindstrom03f39322002-04-02 20:43:11 +0000125 if (BN_num_bits(key->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE) {
Damien Miller3e192952013-12-29 17:47:50 +1100126 error("%s: RSA modulus too small: %d < minimum %d bits",
127 __func__, BN_num_bits(key->rsa->n),
128 SSH_RSA_MINIMUM_MODULUS_SIZE);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000129 return -1;
130 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100131 buffer_init(&b);
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000132 buffer_append(&b, signature, signaturelen);
Damien Millerda108ec2010-08-31 22:36:39 +1000133 ktype = buffer_get_cstring(&b, NULL);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100134 if (strcmp("ssh-rsa", ktype) != 0) {
Damien Miller3e192952013-12-29 17:47:50 +1100135 error("%s: cannot handle type %s", __func__, ktype);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100136 buffer_free(&b);
Darren Tuckera627d422013-06-02 07:31:17 +1000137 free(ktype);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100138 return -1;
139 }
Darren Tuckera627d422013-06-02 07:31:17 +1000140 free(ktype);
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000141 sigblob = buffer_get_string(&b, &len);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100142 rlen = buffer_len(&b);
143 buffer_free(&b);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000144 if (rlen != 0) {
Damien Miller3e192952013-12-29 17:47:50 +1100145 error("%s: remaining bytes in signature %d", __func__, rlen);
Darren Tuckera627d422013-06-02 07:31:17 +1000146 free(sigblob);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100147 return -1;
148 }
Ben Lindstromceae9d12002-06-06 20:55:04 +0000149 /* RSA_verify expects a signature of RSA_size */
150 modlen = RSA_size(key->rsa);
151 if (len > modlen) {
Damien Miller3e192952013-12-29 17:47:50 +1100152 error("%s: len %u > modlen %u", __func__, len, modlen);
Darren Tuckera627d422013-06-02 07:31:17 +1000153 free(sigblob);
Ben Lindstromceae9d12002-06-06 20:55:04 +0000154 return -1;
155 } else if (len < modlen) {
Ben Lindstrom0e50d842002-08-20 18:39:14 +0000156 u_int diff = modlen - len;
Damien Miller3e192952013-12-29 17:47:50 +1100157 debug("%s: add padding: modlen %u > len %u", __func__,
Ben Lindstromceae9d12002-06-06 20:55:04 +0000158 modlen, len);
Damien Miller36812092006-03-26 14:22:47 +1100159 sigblob = xrealloc(sigblob, 1, modlen);
Ben Lindstromceae9d12002-06-06 20:55:04 +0000160 memmove(sigblob + diff, sigblob, len);
161 memset(sigblob, 0, diff);
162 len = modlen;
163 }
Ben Lindstrom60a43812001-03-29 00:32:56 +0000164 nid = (datafellows & SSH_BUG_RSASIGMD5) ? NID_md5 : NID_sha1;
Ben Lindstrom425fb022001-03-29 00:31:20 +0000165 if ((evp_md = EVP_get_digestbynid(nid)) == NULL) {
Damien Miller3e192952013-12-29 17:47:50 +1100166 error("%s: EVP_get_digestbynid %d failed", __func__, nid);
Darren Tuckera627d422013-06-02 07:31:17 +1000167 free(sigblob);
Ben Lindstrom425fb022001-03-29 00:31:20 +0000168 return -1;
169 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100170 EVP_DigestInit(&md, evp_md);
171 EVP_DigestUpdate(&md, data, datalen);
Damien Millerc516e922002-02-05 11:53:43 +1100172 EVP_DigestFinal(&md, digest, &dlen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100173
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000174 ret = openssh_RSA_verify(nid, digest, dlen, sigblob, len, key->rsa);
Damien Millerc516e922002-02-05 11:53:43 +1100175 memset(digest, 'd', sizeof(digest));
Damien Miller0bc1bd82000-11-13 22:57:25 +1100176 memset(sigblob, 's', len);
Darren Tuckera627d422013-06-02 07:31:17 +1000177 free(sigblob);
Damien Miller3e192952013-12-29 17:47:50 +1100178 debug("%s: signature %scorrect", __func__, (ret == 0) ? "in" : "");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100179 return ret;
180}
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000181
182/*
183 * See:
184 * http://www.rsasecurity.com/rsalabs/pkcs/pkcs-1/
185 * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1.asn
186 */
187/*
188 * id-sha1 OBJECT IDENTIFIER ::= { iso(1) identified-organization(3)
189 * oiw(14) secsig(3) algorithms(2) 26 }
190 */
191static const u_char id_sha1[] = {
192 0x30, 0x21, /* type Sequence, length 0x21 (33) */
193 0x30, 0x09, /* type Sequence, length 0x09 */
194 0x06, 0x05, /* type OID, length 0x05 */
195 0x2b, 0x0e, 0x03, 0x02, 0x1a, /* id-sha1 OID */
196 0x05, 0x00, /* NULL */
197 0x04, 0x14 /* Octet string, length 0x14 (20), followed by sha1 hash */
198};
199/*
200 * id-md5 OBJECT IDENTIFIER ::= { iso(1) member-body(2) us(840)
201 * rsadsi(113549) digestAlgorithm(2) 5 }
202 */
203static const u_char id_md5[] = {
204 0x30, 0x20, /* type Sequence, length 0x20 (32) */
205 0x30, 0x0c, /* type Sequence, length 0x09 */
206 0x06, 0x08, /* type OID, length 0x05 */
207 0x2a, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x02, 0x05, /* id-md5 */
208 0x05, 0x00, /* NULL */
209 0x04, 0x10 /* Octet string, length 0x10 (16), followed by md5 hash */
210};
211
212static int
213openssh_RSA_verify(int type, u_char *hash, u_int hashlen,
214 u_char *sigbuf, u_int siglen, RSA *rsa)
215{
Damien Millerf7c23912002-09-04 16:39:48 +1000216 u_int ret, rsasize, oidlen = 0, hlen = 0;
Damien Miller4e8285e2010-08-03 16:04:03 +1000217 int len, oidmatch, hashmatch;
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000218 const u_char *oid = NULL;
219 u_char *decrypted = NULL;
220
221 ret = 0;
222 switch (type) {
223 case NID_sha1:
224 oid = id_sha1;
225 oidlen = sizeof(id_sha1);
226 hlen = 20;
227 break;
228 case NID_md5:
229 oid = id_md5;
230 oidlen = sizeof(id_md5);
231 hlen = 16;
232 break;
233 default:
234 goto done;
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000235 }
236 if (hashlen != hlen) {
237 error("bad hashlen");
238 goto done;
239 }
240 rsasize = RSA_size(rsa);
241 if (siglen == 0 || siglen > rsasize) {
242 error("bad siglen");
243 goto done;
244 }
245 decrypted = xmalloc(rsasize);
246 if ((len = RSA_public_decrypt(siglen, sigbuf, decrypted, rsa,
247 RSA_PKCS1_PADDING)) < 0) {
248 error("RSA_public_decrypt failed: %s",
249 ERR_error_string(ERR_get_error(), NULL));
250 goto done;
251 }
Damien Millereccb9de2005-06-17 12:59:34 +1000252 if (len < 0 || (u_int)len != hlen + oidlen) {
Darren Tuckera251f802003-06-22 20:45:15 +1000253 error("bad decrypted len: %d != %d + %d", len, hlen, oidlen);
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000254 goto done;
255 }
Damien Miller4e8285e2010-08-03 16:04:03 +1000256 oidmatch = timingsafe_bcmp(decrypted, oid, oidlen) == 0;
257 hashmatch = timingsafe_bcmp(decrypted + oidlen, hash, hlen) == 0;
258 if (!oidmatch) {
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000259 error("oid mismatch");
260 goto done;
261 }
Damien Miller4e8285e2010-08-03 16:04:03 +1000262 if (!hashmatch) {
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000263 error("hash mismatch");
264 goto done;
265 }
266 ret = 1;
267done:
Darren Tuckera627d422013-06-02 07:31:17 +1000268 free(decrypted);
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000269 return ret;
270}