blob: 9b14f9a9a71527c2e817139870831f3f1ebc8faa [file] [log] [blame]
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001/* $OpenBSD: ssh-rsa.c,v 1.67 2018/07/03 11:39:54 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"
djm@openbsd.org966ef472017-12-18 23:14:34 +000036#include "log.h"
Damien Miller0bc1bd82000-11-13 22:57:25 +110037
Damien Miller48f54b92018-09-13 12:13:50 +100038#include "openbsd-compat/openssl-compat.h"
39
Damien Miller86687062014-07-02 15:28:02 +100040static int openssh_RSA_verify(int, u_char *, size_t, u_char *, size_t, RSA *);
Ben Lindstrom0deb5d92002-08-20 18:40:03 +000041
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +000042static const char *
43rsa_hash_alg_ident(int hash_alg)
44{
45 switch (hash_alg) {
46 case SSH_DIGEST_SHA1:
47 return "ssh-rsa";
48 case SSH_DIGEST_SHA256:
49 return "rsa-sha2-256";
50 case SSH_DIGEST_SHA512:
51 return "rsa-sha2-512";
52 }
53 return NULL;
54}
55
djm@openbsd.org4ba0d542018-07-03 11:39:54 +000056/*
57 * Returns the hash algorithm ID for a given algorithm identifier as used
58 * inside the signature blob,
59 */
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +000060static int
djm@openbsd.org4ba0d542018-07-03 11:39:54 +000061rsa_hash_id_from_ident(const char *ident)
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +000062{
djm@openbsd.org4ba0d542018-07-03 11:39:54 +000063 if (strcmp(ident, "ssh-rsa") == 0)
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +000064 return SSH_DIGEST_SHA1;
65 if (strcmp(ident, "rsa-sha2-256") == 0)
66 return SSH_DIGEST_SHA256;
67 if (strcmp(ident, "rsa-sha2-512") == 0)
68 return SSH_DIGEST_SHA512;
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +000069 return -1;
70}
71
djm@openbsd.org4ba0d542018-07-03 11:39:54 +000072/*
73 * Return the hash algorithm ID for the specified key name. This includes
74 * all the cases of rsa_hash_id_from_ident() but also the certificate key
75 * types.
76 */
77static int
78rsa_hash_id_from_keyname(const char *alg)
79{
80 int r;
81
82 if ((r = rsa_hash_id_from_ident(alg)) != -1)
83 return r;
84 if (strcmp(alg, "ssh-rsa-cert-v01@openssh.com") == 0)
85 return SSH_DIGEST_SHA1;
86 if (strcmp(alg, "rsa-sha2-256-cert-v01@openssh.com") == 0)
87 return SSH_DIGEST_SHA256;
88 if (strcmp(alg, "rsa-sha2-512-cert-v01@openssh.com") == 0)
89 return SSH_DIGEST_SHA512;
90 return -1;
91}
92
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +000093static int
94rsa_hash_alg_nid(int type)
95{
96 switch (type) {
97 case SSH_DIGEST_SHA1:
98 return NID_sha1;
99 case SSH_DIGEST_SHA256:
100 return NID_sha256;
101 case SSH_DIGEST_SHA512:
102 return NID_sha512;
103 default:
104 return -1;
105 }
106}
107
djm@openbsd.org83fa3a02017-07-01 13:50:45 +0000108int
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000109ssh_rsa_complete_crt_parameters(struct sshkey *key, const BIGNUM *iqmp)
djm@openbsd.org83fa3a02017-07-01 13:50:45 +0000110{
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000111 const BIGNUM *rsa_p, *rsa_q, *rsa_d;
112 BIGNUM *aux = NULL, *d_consttime = NULL;
113 BIGNUM *rsa_dmq1 = NULL, *rsa_dmp1 = NULL, *rsa_iqmp = NULL;
djm@openbsd.org83fa3a02017-07-01 13:50:45 +0000114 BN_CTX *ctx = NULL;
115 int r;
116
117 if (key == NULL || key->rsa == NULL ||
118 sshkey_type_plain(key->type) != KEY_RSA)
119 return SSH_ERR_INVALID_ARGUMENT;
120
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000121 RSA_get0_key(key->rsa, NULL, NULL, &rsa_d);
122 RSA_get0_factors(key->rsa, &rsa_p, &rsa_q);
123
djm@openbsd.org83fa3a02017-07-01 13:50:45 +0000124 if ((ctx = BN_CTX_new()) == NULL)
125 return SSH_ERR_ALLOC_FAIL;
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000126 if ((aux = BN_new()) == NULL ||
127 (rsa_dmq1 = BN_new()) == NULL ||
128 (rsa_dmp1 = BN_new()) == NULL)
129 return SSH_ERR_ALLOC_FAIL;
130 if ((d_consttime = BN_dup(rsa_d)) == NULL ||
131 (rsa_iqmp = BN_dup(iqmp)) == NULL) {
djm@openbsd.org83fa3a02017-07-01 13:50:45 +0000132 r = SSH_ERR_ALLOC_FAIL;
133 goto out;
134 }
jsing@openbsd.orgd2b3db22018-02-14 16:27:24 +0000135 BN_set_flags(aux, BN_FLG_CONSTTIME);
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000136 BN_set_flags(d_consttime, BN_FLG_CONSTTIME);
djm@openbsd.org83fa3a02017-07-01 13:50:45 +0000137
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000138 if ((BN_sub(aux, rsa_q, BN_value_one()) == 0) ||
139 (BN_mod(rsa_dmq1, d_consttime, aux, ctx) == 0) ||
140 (BN_sub(aux, rsa_p, BN_value_one()) == 0) ||
141 (BN_mod(rsa_dmp1, d_consttime, aux, ctx) == 0)) {
djm@openbsd.org83fa3a02017-07-01 13:50:45 +0000142 r = SSH_ERR_LIBCRYPTO_ERROR;
143 goto out;
144 }
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000145 if (!RSA_set0_crt_params(key->rsa, rsa_dmp1, rsa_dmq1, rsa_iqmp)) {
146 r = SSH_ERR_LIBCRYPTO_ERROR;
147 goto out;
148 }
149 rsa_dmp1 = rsa_dmq1 = rsa_iqmp = NULL; /* transferred */
150 /* success */
djm@openbsd.org83fa3a02017-07-01 13:50:45 +0000151 r = 0;
152 out:
153 BN_clear_free(aux);
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000154 BN_clear_free(d_consttime);
155 BN_clear_free(rsa_dmp1);
156 BN_clear_free(rsa_dmq1);
157 BN_clear_free(rsa_iqmp);
djm@openbsd.org83fa3a02017-07-01 13:50:45 +0000158 BN_CTX_free(ctx);
159 return r;
160}
161
Damien Miller0bc1bd82000-11-13 22:57:25 +1100162/* RSASSA-PKCS1-v1_5 (PKCS #1 v2.0 signature) with SHA1 */
163int
Damien Miller86687062014-07-02 15:28:02 +1000164ssh_rsa_sign(const struct sshkey *key, u_char **sigp, size_t *lenp,
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000165 const u_char *data, size_t datalen, const char *alg_ident)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100166{
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000167 const BIGNUM *rsa_n;
Damien Miller86687062014-07-02 15:28:02 +1000168 u_char digest[SSH_DIGEST_MAX_LENGTH], *sig = NULL;
jsing@openbsd.org17499912018-02-07 05:17:56 +0000169 size_t slen = 0;
Damien Miller86687062014-07-02 15:28:02 +1000170 u_int dlen, len;
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000171 int nid, hash_alg, ret = SSH_ERR_INTERNAL_ERROR;
Damien Miller86687062014-07-02 15:28:02 +1000172 struct sshbuf *b = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100173
Damien Miller86687062014-07-02 15:28:02 +1000174 if (lenp != NULL)
175 *lenp = 0;
176 if (sigp != NULL)
177 *sigp = NULL;
178
djm@openbsd.org445e2182016-09-12 23:39:34 +0000179 if (alg_ident == NULL || strlen(alg_ident) == 0)
markus@openbsd.org6262a052015-12-07 20:04:09 +0000180 hash_alg = SSH_DIGEST_SHA1;
181 else
djm@openbsd.org4ba0d542018-07-03 11:39:54 +0000182 hash_alg = rsa_hash_id_from_keyname(alg_ident);
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000183 if (key == NULL || key->rsa == NULL || hash_alg == -1 ||
djm@openbsd.orgbd636f42017-05-07 23:15:59 +0000184 sshkey_type_plain(key->type) != KEY_RSA)
Damien Miller86687062014-07-02 15:28:02 +1000185 return SSH_ERR_INVALID_ARGUMENT;
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000186 RSA_get0_key(key->rsa, &rsa_n, NULL, NULL);
187 if (BN_num_bits(rsa_n) < SSH_RSA_MINIMUM_MODULUS_SIZE)
djm@openbsd.orgbd636f42017-05-07 23:15:59 +0000188 return SSH_ERR_KEY_LENGTH;
Damien Miller86687062014-07-02 15:28:02 +1000189 slen = RSA_size(key->rsa);
190 if (slen <= 0 || slen > SSHBUF_MAX_BIGNUM)
191 return SSH_ERR_INVALID_ARGUMENT;
Damien Miller3e192952013-12-29 17:47:50 +1100192
Damien Millerb3051d02014-01-10 10:58:53 +1100193 /* hash the data */
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000194 nid = rsa_hash_alg_nid(hash_alg);
Damien Miller86687062014-07-02 15:28:02 +1000195 if ((dlen = ssh_digest_bytes(hash_alg)) == 0)
196 return SSH_ERR_INTERNAL_ERROR;
197 if ((ret = ssh_digest_memory(hash_alg, data, datalen,
198 digest, sizeof(digest))) != 0)
199 goto out;
200
201 if ((sig = malloc(slen)) == NULL) {
202 ret = SSH_ERR_ALLOC_FAIL;
203 goto out;
Damien Millerb3051d02014-01-10 10:58:53 +1100204 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100205
Damien Miller86687062014-07-02 15:28:02 +1000206 if (RSA_sign(nid, digest, dlen, sig, &len, key->rsa) != 1) {
207 ret = SSH_ERR_LIBCRYPTO_ERROR;
208 goto out;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100209 }
210 if (len < slen) {
Damien Miller86687062014-07-02 15:28:02 +1000211 size_t diff = slen - len;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100212 memmove(sig + diff, sig, len);
Damien Millera5103f42014-02-04 11:20:14 +1100213 explicit_bzero(sig, diff);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100214 } else if (len > slen) {
Damien Miller86687062014-07-02 15:28:02 +1000215 ret = SSH_ERR_INTERNAL_ERROR;
216 goto out;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100217 }
218 /* encode signature */
Damien Miller86687062014-07-02 15:28:02 +1000219 if ((b = sshbuf_new()) == NULL) {
220 ret = SSH_ERR_ALLOC_FAIL;
221 goto out;
222 }
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000223 if ((ret = sshbuf_put_cstring(b, rsa_hash_alg_ident(hash_alg))) != 0 ||
Damien Miller86687062014-07-02 15:28:02 +1000224 (ret = sshbuf_put_string(b, sig, slen)) != 0)
225 goto out;
226 len = sshbuf_len(b);
227 if (sigp != NULL) {
228 if ((*sigp = malloc(len)) == NULL) {
229 ret = SSH_ERR_ALLOC_FAIL;
230 goto out;
231 }
232 memcpy(*sigp, sshbuf_ptr(b), len);
233 }
Ben Lindstrom2bf759c2002-07-07 22:13:31 +0000234 if (lenp != NULL)
235 *lenp = len;
Damien Miller86687062014-07-02 15:28:02 +1000236 ret = 0;
237 out:
238 explicit_bzero(digest, sizeof(digest));
jsing@openbsd.org17499912018-02-07 05:17:56 +0000239 freezero(sig, slen);
mmcc@openbsd.org52d70782015-12-11 04:21:11 +0000240 sshbuf_free(b);
djm@openbsd.org4ef702e2015-06-15 01:32:50 +0000241 return ret;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100242}
243
244int
Damien Miller86687062014-07-02 15:28:02 +1000245ssh_rsa_verify(const struct sshkey *key,
djm@openbsd.org04c7e282017-12-18 02:25:15 +0000246 const u_char *sig, size_t siglen, const u_char *data, size_t datalen,
247 const char *alg)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100248{
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000249 const BIGNUM *rsa_n;
djm@openbsd.org04c7e282017-12-18 02:25:15 +0000250 char *sigtype = NULL;
djm@openbsd.org4ba0d542018-07-03 11:39:54 +0000251 int hash_alg, want_alg, ret = SSH_ERR_INTERNAL_ERROR;
jsing@openbsd.org17499912018-02-07 05:17:56 +0000252 size_t len = 0, diff, modlen, dlen;
Damien Miller86687062014-07-02 15:28:02 +1000253 struct sshbuf *b = NULL;
254 u_char digest[SSH_DIGEST_MAX_LENGTH], *osigblob, *sigblob = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100255
Damien Miller86687062014-07-02 15:28:02 +1000256 if (key == NULL || key->rsa == NULL ||
257 sshkey_type_plain(key->type) != KEY_RSA ||
djm@openbsd.orgb6e01402016-04-21 06:08:02 +0000258 sig == NULL || siglen == 0)
Damien Miller86687062014-07-02 15:28:02 +1000259 return SSH_ERR_INVALID_ARGUMENT;
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000260 RSA_get0_key(key->rsa, &rsa_n, NULL, NULL);
261 if (BN_num_bits(rsa_n) < SSH_RSA_MINIMUM_MODULUS_SIZE)
djm@openbsd.orgbd636f42017-05-07 23:15:59 +0000262 return SSH_ERR_KEY_LENGTH;
Damien Miller3e192952013-12-29 17:47:50 +1100263
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000264 if ((b = sshbuf_from(sig, siglen)) == NULL)
Damien Miller86687062014-07-02 15:28:02 +1000265 return SSH_ERR_ALLOC_FAIL;
djm@openbsd.org04c7e282017-12-18 02:25:15 +0000266 if (sshbuf_get_cstring(b, &sigtype, NULL) != 0) {
Damien Miller86687062014-07-02 15:28:02 +1000267 ret = SSH_ERR_INVALID_FORMAT;
268 goto out;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000269 }
djm@openbsd.org4ba0d542018-07-03 11:39:54 +0000270 if ((hash_alg = rsa_hash_id_from_ident(sigtype)) == -1) {
Damien Miller86687062014-07-02 15:28:02 +1000271 ret = SSH_ERR_KEY_TYPE_MISMATCH;
272 goto out;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100273 }
djm@openbsd.org4ba0d542018-07-03 11:39:54 +0000274 /*
275 * Allow ssh-rsa-cert-v01 certs to generate SHA2 signatures for
276 * legacy reasons, but otherwise the signature type should match.
277 */
278 if (alg != NULL && strcmp(alg, "ssh-rsa-cert-v01@openssh.com") != 0) {
279 if ((want_alg = rsa_hash_id_from_keyname(alg)) == -1) {
280 ret = SSH_ERR_INVALID_ARGUMENT;
281 goto out;
282 }
283 if (hash_alg != want_alg) {
284 ret = SSH_ERR_SIGNATURE_INVALID;
285 goto out;
286 }
287 }
Damien Miller86687062014-07-02 15:28:02 +1000288 if (sshbuf_get_string(b, &sigblob, &len) != 0) {
289 ret = SSH_ERR_INVALID_FORMAT;
290 goto out;
291 }
292 if (sshbuf_len(b) != 0) {
293 ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
294 goto out;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100295 }
Ben Lindstromceae9d12002-06-06 20:55:04 +0000296 /* RSA_verify expects a signature of RSA_size */
297 modlen = RSA_size(key->rsa);
298 if (len > modlen) {
Damien Miller86687062014-07-02 15:28:02 +1000299 ret = SSH_ERR_KEY_BITS_MISMATCH;
300 goto out;
Ben Lindstromceae9d12002-06-06 20:55:04 +0000301 } else if (len < modlen) {
Damien Miller86687062014-07-02 15:28:02 +1000302 diff = modlen - len;
303 osigblob = sigblob;
304 if ((sigblob = realloc(sigblob, modlen)) == NULL) {
305 sigblob = osigblob; /* put it back for clear/free */
306 ret = SSH_ERR_ALLOC_FAIL;
307 goto out;
308 }
Ben Lindstromceae9d12002-06-06 20:55:04 +0000309 memmove(sigblob + diff, sigblob, len);
Damien Millera5103f42014-02-04 11:20:14 +1100310 explicit_bzero(sigblob, diff);
Ben Lindstromceae9d12002-06-06 20:55:04 +0000311 len = modlen;
312 }
Damien Millerb3051d02014-01-10 10:58:53 +1100313 if ((dlen = ssh_digest_bytes(hash_alg)) == 0) {
Damien Miller86687062014-07-02 15:28:02 +1000314 ret = SSH_ERR_INTERNAL_ERROR;
315 goto out;
Ben Lindstrom425fb022001-03-29 00:31:20 +0000316 }
Damien Miller86687062014-07-02 15:28:02 +1000317 if ((ret = ssh_digest_memory(hash_alg, data, datalen,
318 digest, sizeof(digest))) != 0)
319 goto out;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100320
Damien Millerb3051d02014-01-10 10:58:53 +1100321 ret = openssh_RSA_verify(hash_alg, digest, dlen, sigblob, len,
322 key->rsa);
Damien Miller86687062014-07-02 15:28:02 +1000323 out:
jsing@openbsd.org17499912018-02-07 05:17:56 +0000324 freezero(sigblob, len);
djm@openbsd.org04c7e282017-12-18 02:25:15 +0000325 free(sigtype);
mmcc@openbsd.org52d70782015-12-11 04:21:11 +0000326 sshbuf_free(b);
Damien Millera5103f42014-02-04 11:20:14 +1100327 explicit_bzero(digest, sizeof(digest));
Damien Miller0bc1bd82000-11-13 22:57:25 +1100328 return ret;
329}
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000330
331/*
332 * See:
333 * http://www.rsasecurity.com/rsalabs/pkcs/pkcs-1/
334 * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1.asn
335 */
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000336
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000337/*
338 * id-sha1 OBJECT IDENTIFIER ::= { iso(1) identified-organization(3)
339 * oiw(14) secsig(3) algorithms(2) 26 }
340 */
341static const u_char id_sha1[] = {
342 0x30, 0x21, /* type Sequence, length 0x21 (33) */
343 0x30, 0x09, /* type Sequence, length 0x09 */
344 0x06, 0x05, /* type OID, length 0x05 */
345 0x2b, 0x0e, 0x03, 0x02, 0x1a, /* id-sha1 OID */
346 0x05, 0x00, /* NULL */
347 0x04, 0x14 /* Octet string, length 0x14 (20), followed by sha1 hash */
348};
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000349
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000350/*
351 * See http://csrc.nist.gov/groups/ST/crypto_apps_infra/csor/algorithms.html
352 * id-sha256 OBJECT IDENTIFIER ::= { joint-iso-itu-t(2) country(16) us(840)
353 * organization(1) gov(101) csor(3) nistAlgorithm(4) hashAlgs(2)
354 * id-sha256(1) }
355 */
356static const u_char id_sha256[] = {
357 0x30, 0x31, /* type Sequence, length 0x31 (49) */
358 0x30, 0x0d, /* type Sequence, length 0x0d (13) */
359 0x06, 0x09, /* type OID, length 0x09 */
360 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, /* id-sha256 */
361 0x05, 0x00, /* NULL */
362 0x04, 0x20 /* Octet string, length 0x20 (32), followed by sha256 hash */
363};
364
365/*
366 * See http://csrc.nist.gov/groups/ST/crypto_apps_infra/csor/algorithms.html
367 * id-sha512 OBJECT IDENTIFIER ::= { joint-iso-itu-t(2) country(16) us(840)
368 * organization(1) gov(101) csor(3) nistAlgorithm(4) hashAlgs(2)
369 * id-sha256(3) }
370 */
371static const u_char id_sha512[] = {
372 0x30, 0x51, /* type Sequence, length 0x51 (81) */
373 0x30, 0x0d, /* type Sequence, length 0x0d (13) */
374 0x06, 0x09, /* type OID, length 0x09 */
375 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, /* id-sha512 */
376 0x05, 0x00, /* NULL */
377 0x04, 0x40 /* Octet string, length 0x40 (64), followed by sha512 hash */
378};
379
380static int
381rsa_hash_alg_oid(int hash_alg, const u_char **oidp, size_t *oidlenp)
382{
383 switch (hash_alg) {
384 case SSH_DIGEST_SHA1:
385 *oidp = id_sha1;
386 *oidlenp = sizeof(id_sha1);
387 break;
388 case SSH_DIGEST_SHA256:
389 *oidp = id_sha256;
390 *oidlenp = sizeof(id_sha256);
391 break;
392 case SSH_DIGEST_SHA512:
393 *oidp = id_sha512;
394 *oidlenp = sizeof(id_sha512);
395 break;
396 default:
397 return SSH_ERR_INVALID_ARGUMENT;
398 }
399 return 0;
400}
401
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000402static int
Damien Miller86687062014-07-02 15:28:02 +1000403openssh_RSA_verify(int hash_alg, u_char *hash, size_t hashlen,
404 u_char *sigbuf, size_t siglen, RSA *rsa)
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000405{
djm@openbsd.org61942ea2015-09-09 00:52:44 +0000406 size_t rsasize = 0, oidlen = 0, hlen = 0;
407 int ret, len, oidmatch, hashmatch;
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000408 const u_char *oid = NULL;
409 u_char *decrypted = NULL;
410
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000411 if ((ret = rsa_hash_alg_oid(hash_alg, &oid, &oidlen)) != 0)
412 return ret;
Damien Miller86687062014-07-02 15:28:02 +1000413 ret = SSH_ERR_INTERNAL_ERROR;
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000414 hlen = ssh_digest_bytes(hash_alg);
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000415 if (hashlen != hlen) {
Damien Miller86687062014-07-02 15:28:02 +1000416 ret = SSH_ERR_INVALID_ARGUMENT;
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000417 goto done;
418 }
419 rsasize = RSA_size(rsa);
Damien Miller86687062014-07-02 15:28:02 +1000420 if (rsasize <= 0 || rsasize > SSHBUF_MAX_BIGNUM ||
421 siglen == 0 || siglen > rsasize) {
422 ret = SSH_ERR_INVALID_ARGUMENT;
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000423 goto done;
424 }
Damien Miller86687062014-07-02 15:28:02 +1000425 if ((decrypted = malloc(rsasize)) == NULL) {
426 ret = SSH_ERR_ALLOC_FAIL;
427 goto done;
428 }
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000429 if ((len = RSA_public_decrypt(siglen, sigbuf, decrypted, rsa,
430 RSA_PKCS1_PADDING)) < 0) {
Damien Miller86687062014-07-02 15:28:02 +1000431 ret = SSH_ERR_LIBCRYPTO_ERROR;
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000432 goto done;
433 }
Damien Miller86687062014-07-02 15:28:02 +1000434 if (len < 0 || (size_t)len != hlen + oidlen) {
435 ret = SSH_ERR_INVALID_FORMAT;
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000436 goto done;
437 }
Damien Miller4e8285e2010-08-03 16:04:03 +1000438 oidmatch = timingsafe_bcmp(decrypted, oid, oidlen) == 0;
439 hashmatch = timingsafe_bcmp(decrypted + oidlen, hash, hlen) == 0;
Damien Miller86687062014-07-02 15:28:02 +1000440 if (!oidmatch || !hashmatch) {
441 ret = SSH_ERR_SIGNATURE_INVALID;
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000442 goto done;
443 }
Damien Miller86687062014-07-02 15:28:02 +1000444 ret = 0;
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000445done:
jsing@openbsd.org17499912018-02-07 05:17:56 +0000446 freezero(decrypted, rsasize);
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000447 return ret;
448}
Damien Miller72ef7c12015-01-15 02:21:31 +1100449#endif /* WITH_OPENSSL */