blob: 49e71c87f64b2ae07b1464d56fd78b0c53c17994 [file] [log] [blame]
jsing@openbsd.orgd2b3db22018-02-14 16:27:24 +00001/* $OpenBSD: ssh-rsa.c,v 1.66 2018/02/14 16:27:24 jsing 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 Miller86687062014-07-02 15:28:02 +100038static int openssh_RSA_verify(int, u_char *, size_t, u_char *, size_t, RSA *);
Ben Lindstrom0deb5d92002-08-20 18:40:03 +000039
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +000040static const char *
41rsa_hash_alg_ident(int hash_alg)
42{
43 switch (hash_alg) {
44 case SSH_DIGEST_SHA1:
45 return "ssh-rsa";
46 case SSH_DIGEST_SHA256:
47 return "rsa-sha2-256";
48 case SSH_DIGEST_SHA512:
49 return "rsa-sha2-512";
50 }
51 return NULL;
52}
53
54static int
55rsa_hash_alg_from_ident(const char *ident)
56{
djm@openbsd.org445e2182016-09-12 23:39:34 +000057 if (strcmp(ident, "ssh-rsa") == 0 ||
58 strcmp(ident, "ssh-rsa-cert-v01@openssh.com") == 0)
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +000059 return SSH_DIGEST_SHA1;
60 if (strcmp(ident, "rsa-sha2-256") == 0)
61 return SSH_DIGEST_SHA256;
62 if (strcmp(ident, "rsa-sha2-512") == 0)
63 return SSH_DIGEST_SHA512;
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +000064 return -1;
65}
66
67static int
68rsa_hash_alg_nid(int type)
69{
70 switch (type) {
71 case SSH_DIGEST_SHA1:
72 return NID_sha1;
73 case SSH_DIGEST_SHA256:
74 return NID_sha256;
75 case SSH_DIGEST_SHA512:
76 return NID_sha512;
77 default:
78 return -1;
79 }
80}
81
djm@openbsd.org83fa3a02017-07-01 13:50:45 +000082int
83ssh_rsa_generate_additional_parameters(struct sshkey *key)
84{
djm@openbsd.org83fa3a02017-07-01 13:50:45 +000085 BIGNUM *aux = NULL;
86 BN_CTX *ctx = NULL;
jsing@openbsd.orgd2b3db22018-02-14 16:27:24 +000087 BIGNUM d;
djm@openbsd.org83fa3a02017-07-01 13:50:45 +000088 int r;
89
90 if (key == NULL || key->rsa == NULL ||
91 sshkey_type_plain(key->type) != KEY_RSA)
92 return SSH_ERR_INVALID_ARGUMENT;
93
94 if ((ctx = BN_CTX_new()) == NULL)
95 return SSH_ERR_ALLOC_FAIL;
96 if ((aux = BN_new()) == NULL) {
97 r = SSH_ERR_ALLOC_FAIL;
98 goto out;
99 }
jsing@openbsd.orgd2b3db22018-02-14 16:27:24 +0000100 BN_set_flags(aux, BN_FLG_CONSTTIME);
djm@openbsd.org83fa3a02017-07-01 13:50:45 +0000101
jsing@openbsd.orgd2b3db22018-02-14 16:27:24 +0000102 BN_init(&d);
103 BN_with_flags(&d, key->rsa->d, BN_FLG_CONSTTIME);
104
105 if ((BN_sub(aux, key->rsa->q, BN_value_one()) == 0) ||
106 (BN_mod(key->rsa->dmq1, &d, aux, ctx) == 0) ||
107 (BN_sub(aux, key->rsa->p, BN_value_one()) == 0) ||
108 (BN_mod(key->rsa->dmp1, &d, aux, ctx) == 0)) {
djm@openbsd.org83fa3a02017-07-01 13:50:45 +0000109 r = SSH_ERR_LIBCRYPTO_ERROR;
110 goto out;
111 }
112 r = 0;
113 out:
114 BN_clear_free(aux);
115 BN_CTX_free(ctx);
116 return r;
117}
118
Damien Miller0bc1bd82000-11-13 22:57:25 +1100119/* RSASSA-PKCS1-v1_5 (PKCS #1 v2.0 signature) with SHA1 */
120int
Damien Miller86687062014-07-02 15:28:02 +1000121ssh_rsa_sign(const struct sshkey *key, u_char **sigp, size_t *lenp,
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000122 const u_char *data, size_t datalen, const char *alg_ident)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100123{
Damien Miller86687062014-07-02 15:28:02 +1000124 u_char digest[SSH_DIGEST_MAX_LENGTH], *sig = NULL;
jsing@openbsd.org17499912018-02-07 05:17:56 +0000125 size_t slen = 0;
Damien Miller86687062014-07-02 15:28:02 +1000126 u_int dlen, len;
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000127 int nid, hash_alg, ret = SSH_ERR_INTERNAL_ERROR;
Damien Miller86687062014-07-02 15:28:02 +1000128 struct sshbuf *b = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100129
Damien Miller86687062014-07-02 15:28:02 +1000130 if (lenp != NULL)
131 *lenp = 0;
132 if (sigp != NULL)
133 *sigp = NULL;
134
djm@openbsd.org445e2182016-09-12 23:39:34 +0000135 if (alg_ident == NULL || strlen(alg_ident) == 0)
markus@openbsd.org6262a052015-12-07 20:04:09 +0000136 hash_alg = SSH_DIGEST_SHA1;
137 else
138 hash_alg = rsa_hash_alg_from_ident(alg_ident);
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000139 if (key == NULL || key->rsa == NULL || hash_alg == -1 ||
djm@openbsd.orgbd636f42017-05-07 23:15:59 +0000140 sshkey_type_plain(key->type) != KEY_RSA)
Damien Miller86687062014-07-02 15:28:02 +1000141 return SSH_ERR_INVALID_ARGUMENT;
djm@openbsd.orgbd636f42017-05-07 23:15:59 +0000142 if (BN_num_bits(key->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE)
143 return SSH_ERR_KEY_LENGTH;
Damien Miller86687062014-07-02 15:28:02 +1000144 slen = RSA_size(key->rsa);
145 if (slen <= 0 || slen > SSHBUF_MAX_BIGNUM)
146 return SSH_ERR_INVALID_ARGUMENT;
Damien Miller3e192952013-12-29 17:47:50 +1100147
Damien Millerb3051d02014-01-10 10:58:53 +1100148 /* hash the data */
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000149 nid = rsa_hash_alg_nid(hash_alg);
Damien Miller86687062014-07-02 15:28:02 +1000150 if ((dlen = ssh_digest_bytes(hash_alg)) == 0)
151 return SSH_ERR_INTERNAL_ERROR;
152 if ((ret = ssh_digest_memory(hash_alg, data, datalen,
153 digest, sizeof(digest))) != 0)
154 goto out;
155
156 if ((sig = malloc(slen)) == NULL) {
157 ret = SSH_ERR_ALLOC_FAIL;
158 goto out;
Damien Millerb3051d02014-01-10 10:58:53 +1100159 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100160
Damien Miller86687062014-07-02 15:28:02 +1000161 if (RSA_sign(nid, digest, dlen, sig, &len, key->rsa) != 1) {
162 ret = SSH_ERR_LIBCRYPTO_ERROR;
163 goto out;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100164 }
165 if (len < slen) {
Damien Miller86687062014-07-02 15:28:02 +1000166 size_t diff = slen - len;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100167 memmove(sig + diff, sig, len);
Damien Millera5103f42014-02-04 11:20:14 +1100168 explicit_bzero(sig, diff);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100169 } else if (len > slen) {
Damien Miller86687062014-07-02 15:28:02 +1000170 ret = SSH_ERR_INTERNAL_ERROR;
171 goto out;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100172 }
173 /* encode signature */
Damien Miller86687062014-07-02 15:28:02 +1000174 if ((b = sshbuf_new()) == NULL) {
175 ret = SSH_ERR_ALLOC_FAIL;
176 goto out;
177 }
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000178 if ((ret = sshbuf_put_cstring(b, rsa_hash_alg_ident(hash_alg))) != 0 ||
Damien Miller86687062014-07-02 15:28:02 +1000179 (ret = sshbuf_put_string(b, sig, slen)) != 0)
180 goto out;
181 len = sshbuf_len(b);
182 if (sigp != NULL) {
183 if ((*sigp = malloc(len)) == NULL) {
184 ret = SSH_ERR_ALLOC_FAIL;
185 goto out;
186 }
187 memcpy(*sigp, sshbuf_ptr(b), len);
188 }
Ben Lindstrom2bf759c2002-07-07 22:13:31 +0000189 if (lenp != NULL)
190 *lenp = len;
Damien Miller86687062014-07-02 15:28:02 +1000191 ret = 0;
192 out:
193 explicit_bzero(digest, sizeof(digest));
jsing@openbsd.org17499912018-02-07 05:17:56 +0000194 freezero(sig, slen);
mmcc@openbsd.org52d70782015-12-11 04:21:11 +0000195 sshbuf_free(b);
djm@openbsd.org4ef702e2015-06-15 01:32:50 +0000196 return ret;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100197}
198
199int
Damien Miller86687062014-07-02 15:28:02 +1000200ssh_rsa_verify(const struct sshkey *key,
djm@openbsd.org04c7e282017-12-18 02:25:15 +0000201 const u_char *sig, size_t siglen, const u_char *data, size_t datalen,
202 const char *alg)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100203{
djm@openbsd.org04c7e282017-12-18 02:25:15 +0000204 char *sigtype = NULL;
Damien Miller86687062014-07-02 15:28:02 +1000205 int hash_alg, ret = SSH_ERR_INTERNAL_ERROR;
jsing@openbsd.org17499912018-02-07 05:17:56 +0000206 size_t len = 0, diff, modlen, dlen;
Damien Miller86687062014-07-02 15:28:02 +1000207 struct sshbuf *b = NULL;
208 u_char digest[SSH_DIGEST_MAX_LENGTH], *osigblob, *sigblob = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100209
Damien Miller86687062014-07-02 15:28:02 +1000210 if (key == NULL || key->rsa == NULL ||
211 sshkey_type_plain(key->type) != KEY_RSA ||
djm@openbsd.orgb6e01402016-04-21 06:08:02 +0000212 sig == NULL || siglen == 0)
Damien Miller86687062014-07-02 15:28:02 +1000213 return SSH_ERR_INVALID_ARGUMENT;
djm@openbsd.orgbd636f42017-05-07 23:15:59 +0000214 if (BN_num_bits(key->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE)
215 return SSH_ERR_KEY_LENGTH;
Damien Miller3e192952013-12-29 17:47:50 +1100216
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000217 if ((b = sshbuf_from(sig, siglen)) == NULL)
Damien Miller86687062014-07-02 15:28:02 +1000218 return SSH_ERR_ALLOC_FAIL;
djm@openbsd.org04c7e282017-12-18 02:25:15 +0000219 if (sshbuf_get_cstring(b, &sigtype, NULL) != 0) {
Damien Miller86687062014-07-02 15:28:02 +1000220 ret = SSH_ERR_INVALID_FORMAT;
221 goto out;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000222 }
djm@openbsd.org04c7e282017-12-18 02:25:15 +0000223 /* XXX djm: need cert types that reliably yield SHA-2 signatures */
224 if (alg != NULL && strcmp(alg, sigtype) != 0 &&
225 strcmp(alg, "ssh-rsa-cert-v01@openssh.com") != 0) {
djm@openbsd.org966ef472017-12-18 23:14:34 +0000226 error("%s: RSA signature type mismatch: "
227 "expected %s received %s", __func__, alg, sigtype);
djm@openbsd.org04c7e282017-12-18 02:25:15 +0000228 ret = SSH_ERR_SIGNATURE_INVALID;
229 goto out;
230 }
231 if ((hash_alg = rsa_hash_alg_from_ident(sigtype)) == -1) {
Damien Miller86687062014-07-02 15:28:02 +1000232 ret = SSH_ERR_KEY_TYPE_MISMATCH;
233 goto out;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100234 }
Damien Miller86687062014-07-02 15:28:02 +1000235 if (sshbuf_get_string(b, &sigblob, &len) != 0) {
236 ret = SSH_ERR_INVALID_FORMAT;
237 goto out;
238 }
239 if (sshbuf_len(b) != 0) {
240 ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
241 goto out;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100242 }
Ben Lindstromceae9d12002-06-06 20:55:04 +0000243 /* RSA_verify expects a signature of RSA_size */
244 modlen = RSA_size(key->rsa);
245 if (len > modlen) {
Damien Miller86687062014-07-02 15:28:02 +1000246 ret = SSH_ERR_KEY_BITS_MISMATCH;
247 goto out;
Ben Lindstromceae9d12002-06-06 20:55:04 +0000248 } else if (len < modlen) {
Damien Miller86687062014-07-02 15:28:02 +1000249 diff = modlen - len;
250 osigblob = sigblob;
251 if ((sigblob = realloc(sigblob, modlen)) == NULL) {
252 sigblob = osigblob; /* put it back for clear/free */
253 ret = SSH_ERR_ALLOC_FAIL;
254 goto out;
255 }
Ben Lindstromceae9d12002-06-06 20:55:04 +0000256 memmove(sigblob + diff, sigblob, len);
Damien Millera5103f42014-02-04 11:20:14 +1100257 explicit_bzero(sigblob, diff);
Ben Lindstromceae9d12002-06-06 20:55:04 +0000258 len = modlen;
259 }
Damien Millerb3051d02014-01-10 10:58:53 +1100260 if ((dlen = ssh_digest_bytes(hash_alg)) == 0) {
Damien Miller86687062014-07-02 15:28:02 +1000261 ret = SSH_ERR_INTERNAL_ERROR;
262 goto out;
Ben Lindstrom425fb022001-03-29 00:31:20 +0000263 }
Damien Miller86687062014-07-02 15:28:02 +1000264 if ((ret = ssh_digest_memory(hash_alg, data, datalen,
265 digest, sizeof(digest))) != 0)
266 goto out;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100267
Damien Millerb3051d02014-01-10 10:58:53 +1100268 ret = openssh_RSA_verify(hash_alg, digest, dlen, sigblob, len,
269 key->rsa);
Damien Miller86687062014-07-02 15:28:02 +1000270 out:
jsing@openbsd.org17499912018-02-07 05:17:56 +0000271 freezero(sigblob, len);
djm@openbsd.org04c7e282017-12-18 02:25:15 +0000272 free(sigtype);
mmcc@openbsd.org52d70782015-12-11 04:21:11 +0000273 sshbuf_free(b);
Damien Millera5103f42014-02-04 11:20:14 +1100274 explicit_bzero(digest, sizeof(digest));
Damien Miller0bc1bd82000-11-13 22:57:25 +1100275 return ret;
276}
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000277
278/*
279 * See:
280 * http://www.rsasecurity.com/rsalabs/pkcs/pkcs-1/
281 * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1.asn
282 */
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000283
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000284/*
285 * id-sha1 OBJECT IDENTIFIER ::= { iso(1) identified-organization(3)
286 * oiw(14) secsig(3) algorithms(2) 26 }
287 */
288static const u_char id_sha1[] = {
289 0x30, 0x21, /* type Sequence, length 0x21 (33) */
290 0x30, 0x09, /* type Sequence, length 0x09 */
291 0x06, 0x05, /* type OID, length 0x05 */
292 0x2b, 0x0e, 0x03, 0x02, 0x1a, /* id-sha1 OID */
293 0x05, 0x00, /* NULL */
294 0x04, 0x14 /* Octet string, length 0x14 (20), followed by sha1 hash */
295};
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000296
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000297/*
298 * See http://csrc.nist.gov/groups/ST/crypto_apps_infra/csor/algorithms.html
299 * id-sha256 OBJECT IDENTIFIER ::= { joint-iso-itu-t(2) country(16) us(840)
300 * organization(1) gov(101) csor(3) nistAlgorithm(4) hashAlgs(2)
301 * id-sha256(1) }
302 */
303static const u_char id_sha256[] = {
304 0x30, 0x31, /* type Sequence, length 0x31 (49) */
305 0x30, 0x0d, /* type Sequence, length 0x0d (13) */
306 0x06, 0x09, /* type OID, length 0x09 */
307 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, /* id-sha256 */
308 0x05, 0x00, /* NULL */
309 0x04, 0x20 /* Octet string, length 0x20 (32), followed by sha256 hash */
310};
311
312/*
313 * See http://csrc.nist.gov/groups/ST/crypto_apps_infra/csor/algorithms.html
314 * id-sha512 OBJECT IDENTIFIER ::= { joint-iso-itu-t(2) country(16) us(840)
315 * organization(1) gov(101) csor(3) nistAlgorithm(4) hashAlgs(2)
316 * id-sha256(3) }
317 */
318static const u_char id_sha512[] = {
319 0x30, 0x51, /* type Sequence, length 0x51 (81) */
320 0x30, 0x0d, /* type Sequence, length 0x0d (13) */
321 0x06, 0x09, /* type OID, length 0x09 */
322 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, /* id-sha512 */
323 0x05, 0x00, /* NULL */
324 0x04, 0x40 /* Octet string, length 0x40 (64), followed by sha512 hash */
325};
326
327static int
328rsa_hash_alg_oid(int hash_alg, const u_char **oidp, size_t *oidlenp)
329{
330 switch (hash_alg) {
331 case SSH_DIGEST_SHA1:
332 *oidp = id_sha1;
333 *oidlenp = sizeof(id_sha1);
334 break;
335 case SSH_DIGEST_SHA256:
336 *oidp = id_sha256;
337 *oidlenp = sizeof(id_sha256);
338 break;
339 case SSH_DIGEST_SHA512:
340 *oidp = id_sha512;
341 *oidlenp = sizeof(id_sha512);
342 break;
343 default:
344 return SSH_ERR_INVALID_ARGUMENT;
345 }
346 return 0;
347}
348
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000349static int
Damien Miller86687062014-07-02 15:28:02 +1000350openssh_RSA_verify(int hash_alg, u_char *hash, size_t hashlen,
351 u_char *sigbuf, size_t siglen, RSA *rsa)
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000352{
djm@openbsd.org61942ea2015-09-09 00:52:44 +0000353 size_t rsasize = 0, oidlen = 0, hlen = 0;
354 int ret, len, oidmatch, hashmatch;
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000355 const u_char *oid = NULL;
356 u_char *decrypted = NULL;
357
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000358 if ((ret = rsa_hash_alg_oid(hash_alg, &oid, &oidlen)) != 0)
359 return ret;
Damien Miller86687062014-07-02 15:28:02 +1000360 ret = SSH_ERR_INTERNAL_ERROR;
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000361 hlen = ssh_digest_bytes(hash_alg);
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000362 if (hashlen != hlen) {
Damien Miller86687062014-07-02 15:28:02 +1000363 ret = SSH_ERR_INVALID_ARGUMENT;
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000364 goto done;
365 }
366 rsasize = RSA_size(rsa);
Damien Miller86687062014-07-02 15:28:02 +1000367 if (rsasize <= 0 || rsasize > SSHBUF_MAX_BIGNUM ||
368 siglen == 0 || siglen > rsasize) {
369 ret = SSH_ERR_INVALID_ARGUMENT;
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000370 goto done;
371 }
Damien Miller86687062014-07-02 15:28:02 +1000372 if ((decrypted = malloc(rsasize)) == NULL) {
373 ret = SSH_ERR_ALLOC_FAIL;
374 goto done;
375 }
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000376 if ((len = RSA_public_decrypt(siglen, sigbuf, decrypted, rsa,
377 RSA_PKCS1_PADDING)) < 0) {
Damien Miller86687062014-07-02 15:28:02 +1000378 ret = SSH_ERR_LIBCRYPTO_ERROR;
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000379 goto done;
380 }
Damien Miller86687062014-07-02 15:28:02 +1000381 if (len < 0 || (size_t)len != hlen + oidlen) {
382 ret = SSH_ERR_INVALID_FORMAT;
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000383 goto done;
384 }
Damien Miller4e8285e2010-08-03 16:04:03 +1000385 oidmatch = timingsafe_bcmp(decrypted, oid, oidlen) == 0;
386 hashmatch = timingsafe_bcmp(decrypted + oidlen, hash, hlen) == 0;
Damien Miller86687062014-07-02 15:28:02 +1000387 if (!oidmatch || !hashmatch) {
388 ret = SSH_ERR_SIGNATURE_INVALID;
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000389 goto done;
390 }
Damien Miller86687062014-07-02 15:28:02 +1000391 ret = 0;
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000392done:
jsing@openbsd.org17499912018-02-07 05:17:56 +0000393 freezero(decrypted, rsasize);
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000394 return ret;
395}
Damien Miller72ef7c12015-01-15 02:21:31 +1100396#endif /* WITH_OPENSSL */