2 * Copyright 2019-2020 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"
17 #include <openssl/crypto.h>
18 #include <openssl/core_numbers.h>
19 #include <openssl/core_names.h>
20 #include <openssl/err.h>
21 #include <openssl/rsa.h>
22 #include <openssl/params.h>
23 #include <openssl/evp.h>
24 #include "internal/cryptlib.h"
25 #include "internal/nelem.h"
26 #include "internal/sizes.h"
27 #include "crypto/rsa.h"
28 #include "prov/providercommonerr.h"
29 #include "prov/implementations.h"
30 #include "prov/provider_ctx.h"
31 #include "prov/der_rsa.h"
33 static OSSL_OP_signature_newctx_fn rsa_newctx
;
34 static OSSL_OP_signature_sign_init_fn rsa_sign_init
;
35 static OSSL_OP_signature_verify_init_fn rsa_verify_init
;
36 static OSSL_OP_signature_verify_recover_init_fn rsa_verify_recover_init
;
37 static OSSL_OP_signature_sign_fn rsa_sign
;
38 static OSSL_OP_signature_verify_fn rsa_verify
;
39 static OSSL_OP_signature_verify_recover_fn rsa_verify_recover
;
40 static OSSL_OP_signature_digest_sign_init_fn rsa_digest_sign_init
;
41 static OSSL_OP_signature_digest_sign_update_fn rsa_digest_signverify_update
;
42 static OSSL_OP_signature_digest_sign_final_fn rsa_digest_sign_final
;
43 static OSSL_OP_signature_digest_verify_init_fn rsa_digest_verify_init
;
44 static OSSL_OP_signature_digest_verify_update_fn rsa_digest_signverify_update
;
45 static OSSL_OP_signature_digest_verify_final_fn rsa_digest_verify_final
;
46 static OSSL_OP_signature_freectx_fn rsa_freectx
;
47 static OSSL_OP_signature_dupctx_fn rsa_dupctx
;
48 static OSSL_OP_signature_get_ctx_params_fn rsa_get_ctx_params
;
49 static OSSL_OP_signature_gettable_ctx_params_fn rsa_gettable_ctx_params
;
50 static OSSL_OP_signature_set_ctx_params_fn rsa_set_ctx_params
;
51 static OSSL_OP_signature_settable_ctx_params_fn rsa_settable_ctx_params
;
52 static OSSL_OP_signature_get_ctx_md_params_fn rsa_get_ctx_md_params
;
53 static OSSL_OP_signature_gettable_ctx_md_params_fn rsa_gettable_ctx_md_params
;
54 static OSSL_OP_signature_set_ctx_md_params_fn rsa_set_ctx_md_params
;
55 static OSSL_OP_signature_settable_ctx_md_params_fn rsa_settable_ctx_md_params
;
57 static OSSL_ITEM padding_item
[] = {
58 { RSA_PKCS1_PADDING
, "pkcs1" },
59 { RSA_SSLV23_PADDING
, "sslv23" },
60 { RSA_NO_PADDING
, "none" },
61 { RSA_PKCS1_OAEP_PADDING
, "oaep" }, /* Correct spelling first */
62 { RSA_PKCS1_OAEP_PADDING
, "oeap" },
63 { RSA_X931_PADDING
, "x931" },
64 { RSA_PKCS1_PSS_PADDING
, "pss" },
69 * What's passed as an actual key is defined by the KEYMGMT interface.
70 * We happen to know that our KEYMGMT simply passes RSA structures, so
71 * we use that here too.
81 * Flag to determine if the hash function can be changed (1) or not (0)
82 * Because it's dangerous to change during a DigestSign or DigestVerify
83 * operation, this flag is cleared by their Init function, and set again
84 * by their Final function.
86 unsigned int flag_allow_md
: 1;
88 /* The Algorithm Identifier of the combined signature agorithm */
89 unsigned char aid_buf
[128];
97 char mdname
[OSSL_MAX_NAME_SIZE
]; /* Purely informational */
99 /* RSA padding mode */
101 /* message digest for MGF1 */
103 char mgf1_mdname
[OSSL_MAX_NAME_SIZE
]; /* Purely informational */
104 /* PSS salt length */
106 /* Minimum salt length or -1 if no PSS parameter restriction */
114 static size_t rsa_get_md_size(const PROV_RSA_CTX
*prsactx
)
116 if (prsactx
->md
!= NULL
)
117 return EVP_MD_size(prsactx
->md
);
121 static int rsa_get_md_nid(const EVP_MD
*md
)
124 * Because the RSA library deals with NIDs, we need to translate.
125 * We do so using EVP_MD_is_a(), and therefore need a name to NID
128 static const OSSL_ITEM name_to_nid
[] = {
129 { NID_sha1
, OSSL_DIGEST_NAME_SHA1
},
130 { NID_sha224
, OSSL_DIGEST_NAME_SHA2_224
},
131 { NID_sha256
, OSSL_DIGEST_NAME_SHA2_256
},
132 { NID_sha384
, OSSL_DIGEST_NAME_SHA2_384
},
133 { NID_sha512
, OSSL_DIGEST_NAME_SHA2_512
},
134 { NID_sha512_224
, OSSL_DIGEST_NAME_SHA2_512_224
},
135 { NID_sha512_256
, OSSL_DIGEST_NAME_SHA2_512_256
},
136 { NID_md5
, OSSL_DIGEST_NAME_MD5
},
137 { NID_md5_sha1
, OSSL_DIGEST_NAME_MD5_SHA1
},
138 { NID_md2
, OSSL_DIGEST_NAME_MD2
},
139 { NID_md4
, OSSL_DIGEST_NAME_MD4
},
140 { NID_mdc2
, OSSL_DIGEST_NAME_MDC2
},
141 { NID_ripemd160
, OSSL_DIGEST_NAME_RIPEMD160
},
142 { NID_sha3_224
, OSSL_DIGEST_NAME_SHA3_224
},
143 { NID_sha3_256
, OSSL_DIGEST_NAME_SHA3_256
},
144 { NID_sha3_384
, OSSL_DIGEST_NAME_SHA3_384
},
145 { NID_sha3_512
, OSSL_DIGEST_NAME_SHA3_512
},
148 int mdnid
= NID_undef
;
153 for (i
= 0; i
< OSSL_NELEM(name_to_nid
); i
++) {
154 if (EVP_MD_is_a(md
, name_to_nid
[i
].ptr
)) {
155 mdnid
= (int)name_to_nid
[i
].id
;
160 if (mdnid
== NID_undef
)
161 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_DIGEST
);
167 static int rsa_check_padding(int mdnid
, int padding
)
169 if (padding
== RSA_NO_PADDING
) {
170 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_PADDING_MODE
);
174 if (padding
== RSA_X931_PADDING
) {
175 if (RSA_X931_hash_id(mdnid
) == -1) {
176 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_X931_DIGEST
);
184 static void *rsa_newctx(void *provctx
, const char *propq
)
186 PROV_RSA_CTX
*prsactx
= OPENSSL_zalloc(sizeof(PROV_RSA_CTX
));
191 prsactx
->libctx
= PROV_LIBRARY_CONTEXT_OF(provctx
);
192 prsactx
->flag_allow_md
= 1;
193 if (propq
!= NULL
&& (prsactx
->propq
= OPENSSL_strdup(propq
)) == NULL
) {
194 OPENSSL_free(prsactx
);
196 ERR_raise(ERR_LIB_PROV
, ERR_R_MALLOC_FAILURE
);
201 /* True if PSS parameters are restricted */
202 #define rsa_pss_restricted(prsactx) (prsactx->min_saltlen != -1)
204 static int rsa_signature_init(void *vprsactx
, void *vrsa
, int operation
)
206 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
208 if (prsactx
== NULL
|| vrsa
== NULL
|| !RSA_up_ref(vrsa
))
211 RSA_free(prsactx
->rsa
);
213 prsactx
->operation
= operation
;
214 if (RSA_get0_pss_params(prsactx
->rsa
) != NULL
)
215 prsactx
->pad_mode
= RSA_PKCS1_PSS_PADDING
;
217 prsactx
->pad_mode
= RSA_PKCS1_PADDING
;
218 /* Maximum for sign, auto for verify */
219 prsactx
->saltlen
= RSA_PSS_SALTLEN_AUTO
;
220 prsactx
->min_saltlen
= -1;
225 static int rsa_setup_md(PROV_RSA_CTX
*ctx
, const char *mdname
,
229 mdprops
= ctx
->propq
;
231 if (mdname
!= NULL
) {
232 EVP_MD
*md
= EVP_MD_fetch(ctx
->libctx
, mdname
, mdprops
);
233 int md_nid
= rsa_get_md_nid(md
);
237 || md_nid
== NID_undef
238 || !rsa_check_padding(md_nid
, ctx
->pad_mode
)) {
243 EVP_MD_CTX_free(ctx
->mdctx
);
244 EVP_MD_free(ctx
->md
);
247 * TODO(3.0) Should we care about DER writing errors?
248 * All it really means is that for some reason, there's no
249 * AlgorithmIdentifier to be had (consider RSA with MD5-SHA1),
250 * but the operation itself is still valid, just as long as it's
251 * not used to construct anything that needs an AlgorithmIdentifier.
254 if (WPACKET_init_der(&pkt
, ctx
->aid_buf
, sizeof(ctx
->aid_buf
))
255 && DER_w_algorithmIdentifier_RSA_with(&pkt
, -1, ctx
->rsa
, md_nid
)
256 && WPACKET_finish(&pkt
)) {
257 WPACKET_get_total_written(&pkt
, &ctx
->aid_len
);
258 ctx
->aid
= WPACKET_get_curr(&pkt
);
260 WPACKET_cleanup(&pkt
);
265 OPENSSL_strlcpy(ctx
->mdname
, mdname
, sizeof(ctx
->mdname
));
271 static int rsa_setup_mgf1_md(PROV_RSA_CTX
*ctx
, const char *mdname
,
275 mdprops
= ctx
->propq
;
277 if (ctx
->mgf1_mdname
[0] != '\0')
278 EVP_MD_free(ctx
->mgf1_md
);
280 if ((ctx
->mgf1_md
= EVP_MD_fetch(ctx
->libctx
, mdname
, mdprops
)) == NULL
)
282 OPENSSL_strlcpy(ctx
->mgf1_mdname
, mdname
, sizeof(ctx
->mgf1_mdname
));
287 static int setup_tbuf(PROV_RSA_CTX
*ctx
)
289 if (ctx
->tbuf
!= NULL
)
291 if ((ctx
->tbuf
= OPENSSL_malloc(RSA_size(ctx
->rsa
))) == NULL
) {
292 ERR_raise(ERR_LIB_PROV
, ERR_R_MALLOC_FAILURE
);
298 static void clean_tbuf(PROV_RSA_CTX
*ctx
)
300 if (ctx
->tbuf
!= NULL
)
301 OPENSSL_cleanse(ctx
->tbuf
, RSA_size(ctx
->rsa
));
304 static void free_tbuf(PROV_RSA_CTX
*ctx
)
306 OPENSSL_clear_free(ctx
->tbuf
, RSA_size(ctx
->rsa
));
310 static int rsa_sign_init(void *vprsactx
, void *vrsa
)
312 return rsa_signature_init(vprsactx
, vrsa
, EVP_PKEY_OP_SIGN
);
315 static int rsa_sign(void *vprsactx
, unsigned char *sig
, size_t *siglen
,
316 size_t sigsize
, const unsigned char *tbs
, size_t tbslen
)
318 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
320 size_t rsasize
= RSA_size(prsactx
->rsa
);
321 size_t mdsize
= rsa_get_md_size(prsactx
);
328 if (sigsize
< (size_t)rsasize
)
332 if (tbslen
!= mdsize
) {
333 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_DIGEST_LENGTH
);
338 if (EVP_MD_is_a(prsactx
->md
, OSSL_DIGEST_NAME_MDC2
)) {
341 if (prsactx
->pad_mode
!= RSA_PKCS1_PADDING
) {
342 ERR_raise_data(ERR_LIB_PROV
, PROV_R_INVALID_PADDING_MODE
,
343 "only PKCS#1 padding supported with MDC2");
346 ret
= RSA_sign_ASN1_OCTET_STRING(0, tbs
, tbslen
, sig
, &sltmp
,
350 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
357 switch (prsactx
->pad_mode
) {
358 case RSA_X931_PADDING
:
359 if ((size_t)RSA_size(prsactx
->rsa
) < tbslen
+ 1) {
360 ERR_raise(ERR_LIB_PROV
, PROV_R_KEY_SIZE_TOO_SMALL
);
363 if (!setup_tbuf(prsactx
)) {
364 ERR_raise(ERR_LIB_PROV
, ERR_R_MALLOC_FAILURE
);
367 memcpy(prsactx
->tbuf
, tbs
, tbslen
);
368 prsactx
->tbuf
[tbslen
] = RSA_X931_hash_id(prsactx
->mdnid
);
369 ret
= RSA_private_encrypt(tbslen
+ 1, prsactx
->tbuf
,
370 sig
, prsactx
->rsa
, RSA_X931_PADDING
);
374 case RSA_PKCS1_PADDING
:
378 ret
= RSA_sign(prsactx
->mdnid
, tbs
, tbslen
, sig
, &sltmp
,
381 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
388 case RSA_PKCS1_PSS_PADDING
:
389 /* Check PSS restrictions */
390 if (rsa_pss_restricted(prsactx
)) {
391 switch (prsactx
->saltlen
) {
392 case RSA_PSS_SALTLEN_DIGEST
:
393 if (prsactx
->min_saltlen
> EVP_MD_size(prsactx
->md
)) {
394 ERR_raise(ERR_LIB_PROV
, PROV_R_PSS_SALTLEN_TOO_SMALL
);
399 if (prsactx
->saltlen
>= 0
400 && prsactx
->saltlen
< prsactx
->min_saltlen
) {
401 ERR_raise(ERR_LIB_PROV
, PROV_R_PSS_SALTLEN_TOO_SMALL
);
407 if (!setup_tbuf(prsactx
))
409 if (!RSA_padding_add_PKCS1_PSS_mgf1(prsactx
->rsa
,
411 prsactx
->md
, prsactx
->mgf1_md
,
413 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
416 ret
= RSA_private_encrypt(RSA_size(prsactx
->rsa
), prsactx
->tbuf
,
417 sig
, prsactx
->rsa
, RSA_NO_PADDING
);
422 ERR_raise_data(ERR_LIB_PROV
, PROV_R_INVALID_PADDING_MODE
,
423 "Only X.931, PKCS#1 v1.5 or PSS padding allowed");
427 ret
= RSA_private_encrypt(tbslen
, tbs
, sig
, prsactx
->rsa
,
435 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
443 static int rsa_verify_recover_init(void *vprsactx
, void *vrsa
)
445 return rsa_signature_init(vprsactx
, vrsa
, EVP_PKEY_OP_VERIFYRECOVER
);
448 static int rsa_verify_recover(void *vprsactx
,
452 const unsigned char *sig
,
455 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
459 *routlen
= RSA_size(prsactx
->rsa
);
463 if (prsactx
->md
!= NULL
) {
464 switch (prsactx
->pad_mode
) {
465 case RSA_X931_PADDING
:
466 if (!setup_tbuf(prsactx
))
468 ret
= RSA_public_decrypt(siglen
, sig
, prsactx
->tbuf
, prsactx
->rsa
,
471 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
475 if (prsactx
->tbuf
[ret
] != RSA_X931_hash_id(prsactx
->mdnid
)) {
476 ERR_raise(ERR_LIB_PROV
, PROV_R_ALGORITHM_MISMATCH
);
479 if (ret
!= EVP_MD_size(prsactx
->md
)) {
480 ERR_raise_data(ERR_LIB_PROV
, PROV_R_INVALID_DIGEST_LENGTH
,
481 "Should be %d, but got %d",
482 EVP_MD_size(prsactx
->md
), ret
);
487 if (routsize
< (size_t)ret
) {
488 ERR_raise(ERR_LIB_PROV
, PROV_R_OUTPUT_BUFFER_TOO_SMALL
);
491 memcpy(rout
, prsactx
->tbuf
, ret
);
494 case RSA_PKCS1_PADDING
:
498 ret
= int_rsa_verify(prsactx
->mdnid
, NULL
, 0, rout
, &sltmp
,
499 sig
, siglen
, prsactx
->rsa
);
501 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
509 ERR_raise_data(ERR_LIB_PROV
, PROV_R_INVALID_PADDING_MODE
,
510 "Only X.931 or PKCS#1 v1.5 padding allowed");
514 ret
= RSA_public_decrypt(siglen
, sig
, rout
, prsactx
->rsa
,
517 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
525 static int rsa_verify_init(void *vprsactx
, void *vrsa
)
527 return rsa_signature_init(vprsactx
, vrsa
, EVP_PKEY_OP_VERIFY
);
530 static int rsa_verify(void *vprsactx
, const unsigned char *sig
, size_t siglen
,
531 const unsigned char *tbs
, size_t tbslen
)
533 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
536 if (prsactx
->md
!= NULL
) {
537 switch (prsactx
->pad_mode
) {
538 case RSA_PKCS1_PADDING
:
539 if (!RSA_verify(prsactx
->mdnid
, tbs
, tbslen
, sig
, siglen
,
541 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
545 case RSA_X931_PADDING
:
546 if (rsa_verify_recover(prsactx
, NULL
, &rslen
, 0, sig
, siglen
) <= 0)
549 case RSA_PKCS1_PSS_PADDING
:
555 * We need to check this for the RSA_verify_PKCS1_PSS_mgf1()
558 mdsize
= rsa_get_md_size(prsactx
);
559 if (tbslen
!= mdsize
) {
560 ERR_raise_data(ERR_LIB_PROV
, PROV_R_INVALID_DIGEST_LENGTH
,
561 "Should be %d, but got %d",
566 if (!setup_tbuf(prsactx
))
568 ret
= RSA_public_decrypt(siglen
, sig
, prsactx
->tbuf
,
569 prsactx
->rsa
, RSA_NO_PADDING
);
571 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
574 ret
= RSA_verify_PKCS1_PSS_mgf1(prsactx
->rsa
, tbs
,
575 prsactx
->md
, prsactx
->mgf1_md
,
579 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
585 ERR_raise_data(ERR_LIB_PROV
, PROV_R_INVALID_PADDING_MODE
,
586 "Only X.931, PKCS#1 v1.5 or PSS padding allowed");
590 if (!setup_tbuf(prsactx
))
592 rslen
= RSA_public_decrypt(siglen
, sig
, prsactx
->tbuf
, prsactx
->rsa
,
595 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
600 if ((rslen
!= tbslen
) || memcmp(tbs
, prsactx
->tbuf
, rslen
))
606 static int rsa_digest_signverify_init(void *vprsactx
, const char *mdname
,
607 void *vrsa
, int operation
)
609 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
611 prsactx
->flag_allow_md
= 0;
612 if (!rsa_signature_init(vprsactx
, vrsa
, operation
)
613 || !rsa_setup_md(prsactx
, mdname
, NULL
))
616 prsactx
->mdctx
= EVP_MD_CTX_new();
617 if (prsactx
->mdctx
== NULL
)
620 if (!EVP_DigestInit_ex(prsactx
->mdctx
, prsactx
->md
, NULL
))
626 EVP_MD_CTX_free(prsactx
->mdctx
);
627 EVP_MD_free(prsactx
->md
);
628 prsactx
->mdctx
= NULL
;
633 static int rsa_digest_signverify_update(void *vprsactx
,
634 const unsigned char *data
,
637 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
639 if (prsactx
== NULL
|| prsactx
->mdctx
== NULL
)
642 return EVP_DigestUpdate(prsactx
->mdctx
, data
, datalen
);
645 static int rsa_digest_sign_init(void *vprsactx
, const char *mdname
,
646 const char *props
, void *vrsa
)
648 return rsa_digest_signverify_init(vprsactx
, mdname
, props
, vrsa
,
652 static int rsa_digest_sign_final(void *vprsactx
, unsigned char *sig
,
653 size_t *siglen
, size_t sigsize
)
655 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
656 unsigned char digest
[EVP_MAX_MD_SIZE
];
657 unsigned int dlen
= 0;
659 prsactx
->flag_allow_md
= 1;
660 if (prsactx
== NULL
|| prsactx
->mdctx
== NULL
)
664 * If sig is NULL then we're just finding out the sig size. Other fields
665 * are ignored. Defer to rsa_sign.
669 * TODO(3.0): There is the possibility that some externally provided
670 * digests exceed EVP_MAX_MD_SIZE. We should probably handle that somehow -
671 * but that problem is much larger than just in RSA.
673 if (!EVP_DigestFinal_ex(prsactx
->mdctx
, digest
, &dlen
))
677 return rsa_sign(vprsactx
, sig
, siglen
, sigsize
, digest
, (size_t)dlen
);
680 static int rsa_digest_verify_init(void *vprsactx
, const char *mdname
,
681 const char *props
, void *vrsa
)
683 return rsa_digest_signverify_init(vprsactx
, mdname
, props
, vrsa
,
687 int rsa_digest_verify_final(void *vprsactx
, const unsigned char *sig
,
690 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
691 unsigned char digest
[EVP_MAX_MD_SIZE
];
692 unsigned int dlen
= 0;
694 prsactx
->flag_allow_md
= 1;
695 if (prsactx
== NULL
|| prsactx
->mdctx
== NULL
)
699 * TODO(3.0): There is the possibility that some externally provided
700 * digests exceed EVP_MAX_MD_SIZE. We should probably handle that somehow -
701 * but that problem is much larger than just in RSA.
703 if (!EVP_DigestFinal_ex(prsactx
->mdctx
, digest
, &dlen
))
706 return rsa_verify(vprsactx
, sig
, siglen
, digest
, (size_t)dlen
);
709 static void rsa_freectx(void *vprsactx
)
711 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
716 RSA_free(prsactx
->rsa
);
717 EVP_MD_CTX_free(prsactx
->mdctx
);
718 EVP_MD_free(prsactx
->md
);
719 EVP_MD_free(prsactx
->mgf1_md
);
720 OPENSSL_free(prsactx
->propq
);
723 OPENSSL_clear_free(prsactx
, sizeof(prsactx
));
726 static void *rsa_dupctx(void *vprsactx
)
728 PROV_RSA_CTX
*srcctx
= (PROV_RSA_CTX
*)vprsactx
;
729 PROV_RSA_CTX
*dstctx
;
731 dstctx
= OPENSSL_zalloc(sizeof(*srcctx
));
738 dstctx
->mdctx
= NULL
;
741 if (srcctx
->rsa
!= NULL
&& !RSA_up_ref(srcctx
->rsa
))
743 dstctx
->rsa
= srcctx
->rsa
;
745 if (srcctx
->md
!= NULL
&& !EVP_MD_up_ref(srcctx
->md
))
747 dstctx
->md
= srcctx
->md
;
749 if (srcctx
->mgf1_md
!= NULL
&& !EVP_MD_up_ref(srcctx
->mgf1_md
))
751 dstctx
->mgf1_md
= srcctx
->mgf1_md
;
753 if (srcctx
->mdctx
!= NULL
) {
754 dstctx
->mdctx
= EVP_MD_CTX_new();
755 if (dstctx
->mdctx
== NULL
756 || !EVP_MD_CTX_copy_ex(dstctx
->mdctx
, srcctx
->mdctx
))
766 static int rsa_get_ctx_params(void *vprsactx
, OSSL_PARAM
*params
)
768 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
771 if (prsactx
== NULL
|| params
== NULL
)
774 p
= OSSL_PARAM_locate(params
, OSSL_SIGNATURE_PARAM_ALGORITHM_ID
);
776 && !OSSL_PARAM_set_octet_string(p
, prsactx
->aid
, prsactx
->aid_len
))
779 p
= OSSL_PARAM_locate(params
, OSSL_SIGNATURE_PARAM_PAD_MODE
);
781 switch (p
->data_type
) {
782 case OSSL_PARAM_INTEGER
:
783 if (!OSSL_PARAM_set_int(p
, prsactx
->pad_mode
))
786 case OSSL_PARAM_UTF8_STRING
:
789 const char *word
= NULL
;
791 for (i
= 0; padding_item
[i
].id
!= 0; i
++) {
792 if (prsactx
->pad_mode
== (int)padding_item
[i
].id
) {
793 word
= padding_item
[i
].ptr
;
799 if (!OSSL_PARAM_set_utf8_string(p
, word
))
802 ERR_raise(ERR_LIB_PROV
, ERR_R_INTERNAL_ERROR
);
810 p
= OSSL_PARAM_locate(params
, OSSL_SIGNATURE_PARAM_DIGEST
);
811 if (p
!= NULL
&& !OSSL_PARAM_set_utf8_string(p
, prsactx
->mdname
))
814 p
= OSSL_PARAM_locate(params
, OSSL_SIGNATURE_PARAM_MGF1_DIGEST
);
815 if (p
!= NULL
&& !OSSL_PARAM_set_utf8_string(p
, prsactx
->mgf1_mdname
))
818 p
= OSSL_PARAM_locate(params
, OSSL_SIGNATURE_PARAM_PSS_SALTLEN
);
820 if (p
->data_type
== OSSL_PARAM_INTEGER
) {
821 if (!OSSL_PARAM_set_int(p
, prsactx
->saltlen
))
823 } else if (p
->data_type
== OSSL_PARAM_UTF8_STRING
) {
824 switch (prsactx
->saltlen
) {
825 case RSA_PSS_SALTLEN_DIGEST
:
826 if (!OSSL_PARAM_set_utf8_string(p
, "digest"))
829 case RSA_PSS_SALTLEN_MAX
:
830 if (!OSSL_PARAM_set_utf8_string(p
, "max"))
833 case RSA_PSS_SALTLEN_AUTO
:
834 if (!OSSL_PARAM_set_utf8_string(p
, "auto"))
838 if (BIO_snprintf(p
->data
, p
->data_size
, "%d", prsactx
->saltlen
)
849 static const OSSL_PARAM known_gettable_ctx_params
[] = {
850 OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_ALGORITHM_ID
, NULL
, 0),
851 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PAD_MODE
, NULL
, 0),
852 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST
, NULL
, 0),
853 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_MGF1_DIGEST
, NULL
, 0),
854 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PSS_SALTLEN
, NULL
, 0),
858 static const OSSL_PARAM
*rsa_gettable_ctx_params(void)
860 return known_gettable_ctx_params
;
863 static int rsa_set_ctx_params(void *vprsactx
, const OSSL_PARAM params
[])
865 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
868 if (prsactx
== NULL
|| params
== NULL
)
871 p
= OSSL_PARAM_locate_const(params
, OSSL_SIGNATURE_PARAM_DIGEST
);
872 /* Not allowed during certain operations */
873 if (p
!= NULL
&& !prsactx
->flag_allow_md
)
876 char mdname
[OSSL_MAX_NAME_SIZE
] = "", *pmdname
= mdname
;
877 char mdprops
[OSSL_MAX_PROPQUERY_SIZE
] = "", *pmdprops
= mdprops
;
878 const OSSL_PARAM
*propsp
=
879 OSSL_PARAM_locate_const(params
,
880 OSSL_SIGNATURE_PARAM_PROPERTIES
);
882 if (!OSSL_PARAM_get_utf8_string(p
, &pmdname
, sizeof(mdname
)))
887 else if (!OSSL_PARAM_get_utf8_string(propsp
,
888 &pmdprops
, sizeof(mdprops
)))
891 /* TODO(3.0) PSS check needs more work */
892 if (rsa_pss_restricted(prsactx
)) {
893 /* TODO(3.0) figure out what to do for prsactx->md == NULL */
894 if (prsactx
->md
== NULL
|| EVP_MD_is_a(prsactx
->md
, mdname
))
896 ERR_raise(ERR_LIB_PROV
, PROV_R_DIGEST_NOT_ALLOWED
);
900 /* non-PSS code follows */
901 if (!rsa_setup_md(prsactx
, mdname
, pmdprops
))
905 p
= OSSL_PARAM_locate_const(params
, OSSL_SIGNATURE_PARAM_PAD_MODE
);
908 const char *err_extra_text
= NULL
;
910 switch (p
->data_type
) {
911 case OSSL_PARAM_INTEGER
: /* Support for legacy pad mode number */
912 if (!OSSL_PARAM_get_int(p
, &pad_mode
))
915 case OSSL_PARAM_UTF8_STRING
:
922 for (i
= 0; padding_item
[i
].id
!= 0; i
++) {
923 if (strcmp(p
->data
, padding_item
[i
].ptr
) == 0) {
924 pad_mode
= padding_item
[i
].id
;
935 case RSA_PKCS1_OAEP_PADDING
:
937 * OAEP padding is for asymmetric cipher only so is not compatible
938 * with signature use.
940 err_extra_text
= "OAEP padding not allowed for signing / verifying";
942 case RSA_PKCS1_PSS_PADDING
:
943 if ((prsactx
->operation
944 & (EVP_PKEY_OP_SIGN
| EVP_PKEY_OP_VERIFY
)) == 0) {
946 "PSS padding only allowed for sign and verify operations";
949 if (prsactx
->md
== NULL
950 && !rsa_setup_md(prsactx
, OSSL_DIGEST_NAME_SHA1
, NULL
)) {
951 ERR_raise_data(ERR_LIB_PROV
, PROV_R_INVALID_DIGEST
,
952 "%s could not be fetched",
953 OSSL_DIGEST_NAME_SHA1
);
957 case RSA_PKCS1_PADDING
:
958 err_extra_text
= "PKCS#1 padding not allowed with RSA-PSS";
960 case RSA_SSLV23_PADDING
:
961 err_extra_text
= "SSLv3 padding not allowed with RSA-PSS";
964 err_extra_text
= "No padding not allowed with RSA-PSS";
966 case RSA_X931_PADDING
:
967 err_extra_text
= "X.931 padding not allowed with RSA-PSS";
969 if (RSA_get0_pss_params(prsactx
->rsa
) == NULL
)
974 if (err_extra_text
== NULL
)
975 ERR_raise(ERR_LIB_PROV
,
976 PROV_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE
);
978 ERR_raise_data(ERR_LIB_PROV
,
979 PROV_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE
,
983 if (!rsa_check_padding(prsactx
->mdnid
, pad_mode
))
985 prsactx
->pad_mode
= pad_mode
;
988 p
= OSSL_PARAM_locate_const(params
, OSSL_SIGNATURE_PARAM_PSS_SALTLEN
);
992 if (prsactx
->pad_mode
!= RSA_PKCS1_PSS_PADDING
) {
993 ERR_raise_data(ERR_LIB_PROV
, PROV_R_NOT_SUPPORTED
,
994 "PSS saltlen can only be specified if "
995 "PSS padding has been specified first");
999 switch (p
->data_type
) {
1000 case OSSL_PARAM_INTEGER
: /* Support for legacy pad mode number */
1001 if (!OSSL_PARAM_get_int(p
, &saltlen
))
1004 case OSSL_PARAM_UTF8_STRING
:
1005 if (strcmp(p
->data
, "digest") == 0)
1006 saltlen
= RSA_PSS_SALTLEN_DIGEST
;
1007 else if (strcmp(p
->data
, "max") == 0)
1008 saltlen
= RSA_PSS_SALTLEN_MAX
;
1009 else if (strcmp(p
->data
, "auto") == 0)
1010 saltlen
= RSA_PSS_SALTLEN_AUTO
;
1012 saltlen
= atoi(p
->data
);
1019 * RSA_PSS_SALTLEN_MAX seems curiously named in this check.
1020 * Contrary to what it's name suggests, it's the currently
1021 * lowest saltlen number possible.
1023 if (saltlen
< RSA_PSS_SALTLEN_MAX
) {
1024 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_PSS_SALTLEN
);
1028 if (rsa_pss_restricted(prsactx
)) {
1029 switch (prsactx
->saltlen
) {
1030 case RSA_PSS_SALTLEN_AUTO
:
1031 if (prsactx
->operation
== EVP_PKEY_OP_VERIFY
) {
1032 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_PSS_SALTLEN
);
1036 case RSA_PSS_SALTLEN_DIGEST
:
1037 if (prsactx
->min_saltlen
> EVP_MD_size(prsactx
->md
)) {
1038 ERR_raise_data(ERR_LIB_PROV
,
1039 PROV_R_PSS_SALTLEN_TOO_SMALL
,
1040 "Should be more than %d, but would be "
1041 "set to match digest size (%d)",
1042 prsactx
->min_saltlen
,
1043 EVP_MD_size(prsactx
->md
));
1048 if (saltlen
>= 0 && saltlen
< prsactx
->min_saltlen
) {
1049 ERR_raise_data(ERR_LIB_PROV
,
1050 PROV_R_PSS_SALTLEN_TOO_SMALL
,
1051 "Should be more than %d, "
1052 "but would be set to %d",
1053 prsactx
->min_saltlen
, saltlen
);
1059 prsactx
->saltlen
= saltlen
;
1062 p
= OSSL_PARAM_locate_const(params
, OSSL_SIGNATURE_PARAM_MGF1_DIGEST
);
1064 char mdname
[OSSL_MAX_NAME_SIZE
] = "", *pmdname
= mdname
;
1065 char mdprops
[OSSL_MAX_PROPQUERY_SIZE
] = "", *pmdprops
= mdprops
;
1066 const OSSL_PARAM
*propsp
=
1067 OSSL_PARAM_locate_const(params
,
1068 OSSL_SIGNATURE_PARAM_MGF1_PROPERTIES
);
1070 if (!OSSL_PARAM_get_utf8_string(p
, &pmdname
, sizeof(mdname
)))
1075 else if (!OSSL_PARAM_get_utf8_string(propsp
,
1076 &pmdprops
, sizeof(mdprops
)))
1079 if (prsactx
->pad_mode
!= RSA_PKCS1_PSS_PADDING
) {
1080 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_MGF1_MD
);
1084 if (rsa_pss_restricted(prsactx
)) {
1085 /* TODO(3.0) figure out what to do for prsactx->mgf1_md == NULL */
1086 if (prsactx
->mgf1_md
== NULL
1087 || EVP_MD_is_a(prsactx
->mgf1_md
, mdname
))
1089 ERR_raise(ERR_LIB_PROV
, PROV_R_DIGEST_NOT_ALLOWED
);
1093 /* non-PSS code follows */
1094 if (!rsa_setup_mgf1_md(prsactx
, mdname
, pmdprops
))
1101 static const OSSL_PARAM known_settable_ctx_params
[] = {
1102 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PAD_MODE
, NULL
, 0),
1103 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST
, NULL
, 0),
1104 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PROPERTIES
, NULL
, 0),
1105 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_MGF1_DIGEST
, NULL
, 0),
1106 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_MGF1_PROPERTIES
, NULL
, 0),
1107 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PSS_SALTLEN
, NULL
, 0),
1111 static const OSSL_PARAM
*rsa_settable_ctx_params(void)
1114 * TODO(3.0): Should this function return a different set of settable ctx
1115 * params if the ctx is being used for a DigestSign/DigestVerify? In that
1116 * case it is not allowed to set the digest size/digest name because the
1117 * digest is explicitly set as part of the init.
1119 return known_settable_ctx_params
;
1122 static int rsa_get_ctx_md_params(void *vprsactx
, OSSL_PARAM
*params
)
1124 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
1126 if (prsactx
->mdctx
== NULL
)
1129 return EVP_MD_CTX_get_params(prsactx
->mdctx
, params
);
1132 static const OSSL_PARAM
*rsa_gettable_ctx_md_params(void *vprsactx
)
1134 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
1136 if (prsactx
->md
== NULL
)
1139 return EVP_MD_gettable_ctx_params(prsactx
->md
);
1142 static int rsa_set_ctx_md_params(void *vprsactx
, const OSSL_PARAM params
[])
1144 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
1146 if (prsactx
->mdctx
== NULL
)
1149 return EVP_MD_CTX_set_params(prsactx
->mdctx
, params
);
1152 static const OSSL_PARAM
*rsa_settable_ctx_md_params(void *vprsactx
)
1154 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
1156 if (prsactx
->md
== NULL
)
1159 return EVP_MD_settable_ctx_params(prsactx
->md
);
1162 const OSSL_DISPATCH rsa_signature_functions
[] = {
1163 { OSSL_FUNC_SIGNATURE_NEWCTX
, (void (*)(void))rsa_newctx
},
1164 { OSSL_FUNC_SIGNATURE_SIGN_INIT
, (void (*)(void))rsa_sign_init
},
1165 { OSSL_FUNC_SIGNATURE_SIGN
, (void (*)(void))rsa_sign
},
1166 { OSSL_FUNC_SIGNATURE_VERIFY_INIT
, (void (*)(void))rsa_verify_init
},
1167 { OSSL_FUNC_SIGNATURE_VERIFY
, (void (*)(void))rsa_verify
},
1168 { OSSL_FUNC_SIGNATURE_VERIFY_RECOVER_INIT
,
1169 (void (*)(void))rsa_verify_recover_init
},
1170 { OSSL_FUNC_SIGNATURE_VERIFY_RECOVER
,
1171 (void (*)(void))rsa_verify_recover
},
1172 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT
,
1173 (void (*)(void))rsa_digest_sign_init
},
1174 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE
,
1175 (void (*)(void))rsa_digest_signverify_update
},
1176 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL
,
1177 (void (*)(void))rsa_digest_sign_final
},
1178 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT
,
1179 (void (*)(void))rsa_digest_verify_init
},
1180 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE
,
1181 (void (*)(void))rsa_digest_signverify_update
},
1182 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL
,
1183 (void (*)(void))rsa_digest_verify_final
},
1184 { OSSL_FUNC_SIGNATURE_FREECTX
, (void (*)(void))rsa_freectx
},
1185 { OSSL_FUNC_SIGNATURE_DUPCTX
, (void (*)(void))rsa_dupctx
},
1186 { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS
, (void (*)(void))rsa_get_ctx_params
},
1187 { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS
,
1188 (void (*)(void))rsa_gettable_ctx_params
},
1189 { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS
, (void (*)(void))rsa_set_ctx_params
},
1190 { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS
,
1191 (void (*)(void))rsa_settable_ctx_params
},
1192 { OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS
,
1193 (void (*)(void))rsa_get_ctx_md_params
},
1194 { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS
,
1195 (void (*)(void))rsa_gettable_ctx_md_params
},
1196 { OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS
,
1197 (void (*)(void))rsa_set_ctx_md_params
},
1198 { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS
,
1199 (void (*)(void))rsa_settable_ctx_md_params
},