2 * Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include "internal/cryptlib.h"
12 #include <openssl/asn1t.h>
13 #include <openssl/x509.h>
14 #include <openssl/rsa.h>
15 #include <openssl/bn.h>
16 #include <openssl/evp.h>
17 #include <openssl/x509v3.h>
18 #include <openssl/cms.h>
19 #include "internal/evp_int.h"
22 /* RSA pkey context structure */
25 /* Key gen parameters */
28 /* Keygen callback info */
30 /* RSA padding mode */
34 /* message digest for MGF1 */
38 /* Minimum salt length or -1 if no PSS parameter restriction */
43 unsigned char *oaep_label;
47 /* True if PSS parameters are restricted */
48 #define rsa_pss_restricted(rctx) (rctx->min_saltlen != -1)
50 static int pkey_rsa_init(EVP_PKEY_CTX *ctx)
52 RSA_PKEY_CTX *rctx = OPENSSL_zalloc(sizeof(*rctx));
57 if (pkey_ctx_is_pss(ctx))
58 rctx->pad_mode = RSA_PKCS1_PSS_PADDING;
60 rctx->pad_mode = RSA_PKCS1_PADDING;
62 rctx->min_saltlen = -1;
64 ctx->keygen_info = rctx->gentmp;
65 ctx->keygen_info_count = 2;
70 static int pkey_rsa_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
72 RSA_PKEY_CTX *dctx, *sctx;
74 if (!pkey_rsa_init(dst))
78 dctx->nbits = sctx->nbits;
80 dctx->pub_exp = BN_dup(sctx->pub_exp);
84 dctx->pad_mode = sctx->pad_mode;
86 dctx->mgf1md = sctx->mgf1md;
87 if (sctx->oaep_label) {
88 OPENSSL_free(dctx->oaep_label);
89 dctx->oaep_label = OPENSSL_memdup(sctx->oaep_label, sctx->oaep_labellen);
90 if (!dctx->oaep_label)
92 dctx->oaep_labellen = sctx->oaep_labellen;
97 static int setup_tbuf(RSA_PKEY_CTX *ctx, EVP_PKEY_CTX *pk)
99 if (ctx->tbuf != NULL)
101 ctx->tbuf = OPENSSL_malloc(EVP_PKEY_size(pk->pkey));
102 if (ctx->tbuf == NULL)
107 static void pkey_rsa_cleanup(EVP_PKEY_CTX *ctx)
109 RSA_PKEY_CTX *rctx = ctx->data;
111 BN_free(rctx->pub_exp);
112 OPENSSL_free(rctx->tbuf);
113 OPENSSL_free(rctx->oaep_label);
118 static int pkey_rsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig,
119 size_t *siglen, const unsigned char *tbs,
123 RSA_PKEY_CTX *rctx = ctx->data;
124 RSA *rsa = ctx->pkey->pkey.rsa;
127 if (tbslen != (size_t)EVP_MD_size(rctx->md)) {
128 RSAerr(RSA_F_PKEY_RSA_SIGN, RSA_R_INVALID_DIGEST_LENGTH);
132 if (EVP_MD_type(rctx->md) == NID_mdc2) {
134 if (rctx->pad_mode != RSA_PKCS1_PADDING)
136 ret = RSA_sign_ASN1_OCTET_STRING(0,
137 tbs, tbslen, sig, &sltmp, rsa);
142 } else if (rctx->pad_mode == RSA_X931_PADDING) {
143 if ((size_t)EVP_PKEY_size(ctx->pkey) < tbslen + 1) {
144 RSAerr(RSA_F_PKEY_RSA_SIGN, RSA_R_KEY_SIZE_TOO_SMALL);
147 if (!setup_tbuf(rctx, ctx)) {
148 RSAerr(RSA_F_PKEY_RSA_SIGN, ERR_R_MALLOC_FAILURE);
151 memcpy(rctx->tbuf, tbs, tbslen);
152 rctx->tbuf[tbslen] = RSA_X931_hash_id(EVP_MD_type(rctx->md));
153 ret = RSA_private_encrypt(tbslen + 1, rctx->tbuf,
154 sig, rsa, RSA_X931_PADDING);
155 } else if (rctx->pad_mode == RSA_PKCS1_PADDING) {
157 ret = RSA_sign(EVP_MD_type(rctx->md),
158 tbs, tbslen, sig, &sltmp, rsa);
162 } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) {
163 if (!setup_tbuf(rctx, ctx))
165 if (!RSA_padding_add_PKCS1_PSS_mgf1(rsa,
167 rctx->md, rctx->mgf1md,
170 ret = RSA_private_encrypt(RSA_size(rsa), rctx->tbuf,
171 sig, rsa, RSA_NO_PADDING);
175 ret = RSA_private_encrypt(tbslen, tbs, sig, ctx->pkey->pkey.rsa,
183 static int pkey_rsa_verifyrecover(EVP_PKEY_CTX *ctx,
184 unsigned char *rout, size_t *routlen,
185 const unsigned char *sig, size_t siglen)
188 RSA_PKEY_CTX *rctx = ctx->data;
191 if (rctx->pad_mode == RSA_X931_PADDING) {
192 if (!setup_tbuf(rctx, ctx))
194 ret = RSA_public_decrypt(siglen, sig,
195 rctx->tbuf, ctx->pkey->pkey.rsa,
200 if (rctx->tbuf[ret] != RSA_X931_hash_id(EVP_MD_type(rctx->md))) {
201 RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER,
202 RSA_R_ALGORITHM_MISMATCH);
205 if (ret != EVP_MD_size(rctx->md)) {
206 RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER,
207 RSA_R_INVALID_DIGEST_LENGTH);
211 memcpy(rout, rctx->tbuf, ret);
212 } else if (rctx->pad_mode == RSA_PKCS1_PADDING) {
214 ret = int_rsa_verify(EVP_MD_type(rctx->md),
215 NULL, 0, rout, &sltmp,
216 sig, siglen, ctx->pkey->pkey.rsa);
223 ret = RSA_public_decrypt(siglen, sig, rout, ctx->pkey->pkey.rsa,
231 static int pkey_rsa_verify(EVP_PKEY_CTX *ctx,
232 const unsigned char *sig, size_t siglen,
233 const unsigned char *tbs, size_t tbslen)
235 RSA_PKEY_CTX *rctx = ctx->data;
236 RSA *rsa = ctx->pkey->pkey.rsa;
240 if (rctx->pad_mode == RSA_PKCS1_PADDING)
241 return RSA_verify(EVP_MD_type(rctx->md), tbs, tbslen,
243 if (tbslen != (size_t)EVP_MD_size(rctx->md)) {
244 RSAerr(RSA_F_PKEY_RSA_VERIFY, RSA_R_INVALID_DIGEST_LENGTH);
247 if (rctx->pad_mode == RSA_X931_PADDING) {
248 if (pkey_rsa_verifyrecover(ctx, NULL, &rslen, sig, siglen) <= 0)
250 } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) {
252 if (!setup_tbuf(rctx, ctx))
254 ret = RSA_public_decrypt(siglen, sig, rctx->tbuf,
255 rsa, RSA_NO_PADDING);
258 ret = RSA_verify_PKCS1_PSS_mgf1(rsa, tbs,
259 rctx->md, rctx->mgf1md,
260 rctx->tbuf, rctx->saltlen);
267 if (!setup_tbuf(rctx, ctx))
269 rslen = RSA_public_decrypt(siglen, sig, rctx->tbuf,
270 rsa, rctx->pad_mode);
275 if ((rslen != tbslen) || memcmp(tbs, rctx->tbuf, rslen))
282 static int pkey_rsa_encrypt(EVP_PKEY_CTX *ctx,
283 unsigned char *out, size_t *outlen,
284 const unsigned char *in, size_t inlen)
287 RSA_PKEY_CTX *rctx = ctx->data;
289 if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
290 int klen = RSA_size(ctx->pkey->pkey.rsa);
291 if (!setup_tbuf(rctx, ctx))
293 if (!RSA_padding_add_PKCS1_OAEP_mgf1(rctx->tbuf, klen,
297 rctx->md, rctx->mgf1md))
299 ret = RSA_public_encrypt(klen, rctx->tbuf, out,
300 ctx->pkey->pkey.rsa, RSA_NO_PADDING);
302 ret = RSA_public_encrypt(inlen, in, out, ctx->pkey->pkey.rsa,
310 static int pkey_rsa_decrypt(EVP_PKEY_CTX *ctx,
311 unsigned char *out, size_t *outlen,
312 const unsigned char *in, size_t inlen)
315 RSA_PKEY_CTX *rctx = ctx->data;
317 if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
319 if (!setup_tbuf(rctx, ctx))
321 ret = RSA_private_decrypt(inlen, in, rctx->tbuf,
322 ctx->pkey->pkey.rsa, RSA_NO_PADDING);
325 for (i = 0; i < ret; i++) {
329 ret = RSA_padding_check_PKCS1_OAEP_mgf1(out, ret, rctx->tbuf + i,
333 rctx->md, rctx->mgf1md);
335 ret = RSA_private_decrypt(inlen, in, out, ctx->pkey->pkey.rsa,
343 static int check_padding_md(const EVP_MD *md, int padding)
350 mdnid = EVP_MD_type(md);
352 if (padding == RSA_NO_PADDING) {
353 RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_PADDING_MODE);
357 if (padding == RSA_X931_PADDING) {
358 if (RSA_X931_hash_id(mdnid) == -1) {
359 RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_X931_DIGEST);
364 /* List of all supported RSA digests */
379 RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_DIGEST);
388 static int pkey_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
390 RSA_PKEY_CTX *rctx = ctx->data;
393 case EVP_PKEY_CTRL_RSA_PADDING:
394 if ((p1 >= RSA_PKCS1_PADDING) && (p1 <= RSA_PKCS1_PSS_PADDING)) {
395 if (!check_padding_md(rctx->md, p1))
397 if (p1 == RSA_PKCS1_PSS_PADDING) {
398 if (!(ctx->operation &
399 (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY)))
402 rctx->md = EVP_sha1();
403 } else if (pkey_ctx_is_pss(ctx)) {
406 if (p1 == RSA_PKCS1_OAEP_PADDING) {
407 if (!(ctx->operation & EVP_PKEY_OP_TYPE_CRYPT))
410 rctx->md = EVP_sha1();
416 RSAerr(RSA_F_PKEY_RSA_CTRL,
417 RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
420 case EVP_PKEY_CTRL_GET_RSA_PADDING:
421 *(int *)p2 = rctx->pad_mode;
424 case EVP_PKEY_CTRL_RSA_PSS_SALTLEN:
425 case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN:
426 if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING) {
427 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PSS_SALTLEN);
430 if (type == EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN) {
431 *(int *)p2 = rctx->saltlen;
435 if (rsa_pss_restricted(rctx) && p1 < rctx->min_saltlen) {
436 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_PSS_SALTLEN_TOO_SMALL);
443 case EVP_PKEY_CTRL_RSA_KEYGEN_BITS:
445 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_KEY_SIZE_TOO_SMALL);
451 case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP:
452 if (p2 == NULL || !BN_is_odd((BIGNUM *)p2) || BN_is_one((BIGNUM *)p2)) {
453 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_BAD_E_VALUE);
456 BN_free(rctx->pub_exp);
460 case EVP_PKEY_CTRL_RSA_OAEP_MD:
461 case EVP_PKEY_CTRL_GET_RSA_OAEP_MD:
462 if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
463 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
466 if (type == EVP_PKEY_CTRL_GET_RSA_OAEP_MD)
467 *(const EVP_MD **)p2 = rctx->md;
472 case EVP_PKEY_CTRL_MD:
473 if (!check_padding_md(p2, rctx->pad_mode))
475 if (rsa_pss_restricted(rctx)) {
476 if (EVP_MD_type(rctx->md) == EVP_MD_type(p2))
478 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_DIGEST_NOT_ALLOWED);
484 case EVP_PKEY_CTRL_GET_MD:
485 *(const EVP_MD **)p2 = rctx->md;
488 case EVP_PKEY_CTRL_RSA_MGF1_MD:
489 case EVP_PKEY_CTRL_GET_RSA_MGF1_MD:
490 if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING
491 && rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
492 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_MGF1_MD);
495 if (type == EVP_PKEY_CTRL_GET_RSA_MGF1_MD) {
497 *(const EVP_MD **)p2 = rctx->mgf1md;
499 *(const EVP_MD **)p2 = rctx->md;
501 if (rsa_pss_restricted(rctx)) {
502 if (EVP_MD_type(rctx->md) == EVP_MD_type(p2))
504 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_MGF1_DIGEST_NOT_ALLOWED);
511 case EVP_PKEY_CTRL_RSA_OAEP_LABEL:
512 if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
513 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
516 OPENSSL_free(rctx->oaep_label);
518 rctx->oaep_label = p2;
519 rctx->oaep_labellen = p1;
521 rctx->oaep_label = NULL;
522 rctx->oaep_labellen = 0;
526 case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL:
527 if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
528 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
531 *(unsigned char **)p2 = rctx->oaep_label;
532 return rctx->oaep_labellen;
534 case EVP_PKEY_CTRL_DIGESTINIT:
535 case EVP_PKEY_CTRL_PKCS7_SIGN:
536 #ifndef OPENSSL_NO_CMS
537 case EVP_PKEY_CTRL_CMS_SIGN:
541 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
542 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
543 #ifndef OPENSSL_NO_CMS
544 case EVP_PKEY_CTRL_CMS_DECRYPT:
545 case EVP_PKEY_CTRL_CMS_ENCRYPT:
546 if (!pkey_ctx_is_pss(ctx))
549 case EVP_PKEY_CTRL_PEER_KEY:
550 RSAerr(RSA_F_PKEY_RSA_CTRL,
551 RSA_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
560 static int pkey_rsa_ctrl_str(EVP_PKEY_CTX *ctx,
561 const char *type, const char *value)
564 RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_VALUE_MISSING);
567 if (strcmp(type, "rsa_padding_mode") == 0) {
569 if (strcmp(value, "pkcs1") == 0)
570 pm = RSA_PKCS1_PADDING;
571 else if (strcmp(value, "sslv23") == 0)
572 pm = RSA_SSLV23_PADDING;
573 else if (strcmp(value, "none") == 0)
575 else if (strcmp(value, "oeap") == 0)
576 pm = RSA_PKCS1_OAEP_PADDING;
577 else if (strcmp(value, "oaep") == 0)
578 pm = RSA_PKCS1_OAEP_PADDING;
579 else if (strcmp(value, "x931") == 0)
580 pm = RSA_X931_PADDING;
581 else if (strcmp(value, "pss") == 0)
582 pm = RSA_PKCS1_PSS_PADDING;
584 RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_UNKNOWN_PADDING_TYPE);
587 return EVP_PKEY_CTX_set_rsa_padding(ctx, pm);
590 if (strcmp(type, "rsa_pss_saltlen") == 0) {
592 saltlen = atoi(value);
593 return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, saltlen);
596 if (strcmp(type, "rsa_keygen_bits") == 0) {
599 return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, nbits);
602 if (strcmp(type, "rsa_keygen_pubexp") == 0) {
604 BIGNUM *pubexp = NULL;
605 if (!BN_asc2bn(&pubexp, value))
607 ret = EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx, pubexp);
613 if (strcmp(type, "rsa_mgf1_md") == 0)
614 return EVP_PKEY_CTX_md(ctx,
615 EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
616 EVP_PKEY_CTRL_RSA_MGF1_MD, value);
618 if (pkey_ctx_is_pss(ctx)) {
620 if (strcmp(type, "rsa_pss_keygen_mgf1_md") == 0)
621 return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_KEYGEN,
622 EVP_PKEY_CTRL_RSA_MGF1_MD, value);
624 if (strcmp(type, "rsa_pss_keygen_md") == 0)
625 return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_KEYGEN,
626 EVP_PKEY_CTRL_MD, value);
628 if (strcmp(type, "rsa_pss_keygen_saltlen") == 0) {
630 saltlen = atoi(value);
631 return EVP_PKEY_CTX_set_rsa_pss_keygen_saltlen(ctx, saltlen);
635 if (strcmp(type, "rsa_oaep_md") == 0)
636 return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_CRYPT,
637 EVP_PKEY_CTRL_RSA_OAEP_MD, value);
639 if (strcmp(type, "rsa_oaep_label") == 0) {
643 lab = OPENSSL_hexstr2buf(value, &lablen);
646 ret = EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, lab, lablen);
655 /* Set PSS parameters when generating a key, if necessary */
656 static int rsa_set_pss_param(RSA *rsa, EVP_PKEY_CTX *ctx)
658 RSA_PKEY_CTX *rctx = ctx->data;
660 if (!pkey_ctx_is_pss(ctx))
662 /* If all parameters are default values don't set pss */
663 if (rctx->md == NULL && rctx->mgf1md == NULL && rctx->saltlen == -2)
665 rsa->pss = rsa_pss_params_create(rctx->md, rctx->mgf1md,
666 rctx->saltlen == -2 ? 0 : rctx->saltlen);
667 if (rsa->pss == NULL)
672 static int pkey_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
675 RSA_PKEY_CTX *rctx = ctx->data;
679 if (rctx->pub_exp == NULL) {
680 rctx->pub_exp = BN_new();
681 if (rctx->pub_exp == NULL || !BN_set_word(rctx->pub_exp, RSA_F4))
687 if (ctx->pkey_gencb) {
688 pcb = BN_GENCB_new();
693 evp_pkey_set_cb_translate(pcb, ctx);
696 ret = RSA_generate_key_ex(rsa, rctx->nbits, rctx->pub_exp, pcb);
698 if (ret > 0 && !rsa_set_pss_param(rsa, ctx)) {
703 EVP_PKEY_assign(pkey, ctx->pmeth->pkey_id, rsa);
709 const EVP_PKEY_METHOD rsa_pkey_meth = {
711 EVP_PKEY_FLAG_AUTOARGLEN,
728 pkey_rsa_verifyrecover,
745 * Called for PSS sign or verify initialisation: checks PSS parameter
746 * sanity and sets any restrictions on key usage.
749 static int pkey_pss_init(EVP_PKEY_CTX *ctx)
752 RSA_PKEY_CTX *rctx = ctx->data;
754 const EVP_MD *mgf1md;
757 /* Should never happen */
758 if (!pkey_ctx_is_pss(ctx))
760 rsa = ctx->pkey->pkey.rsa;
761 /* If no restrictions just return */
762 if (rsa->pss == NULL)
764 /* Get and check parameters */
765 if (!rsa_pss_get_param(rsa->pss, &md, &mgf1md, &min_saltlen))
768 rctx->min_saltlen = min_saltlen;
771 * Set PSS restrictions as defaults: we can then block any attempt to
772 * use invalid values in pkey_rsa_ctrl
776 rctx->mgf1md = mgf1md;
777 rctx->saltlen = min_saltlen;
782 const EVP_PKEY_METHOD rsa_pss_pkey_meth = {
784 EVP_PKEY_FLAG_AUTOARGLEN,
800 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,