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_dispatch.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_FUNC_signature_newctx_fn rsa_newctx
;
34 static OSSL_FUNC_signature_sign_init_fn rsa_sign_init
;
35 static OSSL_FUNC_signature_verify_init_fn rsa_verify_init
;
36 static OSSL_FUNC_signature_verify_recover_init_fn rsa_verify_recover_init
;
37 static OSSL_FUNC_signature_sign_fn rsa_sign
;
38 static OSSL_FUNC_signature_verify_fn rsa_verify
;
39 static OSSL_FUNC_signature_verify_recover_fn rsa_verify_recover
;
40 static OSSL_FUNC_signature_digest_sign_init_fn rsa_digest_sign_init
;
41 static OSSL_FUNC_signature_digest_sign_update_fn rsa_digest_signverify_update
;
42 static OSSL_FUNC_signature_digest_sign_final_fn rsa_digest_sign_final
;
43 static OSSL_FUNC_signature_digest_verify_init_fn rsa_digest_verify_init
;
44 static OSSL_FUNC_signature_digest_verify_update_fn rsa_digest_signverify_update
;
45 static OSSL_FUNC_signature_digest_verify_final_fn rsa_digest_verify_final
;
46 static OSSL_FUNC_signature_freectx_fn rsa_freectx
;
47 static OSSL_FUNC_signature_dupctx_fn rsa_dupctx
;
48 static OSSL_FUNC_signature_get_ctx_params_fn rsa_get_ctx_params
;
49 static OSSL_FUNC_signature_gettable_ctx_params_fn rsa_gettable_ctx_params
;
50 static OSSL_FUNC_signature_set_ctx_params_fn rsa_set_ctx_params
;
51 static OSSL_FUNC_signature_settable_ctx_params_fn rsa_settable_ctx_params
;
52 static OSSL_FUNC_signature_get_ctx_md_params_fn rsa_get_ctx_md_params
;
53 static OSSL_FUNC_signature_gettable_ctx_md_params_fn rsa_gettable_ctx_md_params
;
54 static OSSL_FUNC_signature_set_ctx_md_params_fn rsa_set_ctx_md_params
;
55 static OSSL_FUNC_signature_settable_ctx_md_params_fn rsa_settable_ctx_md_params
;
57 static OSSL_ITEM padding_item
[] = {
58 { RSA_PKCS1_PADDING
, OSSL_PKEY_RSA_PAD_MODE_PKCSV15
},
59 { RSA_SSLV23_PADDING
, OSSL_PKEY_RSA_PAD_MODE_SSLV23
},
60 { RSA_NO_PADDING
, OSSL_PKEY_RSA_PAD_MODE_NONE
},
61 { RSA_X931_PADDING
, OSSL_PKEY_RSA_PAD_MODE_X931
},
62 { RSA_PKCS1_PSS_PADDING
, OSSL_PKEY_RSA_PAD_MODE_PSS
},
67 * What's passed as an actual key is defined by the KEYMGMT interface.
68 * We happen to know that our KEYMGMT simply passes RSA structures, so
69 * 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
;
162 static int rsa_check_padding(int mdnid
, int padding
)
164 if (padding
== RSA_NO_PADDING
) {
165 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_PADDING_MODE
);
169 if (padding
== RSA_X931_PADDING
) {
170 if (RSA_X931_hash_id(mdnid
) == -1) {
171 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_X931_DIGEST
);
179 static int rsa_check_parameters(PROV_RSA_CTX
*prsactx
)
181 if (prsactx
->pad_mode
== RSA_PKCS1_PSS_PADDING
) {
184 /* See if minimum salt length exceeds maximum possible */
185 max_saltlen
= RSA_size(prsactx
->rsa
) - EVP_MD_size(prsactx
->md
);
186 if ((RSA_bits(prsactx
->rsa
) & 0x7) == 1)
188 if (prsactx
->min_saltlen
< 0 || prsactx
->min_saltlen
> max_saltlen
) {
189 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_SALT_LENGTH
);
196 static void *rsa_newctx(void *provctx
, const char *propq
)
198 PROV_RSA_CTX
*prsactx
= NULL
;
199 char *propq_copy
= NULL
;
201 if ((prsactx
= OPENSSL_zalloc(sizeof(PROV_RSA_CTX
))) == NULL
203 && (propq_copy
= OPENSSL_strdup(propq
)) == NULL
)) {
204 OPENSSL_free(prsactx
);
205 ERR_raise(ERR_LIB_PROV
, ERR_R_MALLOC_FAILURE
);
209 prsactx
->libctx
= PROV_LIBRARY_CONTEXT_OF(provctx
);
210 prsactx
->flag_allow_md
= 1;
211 prsactx
->propq
= propq_copy
;
215 /* True if PSS parameters are restricted */
216 #define rsa_pss_restricted(prsactx) (prsactx->min_saltlen != -1)
218 static int rsa_setup_md(PROV_RSA_CTX
*ctx
, const char *mdname
,
222 mdprops
= ctx
->propq
;
224 if (mdname
!= NULL
) {
225 EVP_MD
*md
= EVP_MD_fetch(ctx
->libctx
, mdname
, mdprops
);
226 int md_nid
= rsa_get_md_nid(md
);
228 size_t mdname_len
= strlen(mdname
);
231 || md_nid
== NID_undef
232 || !rsa_check_padding(md_nid
, ctx
->pad_mode
)
233 || mdname_len
>= sizeof(ctx
->mdname
)) {
235 ERR_raise_data(ERR_LIB_PROV
, PROV_R_INVALID_DIGEST
,
236 "%s could not be fetched", mdname
);
237 if (md_nid
== NID_undef
)
238 ERR_raise_data(ERR_LIB_PROV
, PROV_R_DIGEST_NOT_ALLOWED
,
239 "digest=%s", mdname
);
240 if (mdname_len
>= sizeof(ctx
->mdname
))
241 ERR_raise_data(ERR_LIB_PROV
, PROV_R_INVALID_DIGEST
,
242 "%s exceeds name buffer length", mdname
);
247 EVP_MD_CTX_free(ctx
->mdctx
);
248 EVP_MD_free(ctx
->md
);
251 * TODO(3.0) Should we care about DER writing errors?
252 * All it really means is that for some reason, there's no
253 * AlgorithmIdentifier to be had (consider RSA with MD5-SHA1),
254 * but the operation itself is still valid, just as long as it's
255 * not used to construct anything that needs an AlgorithmIdentifier.
258 if (WPACKET_init_der(&pkt
, ctx
->aid_buf
, sizeof(ctx
->aid_buf
))
259 && DER_w_algorithmIdentifier_MDWithRSAEncryption(&pkt
, -1, ctx
->rsa
,
261 && WPACKET_finish(&pkt
)) {
262 WPACKET_get_total_written(&pkt
, &ctx
->aid_len
);
263 ctx
->aid
= WPACKET_get_curr(&pkt
);
265 WPACKET_cleanup(&pkt
);
270 OPENSSL_strlcpy(ctx
->mdname
, mdname
, sizeof(ctx
->mdname
));
276 static int rsa_setup_mgf1_md(PROV_RSA_CTX
*ctx
, const char *mdname
,
282 mdprops
= ctx
->propq
;
284 if (ctx
->mgf1_mdname
[0] != '\0')
285 EVP_MD_free(ctx
->mgf1_md
);
287 if ((ctx
->mgf1_md
= EVP_MD_fetch(ctx
->libctx
, mdname
, mdprops
)) == NULL
) {
288 ERR_raise_data(ERR_LIB_PROV
, PROV_R_INVALID_DIGEST
,
289 "%s could not be fetched", mdname
);
292 len
= OPENSSL_strlcpy(ctx
->mgf1_mdname
, mdname
, sizeof(ctx
->mgf1_mdname
));
293 if (len
>= sizeof(ctx
->mgf1_mdname
)) {
294 ERR_raise_data(ERR_LIB_PROV
, PROV_R_INVALID_DIGEST
,
295 "%s exceeds name buffer length", mdname
);
302 static int rsa_signature_init(void *vprsactx
, void *vrsa
, int operation
)
304 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
306 if (prsactx
== NULL
|| vrsa
== NULL
|| !RSA_up_ref(vrsa
))
309 RSA_free(prsactx
->rsa
);
311 prsactx
->operation
= operation
;
313 /* Maximum for sign, auto for verify */
314 prsactx
->saltlen
= RSA_PSS_SALTLEN_AUTO
;
315 prsactx
->min_saltlen
= -1;
317 switch (RSA_test_flags(prsactx
->rsa
, RSA_FLAG_TYPE_MASK
)) {
318 case RSA_FLAG_TYPE_RSA
:
319 prsactx
->pad_mode
= RSA_PKCS1_PADDING
;
321 case RSA_FLAG_TYPE_RSASSAPSS
:
322 prsactx
->pad_mode
= RSA_PKCS1_PSS_PADDING
;
325 const RSA_PSS_PARAMS_30
*pss
=
326 rsa_get0_pss_params_30(prsactx
->rsa
);
328 if (!rsa_pss_params_30_is_unrestricted(pss
)) {
329 int md_nid
= rsa_pss_params_30_hashalg(pss
);
330 int mgf1md_nid
= rsa_pss_params_30_maskgenhashalg(pss
);
331 int min_saltlen
= rsa_pss_params_30_saltlen(pss
);
332 const char *mdname
, *mgf1mdname
;
335 mdname
= rsa_oaeppss_nid2name(md_nid
);
336 mgf1mdname
= rsa_oaeppss_nid2name(mgf1md_nid
);
337 prsactx
->min_saltlen
= min_saltlen
;
339 if (mdname
== NULL
) {
340 ERR_raise_data(ERR_LIB_PROV
, PROV_R_INVALID_DIGEST
,
341 "PSS restrictions lack hash algorithm");
344 if (mgf1mdname
== NULL
) {
345 ERR_raise_data(ERR_LIB_PROV
, PROV_R_INVALID_DIGEST
,
346 "PSS restrictions lack MGF1 hash algorithm");
350 len
= OPENSSL_strlcpy(prsactx
->mdname
, mdname
,
351 sizeof(prsactx
->mdname
));
352 if (len
>= sizeof(prsactx
->mdname
)) {
353 ERR_raise_data(ERR_LIB_PROV
, PROV_R_INVALID_DIGEST
,
354 "hash algorithm name too long");
357 len
= OPENSSL_strlcpy(prsactx
->mgf1_mdname
, mgf1mdname
,
358 sizeof(prsactx
->mgf1_mdname
));
359 if (len
>= sizeof(prsactx
->mgf1_mdname
)) {
360 ERR_raise_data(ERR_LIB_PROV
, PROV_R_INVALID_DIGEST
,
361 "MGF1 hash algorithm name too long");
364 prsactx
->saltlen
= min_saltlen
;
366 return rsa_setup_md(prsactx
, mdname
, prsactx
->propq
)
367 && rsa_setup_mgf1_md(prsactx
, mgf1mdname
, prsactx
->propq
)
368 && rsa_check_parameters(prsactx
);
374 ERR_raise(ERR_LIB_RSA
, PROV_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
381 static int setup_tbuf(PROV_RSA_CTX
*ctx
)
383 if (ctx
->tbuf
!= NULL
)
385 if ((ctx
->tbuf
= OPENSSL_malloc(RSA_size(ctx
->rsa
))) == NULL
) {
386 ERR_raise(ERR_LIB_PROV
, ERR_R_MALLOC_FAILURE
);
392 static void clean_tbuf(PROV_RSA_CTX
*ctx
)
394 if (ctx
->tbuf
!= NULL
)
395 OPENSSL_cleanse(ctx
->tbuf
, RSA_size(ctx
->rsa
));
398 static void free_tbuf(PROV_RSA_CTX
*ctx
)
401 OPENSSL_free(ctx
->tbuf
);
405 static int rsa_sign_init(void *vprsactx
, void *vrsa
)
407 return rsa_signature_init(vprsactx
, vrsa
, EVP_PKEY_OP_SIGN
);
410 static int rsa_sign(void *vprsactx
, unsigned char *sig
, size_t *siglen
,
411 size_t sigsize
, const unsigned char *tbs
, size_t tbslen
)
413 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
415 size_t rsasize
= RSA_size(prsactx
->rsa
);
416 size_t mdsize
= rsa_get_md_size(prsactx
);
423 if (sigsize
< rsasize
) {
424 ERR_raise_data(ERR_LIB_PROV
, PROV_R_INVALID_SIGNATURE_SIZE
,
425 "is %zu, should be at least %zu", sigsize
, rsasize
);
430 if (tbslen
!= mdsize
) {
431 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_DIGEST_LENGTH
);
436 if (EVP_MD_is_a(prsactx
->md
, OSSL_DIGEST_NAME_MDC2
)) {
439 if (prsactx
->pad_mode
!= RSA_PKCS1_PADDING
) {
440 ERR_raise_data(ERR_LIB_PROV
, PROV_R_INVALID_PADDING_MODE
,
441 "only PKCS#1 padding supported with MDC2");
444 ret
= RSA_sign_ASN1_OCTET_STRING(0, tbs
, tbslen
, sig
, &sltmp
,
448 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
455 switch (prsactx
->pad_mode
) {
456 case RSA_X931_PADDING
:
457 if ((size_t)RSA_size(prsactx
->rsa
) < tbslen
+ 1) {
458 ERR_raise_data(ERR_LIB_PROV
, PROV_R_KEY_SIZE_TOO_SMALL
,
459 "RSA key size = %d, expected minimum = %d",
460 RSA_size(prsactx
->rsa
), tbslen
+ 1);
463 if (!setup_tbuf(prsactx
)) {
464 ERR_raise(ERR_LIB_PROV
, ERR_R_MALLOC_FAILURE
);
467 memcpy(prsactx
->tbuf
, tbs
, tbslen
);
468 prsactx
->tbuf
[tbslen
] = RSA_X931_hash_id(prsactx
->mdnid
);
469 ret
= RSA_private_encrypt(tbslen
+ 1, prsactx
->tbuf
,
470 sig
, prsactx
->rsa
, RSA_X931_PADDING
);
474 case RSA_PKCS1_PADDING
:
478 ret
= RSA_sign(prsactx
->mdnid
, tbs
, tbslen
, sig
, &sltmp
,
481 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
488 case RSA_PKCS1_PSS_PADDING
:
489 /* Check PSS restrictions */
490 if (rsa_pss_restricted(prsactx
)) {
491 switch (prsactx
->saltlen
) {
492 case RSA_PSS_SALTLEN_DIGEST
:
493 if (prsactx
->min_saltlen
> EVP_MD_size(prsactx
->md
)) {
494 ERR_raise_data(ERR_LIB_PROV
,
495 PROV_R_PSS_SALTLEN_TOO_SMALL
,
496 "minimum salt length set to %d, "
497 "but the digest only gives %d",
498 prsactx
->min_saltlen
,
499 EVP_MD_size(prsactx
->md
));
504 if (prsactx
->saltlen
>= 0
505 && prsactx
->saltlen
< prsactx
->min_saltlen
) {
506 ERR_raise_data(ERR_LIB_PROV
,
507 PROV_R_PSS_SALTLEN_TOO_SMALL
,
508 "minimum salt length set to %d, but the"
509 "actual salt length is only set to %d",
510 prsactx
->min_saltlen
,
517 if (!setup_tbuf(prsactx
))
519 if (!RSA_padding_add_PKCS1_PSS_mgf1(prsactx
->rsa
,
521 prsactx
->md
, prsactx
->mgf1_md
,
523 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
526 ret
= RSA_private_encrypt(RSA_size(prsactx
->rsa
), prsactx
->tbuf
,
527 sig
, prsactx
->rsa
, RSA_NO_PADDING
);
532 ERR_raise_data(ERR_LIB_PROV
, PROV_R_INVALID_PADDING_MODE
,
533 "Only X.931, PKCS#1 v1.5 or PSS padding allowed");
537 ret
= RSA_private_encrypt(tbslen
, tbs
, sig
, prsactx
->rsa
,
545 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
553 static int rsa_verify_recover_init(void *vprsactx
, void *vrsa
)
555 return rsa_signature_init(vprsactx
, vrsa
, EVP_PKEY_OP_VERIFYRECOVER
);
558 static int rsa_verify_recover(void *vprsactx
,
562 const unsigned char *sig
,
565 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
569 *routlen
= RSA_size(prsactx
->rsa
);
573 if (prsactx
->md
!= NULL
) {
574 switch (prsactx
->pad_mode
) {
575 case RSA_X931_PADDING
:
576 if (!setup_tbuf(prsactx
))
578 ret
= RSA_public_decrypt(siglen
, sig
, prsactx
->tbuf
, prsactx
->rsa
,
581 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
585 if (prsactx
->tbuf
[ret
] != RSA_X931_hash_id(prsactx
->mdnid
)) {
586 ERR_raise(ERR_LIB_PROV
, PROV_R_ALGORITHM_MISMATCH
);
589 if (ret
!= EVP_MD_size(prsactx
->md
)) {
590 ERR_raise_data(ERR_LIB_PROV
, PROV_R_INVALID_DIGEST_LENGTH
,
591 "Should be %d, but got %d",
592 EVP_MD_size(prsactx
->md
), ret
);
597 if (rout
!= prsactx
->tbuf
) {
598 if (routsize
< (size_t)ret
) {
599 ERR_raise_data(ERR_LIB_PROV
, PROV_R_OUTPUT_BUFFER_TOO_SMALL
,
600 "buffer size is %d, should be %d",
604 memcpy(rout
, prsactx
->tbuf
, ret
);
608 case RSA_PKCS1_PADDING
:
612 ret
= int_rsa_verify(prsactx
->mdnid
, NULL
, 0, rout
, &sltmp
,
613 sig
, siglen
, prsactx
->rsa
);
615 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
623 ERR_raise_data(ERR_LIB_PROV
, PROV_R_INVALID_PADDING_MODE
,
624 "Only X.931 or PKCS#1 v1.5 padding allowed");
628 ret
= RSA_public_decrypt(siglen
, sig
, rout
, prsactx
->rsa
,
631 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
639 static int rsa_verify_init(void *vprsactx
, void *vrsa
)
641 return rsa_signature_init(vprsactx
, vrsa
, EVP_PKEY_OP_VERIFY
);
644 static int rsa_verify(void *vprsactx
, const unsigned char *sig
, size_t siglen
,
645 const unsigned char *tbs
, size_t tbslen
)
647 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
650 if (prsactx
->md
!= NULL
) {
651 switch (prsactx
->pad_mode
) {
652 case RSA_PKCS1_PADDING
:
653 if (!RSA_verify(prsactx
->mdnid
, tbs
, tbslen
, sig
, siglen
,
655 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
659 case RSA_X931_PADDING
:
660 if (!setup_tbuf(prsactx
))
662 if (rsa_verify_recover(prsactx
, prsactx
->tbuf
, &rslen
, 0,
666 case RSA_PKCS1_PSS_PADDING
:
672 * We need to check this for the RSA_verify_PKCS1_PSS_mgf1()
675 mdsize
= rsa_get_md_size(prsactx
);
676 if (tbslen
!= mdsize
) {
677 ERR_raise_data(ERR_LIB_PROV
, PROV_R_INVALID_DIGEST_LENGTH
,
678 "Should be %d, but got %d",
683 if (!setup_tbuf(prsactx
))
685 ret
= RSA_public_decrypt(siglen
, sig
, prsactx
->tbuf
,
686 prsactx
->rsa
, RSA_NO_PADDING
);
688 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
691 ret
= RSA_verify_PKCS1_PSS_mgf1(prsactx
->rsa
, tbs
,
692 prsactx
->md
, prsactx
->mgf1_md
,
696 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
702 ERR_raise_data(ERR_LIB_PROV
, PROV_R_INVALID_PADDING_MODE
,
703 "Only X.931, PKCS#1 v1.5 or PSS padding allowed");
707 if (!setup_tbuf(prsactx
))
709 rslen
= RSA_public_decrypt(siglen
, sig
, prsactx
->tbuf
, prsactx
->rsa
,
712 ERR_raise(ERR_LIB_PROV
, ERR_LIB_RSA
);
717 if ((rslen
!= tbslen
) || memcmp(tbs
, prsactx
->tbuf
, rslen
))
723 static int rsa_digest_signverify_init(void *vprsactx
, const char *mdname
,
724 void *vrsa
, int operation
)
726 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
729 prsactx
->flag_allow_md
= 0;
730 if (!rsa_signature_init(vprsactx
, vrsa
, operation
)
731 || !rsa_setup_md(prsactx
, mdname
, NULL
)) /* TODO RL */
734 prsactx
->mdctx
= EVP_MD_CTX_new();
735 if (prsactx
->mdctx
== NULL
) {
736 ERR_raise(ERR_LIB_PROV
, ERR_R_MALLOC_FAILURE
);
740 if (!EVP_DigestInit_ex(prsactx
->mdctx
, prsactx
->md
, NULL
))
746 EVP_MD_CTX_free(prsactx
->mdctx
);
747 EVP_MD_free(prsactx
->md
);
748 prsactx
->mdctx
= NULL
;
753 static int rsa_digest_signverify_update(void *vprsactx
,
754 const unsigned char *data
,
757 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
759 if (prsactx
== NULL
|| prsactx
->mdctx
== NULL
)
762 return EVP_DigestUpdate(prsactx
->mdctx
, data
, datalen
);
765 static int rsa_digest_sign_init(void *vprsactx
, const char *mdname
,
768 return rsa_digest_signverify_init(vprsactx
, mdname
, vrsa
,
772 static int rsa_digest_sign_final(void *vprsactx
, unsigned char *sig
,
773 size_t *siglen
, size_t sigsize
)
775 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
776 unsigned char digest
[EVP_MAX_MD_SIZE
];
777 unsigned int dlen
= 0;
781 prsactx
->flag_allow_md
= 1;
782 if (prsactx
->mdctx
== NULL
)
785 * If sig is NULL then we're just finding out the sig size. Other fields
786 * are ignored. Defer to rsa_sign.
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
))
798 return rsa_sign(vprsactx
, sig
, siglen
, sigsize
, digest
, (size_t)dlen
);
801 static int rsa_digest_verify_init(void *vprsactx
, const char *mdname
,
804 return rsa_digest_signverify_init(vprsactx
, mdname
, vrsa
,
808 int rsa_digest_verify_final(void *vprsactx
, const unsigned char *sig
,
811 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
812 unsigned char digest
[EVP_MAX_MD_SIZE
];
813 unsigned int dlen
= 0;
817 prsactx
->flag_allow_md
= 1;
818 if (prsactx
->mdctx
== NULL
)
822 * TODO(3.0): There is the possibility that some externally provided
823 * digests exceed EVP_MAX_MD_SIZE. We should probably handle that somehow -
824 * but that problem is much larger than just in RSA.
826 if (!EVP_DigestFinal_ex(prsactx
->mdctx
, digest
, &dlen
))
829 return rsa_verify(vprsactx
, sig
, siglen
, digest
, (size_t)dlen
);
832 static void rsa_freectx(void *vprsactx
)
834 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
839 EVP_MD_CTX_free(prsactx
->mdctx
);
840 EVP_MD_free(prsactx
->md
);
841 EVP_MD_free(prsactx
->mgf1_md
);
842 OPENSSL_free(prsactx
->propq
);
844 RSA_free(prsactx
->rsa
);
846 OPENSSL_clear_free(prsactx
, sizeof(*prsactx
));
849 static void *rsa_dupctx(void *vprsactx
)
851 PROV_RSA_CTX
*srcctx
= (PROV_RSA_CTX
*)vprsactx
;
852 PROV_RSA_CTX
*dstctx
;
854 dstctx
= OPENSSL_zalloc(sizeof(*srcctx
));
855 if (dstctx
== NULL
) {
856 ERR_raise(ERR_LIB_PROV
, ERR_R_MALLOC_FAILURE
);
863 dstctx
->mdctx
= NULL
;
866 if (srcctx
->rsa
!= NULL
&& !RSA_up_ref(srcctx
->rsa
))
868 dstctx
->rsa
= srcctx
->rsa
;
870 if (srcctx
->md
!= NULL
&& !EVP_MD_up_ref(srcctx
->md
))
872 dstctx
->md
= srcctx
->md
;
874 if (srcctx
->mgf1_md
!= NULL
&& !EVP_MD_up_ref(srcctx
->mgf1_md
))
876 dstctx
->mgf1_md
= srcctx
->mgf1_md
;
878 if (srcctx
->mdctx
!= NULL
) {
879 dstctx
->mdctx
= EVP_MD_CTX_new();
880 if (dstctx
->mdctx
== NULL
881 || !EVP_MD_CTX_copy_ex(dstctx
->mdctx
, srcctx
->mdctx
))
891 static int rsa_get_ctx_params(void *vprsactx
, OSSL_PARAM
*params
)
893 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
896 if (prsactx
== NULL
|| params
== NULL
)
899 p
= OSSL_PARAM_locate(params
, OSSL_SIGNATURE_PARAM_ALGORITHM_ID
);
901 && !OSSL_PARAM_set_octet_string(p
, prsactx
->aid
, prsactx
->aid_len
))
904 p
= OSSL_PARAM_locate(params
, OSSL_SIGNATURE_PARAM_PAD_MODE
);
906 switch (p
->data_type
) {
907 case OSSL_PARAM_INTEGER
:
908 if (!OSSL_PARAM_set_int(p
, prsactx
->pad_mode
))
911 case OSSL_PARAM_UTF8_STRING
:
914 const char *word
= NULL
;
916 for (i
= 0; padding_item
[i
].id
!= 0; i
++) {
917 if (prsactx
->pad_mode
== (int)padding_item
[i
].id
) {
918 word
= padding_item
[i
].ptr
;
924 if (!OSSL_PARAM_set_utf8_string(p
, word
))
927 ERR_raise(ERR_LIB_PROV
, ERR_R_INTERNAL_ERROR
);
935 p
= OSSL_PARAM_locate(params
, OSSL_SIGNATURE_PARAM_DIGEST
);
936 if (p
!= NULL
&& !OSSL_PARAM_set_utf8_string(p
, prsactx
->mdname
))
939 p
= OSSL_PARAM_locate(params
, OSSL_SIGNATURE_PARAM_MGF1_DIGEST
);
940 if (p
!= NULL
&& !OSSL_PARAM_set_utf8_string(p
, prsactx
->mgf1_mdname
))
943 p
= OSSL_PARAM_locate(params
, OSSL_SIGNATURE_PARAM_PSS_SALTLEN
);
945 if (p
->data_type
== OSSL_PARAM_INTEGER
) {
946 if (!OSSL_PARAM_set_int(p
, prsactx
->saltlen
))
948 } else if (p
->data_type
== OSSL_PARAM_UTF8_STRING
) {
949 const char *value
= NULL
;
951 switch (prsactx
->saltlen
) {
952 case RSA_PSS_SALTLEN_DIGEST
:
953 value
= OSSL_PKEY_RSA_PSS_SALT_LEN_DIGEST
;
955 case RSA_PSS_SALTLEN_MAX
:
956 value
= OSSL_PKEY_RSA_PSS_SALT_LEN_MAX
;
958 case RSA_PSS_SALTLEN_AUTO
:
959 value
= OSSL_PKEY_RSA_PSS_SALT_LEN_AUTO
;
963 int len
= BIO_snprintf(p
->data
, p
->data_size
, "%d",
968 p
->return_size
= len
;
973 && !OSSL_PARAM_set_utf8_string(p
, value
))
981 static const OSSL_PARAM known_gettable_ctx_params
[] = {
982 OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_ALGORITHM_ID
, NULL
, 0),
983 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PAD_MODE
, NULL
, 0),
984 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST
, NULL
, 0),
985 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_MGF1_DIGEST
, NULL
, 0),
986 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PSS_SALTLEN
, NULL
, 0),
990 static const OSSL_PARAM
*rsa_gettable_ctx_params(ossl_unused
void *provctx
)
992 return known_gettable_ctx_params
;
995 static int rsa_set_ctx_params(void *vprsactx
, const OSSL_PARAM params
[])
997 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
1000 if (prsactx
== NULL
|| params
== NULL
)
1003 p
= OSSL_PARAM_locate_const(params
, OSSL_SIGNATURE_PARAM_DIGEST
);
1004 /* Not allowed during certain operations */
1005 if (p
!= NULL
&& !prsactx
->flag_allow_md
)
1008 char mdname
[OSSL_MAX_NAME_SIZE
] = "", *pmdname
= mdname
;
1009 char mdprops
[OSSL_MAX_PROPQUERY_SIZE
] = "", *pmdprops
= mdprops
;
1010 const OSSL_PARAM
*propsp
=
1011 OSSL_PARAM_locate_const(params
,
1012 OSSL_SIGNATURE_PARAM_PROPERTIES
);
1014 if (!OSSL_PARAM_get_utf8_string(p
, &pmdname
, sizeof(mdname
)))
1019 else if (!OSSL_PARAM_get_utf8_string(propsp
,
1020 &pmdprops
, sizeof(mdprops
)))
1023 if (rsa_pss_restricted(prsactx
)) {
1024 /* TODO(3.0) figure out what to do for prsactx->md == NULL */
1025 if (prsactx
->md
== NULL
|| EVP_MD_is_a(prsactx
->md
, mdname
))
1027 ERR_raise(ERR_LIB_PROV
, PROV_R_DIGEST_NOT_ALLOWED
);
1031 /* non-PSS code follows */
1032 if (!rsa_setup_md(prsactx
, mdname
, pmdprops
))
1036 p
= OSSL_PARAM_locate_const(params
, OSSL_SIGNATURE_PARAM_PAD_MODE
);
1039 const char *err_extra_text
= NULL
;
1041 switch (p
->data_type
) {
1042 case OSSL_PARAM_INTEGER
: /* Support for legacy pad mode number */
1043 if (!OSSL_PARAM_get_int(p
, &pad_mode
))
1046 case OSSL_PARAM_UTF8_STRING
:
1050 if (p
->data
== NULL
)
1053 for (i
= 0; padding_item
[i
].id
!= 0; i
++) {
1054 if (strcmp(p
->data
, padding_item
[i
].ptr
) == 0) {
1055 pad_mode
= padding_item
[i
].id
;
1066 case RSA_PKCS1_OAEP_PADDING
:
1068 * OAEP padding is for asymmetric cipher only so is not compatible
1069 * with signature use.
1071 err_extra_text
= "OAEP padding not allowed for signing / verifying";
1073 case RSA_PKCS1_PSS_PADDING
:
1074 if ((prsactx
->operation
1075 & (EVP_PKEY_OP_SIGN
| EVP_PKEY_OP_VERIFY
)) == 0) {
1077 "PSS padding only allowed for sign and verify operations";
1080 if (prsactx
->md
== NULL
1081 && !rsa_setup_md(prsactx
, OSSL_DIGEST_NAME_SHA1
, NULL
)) {
1085 case RSA_PKCS1_PADDING
:
1086 err_extra_text
= "PKCS#1 padding not allowed with RSA-PSS";
1088 case RSA_SSLV23_PADDING
:
1089 err_extra_text
= "SSLv3 padding not allowed with RSA-PSS";
1091 case RSA_NO_PADDING
:
1092 err_extra_text
= "No padding not allowed with RSA-PSS";
1094 case RSA_X931_PADDING
:
1095 err_extra_text
= "X.931 padding not allowed with RSA-PSS";
1097 if (RSA_test_flags(prsactx
->rsa
,
1098 RSA_FLAG_TYPE_MASK
) == RSA_FLAG_TYPE_RSA
)
1103 if (err_extra_text
== NULL
)
1104 ERR_raise(ERR_LIB_PROV
,
1105 PROV_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE
);
1107 ERR_raise_data(ERR_LIB_PROV
,
1108 PROV_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE
,
1112 if (!rsa_check_padding(prsactx
->mdnid
, pad_mode
))
1114 prsactx
->pad_mode
= pad_mode
;
1117 p
= OSSL_PARAM_locate_const(params
, OSSL_SIGNATURE_PARAM_PSS_SALTLEN
);
1121 if (prsactx
->pad_mode
!= RSA_PKCS1_PSS_PADDING
) {
1122 ERR_raise_data(ERR_LIB_PROV
, PROV_R_NOT_SUPPORTED
,
1123 "PSS saltlen can only be specified if "
1124 "PSS padding has been specified first");
1128 switch (p
->data_type
) {
1129 case OSSL_PARAM_INTEGER
: /* Support for legacy pad mode number */
1130 if (!OSSL_PARAM_get_int(p
, &saltlen
))
1133 case OSSL_PARAM_UTF8_STRING
:
1134 if (strcmp(p
->data
, OSSL_PKEY_RSA_PSS_SALT_LEN_DIGEST
) == 0)
1135 saltlen
= RSA_PSS_SALTLEN_DIGEST
;
1136 else if (strcmp(p
->data
, OSSL_PKEY_RSA_PSS_SALT_LEN_MAX
) == 0)
1137 saltlen
= RSA_PSS_SALTLEN_MAX
;
1138 else if (strcmp(p
->data
, OSSL_PKEY_RSA_PSS_SALT_LEN_AUTO
) == 0)
1139 saltlen
= RSA_PSS_SALTLEN_AUTO
;
1141 saltlen
= atoi(p
->data
);
1148 * RSA_PSS_SALTLEN_MAX seems curiously named in this check.
1149 * Contrary to what it's name suggests, it's the currently
1150 * lowest saltlen number possible.
1152 if (saltlen
< RSA_PSS_SALTLEN_MAX
) {
1153 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_PSS_SALTLEN
);
1157 if (rsa_pss_restricted(prsactx
)) {
1159 case RSA_PSS_SALTLEN_AUTO
:
1160 if (prsactx
->operation
== EVP_PKEY_OP_VERIFY
) {
1161 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_PSS_SALTLEN
);
1165 case RSA_PSS_SALTLEN_DIGEST
:
1166 if (prsactx
->min_saltlen
> EVP_MD_size(prsactx
->md
)) {
1167 ERR_raise_data(ERR_LIB_PROV
,
1168 PROV_R_PSS_SALTLEN_TOO_SMALL
,
1169 "Should be more than %d, but would be "
1170 "set to match digest size (%d)",
1171 prsactx
->min_saltlen
,
1172 EVP_MD_size(prsactx
->md
));
1177 if (saltlen
>= 0 && saltlen
< prsactx
->min_saltlen
) {
1178 ERR_raise_data(ERR_LIB_PROV
,
1179 PROV_R_PSS_SALTLEN_TOO_SMALL
,
1180 "Should be more than %d, "
1181 "but would be set to %d",
1182 prsactx
->min_saltlen
, saltlen
);
1188 prsactx
->saltlen
= saltlen
;
1191 p
= OSSL_PARAM_locate_const(params
, OSSL_SIGNATURE_PARAM_MGF1_DIGEST
);
1193 char mdname
[OSSL_MAX_NAME_SIZE
] = "", *pmdname
= mdname
;
1194 char mdprops
[OSSL_MAX_PROPQUERY_SIZE
] = "", *pmdprops
= mdprops
;
1195 const OSSL_PARAM
*propsp
=
1196 OSSL_PARAM_locate_const(params
,
1197 OSSL_SIGNATURE_PARAM_MGF1_PROPERTIES
);
1199 if (!OSSL_PARAM_get_utf8_string(p
, &pmdname
, sizeof(mdname
)))
1204 else if (!OSSL_PARAM_get_utf8_string(propsp
,
1205 &pmdprops
, sizeof(mdprops
)))
1208 if (prsactx
->pad_mode
!= RSA_PKCS1_PSS_PADDING
) {
1209 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_MGF1_MD
);
1213 if (rsa_pss_restricted(prsactx
)) {
1214 /* TODO(3.0) figure out what to do for prsactx->mgf1_md == NULL */
1215 if (prsactx
->mgf1_md
== NULL
1216 || EVP_MD_is_a(prsactx
->mgf1_md
, mdname
))
1218 ERR_raise(ERR_LIB_PROV
, PROV_R_DIGEST_NOT_ALLOWED
);
1222 /* non-PSS code follows */
1223 if (!rsa_setup_mgf1_md(prsactx
, mdname
, pmdprops
))
1230 static const OSSL_PARAM known_settable_ctx_params
[] = {
1231 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PAD_MODE
, NULL
, 0),
1232 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST
, NULL
, 0),
1233 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PROPERTIES
, NULL
, 0),
1234 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_MGF1_DIGEST
, NULL
, 0),
1235 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_MGF1_PROPERTIES
, NULL
, 0),
1236 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PSS_SALTLEN
, NULL
, 0),
1240 static const OSSL_PARAM
*rsa_settable_ctx_params(ossl_unused
void *provctx
)
1243 * TODO(3.0): Should this function return a different set of settable ctx
1244 * params if the ctx is being used for a DigestSign/DigestVerify? In that
1245 * case it is not allowed to set the digest size/digest name because the
1246 * digest is explicitly set as part of the init.
1248 return known_settable_ctx_params
;
1251 static int rsa_get_ctx_md_params(void *vprsactx
, OSSL_PARAM
*params
)
1253 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
1255 if (prsactx
->mdctx
== NULL
)
1258 return EVP_MD_CTX_get_params(prsactx
->mdctx
, params
);
1261 static const OSSL_PARAM
*rsa_gettable_ctx_md_params(void *vprsactx
)
1263 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
1265 if (prsactx
->md
== NULL
)
1268 return EVP_MD_gettable_ctx_params(prsactx
->md
);
1271 static int rsa_set_ctx_md_params(void *vprsactx
, const OSSL_PARAM params
[])
1273 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
1275 if (prsactx
->mdctx
== NULL
)
1278 return EVP_MD_CTX_set_params(prsactx
->mdctx
, params
);
1281 static const OSSL_PARAM
*rsa_settable_ctx_md_params(void *vprsactx
)
1283 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
1285 if (prsactx
->md
== NULL
)
1288 return EVP_MD_settable_ctx_params(prsactx
->md
);
1291 const OSSL_DISPATCH rsa_signature_functions
[] = {
1292 { OSSL_FUNC_SIGNATURE_NEWCTX
, (void (*)(void))rsa_newctx
},
1293 { OSSL_FUNC_SIGNATURE_SIGN_INIT
, (void (*)(void))rsa_sign_init
},
1294 { OSSL_FUNC_SIGNATURE_SIGN
, (void (*)(void))rsa_sign
},
1295 { OSSL_FUNC_SIGNATURE_VERIFY_INIT
, (void (*)(void))rsa_verify_init
},
1296 { OSSL_FUNC_SIGNATURE_VERIFY
, (void (*)(void))rsa_verify
},
1297 { OSSL_FUNC_SIGNATURE_VERIFY_RECOVER_INIT
,
1298 (void (*)(void))rsa_verify_recover_init
},
1299 { OSSL_FUNC_SIGNATURE_VERIFY_RECOVER
,
1300 (void (*)(void))rsa_verify_recover
},
1301 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT
,
1302 (void (*)(void))rsa_digest_sign_init
},
1303 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE
,
1304 (void (*)(void))rsa_digest_signverify_update
},
1305 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL
,
1306 (void (*)(void))rsa_digest_sign_final
},
1307 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT
,
1308 (void (*)(void))rsa_digest_verify_init
},
1309 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE
,
1310 (void (*)(void))rsa_digest_signverify_update
},
1311 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL
,
1312 (void (*)(void))rsa_digest_verify_final
},
1313 { OSSL_FUNC_SIGNATURE_FREECTX
, (void (*)(void))rsa_freectx
},
1314 { OSSL_FUNC_SIGNATURE_DUPCTX
, (void (*)(void))rsa_dupctx
},
1315 { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS
, (void (*)(void))rsa_get_ctx_params
},
1316 { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS
,
1317 (void (*)(void))rsa_gettable_ctx_params
},
1318 { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS
, (void (*)(void))rsa_set_ctx_params
},
1319 { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS
,
1320 (void (*)(void))rsa_settable_ctx_params
},
1321 { OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS
,
1322 (void (*)(void))rsa_get_ctx_md_params
},
1323 { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS
,
1324 (void (*)(void))rsa_gettable_ctx_md_params
},
1325 { OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS
,
1326 (void (*)(void))rsa_set_ctx_md_params
},
1327 { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS
,
1328 (void (*)(void))rsa_settable_ctx_md_params
},