blob: aef798da62c4b6411eae06b2f1527c2dd0cd67e0 [file] [log] [blame]
Damien Miller86687062014-07-02 15:28:02 +10001/* $OpenBSD: ssh-rsa.c,v 1.52 2014/06/24 01:13:21 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 Miller72ef7c12015-01-15 02:21:31 +110020#ifdef WITH_OPENSSL
21
Damien Millerd7834352006-08-05 12:39:39 +100022#include <sys/types.h>
23
Damien Miller0bc1bd82000-11-13 22:57:25 +110024#include <openssl/evp.h>
Damien Miller0bc1bd82000-11-13 22:57:25 +110025#include <openssl/err.h>
26
Damien Millerded319c2006-09-01 15:38:36 +100027#include <stdarg.h>
Damien Millere3476ed2006-07-24 14:13:33 +100028#include <string.h>
29
Damien Miller86687062014-07-02 15:28:02 +100030#include "sshbuf.h"
Ben Lindstrom60a43812001-03-29 00:32:56 +000031#include "compat.h"
Damien Miller86687062014-07-02 15:28:02 +100032#include "ssherr.h"
33#define SSHKEY_INTERNAL
34#include "sshkey.h"
Damien Millerb3051d02014-01-10 10:58:53 +110035#include "digest.h"
Damien Miller0bc1bd82000-11-13 22:57:25 +110036
Damien Miller86687062014-07-02 15:28:02 +100037static int openssh_RSA_verify(int, u_char *, size_t, u_char *, size_t, 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 Miller86687062014-07-02 15:28:02 +100041ssh_rsa_sign(const struct sshkey *key, u_char **sigp, size_t *lenp,
42 const u_char *data, size_t datalen, u_int compat)
Damien Miller0bc1bd82000-11-13 22:57:25 +110043{
Damien Millerb3051d02014-01-10 10:58:53 +110044 int hash_alg;
Damien Miller86687062014-07-02 15:28:02 +100045 u_char digest[SSH_DIGEST_MAX_LENGTH], *sig = NULL;
46 size_t slen;
47 u_int dlen, len;
48 int nid, ret = SSH_ERR_INTERNAL_ERROR;
49 struct sshbuf *b = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +110050
Damien Miller86687062014-07-02 15:28:02 +100051 if (lenp != NULL)
52 *lenp = 0;
53 if (sigp != NULL)
54 *sigp = NULL;
55
56 if (key == NULL || key->rsa == NULL ||
57 sshkey_type_plain(key->type) != KEY_RSA)
58 return SSH_ERR_INVALID_ARGUMENT;
59 slen = RSA_size(key->rsa);
60 if (slen <= 0 || slen > SSHBUF_MAX_BIGNUM)
61 return SSH_ERR_INVALID_ARGUMENT;
Damien Miller3e192952013-12-29 17:47:50 +110062
Damien Millerb3051d02014-01-10 10:58:53 +110063 /* hash the data */
64 hash_alg = SSH_DIGEST_SHA1;
Damien Miller324541e2013-12-31 12:25:40 +110065 nid = NID_sha1;
Damien Miller86687062014-07-02 15:28:02 +100066 if ((dlen = ssh_digest_bytes(hash_alg)) == 0)
67 return SSH_ERR_INTERNAL_ERROR;
68 if ((ret = ssh_digest_memory(hash_alg, data, datalen,
69 digest, sizeof(digest))) != 0)
70 goto out;
71
72 if ((sig = malloc(slen)) == NULL) {
73 ret = SSH_ERR_ALLOC_FAIL;
74 goto out;
Damien Millerb3051d02014-01-10 10:58:53 +110075 }
Damien Miller0bc1bd82000-11-13 22:57:25 +110076
Damien Miller86687062014-07-02 15:28:02 +100077 if (RSA_sign(nid, digest, dlen, sig, &len, key->rsa) != 1) {
78 ret = SSH_ERR_LIBCRYPTO_ERROR;
79 goto out;
Damien Miller0bc1bd82000-11-13 22:57:25 +110080 }
81 if (len < slen) {
Damien Miller86687062014-07-02 15:28:02 +100082 size_t diff = slen - len;
Damien Miller0bc1bd82000-11-13 22:57:25 +110083 memmove(sig + diff, sig, len);
Damien Millera5103f42014-02-04 11:20:14 +110084 explicit_bzero(sig, diff);
Damien Miller0bc1bd82000-11-13 22:57:25 +110085 } else if (len > slen) {
Damien Miller86687062014-07-02 15:28:02 +100086 ret = SSH_ERR_INTERNAL_ERROR;
87 goto out;
Damien Miller0bc1bd82000-11-13 22:57:25 +110088 }
89 /* encode signature */
Damien Miller86687062014-07-02 15:28:02 +100090 if ((b = sshbuf_new()) == NULL) {
91 ret = SSH_ERR_ALLOC_FAIL;
92 goto out;
93 }
94 if ((ret = sshbuf_put_cstring(b, "ssh-rsa")) != 0 ||
95 (ret = sshbuf_put_string(b, sig, slen)) != 0)
96 goto out;
97 len = sshbuf_len(b);
98 if (sigp != NULL) {
99 if ((*sigp = malloc(len)) == NULL) {
100 ret = SSH_ERR_ALLOC_FAIL;
101 goto out;
102 }
103 memcpy(*sigp, sshbuf_ptr(b), len);
104 }
Ben Lindstrom2bf759c2002-07-07 22:13:31 +0000105 if (lenp != NULL)
106 *lenp = len;
Damien Miller86687062014-07-02 15:28:02 +1000107 ret = 0;
108 out:
109 explicit_bzero(digest, sizeof(digest));
110 if (sig != NULL) {
111 explicit_bzero(sig, slen);
112 free(sig);
Ben Lindstrom2bf759c2002-07-07 22:13:31 +0000113 }
Damien Miller86687062014-07-02 15:28:02 +1000114 if (b != NULL)
115 sshbuf_free(b);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100116 return 0;
117}
118
119int
Damien Miller86687062014-07-02 15:28:02 +1000120ssh_rsa_verify(const struct sshkey *key,
121 const u_char *signature, size_t signaturelen,
122 const u_char *data, size_t datalen, u_int compat)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100123{
Damien Miller86687062014-07-02 15:28:02 +1000124 char *ktype = NULL;
125 int hash_alg, ret = SSH_ERR_INTERNAL_ERROR;
126 size_t len, diff, modlen, dlen;
127 struct sshbuf *b = NULL;
128 u_char digest[SSH_DIGEST_MAX_LENGTH], *osigblob, *sigblob = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100129
Damien Miller86687062014-07-02 15:28:02 +1000130 if (key == NULL || key->rsa == NULL ||
131 sshkey_type_plain(key->type) != KEY_RSA ||
132 BN_num_bits(key->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE)
133 return SSH_ERR_INVALID_ARGUMENT;
Damien Miller3e192952013-12-29 17:47:50 +1100134
Damien Miller86687062014-07-02 15:28:02 +1000135 if ((b = sshbuf_from(signature, signaturelen)) == NULL)
136 return SSH_ERR_ALLOC_FAIL;
137 if (sshbuf_get_cstring(b, &ktype, NULL) != 0) {
138 ret = SSH_ERR_INVALID_FORMAT;
139 goto out;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000140 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100141 if (strcmp("ssh-rsa", ktype) != 0) {
Damien Miller86687062014-07-02 15:28:02 +1000142 ret = SSH_ERR_KEY_TYPE_MISMATCH;
143 goto out;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100144 }
Damien Miller86687062014-07-02 15:28:02 +1000145 if (sshbuf_get_string(b, &sigblob, &len) != 0) {
146 ret = SSH_ERR_INVALID_FORMAT;
147 goto out;
148 }
149 if (sshbuf_len(b) != 0) {
150 ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
151 goto out;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100152 }
Ben Lindstromceae9d12002-06-06 20:55:04 +0000153 /* RSA_verify expects a signature of RSA_size */
154 modlen = RSA_size(key->rsa);
155 if (len > modlen) {
Damien Miller86687062014-07-02 15:28:02 +1000156 ret = SSH_ERR_KEY_BITS_MISMATCH;
157 goto out;
Ben Lindstromceae9d12002-06-06 20:55:04 +0000158 } else if (len < modlen) {
Damien Miller86687062014-07-02 15:28:02 +1000159 diff = modlen - len;
160 osigblob = sigblob;
161 if ((sigblob = realloc(sigblob, modlen)) == NULL) {
162 sigblob = osigblob; /* put it back for clear/free */
163 ret = SSH_ERR_ALLOC_FAIL;
164 goto out;
165 }
Ben Lindstromceae9d12002-06-06 20:55:04 +0000166 memmove(sigblob + diff, sigblob, len);
Damien Millera5103f42014-02-04 11:20:14 +1100167 explicit_bzero(sigblob, diff);
Ben Lindstromceae9d12002-06-06 20:55:04 +0000168 len = modlen;
169 }
Damien Millerb3051d02014-01-10 10:58:53 +1100170 hash_alg = SSH_DIGEST_SHA1;
171 if ((dlen = ssh_digest_bytes(hash_alg)) == 0) {
Damien Miller86687062014-07-02 15:28:02 +1000172 ret = SSH_ERR_INTERNAL_ERROR;
173 goto out;
Ben Lindstrom425fb022001-03-29 00:31:20 +0000174 }
Damien Miller86687062014-07-02 15:28:02 +1000175 if ((ret = ssh_digest_memory(hash_alg, data, datalen,
176 digest, sizeof(digest))) != 0)
177 goto out;
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 Miller86687062014-07-02 15:28:02 +1000181 out:
182 if (sigblob != NULL) {
183 explicit_bzero(sigblob, len);
184 free(sigblob);
185 }
186 if (ktype != NULL)
187 free(ktype);
188 if (b != NULL)
189 sshbuf_free(b);
Damien Millera5103f42014-02-04 11:20:14 +1100190 explicit_bzero(digest, sizeof(digest));
Damien Miller0bc1bd82000-11-13 22:57:25 +1100191 return ret;
192}
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000193
194/*
195 * See:
196 * http://www.rsasecurity.com/rsalabs/pkcs/pkcs-1/
197 * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1.asn
198 */
199/*
200 * id-sha1 OBJECT IDENTIFIER ::= { iso(1) identified-organization(3)
201 * oiw(14) secsig(3) algorithms(2) 26 }
202 */
203static const u_char id_sha1[] = {
204 0x30, 0x21, /* type Sequence, length 0x21 (33) */
205 0x30, 0x09, /* type Sequence, length 0x09 */
206 0x06, 0x05, /* type OID, length 0x05 */
207 0x2b, 0x0e, 0x03, 0x02, 0x1a, /* id-sha1 OID */
208 0x05, 0x00, /* NULL */
209 0x04, 0x14 /* Octet string, length 0x14 (20), followed by sha1 hash */
210};
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000211
212static int
Damien Miller86687062014-07-02 15:28:02 +1000213openssh_RSA_verify(int hash_alg, u_char *hash, size_t hashlen,
214 u_char *sigbuf, size_t siglen, RSA *rsa)
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000215{
Damien Miller86687062014-07-02 15:28:02 +1000216 size_t ret, rsasize = 0, 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
Damien Miller86687062014-07-02 15:28:02 +1000221 ret = SSH_ERR_INTERNAL_ERROR;
Damien Millerb3051d02014-01-10 10:58:53 +1100222 switch (hash_alg) {
223 case SSH_DIGEST_SHA1:
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000224 oid = id_sha1;
225 oidlen = sizeof(id_sha1);
226 hlen = 20;
227 break;
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000228 default:
229 goto done;
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000230 }
231 if (hashlen != hlen) {
Damien Miller86687062014-07-02 15:28:02 +1000232 ret = SSH_ERR_INVALID_ARGUMENT;
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000233 goto done;
234 }
235 rsasize = RSA_size(rsa);
Damien Miller86687062014-07-02 15:28:02 +1000236 if (rsasize <= 0 || rsasize > SSHBUF_MAX_BIGNUM ||
237 siglen == 0 || siglen > rsasize) {
238 ret = SSH_ERR_INVALID_ARGUMENT;
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000239 goto done;
240 }
Damien Miller86687062014-07-02 15:28:02 +1000241 if ((decrypted = malloc(rsasize)) == NULL) {
242 ret = SSH_ERR_ALLOC_FAIL;
243 goto done;
244 }
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000245 if ((len = RSA_public_decrypt(siglen, sigbuf, decrypted, rsa,
246 RSA_PKCS1_PADDING)) < 0) {
Damien Miller86687062014-07-02 15:28:02 +1000247 ret = SSH_ERR_LIBCRYPTO_ERROR;
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000248 goto done;
249 }
Damien Miller86687062014-07-02 15:28:02 +1000250 if (len < 0 || (size_t)len != hlen + oidlen) {
251 ret = SSH_ERR_INVALID_FORMAT;
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000252 goto done;
253 }
Damien Miller4e8285e2010-08-03 16:04:03 +1000254 oidmatch = timingsafe_bcmp(decrypted, oid, oidlen) == 0;
255 hashmatch = timingsafe_bcmp(decrypted + oidlen, hash, hlen) == 0;
Damien Miller86687062014-07-02 15:28:02 +1000256 if (!oidmatch || !hashmatch) {
257 ret = SSH_ERR_SIGNATURE_INVALID;
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000258 goto done;
259 }
Damien Miller86687062014-07-02 15:28:02 +1000260 ret = 0;
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000261done:
Damien Miller86687062014-07-02 15:28:02 +1000262 if (decrypted) {
263 explicit_bzero(decrypted, rsasize);
264 free(decrypted);
265 }
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000266 return ret;
267}
Damien Miller72ef7c12015-01-15 02:21:31 +1100268#endif /* WITH_OPENSSL */