1 /* $OpenBSD: ssh-rsa.c,v 1.66 2018/02/14 16:27:24 jsing Exp $ */
3 * Copyright (c) 2000, 2003 Markus Friedl <markus@openbsd.org>
5 * 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.
9 * 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.
22 #include <sys/types.h>
24 #include <openssl/evp.h>
25 #include <openssl/err.h>
33 #define SSHKEY_INTERNAL
38 static int openssh_RSA_verify(int, u_char *, size_t, u_char *, size_t, RSA *);
41 rsa_hash_alg_ident(int hash_alg)
46 case SSH_DIGEST_SHA256:
47 return "rsa-sha2-256";
48 case SSH_DIGEST_SHA512:
49 return "rsa-sha2-512";
55 rsa_hash_alg_from_ident(const char *ident)
57 if (strcmp(ident, "ssh-rsa") == 0 ||
58 strcmp(ident, "ssh-rsa-cert-v01@openssh.com") == 0)
59 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;
68 rsa_hash_alg_nid(int type)
73 case SSH_DIGEST_SHA256:
75 case SSH_DIGEST_SHA512:
83 ssh_rsa_generate_additional_parameters(struct sshkey *key)
89 if (key == NULL || key->rsa == NULL ||
90 sshkey_type_plain(key->type) != KEY_RSA)
91 return SSH_ERR_INVALID_ARGUMENT;
93 if ((ctx = BN_CTX_new()) == NULL)
94 return SSH_ERR_ALLOC_FAIL;
95 if ((aux = BN_new()) == NULL) {
96 r = SSH_ERR_ALLOC_FAIL;
99 BN_set_flags(aux, BN_FLG_CONSTTIME);
102 const BIGNUM *q, *d, *p;
103 BIGNUM *dmq1=NULL, *dmp1=NULL;
104 if ((dmq1 = BN_new()) == NULL ||
105 (dmp1 = BN_new()) == NULL ) {
106 r = SSH_ERR_ALLOC_FAIL;
109 RSA_get0_key(key->rsa, NULL, NULL, &d);
110 RSA_get0_factors(key->rsa, &p, &q);
111 if ((BN_sub(aux, q, BN_value_one()) == 0) ||
112 (BN_mod(dmq1, d, aux, ctx) == 0) ||
113 (BN_sub(aux, p, BN_value_one()) == 0) ||
114 (BN_mod(dmp1, d, aux, ctx) == 0) ||
115 RSA_set0_crt_params(key->rsa, dmp1, dmq1, NULL) == 0) {
116 r = SSH_ERR_LIBCRYPTO_ERROR;
129 /* RSASSA-PKCS1-v1_5 (PKCS #1 v2.0 signature) with SHA1 */
131 ssh_rsa_sign(const struct sshkey *key, u_char **sigp, size_t *lenp,
132 const u_char *data, size_t datalen, const char *alg_ident)
134 u_char digest[SSH_DIGEST_MAX_LENGTH], *sig = NULL;
137 int nid, hash_alg, ret = SSH_ERR_INTERNAL_ERROR;
138 struct sshbuf *b = NULL;
145 if (alg_ident == NULL || strlen(alg_ident) == 0)
146 hash_alg = SSH_DIGEST_SHA1;
148 hash_alg = rsa_hash_alg_from_ident(alg_ident);
149 if (key == NULL || key->rsa == NULL || hash_alg == -1 ||
150 sshkey_type_plain(key->type) != KEY_RSA)
151 return SSH_ERR_INVALID_ARGUMENT;
152 if (RSA_bits(key->rsa) < SSH_RSA_MINIMUM_MODULUS_SIZE)
153 return SSH_ERR_KEY_LENGTH;
154 slen = RSA_size(key->rsa);
155 if (slen <= 0 || slen > SSHBUF_MAX_BIGNUM)
156 return SSH_ERR_INVALID_ARGUMENT;
159 nid = rsa_hash_alg_nid(hash_alg);
160 if ((dlen = ssh_digest_bytes(hash_alg)) == 0)
161 return SSH_ERR_INTERNAL_ERROR;
162 if ((ret = ssh_digest_memory(hash_alg, data, datalen,
163 digest, sizeof(digest))) != 0)
166 if ((sig = malloc(slen)) == NULL) {
167 ret = SSH_ERR_ALLOC_FAIL;
171 if (RSA_sign(nid, digest, dlen, sig, &len, key->rsa) != 1) {
172 ret = SSH_ERR_LIBCRYPTO_ERROR;
176 size_t diff = slen - len;
177 memmove(sig + diff, sig, len);
178 explicit_bzero(sig, diff);
179 } else if (len > slen) {
180 ret = SSH_ERR_INTERNAL_ERROR;
183 /* encode signature */
184 if ((b = sshbuf_new()) == NULL) {
185 ret = SSH_ERR_ALLOC_FAIL;
188 if ((ret = sshbuf_put_cstring(b, rsa_hash_alg_ident(hash_alg))) != 0 ||
189 (ret = sshbuf_put_string(b, sig, slen)) != 0)
193 if ((*sigp = malloc(len)) == NULL) {
194 ret = SSH_ERR_ALLOC_FAIL;
197 memcpy(*sigp, sshbuf_ptr(b), len);
203 explicit_bzero(digest, sizeof(digest));
210 ssh_rsa_verify(const struct sshkey *key,
211 const u_char *sig, size_t siglen, const u_char *data, size_t datalen,
214 char *sigtype = NULL;
215 int hash_alg, ret = SSH_ERR_INTERNAL_ERROR;
216 size_t len = 0, diff, modlen, dlen;
217 struct sshbuf *b = NULL;
218 u_char digest[SSH_DIGEST_MAX_LENGTH], *osigblob, *sigblob = NULL;
220 if (key == NULL || key->rsa == NULL ||
221 sshkey_type_plain(key->type) != KEY_RSA ||
222 sig == NULL || siglen == 0)
223 return SSH_ERR_INVALID_ARGUMENT;
224 if (RSA_bits(key->rsa) < SSH_RSA_MINIMUM_MODULUS_SIZE)
225 return SSH_ERR_KEY_LENGTH;
227 if ((b = sshbuf_from(sig, siglen)) == NULL)
228 return SSH_ERR_ALLOC_FAIL;
229 if (sshbuf_get_cstring(b, &sigtype, NULL) != 0) {
230 ret = SSH_ERR_INVALID_FORMAT;
233 /* XXX djm: need cert types that reliably yield SHA-2 signatures */
234 if (alg != NULL && strcmp(alg, sigtype) != 0 &&
235 strcmp(alg, "ssh-rsa-cert-v01@openssh.com") != 0) {
236 error("%s: RSA signature type mismatch: "
237 "expected %s received %s", __func__, alg, sigtype);
238 ret = SSH_ERR_SIGNATURE_INVALID;
241 if ((hash_alg = rsa_hash_alg_from_ident(sigtype)) == -1) {
242 ret = SSH_ERR_KEY_TYPE_MISMATCH;
245 if (sshbuf_get_string(b, &sigblob, &len) != 0) {
246 ret = SSH_ERR_INVALID_FORMAT;
249 if (sshbuf_len(b) != 0) {
250 ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
253 /* RSA_verify expects a signature of RSA_size */
254 modlen = RSA_size(key->rsa);
256 ret = SSH_ERR_KEY_BITS_MISMATCH;
258 } else if (len < modlen) {
261 if ((sigblob = realloc(sigblob, modlen)) == NULL) {
262 sigblob = osigblob; /* put it back for clear/free */
263 ret = SSH_ERR_ALLOC_FAIL;
266 memmove(sigblob + diff, sigblob, len);
267 explicit_bzero(sigblob, diff);
270 if ((dlen = ssh_digest_bytes(hash_alg)) == 0) {
271 ret = SSH_ERR_INTERNAL_ERROR;
274 if ((ret = ssh_digest_memory(hash_alg, data, datalen,
275 digest, sizeof(digest))) != 0)
278 ret = openssh_RSA_verify(hash_alg, digest, dlen, sigblob, len,
281 freezero(sigblob, len);
284 explicit_bzero(digest, sizeof(digest));
290 * http://www.rsasecurity.com/rsalabs/pkcs/pkcs-1/
291 * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1.asn
295 * id-sha1 OBJECT IDENTIFIER ::= { iso(1) identified-organization(3)
296 * oiw(14) secsig(3) algorithms(2) 26 }
298 static const u_char id_sha1[] = {
299 0x30, 0x21, /* type Sequence, length 0x21 (33) */
300 0x30, 0x09, /* type Sequence, length 0x09 */
301 0x06, 0x05, /* type OID, length 0x05 */
302 0x2b, 0x0e, 0x03, 0x02, 0x1a, /* id-sha1 OID */
303 0x05, 0x00, /* NULL */
304 0x04, 0x14 /* Octet string, length 0x14 (20), followed by sha1 hash */
308 * See http://csrc.nist.gov/groups/ST/crypto_apps_infra/csor/algorithms.html
309 * id-sha256 OBJECT IDENTIFIER ::= { joint-iso-itu-t(2) country(16) us(840)
310 * organization(1) gov(101) csor(3) nistAlgorithm(4) hashAlgs(2)
313 static const u_char id_sha256[] = {
314 0x30, 0x31, /* type Sequence, length 0x31 (49) */
315 0x30, 0x0d, /* type Sequence, length 0x0d (13) */
316 0x06, 0x09, /* type OID, length 0x09 */
317 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, /* id-sha256 */
318 0x05, 0x00, /* NULL */
319 0x04, 0x20 /* Octet string, length 0x20 (32), followed by sha256 hash */
323 * See http://csrc.nist.gov/groups/ST/crypto_apps_infra/csor/algorithms.html
324 * id-sha512 OBJECT IDENTIFIER ::= { joint-iso-itu-t(2) country(16) us(840)
325 * organization(1) gov(101) csor(3) nistAlgorithm(4) hashAlgs(2)
328 static const u_char id_sha512[] = {
329 0x30, 0x51, /* type Sequence, length 0x51 (81) */
330 0x30, 0x0d, /* type Sequence, length 0x0d (13) */
331 0x06, 0x09, /* type OID, length 0x09 */
332 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, /* id-sha512 */
333 0x05, 0x00, /* NULL */
334 0x04, 0x40 /* Octet string, length 0x40 (64), followed by sha512 hash */
338 rsa_hash_alg_oid(int hash_alg, const u_char **oidp, size_t *oidlenp)
341 case SSH_DIGEST_SHA1:
343 *oidlenp = sizeof(id_sha1);
345 case SSH_DIGEST_SHA256:
347 *oidlenp = sizeof(id_sha256);
349 case SSH_DIGEST_SHA512:
351 *oidlenp = sizeof(id_sha512);
354 return SSH_ERR_INVALID_ARGUMENT;
360 openssh_RSA_verify(int hash_alg, u_char *hash, size_t hashlen,
361 u_char *sigbuf, size_t siglen, RSA *rsa)
363 size_t rsasize = 0, oidlen = 0, hlen = 0;
364 int ret, len, oidmatch, hashmatch;
365 const u_char *oid = NULL;
366 u_char *decrypted = NULL;
368 if ((ret = rsa_hash_alg_oid(hash_alg, &oid, &oidlen)) != 0)
370 ret = SSH_ERR_INTERNAL_ERROR;
371 hlen = ssh_digest_bytes(hash_alg);
372 if (hashlen != hlen) {
373 ret = SSH_ERR_INVALID_ARGUMENT;
376 rsasize = RSA_size(rsa);
377 if (rsasize <= 0 || rsasize > SSHBUF_MAX_BIGNUM ||
378 siglen == 0 || siglen > rsasize) {
379 ret = SSH_ERR_INVALID_ARGUMENT;
382 if ((decrypted = malloc(rsasize)) == NULL) {
383 ret = SSH_ERR_ALLOC_FAIL;
386 if ((len = RSA_public_decrypt(siglen, sigbuf, decrypted, rsa,
387 RSA_PKCS1_PADDING)) < 0) {
388 ret = SSH_ERR_LIBCRYPTO_ERROR;
391 if (len < 0 || (size_t)len != hlen + oidlen) {
392 ret = SSH_ERR_INVALID_FORMAT;
395 oidmatch = timingsafe_bcmp(decrypted, oid, oidlen) == 0;
396 hashmatch = timingsafe_bcmp(decrypted + oidlen, hash, hlen) == 0;
397 if (!oidmatch || !hashmatch) {
398 ret = SSH_ERR_SIGNATURE_INVALID;
403 freezero(decrypted, rsasize);
406 #endif /* WITH_OPENSSL */