blob: 89e3c8c74c3f15e223baba0d35db46451983e981 [file] [log] [blame]
jsing@openbsd.org17499912018-02-07 05:17:56 +00001/* $OpenBSD: ssh-rsa.c,v 1.65 2018/02/07 05:17:56 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 +000082/* calculate p-1 and q-1 */
83int
84ssh_rsa_generate_additional_parameters(struct sshkey *key)
85{
86 RSA *rsa;
87 BIGNUM *aux = NULL;
88 BN_CTX *ctx = NULL;
89 int r;
90
91 if (key == NULL || key->rsa == NULL ||
92 sshkey_type_plain(key->type) != KEY_RSA)
93 return SSH_ERR_INVALID_ARGUMENT;
94
95 if ((ctx = BN_CTX_new()) == NULL)
96 return SSH_ERR_ALLOC_FAIL;
97 if ((aux = BN_new()) == NULL) {
98 r = SSH_ERR_ALLOC_FAIL;
99 goto out;
100 }
101 rsa = key->rsa;
102
103 if ((BN_sub(aux, rsa->q, BN_value_one()) == 0) ||
104 (BN_mod(rsa->dmq1, rsa->d, aux, ctx) == 0) ||
105 (BN_sub(aux, rsa->p, BN_value_one()) == 0) ||
106 (BN_mod(rsa->dmp1, rsa->d, aux, ctx) == 0)) {
107 r = SSH_ERR_LIBCRYPTO_ERROR;
108 goto out;
109 }
110 r = 0;
111 out:
112 BN_clear_free(aux);
113 BN_CTX_free(ctx);
114 return r;
115}
116
Damien Miller0bc1bd82000-11-13 22:57:25 +1100117/* RSASSA-PKCS1-v1_5 (PKCS #1 v2.0 signature) with SHA1 */
118int
Damien Miller86687062014-07-02 15:28:02 +1000119ssh_rsa_sign(const struct sshkey *key, u_char **sigp, size_t *lenp,
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000120 const u_char *data, size_t datalen, const char *alg_ident)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100121{
Damien Miller86687062014-07-02 15:28:02 +1000122 u_char digest[SSH_DIGEST_MAX_LENGTH], *sig = NULL;
jsing@openbsd.org17499912018-02-07 05:17:56 +0000123 size_t slen = 0;
Damien Miller86687062014-07-02 15:28:02 +1000124 u_int dlen, len;
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000125 int nid, hash_alg, ret = SSH_ERR_INTERNAL_ERROR;
Damien Miller86687062014-07-02 15:28:02 +1000126 struct sshbuf *b = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100127
Damien Miller86687062014-07-02 15:28:02 +1000128 if (lenp != NULL)
129 *lenp = 0;
130 if (sigp != NULL)
131 *sigp = NULL;
132
djm@openbsd.org445e2182016-09-12 23:39:34 +0000133 if (alg_ident == NULL || strlen(alg_ident) == 0)
markus@openbsd.org6262a052015-12-07 20:04:09 +0000134 hash_alg = SSH_DIGEST_SHA1;
135 else
136 hash_alg = rsa_hash_alg_from_ident(alg_ident);
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000137 if (key == NULL || key->rsa == NULL || hash_alg == -1 ||
djm@openbsd.orgbd636f42017-05-07 23:15:59 +0000138 sshkey_type_plain(key->type) != KEY_RSA)
Damien Miller86687062014-07-02 15:28:02 +1000139 return SSH_ERR_INVALID_ARGUMENT;
djm@openbsd.orgbd636f42017-05-07 23:15:59 +0000140 if (BN_num_bits(key->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE)
141 return SSH_ERR_KEY_LENGTH;
Damien Miller86687062014-07-02 15:28:02 +1000142 slen = RSA_size(key->rsa);
143 if (slen <= 0 || slen > SSHBUF_MAX_BIGNUM)
144 return SSH_ERR_INVALID_ARGUMENT;
Damien Miller3e192952013-12-29 17:47:50 +1100145
Damien Millerb3051d02014-01-10 10:58:53 +1100146 /* hash the data */
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000147 nid = rsa_hash_alg_nid(hash_alg);
Damien Miller86687062014-07-02 15:28:02 +1000148 if ((dlen = ssh_digest_bytes(hash_alg)) == 0)
149 return SSH_ERR_INTERNAL_ERROR;
150 if ((ret = ssh_digest_memory(hash_alg, data, datalen,
151 digest, sizeof(digest))) != 0)
152 goto out;
153
154 if ((sig = malloc(slen)) == NULL) {
155 ret = SSH_ERR_ALLOC_FAIL;
156 goto out;
Damien Millerb3051d02014-01-10 10:58:53 +1100157 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100158
Damien Miller86687062014-07-02 15:28:02 +1000159 if (RSA_sign(nid, digest, dlen, sig, &len, key->rsa) != 1) {
160 ret = SSH_ERR_LIBCRYPTO_ERROR;
161 goto out;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100162 }
163 if (len < slen) {
Damien Miller86687062014-07-02 15:28:02 +1000164 size_t diff = slen - len;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100165 memmove(sig + diff, sig, len);
Damien Millera5103f42014-02-04 11:20:14 +1100166 explicit_bzero(sig, diff);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100167 } else if (len > slen) {
Damien Miller86687062014-07-02 15:28:02 +1000168 ret = SSH_ERR_INTERNAL_ERROR;
169 goto out;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100170 }
171 /* encode signature */
Damien Miller86687062014-07-02 15:28:02 +1000172 if ((b = sshbuf_new()) == NULL) {
173 ret = SSH_ERR_ALLOC_FAIL;
174 goto out;
175 }
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000176 if ((ret = sshbuf_put_cstring(b, rsa_hash_alg_ident(hash_alg))) != 0 ||
Damien Miller86687062014-07-02 15:28:02 +1000177 (ret = sshbuf_put_string(b, sig, slen)) != 0)
178 goto out;
179 len = sshbuf_len(b);
180 if (sigp != NULL) {
181 if ((*sigp = malloc(len)) == NULL) {
182 ret = SSH_ERR_ALLOC_FAIL;
183 goto out;
184 }
185 memcpy(*sigp, sshbuf_ptr(b), len);
186 }
Ben Lindstrom2bf759c2002-07-07 22:13:31 +0000187 if (lenp != NULL)
188 *lenp = len;
Damien Miller86687062014-07-02 15:28:02 +1000189 ret = 0;
190 out:
191 explicit_bzero(digest, sizeof(digest));
jsing@openbsd.org17499912018-02-07 05:17:56 +0000192 freezero(sig, slen);
mmcc@openbsd.org52d70782015-12-11 04:21:11 +0000193 sshbuf_free(b);
djm@openbsd.org4ef702e2015-06-15 01:32:50 +0000194 return ret;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100195}
196
197int
Damien Miller86687062014-07-02 15:28:02 +1000198ssh_rsa_verify(const struct sshkey *key,
djm@openbsd.org04c7e282017-12-18 02:25:15 +0000199 const u_char *sig, size_t siglen, const u_char *data, size_t datalen,
200 const char *alg)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100201{
djm@openbsd.org04c7e282017-12-18 02:25:15 +0000202 char *sigtype = NULL;
Damien Miller86687062014-07-02 15:28:02 +1000203 int hash_alg, ret = SSH_ERR_INTERNAL_ERROR;
jsing@openbsd.org17499912018-02-07 05:17:56 +0000204 size_t len = 0, diff, modlen, dlen;
Damien Miller86687062014-07-02 15:28:02 +1000205 struct sshbuf *b = NULL;
206 u_char digest[SSH_DIGEST_MAX_LENGTH], *osigblob, *sigblob = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100207
Damien Miller86687062014-07-02 15:28:02 +1000208 if (key == NULL || key->rsa == NULL ||
209 sshkey_type_plain(key->type) != KEY_RSA ||
djm@openbsd.orgb6e01402016-04-21 06:08:02 +0000210 sig == NULL || siglen == 0)
Damien Miller86687062014-07-02 15:28:02 +1000211 return SSH_ERR_INVALID_ARGUMENT;
djm@openbsd.orgbd636f42017-05-07 23:15:59 +0000212 if (BN_num_bits(key->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE)
213 return SSH_ERR_KEY_LENGTH;
Damien Miller3e192952013-12-29 17:47:50 +1100214
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000215 if ((b = sshbuf_from(sig, siglen)) == NULL)
Damien Miller86687062014-07-02 15:28:02 +1000216 return SSH_ERR_ALLOC_FAIL;
djm@openbsd.org04c7e282017-12-18 02:25:15 +0000217 if (sshbuf_get_cstring(b, &sigtype, NULL) != 0) {
Damien Miller86687062014-07-02 15:28:02 +1000218 ret = SSH_ERR_INVALID_FORMAT;
219 goto out;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000220 }
djm@openbsd.org04c7e282017-12-18 02:25:15 +0000221 /* XXX djm: need cert types that reliably yield SHA-2 signatures */
222 if (alg != NULL && strcmp(alg, sigtype) != 0 &&
223 strcmp(alg, "ssh-rsa-cert-v01@openssh.com") != 0) {
djm@openbsd.org966ef472017-12-18 23:14:34 +0000224 error("%s: RSA signature type mismatch: "
225 "expected %s received %s", __func__, alg, sigtype);
djm@openbsd.org04c7e282017-12-18 02:25:15 +0000226 ret = SSH_ERR_SIGNATURE_INVALID;
227 goto out;
228 }
229 if ((hash_alg = rsa_hash_alg_from_ident(sigtype)) == -1) {
Damien Miller86687062014-07-02 15:28:02 +1000230 ret = SSH_ERR_KEY_TYPE_MISMATCH;
231 goto out;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100232 }
Damien Miller86687062014-07-02 15:28:02 +1000233 if (sshbuf_get_string(b, &sigblob, &len) != 0) {
234 ret = SSH_ERR_INVALID_FORMAT;
235 goto out;
236 }
237 if (sshbuf_len(b) != 0) {
238 ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
239 goto out;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100240 }
Ben Lindstromceae9d12002-06-06 20:55:04 +0000241 /* RSA_verify expects a signature of RSA_size */
242 modlen = RSA_size(key->rsa);
243 if (len > modlen) {
Damien Miller86687062014-07-02 15:28:02 +1000244 ret = SSH_ERR_KEY_BITS_MISMATCH;
245 goto out;
Ben Lindstromceae9d12002-06-06 20:55:04 +0000246 } else if (len < modlen) {
Damien Miller86687062014-07-02 15:28:02 +1000247 diff = modlen - len;
248 osigblob = sigblob;
249 if ((sigblob = realloc(sigblob, modlen)) == NULL) {
250 sigblob = osigblob; /* put it back for clear/free */
251 ret = SSH_ERR_ALLOC_FAIL;
252 goto out;
253 }
Ben Lindstromceae9d12002-06-06 20:55:04 +0000254 memmove(sigblob + diff, sigblob, len);
Damien Millera5103f42014-02-04 11:20:14 +1100255 explicit_bzero(sigblob, diff);
Ben Lindstromceae9d12002-06-06 20:55:04 +0000256 len = modlen;
257 }
Damien Millerb3051d02014-01-10 10:58:53 +1100258 if ((dlen = ssh_digest_bytes(hash_alg)) == 0) {
Damien Miller86687062014-07-02 15:28:02 +1000259 ret = SSH_ERR_INTERNAL_ERROR;
260 goto out;
Ben Lindstrom425fb022001-03-29 00:31:20 +0000261 }
Damien Miller86687062014-07-02 15:28:02 +1000262 if ((ret = ssh_digest_memory(hash_alg, data, datalen,
263 digest, sizeof(digest))) != 0)
264 goto out;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100265
Damien Millerb3051d02014-01-10 10:58:53 +1100266 ret = openssh_RSA_verify(hash_alg, digest, dlen, sigblob, len,
267 key->rsa);
Damien Miller86687062014-07-02 15:28:02 +1000268 out:
jsing@openbsd.org17499912018-02-07 05:17:56 +0000269 freezero(sigblob, len);
djm@openbsd.org04c7e282017-12-18 02:25:15 +0000270 free(sigtype);
mmcc@openbsd.org52d70782015-12-11 04:21:11 +0000271 sshbuf_free(b);
Damien Millera5103f42014-02-04 11:20:14 +1100272 explicit_bzero(digest, sizeof(digest));
Damien Miller0bc1bd82000-11-13 22:57:25 +1100273 return ret;
274}
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000275
276/*
277 * See:
278 * http://www.rsasecurity.com/rsalabs/pkcs/pkcs-1/
279 * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1.asn
280 */
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000281
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000282/*
283 * id-sha1 OBJECT IDENTIFIER ::= { iso(1) identified-organization(3)
284 * oiw(14) secsig(3) algorithms(2) 26 }
285 */
286static const u_char id_sha1[] = {
287 0x30, 0x21, /* type Sequence, length 0x21 (33) */
288 0x30, 0x09, /* type Sequence, length 0x09 */
289 0x06, 0x05, /* type OID, length 0x05 */
290 0x2b, 0x0e, 0x03, 0x02, 0x1a, /* id-sha1 OID */
291 0x05, 0x00, /* NULL */
292 0x04, 0x14 /* Octet string, length 0x14 (20), followed by sha1 hash */
293};
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000294
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000295/*
296 * See http://csrc.nist.gov/groups/ST/crypto_apps_infra/csor/algorithms.html
297 * id-sha256 OBJECT IDENTIFIER ::= { joint-iso-itu-t(2) country(16) us(840)
298 * organization(1) gov(101) csor(3) nistAlgorithm(4) hashAlgs(2)
299 * id-sha256(1) }
300 */
301static const u_char id_sha256[] = {
302 0x30, 0x31, /* type Sequence, length 0x31 (49) */
303 0x30, 0x0d, /* type Sequence, length 0x0d (13) */
304 0x06, 0x09, /* type OID, length 0x09 */
305 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, /* id-sha256 */
306 0x05, 0x00, /* NULL */
307 0x04, 0x20 /* Octet string, length 0x20 (32), followed by sha256 hash */
308};
309
310/*
311 * See http://csrc.nist.gov/groups/ST/crypto_apps_infra/csor/algorithms.html
312 * id-sha512 OBJECT IDENTIFIER ::= { joint-iso-itu-t(2) country(16) us(840)
313 * organization(1) gov(101) csor(3) nistAlgorithm(4) hashAlgs(2)
314 * id-sha256(3) }
315 */
316static const u_char id_sha512[] = {
317 0x30, 0x51, /* type Sequence, length 0x51 (81) */
318 0x30, 0x0d, /* type Sequence, length 0x0d (13) */
319 0x06, 0x09, /* type OID, length 0x09 */
320 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, /* id-sha512 */
321 0x05, 0x00, /* NULL */
322 0x04, 0x40 /* Octet string, length 0x40 (64), followed by sha512 hash */
323};
324
325static int
326rsa_hash_alg_oid(int hash_alg, const u_char **oidp, size_t *oidlenp)
327{
328 switch (hash_alg) {
329 case SSH_DIGEST_SHA1:
330 *oidp = id_sha1;
331 *oidlenp = sizeof(id_sha1);
332 break;
333 case SSH_DIGEST_SHA256:
334 *oidp = id_sha256;
335 *oidlenp = sizeof(id_sha256);
336 break;
337 case SSH_DIGEST_SHA512:
338 *oidp = id_sha512;
339 *oidlenp = sizeof(id_sha512);
340 break;
341 default:
342 return SSH_ERR_INVALID_ARGUMENT;
343 }
344 return 0;
345}
346
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000347static int
Damien Miller86687062014-07-02 15:28:02 +1000348openssh_RSA_verify(int hash_alg, u_char *hash, size_t hashlen,
349 u_char *sigbuf, size_t siglen, RSA *rsa)
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000350{
djm@openbsd.org61942ea2015-09-09 00:52:44 +0000351 size_t rsasize = 0, oidlen = 0, hlen = 0;
352 int ret, len, oidmatch, hashmatch;
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000353 const u_char *oid = NULL;
354 u_char *decrypted = NULL;
355
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000356 if ((ret = rsa_hash_alg_oid(hash_alg, &oid, &oidlen)) != 0)
357 return ret;
Damien Miller86687062014-07-02 15:28:02 +1000358 ret = SSH_ERR_INTERNAL_ERROR;
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000359 hlen = ssh_digest_bytes(hash_alg);
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000360 if (hashlen != hlen) {
Damien Miller86687062014-07-02 15:28:02 +1000361 ret = SSH_ERR_INVALID_ARGUMENT;
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000362 goto done;
363 }
364 rsasize = RSA_size(rsa);
Damien Miller86687062014-07-02 15:28:02 +1000365 if (rsasize <= 0 || rsasize > SSHBUF_MAX_BIGNUM ||
366 siglen == 0 || siglen > rsasize) {
367 ret = SSH_ERR_INVALID_ARGUMENT;
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000368 goto done;
369 }
Damien Miller86687062014-07-02 15:28:02 +1000370 if ((decrypted = malloc(rsasize)) == NULL) {
371 ret = SSH_ERR_ALLOC_FAIL;
372 goto done;
373 }
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000374 if ((len = RSA_public_decrypt(siglen, sigbuf, decrypted, rsa,
375 RSA_PKCS1_PADDING)) < 0) {
Damien Miller86687062014-07-02 15:28:02 +1000376 ret = SSH_ERR_LIBCRYPTO_ERROR;
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000377 goto done;
378 }
Damien Miller86687062014-07-02 15:28:02 +1000379 if (len < 0 || (size_t)len != hlen + oidlen) {
380 ret = SSH_ERR_INVALID_FORMAT;
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000381 goto done;
382 }
Damien Miller4e8285e2010-08-03 16:04:03 +1000383 oidmatch = timingsafe_bcmp(decrypted, oid, oidlen) == 0;
384 hashmatch = timingsafe_bcmp(decrypted + oidlen, hash, hlen) == 0;
Damien Miller86687062014-07-02 15:28:02 +1000385 if (!oidmatch || !hashmatch) {
386 ret = SSH_ERR_SIGNATURE_INVALID;
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000387 goto done;
388 }
Damien Miller86687062014-07-02 15:28:02 +1000389 ret = 0;
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000390done:
jsing@openbsd.org17499912018-02-07 05:17:56 +0000391 freezero(decrypted, rsasize);
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000392 return ret;
393}
Damien Miller72ef7c12015-01-15 02:21:31 +1100394#endif /* WITH_OPENSSL */