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) {
319 if (!setup_tbuf(rctx, ctx))
321 ret = RSA_private_decrypt(inlen, in, rctx->tbuf,
322 ctx->pkey->pkey.rsa, RSA_NO_PADDING);
325 ret = RSA_padding_check_PKCS1_OAEP_mgf1(out, ret, rctx->tbuf,
329 rctx->md, rctx->mgf1md);
331 ret = RSA_private_decrypt(inlen, in, out, ctx->pkey->pkey.rsa,
339 static int check_padding_md(const EVP_MD *md, int padding)
346 mdnid = EVP_MD_type(md);
348 if (padding == RSA_NO_PADDING) {
349 RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_PADDING_MODE);
353 if (padding == RSA_X931_PADDING) {
354 if (RSA_X931_hash_id(mdnid) == -1) {
355 RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_X931_DIGEST);
360 /* List of all supported RSA digests */
375 RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_DIGEST);
384 static int pkey_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
386 RSA_PKEY_CTX *rctx = ctx->data;
389 case EVP_PKEY_CTRL_RSA_PADDING:
390 if ((p1 >= RSA_PKCS1_PADDING) && (p1 <= RSA_PKCS1_PSS_PADDING)) {
391 if (!check_padding_md(rctx->md, p1))
393 if (p1 == RSA_PKCS1_PSS_PADDING) {
394 if (!(ctx->operation &
395 (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY)))
398 rctx->md = EVP_sha1();
399 } else if (pkey_ctx_is_pss(ctx)) {
402 if (p1 == RSA_PKCS1_OAEP_PADDING) {
403 if (!(ctx->operation & EVP_PKEY_OP_TYPE_CRYPT))
406 rctx->md = EVP_sha1();
412 RSAerr(RSA_F_PKEY_RSA_CTRL,
413 RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
416 case EVP_PKEY_CTRL_GET_RSA_PADDING:
417 *(int *)p2 = rctx->pad_mode;
420 case EVP_PKEY_CTRL_RSA_PSS_SALTLEN:
421 case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN:
422 if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING) {
423 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PSS_SALTLEN);
426 if (type == EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN) {
427 *(int *)p2 = rctx->saltlen;
429 if (p1 < RSA_PSS_SALTLEN_MAX)
431 if (rsa_pss_restricted(rctx)) {
432 if (p1 == RSA_PSS_SALTLEN_AUTO
433 && ctx->operation == EVP_PKEY_OP_VERIFY) {
434 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PSS_SALTLEN);
437 if ((p1 == RSA_PSS_SALTLEN_DIGEST
438 && rctx->min_saltlen > EVP_MD_size(rctx->md))
439 || (p1 >= 0 && p1 < rctx->min_saltlen)) {
440 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_PSS_SALTLEN_TOO_SMALL);
448 case EVP_PKEY_CTRL_RSA_KEYGEN_BITS:
450 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_KEY_SIZE_TOO_SMALL);
456 case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP:
457 if (p2 == NULL || !BN_is_odd((BIGNUM *)p2) || BN_is_one((BIGNUM *)p2)) {
458 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_BAD_E_VALUE);
461 BN_free(rctx->pub_exp);
465 case EVP_PKEY_CTRL_RSA_OAEP_MD:
466 case EVP_PKEY_CTRL_GET_RSA_OAEP_MD:
467 if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
468 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
471 if (type == EVP_PKEY_CTRL_GET_RSA_OAEP_MD)
472 *(const EVP_MD **)p2 = rctx->md;
477 case EVP_PKEY_CTRL_MD:
478 if (!check_padding_md(p2, rctx->pad_mode))
480 if (rsa_pss_restricted(rctx)) {
481 if (EVP_MD_type(rctx->md) == EVP_MD_type(p2))
483 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_DIGEST_NOT_ALLOWED);
489 case EVP_PKEY_CTRL_GET_MD:
490 *(const EVP_MD **)p2 = rctx->md;
493 case EVP_PKEY_CTRL_RSA_MGF1_MD:
494 case EVP_PKEY_CTRL_GET_RSA_MGF1_MD:
495 if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING
496 && rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
497 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_MGF1_MD);
500 if (type == EVP_PKEY_CTRL_GET_RSA_MGF1_MD) {
502 *(const EVP_MD **)p2 = rctx->mgf1md;
504 *(const EVP_MD **)p2 = rctx->md;
506 if (rsa_pss_restricted(rctx)) {
507 if (EVP_MD_type(rctx->mgf1md) == EVP_MD_type(p2))
509 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_MGF1_DIGEST_NOT_ALLOWED);
516 case EVP_PKEY_CTRL_RSA_OAEP_LABEL:
517 if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
518 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
521 OPENSSL_free(rctx->oaep_label);
523 rctx->oaep_label = p2;
524 rctx->oaep_labellen = p1;
526 rctx->oaep_label = NULL;
527 rctx->oaep_labellen = 0;
531 case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL:
532 if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
533 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
536 *(unsigned char **)p2 = rctx->oaep_label;
537 return rctx->oaep_labellen;
539 case EVP_PKEY_CTRL_DIGESTINIT:
540 case EVP_PKEY_CTRL_PKCS7_SIGN:
541 #ifndef OPENSSL_NO_CMS
542 case EVP_PKEY_CTRL_CMS_SIGN:
546 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
547 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
548 #ifndef OPENSSL_NO_CMS
549 case EVP_PKEY_CTRL_CMS_DECRYPT:
550 case EVP_PKEY_CTRL_CMS_ENCRYPT:
552 if (!pkey_ctx_is_pss(ctx))
555 case EVP_PKEY_CTRL_PEER_KEY:
556 RSAerr(RSA_F_PKEY_RSA_CTRL,
557 RSA_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
566 static int pkey_rsa_ctrl_str(EVP_PKEY_CTX *ctx,
567 const char *type, const char *value)
570 RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_VALUE_MISSING);
573 if (strcmp(type, "rsa_padding_mode") == 0) {
575 if (strcmp(value, "pkcs1") == 0)
576 pm = RSA_PKCS1_PADDING;
577 else if (strcmp(value, "sslv23") == 0)
578 pm = RSA_SSLV23_PADDING;
579 else if (strcmp(value, "none") == 0)
581 else if (strcmp(value, "oeap") == 0)
582 pm = RSA_PKCS1_OAEP_PADDING;
583 else if (strcmp(value, "oaep") == 0)
584 pm = RSA_PKCS1_OAEP_PADDING;
585 else if (strcmp(value, "x931") == 0)
586 pm = RSA_X931_PADDING;
587 else if (strcmp(value, "pss") == 0)
588 pm = RSA_PKCS1_PSS_PADDING;
590 RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_UNKNOWN_PADDING_TYPE);
593 return EVP_PKEY_CTX_set_rsa_padding(ctx, pm);
596 if (strcmp(type, "rsa_pss_saltlen") == 0) {
598 if (!strcmp(value, "digest"))
599 saltlen = RSA_PSS_SALTLEN_DIGEST;
600 else if (!strcmp(value, "max"))
601 saltlen = RSA_PSS_SALTLEN_MAX;
602 else if (!strcmp(value, "auto"))
603 saltlen = RSA_PSS_SALTLEN_AUTO;
605 saltlen = atoi(value);
606 return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, saltlen);
609 if (strcmp(type, "rsa_keygen_bits") == 0) {
612 return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, nbits);
615 if (strcmp(type, "rsa_keygen_pubexp") == 0) {
617 BIGNUM *pubexp = NULL;
618 if (!BN_asc2bn(&pubexp, value))
620 ret = EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx, pubexp);
626 if (strcmp(type, "rsa_mgf1_md") == 0)
627 return EVP_PKEY_CTX_md(ctx,
628 EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
629 EVP_PKEY_CTRL_RSA_MGF1_MD, value);
631 if (pkey_ctx_is_pss(ctx)) {
633 if (strcmp(type, "rsa_pss_keygen_mgf1_md") == 0)
634 return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_KEYGEN,
635 EVP_PKEY_CTRL_RSA_MGF1_MD, value);
637 if (strcmp(type, "rsa_pss_keygen_md") == 0)
638 return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_KEYGEN,
639 EVP_PKEY_CTRL_MD, value);
641 if (strcmp(type, "rsa_pss_keygen_saltlen") == 0) {
642 int saltlen = atoi(value);
644 return EVP_PKEY_CTX_set_rsa_pss_keygen_saltlen(ctx, saltlen);
648 if (strcmp(type, "rsa_oaep_md") == 0)
649 return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_CRYPT,
650 EVP_PKEY_CTRL_RSA_OAEP_MD, value);
652 if (strcmp(type, "rsa_oaep_label") == 0) {
656 lab = OPENSSL_hexstr2buf(value, &lablen);
659 ret = EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, lab, lablen);
668 /* Set PSS parameters when generating a key, if necessary */
669 static int rsa_set_pss_param(RSA *rsa, EVP_PKEY_CTX *ctx)
671 RSA_PKEY_CTX *rctx = ctx->data;
673 if (!pkey_ctx_is_pss(ctx))
675 /* If all parameters are default values don't set pss */
676 if (rctx->md == NULL && rctx->mgf1md == NULL && rctx->saltlen == -2)
678 rsa->pss = rsa_pss_params_create(rctx->md, rctx->mgf1md,
679 rctx->saltlen == -2 ? 0 : rctx->saltlen);
680 if (rsa->pss == NULL)
685 static int pkey_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
688 RSA_PKEY_CTX *rctx = ctx->data;
692 if (rctx->pub_exp == NULL) {
693 rctx->pub_exp = BN_new();
694 if (rctx->pub_exp == NULL || !BN_set_word(rctx->pub_exp, RSA_F4))
700 if (ctx->pkey_gencb) {
701 pcb = BN_GENCB_new();
706 evp_pkey_set_cb_translate(pcb, ctx);
709 ret = RSA_generate_key_ex(rsa, rctx->nbits, rctx->pub_exp, pcb);
711 if (ret > 0 && !rsa_set_pss_param(rsa, ctx)) {
716 EVP_PKEY_assign(pkey, ctx->pmeth->pkey_id, rsa);
722 const EVP_PKEY_METHOD rsa_pkey_meth = {
724 EVP_PKEY_FLAG_AUTOARGLEN,
741 pkey_rsa_verifyrecover,
758 * Called for PSS sign or verify initialisation: checks PSS parameter
759 * sanity and sets any restrictions on key usage.
762 static int pkey_pss_init(EVP_PKEY_CTX *ctx)
765 RSA_PKEY_CTX *rctx = ctx->data;
767 const EVP_MD *mgf1md;
768 int min_saltlen, max_saltlen;
770 /* Should never happen */
771 if (!pkey_ctx_is_pss(ctx))
773 rsa = ctx->pkey->pkey.rsa;
774 /* If no restrictions just return */
775 if (rsa->pss == NULL)
777 /* Get and check parameters */
778 if (!rsa_pss_get_param(rsa->pss, &md, &mgf1md, &min_saltlen))
781 /* See if minumum salt length exceeds maximum possible */
782 max_saltlen = RSA_size(rsa) - EVP_MD_size(md);
783 if ((RSA_bits(rsa) & 0x7) == 1)
785 if (min_saltlen > max_saltlen) {
786 RSAerr(RSA_F_PKEY_PSS_INIT, RSA_R_INVALID_SALT_LENGTH);
790 rctx->min_saltlen = min_saltlen;
793 * Set PSS restrictions as defaults: we can then block any attempt to
794 * use invalid values in pkey_rsa_ctrl
798 rctx->mgf1md = mgf1md;
799 rctx->saltlen = min_saltlen;
804 const EVP_PKEY_METHOD rsa_pss_pkey_meth = {
806 EVP_PKEY_FLAG_AUTOARGLEN,
822 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,