Fix coverity CID #1458645 - Dereference before NULL check in rsa_digest_verify_final()
[openssl.git] / providers / implementations / signature / rsa.c
1 /*
2  * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 /*
11  * RSA low level APIs are deprecated for public use, but still ok for
12  * internal use.
13  */
14 #include "internal/deprecated.h"
15
16 #include <string.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"
32
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;
56
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 },
63     { 0,                        NULL     }
64 };
65
66 /*
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.
70  */
71
72 typedef struct {
73     OPENSSL_CTX *libctx;
74     char *propq;
75     RSA *rsa;
76     int operation;
77
78     /*
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.
83      */
84     unsigned int flag_allow_md : 1;
85
86     /* The Algorithm Identifier of the combined signature agorithm */
87     unsigned char aid_buf[128];
88     unsigned char *aid;
89     size_t  aid_len;
90
91     /* main digest */
92     EVP_MD *md;
93     EVP_MD_CTX *mdctx;
94     int mdnid;
95     char mdname[OSSL_MAX_NAME_SIZE]; /* Purely informational */
96
97     /* RSA padding mode */
98     int pad_mode;
99     /* message digest for MGF1 */
100     EVP_MD *mgf1_md;
101     char mgf1_mdname[OSSL_MAX_NAME_SIZE]; /* Purely informational */
102     /* PSS salt length */
103     int saltlen;
104     /* Minimum salt length or -1 if no PSS parameter restriction */
105     int min_saltlen;
106
107     /* Temp buffer */
108     unsigned char *tbuf;
109
110 } PROV_RSA_CTX;
111
112 static size_t rsa_get_md_size(const PROV_RSA_CTX *prsactx)
113 {
114     if (prsactx->md != NULL)
115         return EVP_MD_size(prsactx->md);
116     return 0;
117 }
118
119 static int rsa_get_md_nid(const EVP_MD *md)
120 {
121     /*
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
124      * map.
125      */
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  },
144     };
145     size_t i;
146     int mdnid = NID_undef;
147
148     if (md == NULL)
149         goto end;
150
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;
154             break;
155         }
156     }
157
158  end:
159     return mdnid;
160 }
161
162 static int rsa_check_padding(int mdnid, int padding)
163 {
164     if (padding == RSA_NO_PADDING) {
165         ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_PADDING_MODE);
166         return 0;
167     }
168
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);
172             return 0;
173         }
174     }
175
176     return 1;
177 }
178
179 static int rsa_check_parameters(PROV_RSA_CTX *prsactx)
180 {
181     if (prsactx->pad_mode == RSA_PKCS1_PSS_PADDING) {
182         int max_saltlen;
183
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)
187             max_saltlen--;
188         if (prsactx->min_saltlen < 0 || prsactx->min_saltlen > max_saltlen) {
189             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH);
190             return 0;
191         }
192     }
193     return 1;
194 }
195
196 static void *rsa_newctx(void *provctx, const char *propq)
197 {
198     PROV_RSA_CTX *prsactx = NULL;
199     char *propq_copy = NULL;
200
201     if ((prsactx = OPENSSL_zalloc(sizeof(PROV_RSA_CTX))) == NULL
202         || (propq != NULL
203             && (propq_copy = OPENSSL_strdup(propq)) == NULL)) {
204         OPENSSL_free(prsactx);
205         ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
206         return NULL;
207     }
208
209     prsactx->libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
210     prsactx->flag_allow_md = 1;
211     prsactx->propq = propq_copy;
212     return prsactx;
213 }
214
215 /* True if PSS parameters are restricted */
216 #define rsa_pss_restricted(prsactx) (prsactx->min_saltlen != -1)
217
218 static int rsa_setup_md(PROV_RSA_CTX *ctx, const char *mdname,
219                         const char *mdprops)
220 {
221     if (mdprops == NULL)
222         mdprops = ctx->propq;
223
224     if (mdname != NULL) {
225         EVP_MD *md = EVP_MD_fetch(ctx->libctx, mdname, mdprops);
226         int md_nid = rsa_get_md_nid(md);
227         WPACKET pkt;
228         size_t mdname_len = strlen(mdname);
229
230         if (md == NULL
231             || md_nid == NID_undef
232             || !rsa_check_padding(md_nid, ctx->pad_mode)
233             || mdname_len >= sizeof(ctx->mdname)) {
234             if (md == NULL)
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);
243             EVP_MD_free(md);
244             return 0;
245         }
246
247         EVP_MD_CTX_free(ctx->mdctx);
248         EVP_MD_free(ctx->md);
249
250         /*
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.
256          */
257         ctx->aid_len = 0;
258         if (WPACKET_init_der(&pkt, ctx->aid_buf, sizeof(ctx->aid_buf))
259             && DER_w_algorithmIdentifier_MDWithRSAEncryption(&pkt, -1, ctx->rsa,
260                                                              md_nid)
261             && WPACKET_finish(&pkt)) {
262             WPACKET_get_total_written(&pkt, &ctx->aid_len);
263             ctx->aid = WPACKET_get_curr(&pkt);
264         }
265         WPACKET_cleanup(&pkt);
266
267         ctx->mdctx = NULL;
268         ctx->md = md;
269         ctx->mdnid = md_nid;
270         OPENSSL_strlcpy(ctx->mdname, mdname, sizeof(ctx->mdname));
271     }
272
273     return 1;
274 }
275
276 static int rsa_setup_mgf1_md(PROV_RSA_CTX *ctx, const char *mdname,
277                              const char *mdprops)
278 {
279     size_t len;
280
281     if (mdprops == NULL)
282         mdprops = ctx->propq;
283
284     if (ctx->mgf1_mdname[0] != '\0')
285         EVP_MD_free(ctx->mgf1_md);
286
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);
290         return 0;
291     }
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);
296         return 0;
297     }
298
299     return 1;
300 }
301
302 static int rsa_signature_init(void *vprsactx, void *vrsa, int operation)
303 {
304     PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
305
306     if (prsactx == NULL || vrsa == NULL || !RSA_up_ref(vrsa))
307         return 0;
308
309     RSA_free(prsactx->rsa);
310     prsactx->rsa = vrsa;
311     prsactx->operation = operation;
312
313     /* Maximum for sign, auto for verify */
314     prsactx->saltlen = RSA_PSS_SALTLEN_AUTO;
315     prsactx->min_saltlen = -1;
316
317     switch (RSA_test_flags(prsactx->rsa, RSA_FLAG_TYPE_MASK)) {
318     case RSA_FLAG_TYPE_RSA:
319         prsactx->pad_mode = RSA_PKCS1_PADDING;
320         break;
321     case RSA_FLAG_TYPE_RSASSAPSS:
322         prsactx->pad_mode = RSA_PKCS1_PSS_PADDING;
323
324         {
325             const RSA_PSS_PARAMS_30 *pss =
326                 rsa_get0_pss_params_30(prsactx->rsa);
327
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;
333                 size_t len;
334
335                 mdname = rsa_oaeppss_nid2name(md_nid);
336                 mgf1mdname = rsa_oaeppss_nid2name(mgf1md_nid);
337                 prsactx->min_saltlen = min_saltlen;
338
339                 if (mdname == NULL) {
340                     ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
341                                    "PSS restrictions lack hash algorithm");
342                     return 0;
343                 }
344                 if (mgf1mdname == NULL) {
345                     ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
346                                    "PSS restrictions lack MGF1 hash algorithm");
347                     return 0;
348                 }
349
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");
355                     return 0;
356                 }
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");
362                     return 0;
363                 }
364                 prsactx->saltlen = min_saltlen;
365
366                 return rsa_setup_md(prsactx, mdname, prsactx->propq)
367                     && rsa_setup_mgf1_md(prsactx, mgf1mdname, prsactx->propq)
368                     && rsa_check_parameters(prsactx);
369             }
370         }
371
372         break;
373     default:
374         ERR_raise(ERR_LIB_RSA, PROV_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
375         return 0;
376     }
377
378     return 1;
379 }
380
381 static int setup_tbuf(PROV_RSA_CTX *ctx)
382 {
383     if (ctx->tbuf != NULL)
384         return 1;
385     if ((ctx->tbuf = OPENSSL_malloc(RSA_size(ctx->rsa))) == NULL) {
386         ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
387         return 0;
388     }
389     return 1;
390 }
391
392 static void clean_tbuf(PROV_RSA_CTX *ctx)
393 {
394     if (ctx->tbuf != NULL)
395         OPENSSL_cleanse(ctx->tbuf, RSA_size(ctx->rsa));
396 }
397
398 static void free_tbuf(PROV_RSA_CTX *ctx)
399 {
400     clean_tbuf(ctx);
401     OPENSSL_free(ctx->tbuf);
402     ctx->tbuf = NULL;
403 }
404
405 static int rsa_sign_init(void *vprsactx, void *vrsa)
406 {
407     return rsa_signature_init(vprsactx, vrsa, EVP_PKEY_OP_SIGN);
408 }
409
410 static int rsa_sign(void *vprsactx, unsigned char *sig, size_t *siglen,
411                     size_t sigsize, const unsigned char *tbs, size_t tbslen)
412 {
413     PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
414     int ret;
415     size_t rsasize = RSA_size(prsactx->rsa);
416     size_t mdsize = rsa_get_md_size(prsactx);
417
418     if (sig == NULL) {
419         *siglen = rsasize;
420         return 1;
421     }
422
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);
426         return 0;
427     }
428
429     if (mdsize != 0) {
430         if (tbslen != mdsize) {
431             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_LENGTH);
432             return 0;
433         }
434
435 #ifndef FIPS_MODULE
436         if (EVP_MD_is_a(prsactx->md, OSSL_DIGEST_NAME_MDC2)) {
437             unsigned int sltmp;
438
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");
442                 return 0;
443             }
444             ret = RSA_sign_ASN1_OCTET_STRING(0, tbs, tbslen, sig, &sltmp,
445                                              prsactx->rsa);
446
447             if (ret <= 0) {
448                 ERR_raise(ERR_LIB_PROV, ERR_LIB_RSA);
449                 return 0;
450             }
451             ret = sltmp;
452             goto end;
453         }
454 #endif
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);
461                 return 0;
462             }
463             if (!setup_tbuf(prsactx)) {
464                 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
465                 return 0;
466             }
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);
471             clean_tbuf(prsactx);
472             break;
473
474         case RSA_PKCS1_PADDING:
475             {
476                 unsigned int sltmp;
477
478                 ret = RSA_sign(prsactx->mdnid, tbs, tbslen, sig, &sltmp,
479                                prsactx->rsa);
480                 if (ret <= 0) {
481                     ERR_raise(ERR_LIB_PROV, ERR_LIB_RSA);
482                     return 0;
483                 }
484                 ret = sltmp;
485             }
486             break;
487
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));
500                         return 0;
501                     }
502                     /* FALLTHRU */
503                 default:
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,
511                                        prsactx->saltlen);
512                         return 0;
513                     }
514                     break;
515                 }
516             }
517             if (!setup_tbuf(prsactx))
518                 return 0;
519             if (!RSA_padding_add_PKCS1_PSS_mgf1(prsactx->rsa,
520                                                 prsactx->tbuf, tbs,
521                                                 prsactx->md, prsactx->mgf1_md,
522                                                 prsactx->saltlen)) {
523                 ERR_raise(ERR_LIB_PROV, ERR_LIB_RSA);
524                 return 0;
525             }
526             ret = RSA_private_encrypt(RSA_size(prsactx->rsa), prsactx->tbuf,
527                                       sig, prsactx->rsa, RSA_NO_PADDING);
528             clean_tbuf(prsactx);
529             break;
530
531         default:
532             ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_PADDING_MODE,
533                            "Only X.931, PKCS#1 v1.5 or PSS padding allowed");
534             return 0;
535         }
536     } else {
537         ret = RSA_private_encrypt(tbslen, tbs, sig, prsactx->rsa,
538                                   prsactx->pad_mode);
539     }
540
541 #ifndef FIPS_MODULE
542  end:
543 #endif
544     if (ret <= 0) {
545         ERR_raise(ERR_LIB_PROV, ERR_LIB_RSA);
546         return 0;
547     }
548
549     *siglen = ret;
550     return 1;
551 }
552
553 static int rsa_verify_recover_init(void *vprsactx, void *vrsa)
554 {
555     return rsa_signature_init(vprsactx, vrsa, EVP_PKEY_OP_VERIFYRECOVER);
556 }
557
558 static int rsa_verify_recover(void *vprsactx,
559                               unsigned char *rout,
560                               size_t *routlen,
561                               size_t routsize,
562                               const unsigned char *sig,
563                               size_t siglen)
564 {
565     PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
566     int ret;
567
568     if (rout == NULL) {
569         *routlen = RSA_size(prsactx->rsa);
570         return 1;
571     }
572
573     if (prsactx->md != NULL) {
574         switch (prsactx->pad_mode) {
575         case RSA_X931_PADDING:
576             if (!setup_tbuf(prsactx))
577                 return 0;
578             ret = RSA_public_decrypt(siglen, sig, prsactx->tbuf, prsactx->rsa,
579                                      RSA_X931_PADDING);
580             if (ret < 1) {
581                 ERR_raise(ERR_LIB_PROV, ERR_LIB_RSA);
582                 return 0;
583             }
584             ret--;
585             if (prsactx->tbuf[ret] != RSA_X931_hash_id(prsactx->mdnid)) {
586                 ERR_raise(ERR_LIB_PROV, PROV_R_ALGORITHM_MISMATCH);
587                 return 0;
588             }
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);
593                 return 0;
594             }
595
596             *routlen = 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",
601                                    routsize, ret);
602                     return 0;
603                 }
604                 memcpy(rout, prsactx->tbuf, ret);
605             }
606             break;
607
608         case RSA_PKCS1_PADDING:
609             {
610                 size_t sltmp;
611
612                 ret = int_rsa_verify(prsactx->mdnid, NULL, 0, rout, &sltmp,
613                                      sig, siglen, prsactx->rsa);
614                 if (ret <= 0) {
615                     ERR_raise(ERR_LIB_PROV, ERR_LIB_RSA);
616                     return 0;
617                 }
618                 ret = sltmp;
619             }
620             break;
621
622         default:
623             ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_PADDING_MODE,
624                            "Only X.931 or PKCS#1 v1.5 padding allowed");
625             return 0;
626         }
627     } else {
628         ret = RSA_public_decrypt(siglen, sig, rout, prsactx->rsa,
629                                  prsactx->pad_mode);
630         if (ret < 0) {
631             ERR_raise(ERR_LIB_PROV, ERR_LIB_RSA);
632             return 0;
633         }
634     }
635     *routlen = ret;
636     return 1;
637 }
638
639 static int rsa_verify_init(void *vprsactx, void *vrsa)
640 {
641     return rsa_signature_init(vprsactx, vrsa, EVP_PKEY_OP_VERIFY);
642 }
643
644 static int rsa_verify(void *vprsactx, const unsigned char *sig, size_t siglen,
645                       const unsigned char *tbs, size_t tbslen)
646 {
647     PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
648     size_t rslen;
649
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,
654                             prsactx->rsa)) {
655                 ERR_raise(ERR_LIB_PROV, ERR_LIB_RSA);
656                 return 0;
657             }
658             return 1;
659         case RSA_X931_PADDING:
660             if (!setup_tbuf(prsactx))
661                 return 0;
662             if (rsa_verify_recover(prsactx, prsactx->tbuf, &rslen, 0,
663                                    sig, siglen) <= 0)
664                 return 0;
665             break;
666         case RSA_PKCS1_PSS_PADDING:
667             {
668                 int ret;
669                 size_t mdsize;
670
671                 /*
672                  * We need to check this for the RSA_verify_PKCS1_PSS_mgf1()
673                  * call
674                  */
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",
679                                    mdsize, tbslen);
680                     return 0;
681                 }
682
683                 if (!setup_tbuf(prsactx))
684                     return 0;
685                 ret = RSA_public_decrypt(siglen, sig, prsactx->tbuf,
686                                          prsactx->rsa, RSA_NO_PADDING);
687                 if (ret <= 0) {
688                     ERR_raise(ERR_LIB_PROV, ERR_LIB_RSA);
689                     return 0;
690                 }
691                 ret = RSA_verify_PKCS1_PSS_mgf1(prsactx->rsa, tbs,
692                                                 prsactx->md, prsactx->mgf1_md,
693                                                 prsactx->tbuf,
694                                                 prsactx->saltlen);
695                 if (ret <= 0) {
696                     ERR_raise(ERR_LIB_PROV, ERR_LIB_RSA);
697                     return 0;
698                 }
699                 return 1;
700             }
701         default:
702             ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_PADDING_MODE,
703                            "Only X.931, PKCS#1 v1.5 or PSS padding allowed");
704             return 0;
705         }
706     } else {
707         if (!setup_tbuf(prsactx))
708             return 0;
709         rslen = RSA_public_decrypt(siglen, sig, prsactx->tbuf, prsactx->rsa,
710                                    prsactx->pad_mode);
711         if (rslen == 0) {
712             ERR_raise(ERR_LIB_PROV, ERR_LIB_RSA);
713             return 0;
714         }
715     }
716
717     if ((rslen != tbslen) || memcmp(tbs, prsactx->tbuf, rslen))
718         return 0;
719
720     return 1;
721 }
722
723 static int rsa_digest_signverify_init(void *vprsactx, const char *mdname,
724                                       void *vrsa, int operation)
725 {
726     PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
727
728     prsactx->flag_allow_md = 0;
729     if (!rsa_signature_init(vprsactx, vrsa, operation)
730         || !rsa_setup_md(prsactx, mdname, NULL)) /* TODO RL */
731         return 0;
732
733     prsactx->mdctx = EVP_MD_CTX_new();
734     if (prsactx->mdctx == NULL) {
735         ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
736         goto error;
737     }
738
739     if (!EVP_DigestInit_ex(prsactx->mdctx, prsactx->md, NULL))
740         goto error;
741
742     return 1;
743
744  error:
745     EVP_MD_CTX_free(prsactx->mdctx);
746     EVP_MD_free(prsactx->md);
747     prsactx->mdctx = NULL;
748     prsactx->md = NULL;
749     return 0;
750 }
751
752 static int rsa_digest_signverify_update(void *vprsactx,
753                                         const unsigned char *data,
754                                         size_t datalen)
755 {
756     PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
757
758     if (prsactx == NULL || prsactx->mdctx == NULL)
759         return 0;
760
761     return EVP_DigestUpdate(prsactx->mdctx, data, datalen);
762 }
763
764 static int rsa_digest_sign_init(void *vprsactx, const char *mdname,
765                                 void *vrsa)
766 {
767     return rsa_digest_signverify_init(vprsactx, mdname, vrsa,
768                                       EVP_PKEY_OP_SIGN);
769 }
770
771 static int rsa_digest_sign_final(void *vprsactx, unsigned char *sig,
772                                  size_t *siglen, size_t sigsize)
773 {
774     PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
775     unsigned char digest[EVP_MAX_MD_SIZE];
776     unsigned int dlen = 0;
777
778     if (prsactx == NULL)
779         return 0;
780     prsactx->flag_allow_md = 1;
781     if (prsactx->mdctx == NULL)
782         return 0;
783     /*
784      * If sig is NULL then we're just finding out the sig size. Other fields
785      * are ignored. Defer to rsa_sign.
786      */
787     if (sig != NULL) {
788         /*
789          * TODO(3.0): There is the possibility that some externally provided
790          * digests exceed EVP_MAX_MD_SIZE. We should probably handle that somehow -
791          * but that problem is much larger than just in RSA.
792          */
793         if (!EVP_DigestFinal_ex(prsactx->mdctx, digest, &dlen))
794             return 0;
795     }
796
797     return rsa_sign(vprsactx, sig, siglen, sigsize, digest, (size_t)dlen);
798 }
799
800 static int rsa_digest_verify_init(void *vprsactx, const char *mdname,
801                                   void *vrsa)
802 {
803     return rsa_digest_signverify_init(vprsactx, mdname, vrsa,
804                                       EVP_PKEY_OP_VERIFY);
805 }
806
807 int rsa_digest_verify_final(void *vprsactx, const unsigned char *sig,
808                             size_t siglen)
809 {
810     PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
811     unsigned char digest[EVP_MAX_MD_SIZE];
812     unsigned int dlen = 0;
813
814     prsactx->flag_allow_md = 1;
815     if (prsactx == NULL || prsactx->mdctx == NULL)
816         return 0;
817
818     /*
819      * TODO(3.0): There is the possibility that some externally provided
820      * digests exceed EVP_MAX_MD_SIZE. We should probably handle that somehow -
821      * but that problem is much larger than just in RSA.
822      */
823     if (!EVP_DigestFinal_ex(prsactx->mdctx, digest, &dlen))
824         return 0;
825
826     return rsa_verify(vprsactx, sig, siglen, digest, (size_t)dlen);
827 }
828
829 static void rsa_freectx(void *vprsactx)
830 {
831     PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
832
833     if (prsactx == NULL)
834         return;
835
836     EVP_MD_CTX_free(prsactx->mdctx);
837     EVP_MD_free(prsactx->md);
838     EVP_MD_free(prsactx->mgf1_md);
839     OPENSSL_free(prsactx->propq);
840     free_tbuf(prsactx);
841     RSA_free(prsactx->rsa);
842
843     OPENSSL_clear_free(prsactx, sizeof(*prsactx));
844 }
845
846 static void *rsa_dupctx(void *vprsactx)
847 {
848     PROV_RSA_CTX *srcctx = (PROV_RSA_CTX *)vprsactx;
849     PROV_RSA_CTX *dstctx;
850
851     dstctx = OPENSSL_zalloc(sizeof(*srcctx));
852     if (dstctx == NULL) {
853         ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
854         return NULL;
855     }
856
857     *dstctx = *srcctx;
858     dstctx->rsa = NULL;
859     dstctx->md = NULL;
860     dstctx->mdctx = NULL;
861     dstctx->tbuf = NULL;
862
863     if (srcctx->rsa != NULL && !RSA_up_ref(srcctx->rsa))
864         goto err;
865     dstctx->rsa = srcctx->rsa;
866
867     if (srcctx->md != NULL && !EVP_MD_up_ref(srcctx->md))
868         goto err;
869     dstctx->md = srcctx->md;
870
871     if (srcctx->mgf1_md != NULL && !EVP_MD_up_ref(srcctx->mgf1_md))
872         goto err;
873     dstctx->mgf1_md = srcctx->mgf1_md;
874
875     if (srcctx->mdctx != NULL) {
876         dstctx->mdctx = EVP_MD_CTX_new();
877         if (dstctx->mdctx == NULL
878                 || !EVP_MD_CTX_copy_ex(dstctx->mdctx, srcctx->mdctx))
879             goto err;
880     }
881
882     return dstctx;
883  err:
884     rsa_freectx(dstctx);
885     return NULL;
886 }
887
888 static int rsa_get_ctx_params(void *vprsactx, OSSL_PARAM *params)
889 {
890     PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
891     OSSL_PARAM *p;
892
893     if (prsactx == NULL || params == NULL)
894         return 0;
895
896     p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_ALGORITHM_ID);
897     if (p != NULL
898         && !OSSL_PARAM_set_octet_string(p, prsactx->aid, prsactx->aid_len))
899         return 0;
900
901     p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_PAD_MODE);
902     if (p != NULL)
903         switch (p->data_type) {
904         case OSSL_PARAM_INTEGER:
905             if (!OSSL_PARAM_set_int(p, prsactx->pad_mode))
906                 return 0;
907             break;
908         case OSSL_PARAM_UTF8_STRING:
909             {
910                 int i;
911                 const char *word = NULL;
912
913                 for (i = 0; padding_item[i].id != 0; i++) {
914                     if (prsactx->pad_mode == (int)padding_item[i].id) {
915                         word = padding_item[i].ptr;
916                         break;
917                     }
918                 }
919
920                 if (word != NULL) {
921                     if (!OSSL_PARAM_set_utf8_string(p, word))
922                         return 0;
923                 } else {
924                     ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
925                 }
926             }
927             break;
928         default:
929             return 0;
930         }
931
932     p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_DIGEST);
933     if (p != NULL && !OSSL_PARAM_set_utf8_string(p, prsactx->mdname))
934         return 0;
935
936     p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_MGF1_DIGEST);
937     if (p != NULL && !OSSL_PARAM_set_utf8_string(p, prsactx->mgf1_mdname))
938         return 0;
939
940     p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_PSS_SALTLEN);
941     if (p != NULL) {
942         if (p->data_type == OSSL_PARAM_INTEGER) {
943             if (!OSSL_PARAM_set_int(p, prsactx->saltlen))
944                 return 0;
945         } else if (p->data_type == OSSL_PARAM_UTF8_STRING) {
946             const char *value = NULL;
947
948             switch (prsactx->saltlen) {
949             case RSA_PSS_SALTLEN_DIGEST:
950                 value = OSSL_PKEY_RSA_PSS_SALT_LEN_DIGEST;
951                 break;
952             case RSA_PSS_SALTLEN_MAX:
953                 value = OSSL_PKEY_RSA_PSS_SALT_LEN_MAX;
954                 break;
955             case RSA_PSS_SALTLEN_AUTO:
956                 value = OSSL_PKEY_RSA_PSS_SALT_LEN_AUTO;
957                 break;
958             default:
959                 {
960                     int len = BIO_snprintf(p->data, p->data_size, "%d",
961                                            prsactx->saltlen);
962
963                     if (len <= 0)
964                         return 0;
965                     p->return_size = len;
966                     break;
967                 }
968             }
969             if (value != NULL
970                 && !OSSL_PARAM_set_utf8_string(p, value))
971                 return 0;
972         }
973     }
974
975     return 1;
976 }
977
978 static const OSSL_PARAM known_gettable_ctx_params[] = {
979     OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_ALGORITHM_ID, NULL, 0),
980     OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PAD_MODE, NULL, 0),
981     OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),
982     OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_MGF1_DIGEST, NULL, 0),
983     OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PSS_SALTLEN, NULL, 0),
984     OSSL_PARAM_END
985 };
986
987 static const OSSL_PARAM *rsa_gettable_ctx_params(ossl_unused void *provctx)
988 {
989     return known_gettable_ctx_params;
990 }
991
992 static int rsa_set_ctx_params(void *vprsactx, const OSSL_PARAM params[])
993 {
994     PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
995     const OSSL_PARAM *p;
996
997     if (prsactx == NULL || params == NULL)
998         return 0;
999
1000     p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_DIGEST);
1001     /* Not allowed during certain operations */
1002     if (p != NULL && !prsactx->flag_allow_md)
1003         return 0;
1004     if (p != NULL) {
1005         char mdname[OSSL_MAX_NAME_SIZE] = "", *pmdname = mdname;
1006         char mdprops[OSSL_MAX_PROPQUERY_SIZE] = "", *pmdprops = mdprops;
1007         const OSSL_PARAM *propsp =
1008             OSSL_PARAM_locate_const(params,
1009                                     OSSL_SIGNATURE_PARAM_PROPERTIES);
1010
1011         if (!OSSL_PARAM_get_utf8_string(p, &pmdname, sizeof(mdname)))
1012             return 0;
1013
1014         if (propsp == NULL)
1015             pmdprops = NULL;
1016         else if (!OSSL_PARAM_get_utf8_string(propsp,
1017                                              &pmdprops, sizeof(mdprops)))
1018             return 0;
1019
1020         if (rsa_pss_restricted(prsactx)) {
1021             /* TODO(3.0) figure out what to do for prsactx->md == NULL */
1022             if (prsactx->md == NULL || EVP_MD_is_a(prsactx->md, mdname))
1023                 return 1;
1024             ERR_raise(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED);
1025             return 0;
1026         }
1027
1028         /* non-PSS code follows */
1029         if (!rsa_setup_md(prsactx, mdname, pmdprops))
1030             return 0;
1031     }
1032
1033     p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_PAD_MODE);
1034     if (p != NULL) {
1035         int pad_mode = 0;
1036         const char *err_extra_text = NULL;
1037
1038         switch (p->data_type) {
1039         case OSSL_PARAM_INTEGER: /* Support for legacy pad mode number */
1040             if (!OSSL_PARAM_get_int(p, &pad_mode))
1041                 return 0;
1042             break;
1043         case OSSL_PARAM_UTF8_STRING:
1044             {
1045                 int i;
1046
1047                 if (p->data == NULL)
1048                     return 0;
1049
1050                 for (i = 0; padding_item[i].id != 0; i++) {
1051                     if (strcmp(p->data, padding_item[i].ptr) == 0) {
1052                         pad_mode = padding_item[i].id;
1053                         break;
1054                     }
1055                 }
1056             }
1057             break;
1058         default:
1059             return 0;
1060         }
1061
1062         switch (pad_mode) {
1063         case RSA_PKCS1_OAEP_PADDING:
1064             /*
1065              * OAEP padding is for asymmetric cipher only so is not compatible
1066              * with signature use.
1067              */
1068             err_extra_text = "OAEP padding not allowed for signing / verifying";
1069             goto bad_pad;
1070         case RSA_PKCS1_PSS_PADDING:
1071             if ((prsactx->operation
1072                  & (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY)) == 0) {
1073                 err_extra_text =
1074                     "PSS padding only allowed for sign and verify operations";
1075                 goto bad_pad;
1076             }
1077             if (prsactx->md == NULL
1078                 && !rsa_setup_md(prsactx, OSSL_DIGEST_NAME_SHA1, NULL)) {
1079                 return 0;
1080             }
1081             break;
1082         case RSA_PKCS1_PADDING:
1083             err_extra_text = "PKCS#1 padding not allowed with RSA-PSS";
1084             goto cont;
1085         case RSA_SSLV23_PADDING:
1086             err_extra_text = "SSLv3 padding not allowed with RSA-PSS";
1087             goto cont;
1088         case RSA_NO_PADDING:
1089             err_extra_text = "No padding not allowed with RSA-PSS";
1090             goto cont;
1091         case RSA_X931_PADDING:
1092             err_extra_text = "X.931 padding not allowed with RSA-PSS";
1093         cont:
1094             if (RSA_test_flags(prsactx->rsa,
1095                                RSA_FLAG_TYPE_MASK) == RSA_FLAG_TYPE_RSA)
1096                 break;
1097             /* FALLTHRU */
1098         default:
1099         bad_pad:
1100             if (err_extra_text == NULL)
1101                 ERR_raise(ERR_LIB_PROV,
1102                           PROV_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
1103             else
1104                 ERR_raise_data(ERR_LIB_PROV,
1105                                PROV_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE,
1106                                err_extra_text);
1107             return 0;
1108         }
1109         if (!rsa_check_padding(prsactx->mdnid, pad_mode))
1110             return 0;
1111         prsactx->pad_mode = pad_mode;
1112     }
1113
1114     p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_PSS_SALTLEN);
1115     if (p != NULL) {
1116         int saltlen;
1117
1118         if (prsactx->pad_mode != RSA_PKCS1_PSS_PADDING) {
1119             ERR_raise_data(ERR_LIB_PROV, PROV_R_NOT_SUPPORTED,
1120                            "PSS saltlen can only be specified if "
1121                            "PSS padding has been specified first");
1122             return 0;
1123         }
1124
1125         switch (p->data_type) {
1126         case OSSL_PARAM_INTEGER: /* Support for legacy pad mode number */
1127             if (!OSSL_PARAM_get_int(p, &saltlen))
1128                 return 0;
1129             break;
1130         case OSSL_PARAM_UTF8_STRING:
1131             if (strcmp(p->data, OSSL_PKEY_RSA_PSS_SALT_LEN_DIGEST) == 0)
1132                 saltlen = RSA_PSS_SALTLEN_DIGEST;
1133             else if (strcmp(p->data, OSSL_PKEY_RSA_PSS_SALT_LEN_MAX) == 0)
1134                 saltlen = RSA_PSS_SALTLEN_MAX;
1135             else if (strcmp(p->data, OSSL_PKEY_RSA_PSS_SALT_LEN_AUTO) == 0)
1136                 saltlen = RSA_PSS_SALTLEN_AUTO;
1137             else
1138                 saltlen = atoi(p->data);
1139             break;
1140         default:
1141             return 0;
1142         }
1143
1144         /*
1145          * RSA_PSS_SALTLEN_MAX seems curiously named in this check.
1146          * Contrary to what it's name suggests, it's the currently
1147          * lowest saltlen number possible.
1148          */
1149         if (saltlen < RSA_PSS_SALTLEN_MAX) {
1150             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_PSS_SALTLEN);
1151             return 0;
1152         }
1153
1154         if (rsa_pss_restricted(prsactx)) {
1155             switch (saltlen) {
1156             case RSA_PSS_SALTLEN_AUTO:
1157                 if (prsactx->operation == EVP_PKEY_OP_VERIFY) {
1158                     ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_PSS_SALTLEN);
1159                     return 0;
1160                 }
1161                 break;
1162             case RSA_PSS_SALTLEN_DIGEST:
1163                 if (prsactx->min_saltlen > EVP_MD_size(prsactx->md)) {
1164                     ERR_raise_data(ERR_LIB_PROV,
1165                                    PROV_R_PSS_SALTLEN_TOO_SMALL,
1166                                    "Should be more than %d, but would be "
1167                                    "set to match digest size (%d)",
1168                                    prsactx->min_saltlen,
1169                                    EVP_MD_size(prsactx->md));
1170                     return 0;
1171                 }
1172                 break;
1173             default:
1174                 if (saltlen >= 0 && saltlen < prsactx->min_saltlen) {
1175                     ERR_raise_data(ERR_LIB_PROV,
1176                                    PROV_R_PSS_SALTLEN_TOO_SMALL,
1177                                    "Should be more than %d, "
1178                                    "but would be set to %d",
1179                                    prsactx->min_saltlen, saltlen);
1180                     return 0;
1181                 }
1182             }
1183         }
1184
1185         prsactx->saltlen = saltlen;
1186     }
1187
1188     p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_MGF1_DIGEST);
1189     if (p != NULL) {
1190         char mdname[OSSL_MAX_NAME_SIZE] = "", *pmdname = mdname;
1191         char mdprops[OSSL_MAX_PROPQUERY_SIZE] = "", *pmdprops = mdprops;
1192         const OSSL_PARAM *propsp =
1193             OSSL_PARAM_locate_const(params,
1194                                     OSSL_SIGNATURE_PARAM_MGF1_PROPERTIES);
1195
1196         if (!OSSL_PARAM_get_utf8_string(p, &pmdname, sizeof(mdname)))
1197             return 0;
1198
1199         if (propsp == NULL)
1200             pmdprops = NULL;
1201         else if (!OSSL_PARAM_get_utf8_string(propsp,
1202                                              &pmdprops, sizeof(mdprops)))
1203             return 0;
1204
1205         if (prsactx->pad_mode != RSA_PKCS1_PSS_PADDING) {
1206             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MGF1_MD);
1207             return  0;
1208         }
1209
1210         if (rsa_pss_restricted(prsactx)) {
1211             /* TODO(3.0) figure out what to do for prsactx->mgf1_md == NULL */
1212             if (prsactx->mgf1_md == NULL
1213                 || EVP_MD_is_a(prsactx->mgf1_md, mdname))
1214                 return 1;
1215             ERR_raise(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED);
1216             return 0;
1217         }
1218
1219         /* non-PSS code follows */
1220         if (!rsa_setup_mgf1_md(prsactx, mdname, pmdprops))
1221             return 0;
1222     }
1223
1224     return 1;
1225 }
1226
1227 static const OSSL_PARAM known_settable_ctx_params[] = {
1228     OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PAD_MODE, NULL, 0),
1229     OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),
1230     OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PROPERTIES, NULL, 0),
1231     OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_MGF1_DIGEST, NULL, 0),
1232     OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_MGF1_PROPERTIES, NULL, 0),
1233     OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PSS_SALTLEN, NULL, 0),
1234     OSSL_PARAM_END
1235 };
1236
1237 static const OSSL_PARAM *rsa_settable_ctx_params(ossl_unused void *provctx)
1238 {
1239     /*
1240      * TODO(3.0): Should this function return a different set of settable ctx
1241      * params if the ctx is being used for a DigestSign/DigestVerify? In that
1242      * case it is not allowed to set the digest size/digest name because the
1243      * digest is explicitly set as part of the init.
1244      */
1245     return known_settable_ctx_params;
1246 }
1247
1248 static int rsa_get_ctx_md_params(void *vprsactx, OSSL_PARAM *params)
1249 {
1250     PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1251
1252     if (prsactx->mdctx == NULL)
1253         return 0;
1254
1255     return EVP_MD_CTX_get_params(prsactx->mdctx, params);
1256 }
1257
1258 static const OSSL_PARAM *rsa_gettable_ctx_md_params(void *vprsactx)
1259 {
1260     PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1261
1262     if (prsactx->md == NULL)
1263         return 0;
1264
1265     return EVP_MD_gettable_ctx_params(prsactx->md);
1266 }
1267
1268 static int rsa_set_ctx_md_params(void *vprsactx, const OSSL_PARAM params[])
1269 {
1270     PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1271
1272     if (prsactx->mdctx == NULL)
1273         return 0;
1274
1275     return EVP_MD_CTX_set_params(prsactx->mdctx, params);
1276 }
1277
1278 static const OSSL_PARAM *rsa_settable_ctx_md_params(void *vprsactx)
1279 {
1280     PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1281
1282     if (prsactx->md == NULL)
1283         return 0;
1284
1285     return EVP_MD_settable_ctx_params(prsactx->md);
1286 }
1287
1288 const OSSL_DISPATCH rsa_signature_functions[] = {
1289     { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))rsa_newctx },
1290     { OSSL_FUNC_SIGNATURE_SIGN_INIT, (void (*)(void))rsa_sign_init },
1291     { OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))rsa_sign },
1292     { OSSL_FUNC_SIGNATURE_VERIFY_INIT, (void (*)(void))rsa_verify_init },
1293     { OSSL_FUNC_SIGNATURE_VERIFY, (void (*)(void))rsa_verify },
1294     { OSSL_FUNC_SIGNATURE_VERIFY_RECOVER_INIT,
1295       (void (*)(void))rsa_verify_recover_init },
1296     { OSSL_FUNC_SIGNATURE_VERIFY_RECOVER,
1297       (void (*)(void))rsa_verify_recover },
1298     { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT,
1299       (void (*)(void))rsa_digest_sign_init },
1300     { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE,
1301       (void (*)(void))rsa_digest_signverify_update },
1302     { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL,
1303       (void (*)(void))rsa_digest_sign_final },
1304     { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT,
1305       (void (*)(void))rsa_digest_verify_init },
1306     { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE,
1307       (void (*)(void))rsa_digest_signverify_update },
1308     { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL,
1309       (void (*)(void))rsa_digest_verify_final },
1310     { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))rsa_freectx },
1311     { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))rsa_dupctx },
1312     { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, (void (*)(void))rsa_get_ctx_params },
1313     { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS,
1314       (void (*)(void))rsa_gettable_ctx_params },
1315     { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, (void (*)(void))rsa_set_ctx_params },
1316     { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS,
1317       (void (*)(void))rsa_settable_ctx_params },
1318     { OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS,
1319       (void (*)(void))rsa_get_ctx_md_params },
1320     { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS,
1321       (void (*)(void))rsa_gettable_ctx_md_params },
1322     { OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS,
1323       (void (*)(void))rsa_set_ctx_md_params },
1324     { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS,
1325       (void (*)(void))rsa_settable_ctx_md_params },
1326     { 0, NULL }
1327 };