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_MDWithRSAEncryption(&pkt
, -1, ctx
->rsa
,
259 && WPACKET_finish(&pkt
)) {
260 WPACKET_get_total_written(&pkt
, &ctx
->aid_len
);
261 ctx
->aid
= WPACKET_get_curr(&pkt
);
263 WPACKET_cleanup(&pkt
);
268 OPENSSL_strlcpy(ctx
->mdname
, mdname
, sizeof(ctx
->mdname
));
274 static int rsa_setup_mgf1_md(PROV_RSA_CTX
*ctx
, const char *mdname
,
278 mdprops
= ctx
->propq
;
280 if (ctx
->mgf1_mdname
[0] != '\0')
281 EVP_MD_free(ctx
->mgf1_md
);
283 if ((ctx
->mgf1_md
= EVP_MD_fetch(ctx
->libctx
, mdname
, mdprops
)) == NULL
) {
284 ERR_raise_data(ERR_LIB_PROV
, PROV_R_INVALID_DIGEST
,
285 "%s could not be fetched", mdname
);
288 OPENSSL_strlcpy(ctx
->mgf1_mdname
, mdname
, sizeof(ctx
->mgf1_mdname
));
293 static int rsa_signature_init(void *vprsactx
, void *vrsa
, int operation
)
295 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
297 if (prsactx
== NULL
|| vrsa
== NULL
|| !RSA_up_ref(vrsa
))
300 RSA_free(prsactx
->rsa
);
302 prsactx
->operation
= operation
;
304 /* Maximum for sign, auto for verify */
305 prsactx
->saltlen
= RSA_PSS_SALTLEN_AUTO
;
306 prsactx
->min_saltlen
= -1;
308 switch (RSA_test_flags(prsactx
->rsa
, RSA_FLAG_TYPE_MASK
)) {
309 case RSA_FLAG_TYPE_RSA
:
310 prsactx
->pad_mode
= RSA_PKCS1_PADDING
;
312 case RSA_FLAG_TYPE_RSASSAPSS
:
313 prsactx
->pad_mode
= RSA_PKCS1_PSS_PADDING
;
316 const RSA_PSS_PARAMS_30
*pss
=
317 rsa_get0_pss_params_30(prsactx
->rsa
);
319 if (!rsa_pss_params_30_is_unrestricted(pss
)) {
320 int md_nid
= rsa_pss_params_30_hashalg(pss
);
321 int mgf1md_nid
= rsa_pss_params_30_maskgenhashalg(pss
);
322 int min_saltlen
= rsa_pss_params_30_saltlen(pss
);
323 const char *mdname
, *mgf1mdname
;
325 mdname
= rsa_oaeppss_nid2name(md_nid
);
326 mgf1mdname
= rsa_oaeppss_nid2name(mgf1md_nid
);
327 prsactx
->min_saltlen
= min_saltlen
;
329 if (mdname
== NULL
) {
330 ERR_raise_data(ERR_LIB_PROV
, PROV_R_INVALID_DIGEST
,
331 "PSS restrictions lack hash algorithm");
334 if (mgf1mdname
== NULL
) {
335 ERR_raise_data(ERR_LIB_PROV
, PROV_R_INVALID_DIGEST
,
336 "PSS restrictions lack MGF1 hash algorithm");
340 strncpy(prsactx
->mdname
, mdname
, sizeof(prsactx
->mdname
));
341 strncpy(prsactx
->mgf1_mdname
, mgf1mdname
,
342 sizeof(prsactx
->mgf1_mdname
));
343 prsactx
->saltlen
= min_saltlen
;
345 return rsa_setup_md(prsactx
, mdname
, prsactx
->propq
)
346 && rsa_setup_mgf1_md(prsactx
, mgf1mdname
, prsactx
->propq
);
352 ERR_raise(ERR_LIB_RSA
, PROV_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
359 static int setup_tbuf(PROV_RSA_CTX
*ctx
)
361 if (ctx
->tbuf
!= NULL
)
363 if ((ctx
->tbuf
= OPENSSL_malloc(RSA_size(ctx
->rsa
))) == NULL
) {
364 ERR_raise(ERR_LIB_PROV
, ERR_R_MALLOC_FAILURE
);
370 static void clean_tbuf(PROV_RSA_CTX
*ctx
)
372 if (ctx
->tbuf
!= NULL
)
373 OPENSSL_cleanse(ctx
->tbuf
, RSA_size(ctx
->rsa
));
376 static void free_tbuf(PROV_RSA_CTX
*ctx
)
379 OPENSSL_free(ctx
->tbuf
);
383 static int rsa_sign_init(void *vprsactx
, void *vrsa
)
385 return rsa_signature_init(vprsactx
, vrsa
, EVP_PKEY_OP_SIGN
);
388 static int rsa_sign(void *vprsactx
, unsigned char *sig
, size_t *siglen
,
389 size_t sigsize
, const unsigned char *tbs
, size_t tbslen
)
391 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
393 size_t rsasize
= RSA_size(prsactx
->rsa
);
394 size_t mdsize
= rsa_get_md_size(prsactx
);
401 if (sigsize
< rsasize
) {
402 ERR_raise_data(ERR_LIB_PROV
, PROV_R_INVALID_SIGNATURE_SIZE
,
403 "is %zu, should be at least %zu", sigsize
, rsasize
);
408 if (tbslen
!= mdsize
) {
409 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_DIGEST_LENGTH
);
414 if (EVP_MD_is_a(prsactx
->md
, OSSL_DIGEST_NAME_MDC2
)) {
417 if (prsactx
->pad_mode
!= RSA_PKCS1_PADDING
) {
418 ERR_raise_data(ERR_LIB_PROV
, PROV_R_INVALID_PADDING_MODE
,
419 "only PKCS#1 padding supported with MDC2");
422 ret
= RSA_sign_ASN1_OCTET_STRING(0, tbs
, tbslen
, sig
, &sltmp
,
426 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
433 switch (prsactx
->pad_mode
) {
434 case RSA_X931_PADDING
:
435 if ((size_t)RSA_size(prsactx
->rsa
) < tbslen
+ 1) {
436 ERR_raise_data(ERR_LIB_PROV
, PROV_R_KEY_SIZE_TOO_SMALL
,
437 "RSA key size = %d, expected minimum = %d",
438 RSA_size(prsactx
->rsa
), tbslen
+ 1);
441 if (!setup_tbuf(prsactx
)) {
442 ERR_raise(ERR_LIB_PROV
, ERR_R_MALLOC_FAILURE
);
445 memcpy(prsactx
->tbuf
, tbs
, tbslen
);
446 prsactx
->tbuf
[tbslen
] = RSA_X931_hash_id(prsactx
->mdnid
);
447 ret
= RSA_private_encrypt(tbslen
+ 1, prsactx
->tbuf
,
448 sig
, prsactx
->rsa
, RSA_X931_PADDING
);
452 case RSA_PKCS1_PADDING
:
456 ret
= RSA_sign(prsactx
->mdnid
, tbs
, tbslen
, sig
, &sltmp
,
459 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
466 case RSA_PKCS1_PSS_PADDING
:
467 /* Check PSS restrictions */
468 if (rsa_pss_restricted(prsactx
)) {
469 switch (prsactx
->saltlen
) {
470 case RSA_PSS_SALTLEN_DIGEST
:
471 if (prsactx
->min_saltlen
> EVP_MD_size(prsactx
->md
)) {
472 ERR_raise_data(ERR_LIB_PROV
,
473 PROV_R_PSS_SALTLEN_TOO_SMALL
,
474 "minimum salt length set to %d, "
475 "but the digest only gives %d",
476 prsactx
->min_saltlen
,
477 EVP_MD_size(prsactx
->md
));
482 if (prsactx
->saltlen
>= 0
483 && prsactx
->saltlen
< prsactx
->min_saltlen
) {
484 ERR_raise_data(ERR_LIB_PROV
,
485 PROV_R_PSS_SALTLEN_TOO_SMALL
,
486 "minimum salt length set to %d, but the"
487 "actual salt length is only set to %d",
488 prsactx
->min_saltlen
,
495 if (!setup_tbuf(prsactx
))
497 if (!RSA_padding_add_PKCS1_PSS_mgf1(prsactx
->rsa
,
499 prsactx
->md
, prsactx
->mgf1_md
,
501 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
504 ret
= RSA_private_encrypt(RSA_size(prsactx
->rsa
), prsactx
->tbuf
,
505 sig
, prsactx
->rsa
, RSA_NO_PADDING
);
510 ERR_raise_data(ERR_LIB_PROV
, PROV_R_INVALID_PADDING_MODE
,
511 "Only X.931, PKCS#1 v1.5 or PSS padding allowed");
515 ret
= RSA_private_encrypt(tbslen
, tbs
, sig
, prsactx
->rsa
,
523 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
531 static int rsa_verify_recover_init(void *vprsactx
, void *vrsa
)
533 return rsa_signature_init(vprsactx
, vrsa
, EVP_PKEY_OP_VERIFYRECOVER
);
536 static int rsa_verify_recover(void *vprsactx
,
540 const unsigned char *sig
,
543 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
547 *routlen
= RSA_size(prsactx
->rsa
);
551 if (prsactx
->md
!= NULL
) {
552 switch (prsactx
->pad_mode
) {
553 case RSA_X931_PADDING
:
554 if (!setup_tbuf(prsactx
))
556 ret
= RSA_public_decrypt(siglen
, sig
, prsactx
->tbuf
, prsactx
->rsa
,
559 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
563 if (prsactx
->tbuf
[ret
] != RSA_X931_hash_id(prsactx
->mdnid
)) {
564 ERR_raise(ERR_LIB_PROV
, PROV_R_ALGORITHM_MISMATCH
);
567 if (ret
!= EVP_MD_size(prsactx
->md
)) {
568 ERR_raise_data(ERR_LIB_PROV
, PROV_R_INVALID_DIGEST_LENGTH
,
569 "Should be %d, but got %d",
570 EVP_MD_size(prsactx
->md
), ret
);
575 if (routsize
< (size_t)ret
) {
576 ERR_raise_data(ERR_LIB_PROV
, PROV_R_OUTPUT_BUFFER_TOO_SMALL
,
577 "buffer size is %d, should be %d",
581 memcpy(rout
, prsactx
->tbuf
, ret
);
584 case RSA_PKCS1_PADDING
:
588 ret
= int_rsa_verify(prsactx
->mdnid
, NULL
, 0, rout
, &sltmp
,
589 sig
, siglen
, prsactx
->rsa
);
591 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
599 ERR_raise_data(ERR_LIB_PROV
, PROV_R_INVALID_PADDING_MODE
,
600 "Only X.931 or PKCS#1 v1.5 padding allowed");
604 ret
= RSA_public_decrypt(siglen
, sig
, rout
, prsactx
->rsa
,
607 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
615 static int rsa_verify_init(void *vprsactx
, void *vrsa
)
617 return rsa_signature_init(vprsactx
, vrsa
, EVP_PKEY_OP_VERIFY
);
620 static int rsa_verify(void *vprsactx
, const unsigned char *sig
, size_t siglen
,
621 const unsigned char *tbs
, size_t tbslen
)
623 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
626 if (prsactx
->md
!= NULL
) {
627 switch (prsactx
->pad_mode
) {
628 case RSA_PKCS1_PADDING
:
629 if (!RSA_verify(prsactx
->mdnid
, tbs
, tbslen
, sig
, siglen
,
631 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
635 case RSA_X931_PADDING
:
636 if (rsa_verify_recover(prsactx
, NULL
, &rslen
, 0, sig
, siglen
) <= 0)
639 case RSA_PKCS1_PSS_PADDING
:
645 * We need to check this for the RSA_verify_PKCS1_PSS_mgf1()
648 mdsize
= rsa_get_md_size(prsactx
);
649 if (tbslen
!= mdsize
) {
650 ERR_raise_data(ERR_LIB_PROV
, PROV_R_INVALID_DIGEST_LENGTH
,
651 "Should be %d, but got %d",
656 if (!setup_tbuf(prsactx
))
658 ret
= RSA_public_decrypt(siglen
, sig
, prsactx
->tbuf
,
659 prsactx
->rsa
, RSA_NO_PADDING
);
661 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
664 ret
= RSA_verify_PKCS1_PSS_mgf1(prsactx
->rsa
, tbs
,
665 prsactx
->md
, prsactx
->mgf1_md
,
669 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
675 ERR_raise_data(ERR_LIB_PROV
, PROV_R_INVALID_PADDING_MODE
,
676 "Only X.931, PKCS#1 v1.5 or PSS padding allowed");
680 if (!setup_tbuf(prsactx
))
682 rslen
= RSA_public_decrypt(siglen
, sig
, prsactx
->tbuf
, prsactx
->rsa
,
685 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
690 if ((rslen
!= tbslen
) || memcmp(tbs
, prsactx
->tbuf
, rslen
))
696 static int rsa_digest_signverify_init(void *vprsactx
, const char *mdname
,
697 void *vrsa
, int operation
)
699 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
701 prsactx
->flag_allow_md
= 0;
702 if (!rsa_signature_init(vprsactx
, vrsa
, operation
)
703 || !rsa_setup_md(prsactx
, mdname
, NULL
)) /* TODO RL */
706 prsactx
->mdctx
= EVP_MD_CTX_new();
707 if (prsactx
->mdctx
== NULL
) {
708 ERR_raise(ERR_LIB_PROV
, ERR_R_MALLOC_FAILURE
);
712 if (!EVP_DigestInit_ex(prsactx
->mdctx
, prsactx
->md
, NULL
))
718 EVP_MD_CTX_free(prsactx
->mdctx
);
719 EVP_MD_free(prsactx
->md
);
720 prsactx
->mdctx
= NULL
;
725 static int rsa_digest_signverify_update(void *vprsactx
,
726 const unsigned char *data
,
729 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
731 if (prsactx
== NULL
|| prsactx
->mdctx
== NULL
)
734 return EVP_DigestUpdate(prsactx
->mdctx
, data
, datalen
);
737 static int rsa_digest_sign_init(void *vprsactx
, const char *mdname
,
740 return rsa_digest_signverify_init(vprsactx
, mdname
, vrsa
,
744 static int rsa_digest_sign_final(void *vprsactx
, unsigned char *sig
,
745 size_t *siglen
, size_t sigsize
)
747 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
748 unsigned char digest
[EVP_MAX_MD_SIZE
];
749 unsigned int dlen
= 0;
751 prsactx
->flag_allow_md
= 1;
752 if (prsactx
== NULL
|| prsactx
->mdctx
== NULL
)
756 * If sig is NULL then we're just finding out the sig size. Other fields
757 * are ignored. Defer to rsa_sign.
761 * TODO(3.0): There is the possibility that some externally provided
762 * digests exceed EVP_MAX_MD_SIZE. We should probably handle that somehow -
763 * but that problem is much larger than just in RSA.
765 if (!EVP_DigestFinal_ex(prsactx
->mdctx
, digest
, &dlen
))
769 return rsa_sign(vprsactx
, sig
, siglen
, sigsize
, digest
, (size_t)dlen
);
772 static int rsa_digest_verify_init(void *vprsactx
, const char *mdname
,
775 return rsa_digest_signverify_init(vprsactx
, mdname
, vrsa
,
779 int rsa_digest_verify_final(void *vprsactx
, const unsigned char *sig
,
782 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
783 unsigned char digest
[EVP_MAX_MD_SIZE
];
784 unsigned int dlen
= 0;
786 prsactx
->flag_allow_md
= 1;
787 if (prsactx
== NULL
|| prsactx
->mdctx
== NULL
)
791 * TODO(3.0): There is the possibility that some externally provided
792 * digests exceed EVP_MAX_MD_SIZE. We should probably handle that somehow -
793 * but that problem is much larger than just in RSA.
795 if (!EVP_DigestFinal_ex(prsactx
->mdctx
, digest
, &dlen
))
798 return rsa_verify(vprsactx
, sig
, siglen
, digest
, (size_t)dlen
);
801 static void rsa_freectx(void *vprsactx
)
803 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
808 RSA_free(prsactx
->rsa
);
809 EVP_MD_CTX_free(prsactx
->mdctx
);
810 EVP_MD_free(prsactx
->md
);
811 EVP_MD_free(prsactx
->mgf1_md
);
812 OPENSSL_free(prsactx
->propq
);
815 OPENSSL_clear_free(prsactx
, sizeof(prsactx
));
818 static void *rsa_dupctx(void *vprsactx
)
820 PROV_RSA_CTX
*srcctx
= (PROV_RSA_CTX
*)vprsactx
;
821 PROV_RSA_CTX
*dstctx
;
823 dstctx
= OPENSSL_zalloc(sizeof(*srcctx
));
824 if (dstctx
== NULL
) {
825 ERR_raise(ERR_LIB_PROV
, ERR_R_MALLOC_FAILURE
);
832 dstctx
->mdctx
= NULL
;
835 if (srcctx
->rsa
!= NULL
&& !RSA_up_ref(srcctx
->rsa
))
837 dstctx
->rsa
= srcctx
->rsa
;
839 if (srcctx
->md
!= NULL
&& !EVP_MD_up_ref(srcctx
->md
))
841 dstctx
->md
= srcctx
->md
;
843 if (srcctx
->mgf1_md
!= NULL
&& !EVP_MD_up_ref(srcctx
->mgf1_md
))
845 dstctx
->mgf1_md
= srcctx
->mgf1_md
;
847 if (srcctx
->mdctx
!= NULL
) {
848 dstctx
->mdctx
= EVP_MD_CTX_new();
849 if (dstctx
->mdctx
== NULL
850 || !EVP_MD_CTX_copy_ex(dstctx
->mdctx
, srcctx
->mdctx
))
860 static int rsa_get_ctx_params(void *vprsactx
, OSSL_PARAM
*params
)
862 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
865 if (prsactx
== NULL
|| params
== NULL
)
868 p
= OSSL_PARAM_locate(params
, OSSL_SIGNATURE_PARAM_ALGORITHM_ID
);
870 && !OSSL_PARAM_set_octet_string(p
, prsactx
->aid
, prsactx
->aid_len
))
873 p
= OSSL_PARAM_locate(params
, OSSL_SIGNATURE_PARAM_PAD_MODE
);
875 switch (p
->data_type
) {
876 case OSSL_PARAM_INTEGER
:
877 if (!OSSL_PARAM_set_int(p
, prsactx
->pad_mode
))
880 case OSSL_PARAM_UTF8_STRING
:
883 const char *word
= NULL
;
885 for (i
= 0; padding_item
[i
].id
!= 0; i
++) {
886 if (prsactx
->pad_mode
== (int)padding_item
[i
].id
) {
887 word
= padding_item
[i
].ptr
;
893 if (!OSSL_PARAM_set_utf8_string(p
, word
))
896 ERR_raise(ERR_LIB_PROV
, ERR_R_INTERNAL_ERROR
);
904 p
= OSSL_PARAM_locate(params
, OSSL_SIGNATURE_PARAM_DIGEST
);
905 if (p
!= NULL
&& !OSSL_PARAM_set_utf8_string(p
, prsactx
->mdname
))
908 p
= OSSL_PARAM_locate(params
, OSSL_SIGNATURE_PARAM_MGF1_DIGEST
);
909 if (p
!= NULL
&& !OSSL_PARAM_set_utf8_string(p
, prsactx
->mgf1_mdname
))
912 p
= OSSL_PARAM_locate(params
, OSSL_SIGNATURE_PARAM_PSS_SALTLEN
);
914 if (p
->data_type
== OSSL_PARAM_INTEGER
) {
915 if (!OSSL_PARAM_set_int(p
, prsactx
->saltlen
))
917 } else if (p
->data_type
== OSSL_PARAM_UTF8_STRING
) {
918 switch (prsactx
->saltlen
) {
919 case RSA_PSS_SALTLEN_DIGEST
:
920 if (!OSSL_PARAM_set_utf8_string(p
, "digest"))
923 case RSA_PSS_SALTLEN_MAX
:
924 if (!OSSL_PARAM_set_utf8_string(p
, "max"))
927 case RSA_PSS_SALTLEN_AUTO
:
928 if (!OSSL_PARAM_set_utf8_string(p
, "auto"))
932 if (BIO_snprintf(p
->data
, p
->data_size
, "%d", prsactx
->saltlen
)
943 static const OSSL_PARAM known_gettable_ctx_params
[] = {
944 OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_ALGORITHM_ID
, NULL
, 0),
945 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PAD_MODE
, NULL
, 0),
946 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST
, NULL
, 0),
947 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_MGF1_DIGEST
, NULL
, 0),
948 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PSS_SALTLEN
, NULL
, 0),
952 static const OSSL_PARAM
*rsa_gettable_ctx_params(void)
954 return known_gettable_ctx_params
;
957 static int rsa_set_ctx_params(void *vprsactx
, const OSSL_PARAM params
[])
959 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
962 if (prsactx
== NULL
|| params
== NULL
)
965 p
= OSSL_PARAM_locate_const(params
, OSSL_SIGNATURE_PARAM_DIGEST
);
966 /* Not allowed during certain operations */
967 if (p
!= NULL
&& !prsactx
->flag_allow_md
)
970 char mdname
[OSSL_MAX_NAME_SIZE
] = "", *pmdname
= mdname
;
971 char mdprops
[OSSL_MAX_PROPQUERY_SIZE
] = "", *pmdprops
= mdprops
;
972 const OSSL_PARAM
*propsp
=
973 OSSL_PARAM_locate_const(params
,
974 OSSL_SIGNATURE_PARAM_PROPERTIES
);
976 if (!OSSL_PARAM_get_utf8_string(p
, &pmdname
, sizeof(mdname
)))
981 else if (!OSSL_PARAM_get_utf8_string(propsp
,
982 &pmdprops
, sizeof(mdprops
)))
985 if (rsa_pss_restricted(prsactx
)) {
986 /* TODO(3.0) figure out what to do for prsactx->md == NULL */
987 if (prsactx
->md
== NULL
|| EVP_MD_is_a(prsactx
->md
, mdname
))
989 ERR_raise(ERR_LIB_PROV
, PROV_R_DIGEST_NOT_ALLOWED
);
993 /* non-PSS code follows */
994 if (!rsa_setup_md(prsactx
, mdname
, pmdprops
))
998 p
= OSSL_PARAM_locate_const(params
, OSSL_SIGNATURE_PARAM_PAD_MODE
);
1001 const char *err_extra_text
= NULL
;
1003 switch (p
->data_type
) {
1004 case OSSL_PARAM_INTEGER
: /* Support for legacy pad mode number */
1005 if (!OSSL_PARAM_get_int(p
, &pad_mode
))
1008 case OSSL_PARAM_UTF8_STRING
:
1012 if (p
->data
== NULL
)
1015 for (i
= 0; padding_item
[i
].id
!= 0; i
++) {
1016 if (strcmp(p
->data
, padding_item
[i
].ptr
) == 0) {
1017 pad_mode
= padding_item
[i
].id
;
1028 case RSA_PKCS1_OAEP_PADDING
:
1030 * OAEP padding is for asymmetric cipher only so is not compatible
1031 * with signature use.
1033 err_extra_text
= "OAEP padding not allowed for signing / verifying";
1035 case RSA_PKCS1_PSS_PADDING
:
1036 if ((prsactx
->operation
1037 & (EVP_PKEY_OP_SIGN
| EVP_PKEY_OP_VERIFY
)) == 0) {
1039 "PSS padding only allowed for sign and verify operations";
1042 if (prsactx
->md
== NULL
1043 && !rsa_setup_md(prsactx
, OSSL_DIGEST_NAME_SHA1
, NULL
)) {
1047 case RSA_PKCS1_PADDING
:
1048 err_extra_text
= "PKCS#1 padding not allowed with RSA-PSS";
1050 case RSA_SSLV23_PADDING
:
1051 err_extra_text
= "SSLv3 padding not allowed with RSA-PSS";
1053 case RSA_NO_PADDING
:
1054 err_extra_text
= "No padding not allowed with RSA-PSS";
1056 case RSA_X931_PADDING
:
1057 err_extra_text
= "X.931 padding not allowed with RSA-PSS";
1059 if (RSA_test_flags(prsactx
->rsa
,
1060 RSA_FLAG_TYPE_MASK
) == RSA_FLAG_TYPE_RSA
)
1065 if (err_extra_text
== NULL
)
1066 ERR_raise(ERR_LIB_PROV
,
1067 PROV_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE
);
1069 ERR_raise_data(ERR_LIB_PROV
,
1070 PROV_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE
,
1074 if (!rsa_check_padding(prsactx
->mdnid
, pad_mode
))
1076 prsactx
->pad_mode
= pad_mode
;
1079 p
= OSSL_PARAM_locate_const(params
, OSSL_SIGNATURE_PARAM_PSS_SALTLEN
);
1083 if (prsactx
->pad_mode
!= RSA_PKCS1_PSS_PADDING
) {
1084 ERR_raise_data(ERR_LIB_PROV
, PROV_R_NOT_SUPPORTED
,
1085 "PSS saltlen can only be specified if "
1086 "PSS padding has been specified first");
1090 switch (p
->data_type
) {
1091 case OSSL_PARAM_INTEGER
: /* Support for legacy pad mode number */
1092 if (!OSSL_PARAM_get_int(p
, &saltlen
))
1095 case OSSL_PARAM_UTF8_STRING
:
1096 if (strcmp(p
->data
, "digest") == 0)
1097 saltlen
= RSA_PSS_SALTLEN_DIGEST
;
1098 else if (strcmp(p
->data
, "max") == 0)
1099 saltlen
= RSA_PSS_SALTLEN_MAX
;
1100 else if (strcmp(p
->data
, "auto") == 0)
1101 saltlen
= RSA_PSS_SALTLEN_AUTO
;
1103 saltlen
= atoi(p
->data
);
1110 * RSA_PSS_SALTLEN_MAX seems curiously named in this check.
1111 * Contrary to what it's name suggests, it's the currently
1112 * lowest saltlen number possible.
1114 if (saltlen
< RSA_PSS_SALTLEN_MAX
) {
1115 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_PSS_SALTLEN
);
1119 if (rsa_pss_restricted(prsactx
)) {
1120 switch (prsactx
->saltlen
) {
1121 case RSA_PSS_SALTLEN_AUTO
:
1122 if (prsactx
->operation
== EVP_PKEY_OP_VERIFY
) {
1123 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_PSS_SALTLEN
);
1127 case RSA_PSS_SALTLEN_DIGEST
:
1128 if (prsactx
->min_saltlen
> EVP_MD_size(prsactx
->md
)) {
1129 ERR_raise_data(ERR_LIB_PROV
,
1130 PROV_R_PSS_SALTLEN_TOO_SMALL
,
1131 "Should be more than %d, but would be "
1132 "set to match digest size (%d)",
1133 prsactx
->min_saltlen
,
1134 EVP_MD_size(prsactx
->md
));
1139 if (saltlen
>= 0 && saltlen
< prsactx
->min_saltlen
) {
1140 ERR_raise_data(ERR_LIB_PROV
,
1141 PROV_R_PSS_SALTLEN_TOO_SMALL
,
1142 "Should be more than %d, "
1143 "but would be set to %d",
1144 prsactx
->min_saltlen
, saltlen
);
1150 prsactx
->saltlen
= saltlen
;
1153 p
= OSSL_PARAM_locate_const(params
, OSSL_SIGNATURE_PARAM_MGF1_DIGEST
);
1155 char mdname
[OSSL_MAX_NAME_SIZE
] = "", *pmdname
= mdname
;
1156 char mdprops
[OSSL_MAX_PROPQUERY_SIZE
] = "", *pmdprops
= mdprops
;
1157 const OSSL_PARAM
*propsp
=
1158 OSSL_PARAM_locate_const(params
,
1159 OSSL_SIGNATURE_PARAM_MGF1_PROPERTIES
);
1161 if (!OSSL_PARAM_get_utf8_string(p
, &pmdname
, sizeof(mdname
)))
1166 else if (!OSSL_PARAM_get_utf8_string(propsp
,
1167 &pmdprops
, sizeof(mdprops
)))
1170 if (prsactx
->pad_mode
!= RSA_PKCS1_PSS_PADDING
) {
1171 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_MGF1_MD
);
1175 if (rsa_pss_restricted(prsactx
)) {
1176 /* TODO(3.0) figure out what to do for prsactx->mgf1_md == NULL */
1177 if (prsactx
->mgf1_md
== NULL
1178 || EVP_MD_is_a(prsactx
->mgf1_md
, mdname
))
1180 ERR_raise(ERR_LIB_PROV
, PROV_R_DIGEST_NOT_ALLOWED
);
1184 /* non-PSS code follows */
1185 if (!rsa_setup_mgf1_md(prsactx
, mdname
, pmdprops
))
1192 static const OSSL_PARAM known_settable_ctx_params
[] = {
1193 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PAD_MODE
, NULL
, 0),
1194 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST
, NULL
, 0),
1195 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PROPERTIES
, NULL
, 0),
1196 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_MGF1_DIGEST
, NULL
, 0),
1197 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_MGF1_PROPERTIES
, NULL
, 0),
1198 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PSS_SALTLEN
, NULL
, 0),
1202 static const OSSL_PARAM
*rsa_settable_ctx_params(void)
1205 * TODO(3.0): Should this function return a different set of settable ctx
1206 * params if the ctx is being used for a DigestSign/DigestVerify? In that
1207 * case it is not allowed to set the digest size/digest name because the
1208 * digest is explicitly set as part of the init.
1210 return known_settable_ctx_params
;
1213 static int rsa_get_ctx_md_params(void *vprsactx
, OSSL_PARAM
*params
)
1215 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
1217 if (prsactx
->mdctx
== NULL
)
1220 return EVP_MD_CTX_get_params(prsactx
->mdctx
, params
);
1223 static const OSSL_PARAM
*rsa_gettable_ctx_md_params(void *vprsactx
)
1225 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
1227 if (prsactx
->md
== NULL
)
1230 return EVP_MD_gettable_ctx_params(prsactx
->md
);
1233 static int rsa_set_ctx_md_params(void *vprsactx
, const OSSL_PARAM params
[])
1235 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
1237 if (prsactx
->mdctx
== NULL
)
1240 return EVP_MD_CTX_set_params(prsactx
->mdctx
, params
);
1243 static const OSSL_PARAM
*rsa_settable_ctx_md_params(void *vprsactx
)
1245 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
1247 if (prsactx
->md
== NULL
)
1250 return EVP_MD_settable_ctx_params(prsactx
->md
);
1253 const OSSL_DISPATCH rsa_signature_functions
[] = {
1254 { OSSL_FUNC_SIGNATURE_NEWCTX
, (void (*)(void))rsa_newctx
},
1255 { OSSL_FUNC_SIGNATURE_SIGN_INIT
, (void (*)(void))rsa_sign_init
},
1256 { OSSL_FUNC_SIGNATURE_SIGN
, (void (*)(void))rsa_sign
},
1257 { OSSL_FUNC_SIGNATURE_VERIFY_INIT
, (void (*)(void))rsa_verify_init
},
1258 { OSSL_FUNC_SIGNATURE_VERIFY
, (void (*)(void))rsa_verify
},
1259 { OSSL_FUNC_SIGNATURE_VERIFY_RECOVER_INIT
,
1260 (void (*)(void))rsa_verify_recover_init
},
1261 { OSSL_FUNC_SIGNATURE_VERIFY_RECOVER
,
1262 (void (*)(void))rsa_verify_recover
},
1263 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT
,
1264 (void (*)(void))rsa_digest_sign_init
},
1265 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE
,
1266 (void (*)(void))rsa_digest_signverify_update
},
1267 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL
,
1268 (void (*)(void))rsa_digest_sign_final
},
1269 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT
,
1270 (void (*)(void))rsa_digest_verify_init
},
1271 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE
,
1272 (void (*)(void))rsa_digest_signverify_update
},
1273 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL
,
1274 (void (*)(void))rsa_digest_verify_final
},
1275 { OSSL_FUNC_SIGNATURE_FREECTX
, (void (*)(void))rsa_freectx
},
1276 { OSSL_FUNC_SIGNATURE_DUPCTX
, (void (*)(void))rsa_dupctx
},
1277 { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS
, (void (*)(void))rsa_get_ctx_params
},
1278 { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS
,
1279 (void (*)(void))rsa_gettable_ctx_params
},
1280 { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS
, (void (*)(void))rsa_set_ctx_params
},
1281 { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS
,
1282 (void (*)(void))rsa_settable_ctx_params
},
1283 { OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS
,
1284 (void (*)(void))rsa_get_ctx_md_params
},
1285 { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS
,
1286 (void (*)(void))rsa_gettable_ctx_md_params
},
1287 { OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS
,
1288 (void (*)(void))rsa_set_ctx_md_params
},
1289 { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS
,
1290 (void (*)(void))rsa_settable_ctx_md_params
},