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
;
164 static int rsa_check_padding(int mdnid
, int padding
)
166 if (padding
== RSA_NO_PADDING
) {
167 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_PADDING_MODE
);
171 if (padding
== RSA_X931_PADDING
) {
172 if (RSA_X931_hash_id(mdnid
) == -1) {
173 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_X931_DIGEST
);
181 static int rsa_check_parameters(EVP_MD
*md
, PROV_RSA_CTX
*prsactx
)
183 if (prsactx
->pad_mode
== RSA_PKCS1_PSS_PADDING
) {
186 /* See if minimum salt length exceeds maximum possible */
187 max_saltlen
= RSA_size(prsactx
->rsa
) - EVP_MD_size(md
);
188 if ((RSA_bits(prsactx
->rsa
) & 0x7) == 1)
190 if (prsactx
->min_saltlen
> max_saltlen
) {
191 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_SALT_LENGTH
);
198 static void *rsa_newctx(void *provctx
, const char *propq
)
200 PROV_RSA_CTX
*prsactx
= NULL
;
201 char *propq_copy
= NULL
;
203 if ((prsactx
= OPENSSL_zalloc(sizeof(PROV_RSA_CTX
))) == NULL
205 && (propq_copy
= OPENSSL_strdup(propq
)) == NULL
)) {
206 OPENSSL_free(prsactx
);
207 ERR_raise(ERR_LIB_PROV
, ERR_R_MALLOC_FAILURE
);
211 prsactx
->libctx
= PROV_LIBRARY_CONTEXT_OF(provctx
);
212 prsactx
->flag_allow_md
= 1;
213 prsactx
->propq
= propq_copy
;
217 /* True if PSS parameters are restricted */
218 #define rsa_pss_restricted(prsactx) (prsactx->min_saltlen != -1)
220 static int rsa_setup_md(PROV_RSA_CTX
*ctx
, const char *mdname
,
224 mdprops
= ctx
->propq
;
226 if (mdname
!= NULL
) {
227 EVP_MD
*md
= EVP_MD_fetch(ctx
->libctx
, mdname
, mdprops
);
228 int md_nid
= rsa_get_md_nid(md
);
232 || md_nid
== NID_undef
233 || !rsa_check_padding(md_nid
, ctx
->pad_mode
)
234 || !rsa_check_parameters(md
, ctx
)) {
236 ERR_raise_data(ERR_LIB_PROV
, PROV_R_INVALID_DIGEST
,
237 "%s could not be fetched", mdname
);
238 if (md_nid
== NID_undef
)
239 ERR_raise_data(ERR_LIB_PROV
, PROV_R_DIGEST_NOT_ALLOWED
,
240 "digest=%s", mdname
);
245 EVP_MD_CTX_free(ctx
->mdctx
);
246 EVP_MD_free(ctx
->md
);
249 * TODO(3.0) Should we care about DER writing errors?
250 * All it really means is that for some reason, there's no
251 * AlgorithmIdentifier to be had (consider RSA with MD5-SHA1),
252 * but the operation itself is still valid, just as long as it's
253 * not used to construct anything that needs an AlgorithmIdentifier.
256 if (WPACKET_init_der(&pkt
, ctx
->aid_buf
, sizeof(ctx
->aid_buf
))
257 && DER_w_algorithmIdentifier_RSA_with(&pkt
, -1, ctx
->rsa
, md_nid
)
258 && WPACKET_finish(&pkt
)) {
259 WPACKET_get_total_written(&pkt
, &ctx
->aid_len
);
260 ctx
->aid
= WPACKET_get_curr(&pkt
);
262 WPACKET_cleanup(&pkt
);
267 OPENSSL_strlcpy(ctx
->mdname
, mdname
, sizeof(ctx
->mdname
));
273 static int rsa_setup_mgf1_md(PROV_RSA_CTX
*ctx
, const char *mdname
,
277 mdprops
= ctx
->propq
;
279 if (ctx
->mgf1_mdname
[0] != '\0')
280 EVP_MD_free(ctx
->mgf1_md
);
282 if ((ctx
->mgf1_md
= EVP_MD_fetch(ctx
->libctx
, mdname
, mdprops
)) == NULL
) {
283 ERR_raise_data(ERR_LIB_PROV
, PROV_R_INVALID_DIGEST
,
284 "%s could not be fetched", mdname
);
287 OPENSSL_strlcpy(ctx
->mgf1_mdname
, mdname
, sizeof(ctx
->mgf1_mdname
));
292 static int rsa_signature_init(void *vprsactx
, void *vrsa
, int operation
)
294 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
296 if (prsactx
== NULL
|| vrsa
== NULL
|| !RSA_up_ref(vrsa
))
299 RSA_free(prsactx
->rsa
);
301 prsactx
->operation
= operation
;
303 /* Maximum for sign, auto for verify */
304 prsactx
->saltlen
= RSA_PSS_SALTLEN_AUTO
;
305 prsactx
->min_saltlen
= -1;
307 switch (RSA_test_flags(prsactx
->rsa
, RSA_FLAG_TYPE_MASK
)) {
308 case RSA_FLAG_TYPE_RSA
:
309 prsactx
->pad_mode
= RSA_PKCS1_PADDING
;
311 case RSA_FLAG_TYPE_RSASSAPSS
:
312 prsactx
->pad_mode
= RSA_PKCS1_PSS_PADDING
;
315 const RSA_PSS_PARAMS_30
*pss
=
316 rsa_get0_pss_params_30(prsactx
->rsa
);
318 if (!rsa_pss_params_30_is_unrestricted(pss
)) {
319 int md_nid
= rsa_pss_params_30_hashalg(pss
);
320 int mgf1md_nid
= rsa_pss_params_30_maskgenhashalg(pss
);
321 int min_saltlen
= rsa_pss_params_30_saltlen(pss
);
322 const char *mdname
, *mgf1mdname
;
324 mdname
= rsa_oaeppss_nid2name(md_nid
);
325 mgf1mdname
= rsa_oaeppss_nid2name(mgf1md_nid
);
326 prsactx
->min_saltlen
= min_saltlen
;
328 if (mdname
== NULL
) {
329 ERR_raise_data(ERR_LIB_PROV
, PROV_R_INVALID_DIGEST
,
330 "PSS restrictions lack hash algorithm");
333 if (mgf1mdname
== NULL
) {
334 ERR_raise_data(ERR_LIB_PROV
, PROV_R_INVALID_DIGEST
,
335 "PSS restrictions lack MGF1 hash algorithm");
339 strncpy(prsactx
->mdname
, mdname
, sizeof(prsactx
->mdname
));
340 strncpy(prsactx
->mgf1_mdname
, mgf1mdname
,
341 sizeof(prsactx
->mgf1_mdname
));
342 prsactx
->saltlen
= min_saltlen
;
344 return rsa_setup_md(prsactx
, mdname
, prsactx
->propq
)
345 && rsa_setup_mgf1_md(prsactx
, mgf1mdname
, prsactx
->propq
);
351 ERR_raise(ERR_LIB_RSA
, PROV_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
358 static int setup_tbuf(PROV_RSA_CTX
*ctx
)
360 if (ctx
->tbuf
!= NULL
)
362 if ((ctx
->tbuf
= OPENSSL_malloc(RSA_size(ctx
->rsa
))) == NULL
) {
363 ERR_raise(ERR_LIB_PROV
, ERR_R_MALLOC_FAILURE
);
369 static void clean_tbuf(PROV_RSA_CTX
*ctx
)
371 if (ctx
->tbuf
!= NULL
)
372 OPENSSL_cleanse(ctx
->tbuf
, RSA_size(ctx
->rsa
));
375 static void free_tbuf(PROV_RSA_CTX
*ctx
)
378 OPENSSL_free(ctx
->tbuf
);
382 static int rsa_sign_init(void *vprsactx
, void *vrsa
)
384 return rsa_signature_init(vprsactx
, vrsa
, EVP_PKEY_OP_SIGN
);
387 static int rsa_sign(void *vprsactx
, unsigned char *sig
, size_t *siglen
,
388 size_t sigsize
, const unsigned char *tbs
, size_t tbslen
)
390 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
392 size_t rsasize
= RSA_size(prsactx
->rsa
);
393 size_t mdsize
= rsa_get_md_size(prsactx
);
400 if (sigsize
< rsasize
) {
401 ERR_raise_data(ERR_LIB_PROV
, PROV_R_INVALID_SIGNATURE_SIZE
,
402 "is %zu, should be at least %zu", sigsize
, rsasize
);
407 if (tbslen
!= mdsize
) {
408 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_DIGEST_LENGTH
);
413 if (EVP_MD_is_a(prsactx
->md
, OSSL_DIGEST_NAME_MDC2
)) {
416 if (prsactx
->pad_mode
!= RSA_PKCS1_PADDING
) {
417 ERR_raise_data(ERR_LIB_PROV
, PROV_R_INVALID_PADDING_MODE
,
418 "only PKCS#1 padding supported with MDC2");
421 ret
= RSA_sign_ASN1_OCTET_STRING(0, tbs
, tbslen
, sig
, &sltmp
,
425 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
432 switch (prsactx
->pad_mode
) {
433 case RSA_X931_PADDING
:
434 if ((size_t)RSA_size(prsactx
->rsa
) < tbslen
+ 1) {
435 ERR_raise_data(ERR_LIB_PROV
, PROV_R_KEY_SIZE_TOO_SMALL
,
436 "RSA key size = %d, expected minimum = %d",
437 RSA_size(prsactx
->rsa
), tbslen
+ 1);
440 if (!setup_tbuf(prsactx
)) {
441 ERR_raise(ERR_LIB_PROV
, ERR_R_MALLOC_FAILURE
);
444 memcpy(prsactx
->tbuf
, tbs
, tbslen
);
445 prsactx
->tbuf
[tbslen
] = RSA_X931_hash_id(prsactx
->mdnid
);
446 ret
= RSA_private_encrypt(tbslen
+ 1, prsactx
->tbuf
,
447 sig
, prsactx
->rsa
, RSA_X931_PADDING
);
451 case RSA_PKCS1_PADDING
:
455 ret
= RSA_sign(prsactx
->mdnid
, tbs
, tbslen
, sig
, &sltmp
,
458 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
465 case RSA_PKCS1_PSS_PADDING
:
466 /* Check PSS restrictions */
467 if (rsa_pss_restricted(prsactx
)) {
468 switch (prsactx
->saltlen
) {
469 case RSA_PSS_SALTLEN_DIGEST
:
470 if (prsactx
->min_saltlen
> EVP_MD_size(prsactx
->md
)) {
471 ERR_raise_data(ERR_LIB_PROV
,
472 PROV_R_PSS_SALTLEN_TOO_SMALL
,
473 "minimum salt length set to %d, "
474 "but the digest only gives %d",
475 prsactx
->min_saltlen
,
476 EVP_MD_size(prsactx
->md
));
481 if (prsactx
->saltlen
>= 0
482 && prsactx
->saltlen
< prsactx
->min_saltlen
) {
483 ERR_raise_data(ERR_LIB_PROV
,
484 PROV_R_PSS_SALTLEN_TOO_SMALL
,
485 "minimum salt length set to %d, but the"
486 "actual salt length is only set to %d",
487 prsactx
->min_saltlen
,
494 if (!setup_tbuf(prsactx
))
496 if (!RSA_padding_add_PKCS1_PSS_mgf1(prsactx
->rsa
,
498 prsactx
->md
, prsactx
->mgf1_md
,
500 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
503 ret
= RSA_private_encrypt(RSA_size(prsactx
->rsa
), prsactx
->tbuf
,
504 sig
, prsactx
->rsa
, RSA_NO_PADDING
);
509 ERR_raise_data(ERR_LIB_PROV
, PROV_R_INVALID_PADDING_MODE
,
510 "Only X.931, PKCS#1 v1.5 or PSS padding allowed");
514 ret
= RSA_private_encrypt(tbslen
, tbs
, sig
, prsactx
->rsa
,
522 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
530 static int rsa_verify_recover_init(void *vprsactx
, void *vrsa
)
532 return rsa_signature_init(vprsactx
, vrsa
, EVP_PKEY_OP_VERIFYRECOVER
);
535 static int rsa_verify_recover(void *vprsactx
,
539 const unsigned char *sig
,
542 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
546 *routlen
= RSA_size(prsactx
->rsa
);
550 if (prsactx
->md
!= NULL
) {
551 switch (prsactx
->pad_mode
) {
552 case RSA_X931_PADDING
:
553 if (!setup_tbuf(prsactx
))
555 ret
= RSA_public_decrypt(siglen
, sig
, prsactx
->tbuf
, prsactx
->rsa
,
558 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
562 if (prsactx
->tbuf
[ret
] != RSA_X931_hash_id(prsactx
->mdnid
)) {
563 ERR_raise(ERR_LIB_PROV
, PROV_R_ALGORITHM_MISMATCH
);
566 if (ret
!= EVP_MD_size(prsactx
->md
)) {
567 ERR_raise_data(ERR_LIB_PROV
, PROV_R_INVALID_DIGEST_LENGTH
,
568 "Should be %d, but got %d",
569 EVP_MD_size(prsactx
->md
), ret
);
574 if (routsize
< (size_t)ret
) {
575 ERR_raise_data(ERR_LIB_PROV
, PROV_R_OUTPUT_BUFFER_TOO_SMALL
,
576 "buffer size is %d, should be %d",
580 memcpy(rout
, prsactx
->tbuf
, ret
);
583 case RSA_PKCS1_PADDING
:
587 ret
= int_rsa_verify(prsactx
->mdnid
, NULL
, 0, rout
, &sltmp
,
588 sig
, siglen
, prsactx
->rsa
);
590 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
598 ERR_raise_data(ERR_LIB_PROV
, PROV_R_INVALID_PADDING_MODE
,
599 "Only X.931 or PKCS#1 v1.5 padding allowed");
603 ret
= RSA_public_decrypt(siglen
, sig
, rout
, prsactx
->rsa
,
606 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
614 static int rsa_verify_init(void *vprsactx
, void *vrsa
)
616 return rsa_signature_init(vprsactx
, vrsa
, EVP_PKEY_OP_VERIFY
);
619 static int rsa_verify(void *vprsactx
, const unsigned char *sig
, size_t siglen
,
620 const unsigned char *tbs
, size_t tbslen
)
622 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
625 if (prsactx
->md
!= NULL
) {
626 switch (prsactx
->pad_mode
) {
627 case RSA_PKCS1_PADDING
:
628 if (!RSA_verify(prsactx
->mdnid
, tbs
, tbslen
, sig
, siglen
,
630 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
634 case RSA_X931_PADDING
:
635 if (rsa_verify_recover(prsactx
, NULL
, &rslen
, 0, sig
, siglen
) <= 0)
638 case RSA_PKCS1_PSS_PADDING
:
644 * We need to check this for the RSA_verify_PKCS1_PSS_mgf1()
647 mdsize
= rsa_get_md_size(prsactx
);
648 if (tbslen
!= mdsize
) {
649 ERR_raise_data(ERR_LIB_PROV
, PROV_R_INVALID_DIGEST_LENGTH
,
650 "Should be %d, but got %d",
655 if (!setup_tbuf(prsactx
))
657 ret
= RSA_public_decrypt(siglen
, sig
, prsactx
->tbuf
,
658 prsactx
->rsa
, RSA_NO_PADDING
);
660 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
663 ret
= RSA_verify_PKCS1_PSS_mgf1(prsactx
->rsa
, tbs
,
664 prsactx
->md
, prsactx
->mgf1_md
,
668 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
674 ERR_raise_data(ERR_LIB_PROV
, PROV_R_INVALID_PADDING_MODE
,
675 "Only X.931, PKCS#1 v1.5 or PSS padding allowed");
679 if (!setup_tbuf(prsactx
))
681 rslen
= RSA_public_decrypt(siglen
, sig
, prsactx
->tbuf
, prsactx
->rsa
,
684 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
689 if ((rslen
!= tbslen
) || memcmp(tbs
, prsactx
->tbuf
, rslen
))
695 static int rsa_digest_signverify_init(void *vprsactx
, const char *mdname
,
696 void *vrsa
, int operation
)
698 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
700 prsactx
->flag_allow_md
= 0;
701 if (!rsa_signature_init(vprsactx
, vrsa
, operation
)
702 || !rsa_setup_md(prsactx
, mdname
, NULL
)) /* TODO RL */
705 prsactx
->mdctx
= EVP_MD_CTX_new();
706 if (prsactx
->mdctx
== NULL
) {
707 ERR_raise(ERR_LIB_PROV
, ERR_R_MALLOC_FAILURE
);
711 if (!EVP_DigestInit_ex(prsactx
->mdctx
, prsactx
->md
, NULL
))
717 EVP_MD_CTX_free(prsactx
->mdctx
);
718 EVP_MD_free(prsactx
->md
);
719 prsactx
->mdctx
= NULL
;
724 static int rsa_digest_signverify_update(void *vprsactx
,
725 const unsigned char *data
,
728 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
730 if (prsactx
== NULL
|| prsactx
->mdctx
== NULL
)
733 return EVP_DigestUpdate(prsactx
->mdctx
, data
, datalen
);
736 static int rsa_digest_sign_init(void *vprsactx
, const char *mdname
,
739 return rsa_digest_signverify_init(vprsactx
, mdname
, vrsa
,
743 static int rsa_digest_sign_final(void *vprsactx
, unsigned char *sig
,
744 size_t *siglen
, size_t sigsize
)
746 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
747 unsigned char digest
[EVP_MAX_MD_SIZE
];
748 unsigned int dlen
= 0;
750 prsactx
->flag_allow_md
= 1;
751 if (prsactx
== NULL
|| prsactx
->mdctx
== NULL
)
755 * If sig is NULL then we're just finding out the sig size. Other fields
756 * are ignored. Defer to rsa_sign.
760 * TODO(3.0): There is the possibility that some externally provided
761 * digests exceed EVP_MAX_MD_SIZE. We should probably handle that somehow -
762 * but that problem is much larger than just in RSA.
764 if (!EVP_DigestFinal_ex(prsactx
->mdctx
, digest
, &dlen
))
768 return rsa_sign(vprsactx
, sig
, siglen
, sigsize
, digest
, (size_t)dlen
);
771 static int rsa_digest_verify_init(void *vprsactx
, const char *mdname
,
774 return rsa_digest_signverify_init(vprsactx
, mdname
, vrsa
,
778 int rsa_digest_verify_final(void *vprsactx
, const unsigned char *sig
,
781 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
782 unsigned char digest
[EVP_MAX_MD_SIZE
];
783 unsigned int dlen
= 0;
785 prsactx
->flag_allow_md
= 1;
786 if (prsactx
== NULL
|| prsactx
->mdctx
== NULL
)
790 * TODO(3.0): There is the possibility that some externally provided
791 * digests exceed EVP_MAX_MD_SIZE. We should probably handle that somehow -
792 * but that problem is much larger than just in RSA.
794 if (!EVP_DigestFinal_ex(prsactx
->mdctx
, digest
, &dlen
))
797 return rsa_verify(vprsactx
, sig
, siglen
, digest
, (size_t)dlen
);
800 static void rsa_freectx(void *vprsactx
)
802 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
807 RSA_free(prsactx
->rsa
);
808 EVP_MD_CTX_free(prsactx
->mdctx
);
809 EVP_MD_free(prsactx
->md
);
810 EVP_MD_free(prsactx
->mgf1_md
);
811 OPENSSL_free(prsactx
->propq
);
814 OPENSSL_clear_free(prsactx
, sizeof(prsactx
));
817 static void *rsa_dupctx(void *vprsactx
)
819 PROV_RSA_CTX
*srcctx
= (PROV_RSA_CTX
*)vprsactx
;
820 PROV_RSA_CTX
*dstctx
;
822 dstctx
= OPENSSL_zalloc(sizeof(*srcctx
));
823 if (dstctx
== NULL
) {
824 ERR_raise(ERR_LIB_PROV
, ERR_R_MALLOC_FAILURE
);
831 dstctx
->mdctx
= NULL
;
834 if (srcctx
->rsa
!= NULL
&& !RSA_up_ref(srcctx
->rsa
))
836 dstctx
->rsa
= srcctx
->rsa
;
838 if (srcctx
->md
!= NULL
&& !EVP_MD_up_ref(srcctx
->md
))
840 dstctx
->md
= srcctx
->md
;
842 if (srcctx
->mgf1_md
!= NULL
&& !EVP_MD_up_ref(srcctx
->mgf1_md
))
844 dstctx
->mgf1_md
= srcctx
->mgf1_md
;
846 if (srcctx
->mdctx
!= NULL
) {
847 dstctx
->mdctx
= EVP_MD_CTX_new();
848 if (dstctx
->mdctx
== NULL
849 || !EVP_MD_CTX_copy_ex(dstctx
->mdctx
, srcctx
->mdctx
))
859 static int rsa_get_ctx_params(void *vprsactx
, OSSL_PARAM
*params
)
861 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
864 if (prsactx
== NULL
|| params
== NULL
)
867 p
= OSSL_PARAM_locate(params
, OSSL_SIGNATURE_PARAM_ALGORITHM_ID
);
869 && !OSSL_PARAM_set_octet_string(p
, prsactx
->aid
, prsactx
->aid_len
))
872 p
= OSSL_PARAM_locate(params
, OSSL_SIGNATURE_PARAM_PAD_MODE
);
874 switch (p
->data_type
) {
875 case OSSL_PARAM_INTEGER
:
876 if (!OSSL_PARAM_set_int(p
, prsactx
->pad_mode
))
879 case OSSL_PARAM_UTF8_STRING
:
882 const char *word
= NULL
;
884 for (i
= 0; padding_item
[i
].id
!= 0; i
++) {
885 if (prsactx
->pad_mode
== (int)padding_item
[i
].id
) {
886 word
= padding_item
[i
].ptr
;
892 if (!OSSL_PARAM_set_utf8_string(p
, word
))
895 ERR_raise(ERR_LIB_PROV
, ERR_R_INTERNAL_ERROR
);
903 p
= OSSL_PARAM_locate(params
, OSSL_SIGNATURE_PARAM_DIGEST
);
904 if (p
!= NULL
&& !OSSL_PARAM_set_utf8_string(p
, prsactx
->mdname
))
907 p
= OSSL_PARAM_locate(params
, OSSL_SIGNATURE_PARAM_MGF1_DIGEST
);
908 if (p
!= NULL
&& !OSSL_PARAM_set_utf8_string(p
, prsactx
->mgf1_mdname
))
911 p
= OSSL_PARAM_locate(params
, OSSL_SIGNATURE_PARAM_PSS_SALTLEN
);
913 if (p
->data_type
== OSSL_PARAM_INTEGER
) {
914 if (!OSSL_PARAM_set_int(p
, prsactx
->saltlen
))
916 } else if (p
->data_type
== OSSL_PARAM_UTF8_STRING
) {
917 switch (prsactx
->saltlen
) {
918 case RSA_PSS_SALTLEN_DIGEST
:
919 if (!OSSL_PARAM_set_utf8_string(p
, "digest"))
922 case RSA_PSS_SALTLEN_MAX
:
923 if (!OSSL_PARAM_set_utf8_string(p
, "max"))
926 case RSA_PSS_SALTLEN_AUTO
:
927 if (!OSSL_PARAM_set_utf8_string(p
, "auto"))
931 if (BIO_snprintf(p
->data
, p
->data_size
, "%d", prsactx
->saltlen
)
942 static const OSSL_PARAM known_gettable_ctx_params
[] = {
943 OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_ALGORITHM_ID
, NULL
, 0),
944 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PAD_MODE
, NULL
, 0),
945 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST
, NULL
, 0),
946 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_MGF1_DIGEST
, NULL
, 0),
947 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PSS_SALTLEN
, NULL
, 0),
951 static const OSSL_PARAM
*rsa_gettable_ctx_params(void)
953 return known_gettable_ctx_params
;
956 static int rsa_set_ctx_params(void *vprsactx
, const OSSL_PARAM params
[])
958 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
961 if (prsactx
== NULL
|| params
== NULL
)
964 p
= OSSL_PARAM_locate_const(params
, OSSL_SIGNATURE_PARAM_DIGEST
);
965 /* Not allowed during certain operations */
966 if (p
!= NULL
&& !prsactx
->flag_allow_md
)
969 char mdname
[OSSL_MAX_NAME_SIZE
] = "", *pmdname
= mdname
;
970 char mdprops
[OSSL_MAX_PROPQUERY_SIZE
] = "", *pmdprops
= mdprops
;
971 const OSSL_PARAM
*propsp
=
972 OSSL_PARAM_locate_const(params
,
973 OSSL_SIGNATURE_PARAM_PROPERTIES
);
975 if (!OSSL_PARAM_get_utf8_string(p
, &pmdname
, sizeof(mdname
)))
980 else if (!OSSL_PARAM_get_utf8_string(propsp
,
981 &pmdprops
, sizeof(mdprops
)))
984 if (rsa_pss_restricted(prsactx
)) {
985 /* TODO(3.0) figure out what to do for prsactx->md == NULL */
986 if (prsactx
->md
== NULL
|| EVP_MD_is_a(prsactx
->md
, mdname
))
988 ERR_raise(ERR_LIB_PROV
, PROV_R_DIGEST_NOT_ALLOWED
);
992 /* non-PSS code follows */
993 if (!rsa_setup_md(prsactx
, mdname
, pmdprops
))
997 p
= OSSL_PARAM_locate_const(params
, OSSL_SIGNATURE_PARAM_PAD_MODE
);
1000 const char *err_extra_text
= NULL
;
1002 switch (p
->data_type
) {
1003 case OSSL_PARAM_INTEGER
: /* Support for legacy pad mode number */
1004 if (!OSSL_PARAM_get_int(p
, &pad_mode
))
1007 case OSSL_PARAM_UTF8_STRING
:
1011 if (p
->data
== NULL
)
1014 for (i
= 0; padding_item
[i
].id
!= 0; i
++) {
1015 if (strcmp(p
->data
, padding_item
[i
].ptr
) == 0) {
1016 pad_mode
= padding_item
[i
].id
;
1027 case RSA_PKCS1_OAEP_PADDING
:
1029 * OAEP padding is for asymmetric cipher only so is not compatible
1030 * with signature use.
1032 err_extra_text
= "OAEP padding not allowed for signing / verifying";
1034 case RSA_PKCS1_PSS_PADDING
:
1035 if ((prsactx
->operation
1036 & (EVP_PKEY_OP_SIGN
| EVP_PKEY_OP_VERIFY
)) == 0) {
1038 "PSS padding only allowed for sign and verify operations";
1041 if (prsactx
->md
== NULL
1042 && !rsa_setup_md(prsactx
, OSSL_DIGEST_NAME_SHA1
, NULL
)) {
1046 case RSA_PKCS1_PADDING
:
1047 err_extra_text
= "PKCS#1 padding not allowed with RSA-PSS";
1049 case RSA_SSLV23_PADDING
:
1050 err_extra_text
= "SSLv3 padding not allowed with RSA-PSS";
1052 case RSA_NO_PADDING
:
1053 err_extra_text
= "No padding not allowed with RSA-PSS";
1055 case RSA_X931_PADDING
:
1056 err_extra_text
= "X.931 padding not allowed with RSA-PSS";
1058 if (RSA_test_flags(prsactx
->rsa
,
1059 RSA_FLAG_TYPE_MASK
) == RSA_FLAG_TYPE_RSA
)
1064 if (err_extra_text
== NULL
)
1065 ERR_raise(ERR_LIB_PROV
,
1066 PROV_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE
);
1068 ERR_raise_data(ERR_LIB_PROV
,
1069 PROV_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE
,
1073 if (!rsa_check_padding(prsactx
->mdnid
, pad_mode
))
1075 prsactx
->pad_mode
= pad_mode
;
1078 p
= OSSL_PARAM_locate_const(params
, OSSL_SIGNATURE_PARAM_PSS_SALTLEN
);
1082 if (prsactx
->pad_mode
!= RSA_PKCS1_PSS_PADDING
) {
1083 ERR_raise_data(ERR_LIB_PROV
, PROV_R_NOT_SUPPORTED
,
1084 "PSS saltlen can only be specified if "
1085 "PSS padding has been specified first");
1089 switch (p
->data_type
) {
1090 case OSSL_PARAM_INTEGER
: /* Support for legacy pad mode number */
1091 if (!OSSL_PARAM_get_int(p
, &saltlen
))
1094 case OSSL_PARAM_UTF8_STRING
:
1095 if (strcmp(p
->data
, "digest") == 0)
1096 saltlen
= RSA_PSS_SALTLEN_DIGEST
;
1097 else if (strcmp(p
->data
, "max") == 0)
1098 saltlen
= RSA_PSS_SALTLEN_MAX
;
1099 else if (strcmp(p
->data
, "auto") == 0)
1100 saltlen
= RSA_PSS_SALTLEN_AUTO
;
1102 saltlen
= atoi(p
->data
);
1109 * RSA_PSS_SALTLEN_MAX seems curiously named in this check.
1110 * Contrary to what it's name suggests, it's the currently
1111 * lowest saltlen number possible.
1113 if (saltlen
< RSA_PSS_SALTLEN_MAX
) {
1114 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_PSS_SALTLEN
);
1118 if (rsa_pss_restricted(prsactx
)) {
1119 switch (prsactx
->saltlen
) {
1120 case RSA_PSS_SALTLEN_AUTO
:
1121 if (prsactx
->operation
== EVP_PKEY_OP_VERIFY
) {
1122 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_PSS_SALTLEN
);
1126 case RSA_PSS_SALTLEN_DIGEST
:
1127 if (prsactx
->min_saltlen
> EVP_MD_size(prsactx
->md
)) {
1128 ERR_raise_data(ERR_LIB_PROV
,
1129 PROV_R_PSS_SALTLEN_TOO_SMALL
,
1130 "Should be more than %d, but would be "
1131 "set to match digest size (%d)",
1132 prsactx
->min_saltlen
,
1133 EVP_MD_size(prsactx
->md
));
1138 if (saltlen
>= 0 && saltlen
< prsactx
->min_saltlen
) {
1139 ERR_raise_data(ERR_LIB_PROV
,
1140 PROV_R_PSS_SALTLEN_TOO_SMALL
,
1141 "Should be more than %d, "
1142 "but would be set to %d",
1143 prsactx
->min_saltlen
, saltlen
);
1149 prsactx
->saltlen
= saltlen
;
1152 p
= OSSL_PARAM_locate_const(params
, OSSL_SIGNATURE_PARAM_MGF1_DIGEST
);
1154 char mdname
[OSSL_MAX_NAME_SIZE
] = "", *pmdname
= mdname
;
1155 char mdprops
[OSSL_MAX_PROPQUERY_SIZE
] = "", *pmdprops
= mdprops
;
1156 const OSSL_PARAM
*propsp
=
1157 OSSL_PARAM_locate_const(params
,
1158 OSSL_SIGNATURE_PARAM_MGF1_PROPERTIES
);
1160 if (!OSSL_PARAM_get_utf8_string(p
, &pmdname
, sizeof(mdname
)))
1165 else if (!OSSL_PARAM_get_utf8_string(propsp
,
1166 &pmdprops
, sizeof(mdprops
)))
1169 if (prsactx
->pad_mode
!= RSA_PKCS1_PSS_PADDING
) {
1170 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_MGF1_MD
);
1174 if (rsa_pss_restricted(prsactx
)) {
1175 /* TODO(3.0) figure out what to do for prsactx->mgf1_md == NULL */
1176 if (prsactx
->mgf1_md
== NULL
1177 || EVP_MD_is_a(prsactx
->mgf1_md
, mdname
))
1179 ERR_raise(ERR_LIB_PROV
, PROV_R_DIGEST_NOT_ALLOWED
);
1183 /* non-PSS code follows */
1184 if (!rsa_setup_mgf1_md(prsactx
, mdname
, pmdprops
))
1191 static const OSSL_PARAM known_settable_ctx_params
[] = {
1192 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PAD_MODE
, NULL
, 0),
1193 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST
, NULL
, 0),
1194 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PROPERTIES
, NULL
, 0),
1195 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_MGF1_DIGEST
, NULL
, 0),
1196 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_MGF1_PROPERTIES
, NULL
, 0),
1197 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PSS_SALTLEN
, NULL
, 0),
1201 static const OSSL_PARAM
*rsa_settable_ctx_params(void)
1204 * TODO(3.0): Should this function return a different set of settable ctx
1205 * params if the ctx is being used for a DigestSign/DigestVerify? In that
1206 * case it is not allowed to set the digest size/digest name because the
1207 * digest is explicitly set as part of the init.
1209 return known_settable_ctx_params
;
1212 static int rsa_get_ctx_md_params(void *vprsactx
, OSSL_PARAM
*params
)
1214 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
1216 if (prsactx
->mdctx
== NULL
)
1219 return EVP_MD_CTX_get_params(prsactx
->mdctx
, params
);
1222 static const OSSL_PARAM
*rsa_gettable_ctx_md_params(void *vprsactx
)
1224 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
1226 if (prsactx
->md
== NULL
)
1229 return EVP_MD_gettable_ctx_params(prsactx
->md
);
1232 static int rsa_set_ctx_md_params(void *vprsactx
, const OSSL_PARAM params
[])
1234 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
1236 if (prsactx
->mdctx
== NULL
)
1239 return EVP_MD_CTX_set_params(prsactx
->mdctx
, params
);
1242 static const OSSL_PARAM
*rsa_settable_ctx_md_params(void *vprsactx
)
1244 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
1246 if (prsactx
->md
== NULL
)
1249 return EVP_MD_settable_ctx_params(prsactx
->md
);
1252 const OSSL_DISPATCH rsa_signature_functions
[] = {
1253 { OSSL_FUNC_SIGNATURE_NEWCTX
, (void (*)(void))rsa_newctx
},
1254 { OSSL_FUNC_SIGNATURE_SIGN_INIT
, (void (*)(void))rsa_sign_init
},
1255 { OSSL_FUNC_SIGNATURE_SIGN
, (void (*)(void))rsa_sign
},
1256 { OSSL_FUNC_SIGNATURE_VERIFY_INIT
, (void (*)(void))rsa_verify_init
},
1257 { OSSL_FUNC_SIGNATURE_VERIFY
, (void (*)(void))rsa_verify
},
1258 { OSSL_FUNC_SIGNATURE_VERIFY_RECOVER_INIT
,
1259 (void (*)(void))rsa_verify_recover_init
},
1260 { OSSL_FUNC_SIGNATURE_VERIFY_RECOVER
,
1261 (void (*)(void))rsa_verify_recover
},
1262 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT
,
1263 (void (*)(void))rsa_digest_sign_init
},
1264 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE
,
1265 (void (*)(void))rsa_digest_signverify_update
},
1266 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL
,
1267 (void (*)(void))rsa_digest_sign_final
},
1268 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT
,
1269 (void (*)(void))rsa_digest_verify_init
},
1270 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE
,
1271 (void (*)(void))rsa_digest_signverify_update
},
1272 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL
,
1273 (void (*)(void))rsa_digest_verify_final
},
1274 { OSSL_FUNC_SIGNATURE_FREECTX
, (void (*)(void))rsa_freectx
},
1275 { OSSL_FUNC_SIGNATURE_DUPCTX
, (void (*)(void))rsa_dupctx
},
1276 { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS
, (void (*)(void))rsa_get_ctx_params
},
1277 { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS
,
1278 (void (*)(void))rsa_gettable_ctx_params
},
1279 { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS
, (void (*)(void))rsa_set_ctx_params
},
1280 { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS
,
1281 (void (*)(void))rsa_settable_ctx_params
},
1282 { OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS
,
1283 (void (*)(void))rsa_get_ctx_md_params
},
1284 { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS
,
1285 (void (*)(void))rsa_gettable_ctx_md_params
},
1286 { OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS
,
1287 (void (*)(void))rsa_set_ctx_md_params
},
1288 { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS
,
1289 (void (*)(void))rsa_settable_ctx_md_params
},