blob: cde05df10143c9561d0b44c24246037993c59a5f [file] [log] [blame]
djm@openbsd.org445e2182016-09-12 23:39:34 +00001/* $OpenBSD: ssh-rsa.c,v 1.60 2016/09/12 23:39:34 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
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +000039static const char *
40rsa_hash_alg_ident(int hash_alg)
41{
42 switch (hash_alg) {
43 case SSH_DIGEST_SHA1:
44 return "ssh-rsa";
45 case SSH_DIGEST_SHA256:
46 return "rsa-sha2-256";
47 case SSH_DIGEST_SHA512:
48 return "rsa-sha2-512";
49 }
50 return NULL;
51}
52
53static int
54rsa_hash_alg_from_ident(const char *ident)
55{
djm@openbsd.org445e2182016-09-12 23:39:34 +000056 if (strcmp(ident, "ssh-rsa") == 0 ||
57 strcmp(ident, "ssh-rsa-cert-v01@openssh.com") == 0)
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +000058 return SSH_DIGEST_SHA1;
59 if (strcmp(ident, "rsa-sha2-256") == 0)
60 return SSH_DIGEST_SHA256;
61 if (strcmp(ident, "rsa-sha2-512") == 0)
62 return SSH_DIGEST_SHA512;
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +000063 return -1;
64}
65
66static int
67rsa_hash_alg_nid(int type)
68{
69 switch (type) {
70 case SSH_DIGEST_SHA1:
71 return NID_sha1;
72 case SSH_DIGEST_SHA256:
73 return NID_sha256;
74 case SSH_DIGEST_SHA512:
75 return NID_sha512;
76 default:
77 return -1;
78 }
79}
80
Damien Miller0bc1bd82000-11-13 22:57:25 +110081/* RSASSA-PKCS1-v1_5 (PKCS #1 v2.0 signature) with SHA1 */
82int
Damien Miller86687062014-07-02 15:28:02 +100083ssh_rsa_sign(const struct sshkey *key, u_char **sigp, size_t *lenp,
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +000084 const u_char *data, size_t datalen, const char *alg_ident)
Damien Miller0bc1bd82000-11-13 22:57:25 +110085{
Damien Miller86687062014-07-02 15:28:02 +100086 u_char digest[SSH_DIGEST_MAX_LENGTH], *sig = NULL;
87 size_t slen;
88 u_int dlen, len;
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +000089 int nid, hash_alg, ret = SSH_ERR_INTERNAL_ERROR;
Damien Miller86687062014-07-02 15:28:02 +100090 struct sshbuf *b = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +110091
Damien Miller86687062014-07-02 15:28:02 +100092 if (lenp != NULL)
93 *lenp = 0;
94 if (sigp != NULL)
95 *sigp = NULL;
96
djm@openbsd.org445e2182016-09-12 23:39:34 +000097 if (alg_ident == NULL || strlen(alg_ident) == 0)
markus@openbsd.org6262a052015-12-07 20:04:09 +000098 hash_alg = SSH_DIGEST_SHA1;
99 else
100 hash_alg = rsa_hash_alg_from_ident(alg_ident);
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000101 if (key == NULL || key->rsa == NULL || hash_alg == -1 ||
102 sshkey_type_plain(key->type) != KEY_RSA ||
103 BN_num_bits(key->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE)
Damien Miller86687062014-07-02 15:28:02 +1000104 return SSH_ERR_INVALID_ARGUMENT;
105 slen = RSA_size(key->rsa);
106 if (slen <= 0 || slen > SSHBUF_MAX_BIGNUM)
107 return SSH_ERR_INVALID_ARGUMENT;
Damien Miller3e192952013-12-29 17:47:50 +1100108
Damien Millerb3051d02014-01-10 10:58:53 +1100109 /* hash the data */
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000110 nid = rsa_hash_alg_nid(hash_alg);
Damien Miller86687062014-07-02 15:28:02 +1000111 if ((dlen = ssh_digest_bytes(hash_alg)) == 0)
112 return SSH_ERR_INTERNAL_ERROR;
113 if ((ret = ssh_digest_memory(hash_alg, data, datalen,
114 digest, sizeof(digest))) != 0)
115 goto out;
116
117 if ((sig = malloc(slen)) == NULL) {
118 ret = SSH_ERR_ALLOC_FAIL;
119 goto out;
Damien Millerb3051d02014-01-10 10:58:53 +1100120 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100121
Damien Miller86687062014-07-02 15:28:02 +1000122 if (RSA_sign(nid, digest, dlen, sig, &len, key->rsa) != 1) {
123 ret = SSH_ERR_LIBCRYPTO_ERROR;
124 goto out;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100125 }
126 if (len < slen) {
Damien Miller86687062014-07-02 15:28:02 +1000127 size_t diff = slen - len;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100128 memmove(sig + diff, sig, len);
Damien Millera5103f42014-02-04 11:20:14 +1100129 explicit_bzero(sig, diff);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100130 } else if (len > slen) {
Damien Miller86687062014-07-02 15:28:02 +1000131 ret = SSH_ERR_INTERNAL_ERROR;
132 goto out;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100133 }
134 /* encode signature */
Damien Miller86687062014-07-02 15:28:02 +1000135 if ((b = sshbuf_new()) == NULL) {
136 ret = SSH_ERR_ALLOC_FAIL;
137 goto out;
138 }
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000139 if ((ret = sshbuf_put_cstring(b, rsa_hash_alg_ident(hash_alg))) != 0 ||
Damien Miller86687062014-07-02 15:28:02 +1000140 (ret = sshbuf_put_string(b, sig, slen)) != 0)
141 goto out;
142 len = sshbuf_len(b);
143 if (sigp != NULL) {
144 if ((*sigp = malloc(len)) == NULL) {
145 ret = SSH_ERR_ALLOC_FAIL;
146 goto out;
147 }
148 memcpy(*sigp, sshbuf_ptr(b), len);
149 }
Ben Lindstrom2bf759c2002-07-07 22:13:31 +0000150 if (lenp != NULL)
151 *lenp = len;
Damien Miller86687062014-07-02 15:28:02 +1000152 ret = 0;
153 out:
154 explicit_bzero(digest, sizeof(digest));
155 if (sig != NULL) {
156 explicit_bzero(sig, slen);
157 free(sig);
Ben Lindstrom2bf759c2002-07-07 22:13:31 +0000158 }
mmcc@openbsd.org52d70782015-12-11 04:21:11 +0000159 sshbuf_free(b);
djm@openbsd.org4ef702e2015-06-15 01:32:50 +0000160 return ret;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100161}
162
163int
Damien Miller86687062014-07-02 15:28:02 +1000164ssh_rsa_verify(const struct sshkey *key,
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000165 const u_char *sig, size_t siglen, const u_char *data, size_t datalen)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100166{
Damien Miller86687062014-07-02 15:28:02 +1000167 char *ktype = NULL;
168 int hash_alg, ret = SSH_ERR_INTERNAL_ERROR;
169 size_t len, diff, modlen, dlen;
170 struct sshbuf *b = NULL;
171 u_char digest[SSH_DIGEST_MAX_LENGTH], *osigblob, *sigblob = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100172
Damien Miller86687062014-07-02 15:28:02 +1000173 if (key == NULL || key->rsa == NULL ||
174 sshkey_type_plain(key->type) != KEY_RSA ||
djm@openbsd.orgb6e01402016-04-21 06:08:02 +0000175 BN_num_bits(key->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE ||
176 sig == NULL || siglen == 0)
Damien Miller86687062014-07-02 15:28:02 +1000177 return SSH_ERR_INVALID_ARGUMENT;
Damien Miller3e192952013-12-29 17:47:50 +1100178
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000179 if ((b = sshbuf_from(sig, siglen)) == NULL)
Damien Miller86687062014-07-02 15:28:02 +1000180 return SSH_ERR_ALLOC_FAIL;
181 if (sshbuf_get_cstring(b, &ktype, NULL) != 0) {
182 ret = SSH_ERR_INVALID_FORMAT;
183 goto out;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000184 }
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000185 if ((hash_alg = rsa_hash_alg_from_ident(ktype)) == -1) {
Damien Miller86687062014-07-02 15:28:02 +1000186 ret = SSH_ERR_KEY_TYPE_MISMATCH;
187 goto out;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100188 }
Damien Miller86687062014-07-02 15:28:02 +1000189 if (sshbuf_get_string(b, &sigblob, &len) != 0) {
190 ret = SSH_ERR_INVALID_FORMAT;
191 goto out;
192 }
193 if (sshbuf_len(b) != 0) {
194 ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
195 goto out;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100196 }
Ben Lindstromceae9d12002-06-06 20:55:04 +0000197 /* RSA_verify expects a signature of RSA_size */
198 modlen = RSA_size(key->rsa);
199 if (len > modlen) {
Damien Miller86687062014-07-02 15:28:02 +1000200 ret = SSH_ERR_KEY_BITS_MISMATCH;
201 goto out;
Ben Lindstromceae9d12002-06-06 20:55:04 +0000202 } else if (len < modlen) {
Damien Miller86687062014-07-02 15:28:02 +1000203 diff = modlen - len;
204 osigblob = sigblob;
205 if ((sigblob = realloc(sigblob, modlen)) == NULL) {
206 sigblob = osigblob; /* put it back for clear/free */
207 ret = SSH_ERR_ALLOC_FAIL;
208 goto out;
209 }
Ben Lindstromceae9d12002-06-06 20:55:04 +0000210 memmove(sigblob + diff, sigblob, len);
Damien Millera5103f42014-02-04 11:20:14 +1100211 explicit_bzero(sigblob, diff);
Ben Lindstromceae9d12002-06-06 20:55:04 +0000212 len = modlen;
213 }
Damien Millerb3051d02014-01-10 10:58:53 +1100214 if ((dlen = ssh_digest_bytes(hash_alg)) == 0) {
Damien Miller86687062014-07-02 15:28:02 +1000215 ret = SSH_ERR_INTERNAL_ERROR;
216 goto out;
Ben Lindstrom425fb022001-03-29 00:31:20 +0000217 }
Damien Miller86687062014-07-02 15:28:02 +1000218 if ((ret = ssh_digest_memory(hash_alg, data, datalen,
219 digest, sizeof(digest))) != 0)
220 goto out;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100221
Damien Millerb3051d02014-01-10 10:58:53 +1100222 ret = openssh_RSA_verify(hash_alg, digest, dlen, sigblob, len,
223 key->rsa);
Damien Miller86687062014-07-02 15:28:02 +1000224 out:
225 if (sigblob != NULL) {
226 explicit_bzero(sigblob, len);
227 free(sigblob);
228 }
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +0000229 free(ktype);
mmcc@openbsd.org52d70782015-12-11 04:21:11 +0000230 sshbuf_free(b);
Damien Millera5103f42014-02-04 11:20:14 +1100231 explicit_bzero(digest, sizeof(digest));
Damien Miller0bc1bd82000-11-13 22:57:25 +1100232 return ret;
233}
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000234
235/*
236 * See:
237 * http://www.rsasecurity.com/rsalabs/pkcs/pkcs-1/
238 * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1.asn
239 */
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000240
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000241/*
242 * id-sha1 OBJECT IDENTIFIER ::= { iso(1) identified-organization(3)
243 * oiw(14) secsig(3) algorithms(2) 26 }
244 */
245static const u_char id_sha1[] = {
246 0x30, 0x21, /* type Sequence, length 0x21 (33) */
247 0x30, 0x09, /* type Sequence, length 0x09 */
248 0x06, 0x05, /* type OID, length 0x05 */
249 0x2b, 0x0e, 0x03, 0x02, 0x1a, /* id-sha1 OID */
250 0x05, 0x00, /* NULL */
251 0x04, 0x14 /* Octet string, length 0x14 (20), followed by sha1 hash */
252};
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000253
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000254/*
255 * See http://csrc.nist.gov/groups/ST/crypto_apps_infra/csor/algorithms.html
256 * id-sha256 OBJECT IDENTIFIER ::= { joint-iso-itu-t(2) country(16) us(840)
257 * organization(1) gov(101) csor(3) nistAlgorithm(4) hashAlgs(2)
258 * id-sha256(1) }
259 */
260static const u_char id_sha256[] = {
261 0x30, 0x31, /* type Sequence, length 0x31 (49) */
262 0x30, 0x0d, /* type Sequence, length 0x0d (13) */
263 0x06, 0x09, /* type OID, length 0x09 */
264 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, /* id-sha256 */
265 0x05, 0x00, /* NULL */
266 0x04, 0x20 /* Octet string, length 0x20 (32), followed by sha256 hash */
267};
268
269/*
270 * See http://csrc.nist.gov/groups/ST/crypto_apps_infra/csor/algorithms.html
271 * id-sha512 OBJECT IDENTIFIER ::= { joint-iso-itu-t(2) country(16) us(840)
272 * organization(1) gov(101) csor(3) nistAlgorithm(4) hashAlgs(2)
273 * id-sha256(3) }
274 */
275static const u_char id_sha512[] = {
276 0x30, 0x51, /* type Sequence, length 0x51 (81) */
277 0x30, 0x0d, /* type Sequence, length 0x0d (13) */
278 0x06, 0x09, /* type OID, length 0x09 */
279 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, /* id-sha512 */
280 0x05, 0x00, /* NULL */
281 0x04, 0x40 /* Octet string, length 0x40 (64), followed by sha512 hash */
282};
283
284static int
285rsa_hash_alg_oid(int hash_alg, const u_char **oidp, size_t *oidlenp)
286{
287 switch (hash_alg) {
288 case SSH_DIGEST_SHA1:
289 *oidp = id_sha1;
290 *oidlenp = sizeof(id_sha1);
291 break;
292 case SSH_DIGEST_SHA256:
293 *oidp = id_sha256;
294 *oidlenp = sizeof(id_sha256);
295 break;
296 case SSH_DIGEST_SHA512:
297 *oidp = id_sha512;
298 *oidlenp = sizeof(id_sha512);
299 break;
300 default:
301 return SSH_ERR_INVALID_ARGUMENT;
302 }
303 return 0;
304}
305
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000306static int
Damien Miller86687062014-07-02 15:28:02 +1000307openssh_RSA_verify(int hash_alg, u_char *hash, size_t hashlen,
308 u_char *sigbuf, size_t siglen, RSA *rsa)
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000309{
djm@openbsd.org61942ea2015-09-09 00:52:44 +0000310 size_t rsasize = 0, oidlen = 0, hlen = 0;
311 int ret, len, oidmatch, hashmatch;
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000312 const u_char *oid = NULL;
313 u_char *decrypted = NULL;
314
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000315 if ((ret = rsa_hash_alg_oid(hash_alg, &oid, &oidlen)) != 0)
316 return ret;
Damien Miller86687062014-07-02 15:28:02 +1000317 ret = SSH_ERR_INTERNAL_ERROR;
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000318 hlen = ssh_digest_bytes(hash_alg);
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000319 if (hashlen != hlen) {
Damien Miller86687062014-07-02 15:28:02 +1000320 ret = SSH_ERR_INVALID_ARGUMENT;
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000321 goto done;
322 }
323 rsasize = RSA_size(rsa);
Damien Miller86687062014-07-02 15:28:02 +1000324 if (rsasize <= 0 || rsasize > SSHBUF_MAX_BIGNUM ||
325 siglen == 0 || siglen > rsasize) {
326 ret = SSH_ERR_INVALID_ARGUMENT;
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000327 goto done;
328 }
Damien Miller86687062014-07-02 15:28:02 +1000329 if ((decrypted = malloc(rsasize)) == NULL) {
330 ret = SSH_ERR_ALLOC_FAIL;
331 goto done;
332 }
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000333 if ((len = RSA_public_decrypt(siglen, sigbuf, decrypted, rsa,
334 RSA_PKCS1_PADDING)) < 0) {
Damien Miller86687062014-07-02 15:28:02 +1000335 ret = SSH_ERR_LIBCRYPTO_ERROR;
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000336 goto done;
337 }
Damien Miller86687062014-07-02 15:28:02 +1000338 if (len < 0 || (size_t)len != hlen + oidlen) {
339 ret = SSH_ERR_INVALID_FORMAT;
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000340 goto done;
341 }
Damien Miller4e8285e2010-08-03 16:04:03 +1000342 oidmatch = timingsafe_bcmp(decrypted, oid, oidlen) == 0;
343 hashmatch = timingsafe_bcmp(decrypted + oidlen, hash, hlen) == 0;
Damien Miller86687062014-07-02 15:28:02 +1000344 if (!oidmatch || !hashmatch) {
345 ret = SSH_ERR_SIGNATURE_INVALID;
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000346 goto done;
347 }
Damien Miller86687062014-07-02 15:28:02 +1000348 ret = 0;
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000349done:
Damien Miller86687062014-07-02 15:28:02 +1000350 if (decrypted) {
351 explicit_bzero(decrypted, rsasize);
352 free(decrypted);
353 }
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000354 return ret;
355}
Damien Miller72ef7c12015-01-15 02:21:31 +1100356#endif /* WITH_OPENSSL */