2 * Copyright 2006-2017 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);
177 ret = RSA_private_encrypt(tbslen, tbs, sig, ctx->pkey->pkey.rsa,
186 static int pkey_rsa_verifyrecover(EVP_PKEY_CTX *ctx,
187 unsigned char *rout, size_t *routlen,
188 const unsigned char *sig, size_t siglen)
191 RSA_PKEY_CTX *rctx = ctx->data;
194 if (rctx->pad_mode == RSA_X931_PADDING) {
195 if (!setup_tbuf(rctx, ctx))
197 ret = RSA_public_decrypt(siglen, sig,
198 rctx->tbuf, ctx->pkey->pkey.rsa,
203 if (rctx->tbuf[ret] != RSA_X931_hash_id(EVP_MD_type(rctx->md))) {
204 RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER,
205 RSA_R_ALGORITHM_MISMATCH);
208 if (ret != EVP_MD_size(rctx->md)) {
209 RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER,
210 RSA_R_INVALID_DIGEST_LENGTH);
214 memcpy(rout, rctx->tbuf, ret);
215 } else if (rctx->pad_mode == RSA_PKCS1_PADDING) {
217 ret = int_rsa_verify(EVP_MD_type(rctx->md),
218 NULL, 0, rout, &sltmp,
219 sig, siglen, ctx->pkey->pkey.rsa);
227 ret = RSA_public_decrypt(siglen, sig, rout, ctx->pkey->pkey.rsa,
236 static int pkey_rsa_verify(EVP_PKEY_CTX *ctx,
237 const unsigned char *sig, size_t siglen,
238 const unsigned char *tbs, size_t tbslen)
240 RSA_PKEY_CTX *rctx = ctx->data;
241 RSA *rsa = ctx->pkey->pkey.rsa;
245 if (rctx->pad_mode == RSA_PKCS1_PADDING)
246 return RSA_verify(EVP_MD_type(rctx->md), tbs, tbslen,
248 if (tbslen != (size_t)EVP_MD_size(rctx->md)) {
249 RSAerr(RSA_F_PKEY_RSA_VERIFY, RSA_R_INVALID_DIGEST_LENGTH);
252 if (rctx->pad_mode == RSA_X931_PADDING) {
253 if (pkey_rsa_verifyrecover(ctx, NULL, &rslen, sig, siglen) <= 0)
255 } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) {
257 if (!setup_tbuf(rctx, ctx))
259 ret = RSA_public_decrypt(siglen, sig, rctx->tbuf,
260 rsa, RSA_NO_PADDING);
263 ret = RSA_verify_PKCS1_PSS_mgf1(rsa, tbs,
264 rctx->md, rctx->mgf1md,
265 rctx->tbuf, rctx->saltlen);
273 if (!setup_tbuf(rctx, ctx))
275 rslen = RSA_public_decrypt(siglen, sig, rctx->tbuf,
276 rsa, rctx->pad_mode);
281 if ((rslen != tbslen) || memcmp(tbs, rctx->tbuf, rslen))
288 static int pkey_rsa_encrypt(EVP_PKEY_CTX *ctx,
289 unsigned char *out, size_t *outlen,
290 const unsigned char *in, size_t inlen)
293 RSA_PKEY_CTX *rctx = ctx->data;
295 if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
296 int klen = RSA_size(ctx->pkey->pkey.rsa);
297 if (!setup_tbuf(rctx, ctx))
299 if (!RSA_padding_add_PKCS1_OAEP_mgf1(rctx->tbuf, klen,
303 rctx->md, rctx->mgf1md))
305 ret = RSA_public_encrypt(klen, rctx->tbuf, out,
306 ctx->pkey->pkey.rsa, RSA_NO_PADDING);
308 ret = RSA_public_encrypt(inlen, in, out, ctx->pkey->pkey.rsa,
317 static int pkey_rsa_decrypt(EVP_PKEY_CTX *ctx,
318 unsigned char *out, size_t *outlen,
319 const unsigned char *in, size_t inlen)
322 RSA_PKEY_CTX *rctx = ctx->data;
324 if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
325 if (!setup_tbuf(rctx, ctx))
327 ret = RSA_private_decrypt(inlen, in, rctx->tbuf,
328 ctx->pkey->pkey.rsa, RSA_NO_PADDING);
331 ret = RSA_padding_check_PKCS1_OAEP_mgf1(out, ret, rctx->tbuf,
335 rctx->md, rctx->mgf1md);
337 ret = RSA_private_decrypt(inlen, in, out, ctx->pkey->pkey.rsa,
346 static int check_padding_md(const EVP_MD *md, int padding)
353 mdnid = EVP_MD_type(md);
355 if (padding == RSA_NO_PADDING) {
356 RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_PADDING_MODE);
360 if (padding == RSA_X931_PADDING) {
361 if (RSA_X931_hash_id(mdnid) == -1) {
362 RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_X931_DIGEST);
367 /* List of all supported RSA digests */
386 RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_DIGEST);
395 static int pkey_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
397 RSA_PKEY_CTX *rctx = ctx->data;
400 case EVP_PKEY_CTRL_RSA_PADDING:
401 if ((p1 >= RSA_PKCS1_PADDING) && (p1 <= RSA_PKCS1_PSS_PADDING)) {
402 if (!check_padding_md(rctx->md, p1))
404 if (p1 == RSA_PKCS1_PSS_PADDING) {
405 if (!(ctx->operation &
406 (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY)))
409 rctx->md = EVP_sha1();
410 } else if (pkey_ctx_is_pss(ctx)) {
413 if (p1 == RSA_PKCS1_OAEP_PADDING) {
414 if (!(ctx->operation & EVP_PKEY_OP_TYPE_CRYPT))
417 rctx->md = EVP_sha1();
423 RSAerr(RSA_F_PKEY_RSA_CTRL,
424 RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
427 case EVP_PKEY_CTRL_GET_RSA_PADDING:
428 *(int *)p2 = rctx->pad_mode;
431 case EVP_PKEY_CTRL_RSA_PSS_SALTLEN:
432 case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN:
433 if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING) {
434 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PSS_SALTLEN);
437 if (type == EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN) {
438 *(int *)p2 = rctx->saltlen;
440 if (p1 < RSA_PSS_SALTLEN_MAX)
442 if (rsa_pss_restricted(rctx)) {
443 if (p1 == RSA_PSS_SALTLEN_AUTO
444 && ctx->operation == EVP_PKEY_OP_VERIFY) {
445 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PSS_SALTLEN);
448 if ((p1 == RSA_PSS_SALTLEN_DIGEST
449 && rctx->min_saltlen > EVP_MD_size(rctx->md))
450 || (p1 >= 0 && p1 < rctx->min_saltlen)) {
451 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_PSS_SALTLEN_TOO_SMALL);
459 case EVP_PKEY_CTRL_RSA_KEYGEN_BITS:
461 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_KEY_SIZE_TOO_SMALL);
467 case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP:
468 if (p2 == NULL || !BN_is_odd((BIGNUM *)p2) || BN_is_one((BIGNUM *)p2)) {
469 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_BAD_E_VALUE);
472 BN_free(rctx->pub_exp);
476 case EVP_PKEY_CTRL_RSA_OAEP_MD:
477 case EVP_PKEY_CTRL_GET_RSA_OAEP_MD:
478 if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
479 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
482 if (type == EVP_PKEY_CTRL_GET_RSA_OAEP_MD)
483 *(const EVP_MD **)p2 = rctx->md;
488 case EVP_PKEY_CTRL_MD:
489 if (!check_padding_md(p2, rctx->pad_mode))
491 if (rsa_pss_restricted(rctx)) {
492 if (EVP_MD_type(rctx->md) == EVP_MD_type(p2))
494 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_DIGEST_NOT_ALLOWED);
500 case EVP_PKEY_CTRL_GET_MD:
501 *(const EVP_MD **)p2 = rctx->md;
504 case EVP_PKEY_CTRL_RSA_MGF1_MD:
505 case EVP_PKEY_CTRL_GET_RSA_MGF1_MD:
506 if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING
507 && rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
508 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_MGF1_MD);
511 if (type == EVP_PKEY_CTRL_GET_RSA_MGF1_MD) {
513 *(const EVP_MD **)p2 = rctx->mgf1md;
515 *(const EVP_MD **)p2 = rctx->md;
517 if (rsa_pss_restricted(rctx)) {
518 if (EVP_MD_type(rctx->mgf1md) == EVP_MD_type(p2))
520 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_MGF1_DIGEST_NOT_ALLOWED);
527 case EVP_PKEY_CTRL_RSA_OAEP_LABEL:
528 if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
529 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
532 OPENSSL_free(rctx->oaep_label);
534 rctx->oaep_label = p2;
535 rctx->oaep_labellen = p1;
537 rctx->oaep_label = NULL;
538 rctx->oaep_labellen = 0;
542 case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL:
543 if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
544 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
547 *(unsigned char **)p2 = rctx->oaep_label;
548 return rctx->oaep_labellen;
550 case EVP_PKEY_CTRL_DIGESTINIT:
551 case EVP_PKEY_CTRL_PKCS7_SIGN:
552 #ifndef OPENSSL_NO_CMS
553 case EVP_PKEY_CTRL_CMS_SIGN:
557 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
558 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
559 #ifndef OPENSSL_NO_CMS
560 case EVP_PKEY_CTRL_CMS_DECRYPT:
561 case EVP_PKEY_CTRL_CMS_ENCRYPT:
563 if (!pkey_ctx_is_pss(ctx))
566 case EVP_PKEY_CTRL_PEER_KEY:
567 RSAerr(RSA_F_PKEY_RSA_CTRL,
568 RSA_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
577 static int pkey_rsa_ctrl_str(EVP_PKEY_CTX *ctx,
578 const char *type, const char *value)
581 RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_VALUE_MISSING);
584 if (strcmp(type, "rsa_padding_mode") == 0) {
586 if (strcmp(value, "pkcs1") == 0) {
587 pm = RSA_PKCS1_PADDING;
588 } else if (strcmp(value, "sslv23") == 0) {
589 pm = RSA_SSLV23_PADDING;
590 } else if (strcmp(value, "none") == 0) {
592 } else if (strcmp(value, "oeap") == 0) {
593 pm = RSA_PKCS1_OAEP_PADDING;
594 } else if (strcmp(value, "oaep") == 0) {
595 pm = RSA_PKCS1_OAEP_PADDING;
596 } else if (strcmp(value, "x931") == 0) {
597 pm = RSA_X931_PADDING;
598 } else if (strcmp(value, "pss") == 0) {
599 pm = RSA_PKCS1_PSS_PADDING;
601 RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_UNKNOWN_PADDING_TYPE);
604 return EVP_PKEY_CTX_set_rsa_padding(ctx, pm);
607 if (strcmp(type, "rsa_pss_saltlen") == 0) {
609 if (!strcmp(value, "digest"))
610 saltlen = RSA_PSS_SALTLEN_DIGEST;
611 else if (!strcmp(value, "max"))
612 saltlen = RSA_PSS_SALTLEN_MAX;
613 else if (!strcmp(value, "auto"))
614 saltlen = RSA_PSS_SALTLEN_AUTO;
616 saltlen = atoi(value);
617 return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, saltlen);
620 if (strcmp(type, "rsa_keygen_bits") == 0) {
623 return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, nbits);
626 if (strcmp(type, "rsa_keygen_pubexp") == 0) {
628 BIGNUM *pubexp = NULL;
629 if (!BN_asc2bn(&pubexp, value))
631 ret = EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx, pubexp);
637 if (strcmp(type, "rsa_mgf1_md") == 0)
638 return EVP_PKEY_CTX_md(ctx,
639 EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
640 EVP_PKEY_CTRL_RSA_MGF1_MD, value);
642 if (pkey_ctx_is_pss(ctx)) {
644 if (strcmp(type, "rsa_pss_keygen_mgf1_md") == 0)
645 return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_KEYGEN,
646 EVP_PKEY_CTRL_RSA_MGF1_MD, value);
648 if (strcmp(type, "rsa_pss_keygen_md") == 0)
649 return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_KEYGEN,
650 EVP_PKEY_CTRL_MD, value);
652 if (strcmp(type, "rsa_pss_keygen_saltlen") == 0) {
653 int saltlen = atoi(value);
655 return EVP_PKEY_CTX_set_rsa_pss_keygen_saltlen(ctx, saltlen);
659 if (strcmp(type, "rsa_oaep_md") == 0)
660 return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_CRYPT,
661 EVP_PKEY_CTRL_RSA_OAEP_MD, value);
663 if (strcmp(type, "rsa_oaep_label") == 0) {
667 lab = OPENSSL_hexstr2buf(value, &lablen);
670 ret = EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, lab, lablen);
679 /* Set PSS parameters when generating a key, if necessary */
680 static int rsa_set_pss_param(RSA *rsa, EVP_PKEY_CTX *ctx)
682 RSA_PKEY_CTX *rctx = ctx->data;
684 if (!pkey_ctx_is_pss(ctx))
686 /* If all parameters are default values don't set pss */
687 if (rctx->md == NULL && rctx->mgf1md == NULL && rctx->saltlen == -2)
689 rsa->pss = rsa_pss_params_create(rctx->md, rctx->mgf1md,
690 rctx->saltlen == -2 ? 0 : rctx->saltlen);
691 if (rsa->pss == NULL)
696 static int pkey_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
699 RSA_PKEY_CTX *rctx = ctx->data;
703 if (rctx->pub_exp == NULL) {
704 rctx->pub_exp = BN_new();
705 if (rctx->pub_exp == NULL || !BN_set_word(rctx->pub_exp, RSA_F4))
711 if (ctx->pkey_gencb) {
712 pcb = BN_GENCB_new();
717 evp_pkey_set_cb_translate(pcb, ctx);
721 ret = RSA_generate_key_ex(rsa, rctx->nbits, rctx->pub_exp, pcb);
723 if (ret > 0 && !rsa_set_pss_param(rsa, ctx)) {
728 EVP_PKEY_assign(pkey, ctx->pmeth->pkey_id, rsa);
734 const EVP_PKEY_METHOD rsa_pkey_meth = {
736 EVP_PKEY_FLAG_AUTOARGLEN,
753 pkey_rsa_verifyrecover,
770 * Called for PSS sign or verify initialisation: checks PSS parameter
771 * sanity and sets any restrictions on key usage.
774 static int pkey_pss_init(EVP_PKEY_CTX *ctx)
777 RSA_PKEY_CTX *rctx = ctx->data;
779 const EVP_MD *mgf1md;
780 int min_saltlen, max_saltlen;
782 /* Should never happen */
783 if (!pkey_ctx_is_pss(ctx))
785 rsa = ctx->pkey->pkey.rsa;
786 /* If no restrictions just return */
787 if (rsa->pss == NULL)
789 /* Get and check parameters */
790 if (!rsa_pss_get_param(rsa->pss, &md, &mgf1md, &min_saltlen))
793 /* See if minumum salt length exceeds maximum possible */
794 max_saltlen = RSA_size(rsa) - EVP_MD_size(md);
795 if ((RSA_bits(rsa) & 0x7) == 1)
797 if (min_saltlen > max_saltlen) {
798 RSAerr(RSA_F_PKEY_PSS_INIT, RSA_R_INVALID_SALT_LENGTH);
802 rctx->min_saltlen = min_saltlen;
805 * Set PSS restrictions as defaults: we can then block any attempt to
806 * use invalid values in pkey_rsa_ctrl
810 rctx->mgf1md = mgf1md;
811 rctx->saltlen = min_saltlen;
816 const EVP_PKEY_METHOD rsa_pss_pkey_meth = {
818 EVP_PKEY_FLAG_AUTOARGLEN,
834 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,