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;
61 /* Maximum for sign, auto for verify */
62 rctx->saltlen = RSA_PSS_SALTLEN_AUTO;
63 rctx->min_saltlen = -1;
65 ctx->keygen_info = rctx->gentmp;
66 ctx->keygen_info_count = 2;
71 static int pkey_rsa_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
73 RSA_PKEY_CTX *dctx, *sctx;
75 if (!pkey_rsa_init(dst))
79 dctx->nbits = sctx->nbits;
81 dctx->pub_exp = BN_dup(sctx->pub_exp);
85 dctx->pad_mode = sctx->pad_mode;
87 dctx->mgf1md = sctx->mgf1md;
88 if (sctx->oaep_label) {
89 OPENSSL_free(dctx->oaep_label);
90 dctx->oaep_label = OPENSSL_memdup(sctx->oaep_label, sctx->oaep_labellen);
91 if (!dctx->oaep_label)
93 dctx->oaep_labellen = sctx->oaep_labellen;
98 static int setup_tbuf(RSA_PKEY_CTX *ctx, EVP_PKEY_CTX *pk)
100 if (ctx->tbuf != NULL)
102 ctx->tbuf = OPENSSL_malloc(EVP_PKEY_size(pk->pkey));
103 if (ctx->tbuf == NULL)
108 static void pkey_rsa_cleanup(EVP_PKEY_CTX *ctx)
110 RSA_PKEY_CTX *rctx = ctx->data;
112 BN_free(rctx->pub_exp);
113 OPENSSL_free(rctx->tbuf);
114 OPENSSL_free(rctx->oaep_label);
119 static int pkey_rsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig,
120 size_t *siglen, const unsigned char *tbs,
124 RSA_PKEY_CTX *rctx = ctx->data;
125 RSA *rsa = ctx->pkey->pkey.rsa;
128 if (tbslen != (size_t)EVP_MD_size(rctx->md)) {
129 RSAerr(RSA_F_PKEY_RSA_SIGN, RSA_R_INVALID_DIGEST_LENGTH);
133 if (EVP_MD_type(rctx->md) == NID_mdc2) {
135 if (rctx->pad_mode != RSA_PKCS1_PADDING)
137 ret = RSA_sign_ASN1_OCTET_STRING(0,
138 tbs, tbslen, sig, &sltmp, rsa);
143 } else if (rctx->pad_mode == RSA_X931_PADDING) {
144 if ((size_t)EVP_PKEY_size(ctx->pkey) < tbslen + 1) {
145 RSAerr(RSA_F_PKEY_RSA_SIGN, RSA_R_KEY_SIZE_TOO_SMALL);
148 if (!setup_tbuf(rctx, ctx)) {
149 RSAerr(RSA_F_PKEY_RSA_SIGN, ERR_R_MALLOC_FAILURE);
152 memcpy(rctx->tbuf, tbs, tbslen);
153 rctx->tbuf[tbslen] = RSA_X931_hash_id(EVP_MD_type(rctx->md));
154 ret = RSA_private_encrypt(tbslen + 1, rctx->tbuf,
155 sig, rsa, RSA_X931_PADDING);
156 } else if (rctx->pad_mode == RSA_PKCS1_PADDING) {
158 ret = RSA_sign(EVP_MD_type(rctx->md),
159 tbs, tbslen, sig, &sltmp, rsa);
163 } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) {
164 if (!setup_tbuf(rctx, ctx))
166 if (!RSA_padding_add_PKCS1_PSS_mgf1(rsa,
168 rctx->md, rctx->mgf1md,
171 ret = RSA_private_encrypt(RSA_size(rsa), rctx->tbuf,
172 sig, rsa, RSA_NO_PADDING);
176 ret = RSA_private_encrypt(tbslen, tbs, sig, ctx->pkey->pkey.rsa,
184 static int pkey_rsa_verifyrecover(EVP_PKEY_CTX *ctx,
185 unsigned char *rout, size_t *routlen,
186 const unsigned char *sig, size_t siglen)
189 RSA_PKEY_CTX *rctx = ctx->data;
192 if (rctx->pad_mode == RSA_X931_PADDING) {
193 if (!setup_tbuf(rctx, ctx))
195 ret = RSA_public_decrypt(siglen, sig,
196 rctx->tbuf, ctx->pkey->pkey.rsa,
201 if (rctx->tbuf[ret] != RSA_X931_hash_id(EVP_MD_type(rctx->md))) {
202 RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER,
203 RSA_R_ALGORITHM_MISMATCH);
206 if (ret != EVP_MD_size(rctx->md)) {
207 RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER,
208 RSA_R_INVALID_DIGEST_LENGTH);
212 memcpy(rout, rctx->tbuf, ret);
213 } else if (rctx->pad_mode == RSA_PKCS1_PADDING) {
215 ret = int_rsa_verify(EVP_MD_type(rctx->md),
216 NULL, 0, rout, &sltmp,
217 sig, siglen, ctx->pkey->pkey.rsa);
224 ret = RSA_public_decrypt(siglen, sig, rout, ctx->pkey->pkey.rsa,
232 static int pkey_rsa_verify(EVP_PKEY_CTX *ctx,
233 const unsigned char *sig, size_t siglen,
234 const unsigned char *tbs, size_t tbslen)
236 RSA_PKEY_CTX *rctx = ctx->data;
237 RSA *rsa = ctx->pkey->pkey.rsa;
241 if (rctx->pad_mode == RSA_PKCS1_PADDING)
242 return RSA_verify(EVP_MD_type(rctx->md), tbs, tbslen,
244 if (tbslen != (size_t)EVP_MD_size(rctx->md)) {
245 RSAerr(RSA_F_PKEY_RSA_VERIFY, RSA_R_INVALID_DIGEST_LENGTH);
248 if (rctx->pad_mode == RSA_X931_PADDING) {
249 if (pkey_rsa_verifyrecover(ctx, NULL, &rslen, sig, siglen) <= 0)
251 } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) {
253 if (!setup_tbuf(rctx, ctx))
255 ret = RSA_public_decrypt(siglen, sig, rctx->tbuf,
256 rsa, RSA_NO_PADDING);
259 ret = RSA_verify_PKCS1_PSS_mgf1(rsa, tbs,
260 rctx->md, rctx->mgf1md,
261 rctx->tbuf, rctx->saltlen);
268 if (!setup_tbuf(rctx, ctx))
270 rslen = RSA_public_decrypt(siglen, sig, rctx->tbuf,
271 rsa, rctx->pad_mode);
276 if ((rslen != tbslen) || memcmp(tbs, rctx->tbuf, rslen))
283 static int pkey_rsa_encrypt(EVP_PKEY_CTX *ctx,
284 unsigned char *out, size_t *outlen,
285 const unsigned char *in, size_t inlen)
288 RSA_PKEY_CTX *rctx = ctx->data;
290 if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
291 int klen = RSA_size(ctx->pkey->pkey.rsa);
292 if (!setup_tbuf(rctx, ctx))
294 if (!RSA_padding_add_PKCS1_OAEP_mgf1(rctx->tbuf, klen,
298 rctx->md, rctx->mgf1md))
300 ret = RSA_public_encrypt(klen, rctx->tbuf, out,
301 ctx->pkey->pkey.rsa, RSA_NO_PADDING);
303 ret = RSA_public_encrypt(inlen, in, out, ctx->pkey->pkey.rsa,
311 static int pkey_rsa_decrypt(EVP_PKEY_CTX *ctx,
312 unsigned char *out, size_t *outlen,
313 const unsigned char *in, size_t inlen)
316 RSA_PKEY_CTX *rctx = ctx->data;
318 if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
320 if (!setup_tbuf(rctx, ctx))
322 ret = RSA_private_decrypt(inlen, in, rctx->tbuf,
323 ctx->pkey->pkey.rsa, RSA_NO_PADDING);
326 for (i = 0; i < ret; i++) {
330 ret = RSA_padding_check_PKCS1_OAEP_mgf1(out, ret, rctx->tbuf + i,
334 rctx->md, rctx->mgf1md);
336 ret = RSA_private_decrypt(inlen, in, out, ctx->pkey->pkey.rsa,
344 static int check_padding_md(const EVP_MD *md, int padding)
351 mdnid = EVP_MD_type(md);
353 if (padding == RSA_NO_PADDING) {
354 RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_PADDING_MODE);
358 if (padding == RSA_X931_PADDING) {
359 if (RSA_X931_hash_id(mdnid) == -1) {
360 RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_X931_DIGEST);
365 /* List of all supported RSA digests */
380 RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_DIGEST);
389 static int pkey_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
391 RSA_PKEY_CTX *rctx = ctx->data;
394 case EVP_PKEY_CTRL_RSA_PADDING:
395 if ((p1 >= RSA_PKCS1_PADDING) && (p1 <= RSA_PKCS1_PSS_PADDING)) {
396 if (!check_padding_md(rctx->md, p1))
398 if (p1 == RSA_PKCS1_PSS_PADDING) {
399 if (!(ctx->operation &
400 (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY)))
403 rctx->md = EVP_sha1();
404 } else if (pkey_ctx_is_pss(ctx)) {
407 if (p1 == RSA_PKCS1_OAEP_PADDING) {
408 if (!(ctx->operation & EVP_PKEY_OP_TYPE_CRYPT))
411 rctx->md = EVP_sha1();
417 RSAerr(RSA_F_PKEY_RSA_CTRL,
418 RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
421 case EVP_PKEY_CTRL_GET_RSA_PADDING:
422 *(int *)p2 = rctx->pad_mode;
425 case EVP_PKEY_CTRL_RSA_PSS_SALTLEN:
426 case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN:
427 if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING) {
428 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PSS_SALTLEN);
431 if (type == EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN) {
432 *(int *)p2 = rctx->saltlen;
434 if (p1 < RSA_PSS_SALTLEN_MAX)
436 if (rsa_pss_restricted(rctx)) {
437 if (p1 == RSA_PSS_SALTLEN_AUTO
438 && ctx->operation == EVP_PKEY_OP_VERIFY) {
439 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PSS_SALTLEN);
442 if ((p1 == RSA_PSS_SALTLEN_DIGEST
443 && rctx->min_saltlen > EVP_MD_size(rctx->md))
444 || (p1 >= 0 && p1 < rctx->min_saltlen)) {
445 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_PSS_SALTLEN_TOO_SMALL);
453 case EVP_PKEY_CTRL_RSA_KEYGEN_BITS:
455 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_KEY_SIZE_TOO_SMALL);
461 case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP:
462 if (p2 == NULL || !BN_is_odd((BIGNUM *)p2) || BN_is_one((BIGNUM *)p2)) {
463 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_BAD_E_VALUE);
466 BN_free(rctx->pub_exp);
470 case EVP_PKEY_CTRL_RSA_OAEP_MD:
471 case EVP_PKEY_CTRL_GET_RSA_OAEP_MD:
472 if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
473 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
476 if (type == EVP_PKEY_CTRL_GET_RSA_OAEP_MD)
477 *(const EVP_MD **)p2 = rctx->md;
482 case EVP_PKEY_CTRL_MD:
483 if (!check_padding_md(p2, rctx->pad_mode))
485 if (rsa_pss_restricted(rctx)) {
486 if (EVP_MD_type(rctx->md) == EVP_MD_type(p2))
488 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_DIGEST_NOT_ALLOWED);
494 case EVP_PKEY_CTRL_GET_MD:
495 *(const EVP_MD **)p2 = rctx->md;
498 case EVP_PKEY_CTRL_RSA_MGF1_MD:
499 case EVP_PKEY_CTRL_GET_RSA_MGF1_MD:
500 if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING
501 && rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
502 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_MGF1_MD);
505 if (type == EVP_PKEY_CTRL_GET_RSA_MGF1_MD) {
507 *(const EVP_MD **)p2 = rctx->mgf1md;
509 *(const EVP_MD **)p2 = rctx->md;
511 if (rsa_pss_restricted(rctx)) {
512 if (EVP_MD_type(rctx->md) == EVP_MD_type(p2))
514 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_MGF1_DIGEST_NOT_ALLOWED);
521 case EVP_PKEY_CTRL_RSA_OAEP_LABEL:
522 if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
523 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
526 OPENSSL_free(rctx->oaep_label);
528 rctx->oaep_label = p2;
529 rctx->oaep_labellen = p1;
531 rctx->oaep_label = NULL;
532 rctx->oaep_labellen = 0;
536 case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL:
537 if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
538 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
541 *(unsigned char **)p2 = rctx->oaep_label;
542 return rctx->oaep_labellen;
544 case EVP_PKEY_CTRL_DIGESTINIT:
545 case EVP_PKEY_CTRL_PKCS7_SIGN:
546 #ifndef OPENSSL_NO_CMS
547 case EVP_PKEY_CTRL_CMS_SIGN:
551 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
552 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
553 #ifndef OPENSSL_NO_CMS
554 case EVP_PKEY_CTRL_CMS_DECRYPT:
555 case EVP_PKEY_CTRL_CMS_ENCRYPT:
556 if (!pkey_ctx_is_pss(ctx))
559 case EVP_PKEY_CTRL_PEER_KEY:
560 RSAerr(RSA_F_PKEY_RSA_CTRL,
561 RSA_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
570 static int pkey_rsa_ctrl_str(EVP_PKEY_CTX *ctx,
571 const char *type, const char *value)
574 RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_VALUE_MISSING);
577 if (strcmp(type, "rsa_padding_mode") == 0) {
579 if (strcmp(value, "pkcs1") == 0)
580 pm = RSA_PKCS1_PADDING;
581 else if (strcmp(value, "sslv23") == 0)
582 pm = RSA_SSLV23_PADDING;
583 else if (strcmp(value, "none") == 0)
585 else if (strcmp(value, "oeap") == 0)
586 pm = RSA_PKCS1_OAEP_PADDING;
587 else if (strcmp(value, "oaep") == 0)
588 pm = RSA_PKCS1_OAEP_PADDING;
589 else if (strcmp(value, "x931") == 0)
590 pm = RSA_X931_PADDING;
591 else if (strcmp(value, "pss") == 0)
592 pm = RSA_PKCS1_PSS_PADDING;
594 RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_UNKNOWN_PADDING_TYPE);
597 return EVP_PKEY_CTX_set_rsa_padding(ctx, pm);
600 if (strcmp(type, "rsa_pss_saltlen") == 0) {
602 if (!strcmp(value, "digest"))
603 saltlen = RSA_PSS_SALTLEN_DIGEST;
604 else if (!strcmp(value, "max"))
605 saltlen = RSA_PSS_SALTLEN_MAX;
606 else if (!strcmp(value, "auto"))
607 saltlen = RSA_PSS_SALTLEN_AUTO;
609 saltlen = atoi(value);
610 return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, saltlen);
613 if (strcmp(type, "rsa_keygen_bits") == 0) {
616 return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, nbits);
619 if (strcmp(type, "rsa_keygen_pubexp") == 0) {
621 BIGNUM *pubexp = NULL;
622 if (!BN_asc2bn(&pubexp, value))
624 ret = EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx, pubexp);
630 if (strcmp(type, "rsa_mgf1_md") == 0)
631 return EVP_PKEY_CTX_md(ctx,
632 EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
633 EVP_PKEY_CTRL_RSA_MGF1_MD, value);
635 if (pkey_ctx_is_pss(ctx)) {
637 if (strcmp(type, "rsa_pss_keygen_mgf1_md") == 0)
638 return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_KEYGEN,
639 EVP_PKEY_CTRL_RSA_MGF1_MD, value);
641 if (strcmp(type, "rsa_pss_keygen_md") == 0)
642 return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_KEYGEN,
643 EVP_PKEY_CTRL_MD, value);
645 if (strcmp(type, "rsa_pss_keygen_saltlen") == 0) {
646 int saltlen = atoi(value);
648 return EVP_PKEY_CTX_set_rsa_pss_keygen_saltlen(ctx, saltlen);
652 if (strcmp(type, "rsa_oaep_md") == 0)
653 return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_CRYPT,
654 EVP_PKEY_CTRL_RSA_OAEP_MD, value);
656 if (strcmp(type, "rsa_oaep_label") == 0) {
660 lab = OPENSSL_hexstr2buf(value, &lablen);
663 ret = EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, lab, lablen);
672 /* Set PSS parameters when generating a key, if necessary */
673 static int rsa_set_pss_param(RSA *rsa, EVP_PKEY_CTX *ctx)
675 RSA_PKEY_CTX *rctx = ctx->data;
677 if (!pkey_ctx_is_pss(ctx))
679 /* If all parameters are default values don't set pss */
680 if (rctx->md == NULL && rctx->mgf1md == NULL && rctx->saltlen == -2)
682 rsa->pss = rsa_pss_params_create(rctx->md, rctx->mgf1md,
683 rctx->saltlen == -2 ? 0 : rctx->saltlen);
684 if (rsa->pss == NULL)
689 static int pkey_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
692 RSA_PKEY_CTX *rctx = ctx->data;
696 if (rctx->pub_exp == NULL) {
697 rctx->pub_exp = BN_new();
698 if (rctx->pub_exp == NULL || !BN_set_word(rctx->pub_exp, RSA_F4))
704 if (ctx->pkey_gencb) {
705 pcb = BN_GENCB_new();
710 evp_pkey_set_cb_translate(pcb, ctx);
713 ret = RSA_generate_key_ex(rsa, rctx->nbits, rctx->pub_exp, pcb);
715 if (ret > 0 && !rsa_set_pss_param(rsa, ctx)) {
720 EVP_PKEY_assign(pkey, ctx->pmeth->pkey_id, rsa);
726 const EVP_PKEY_METHOD rsa_pkey_meth = {
728 EVP_PKEY_FLAG_AUTOARGLEN,
745 pkey_rsa_verifyrecover,
762 * Called for PSS sign or verify initialisation: checks PSS parameter
763 * sanity and sets any restrictions on key usage.
766 static int pkey_pss_init(EVP_PKEY_CTX *ctx)
769 RSA_PKEY_CTX *rctx = ctx->data;
771 const EVP_MD *mgf1md;
772 int min_saltlen, max_saltlen;
774 /* Should never happen */
775 if (!pkey_ctx_is_pss(ctx))
777 rsa = ctx->pkey->pkey.rsa;
778 /* If no restrictions just return */
779 if (rsa->pss == NULL)
781 /* Get and check parameters */
782 if (!rsa_pss_get_param(rsa->pss, &md, &mgf1md, &min_saltlen))
785 /* See if minumum salt length exceeds maximum possible */
786 max_saltlen = RSA_size(rsa) - EVP_MD_size(md);
787 if ((RSA_bits(rsa) & 0x7) == 1)
789 if (min_saltlen > max_saltlen) {
790 RSAerr(RSA_F_PKEY_PSS_INIT, RSA_R_INVALID_SALT_LENGTH);
794 rctx->min_saltlen = min_saltlen;
797 * Set PSS restrictions as defaults: we can then block any attempt to
798 * use invalid values in pkey_rsa_ctrl
802 rctx->mgf1md = mgf1md;
803 rctx->saltlen = min_saltlen;
808 const EVP_PKEY_METHOD rsa_pss_pkey_meth = {
810 EVP_PKEY_FLAG_AUTOARGLEN,
826 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,