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_signature_init
;
35 static OSSL_OP_signature_verify_init_fn rsa_signature_init
;
36 static OSSL_OP_signature_verify_recover_init_fn rsa_signature_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_signverify_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_signverify_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.
79 * Flag to determine if the hash function can be changed (1) or not (0)
80 * Because it's dangerous to change during a DigestSign or DigestVerify
81 * operation, this flag is cleared by their Init function, and set again
82 * by their Final function.
84 unsigned int flag_allow_md
: 1;
86 /* The Algorithm Identifier of the combined signature agorithm */
87 unsigned char aid_buf
[128];
95 char mdname
[OSSL_MAX_NAME_SIZE
]; /* Purely informational */
97 /* RSA padding mode */
99 /* message digest for MGF1 */
101 char mgf1_mdname
[OSSL_MAX_NAME_SIZE
]; /* Purely informational */
102 /* PSS salt length */
104 /* Minimum salt length or -1 if no PSS parameter restriction */
112 static size_t rsa_get_md_size(const PROV_RSA_CTX
*prsactx
)
114 if (prsactx
->md
!= NULL
)
115 return EVP_MD_size(prsactx
->md
);
119 static int rsa_get_md_nid(const EVP_MD
*md
)
122 * Because the RSA library deals with NIDs, we need to translate.
123 * We do so using EVP_MD_is_a(), and therefore need a name to NID
126 static const OSSL_ITEM name_to_nid
[] = {
127 { NID_sha1
, OSSL_DIGEST_NAME_SHA1
},
128 { NID_sha224
, OSSL_DIGEST_NAME_SHA2_224
},
129 { NID_sha256
, OSSL_DIGEST_NAME_SHA2_256
},
130 { NID_sha384
, OSSL_DIGEST_NAME_SHA2_384
},
131 { NID_sha512
, OSSL_DIGEST_NAME_SHA2_512
},
132 { NID_sha512_224
, OSSL_DIGEST_NAME_SHA2_512_224
},
133 { NID_sha512_256
, OSSL_DIGEST_NAME_SHA2_512_256
},
134 { NID_md5
, OSSL_DIGEST_NAME_MD5
},
135 { NID_md5_sha1
, OSSL_DIGEST_NAME_MD5_SHA1
},
136 { NID_md2
, OSSL_DIGEST_NAME_MD2
},
137 { NID_md4
, OSSL_DIGEST_NAME_MD4
},
138 { NID_mdc2
, OSSL_DIGEST_NAME_MDC2
},
139 { NID_ripemd160
, OSSL_DIGEST_NAME_RIPEMD160
},
140 { NID_sha3_224
, OSSL_DIGEST_NAME_SHA3_224
},
141 { NID_sha3_256
, OSSL_DIGEST_NAME_SHA3_256
},
142 { NID_sha3_384
, OSSL_DIGEST_NAME_SHA3_384
},
143 { NID_sha3_512
, OSSL_DIGEST_NAME_SHA3_512
},
146 int mdnid
= NID_undef
;
151 for (i
= 0; i
< OSSL_NELEM(name_to_nid
); i
++) {
152 if (EVP_MD_is_a(md
, name_to_nid
[i
].ptr
)) {
153 mdnid
= (int)name_to_nid
[i
].id
;
158 if (mdnid
== NID_undef
)
159 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_DIGEST
);
165 static int rsa_check_padding(int mdnid
, int padding
)
167 if (padding
== RSA_NO_PADDING
) {
168 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_PADDING_MODE
);
172 if (padding
== RSA_X931_PADDING
) {
173 if (RSA_X931_hash_id(mdnid
) == -1) {
174 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_X931_DIGEST
);
182 static void *rsa_newctx(void *provctx
)
184 PROV_RSA_CTX
*prsactx
= OPENSSL_zalloc(sizeof(PROV_RSA_CTX
));
189 prsactx
->libctx
= PROV_LIBRARY_CONTEXT_OF(provctx
);
190 prsactx
->flag_allow_md
= 1;
194 /* True if PSS parameters are restricted */
195 #define rsa_pss_restricted(prsactx) (prsactx->min_saltlen != -1)
197 static int rsa_signature_init(void *vprsactx
, void *vrsa
)
199 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
201 if (prsactx
== NULL
|| vrsa
== NULL
|| !RSA_up_ref(vrsa
))
204 RSA_free(prsactx
->rsa
);
206 if (RSA_get0_pss_params(prsactx
->rsa
) != NULL
)
207 prsactx
->pad_mode
= RSA_PKCS1_PSS_PADDING
;
209 prsactx
->pad_mode
= RSA_PKCS1_PADDING
;
210 /* Maximum for sign, auto for verify */
211 prsactx
->saltlen
= RSA_PSS_SALTLEN_AUTO
;
212 prsactx
->min_saltlen
= -1;
217 static int rsa_setup_md(PROV_RSA_CTX
*ctx
, const char *mdname
,
220 if (mdname
!= NULL
) {
221 EVP_MD
*md
= EVP_MD_fetch(ctx
->libctx
, mdname
, mdprops
);
222 int md_nid
= rsa_get_md_nid(md
);
226 || md_nid
== NID_undef
227 || !rsa_check_padding(md_nid
, ctx
->pad_mode
)) {
232 EVP_MD_CTX_free(ctx
->mdctx
);
233 EVP_MD_free(ctx
->md
);
236 * TODO(3.0) Should we care about DER writing errors?
237 * All it really means is that for some reason, there's no
238 * AlgorithmIdentifier to be had (consider RSA with MD5-SHA1),
239 * but the operation itself is still valid, just as long as it's
240 * not used to construct anything that needs an AlgorithmIdentifier.
243 if (WPACKET_init_der(&pkt
, ctx
->aid_buf
, sizeof(ctx
->aid_buf
))
244 && DER_w_algorithmIdentifier_RSA_with(&pkt
, -1, ctx
->rsa
, md_nid
)
245 && WPACKET_finish(&pkt
)) {
246 WPACKET_get_total_written(&pkt
, &ctx
->aid_len
);
247 ctx
->aid
= WPACKET_get_curr(&pkt
);
249 WPACKET_cleanup(&pkt
);
254 OPENSSL_strlcpy(ctx
->mdname
, mdname
, sizeof(ctx
->mdname
));
260 static int rsa_setup_mgf1_md(PROV_RSA_CTX
*ctx
, const char *mdname
,
263 if (ctx
->mgf1_mdname
[0] != '\0')
264 EVP_MD_free(ctx
->mgf1_md
);
266 if ((ctx
->mgf1_md
= EVP_MD_fetch(ctx
->libctx
, mdname
, props
)) == NULL
)
268 OPENSSL_strlcpy(ctx
->mgf1_mdname
, mdname
, sizeof(ctx
->mgf1_mdname
));
273 static int setup_tbuf(PROV_RSA_CTX
*ctx
)
275 if (ctx
->tbuf
!= NULL
)
277 if ((ctx
->tbuf
= OPENSSL_malloc(RSA_size(ctx
->rsa
))) == NULL
) {
278 ERR_raise(ERR_LIB_PROV
, ERR_R_MALLOC_FAILURE
);
284 static void clean_tbuf(PROV_RSA_CTX
*ctx
)
286 if (ctx
->tbuf
!= NULL
)
287 OPENSSL_cleanse(ctx
->tbuf
, RSA_size(ctx
->rsa
));
290 static void free_tbuf(PROV_RSA_CTX
*ctx
)
292 OPENSSL_clear_free(ctx
->tbuf
, RSA_size(ctx
->rsa
));
296 static int rsa_sign(void *vprsactx
, unsigned char *sig
, size_t *siglen
,
297 size_t sigsize
, const unsigned char *tbs
, size_t tbslen
)
299 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
301 size_t rsasize
= RSA_size(prsactx
->rsa
);
302 size_t mdsize
= rsa_get_md_size(prsactx
);
309 if (sigsize
< (size_t)rsasize
)
313 if (tbslen
!= mdsize
) {
314 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_DIGEST_LENGTH
);
319 if (EVP_MD_is_a(prsactx
->md
, OSSL_DIGEST_NAME_MDC2
)) {
322 if (prsactx
->pad_mode
!= RSA_PKCS1_PADDING
) {
323 ERR_raise_data(ERR_LIB_PROV
, PROV_R_INVALID_PADDING_MODE
,
324 "only PKCS#1 padding supported with MDC2");
327 ret
= RSA_sign_ASN1_OCTET_STRING(0, tbs
, tbslen
, sig
, &sltmp
,
331 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
338 switch (prsactx
->pad_mode
) {
339 case RSA_X931_PADDING
:
340 if ((size_t)RSA_size(prsactx
->rsa
) < tbslen
+ 1) {
341 ERR_raise(ERR_LIB_PROV
, PROV_R_KEY_SIZE_TOO_SMALL
);
344 if (!setup_tbuf(prsactx
)) {
345 ERR_raise(ERR_LIB_PROV
, ERR_R_MALLOC_FAILURE
);
348 memcpy(prsactx
->tbuf
, tbs
, tbslen
);
349 prsactx
->tbuf
[tbslen
] = RSA_X931_hash_id(prsactx
->mdnid
);
350 ret
= RSA_private_encrypt(tbslen
+ 1, prsactx
->tbuf
,
351 sig
, prsactx
->rsa
, RSA_X931_PADDING
);
355 case RSA_PKCS1_PADDING
:
359 ret
= RSA_sign(prsactx
->mdnid
, tbs
, tbslen
, sig
, &sltmp
,
362 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
369 case RSA_PKCS1_PSS_PADDING
:
370 /* Check PSS restrictions */
371 if (rsa_pss_restricted(prsactx
)) {
372 switch (prsactx
->saltlen
) {
373 case RSA_PSS_SALTLEN_DIGEST
:
374 if (prsactx
->min_saltlen
> EVP_MD_size(prsactx
->md
)) {
375 ERR_raise(ERR_LIB_PROV
, PROV_R_PSS_SALTLEN_TOO_SMALL
);
380 if (prsactx
->saltlen
>= 0
381 && prsactx
->saltlen
< prsactx
->min_saltlen
) {
382 ERR_raise(ERR_LIB_PROV
, PROV_R_PSS_SALTLEN_TOO_SMALL
);
388 if (!setup_tbuf(prsactx
))
390 if (!RSA_padding_add_PKCS1_PSS_mgf1(prsactx
->rsa
,
392 prsactx
->md
, prsactx
->mgf1_md
,
394 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
397 ret
= RSA_private_encrypt(RSA_size(prsactx
->rsa
), prsactx
->tbuf
,
398 sig
, prsactx
->rsa
, RSA_NO_PADDING
);
403 ERR_raise_data(ERR_LIB_PROV
, PROV_R_INVALID_PADDING_MODE
,
404 "Only X.931, PKCS#1 v1.5 or PSS padding allowed");
408 ret
= RSA_private_encrypt(tbslen
, tbs
, sig
, prsactx
->rsa
,
416 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
424 static int rsa_verify_recover(void *vprsactx
,
428 const unsigned char *sig
,
431 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
435 *routlen
= RSA_size(prsactx
->rsa
);
439 if (prsactx
->md
!= NULL
) {
440 switch (prsactx
->pad_mode
) {
441 case RSA_X931_PADDING
:
442 if (!setup_tbuf(prsactx
))
444 ret
= RSA_public_decrypt(siglen
, sig
, prsactx
->tbuf
, prsactx
->rsa
,
447 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
451 if (prsactx
->tbuf
[ret
] != RSA_X931_hash_id(prsactx
->mdnid
)) {
452 ERR_raise(ERR_LIB_PROV
, PROV_R_ALGORITHM_MISMATCH
);
455 if (ret
!= EVP_MD_size(prsactx
->md
)) {
456 ERR_raise_data(ERR_LIB_PROV
, PROV_R_INVALID_DIGEST_LENGTH
,
457 "Should be %d, but got %d",
458 EVP_MD_size(prsactx
->md
), ret
);
463 if (routsize
< (size_t)ret
) {
464 ERR_raise(ERR_LIB_PROV
, PROV_R_OUTPUT_BUFFER_TOO_SMALL
);
467 memcpy(rout
, prsactx
->tbuf
, ret
);
470 case RSA_PKCS1_PADDING
:
474 ret
= int_rsa_verify(prsactx
->mdnid
, NULL
, 0, rout
, &sltmp
,
475 sig
, siglen
, prsactx
->rsa
);
477 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
485 ERR_raise_data(ERR_LIB_PROV
, PROV_R_INVALID_PADDING_MODE
,
486 "Only X.931 or PKCS#1 v1.5 padding allowed");
490 ret
= RSA_public_decrypt(siglen
, sig
, rout
, prsactx
->rsa
,
493 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
501 static int rsa_verify(void *vprsactx
, const unsigned char *sig
, size_t siglen
,
502 const unsigned char *tbs
, size_t tbslen
)
504 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
507 if (prsactx
->md
!= NULL
) {
508 switch (prsactx
->pad_mode
) {
509 case RSA_PKCS1_PADDING
:
510 if (!RSA_verify(prsactx
->mdnid
, tbs
, tbslen
, sig
, siglen
,
512 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
516 case RSA_X931_PADDING
:
517 if (rsa_verify_recover(prsactx
, NULL
, &rslen
, 0, sig
, siglen
) <= 0)
520 case RSA_PKCS1_PSS_PADDING
:
525 /* Check PSS restrictions */
526 if (rsa_pss_restricted(prsactx
)) {
527 switch (prsactx
->saltlen
) {
528 case RSA_PSS_SALTLEN_AUTO
:
529 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_PSS_SALTLEN
);
531 case RSA_PSS_SALTLEN_DIGEST
:
532 if (prsactx
->min_saltlen
> EVP_MD_size(prsactx
->md
)) {
533 ERR_raise(ERR_LIB_PROV
,
534 PROV_R_PSS_SALTLEN_TOO_SMALL
);
539 if (prsactx
->saltlen
>= 0
540 && prsactx
->saltlen
< prsactx
->min_saltlen
) {
541 ERR_raise(ERR_LIB_PROV
, PROV_R_PSS_SALTLEN_TOO_SMALL
);
549 * We need to check this for the RSA_verify_PKCS1_PSS_mgf1()
552 mdsize
= rsa_get_md_size(prsactx
);
553 if (tbslen
!= mdsize
) {
554 ERR_raise_data(ERR_LIB_PROV
, PROV_R_INVALID_DIGEST_LENGTH
,
555 "Should be %d, but got %d",
560 if (!setup_tbuf(prsactx
))
562 ret
= RSA_public_decrypt(siglen
, sig
, prsactx
->tbuf
,
563 prsactx
->rsa
, RSA_NO_PADDING
);
565 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
568 ret
= RSA_verify_PKCS1_PSS_mgf1(prsactx
->rsa
, tbs
,
569 prsactx
->md
, prsactx
->mgf1_md
,
573 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
579 ERR_raise_data(ERR_LIB_PROV
, PROV_R_INVALID_PADDING_MODE
,
580 "Only X.931, PKCS#1 v1.5 or PSS padding allowed");
584 if (!setup_tbuf(prsactx
))
586 rslen
= RSA_public_decrypt(siglen
, sig
, prsactx
->tbuf
, prsactx
->rsa
,
589 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
594 if ((rslen
!= tbslen
) || memcmp(tbs
, prsactx
->tbuf
, rslen
))
600 static int rsa_digest_signverify_init(void *vprsactx
, const char *mdname
,
601 const char *props
, void *vrsa
)
603 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
605 prsactx
->flag_allow_md
= 0;
606 if (!rsa_signature_init(vprsactx
, vrsa
)
607 || !rsa_setup_md(prsactx
, mdname
, props
))
610 prsactx
->mdctx
= EVP_MD_CTX_new();
611 if (prsactx
->mdctx
== NULL
)
614 if (!EVP_DigestInit_ex(prsactx
->mdctx
, prsactx
->md
, NULL
))
620 EVP_MD_CTX_free(prsactx
->mdctx
);
621 EVP_MD_free(prsactx
->md
);
622 prsactx
->mdctx
= NULL
;
627 int rsa_digest_signverify_update(void *vprsactx
, const unsigned char *data
,
630 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
632 if (prsactx
== NULL
|| prsactx
->mdctx
== NULL
)
635 return EVP_DigestUpdate(prsactx
->mdctx
, data
, datalen
);
638 int rsa_digest_sign_final(void *vprsactx
, unsigned char *sig
, size_t *siglen
,
641 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
642 unsigned char digest
[EVP_MAX_MD_SIZE
];
643 unsigned int dlen
= 0;
645 prsactx
->flag_allow_md
= 1;
646 if (prsactx
== NULL
|| prsactx
->mdctx
== NULL
)
650 * If sig is NULL then we're just finding out the sig size. Other fields
651 * are ignored. Defer to rsa_sign.
655 * TODO(3.0): There is the possibility that some externally provided
656 * digests exceed EVP_MAX_MD_SIZE. We should probably handle that somehow -
657 * but that problem is much larger than just in RSA.
659 if (!EVP_DigestFinal_ex(prsactx
->mdctx
, digest
, &dlen
))
663 return rsa_sign(vprsactx
, sig
, siglen
, sigsize
, digest
, (size_t)dlen
);
667 int rsa_digest_verify_final(void *vprsactx
, const unsigned char *sig
,
670 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
671 unsigned char digest
[EVP_MAX_MD_SIZE
];
672 unsigned int dlen
= 0;
674 prsactx
->flag_allow_md
= 1;
675 if (prsactx
== NULL
|| prsactx
->mdctx
== NULL
)
679 * TODO(3.0): There is the possibility that some externally provided
680 * digests exceed EVP_MAX_MD_SIZE. We should probably handle that somehow -
681 * but that problem is much larger than just in RSA.
683 if (!EVP_DigestFinal_ex(prsactx
->mdctx
, digest
, &dlen
))
686 return rsa_verify(vprsactx
, sig
, siglen
, digest
, (size_t)dlen
);
689 static void rsa_freectx(void *vprsactx
)
691 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
696 RSA_free(prsactx
->rsa
);
697 EVP_MD_CTX_free(prsactx
->mdctx
);
698 EVP_MD_free(prsactx
->md
);
699 EVP_MD_free(prsactx
->mgf1_md
);
702 OPENSSL_clear_free(prsactx
, sizeof(prsactx
));
705 static void *rsa_dupctx(void *vprsactx
)
707 PROV_RSA_CTX
*srcctx
= (PROV_RSA_CTX
*)vprsactx
;
708 PROV_RSA_CTX
*dstctx
;
710 dstctx
= OPENSSL_zalloc(sizeof(*srcctx
));
717 dstctx
->mdctx
= NULL
;
720 if (srcctx
->rsa
!= NULL
&& !RSA_up_ref(srcctx
->rsa
))
722 dstctx
->rsa
= srcctx
->rsa
;
724 if (srcctx
->md
!= NULL
&& !EVP_MD_up_ref(srcctx
->md
))
726 dstctx
->md
= srcctx
->md
;
728 if (srcctx
->mgf1_md
!= NULL
&& !EVP_MD_up_ref(srcctx
->mgf1_md
))
730 dstctx
->mgf1_md
= srcctx
->mgf1_md
;
732 if (srcctx
->mdctx
!= NULL
) {
733 dstctx
->mdctx
= EVP_MD_CTX_new();
734 if (dstctx
->mdctx
== NULL
735 || !EVP_MD_CTX_copy_ex(dstctx
->mdctx
, srcctx
->mdctx
))
745 static int rsa_get_ctx_params(void *vprsactx
, OSSL_PARAM
*params
)
747 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
750 if (prsactx
== NULL
|| params
== NULL
)
753 p
= OSSL_PARAM_locate(params
, OSSL_SIGNATURE_PARAM_ALGORITHM_ID
);
755 && !OSSL_PARAM_set_octet_string(p
, prsactx
->aid
, prsactx
->aid_len
))
758 p
= OSSL_PARAM_locate(params
, OSSL_SIGNATURE_PARAM_PAD_MODE
);
760 switch (p
->data_type
) {
761 case OSSL_PARAM_INTEGER
:
762 if (!OSSL_PARAM_set_int(p
, prsactx
->pad_mode
))
765 case OSSL_PARAM_UTF8_STRING
:
768 const char *word
= NULL
;
770 for (i
= 0; padding_item
[i
].id
!= 0; i
++) {
771 if (prsactx
->pad_mode
== (int)padding_item
[i
].id
) {
772 word
= padding_item
[i
].ptr
;
778 if (!OSSL_PARAM_set_utf8_string(p
, word
))
781 ERR_raise(ERR_LIB_PROV
, ERR_R_INTERNAL_ERROR
);
789 p
= OSSL_PARAM_locate(params
, OSSL_SIGNATURE_PARAM_DIGEST
);
790 if (p
!= NULL
&& !OSSL_PARAM_set_utf8_string(p
, prsactx
->mdname
))
793 p
= OSSL_PARAM_locate(params
, OSSL_SIGNATURE_PARAM_MGF1_DIGEST
);
794 if (p
!= NULL
&& !OSSL_PARAM_set_utf8_string(p
, prsactx
->mgf1_mdname
))
797 p
= OSSL_PARAM_locate(params
, OSSL_SIGNATURE_PARAM_PSS_SALTLEN
);
799 if (p
->data_type
== OSSL_PARAM_INTEGER
) {
800 if (!OSSL_PARAM_set_int(p
, prsactx
->saltlen
))
802 } else if (p
->data_type
== OSSL_PARAM_UTF8_STRING
) {
803 switch (prsactx
->saltlen
) {
804 case RSA_PSS_SALTLEN_DIGEST
:
805 if (!OSSL_PARAM_set_utf8_string(p
, "digest"))
808 case RSA_PSS_SALTLEN_MAX
:
809 if (!OSSL_PARAM_set_utf8_string(p
, "max"))
812 case RSA_PSS_SALTLEN_AUTO
:
813 if (!OSSL_PARAM_set_utf8_string(p
, "auto"))
817 if (BIO_snprintf(p
->data
, p
->data_size
, "%d", prsactx
->saltlen
)
828 static const OSSL_PARAM known_gettable_ctx_params
[] = {
829 OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_ALGORITHM_ID
, NULL
, 0),
830 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PAD_MODE
, NULL
, 0),
831 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST
, NULL
, 0),
832 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_MGF1_DIGEST
, NULL
, 0),
833 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PSS_SALTLEN
, NULL
, 0),
837 static const OSSL_PARAM
*rsa_gettable_ctx_params(void)
839 return known_gettable_ctx_params
;
842 static int rsa_set_ctx_params(void *vprsactx
, const OSSL_PARAM params
[])
844 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
847 if (prsactx
== NULL
|| params
== NULL
)
850 p
= OSSL_PARAM_locate_const(params
, OSSL_SIGNATURE_PARAM_DIGEST
);
851 /* Not allowed during certain operations */
852 if (p
!= NULL
&& !prsactx
->flag_allow_md
)
855 char mdname
[OSSL_MAX_NAME_SIZE
] = "", *pmdname
= mdname
;
856 char mdprops
[OSSL_MAX_PROPQUERY_SIZE
] = "", *pmdprops
= mdprops
;
857 const OSSL_PARAM
*propsp
=
858 OSSL_PARAM_locate_const(params
,
859 OSSL_SIGNATURE_PARAM_PROPERTIES
);
861 if (!OSSL_PARAM_get_utf8_string(p
, &pmdname
, sizeof(mdname
)))
864 && !OSSL_PARAM_get_utf8_string(propsp
, &pmdprops
, sizeof(mdprops
)))
867 /* TODO(3.0) PSS check needs more work */
868 if (rsa_pss_restricted(prsactx
)) {
869 /* TODO(3.0) figure out what to do for prsactx->md == NULL */
870 if (prsactx
->md
== NULL
|| EVP_MD_is_a(prsactx
->md
, mdname
))
872 ERR_raise(ERR_LIB_PROV
, PROV_R_DIGEST_NOT_ALLOWED
);
876 /* non-PSS code follows */
877 if (!rsa_setup_md(prsactx
, mdname
, mdprops
))
881 p
= OSSL_PARAM_locate_const(params
, OSSL_SIGNATURE_PARAM_PAD_MODE
);
885 switch (p
->data_type
) {
886 case OSSL_PARAM_INTEGER
: /* Support for legacy pad mode number */
887 if (!OSSL_PARAM_get_int(p
, &pad_mode
))
890 case OSSL_PARAM_UTF8_STRING
:
897 for (i
= 0; padding_item
[i
].id
!= 0; i
++) {
898 if (strcmp(p
->data
, padding_item
[i
].ptr
) == 0) {
899 pad_mode
= padding_item
[i
].id
;
910 case RSA_PKCS1_OAEP_PADDING
:
912 * OAEP padding is for asymmetric cipher only so is not compatible
913 * with signature use.
915 ERR_raise_data(ERR_LIB_PROV
,
916 PROV_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE
,
917 "OAEP padding not allowed for signing / verifying");
919 case RSA_PKCS1_PSS_PADDING
:
920 if (prsactx
->mdname
[0] == '\0')
921 rsa_setup_md(prsactx
, "SHA1", "");
923 case RSA_PKCS1_PADDING
:
924 case RSA_SSLV23_PADDING
:
926 case RSA_X931_PADDING
:
927 if (RSA_get0_pss_params(prsactx
->rsa
) != NULL
) {
928 ERR_raise_data(ERR_LIB_PROV
,
929 PROV_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE
,
930 "X.931 padding not allowed with RSA-PSS");
934 if (!rsa_check_padding(prsactx
->mdnid
, pad_mode
))
940 prsactx
->pad_mode
= pad_mode
;
943 p
= OSSL_PARAM_locate_const(params
, OSSL_SIGNATURE_PARAM_PSS_SALTLEN
);
947 if (prsactx
->pad_mode
!= RSA_PKCS1_PSS_PADDING
) {
948 ERR_raise_data(ERR_LIB_PROV
, PROV_R_NOT_SUPPORTED
,
949 "PSS saltlen can only be specified if "
950 "PSS padding has been specified first");
954 switch (p
->data_type
) {
955 case OSSL_PARAM_INTEGER
: /* Support for legacy pad mode number */
956 if (!OSSL_PARAM_get_int(p
, &saltlen
))
959 case OSSL_PARAM_UTF8_STRING
:
960 if (strcmp(p
->data
, "digest") == 0)
961 saltlen
= RSA_PSS_SALTLEN_DIGEST
;
962 else if (strcmp(p
->data
, "max") == 0)
963 saltlen
= RSA_PSS_SALTLEN_MAX
;
964 else if (strcmp(p
->data
, "auto") == 0)
965 saltlen
= RSA_PSS_SALTLEN_AUTO
;
967 saltlen
= atoi(p
->data
);
974 * RSA_PSS_SALTLEN_MAX seems curiously named in this check.
975 * Contrary to what it's name suggests, it's the currently
976 * lowest saltlen number possible.
978 if (saltlen
< RSA_PSS_SALTLEN_MAX
) {
979 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_PSS_SALTLEN
);
983 prsactx
->saltlen
= saltlen
;
986 p
= OSSL_PARAM_locate_const(params
, OSSL_SIGNATURE_PARAM_MGF1_DIGEST
);
988 char mdname
[OSSL_MAX_NAME_SIZE
] = "", *pmdname
= mdname
;
989 char mdprops
[OSSL_MAX_PROPQUERY_SIZE
] = "", *pmdprops
= mdprops
;
990 const OSSL_PARAM
*propsp
=
991 OSSL_PARAM_locate_const(params
,
992 OSSL_SIGNATURE_PARAM_MGF1_PROPERTIES
);
994 if (!OSSL_PARAM_get_utf8_string(p
, &pmdname
, sizeof(mdname
)))
997 && !OSSL_PARAM_get_utf8_string(propsp
, &pmdprops
, sizeof(mdprops
)))
1000 if (prsactx
->pad_mode
!= RSA_PKCS1_PSS_PADDING
) {
1001 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_MGF1_MD
);
1005 /* TODO(3.0) PSS check needs more work */
1006 if (rsa_pss_restricted(prsactx
)) {
1007 /* TODO(3.0) figure out what to do for prsactx->md == NULL */
1008 if (prsactx
->mgf1_md
== NULL
1009 || EVP_MD_is_a(prsactx
->mgf1_md
, mdname
))
1011 ERR_raise(ERR_LIB_PROV
, PROV_R_DIGEST_NOT_ALLOWED
);
1015 /* non-PSS code follows */
1016 if (!rsa_setup_mgf1_md(prsactx
, mdname
, mdprops
))
1023 static const OSSL_PARAM known_settable_ctx_params
[] = {
1024 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PAD_MODE
, NULL
, 0),
1025 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST
, NULL
, 0),
1026 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PROPERTIES
, NULL
, 0),
1027 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_MGF1_DIGEST
, NULL
, 0),
1028 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_MGF1_PROPERTIES
, NULL
, 0),
1029 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PSS_SALTLEN
, NULL
, 0),
1033 static const OSSL_PARAM
*rsa_settable_ctx_params(void)
1036 * TODO(3.0): Should this function return a different set of settable ctx
1037 * params if the ctx is being used for a DigestSign/DigestVerify? In that
1038 * case it is not allowed to set the digest size/digest name because the
1039 * digest is explicitly set as part of the init.
1041 return known_settable_ctx_params
;
1044 static int rsa_get_ctx_md_params(void *vprsactx
, OSSL_PARAM
*params
)
1046 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
1048 if (prsactx
->mdctx
== NULL
)
1051 return EVP_MD_CTX_get_params(prsactx
->mdctx
, params
);
1054 static const OSSL_PARAM
*rsa_gettable_ctx_md_params(void *vprsactx
)
1056 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
1058 if (prsactx
->md
== NULL
)
1061 return EVP_MD_gettable_ctx_params(prsactx
->md
);
1064 static int rsa_set_ctx_md_params(void *vprsactx
, const OSSL_PARAM params
[])
1066 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
1068 if (prsactx
->mdctx
== NULL
)
1071 return EVP_MD_CTX_set_params(prsactx
->mdctx
, params
);
1074 static const OSSL_PARAM
*rsa_settable_ctx_md_params(void *vprsactx
)
1076 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
1078 if (prsactx
->md
== NULL
)
1081 return EVP_MD_settable_ctx_params(prsactx
->md
);
1084 const OSSL_DISPATCH rsa_signature_functions
[] = {
1085 { OSSL_FUNC_SIGNATURE_NEWCTX
, (void (*)(void))rsa_newctx
},
1086 { OSSL_FUNC_SIGNATURE_SIGN_INIT
, (void (*)(void))rsa_signature_init
},
1087 { OSSL_FUNC_SIGNATURE_SIGN
, (void (*)(void))rsa_sign
},
1088 { OSSL_FUNC_SIGNATURE_VERIFY_INIT
, (void (*)(void))rsa_signature_init
},
1089 { OSSL_FUNC_SIGNATURE_VERIFY
, (void (*)(void))rsa_verify
},
1090 { OSSL_FUNC_SIGNATURE_VERIFY_RECOVER_INIT
, (void (*)(void))rsa_signature_init
},
1091 { OSSL_FUNC_SIGNATURE_VERIFY_RECOVER
, (void (*)(void))rsa_verify_recover
},
1092 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT
,
1093 (void (*)(void))rsa_digest_signverify_init
},
1094 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE
,
1095 (void (*)(void))rsa_digest_signverify_update
},
1096 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL
,
1097 (void (*)(void))rsa_digest_sign_final
},
1098 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT
,
1099 (void (*)(void))rsa_digest_signverify_init
},
1100 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE
,
1101 (void (*)(void))rsa_digest_signverify_update
},
1102 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL
,
1103 (void (*)(void))rsa_digest_verify_final
},
1104 { OSSL_FUNC_SIGNATURE_FREECTX
, (void (*)(void))rsa_freectx
},
1105 { OSSL_FUNC_SIGNATURE_DUPCTX
, (void (*)(void))rsa_dupctx
},
1106 { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS
, (void (*)(void))rsa_get_ctx_params
},
1107 { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS
,
1108 (void (*)(void))rsa_gettable_ctx_params
},
1109 { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS
, (void (*)(void))rsa_set_ctx_params
},
1110 { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS
,
1111 (void (*)(void))rsa_settable_ctx_params
},
1112 { OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS
,
1113 (void (*)(void))rsa_get_ctx_md_params
},
1114 { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS
,
1115 (void (*)(void))rsa_gettable_ctx_md_params
},
1116 { OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS
,
1117 (void (*)(void))rsa_set_ctx_md_params
},
1118 { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS
,
1119 (void (*)(void))rsa_settable_ctx_md_params
},