2 * Copyright 2006-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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 * RSA low level APIs are deprecated for public use, but still ok for
14 #include "internal/deprecated.h"
16 #include "internal/constant_time.h"
19 #include "internal/cryptlib.h"
20 #include <openssl/asn1t.h>
21 #include <openssl/x509.h>
22 #include <openssl/rsa.h>
23 #include <openssl/bn.h>
24 #include <openssl/evp.h>
25 #include <openssl/x509v3.h>
26 #include <openssl/cms.h>
27 #include "crypto/evp.h"
28 #include "crypto/rsa.h"
29 #include "rsa_local.h"
31 /* RSA pkey context structure */
34 /* Key gen parameters */
38 /* Keygen callback info */
40 /* RSA padding mode */
44 /* message digest for MGF1 */
48 /* Minimum salt length or -1 if no PSS parameter restriction */
53 unsigned char *oaep_label;
55 /* if to use implicit rejection in PKCS#1 v1.5 decryption */
56 int implicit_rejection;
59 /* True if PSS parameters are restricted */
60 #define rsa_pss_restricted(rctx) (rctx->min_saltlen != -1)
62 static int pkey_rsa_init(EVP_PKEY_CTX *ctx)
64 RSA_PKEY_CTX *rctx = OPENSSL_zalloc(sizeof(*rctx));
69 rctx->primes = RSA_DEFAULT_PRIME_NUM;
70 if (pkey_ctx_is_pss(ctx))
71 rctx->pad_mode = RSA_PKCS1_PSS_PADDING;
73 rctx->pad_mode = RSA_PKCS1_PADDING;
74 /* Maximum for sign, auto for verify */
75 rctx->saltlen = RSA_PSS_SALTLEN_AUTO;
76 rctx->min_saltlen = -1;
77 rctx->implicit_rejection = 1;
79 ctx->keygen_info = rctx->gentmp;
80 ctx->keygen_info_count = 2;
85 static int pkey_rsa_copy(EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src)
87 RSA_PKEY_CTX *dctx, *sctx;
89 if (!pkey_rsa_init(dst))
93 dctx->nbits = sctx->nbits;
95 dctx->pub_exp = BN_dup(sctx->pub_exp);
99 dctx->pad_mode = sctx->pad_mode;
101 dctx->mgf1md = sctx->mgf1md;
102 dctx->saltlen = sctx->saltlen;
103 dctx->implicit_rejection = sctx->implicit_rejection;
104 if (sctx->oaep_label) {
105 OPENSSL_free(dctx->oaep_label);
106 dctx->oaep_label = OPENSSL_memdup(sctx->oaep_label, sctx->oaep_labellen);
107 if (!dctx->oaep_label)
109 dctx->oaep_labellen = sctx->oaep_labellen;
114 static int setup_tbuf(RSA_PKEY_CTX *ctx, EVP_PKEY_CTX *pk)
116 if (ctx->tbuf != NULL)
119 OPENSSL_malloc(RSA_size(EVP_PKEY_get0_RSA(pk->pkey)))) == NULL)
124 static void pkey_rsa_cleanup(EVP_PKEY_CTX *ctx)
126 RSA_PKEY_CTX *rctx = ctx->data;
128 BN_free(rctx->pub_exp);
129 OPENSSL_free(rctx->tbuf);
130 OPENSSL_free(rctx->oaep_label);
135 static int pkey_rsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig,
136 size_t *siglen, const unsigned char *tbs,
140 RSA_PKEY_CTX *rctx = ctx->data;
142 * Discard const. Its marked as const because this may be a cached copy of
143 * the "real" key. These calls don't make any modifications that need to
144 * be reflected back in the "original" key.
146 RSA *rsa = (RSA *)EVP_PKEY_get0_RSA(ctx->pkey);
149 if (tbslen != (size_t)EVP_MD_get_size(rctx->md)) {
150 ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_DIGEST_LENGTH);
154 if (EVP_MD_get_type(rctx->md) == NID_mdc2) {
156 if (rctx->pad_mode != RSA_PKCS1_PADDING)
158 ret = RSA_sign_ASN1_OCTET_STRING(0, tbs, tbslen, sig, &sltmp, rsa);
163 } else if (rctx->pad_mode == RSA_X931_PADDING) {
164 if ((size_t)RSA_size(rsa) < tbslen + 1) {
165 ERR_raise(ERR_LIB_RSA, RSA_R_KEY_SIZE_TOO_SMALL);
168 if (!setup_tbuf(rctx, ctx)) {
169 ERR_raise(ERR_LIB_RSA, ERR_R_RSA_LIB);
172 memcpy(rctx->tbuf, tbs, tbslen);
173 rctx->tbuf[tbslen] = RSA_X931_hash_id(EVP_MD_get_type(rctx->md));
174 ret = RSA_private_encrypt(tbslen + 1, rctx->tbuf,
175 sig, rsa, RSA_X931_PADDING);
176 } else if (rctx->pad_mode == RSA_PKCS1_PADDING) {
178 ret = RSA_sign(EVP_MD_get_type(rctx->md),
179 tbs, tbslen, sig, &sltmp, rsa);
183 } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) {
184 if (!setup_tbuf(rctx, ctx))
186 if (!RSA_padding_add_PKCS1_PSS_mgf1(rsa,
188 rctx->md, rctx->mgf1md,
191 ret = RSA_private_encrypt(RSA_size(rsa), rctx->tbuf,
192 sig, rsa, RSA_NO_PADDING);
197 ret = RSA_private_encrypt(tbslen, tbs, sig, rsa, rctx->pad_mode);
205 static int pkey_rsa_verifyrecover(EVP_PKEY_CTX *ctx,
206 unsigned char *rout, size_t *routlen,
207 const unsigned char *sig, size_t siglen)
210 RSA_PKEY_CTX *rctx = ctx->data;
212 * Discard const. Its marked as const because this may be a cached copy of
213 * the "real" key. These calls don't make any modifications that need to
214 * be reflected back in the "original" key.
216 RSA *rsa = (RSA *)EVP_PKEY_get0_RSA(ctx->pkey);
219 if (rctx->pad_mode == RSA_X931_PADDING) {
220 if (!setup_tbuf(rctx, ctx))
222 ret = RSA_public_decrypt(siglen, sig, rctx->tbuf, rsa,
227 if (rctx->tbuf[ret] != RSA_X931_hash_id(EVP_MD_get_type(rctx->md))) {
228 ERR_raise(ERR_LIB_RSA, RSA_R_ALGORITHM_MISMATCH);
231 if (ret != EVP_MD_get_size(rctx->md)) {
232 ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_DIGEST_LENGTH);
236 memcpy(rout, rctx->tbuf, ret);
237 } else if (rctx->pad_mode == RSA_PKCS1_PADDING) {
239 ret = ossl_rsa_verify(EVP_MD_get_type(rctx->md),
240 NULL, 0, rout, &sltmp,
249 ret = RSA_public_decrypt(siglen, sig, rout, rsa, rctx->pad_mode);
257 static int pkey_rsa_verify(EVP_PKEY_CTX *ctx,
258 const unsigned char *sig, size_t siglen,
259 const unsigned char *tbs, size_t tbslen)
261 RSA_PKEY_CTX *rctx = ctx->data;
263 * Discard const. Its marked as const because this may be a cached copy of
264 * the "real" key. These calls don't make any modifications that need to
265 * be reflected back in the "original" key.
267 RSA *rsa = (RSA *)EVP_PKEY_get0_RSA(ctx->pkey);
271 if (rctx->pad_mode == RSA_PKCS1_PADDING)
272 return RSA_verify(EVP_MD_get_type(rctx->md), tbs, tbslen,
274 if (tbslen != (size_t)EVP_MD_get_size(rctx->md)) {
275 ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_DIGEST_LENGTH);
278 if (rctx->pad_mode == RSA_X931_PADDING) {
279 if (pkey_rsa_verifyrecover(ctx, NULL, &rslen, sig, siglen) <= 0)
281 } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) {
283 if (!setup_tbuf(rctx, ctx))
285 ret = RSA_public_decrypt(siglen, sig, rctx->tbuf,
286 rsa, RSA_NO_PADDING);
289 ret = RSA_verify_PKCS1_PSS_mgf1(rsa, tbs,
290 rctx->md, rctx->mgf1md,
291 rctx->tbuf, rctx->saltlen);
299 if (!setup_tbuf(rctx, ctx))
301 rslen = RSA_public_decrypt(siglen, sig, rctx->tbuf,
302 rsa, rctx->pad_mode);
307 if ((rslen != tbslen) || memcmp(tbs, rctx->tbuf, rslen))
314 static int pkey_rsa_encrypt(EVP_PKEY_CTX *ctx,
315 unsigned char *out, size_t *outlen,
316 const unsigned char *in, size_t inlen)
319 RSA_PKEY_CTX *rctx = ctx->data;
321 * Discard const. Its marked as const because this may be a cached copy of
322 * the "real" key. These calls don't make any modifications that need to
323 * be reflected back in the "original" key.
325 RSA *rsa = (RSA *)EVP_PKEY_get0_RSA(ctx->pkey);
327 if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
328 int klen = RSA_size(rsa);
329 if (!setup_tbuf(rctx, ctx))
331 if (!RSA_padding_add_PKCS1_OAEP_mgf1(rctx->tbuf, klen,
335 rctx->md, rctx->mgf1md))
337 ret = RSA_public_encrypt(klen, rctx->tbuf, out, rsa, RSA_NO_PADDING);
339 ret = RSA_public_encrypt(inlen, in, out, rsa, rctx->pad_mode);
347 static int pkey_rsa_decrypt(EVP_PKEY_CTX *ctx,
348 unsigned char *out, size_t *outlen,
349 const unsigned char *in, size_t inlen)
353 RSA_PKEY_CTX *rctx = ctx->data;
355 * Discard const. Its marked as const because this may be a cached copy of
356 * the "real" key. These calls don't make any modifications that need to
357 * be reflected back in the "original" key.
359 RSA *rsa = (RSA *)EVP_PKEY_get0_RSA(ctx->pkey);
361 if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
362 if (!setup_tbuf(rctx, ctx))
364 ret = RSA_private_decrypt(inlen, in, rctx->tbuf, rsa, RSA_NO_PADDING);
367 ret = RSA_padding_check_PKCS1_OAEP_mgf1(out, ret, rctx->tbuf,
371 rctx->md, rctx->mgf1md);
373 if (rctx->pad_mode == RSA_PKCS1_PADDING &&
374 rctx->implicit_rejection == 0)
375 pad_mode = RSA_PKCS1_NO_IMPLICIT_REJECT_PADDING;
377 pad_mode = rctx->pad_mode;
378 ret = RSA_private_decrypt(inlen, in, out, rsa, pad_mode);
380 *outlen = constant_time_select_s(constant_time_msb_s(ret), *outlen, ret);
381 ret = constant_time_select_int(constant_time_msb(ret), ret, 1);
385 static int check_padding_md(const EVP_MD *md, int padding)
392 mdnid = EVP_MD_get_type(md);
394 if (padding == RSA_NO_PADDING) {
395 ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PADDING_MODE);
399 if (padding == RSA_X931_PADDING) {
400 if (RSA_X931_hash_id(mdnid) == -1) {
401 ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_X931_DIGEST);
406 /* List of all supported RSA digests */
427 ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_DIGEST);
436 static int pkey_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
438 RSA_PKEY_CTX *rctx = ctx->data;
441 case EVP_PKEY_CTRL_RSA_PADDING:
442 if ((p1 >= RSA_PKCS1_PADDING) && (p1 <= RSA_PKCS1_PSS_PADDING)) {
443 if (!check_padding_md(rctx->md, p1))
445 if (p1 == RSA_PKCS1_PSS_PADDING) {
446 if (!(ctx->operation &
447 (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY)))
450 rctx->md = EVP_sha1();
451 } else if (pkey_ctx_is_pss(ctx)) {
454 if (p1 == RSA_PKCS1_OAEP_PADDING) {
455 if (!(ctx->operation & EVP_PKEY_OP_TYPE_CRYPT))
458 rctx->md = EVP_sha1();
464 ERR_raise(ERR_LIB_RSA, RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
467 case EVP_PKEY_CTRL_GET_RSA_PADDING:
468 *(int *)p2 = rctx->pad_mode;
471 case EVP_PKEY_CTRL_RSA_PSS_SALTLEN:
472 case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN:
473 if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING) {
474 ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PSS_SALTLEN);
477 if (type == EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN) {
478 *(int *)p2 = rctx->saltlen;
480 if (p1 < RSA_PSS_SALTLEN_MAX)
482 if (rsa_pss_restricted(rctx)) {
483 if (p1 == RSA_PSS_SALTLEN_AUTO
484 && ctx->operation == EVP_PKEY_OP_VERIFY) {
485 ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PSS_SALTLEN);
488 if ((p1 == RSA_PSS_SALTLEN_DIGEST
489 && rctx->min_saltlen > EVP_MD_get_size(rctx->md))
490 || (p1 >= 0 && p1 < rctx->min_saltlen)) {
491 ERR_raise(ERR_LIB_RSA, RSA_R_PSS_SALTLEN_TOO_SMALL);
499 case EVP_PKEY_CTRL_RSA_KEYGEN_BITS:
500 if (p1 < RSA_MIN_MODULUS_BITS) {
501 ERR_raise(ERR_LIB_RSA, RSA_R_KEY_SIZE_TOO_SMALL);
507 case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP:
508 if (p2 == NULL || !BN_is_odd((BIGNUM *)p2) || BN_is_one((BIGNUM *)p2)) {
509 ERR_raise(ERR_LIB_RSA, RSA_R_BAD_E_VALUE);
512 BN_free(rctx->pub_exp);
516 case EVP_PKEY_CTRL_RSA_KEYGEN_PRIMES:
517 if (p1 < RSA_DEFAULT_PRIME_NUM || p1 > RSA_MAX_PRIME_NUM) {
518 ERR_raise(ERR_LIB_RSA, RSA_R_KEY_PRIME_NUM_INVALID);
524 case EVP_PKEY_CTRL_RSA_OAEP_MD:
525 case EVP_PKEY_CTRL_GET_RSA_OAEP_MD:
526 if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
527 ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PADDING_MODE);
530 if (type == EVP_PKEY_CTRL_GET_RSA_OAEP_MD)
531 *(const EVP_MD **)p2 = rctx->md;
536 case EVP_PKEY_CTRL_MD:
537 if (!check_padding_md(p2, rctx->pad_mode))
539 if (rsa_pss_restricted(rctx)) {
540 if (EVP_MD_get_type(rctx->md) == EVP_MD_get_type(p2))
542 ERR_raise(ERR_LIB_RSA, RSA_R_DIGEST_NOT_ALLOWED);
548 case EVP_PKEY_CTRL_GET_MD:
549 *(const EVP_MD **)p2 = rctx->md;
552 case EVP_PKEY_CTRL_RSA_MGF1_MD:
553 case EVP_PKEY_CTRL_GET_RSA_MGF1_MD:
554 if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING
555 && rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
556 ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_MGF1_MD);
559 if (type == EVP_PKEY_CTRL_GET_RSA_MGF1_MD) {
561 *(const EVP_MD **)p2 = rctx->mgf1md;
563 *(const EVP_MD **)p2 = rctx->md;
565 if (rsa_pss_restricted(rctx)) {
566 if (EVP_MD_get_type(rctx->mgf1md) == EVP_MD_get_type(p2))
568 ERR_raise(ERR_LIB_RSA, RSA_R_MGF1_DIGEST_NOT_ALLOWED);
575 case EVP_PKEY_CTRL_RSA_OAEP_LABEL:
576 if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
577 ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PADDING_MODE);
580 OPENSSL_free(rctx->oaep_label);
582 rctx->oaep_label = p2;
583 rctx->oaep_labellen = p1;
585 rctx->oaep_label = NULL;
586 rctx->oaep_labellen = 0;
590 case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL:
591 if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
592 ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PADDING_MODE);
596 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
599 *(unsigned char **)p2 = rctx->oaep_label;
600 return rctx->oaep_labellen;
602 case EVP_PKEY_CTRL_RSA_IMPLICIT_REJECTION:
603 if (rctx->pad_mode != RSA_PKCS1_PADDING) {
604 ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PADDING_MODE);
607 rctx->implicit_rejection = p1;
610 case EVP_PKEY_CTRL_DIGESTINIT:
611 case EVP_PKEY_CTRL_PKCS7_SIGN:
612 #ifndef OPENSSL_NO_CMS
613 case EVP_PKEY_CTRL_CMS_SIGN:
617 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
618 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
619 #ifndef OPENSSL_NO_CMS
620 case EVP_PKEY_CTRL_CMS_DECRYPT:
621 case EVP_PKEY_CTRL_CMS_ENCRYPT:
623 if (!pkey_ctx_is_pss(ctx))
626 case EVP_PKEY_CTRL_PEER_KEY:
627 ERR_raise(ERR_LIB_RSA, RSA_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
636 static int pkey_rsa_ctrl_str(EVP_PKEY_CTX *ctx,
637 const char *type, const char *value)
640 ERR_raise(ERR_LIB_RSA, RSA_R_VALUE_MISSING);
643 if (strcmp(type, "rsa_padding_mode") == 0) {
646 if (strcmp(value, "pkcs1") == 0) {
647 pm = RSA_PKCS1_PADDING;
648 } else if (strcmp(value, "none") == 0) {
650 } else if (strcmp(value, "oeap") == 0) {
651 pm = RSA_PKCS1_OAEP_PADDING;
652 } else if (strcmp(value, "oaep") == 0) {
653 pm = RSA_PKCS1_OAEP_PADDING;
654 } else if (strcmp(value, "x931") == 0) {
655 pm = RSA_X931_PADDING;
656 } else if (strcmp(value, "pss") == 0) {
657 pm = RSA_PKCS1_PSS_PADDING;
659 ERR_raise(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE);
662 return EVP_PKEY_CTX_set_rsa_padding(ctx, pm);
665 if (strcmp(type, "rsa_pss_saltlen") == 0) {
668 if (!strcmp(value, "digest"))
669 saltlen = RSA_PSS_SALTLEN_DIGEST;
670 else if (!strcmp(value, "max"))
671 saltlen = RSA_PSS_SALTLEN_MAX;
672 else if (!strcmp(value, "auto"))
673 saltlen = RSA_PSS_SALTLEN_AUTO;
675 saltlen = atoi(value);
676 return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, saltlen);
679 if (strcmp(type, "rsa_keygen_bits") == 0) {
680 int nbits = atoi(value);
682 return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, nbits);
685 if (strcmp(type, "rsa_keygen_pubexp") == 0) {
688 BIGNUM *pubexp = NULL;
689 if (!BN_asc2bn(&pubexp, value))
691 ret = EVP_PKEY_CTX_set1_rsa_keygen_pubexp(ctx, pubexp);
696 if (strcmp(type, "rsa_keygen_primes") == 0) {
697 int nprimes = atoi(value);
699 return EVP_PKEY_CTX_set_rsa_keygen_primes(ctx, nprimes);
702 if (strcmp(type, "rsa_mgf1_md") == 0)
703 return EVP_PKEY_CTX_md(ctx,
704 EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
705 EVP_PKEY_CTRL_RSA_MGF1_MD, value);
707 if (pkey_ctx_is_pss(ctx)) {
709 if (strcmp(type, "rsa_pss_keygen_mgf1_md") == 0)
710 return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_KEYGEN,
711 EVP_PKEY_CTRL_RSA_MGF1_MD, value);
713 if (strcmp(type, "rsa_pss_keygen_md") == 0)
714 return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_KEYGEN,
715 EVP_PKEY_CTRL_MD, value);
717 if (strcmp(type, "rsa_pss_keygen_saltlen") == 0) {
718 int saltlen = atoi(value);
720 return EVP_PKEY_CTX_set_rsa_pss_keygen_saltlen(ctx, saltlen);
724 if (strcmp(type, "rsa_oaep_md") == 0)
725 return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_CRYPT,
726 EVP_PKEY_CTRL_RSA_OAEP_MD, value);
728 if (strcmp(type, "rsa_oaep_label") == 0) {
733 lab = OPENSSL_hexstr2buf(value, &lablen);
736 ret = EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, lab, lablen);
745 /* Set PSS parameters when generating a key, if necessary */
746 static int rsa_set_pss_param(RSA *rsa, EVP_PKEY_CTX *ctx)
748 RSA_PKEY_CTX *rctx = ctx->data;
750 if (!pkey_ctx_is_pss(ctx))
752 /* If all parameters are default values don't set pss */
753 if (rctx->md == NULL && rctx->mgf1md == NULL && rctx->saltlen == -2)
755 rsa->pss = ossl_rsa_pss_params_create(rctx->md, rctx->mgf1md,
757 ? 0 : rctx->saltlen);
758 if (rsa->pss == NULL)
763 static int pkey_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
766 RSA_PKEY_CTX *rctx = ctx->data;
770 if (rctx->pub_exp == NULL) {
771 rctx->pub_exp = BN_new();
772 if (rctx->pub_exp == NULL || !BN_set_word(rctx->pub_exp, RSA_F4))
778 if (ctx->pkey_gencb) {
779 pcb = BN_GENCB_new();
784 evp_pkey_set_cb_translate(pcb, ctx);
788 ret = RSA_generate_multi_prime_key(rsa, rctx->nbits, rctx->primes,
791 if (ret > 0 && !rsa_set_pss_param(rsa, ctx)) {
796 EVP_PKEY_assign(pkey, ctx->pmeth->pkey_id, rsa);
802 static const EVP_PKEY_METHOD rsa_pkey_meth = {
804 EVP_PKEY_FLAG_AUTOARGLEN,
821 pkey_rsa_verifyrecover,
837 const EVP_PKEY_METHOD *ossl_rsa_pkey_method(void)
839 return &rsa_pkey_meth;
843 * Called for PSS sign or verify initialisation: checks PSS parameter
844 * sanity and sets any restrictions on key usage.
847 static int pkey_pss_init(EVP_PKEY_CTX *ctx)
850 RSA_PKEY_CTX *rctx = ctx->data;
852 const EVP_MD *mgf1md;
853 int min_saltlen, max_saltlen;
855 /* Should never happen */
856 if (!pkey_ctx_is_pss(ctx))
858 rsa = EVP_PKEY_get0_RSA(ctx->pkey);
859 /* If no restrictions just return */
860 if (rsa->pss == NULL)
862 /* Get and check parameters */
863 if (!ossl_rsa_pss_get_param(rsa->pss, &md, &mgf1md, &min_saltlen))
866 /* See if minimum salt length exceeds maximum possible */
867 max_saltlen = RSA_size(rsa) - EVP_MD_get_size(md);
868 if ((RSA_bits(rsa) & 0x7) == 1)
870 if (min_saltlen > max_saltlen) {
871 ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_SALT_LENGTH);
875 rctx->min_saltlen = min_saltlen;
878 * Set PSS restrictions as defaults: we can then block any attempt to
879 * use invalid values in pkey_rsa_ctrl
883 rctx->mgf1md = mgf1md;
884 rctx->saltlen = min_saltlen;
889 static const EVP_PKEY_METHOD rsa_pss_pkey_meth = {
891 EVP_PKEY_FLAG_AUTOARGLEN,
907 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
913 const EVP_PKEY_METHOD *ossl_rsa_pss_pkey_method(void)
915 return &rsa_pss_pkey_meth;