blob: a2112d033e5874873d380914ad2d413a0a2c41e6 [file] [log] [blame]
Damien Millerb3051d02014-01-10 10:58:53 +11001/* $OpenBSD: ssh-rsa.c,v 1.50 2014/01/09 23:20:00 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 Millerb3051d02014-01-10 10:58:53 +110035#include "digest.h"
Damien Miller0bc1bd82000-11-13 22:57:25 +110036
Ben Lindstrom93576d92002-12-23 02:06:19 +000037static int openssh_RSA_verify(int, u_char *, u_int, u_char *, u_int, RSA *);
Ben Lindstrom0deb5d92002-08-20 18:40:03 +000038
Damien Miller0bc1bd82000-11-13 22:57:25 +110039/* RSASSA-PKCS1-v1_5 (PKCS #1 v2.0 signature) with SHA1 */
40int
Damien Millerf58b58c2003-11-17 21:18:23 +110041ssh_rsa_sign(const Key *key, u_char **sigp, u_int *lenp,
42 const u_char *data, u_int datalen)
Damien Miller0bc1bd82000-11-13 22:57:25 +110043{
Damien Millerb3051d02014-01-10 10:58:53 +110044 int hash_alg;
45 u_char digest[SSH_DIGEST_MAX_LENGTH], *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
Damien Millerb3051d02014-01-10 10:58:53 +110056 /* hash the data */
57 hash_alg = SSH_DIGEST_SHA1;
Damien Miller324541e2013-12-31 12:25:40 +110058 nid = NID_sha1;
Damien Millerb3051d02014-01-10 10:58:53 +110059 if ((dlen = ssh_digest_bytes(hash_alg)) == 0) {
60 error("%s: bad hash algorithm %d", __func__, hash_alg);
Ben Lindstrom425fb022001-03-29 00:31:20 +000061 return -1;
62 }
Damien Millerb3051d02014-01-10 10:58:53 +110063 if (ssh_digest_memory(hash_alg, data, datalen,
64 digest, sizeof(digest)) != 0) {
65 error("%s: ssh_digest_memory failed", __func__);
66 return -1;
67 }
Damien Miller0bc1bd82000-11-13 22:57:25 +110068
Ben Lindstrom425fb022001-03-29 00:31:20 +000069 slen = RSA_size(key->rsa);
70 sig = xmalloc(slen);
71
72 ok = RSA_sign(nid, digest, dlen, sig, &len, key->rsa);
Damien Millerc516e922002-02-05 11:53:43 +110073 memset(digest, 'd', sizeof(digest));
Damien Miller0bc1bd82000-11-13 22:57:25 +110074
75 if (ok != 1) {
76 int ecode = ERR_get_error();
Damien Miller90967402006-03-26 14:07:26 +110077
Damien Miller3e192952013-12-29 17:47:50 +110078 error("%s: RSA_sign failed: %s", __func__,
Ben Lindstrom5c385522002-06-23 21:23:20 +000079 ERR_error_string(ecode, NULL));
Darren Tuckera627d422013-06-02 07:31:17 +100080 free(sig);
Damien Miller0bc1bd82000-11-13 22:57:25 +110081 return -1;
82 }
83 if (len < slen) {
Ben Lindstrom0e50d842002-08-20 18:39:14 +000084 u_int diff = slen - len;
Ben Lindstrom5c385522002-06-23 21:23:20 +000085 debug("slen %u > len %u", slen, len);
Damien Miller0bc1bd82000-11-13 22:57:25 +110086 memmove(sig + diff, sig, len);
87 memset(sig, 0, diff);
88 } else if (len > slen) {
Damien Miller3e192952013-12-29 17:47:50 +110089 error("%s: slen %u slen2 %u", __func__, slen, len);
Darren Tuckera627d422013-06-02 07:31:17 +100090 free(sig);
Damien Miller0bc1bd82000-11-13 22:57:25 +110091 return -1;
92 }
93 /* encode signature */
94 buffer_init(&b);
95 buffer_put_cstring(&b, "ssh-rsa");
96 buffer_put_string(&b, sig, slen);
97 len = buffer_len(&b);
Ben Lindstrom2bf759c2002-07-07 22:13:31 +000098 if (lenp != NULL)
99 *lenp = len;
100 if (sigp != NULL) {
101 *sigp = xmalloc(len);
102 memcpy(*sigp, buffer_ptr(&b), len);
103 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100104 buffer_free(&b);
105 memset(sig, 's', slen);
Darren Tuckera627d422013-06-02 07:31:17 +1000106 free(sig);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100107
Damien Miller0bc1bd82000-11-13 22:57:25 +1100108 return 0;
109}
110
111int
Damien Millerf58b58c2003-11-17 21:18:23 +1100112ssh_rsa_verify(const Key *key, const u_char *signature, u_int signaturelen,
113 const u_char *data, u_int datalen)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100114{
115 Buffer b;
Damien Millerb3051d02014-01-10 10:58:53 +1100116 int hash_alg;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100117 char *ktype;
Damien Millerb3051d02014-01-10 10:58:53 +1100118 u_char digest[SSH_DIGEST_MAX_LENGTH], *sigblob;
Ben Lindstromceae9d12002-06-06 20:55:04 +0000119 u_int len, dlen, modlen;
Damien Millerb3051d02014-01-10 10:58:53 +1100120 int rlen, ret;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100121
Damien Miller3e192952013-12-29 17:47:50 +1100122 if (key == NULL || key_type_plain(key->type) != KEY_RSA ||
123 key->rsa == NULL) {
124 error("%s: no RSA key", __func__);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100125 return -1;
126 }
Damien Miller3e192952013-12-29 17:47:50 +1100127
Ben Lindstrom03f39322002-04-02 20:43:11 +0000128 if (BN_num_bits(key->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE) {
Damien Miller3e192952013-12-29 17:47:50 +1100129 error("%s: RSA modulus too small: %d < minimum %d bits",
130 __func__, BN_num_bits(key->rsa->n),
131 SSH_RSA_MINIMUM_MODULUS_SIZE);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000132 return -1;
133 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100134 buffer_init(&b);
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000135 buffer_append(&b, signature, signaturelen);
Damien Millerda108ec2010-08-31 22:36:39 +1000136 ktype = buffer_get_cstring(&b, NULL);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100137 if (strcmp("ssh-rsa", ktype) != 0) {
Damien Miller3e192952013-12-29 17:47:50 +1100138 error("%s: cannot handle type %s", __func__, ktype);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100139 buffer_free(&b);
Darren Tuckera627d422013-06-02 07:31:17 +1000140 free(ktype);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100141 return -1;
142 }
Darren Tuckera627d422013-06-02 07:31:17 +1000143 free(ktype);
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000144 sigblob = buffer_get_string(&b, &len);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100145 rlen = buffer_len(&b);
146 buffer_free(&b);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000147 if (rlen != 0) {
Damien Miller3e192952013-12-29 17:47:50 +1100148 error("%s: remaining bytes in signature %d", __func__, rlen);
Darren Tuckera627d422013-06-02 07:31:17 +1000149 free(sigblob);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100150 return -1;
151 }
Ben Lindstromceae9d12002-06-06 20:55:04 +0000152 /* RSA_verify expects a signature of RSA_size */
153 modlen = RSA_size(key->rsa);
154 if (len > modlen) {
Damien Miller3e192952013-12-29 17:47:50 +1100155 error("%s: len %u > modlen %u", __func__, len, modlen);
Darren Tuckera627d422013-06-02 07:31:17 +1000156 free(sigblob);
Ben Lindstromceae9d12002-06-06 20:55:04 +0000157 return -1;
158 } else if (len < modlen) {
Ben Lindstrom0e50d842002-08-20 18:39:14 +0000159 u_int diff = modlen - len;
Damien Miller3e192952013-12-29 17:47:50 +1100160 debug("%s: add padding: modlen %u > len %u", __func__,
Ben Lindstromceae9d12002-06-06 20:55:04 +0000161 modlen, len);
Damien Miller36812092006-03-26 14:22:47 +1100162 sigblob = xrealloc(sigblob, 1, modlen);
Ben Lindstromceae9d12002-06-06 20:55:04 +0000163 memmove(sigblob + diff, sigblob, len);
164 memset(sigblob, 0, diff);
165 len = modlen;
166 }
Damien Millerb3051d02014-01-10 10:58:53 +1100167 /* hash the data */
168 hash_alg = SSH_DIGEST_SHA1;
169 if ((dlen = ssh_digest_bytes(hash_alg)) == 0) {
170 error("%s: bad hash algorithm %d", __func__, hash_alg);
Ben Lindstrom425fb022001-03-29 00:31:20 +0000171 return -1;
172 }
Damien Millerb3051d02014-01-10 10:58:53 +1100173 if (ssh_digest_memory(hash_alg, data, datalen,
174 digest, sizeof(digest)) != 0) {
175 error("%s: ssh_digest_memory failed", __func__);
176 return -1;
177 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100178
Damien Millerb3051d02014-01-10 10:58:53 +1100179 ret = openssh_RSA_verify(hash_alg, digest, dlen, sigblob, len,
180 key->rsa);
Damien Millerc516e922002-02-05 11:53:43 +1100181 memset(digest, 'd', sizeof(digest));
Damien Miller0bc1bd82000-11-13 22:57:25 +1100182 memset(sigblob, 's', len);
Darren Tuckera627d422013-06-02 07:31:17 +1000183 free(sigblob);
Damien Miller3e192952013-12-29 17:47:50 +1100184 debug("%s: signature %scorrect", __func__, (ret == 0) ? "in" : "");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100185 return ret;
186}
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000187
188/*
189 * See:
190 * http://www.rsasecurity.com/rsalabs/pkcs/pkcs-1/
191 * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1.asn
192 */
193/*
194 * id-sha1 OBJECT IDENTIFIER ::= { iso(1) identified-organization(3)
195 * oiw(14) secsig(3) algorithms(2) 26 }
196 */
197static const u_char id_sha1[] = {
198 0x30, 0x21, /* type Sequence, length 0x21 (33) */
199 0x30, 0x09, /* type Sequence, length 0x09 */
200 0x06, 0x05, /* type OID, length 0x05 */
201 0x2b, 0x0e, 0x03, 0x02, 0x1a, /* id-sha1 OID */
202 0x05, 0x00, /* NULL */
203 0x04, 0x14 /* Octet string, length 0x14 (20), followed by sha1 hash */
204};
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000205
206static int
Damien Millerb3051d02014-01-10 10:58:53 +1100207openssh_RSA_verify(int hash_alg, u_char *hash, u_int hashlen,
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000208 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;
Damien Miller4e8285e2010-08-03 16:04:03 +1000211 int len, oidmatch, hashmatch;
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000212 const u_char *oid = NULL;
213 u_char *decrypted = NULL;
214
215 ret = 0;
Damien Millerb3051d02014-01-10 10:58:53 +1100216 switch (hash_alg) {
217 case SSH_DIGEST_SHA1:
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000218 oid = id_sha1;
219 oidlen = sizeof(id_sha1);
220 hlen = 20;
221 break;
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000222 default:
223 goto done;
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000224 }
225 if (hashlen != hlen) {
226 error("bad hashlen");
227 goto done;
228 }
229 rsasize = RSA_size(rsa);
230 if (siglen == 0 || siglen > rsasize) {
231 error("bad siglen");
232 goto done;
233 }
234 decrypted = xmalloc(rsasize);
235 if ((len = RSA_public_decrypt(siglen, sigbuf, decrypted, rsa,
236 RSA_PKCS1_PADDING)) < 0) {
237 error("RSA_public_decrypt failed: %s",
238 ERR_error_string(ERR_get_error(), NULL));
239 goto done;
240 }
Damien Millereccb9de2005-06-17 12:59:34 +1000241 if (len < 0 || (u_int)len != hlen + oidlen) {
Darren Tuckera251f802003-06-22 20:45:15 +1000242 error("bad decrypted len: %d != %d + %d", len, hlen, oidlen);
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000243 goto done;
244 }
Damien Miller4e8285e2010-08-03 16:04:03 +1000245 oidmatch = timingsafe_bcmp(decrypted, oid, oidlen) == 0;
246 hashmatch = timingsafe_bcmp(decrypted + oidlen, hash, hlen) == 0;
247 if (!oidmatch) {
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000248 error("oid mismatch");
249 goto done;
250 }
Damien Miller4e8285e2010-08-03 16:04:03 +1000251 if (!hashmatch) {
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000252 error("hash mismatch");
253 goto done;
254 }
255 ret = 1;
256done:
Darren Tuckera627d422013-06-02 07:31:17 +1000257 free(decrypted);
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000258 return ret;
259}