Coverage Report

Created: 2025-08-25 06:30

/src/openssl/providers/implementations/signature/rsa_sig.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2019-2024 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
/*
12
 * RSA low level APIs are deprecated for public use, but still ok for
13
 * internal use.
14
 */
15
#include "internal/deprecated.h"
16
17
#include <string.h>
18
#include <openssl/crypto.h>
19
#include <openssl/core_dispatch.h>
20
#include <openssl/core_names.h>
21
#include <openssl/err.h>
22
#include <openssl/obj_mac.h>
23
#include <openssl/rsa.h>
24
#include <openssl/params.h>
25
#include <openssl/evp.h>
26
#include <openssl/proverr.h>
27
#include "internal/cryptlib.h"
28
#include "internal/nelem.h"
29
#include "internal/sizes.h"
30
#include "crypto/rsa.h"
31
#include "prov/providercommon.h"
32
#include "prov/implementations.h"
33
#include "prov/provider_ctx.h"
34
#include "prov/der_rsa.h"
35
#include "prov/securitycheck.h"
36
37
0
#define RSA_DEFAULT_DIGEST_NAME OSSL_DIGEST_NAME_SHA1
38
39
static OSSL_FUNC_signature_newctx_fn rsa_newctx;
40
static OSSL_FUNC_signature_sign_init_fn rsa_sign_init;
41
static OSSL_FUNC_signature_verify_init_fn rsa_verify_init;
42
static OSSL_FUNC_signature_verify_recover_init_fn rsa_verify_recover_init;
43
static OSSL_FUNC_signature_sign_fn rsa_sign;
44
static OSSL_FUNC_signature_sign_message_update_fn rsa_signverify_message_update;
45
static OSSL_FUNC_signature_sign_message_final_fn rsa_sign_message_final;
46
static OSSL_FUNC_signature_verify_fn rsa_verify;
47
static OSSL_FUNC_signature_verify_recover_fn rsa_verify_recover;
48
static OSSL_FUNC_signature_verify_message_update_fn rsa_signverify_message_update;
49
static OSSL_FUNC_signature_verify_message_final_fn rsa_verify_message_final;
50
static OSSL_FUNC_signature_digest_sign_init_fn rsa_digest_sign_init;
51
static OSSL_FUNC_signature_digest_sign_update_fn rsa_digest_sign_update;
52
static OSSL_FUNC_signature_digest_sign_final_fn rsa_digest_sign_final;
53
static OSSL_FUNC_signature_digest_verify_init_fn rsa_digest_verify_init;
54
static OSSL_FUNC_signature_digest_verify_update_fn rsa_digest_verify_update;
55
static OSSL_FUNC_signature_digest_verify_final_fn rsa_digest_verify_final;
56
static OSSL_FUNC_signature_freectx_fn rsa_freectx;
57
static OSSL_FUNC_signature_dupctx_fn rsa_dupctx;
58
static OSSL_FUNC_signature_query_key_types_fn rsa_sigalg_query_key_types;
59
static OSSL_FUNC_signature_get_ctx_params_fn rsa_get_ctx_params;
60
static OSSL_FUNC_signature_gettable_ctx_params_fn rsa_gettable_ctx_params;
61
static OSSL_FUNC_signature_set_ctx_params_fn rsa_set_ctx_params;
62
static OSSL_FUNC_signature_settable_ctx_params_fn rsa_settable_ctx_params;
63
static OSSL_FUNC_signature_get_ctx_md_params_fn rsa_get_ctx_md_params;
64
static OSSL_FUNC_signature_gettable_ctx_md_params_fn rsa_gettable_ctx_md_params;
65
static OSSL_FUNC_signature_set_ctx_md_params_fn rsa_set_ctx_md_params;
66
static OSSL_FUNC_signature_settable_ctx_md_params_fn rsa_settable_ctx_md_params;
67
static OSSL_FUNC_signature_set_ctx_params_fn rsa_sigalg_set_ctx_params;
68
static OSSL_FUNC_signature_settable_ctx_params_fn rsa_sigalg_settable_ctx_params;
69
70
static OSSL_ITEM padding_item[] = {
71
    { RSA_PKCS1_PADDING,        OSSL_PKEY_RSA_PAD_MODE_PKCSV15 },
72
    { RSA_NO_PADDING,           OSSL_PKEY_RSA_PAD_MODE_NONE },
73
    { RSA_X931_PADDING,         OSSL_PKEY_RSA_PAD_MODE_X931 },
74
    { RSA_PKCS1_PSS_PADDING,    OSSL_PKEY_RSA_PAD_MODE_PSS },
75
    { 0,                        NULL     }
76
};
77
78
/*
79
 * What's passed as an actual key is defined by the KEYMGMT interface.
80
 * We happen to know that our KEYMGMT simply passes RSA structures, so
81
 * we use that here too.
82
 */
83
84
typedef struct {
85
    OSSL_LIB_CTX *libctx;
86
    char *propq;
87
    RSA *rsa;
88
    int operation;
89
90
    /*
91
     * Flag to determine if a full sigalg is run (1) or if a composable
92
     * signature algorithm is run (0).
93
     *
94
     * When a full sigalg is run (1), this currently affects the following
95
     * other flags, which are to remain untouched after their initialization:
96
     *
97
     * - flag_allow_md (initialized to 0)
98
     */
99
    unsigned int flag_sigalg : 1;
100
    /*
101
     * Flag to determine if the hash function can be changed (1) or not (0)
102
     * Because it's dangerous to change during a DigestSign or DigestVerify
103
     * operation, this flag is cleared by their Init function, and set again
104
     * by their Final function.
105
     * Implementations of full sigalgs (such as RSA-SHA256) hard-code this
106
     * flag to not allow changes (0).
107
     */
108
    unsigned int flag_allow_md : 1;
109
    unsigned int mgf1_md_set : 1;
110
    /*
111
     * Flags to say what are the possible next external calls in what
112
     * consitutes the life cycle of an algorithm.  The relevant calls are:
113
     * - init
114
     * - update
115
     * - final
116
     * - oneshot
117
     * All other external calls are regarded as utilitarian and are allowed
118
     * at any time (they may be affected by other flags, like flag_allow_md,
119
     * though).
120
     */
121
    unsigned int flag_allow_update : 1;
122
    unsigned int flag_allow_final : 1;
123
    unsigned int flag_allow_oneshot : 1;
124
125
    /* main digest */
126
    EVP_MD *md;
127
    EVP_MD_CTX *mdctx;
128
    int mdnid;
129
    char mdname[OSSL_MAX_NAME_SIZE]; /* Purely informational */
130
131
    /* RSA padding mode */
132
    int pad_mode;
133
    /* message digest for MGF1 */
134
    EVP_MD *mgf1_md;
135
    int mgf1_mdnid;
136
    char mgf1_mdname[OSSL_MAX_NAME_SIZE]; /* Purely informational */
137
    /* PSS salt length */
138
    int saltlen;
139
    /* Minimum salt length or -1 if no PSS parameter restriction */
140
    int min_saltlen;
141
142
    /* Signature, for verification */
143
    unsigned char *sig;
144
    size_t siglen;
145
146
#ifdef FIPS_MODULE
147
    /*
148
     * FIPS 140-3 IG 2.4.B mandates that verification based on a digest of a
149
     * message is not permitted.  However, signing based on a digest is still
150
     * permitted.
151
     */
152
    int verify_message;
153
#endif
154
155
    /* Temp buffer */
156
    unsigned char *tbuf;
157
158
    OSSL_FIPS_IND_DECLARE
159
} PROV_RSA_CTX;
160
161
/* True if PSS parameters are restricted */
162
0
#define rsa_pss_restricted(prsactx) (prsactx->min_saltlen != -1)
163
164
static int rsa_get_md_size(const PROV_RSA_CTX *prsactx)
165
0
{
166
0
    int md_size;
167
168
0
    if (prsactx->md != NULL) {
169
0
        md_size = EVP_MD_get_size(prsactx->md);
170
0
        if (md_size <= 0)
171
0
            return 0;
172
0
        return md_size;
173
0
    }
174
0
    return 0;
175
0
}
176
177
static int rsa_check_padding(const PROV_RSA_CTX *prsactx,
178
                             const char *mdname, const char *mgf1_mdname,
179
                             int mdnid)
180
0
{
181
0
    switch (prsactx->pad_mode) {
182
0
    case RSA_NO_PADDING:
183
0
        if (mdname != NULL || mdnid != NID_undef) {
184
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_PADDING_MODE);
185
0
            return 0;
186
0
        }
187
0
        break;
188
0
    case RSA_X931_PADDING:
189
0
        if (RSA_X931_hash_id(mdnid) == -1) {
190
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_X931_DIGEST);
191
0
            return 0;
192
0
        }
193
0
        break;
194
0
    case RSA_PKCS1_PSS_PADDING:
195
0
        if (rsa_pss_restricted(prsactx))
196
0
            if ((mdname != NULL && !EVP_MD_is_a(prsactx->md, mdname))
197
0
                || (mgf1_mdname != NULL
198
0
                    && !EVP_MD_is_a(prsactx->mgf1_md, mgf1_mdname))) {
199
0
                ERR_raise(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED);
200
0
                return 0;
201
0
            }
202
0
        break;
203
0
    default:
204
0
        break;
205
0
    }
206
207
0
    return 1;
208
0
}
209
210
static int rsa_check_parameters(PROV_RSA_CTX *prsactx, int min_saltlen)
211
0
{
212
0
    if (prsactx->pad_mode == RSA_PKCS1_PSS_PADDING) {
213
0
        int max_saltlen;
214
215
        /* See if minimum salt length exceeds maximum possible */
216
0
        max_saltlen = RSA_size(prsactx->rsa) - EVP_MD_get_size(prsactx->md);
217
0
        if ((RSA_bits(prsactx->rsa) & 0x7) == 1)
218
0
            max_saltlen--;
219
0
        if (min_saltlen < 0 || min_saltlen > max_saltlen) {
220
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH);
221
0
            return 0;
222
0
        }
223
0
        prsactx->min_saltlen = min_saltlen;
224
0
    }
225
0
    return 1;
226
0
}
227
228
static void *rsa_newctx(void *provctx, const char *propq)
229
0
{
230
0
    PROV_RSA_CTX *prsactx = NULL;
231
0
    char *propq_copy = NULL;
232
233
0
    if (!ossl_prov_is_running())
234
0
        return NULL;
235
236
0
    if ((prsactx = OPENSSL_zalloc(sizeof(PROV_RSA_CTX))) == NULL
237
0
        || (propq != NULL
238
0
            && (propq_copy = OPENSSL_strdup(propq)) == NULL)) {
239
0
        OPENSSL_free(prsactx);
240
0
        return NULL;
241
0
    }
242
243
0
    OSSL_FIPS_IND_INIT(prsactx)
244
0
    prsactx->libctx = PROV_LIBCTX_OF(provctx);
245
0
    prsactx->flag_allow_md = 1;
246
#ifdef FIPS_MODULE
247
    prsactx->verify_message = 1;
248
#endif
249
0
    prsactx->propq = propq_copy;
250
    /* Maximum up to digest length for sign, auto for verify */
251
0
    prsactx->saltlen = RSA_PSS_SALTLEN_AUTO_DIGEST_MAX;
252
0
    prsactx->min_saltlen = -1;
253
0
    return prsactx;
254
0
}
255
256
static int rsa_pss_compute_saltlen(PROV_RSA_CTX *ctx)
257
0
{
258
0
    int saltlen = ctx->saltlen;
259
0
    int saltlenMax = -1;
260
261
    /* FIPS 186-4 section 5 "The RSA Digital Signature Algorithm", subsection
262
     * 5.5 "PKCS #1" says: "For RSASSA-PSS […] the length (in bytes) of the
263
     * salt (sLen) shall satisfy 0 <= sLen <= hLen, where hLen is the length of
264
     * the hash function output block (in bytes)."
265
     *
266
     * Provide a way to use at most the digest length, so that the default does
267
     * not violate FIPS 186-4. */
268
0
    if (saltlen == RSA_PSS_SALTLEN_DIGEST) {
269
0
        if ((saltlen = EVP_MD_get_size(ctx->md)) <= 0) {
270
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST);
271
0
            return -1;
272
0
        }
273
0
    } else if (saltlen == RSA_PSS_SALTLEN_AUTO_DIGEST_MAX) {
274
0
        saltlen = RSA_PSS_SALTLEN_MAX;
275
0
        if ((saltlenMax = EVP_MD_get_size(ctx->md)) <= 0) {
276
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST);
277
0
            return -1;
278
0
        }
279
0
    }
280
0
    if (saltlen == RSA_PSS_SALTLEN_MAX || saltlen == RSA_PSS_SALTLEN_AUTO) {
281
0
        int mdsize, rsasize;
282
283
0
        if ((mdsize = EVP_MD_get_size(ctx->md)) <= 0) {
284
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST);
285
0
            return -1;
286
0
        }
287
0
        if ((rsasize = RSA_size(ctx->rsa)) <= 2 || rsasize - 2 < mdsize) {
288
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
289
0
            return -1;
290
0
        }
291
0
        saltlen = rsasize - mdsize - 2;
292
0
        if ((RSA_bits(ctx->rsa) & 0x7) == 1)
293
0
            saltlen--;
294
0
        if (saltlenMax >= 0 && saltlen > saltlenMax)
295
0
            saltlen = saltlenMax;
296
0
    }
297
0
    if (saltlen < 0) {
298
0
        ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
299
0
        return -1;
300
0
    } else if (saltlen < ctx->min_saltlen) {
301
0
        ERR_raise_data(ERR_LIB_PROV, PROV_R_PSS_SALTLEN_TOO_SMALL,
302
0
                       "minimum salt length: %d, actual salt length: %d",
303
0
                       ctx->min_saltlen, saltlen);
304
0
        return -1;
305
0
    }
306
0
    return saltlen;
307
0
}
308
309
static unsigned char *rsa_generate_signature_aid(PROV_RSA_CTX *ctx,
310
                                                 unsigned char *aid_buf,
311
                                                 size_t buf_len,
312
                                                 size_t *aid_len)
313
0
{
314
0
    WPACKET pkt;
315
0
    unsigned char *aid = NULL;
316
0
    int saltlen;
317
0
    RSA_PSS_PARAMS_30 pss_params;
318
0
    int ret;
319
320
0
    if (!WPACKET_init_der(&pkt, aid_buf, buf_len)) {
321
0
        ERR_raise(ERR_LIB_PROV, ERR_R_CRYPTO_LIB);
322
0
        return NULL;
323
0
    }
324
325
0
    switch (ctx->pad_mode) {
326
0
    case RSA_PKCS1_PADDING:
327
0
        ret = ossl_DER_w_algorithmIdentifier_MDWithRSAEncryption(&pkt, -1,
328
0
                                                                 ctx->mdnid);
329
330
0
        if (ret > 0) {
331
0
            break;
332
0
        } else if (ret == 0) {
333
0
            ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
334
0
            goto cleanup;
335
0
        }
336
0
        ERR_raise_data(ERR_LIB_PROV, ERR_R_UNSUPPORTED,
337
0
                       "Algorithm ID generation - md NID: %d",
338
0
                       ctx->mdnid);
339
0
        goto cleanup;
340
0
    case RSA_PKCS1_PSS_PADDING:
341
0
        saltlen = rsa_pss_compute_saltlen(ctx);
342
0
        if (saltlen < 0)
343
0
            goto cleanup;
344
0
        if (!ossl_rsa_pss_params_30_set_defaults(&pss_params)
345
0
            || !ossl_rsa_pss_params_30_set_hashalg(&pss_params, ctx->mdnid)
346
0
            || !ossl_rsa_pss_params_30_set_maskgenhashalg(&pss_params,
347
0
                                                          ctx->mgf1_mdnid)
348
0
            || !ossl_rsa_pss_params_30_set_saltlen(&pss_params, saltlen)
349
0
            || !ossl_DER_w_algorithmIdentifier_RSA_PSS(&pkt, -1,
350
0
                                                       RSA_FLAG_TYPE_RSASSAPSS,
351
0
                                                       &pss_params)) {
352
0
            ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
353
0
            goto cleanup;
354
0
        }
355
0
        break;
356
0
    default:
357
0
        ERR_raise_data(ERR_LIB_PROV, ERR_R_UNSUPPORTED,
358
0
                       "Algorithm ID generation - pad mode: %d",
359
0
                       ctx->pad_mode);
360
0
        goto cleanup;
361
0
    }
362
0
    if (WPACKET_finish(&pkt)) {
363
0
        WPACKET_get_total_written(&pkt, aid_len);
364
0
        aid = WPACKET_get_curr(&pkt);
365
0
    }
366
0
 cleanup:
367
0
    WPACKET_cleanup(&pkt);
368
0
    return aid;
369
0
}
370
371
static int rsa_setup_md(PROV_RSA_CTX *ctx, const char *mdname,
372
                        const char *mdprops, const char *desc)
373
0
{
374
0
    EVP_MD *md = NULL;
375
376
0
    if (mdprops == NULL)
377
0
        mdprops = ctx->propq;
378
379
0
    if (mdname != NULL) {
380
0
        int md_nid;
381
0
        size_t mdname_len = strlen(mdname);
382
383
0
        md = EVP_MD_fetch(ctx->libctx, mdname, mdprops);
384
385
0
        if (md == NULL) {
386
0
            ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
387
0
                           "%s could not be fetched", mdname);
388
0
            goto err;
389
0
        }
390
0
        md_nid = ossl_digest_rsa_sign_get_md_nid(md);
391
0
        if (md_nid == NID_undef) {
392
0
            ERR_raise_data(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED,
393
0
                           "digest=%s", mdname);
394
0
            goto err;
395
0
        }
396
        /*
397
         * XOF digests are not allowed except for RSA PSS.
398
         * We don't support XOF digests with RSA PSS (yet), so just fail.
399
         * When we do support them, uncomment the second clause.
400
         */
401
0
        if (EVP_MD_xof(md)
402
0
                /* && ctx->pad_mode != RSA_PKCS1_PSS_PADDING */) {
403
0
            ERR_raise(ERR_LIB_PROV, PROV_R_XOF_DIGESTS_NOT_ALLOWED);
404
0
            goto err;
405
0
        }
406
#ifdef FIPS_MODULE
407
        {
408
            int sha1_allowed
409
                = ((ctx->operation
410
                    & (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_SIGNMSG)) == 0);
411
412
            if (!ossl_fips_ind_digest_sign_check(OSSL_FIPS_IND_GET(ctx),
413
                                                 OSSL_FIPS_IND_SETTABLE1,
414
                                                 ctx->libctx,
415
                                                 md_nid, sha1_allowed, desc,
416
                                                 ossl_fips_config_signature_digest_check))
417
                goto err;
418
        }
419
#endif
420
421
0
        if (!rsa_check_padding(ctx, mdname, NULL, md_nid))
422
0
            goto err;
423
0
        if (mdname_len >= sizeof(ctx->mdname)) {
424
0
            ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
425
0
                           "%s exceeds name buffer length", mdname);
426
0
            goto err;
427
0
        }
428
429
0
        if (!ctx->flag_allow_md) {
430
0
            if (ctx->mdname[0] != '\0' && !EVP_MD_is_a(md, ctx->mdname)) {
431
0
                ERR_raise_data(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED,
432
0
                               "digest %s != %s", mdname, ctx->mdname);
433
0
                goto err;
434
0
            }
435
0
            EVP_MD_free(md);
436
0
            return 1;
437
0
        }
438
439
0
        if (!ctx->mgf1_md_set) {
440
0
            if (!EVP_MD_up_ref(md)) {
441
0
                goto err;
442
0
            }
443
0
            EVP_MD_free(ctx->mgf1_md);
444
0
            ctx->mgf1_md = md;
445
0
            ctx->mgf1_mdnid = md_nid;
446
0
            OPENSSL_strlcpy(ctx->mgf1_mdname, mdname, sizeof(ctx->mgf1_mdname));
447
0
        }
448
449
0
        EVP_MD_CTX_free(ctx->mdctx);
450
0
        EVP_MD_free(ctx->md);
451
452
0
        ctx->mdctx = NULL;
453
0
        ctx->md = md;
454
0
        ctx->mdnid = md_nid;
455
0
        OPENSSL_strlcpy(ctx->mdname, mdname, sizeof(ctx->mdname));
456
0
    }
457
458
0
    return 1;
459
0
err:
460
0
    EVP_MD_free(md);
461
0
    return 0;
462
0
}
463
464
static int rsa_setup_mgf1_md(PROV_RSA_CTX *ctx, const char *mdname,
465
                             const char *mdprops)
466
0
{
467
0
    size_t len;
468
0
    EVP_MD *md = NULL;
469
0
    int mdnid;
470
471
0
    if (mdprops == NULL)
472
0
        mdprops = ctx->propq;
473
474
0
    if ((md = EVP_MD_fetch(ctx->libctx, mdname, mdprops)) == NULL) {
475
0
        ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
476
0
                       "%s could not be fetched", mdname);
477
0
        return 0;
478
0
    }
479
    /* The default for mgf1 is SHA1 - so allow SHA1 */
480
0
    if ((mdnid = ossl_digest_rsa_sign_get_md_nid(md)) <= 0
481
0
        || !rsa_check_padding(ctx, NULL, mdname, mdnid)) {
482
0
        if (mdnid <= 0)
483
0
            ERR_raise_data(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED,
484
0
                           "digest=%s", mdname);
485
0
        EVP_MD_free(md);
486
0
        return 0;
487
0
    }
488
0
    len = OPENSSL_strlcpy(ctx->mgf1_mdname, mdname, sizeof(ctx->mgf1_mdname));
489
0
    if (len >= sizeof(ctx->mgf1_mdname)) {
490
0
        ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
491
0
                       "%s exceeds name buffer length", mdname);
492
0
        EVP_MD_free(md);
493
0
        return 0;
494
0
    }
495
496
0
    EVP_MD_free(ctx->mgf1_md);
497
0
    ctx->mgf1_md = md;
498
0
    ctx->mgf1_mdnid = mdnid;
499
0
    ctx->mgf1_md_set = 1;
500
0
    return 1;
501
0
}
502
503
static int
504
rsa_signverify_init(PROV_RSA_CTX *prsactx, void *vrsa,
505
                    OSSL_FUNC_signature_set_ctx_params_fn *set_ctx_params,
506
                    const OSSL_PARAM params[], int operation,
507
                    const char *desc)
508
0
{
509
0
    int protect;
510
511
0
    if (!ossl_prov_is_running() || prsactx == NULL)
512
0
        return 0;
513
514
0
    if (vrsa == NULL && prsactx->rsa == NULL) {
515
0
        ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
516
0
        return 0;
517
0
    }
518
519
0
    if (vrsa != NULL) {
520
0
        if (!RSA_up_ref(vrsa))
521
0
            return 0;
522
0
        RSA_free(prsactx->rsa);
523
0
        prsactx->rsa = vrsa;
524
0
    }
525
0
    if (!ossl_rsa_key_op_get_protect(prsactx->rsa, operation, &protect))
526
0
        return 0;
527
528
0
    prsactx->operation = operation;
529
0
    prsactx->flag_allow_update = 1;
530
0
    prsactx->flag_allow_final = 1;
531
0
    prsactx->flag_allow_oneshot = 1;
532
533
    /* Maximize up to digest length for sign, auto for verify */
534
0
    prsactx->saltlen = RSA_PSS_SALTLEN_AUTO_DIGEST_MAX;
535
0
    prsactx->min_saltlen = -1;
536
537
0
    switch (RSA_test_flags(prsactx->rsa, RSA_FLAG_TYPE_MASK)) {
538
0
    case RSA_FLAG_TYPE_RSA:
539
0
        prsactx->pad_mode = RSA_PKCS1_PADDING;
540
0
        break;
541
0
    case RSA_FLAG_TYPE_RSASSAPSS:
542
0
        prsactx->pad_mode = RSA_PKCS1_PSS_PADDING;
543
544
0
        {
545
0
            const RSA_PSS_PARAMS_30 *pss =
546
0
                ossl_rsa_get0_pss_params_30(prsactx->rsa);
547
548
0
            if (!ossl_rsa_pss_params_30_is_unrestricted(pss)) {
549
0
                int md_nid = ossl_rsa_pss_params_30_hashalg(pss);
550
0
                int mgf1md_nid = ossl_rsa_pss_params_30_maskgenhashalg(pss);
551
0
                int min_saltlen = ossl_rsa_pss_params_30_saltlen(pss);
552
0
                const char *mdname, *mgf1mdname;
553
0
                size_t len;
554
555
0
                mdname = ossl_rsa_oaeppss_nid2name(md_nid);
556
0
                mgf1mdname = ossl_rsa_oaeppss_nid2name(mgf1md_nid);
557
558
0
                if (mdname == NULL) {
559
0
                    ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
560
0
                                   "PSS restrictions lack hash algorithm");
561
0
                    return 0;
562
0
                }
563
0
                if (mgf1mdname == NULL) {
564
0
                    ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
565
0
                                   "PSS restrictions lack MGF1 hash algorithm");
566
0
                    return 0;
567
0
                }
568
569
0
                len = OPENSSL_strlcpy(prsactx->mdname, mdname,
570
0
                                      sizeof(prsactx->mdname));
571
0
                if (len >= sizeof(prsactx->mdname)) {
572
0
                    ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
573
0
                                   "hash algorithm name too long");
574
0
                    return 0;
575
0
                }
576
0
                len = OPENSSL_strlcpy(prsactx->mgf1_mdname, mgf1mdname,
577
0
                                      sizeof(prsactx->mgf1_mdname));
578
0
                if (len >= sizeof(prsactx->mgf1_mdname)) {
579
0
                    ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
580
0
                                   "MGF1 hash algorithm name too long");
581
0
                    return 0;
582
0
                }
583
0
                prsactx->saltlen = min_saltlen;
584
585
                /* call rsa_setup_mgf1_md before rsa_setup_md to avoid duplication */
586
0
                if (!rsa_setup_mgf1_md(prsactx, mgf1mdname, prsactx->propq)
587
0
                    || !rsa_setup_md(prsactx, mdname, prsactx->propq, desc)
588
0
                    || !rsa_check_parameters(prsactx, min_saltlen))
589
0
                    return 0;
590
0
            }
591
0
        }
592
593
0
        break;
594
0
    default:
595
0
        ERR_raise(ERR_LIB_RSA, PROV_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
596
0
        return 0;
597
0
    }
598
599
0
    OSSL_FIPS_IND_SET_APPROVED(prsactx)
600
0
    if (!set_ctx_params(prsactx, params))
601
0
        return 0;
602
#ifdef FIPS_MODULE
603
    if (!ossl_fips_ind_rsa_key_check(OSSL_FIPS_IND_GET(prsactx),
604
                                     OSSL_FIPS_IND_SETTABLE0, prsactx->libctx,
605
                                     prsactx->rsa, desc, protect))
606
        return 0;
607
#endif
608
0
    return 1;
609
0
}
610
611
static int setup_tbuf(PROV_RSA_CTX *ctx)
612
0
{
613
0
    if (ctx->tbuf != NULL)
614
0
        return 1;
615
0
    if ((ctx->tbuf = OPENSSL_malloc(RSA_size(ctx->rsa))) == NULL)
616
0
        return 0;
617
0
    return 1;
618
0
}
619
620
static void clean_tbuf(PROV_RSA_CTX *ctx)
621
0
{
622
0
    if (ctx->tbuf != NULL)
623
0
        OPENSSL_cleanse(ctx->tbuf, RSA_size(ctx->rsa));
624
0
}
625
626
static void free_tbuf(PROV_RSA_CTX *ctx)
627
0
{
628
0
    clean_tbuf(ctx);
629
0
    OPENSSL_free(ctx->tbuf);
630
0
    ctx->tbuf = NULL;
631
0
}
632
633
#ifdef FIPS_MODULE
634
static int rsa_pss_saltlen_check_passed(PROV_RSA_CTX *ctx, const char *algoname, int saltlen)
635
{
636
    int mdsize = rsa_get_md_size(ctx);
637
    /*
638
     * Perform the check if the salt length is compliant to FIPS 186-5.
639
     *
640
     * According to FIPS 186-5 5.4 (g), the salt length shall be between zero
641
     * and the output block length of the digest function (inclusive).
642
     */
643
    int approved = (saltlen >= 0 && saltlen <= mdsize);
644
645
    if (!approved) {
646
        if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE3,
647
                                         ctx->libctx,
648
                                         algoname, "PSS Salt Length",
649
                                         ossl_fips_config_rsa_pss_saltlen_check)) {
650
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH);
651
            return 0;
652
        }
653
    }
654
655
    return 1;
656
}
657
#endif
658
659
static int rsa_sign_init(void *vprsactx, void *vrsa, const OSSL_PARAM params[])
660
0
{
661
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
662
663
#ifdef FIPS_MODULE
664
    if (prsactx != NULL)
665
        prsactx->verify_message = 1;
666
#endif
667
668
0
    return rsa_signverify_init(prsactx, vrsa, rsa_set_ctx_params, params,
669
0
                               EVP_PKEY_OP_SIGN, "RSA Sign Init");
670
0
}
671
672
/*
673
 * Sign tbs without digesting it first.  This is suitable for "primitive"
674
 * signing and signing the digest of a message, i.e. should be used with
675
 * implementations of the keytype related algorithms.
676
 */
677
static int rsa_sign_directly(PROV_RSA_CTX *prsactx,
678
                             unsigned char *sig, size_t *siglen, size_t sigsize,
679
                             const unsigned char *tbs, size_t tbslen)
680
0
{
681
0
    int ret;
682
0
    size_t rsasize = RSA_size(prsactx->rsa);
683
0
    size_t mdsize = rsa_get_md_size(prsactx);
684
685
0
    if (!ossl_prov_is_running())
686
0
        return 0;
687
688
0
    if (sig == NULL) {
689
0
        *siglen = rsasize;
690
0
        return 1;
691
0
    }
692
693
0
    if (sigsize < rsasize) {
694
0
        ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_SIGNATURE_SIZE,
695
0
                       "is %zu, should be at least %zu", sigsize, rsasize);
696
0
        return 0;
697
0
    }
698
699
0
    if (mdsize != 0) {
700
0
        if (tbslen != mdsize) {
701
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_LENGTH);
702
0
            return 0;
703
0
        }
704
705
0
#ifndef FIPS_MODULE
706
0
        if (EVP_MD_is_a(prsactx->md, OSSL_DIGEST_NAME_MDC2)) {
707
0
            unsigned int sltmp;
708
709
0
            if (prsactx->pad_mode != RSA_PKCS1_PADDING) {
710
0
                ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_PADDING_MODE,
711
0
                               "only PKCS#1 padding supported with MDC2");
712
0
                return 0;
713
0
            }
714
0
            ret = RSA_sign_ASN1_OCTET_STRING(0, tbs, (unsigned int)tbslen, sig,
715
0
                                             &sltmp, prsactx->rsa);
716
717
0
            if (ret <= 0) {
718
0
                ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
719
0
                return 0;
720
0
            }
721
0
            ret = sltmp;
722
0
            goto end;
723
0
        }
724
0
#endif
725
0
        switch (prsactx->pad_mode) {
726
0
        case RSA_X931_PADDING:
727
0
            if ((size_t)RSA_size(prsactx->rsa) < tbslen + 1) {
728
0
                ERR_raise_data(ERR_LIB_PROV, PROV_R_KEY_SIZE_TOO_SMALL,
729
0
                               "RSA key size = %d, expected minimum = %d",
730
0
                               RSA_size(prsactx->rsa), tbslen + 1);
731
0
                return 0;
732
0
            }
733
0
            if (!setup_tbuf(prsactx)) {
734
0
                ERR_raise(ERR_LIB_PROV, ERR_R_PROV_LIB);
735
0
                return 0;
736
0
            }
737
0
            memcpy(prsactx->tbuf, tbs, tbslen);
738
0
            prsactx->tbuf[tbslen] = RSA_X931_hash_id(prsactx->mdnid);
739
0
            ret = RSA_private_encrypt((int)(tbslen + 1), prsactx->tbuf,
740
0
                                      sig, prsactx->rsa, RSA_X931_PADDING);
741
0
            clean_tbuf(prsactx);
742
0
            break;
743
0
        case RSA_PKCS1_PADDING:
744
0
            {
745
0
                unsigned int sltmp;
746
747
0
                ret = RSA_sign(prsactx->mdnid, tbs, (unsigned int)tbslen,
748
0
                               sig, &sltmp, prsactx->rsa);
749
0
                if (ret <= 0) {
750
0
                    ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
751
0
                    return 0;
752
0
                }
753
0
                ret = sltmp;
754
0
            }
755
0
            break;
756
757
0
        case RSA_PKCS1_PSS_PADDING:
758
0
            {
759
0
                int saltlen;
760
761
                /* Check PSS restrictions */
762
0
                if (rsa_pss_restricted(prsactx)) {
763
0
                    switch (prsactx->saltlen) {
764
0
                    case RSA_PSS_SALTLEN_DIGEST:
765
0
                        if (prsactx->min_saltlen > EVP_MD_get_size(prsactx->md)) {
766
0
                            ERR_raise_data(ERR_LIB_PROV,
767
0
                                           PROV_R_PSS_SALTLEN_TOO_SMALL,
768
0
                                           "minimum salt length set to %d, "
769
0
                                           "but the digest only gives %d",
770
0
                                           prsactx->min_saltlen,
771
0
                                           EVP_MD_get_size(prsactx->md));
772
0
                            return 0;
773
0
                        }
774
                        /* FALLTHRU */
775
0
                    default:
776
0
                        if (prsactx->saltlen >= 0
777
0
                            && prsactx->saltlen < prsactx->min_saltlen) {
778
0
                            ERR_raise_data(ERR_LIB_PROV,
779
0
                                           PROV_R_PSS_SALTLEN_TOO_SMALL,
780
0
                                           "minimum salt length set to %d, but the"
781
0
                                           "actual salt length is only set to %d",
782
0
                                           prsactx->min_saltlen,
783
0
                                           prsactx->saltlen);
784
0
                            return 0;
785
0
                        }
786
0
                        break;
787
0
                    }
788
0
                }
789
0
                if (!setup_tbuf(prsactx))
790
0
                    return 0;
791
0
                saltlen = prsactx->saltlen;
792
0
                if (!ossl_rsa_padding_add_PKCS1_PSS_mgf1(prsactx->rsa,
793
0
                                                         prsactx->tbuf, tbs,
794
0
                                                         prsactx->md, prsactx->mgf1_md,
795
0
                                                         &saltlen)) {
796
0
                    ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
797
0
                    return 0;
798
0
                }
799
#ifdef FIPS_MODULE
800
                if (!rsa_pss_saltlen_check_passed(prsactx, "RSA Sign", saltlen))
801
                    return 0;
802
#endif
803
0
                ret = RSA_private_encrypt(RSA_size(prsactx->rsa), prsactx->tbuf,
804
0
                                          sig, prsactx->rsa, RSA_NO_PADDING);
805
0
                clean_tbuf(prsactx);
806
0
            }
807
0
            break;
808
809
0
        default:
810
0
            ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_PADDING_MODE,
811
0
                           "Only X.931, PKCS#1 v1.5 or PSS padding allowed");
812
0
            return 0;
813
0
        }
814
0
    } else {
815
0
        ret = RSA_private_encrypt((int)tbslen, tbs, sig, prsactx->rsa,
816
0
                                  prsactx->pad_mode);
817
0
    }
818
819
0
#ifndef FIPS_MODULE
820
0
 end:
821
0
#endif
822
0
    if (ret <= 0) {
823
0
        ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
824
0
        return 0;
825
0
    }
826
827
0
    *siglen = ret;
828
0
    return 1;
829
0
}
830
831
static int rsa_signverify_message_update(void *vprsactx,
832
                                         const unsigned char *data,
833
                                         size_t datalen)
834
0
{
835
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
836
837
0
    if (prsactx == NULL || prsactx->mdctx == NULL)
838
0
        return 0;
839
840
0
    if (!prsactx->flag_allow_update) {
841
0
        ERR_raise(ERR_LIB_PROV, PROV_R_UPDATE_CALL_OUT_OF_ORDER);
842
0
        return 0;
843
0
    }
844
0
    prsactx->flag_allow_oneshot = 0;
845
846
0
    return EVP_DigestUpdate(prsactx->mdctx, data, datalen);
847
0
}
848
849
static int rsa_sign_message_final(void *vprsactx, unsigned char *sig,
850
                                  size_t *siglen, size_t sigsize)
851
0
{
852
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
853
0
    unsigned char digest[EVP_MAX_MD_SIZE];
854
0
    unsigned int dlen = 0;
855
856
0
    if (!ossl_prov_is_running() || prsactx == NULL)
857
0
        return 0;
858
0
    if (prsactx->mdctx == NULL)
859
0
        return 0;
860
0
    if (!prsactx->flag_allow_final) {
861
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FINAL_CALL_OUT_OF_ORDER);
862
0
        return 0;
863
0
    }
864
865
    /*
866
     * If sig is NULL then we're just finding out the sig size. Other fields
867
     * are ignored. Defer to rsa_sign.
868
     */
869
0
    if (sig != NULL) {
870
        /*
871
         * The digests used here are all known (see rsa_get_md_nid()), so they
872
         * should not exceed the internal buffer size of EVP_MAX_MD_SIZE.
873
         */
874
0
        if (!EVP_DigestFinal_ex(prsactx->mdctx, digest, &dlen))
875
0
            return 0;
876
877
0
        prsactx->flag_allow_update = 0;
878
0
        prsactx->flag_allow_oneshot = 0;
879
0
        prsactx->flag_allow_final = 0;
880
0
    }
881
882
0
    return rsa_sign_directly(prsactx, sig, siglen, sigsize, digest, dlen);
883
0
}
884
885
/*
886
 * If signing a message, digest tbs and sign the result.
887
 * Otherwise, sign tbs directly.
888
 */
889
static int rsa_sign(void *vprsactx, unsigned char *sig, size_t *siglen,
890
                    size_t sigsize, const unsigned char *tbs, size_t tbslen)
891
0
{
892
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
893
894
0
    if (!ossl_prov_is_running() || prsactx == NULL)
895
0
        return 0;
896
0
    if (!prsactx->flag_allow_oneshot) {
897
0
        ERR_raise(ERR_LIB_PROV, PROV_R_ONESHOT_CALL_OUT_OF_ORDER);
898
0
        return 0;
899
0
    }
900
901
0
    if (prsactx->operation == EVP_PKEY_OP_SIGNMSG) {
902
        /*
903
         * If |sig| is NULL, the caller is only looking for the sig length.
904
         * DO NOT update the input in this case.
905
         */
906
0
        if (sig == NULL)
907
0
            return rsa_sign_message_final(prsactx, sig, siglen, sigsize);
908
909
0
        return rsa_signverify_message_update(prsactx, tbs, tbslen)
910
0
            && rsa_sign_message_final(prsactx, sig, siglen, sigsize);
911
0
    }
912
0
    return rsa_sign_directly(prsactx, sig, siglen, sigsize, tbs, tbslen);
913
0
}
914
915
static int rsa_verify_recover_init(void *vprsactx, void *vrsa,
916
                                   const OSSL_PARAM params[])
917
0
{
918
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
919
920
#ifdef FIPS_MODULE
921
    if (prsactx != NULL)
922
        prsactx->verify_message = 0;
923
#endif
924
925
0
    return rsa_signverify_init(prsactx, vrsa, rsa_set_ctx_params, params,
926
0
                               EVP_PKEY_OP_VERIFYRECOVER, "RSA VerifyRecover Init");
927
0
}
928
929
/*
930
 * There is no message variant of verify recover, so no need for
931
 * 'rsa_verify_recover_directly', just use this function, er, directly.
932
 */
933
static int rsa_verify_recover(void *vprsactx,
934
                              unsigned char *rout, size_t *routlen,
935
                              size_t routsize,
936
                              const unsigned char *sig, size_t siglen)
937
0
{
938
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
939
0
    int ret;
940
941
0
    if (!ossl_prov_is_running())
942
0
        return 0;
943
944
0
    if (rout == NULL) {
945
0
        *routlen = RSA_size(prsactx->rsa);
946
0
        return 1;
947
0
    }
948
949
0
    if (prsactx->md != NULL) {
950
0
        switch (prsactx->pad_mode) {
951
0
        case RSA_X931_PADDING:
952
0
            if (!setup_tbuf(prsactx))
953
0
                return 0;
954
0
            ret = RSA_public_decrypt((int)siglen, sig, prsactx->tbuf, prsactx->rsa,
955
0
                                     RSA_X931_PADDING);
956
0
            if (ret <= 0) {
957
0
                ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
958
0
                return 0;
959
0
            }
960
0
            ret--;
961
0
            if (prsactx->tbuf[ret] != RSA_X931_hash_id(prsactx->mdnid)) {
962
0
                ERR_raise(ERR_LIB_PROV, PROV_R_ALGORITHM_MISMATCH);
963
0
                return 0;
964
0
            }
965
0
            if (ret != EVP_MD_get_size(prsactx->md)) {
966
0
                ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_LENGTH,
967
0
                               "Should be %d, but got %d",
968
0
                               EVP_MD_get_size(prsactx->md), ret);
969
0
                return 0;
970
0
            }
971
972
0
            *routlen = ret;
973
0
            if (rout != prsactx->tbuf) {
974
0
                if (routsize < (size_t)ret) {
975
0
                    ERR_raise_data(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL,
976
0
                                   "buffer size is %d, should be %d",
977
0
                                   routsize, ret);
978
0
                    return 0;
979
0
                }
980
0
                memcpy(rout, prsactx->tbuf, ret);
981
0
            }
982
0
            break;
983
984
0
        case RSA_PKCS1_PADDING:
985
0
            {
986
0
                size_t sltmp;
987
988
0
                ret = ossl_rsa_verify(prsactx->mdnid, NULL, 0, rout, &sltmp,
989
0
                                      sig, siglen, prsactx->rsa);
990
0
                if (ret <= 0) {
991
0
                    ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
992
0
                    return 0;
993
0
                }
994
0
                ret = (int)sltmp;
995
0
            }
996
0
            break;
997
998
0
        default:
999
0
            ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_PADDING_MODE,
1000
0
                           "Only X.931 or PKCS#1 v1.5 padding allowed");
1001
0
            return 0;
1002
0
        }
1003
0
    } else {
1004
0
        ret = RSA_public_decrypt((int)siglen, sig, rout, prsactx->rsa,
1005
0
                                 prsactx->pad_mode);
1006
0
        if (ret <= 0) {
1007
0
            ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
1008
0
            return 0;
1009
0
        }
1010
0
    }
1011
0
    *routlen = ret;
1012
0
    return 1;
1013
0
}
1014
1015
static int rsa_verify_init(void *vprsactx, void *vrsa,
1016
                           const OSSL_PARAM params[])
1017
0
{
1018
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1019
1020
#ifdef FIPS_MODULE
1021
    if (prsactx != NULL)
1022
        prsactx->verify_message = 0;
1023
#endif
1024
1025
0
    return rsa_signverify_init(prsactx, vrsa, rsa_set_ctx_params, params,
1026
0
                               EVP_PKEY_OP_VERIFY, "RSA Verify Init");
1027
0
}
1028
1029
static int rsa_verify_directly(PROV_RSA_CTX *prsactx,
1030
                               const unsigned char *sig, size_t siglen,
1031
                               const unsigned char *tbs, size_t tbslen)
1032
0
{
1033
0
    size_t rslen;
1034
1035
0
    if (!ossl_prov_is_running())
1036
0
        return 0;
1037
0
    if (prsactx->md != NULL) {
1038
0
        switch (prsactx->pad_mode) {
1039
0
        case RSA_PKCS1_PADDING:
1040
0
            if (!RSA_verify(prsactx->mdnid, tbs, (unsigned int)tbslen,
1041
0
                            sig, (unsigned int)siglen, prsactx->rsa)) {
1042
0
                ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
1043
0
                return 0;
1044
0
            }
1045
0
            return 1;
1046
0
        case RSA_X931_PADDING:
1047
0
            if (!setup_tbuf(prsactx))
1048
0
                return 0;
1049
0
            if (rsa_verify_recover(prsactx, prsactx->tbuf, &rslen, 0,
1050
0
                                   sig, siglen) <= 0)
1051
0
                return 0;
1052
0
            break;
1053
0
        case RSA_PKCS1_PSS_PADDING:
1054
0
            {
1055
0
                int ret;
1056
0
                int saltlen;
1057
0
                size_t mdsize;
1058
1059
                /*
1060
                 * We need to check this for the RSA_verify_PKCS1_PSS_mgf1()
1061
                 * call
1062
                 */
1063
0
                mdsize = rsa_get_md_size(prsactx);
1064
0
                if (tbslen != mdsize) {
1065
0
                    ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_LENGTH,
1066
0
                                   "Should be %d, but got %d",
1067
0
                                   mdsize, tbslen);
1068
0
                    return 0;
1069
0
                }
1070
1071
0
                if (!setup_tbuf(prsactx))
1072
0
                    return 0;
1073
0
                ret = RSA_public_decrypt((int)siglen, sig, prsactx->tbuf,
1074
0
                                         prsactx->rsa, RSA_NO_PADDING);
1075
0
                if (ret <= 0) {
1076
0
                    ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
1077
0
                    return 0;
1078
0
                }
1079
0
                saltlen = prsactx->saltlen;
1080
0
                ret = ossl_rsa_verify_PKCS1_PSS_mgf1(prsactx->rsa, tbs,
1081
0
                                                     prsactx->md, prsactx->mgf1_md,
1082
0
                                                     prsactx->tbuf,
1083
0
                                                     &saltlen);
1084
0
                if (ret <= 0) {
1085
0
                    ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
1086
0
                    return 0;
1087
0
                }
1088
#ifdef FIPS_MODULE
1089
                if (!rsa_pss_saltlen_check_passed(prsactx, "RSA Verify", saltlen))
1090
                    return 0;
1091
#endif
1092
0
                return 1;
1093
0
            }
1094
0
        default:
1095
0
            ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_PADDING_MODE,
1096
0
                           "Only X.931, PKCS#1 v1.5 or PSS padding allowed");
1097
0
            return 0;
1098
0
        }
1099
0
    } else {
1100
0
        int ret;
1101
1102
0
        if (!setup_tbuf(prsactx))
1103
0
            return 0;
1104
0
        ret = RSA_public_decrypt((int)siglen, sig, prsactx->tbuf, prsactx->rsa,
1105
0
                                 prsactx->pad_mode);
1106
0
        if (ret <= 0) {
1107
0
            ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
1108
0
            return 0;
1109
0
        }
1110
0
        rslen = (size_t)ret;
1111
0
    }
1112
1113
0
    if ((rslen != tbslen) || memcmp(tbs, prsactx->tbuf, rslen))
1114
0
        return 0;
1115
1116
0
    return 1;
1117
0
}
1118
1119
static int rsa_verify_set_sig(void *vprsactx,
1120
                              const unsigned char *sig, size_t siglen)
1121
0
{
1122
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1123
0
    OSSL_PARAM params[2];
1124
1125
0
    params[0] =
1126
0
        OSSL_PARAM_construct_octet_string(OSSL_SIGNATURE_PARAM_SIGNATURE,
1127
0
                                          (unsigned char *)sig, siglen);
1128
0
    params[1] = OSSL_PARAM_construct_end();
1129
0
    return rsa_sigalg_set_ctx_params(prsactx, params);
1130
0
}
1131
1132
static int rsa_verify_message_final(void *vprsactx)
1133
0
{
1134
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1135
0
    unsigned char digest[EVP_MAX_MD_SIZE];
1136
0
    unsigned int dlen = 0;
1137
1138
0
    if (!ossl_prov_is_running() || prsactx == NULL)
1139
0
        return 0;
1140
0
    if (prsactx->mdctx == NULL)
1141
0
        return 0;
1142
0
    if (!prsactx->flag_allow_final) {
1143
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FINAL_CALL_OUT_OF_ORDER);
1144
0
        return 0;
1145
0
    }
1146
1147
    /*
1148
     * The digests used here are all known (see rsa_get_md_nid()), so they
1149
     * should not exceed the internal buffer size of EVP_MAX_MD_SIZE.
1150
     */
1151
0
    if (!EVP_DigestFinal_ex(prsactx->mdctx, digest, &dlen))
1152
0
        return 0;
1153
1154
0
    prsactx->flag_allow_update = 0;
1155
0
    prsactx->flag_allow_final = 0;
1156
0
    prsactx->flag_allow_oneshot = 0;
1157
1158
0
    return rsa_verify_directly(prsactx, prsactx->sig, prsactx->siglen,
1159
0
                               digest, dlen);
1160
0
}
1161
1162
/*
1163
 * If verifying a message, digest tbs and verify the result.
1164
 * Otherwise, verify tbs directly.
1165
 */
1166
static int rsa_verify(void *vprsactx,
1167
                      const unsigned char *sig, size_t siglen,
1168
                      const unsigned char *tbs, size_t tbslen)
1169
0
{
1170
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1171
1172
0
    if (!ossl_prov_is_running() || prsactx == NULL)
1173
0
        return 0;
1174
0
    if (!prsactx->flag_allow_oneshot) {
1175
0
        ERR_raise(ERR_LIB_PROV, PROV_R_ONESHOT_CALL_OUT_OF_ORDER);
1176
0
        return 0;
1177
0
    }
1178
1179
0
    if (prsactx->operation == EVP_PKEY_OP_VERIFYMSG)
1180
0
        return rsa_verify_set_sig(prsactx, sig, siglen)
1181
0
            && rsa_signverify_message_update(prsactx, tbs, tbslen)
1182
0
            && rsa_verify_message_final(prsactx);
1183
0
    return rsa_verify_directly(prsactx, sig, siglen, tbs, tbslen);
1184
0
}
1185
1186
/* DigestSign/DigestVerify wrappers */
1187
1188
static int rsa_digest_signverify_init(void *vprsactx, const char *mdname,
1189
                                      void *vrsa, const OSSL_PARAM params[],
1190
                                      int operation, const char *desc)
1191
0
{
1192
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1193
1194
#ifdef FIPS_MODULE
1195
    if (prsactx != NULL)
1196
        prsactx->verify_message = 1;
1197
#endif
1198
1199
0
    if (!rsa_signverify_init(prsactx, vrsa, rsa_set_ctx_params, params,
1200
0
                             operation, desc))
1201
0
        return 0;
1202
1203
0
    if (mdname != NULL
1204
        /* was rsa_setup_md already called in rsa_signverify_init()? */
1205
0
        && (mdname[0] == '\0' || OPENSSL_strcasecmp(prsactx->mdname, mdname) != 0)
1206
0
        && !rsa_setup_md(prsactx, mdname, prsactx->propq, desc))
1207
0
        return 0;
1208
1209
0
    prsactx->flag_allow_md = 0;
1210
1211
0
    if (prsactx->mdctx == NULL) {
1212
0
        prsactx->mdctx = EVP_MD_CTX_new();
1213
0
        if (prsactx->mdctx == NULL)
1214
0
            goto error;
1215
0
    }
1216
1217
0
    if (!EVP_DigestInit_ex2(prsactx->mdctx, prsactx->md, params))
1218
0
        goto error;
1219
1220
0
    return 1;
1221
1222
0
 error:
1223
0
    EVP_MD_CTX_free(prsactx->mdctx);
1224
0
    prsactx->mdctx = NULL;
1225
0
    return 0;
1226
0
}
1227
1228
static int rsa_digest_sign_init(void *vprsactx, const char *mdname,
1229
                                void *vrsa, const OSSL_PARAM params[])
1230
0
{
1231
0
    if (!ossl_prov_is_running())
1232
0
        return 0;
1233
0
    return rsa_digest_signverify_init(vprsactx, mdname, vrsa,
1234
0
                                      params, EVP_PKEY_OP_SIGNMSG,
1235
0
                                      "RSA Digest Sign Init");
1236
0
}
1237
1238
static int rsa_digest_sign_update(void *vprsactx, const unsigned char *data,
1239
                                  size_t datalen)
1240
0
{
1241
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1242
1243
0
    if (prsactx == NULL)
1244
0
        return 0;
1245
    /* Sigalg implementations shouldn't do digest_sign */
1246
0
    if (prsactx->flag_sigalg)
1247
0
        return 0;
1248
1249
0
    return rsa_signverify_message_update(prsactx, data, datalen);
1250
0
}
1251
1252
static int rsa_digest_sign_final(void *vprsactx, unsigned char *sig,
1253
                                 size_t *siglen, size_t sigsize)
1254
0
{
1255
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1256
0
    int ok = 0;
1257
1258
0
    if (prsactx == NULL)
1259
0
        return 0;
1260
    /* Sigalg implementations shouldn't do digest_sign */
1261
0
    if (prsactx->flag_sigalg)
1262
0
        return 0;
1263
1264
0
    if (rsa_sign_message_final(prsactx, sig, siglen, sigsize))
1265
0
        ok = 1;
1266
1267
0
    prsactx->flag_allow_md = 1;
1268
1269
0
    return ok;
1270
0
}
1271
1272
static int rsa_digest_verify_init(void *vprsactx, const char *mdname,
1273
                                  void *vrsa, const OSSL_PARAM params[])
1274
0
{
1275
0
    if (!ossl_prov_is_running())
1276
0
        return 0;
1277
0
    return rsa_digest_signverify_init(vprsactx, mdname, vrsa,
1278
0
                                      params, EVP_PKEY_OP_VERIFYMSG,
1279
0
                                      "RSA Digest Verify Init");
1280
0
}
1281
1282
static int rsa_digest_verify_update(void *vprsactx, const unsigned char *data,
1283
                                    size_t datalen)
1284
0
{
1285
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1286
1287
0
    if (prsactx == NULL)
1288
0
        return 0;
1289
    /* Sigalg implementations shouldn't do digest_sign */
1290
0
    if (prsactx->flag_sigalg)
1291
0
        return 0;
1292
1293
0
    return rsa_signverify_message_update(prsactx, data, datalen);
1294
0
}
1295
1296
int rsa_digest_verify_final(void *vprsactx, const unsigned char *sig,
1297
                            size_t siglen)
1298
0
{
1299
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1300
0
    int ok = 0;
1301
1302
0
    if (prsactx == NULL)
1303
0
        return 0;
1304
    /* Sigalg implementations shouldn't do digest_verify */
1305
0
    if (prsactx->flag_sigalg)
1306
0
        return 0;
1307
1308
0
    if (rsa_verify_set_sig(prsactx, sig, siglen)
1309
0
        && rsa_verify_message_final(vprsactx))
1310
0
        ok = 1;
1311
1312
0
    prsactx->flag_allow_md = 1;
1313
1314
0
    return ok;
1315
0
}
1316
1317
static void rsa_freectx(void *vprsactx)
1318
0
{
1319
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1320
1321
0
    if (prsactx == NULL)
1322
0
        return;
1323
1324
0
    EVP_MD_CTX_free(prsactx->mdctx);
1325
0
    EVP_MD_free(prsactx->md);
1326
0
    EVP_MD_free(prsactx->mgf1_md);
1327
0
    OPENSSL_free(prsactx->sig);
1328
0
    OPENSSL_free(prsactx->propq);
1329
0
    free_tbuf(prsactx);
1330
0
    RSA_free(prsactx->rsa);
1331
1332
0
    OPENSSL_clear_free(prsactx, sizeof(*prsactx));
1333
0
}
1334
1335
static void *rsa_dupctx(void *vprsactx)
1336
0
{
1337
0
    PROV_RSA_CTX *srcctx = (PROV_RSA_CTX *)vprsactx;
1338
0
    PROV_RSA_CTX *dstctx;
1339
1340
0
    if (!ossl_prov_is_running())
1341
0
        return NULL;
1342
1343
0
    dstctx = OPENSSL_zalloc(sizeof(*srcctx));
1344
0
    if (dstctx == NULL)
1345
0
        return NULL;
1346
1347
0
    *dstctx = *srcctx;
1348
0
    dstctx->rsa = NULL;
1349
0
    dstctx->md = NULL;
1350
0
    dstctx->mgf1_md = NULL;
1351
0
    dstctx->mdctx = NULL;
1352
0
    dstctx->tbuf = NULL;
1353
0
    dstctx->propq = NULL;
1354
1355
0
    if (srcctx->rsa != NULL && !RSA_up_ref(srcctx->rsa))
1356
0
        goto err;
1357
0
    dstctx->rsa = srcctx->rsa;
1358
1359
0
    if (srcctx->md != NULL && !EVP_MD_up_ref(srcctx->md))
1360
0
        goto err;
1361
0
    dstctx->md = srcctx->md;
1362
1363
0
    if (srcctx->mgf1_md != NULL && !EVP_MD_up_ref(srcctx->mgf1_md))
1364
0
        goto err;
1365
0
    dstctx->mgf1_md = srcctx->mgf1_md;
1366
1367
0
    if (srcctx->mdctx != NULL) {
1368
0
        dstctx->mdctx = EVP_MD_CTX_new();
1369
0
        if (dstctx->mdctx == NULL
1370
0
                || !EVP_MD_CTX_copy_ex(dstctx->mdctx, srcctx->mdctx))
1371
0
            goto err;
1372
0
    }
1373
1374
0
    if (srcctx->propq != NULL) {
1375
0
        dstctx->propq = OPENSSL_strdup(srcctx->propq);
1376
0
        if (dstctx->propq == NULL)
1377
0
            goto err;
1378
0
    }
1379
1380
0
    return dstctx;
1381
0
 err:
1382
0
    rsa_freectx(dstctx);
1383
0
    return NULL;
1384
0
}
1385
1386
/* Machine generated by util/perl/OpenSSL/paramnames.pm */
1387
#ifndef rsa_get_ctx_params_list
1388
static const OSSL_PARAM rsa_get_ctx_params_list[] = {
1389
    OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_ALGORITHM_ID, NULL, 0),
1390
    OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PAD_MODE, NULL, 0),
1391
    OSSL_PARAM_int(OSSL_SIGNATURE_PARAM_PAD_MODE, NULL),
1392
    OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),
1393
    OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_MGF1_DIGEST, NULL, 0),
1394
    OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PSS_SALTLEN, NULL, 0),
1395
    OSSL_PARAM_int(OSSL_SIGNATURE_PARAM_PSS_SALTLEN, NULL),
1396
# if defined(FIPS_MODULE)
1397
    OSSL_PARAM_uint(OSSL_SIGNATURE_PARAM_FIPS_VERIFY_MESSAGE, NULL),
1398
# endif
1399
# if defined(FIPS_MODULE)
1400
    OSSL_PARAM_int(OSSL_SIGNATURE_PARAM_FIPS_APPROVED_INDICATOR, NULL),
1401
# endif
1402
    OSSL_PARAM_END
1403
};
1404
#endif
1405
1406
#ifndef rsa_get_ctx_params_st
1407
struct rsa_get_ctx_params_st {
1408
    OSSL_PARAM *algid;
1409
    OSSL_PARAM *digest;
1410
# if defined(FIPS_MODULE)
1411
    OSSL_PARAM *ind;
1412
# endif
1413
    OSSL_PARAM *mgf1;
1414
    OSSL_PARAM *pad;
1415
    OSSL_PARAM *slen;
1416
# if defined(FIPS_MODULE)
1417
    OSSL_PARAM *verify;
1418
# endif
1419
};
1420
#endif
1421
1422
#ifndef rsa_get_ctx_params_decoder
1423
static int rsa_get_ctx_params_decoder
1424
    (const OSSL_PARAM *p, struct rsa_get_ctx_params_st *r)
1425
0
{
1426
0
    const char *s;
1427
1428
0
    memset(r, 0, sizeof(*r));
1429
0
    if (p != NULL)
1430
0
        for (; (s = p->key) != NULL; p++)
1431
0
            switch(s[0]) {
1432
0
            default:
1433
0
                break;
1434
0
            case 'a':
1435
0
                if (ossl_likely(strcmp("lgorithm-id", s + 1) == 0)) {
1436
                    /* SIGNATURE_PARAM_ALGORITHM_ID */
1437
0
                    if (ossl_unlikely(r->algid != NULL)) {
1438
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
1439
0
                                       "param %s is repeated", s);
1440
0
                        return 0;
1441
0
                    }
1442
0
                    r->algid = (OSSL_PARAM *)p;
1443
0
                }
1444
0
                break;
1445
0
            case 'd':
1446
0
                if (ossl_likely(strcmp("igest", s + 1) == 0)) {
1447
                    /* SIGNATURE_PARAM_DIGEST */
1448
0
                    if (ossl_unlikely(r->digest != NULL)) {
1449
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
1450
0
                                       "param %s is repeated", s);
1451
0
                        return 0;
1452
0
                    }
1453
0
                    r->digest = (OSSL_PARAM *)p;
1454
0
                }
1455
0
                break;
1456
0
            case 'f':
1457
# if defined(FIPS_MODULE)
1458
                if (ossl_likely(strcmp("ips-indicator", s + 1) == 0)) {
1459
                    /* SIGNATURE_PARAM_FIPS_APPROVED_INDICATOR */
1460
                    if (ossl_unlikely(r->ind != NULL)) {
1461
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
1462
                                       "param %s is repeated", s);
1463
                        return 0;
1464
                    }
1465
                    r->ind = (OSSL_PARAM *)p;
1466
                }
1467
# endif
1468
0
                break;
1469
0
            case 'm':
1470
0
                if (ossl_likely(strcmp("gf1-digest", s + 1) == 0)) {
1471
                    /* SIGNATURE_PARAM_MGF1_DIGEST */
1472
0
                    if (ossl_unlikely(r->mgf1 != NULL)) {
1473
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
1474
0
                                       "param %s is repeated", s);
1475
0
                        return 0;
1476
0
                    }
1477
0
                    r->mgf1 = (OSSL_PARAM *)p;
1478
0
                }
1479
0
                break;
1480
0
            case 'p':
1481
0
                if (ossl_likely(strcmp("ad-mode", s + 1) == 0)) {
1482
                    /* SIGNATURE_PARAM_PAD_MODE */
1483
0
                    if (ossl_unlikely(r->pad != NULL)) {
1484
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
1485
0
                                       "param %s is repeated", s);
1486
0
                        return 0;
1487
0
                    }
1488
0
                    r->pad = (OSSL_PARAM *)p;
1489
0
                }
1490
0
                break;
1491
0
            case 's':
1492
0
                if (ossl_likely(strcmp("altlen", s + 1) == 0)) {
1493
                    /* SIGNATURE_PARAM_PSS_SALTLEN */
1494
0
                    if (ossl_unlikely(r->slen != NULL)) {
1495
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
1496
0
                                       "param %s is repeated", s);
1497
0
                        return 0;
1498
0
                    }
1499
0
                    r->slen = (OSSL_PARAM *)p;
1500
0
                }
1501
0
                break;
1502
0
            case 'v':
1503
# if defined(FIPS_MODULE)
1504
                if (ossl_likely(strcmp("erify-message", s + 1) == 0)) {
1505
                    /* SIGNATURE_PARAM_FIPS_VERIFY_MESSAGE */
1506
                    if (ossl_unlikely(r->verify != NULL)) {
1507
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
1508
                                       "param %s is repeated", s);
1509
                        return 0;
1510
                    }
1511
                    r->verify = (OSSL_PARAM *)p;
1512
                }
1513
# endif
1514
0
                break;
1515
0
            }
1516
0
    return 1;
1517
0
}
1518
#endif
1519
/* End of machine generated */
1520
1521
static int rsa_get_ctx_params(void *vprsactx, OSSL_PARAM *params)
1522
0
{
1523
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1524
0
    struct rsa_get_ctx_params_st p;
1525
1526
0
    if (prsactx == NULL || !rsa_get_ctx_params_decoder(params, &p))
1527
0
        return 0;
1528
1529
0
    if (p.algid != NULL) {
1530
        /* The Algorithm Identifier of the combined signature algorithm */
1531
0
        unsigned char aid_buf[128];
1532
0
        unsigned char *aid;
1533
0
        size_t  aid_len;
1534
1535
0
        aid = rsa_generate_signature_aid(prsactx, aid_buf,
1536
0
                                         sizeof(aid_buf), &aid_len);
1537
0
        if (aid == NULL || !OSSL_PARAM_set_octet_string(p.algid, aid, aid_len))
1538
0
            return 0;
1539
0
    }
1540
1541
0
    if (p.pad != NULL) {
1542
0
        if (p.pad->data_type != OSSL_PARAM_UTF8_STRING) {
1543
0
            if (!OSSL_PARAM_set_int(p.pad, prsactx->pad_mode))
1544
0
                return 0;
1545
0
        } else {
1546
0
            int i;
1547
0
            const char *word = NULL;
1548
1549
0
            for (i = 0; padding_item[i].id != 0; i++) {
1550
0
                if (prsactx->pad_mode == (int)padding_item[i].id) {
1551
0
                    word = padding_item[i].ptr;
1552
0
                    break;
1553
0
                }
1554
0
            }
1555
1556
0
            if (word != NULL) {
1557
0
                if (!OSSL_PARAM_set_utf8_string(p.pad, word))
1558
0
                    return 0;
1559
0
            } else {
1560
0
                ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
1561
0
            }
1562
0
        }
1563
0
    }
1564
1565
0
    if (p.digest != NULL && !OSSL_PARAM_set_utf8_string(p.digest, prsactx->mdname))
1566
0
        return 0;
1567
1568
0
    if (p.mgf1 != NULL && !OSSL_PARAM_set_utf8_string(p.mgf1, prsactx->mgf1_mdname))
1569
0
        return 0;
1570
1571
0
    if (p.slen != NULL) {
1572
0
        if (p.slen->data_type != OSSL_PARAM_UTF8_STRING) {
1573
0
            if (!OSSL_PARAM_set_int(p.slen, prsactx->saltlen))
1574
0
                return 0;
1575
0
        } else {
1576
0
            const char *value = NULL;
1577
1578
0
            switch (prsactx->saltlen) {
1579
0
            case RSA_PSS_SALTLEN_DIGEST:
1580
0
                value = OSSL_PKEY_RSA_PSS_SALT_LEN_DIGEST;
1581
0
                break;
1582
0
            case RSA_PSS_SALTLEN_MAX:
1583
0
                value = OSSL_PKEY_RSA_PSS_SALT_LEN_MAX;
1584
0
                break;
1585
0
            case RSA_PSS_SALTLEN_AUTO:
1586
0
                value = OSSL_PKEY_RSA_PSS_SALT_LEN_AUTO;
1587
0
                break;
1588
0
            case RSA_PSS_SALTLEN_AUTO_DIGEST_MAX:
1589
0
                value = OSSL_PKEY_RSA_PSS_SALT_LEN_AUTO_DIGEST_MAX;
1590
0
                break;
1591
0
            default:
1592
0
                {
1593
0
                    int len = BIO_snprintf(p.slen->data, p.slen->data_size, "%d",
1594
0
                                           prsactx->saltlen);
1595
1596
0
                    if (len <= 0)
1597
0
                        return 0;
1598
0
                    p.slen->return_size = len;
1599
0
                    break;
1600
0
                }
1601
0
            }
1602
0
            if (value != NULL
1603
0
                && !OSSL_PARAM_set_utf8_string(p.slen, value))
1604
0
                return 0;
1605
0
        }
1606
0
    }
1607
1608
#ifdef FIPS_MODULE
1609
    if (p.verify != NULL && !OSSL_PARAM_set_uint(p.verify, prsactx->verify_message))
1610
        return 0;
1611
#endif
1612
1613
0
    if (!OSSL_FIPS_IND_GET_CTX_FROM_PARAM(prsactx, p.ind))
1614
0
        return 0;
1615
0
    return 1;
1616
0
}
1617
1618
static const OSSL_PARAM *rsa_gettable_ctx_params(ossl_unused void *vprsactx,
1619
                                                 ossl_unused void *provctx)
1620
0
{
1621
0
    return rsa_get_ctx_params_list;
1622
0
}
1623
1624
#ifdef FIPS_MODULE
1625
static int rsa_x931_padding_allowed(PROV_RSA_CTX *ctx)
1626
{
1627
    int approved = ((ctx->operation & EVP_PKEY_OP_SIGN) == 0);
1628
1629
    if (!approved) {
1630
        if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE2,
1631
                                         ctx->libctx,
1632
                                         "RSA Sign set ctx", "X931 Padding",
1633
                                         ossl_fips_config_rsa_sign_x931_disallowed)) {
1634
            ERR_raise(ERR_LIB_PROV,
1635
                      PROV_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
1636
            return 0;
1637
        }
1638
    }
1639
    return 1;
1640
}
1641
#endif
1642
1643
/* Machine generated by util/perl/OpenSSL/paramnames.pm */
1644
#ifndef rsa_set_ctx_params_list
1645
static const OSSL_PARAM rsa_set_ctx_params_list[] = {
1646
    OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),
1647
    OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PROPERTIES, NULL, 0),
1648
    OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PAD_MODE, NULL, 0),
1649
    OSSL_PARAM_int(OSSL_SIGNATURE_PARAM_PAD_MODE, NULL),
1650
    OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_MGF1_DIGEST, NULL, 0),
1651
    OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_MGF1_PROPERTIES, NULL, 0),
1652
    OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PSS_SALTLEN, NULL, 0),
1653
    OSSL_PARAM_int(OSSL_SIGNATURE_PARAM_PSS_SALTLEN, NULL),
1654
# if defined(FIPS_MODULE)
1655
    OSSL_PARAM_int(OSSL_SIGNATURE_PARAM_FIPS_KEY_CHECK, NULL),
1656
# endif
1657
# if defined(FIPS_MODULE)
1658
    OSSL_PARAM_int(OSSL_SIGNATURE_PARAM_FIPS_DIGEST_CHECK, NULL),
1659
# endif
1660
# if defined(FIPS_MODULE)
1661
    OSSL_PARAM_int(OSSL_SIGNATURE_PARAM_FIPS_RSA_PSS_SALTLEN_CHECK, NULL),
1662
# endif
1663
# if defined(FIPS_MODULE)
1664
    OSSL_PARAM_int(OSSL_SIGNATURE_PARAM_FIPS_SIGN_X931_PAD_CHECK, NULL),
1665
# endif
1666
    OSSL_PARAM_END
1667
};
1668
#endif
1669
1670
#ifndef rsa_set_ctx_params_st
1671
struct rsa_set_ctx_params_st {
1672
    OSSL_PARAM *digest;
1673
# if defined(FIPS_MODULE)
1674
    OSSL_PARAM *ind_d;
1675
# endif
1676
# if defined(FIPS_MODULE)
1677
    OSSL_PARAM *ind_k;
1678
# endif
1679
# if defined(FIPS_MODULE)
1680
    OSSL_PARAM *ind_slen;
1681
# endif
1682
# if defined(FIPS_MODULE)
1683
    OSSL_PARAM *ind_xpad;
1684
# endif
1685
    OSSL_PARAM *mgf1;
1686
    OSSL_PARAM *mgf1pq;
1687
    OSSL_PARAM *pad;
1688
    OSSL_PARAM *propq;
1689
    OSSL_PARAM *slen;
1690
};
1691
#endif
1692
1693
#ifndef rsa_set_ctx_params_decoder
1694
static int rsa_set_ctx_params_decoder
1695
    (const OSSL_PARAM *p, struct rsa_set_ctx_params_st *r)
1696
0
{
1697
0
    const char *s;
1698
1699
0
    memset(r, 0, sizeof(*r));
1700
0
    if (p != NULL)
1701
0
        for (; (s = p->key) != NULL; p++)
1702
0
            switch(s[0]) {
1703
0
            default:
1704
0
                break;
1705
0
            case 'd':
1706
0
                switch(s[1]) {
1707
0
                default:
1708
0
                    break;
1709
0
                case 'i':
1710
0
                    switch(s[2]) {
1711
0
                    default:
1712
0
                        break;
1713
0
                    case 'g':
1714
0
                        switch(s[3]) {
1715
0
                        default:
1716
0
                            break;
1717
0
                        case 'e':
1718
0
                            switch(s[4]) {
1719
0
                            default:
1720
0
                                break;
1721
0
                            case 's':
1722
0
                                switch(s[5]) {
1723
0
                                default:
1724
0
                                    break;
1725
0
                                case 't':
1726
0
                                    switch(s[6]) {
1727
0
                                    default:
1728
0
                                        break;
1729
0
                                    case '-':
1730
# if defined(FIPS_MODULE)
1731
                                        if (ossl_likely(strcmp("check", s + 7) == 0)) {
1732
                                            /* SIGNATURE_PARAM_FIPS_DIGEST_CHECK */
1733
                                            if (ossl_unlikely(r->ind_d != NULL)) {
1734
                                                ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
1735
                                                               "param %s is repeated", s);
1736
                                                return 0;
1737
                                            }
1738
                                            r->ind_d = (OSSL_PARAM *)p;
1739
                                        }
1740
# endif
1741
0
                                        break;
1742
0
                                    case '\0':
1743
0
                                        if (ossl_unlikely(r->digest != NULL)) {
1744
0
                                            ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
1745
0
                                                           "param %s is repeated", s);
1746
0
                                            return 0;
1747
0
                                        }
1748
0
                                        r->digest = (OSSL_PARAM *)p;
1749
0
                                    }
1750
0
                                }
1751
0
                            }
1752
0
                        }
1753
0
                    }
1754
0
                }
1755
0
                break;
1756
0
            case 'k':
1757
# if defined(FIPS_MODULE)
1758
                if (ossl_likely(strcmp("ey-check", s + 1) == 0)) {
1759
                    /* SIGNATURE_PARAM_FIPS_KEY_CHECK */
1760
                    if (ossl_unlikely(r->ind_k != NULL)) {
1761
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
1762
                                       "param %s is repeated", s);
1763
                        return 0;
1764
                    }
1765
                    r->ind_k = (OSSL_PARAM *)p;
1766
                }
1767
# endif
1768
0
                break;
1769
0
            case 'm':
1770
0
                switch(s[1]) {
1771
0
                default:
1772
0
                    break;
1773
0
                case 'g':
1774
0
                    switch(s[2]) {
1775
0
                    default:
1776
0
                        break;
1777
0
                    case 'f':
1778
0
                        switch(s[3]) {
1779
0
                        default:
1780
0
                            break;
1781
0
                        case '1':
1782
0
                            switch(s[4]) {
1783
0
                            default:
1784
0
                                break;
1785
0
                            case '-':
1786
0
                                switch(s[5]) {
1787
0
                                default:
1788
0
                                    break;
1789
0
                                case 'd':
1790
0
                                    if (ossl_likely(strcmp("igest", s + 6) == 0)) {
1791
                                        /* SIGNATURE_PARAM_MGF1_DIGEST */
1792
0
                                        if (ossl_unlikely(r->mgf1 != NULL)) {
1793
0
                                            ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
1794
0
                                                           "param %s is repeated", s);
1795
0
                                            return 0;
1796
0
                                        }
1797
0
                                        r->mgf1 = (OSSL_PARAM *)p;
1798
0
                                    }
1799
0
                                    break;
1800
0
                                case 'p':
1801
0
                                    if (ossl_likely(strcmp("roperties", s + 6) == 0)) {
1802
                                        /* SIGNATURE_PARAM_MGF1_PROPERTIES */
1803
0
                                        if (ossl_unlikely(r->mgf1pq != NULL)) {
1804
0
                                            ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
1805
0
                                                           "param %s is repeated", s);
1806
0
                                            return 0;
1807
0
                                        }
1808
0
                                        r->mgf1pq = (OSSL_PARAM *)p;
1809
0
                                    }
1810
0
                                }
1811
0
                            }
1812
0
                        }
1813
0
                    }
1814
0
                }
1815
0
                break;
1816
0
            case 'p':
1817
0
                switch(s[1]) {
1818
0
                default:
1819
0
                    break;
1820
0
                case 'a':
1821
0
                    if (ossl_likely(strcmp("d-mode", s + 2) == 0)) {
1822
                        /* SIGNATURE_PARAM_PAD_MODE */
1823
0
                        if (ossl_unlikely(r->pad != NULL)) {
1824
0
                            ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
1825
0
                                           "param %s is repeated", s);
1826
0
                            return 0;
1827
0
                        }
1828
0
                        r->pad = (OSSL_PARAM *)p;
1829
0
                    }
1830
0
                    break;
1831
0
                case 'r':
1832
0
                    if (ossl_likely(strcmp("operties", s + 2) == 0)) {
1833
                        /* SIGNATURE_PARAM_PROPERTIES */
1834
0
                        if (ossl_unlikely(r->propq != NULL)) {
1835
0
                            ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
1836
0
                                           "param %s is repeated", s);
1837
0
                            return 0;
1838
0
                        }
1839
0
                        r->propq = (OSSL_PARAM *)p;
1840
0
                    }
1841
0
                }
1842
0
                break;
1843
0
            case 'r':
1844
# if defined(FIPS_MODULE)
1845
                if (ossl_likely(strcmp("sa-pss-saltlen-check", s + 1) == 0)) {
1846
                    /* SIGNATURE_PARAM_FIPS_RSA_PSS_SALTLEN_CHECK */
1847
                    if (ossl_unlikely(r->ind_slen != NULL)) {
1848
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
1849
                                       "param %s is repeated", s);
1850
                        return 0;
1851
                    }
1852
                    r->ind_slen = (OSSL_PARAM *)p;
1853
                }
1854
# endif
1855
0
                break;
1856
0
            case 's':
1857
0
                switch(s[1]) {
1858
0
                default:
1859
0
                    break;
1860
0
                case 'a':
1861
0
                    if (ossl_likely(strcmp("ltlen", s + 2) == 0)) {
1862
                        /* SIGNATURE_PARAM_PSS_SALTLEN */
1863
0
                        if (ossl_unlikely(r->slen != NULL)) {
1864
0
                            ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
1865
0
                                           "param %s is repeated", s);
1866
0
                            return 0;
1867
0
                        }
1868
0
                        r->slen = (OSSL_PARAM *)p;
1869
0
                    }
1870
0
                    break;
1871
0
                case 'i':
1872
# if defined(FIPS_MODULE)
1873
                    if (ossl_likely(strcmp("gn-x931-pad-check", s + 2) == 0)) {
1874
                        /* SIGNATURE_PARAM_FIPS_SIGN_X931_PAD_CHECK */
1875
                        if (ossl_unlikely(r->ind_xpad != NULL)) {
1876
                            ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
1877
                                           "param %s is repeated", s);
1878
                            return 0;
1879
                        }
1880
                        r->ind_xpad = (OSSL_PARAM *)p;
1881
                    }
1882
# endif
1883
0
                    break;
1884
0
                }
1885
0
                break;
1886
0
            }
1887
0
    return 1;
1888
0
}
1889
#endif
1890
/* End of machine generated */
1891
1892
#define rsa_set_ctx_params_no_digest_st  rsa_set_ctx_params_st
1893
1894
/* Machine generated by util/perl/OpenSSL/paramnames.pm */
1895
#ifndef rsa_set_ctx_params_no_digest_list
1896
static const OSSL_PARAM rsa_set_ctx_params_no_digest_list[] = {
1897
    OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PAD_MODE, NULL, 0),
1898
    OSSL_PARAM_int(OSSL_SIGNATURE_PARAM_PAD_MODE, NULL),
1899
    OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_MGF1_DIGEST, NULL, 0),
1900
    OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_MGF1_PROPERTIES, NULL, 0),
1901
    OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PSS_SALTLEN, NULL, 0),
1902
    OSSL_PARAM_int(OSSL_SIGNATURE_PARAM_PSS_SALTLEN, NULL),
1903
# if defined(FIPS_MODULE)
1904
    OSSL_PARAM_int(OSSL_SIGNATURE_PARAM_FIPS_KEY_CHECK, NULL),
1905
# endif
1906
# if defined(FIPS_MODULE)
1907
    OSSL_PARAM_int(OSSL_SIGNATURE_PARAM_FIPS_DIGEST_CHECK, NULL),
1908
# endif
1909
# if defined(FIPS_MODULE)
1910
    OSSL_PARAM_int(OSSL_SIGNATURE_PARAM_FIPS_RSA_PSS_SALTLEN_CHECK, NULL),
1911
# endif
1912
# if defined(FIPS_MODULE)
1913
    OSSL_PARAM_int(OSSL_SIGNATURE_PARAM_FIPS_SIGN_X931_PAD_CHECK, NULL),
1914
# endif
1915
    OSSL_PARAM_END
1916
};
1917
#endif
1918
1919
#ifndef rsa_set_ctx_params_no_digest_st
1920
struct rsa_set_ctx_params_no_digest_st {
1921
# if defined(FIPS_MODULE)
1922
    OSSL_PARAM *ind_d;
1923
# endif
1924
# if defined(FIPS_MODULE)
1925
    OSSL_PARAM *ind_k;
1926
# endif
1927
# if defined(FIPS_MODULE)
1928
    OSSL_PARAM *ind_slen;
1929
# endif
1930
# if defined(FIPS_MODULE)
1931
    OSSL_PARAM *ind_xpad;
1932
# endif
1933
    OSSL_PARAM *mgf1;
1934
    OSSL_PARAM *mgf1pq;
1935
    OSSL_PARAM *pad;
1936
    OSSL_PARAM *slen;
1937
};
1938
#endif
1939
1940
#ifndef rsa_set_ctx_params_no_digest_decoder
1941
static int rsa_set_ctx_params_no_digest_decoder
1942
    (const OSSL_PARAM *p, struct rsa_set_ctx_params_no_digest_st *r)
1943
0
{
1944
0
    const char *s;
1945
1946
0
    memset(r, 0, sizeof(*r));
1947
0
    if (p != NULL)
1948
0
        for (; (s = p->key) != NULL; p++)
1949
0
            switch(s[0]) {
1950
0
            default:
1951
0
                break;
1952
0
            case 'd':
1953
# if defined(FIPS_MODULE)
1954
                if (ossl_likely(strcmp("igest-check", s + 1) == 0)) {
1955
                    /* SIGNATURE_PARAM_FIPS_DIGEST_CHECK */
1956
                    if (ossl_unlikely(r->ind_d != NULL)) {
1957
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
1958
                                       "param %s is repeated", s);
1959
                        return 0;
1960
                    }
1961
                    r->ind_d = (OSSL_PARAM *)p;
1962
                }
1963
# endif
1964
0
                break;
1965
0
            case 'k':
1966
# if defined(FIPS_MODULE)
1967
                if (ossl_likely(strcmp("ey-check", s + 1) == 0)) {
1968
                    /* SIGNATURE_PARAM_FIPS_KEY_CHECK */
1969
                    if (ossl_unlikely(r->ind_k != NULL)) {
1970
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
1971
                                       "param %s is repeated", s);
1972
                        return 0;
1973
                    }
1974
                    r->ind_k = (OSSL_PARAM *)p;
1975
                }
1976
# endif
1977
0
                break;
1978
0
            case 'm':
1979
0
                switch(s[1]) {
1980
0
                default:
1981
0
                    break;
1982
0
                case 'g':
1983
0
                    switch(s[2]) {
1984
0
                    default:
1985
0
                        break;
1986
0
                    case 'f':
1987
0
                        switch(s[3]) {
1988
0
                        default:
1989
0
                            break;
1990
0
                        case '1':
1991
0
                            switch(s[4]) {
1992
0
                            default:
1993
0
                                break;
1994
0
                            case '-':
1995
0
                                switch(s[5]) {
1996
0
                                default:
1997
0
                                    break;
1998
0
                                case 'd':
1999
0
                                    if (ossl_likely(strcmp("igest", s + 6) == 0)) {
2000
                                        /* SIGNATURE_PARAM_MGF1_DIGEST */
2001
0
                                        if (ossl_unlikely(r->mgf1 != NULL)) {
2002
0
                                            ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
2003
0
                                                           "param %s is repeated", s);
2004
0
                                            return 0;
2005
0
                                        }
2006
0
                                        r->mgf1 = (OSSL_PARAM *)p;
2007
0
                                    }
2008
0
                                    break;
2009
0
                                case 'p':
2010
0
                                    if (ossl_likely(strcmp("roperties", s + 6) == 0)) {
2011
                                        /* SIGNATURE_PARAM_MGF1_PROPERTIES */
2012
0
                                        if (ossl_unlikely(r->mgf1pq != NULL)) {
2013
0
                                            ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
2014
0
                                                           "param %s is repeated", s);
2015
0
                                            return 0;
2016
0
                                        }
2017
0
                                        r->mgf1pq = (OSSL_PARAM *)p;
2018
0
                                    }
2019
0
                                }
2020
0
                            }
2021
0
                        }
2022
0
                    }
2023
0
                }
2024
0
                break;
2025
0
            case 'p':
2026
0
                if (ossl_likely(strcmp("ad-mode", s + 1) == 0)) {
2027
                    /* SIGNATURE_PARAM_PAD_MODE */
2028
0
                    if (ossl_unlikely(r->pad != NULL)) {
2029
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
2030
0
                                       "param %s is repeated", s);
2031
0
                        return 0;
2032
0
                    }
2033
0
                    r->pad = (OSSL_PARAM *)p;
2034
0
                }
2035
0
                break;
2036
0
            case 'r':
2037
# if defined(FIPS_MODULE)
2038
                if (ossl_likely(strcmp("sa-pss-saltlen-check", s + 1) == 0)) {
2039
                    /* SIGNATURE_PARAM_FIPS_RSA_PSS_SALTLEN_CHECK */
2040
                    if (ossl_unlikely(r->ind_slen != NULL)) {
2041
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
2042
                                       "param %s is repeated", s);
2043
                        return 0;
2044
                    }
2045
                    r->ind_slen = (OSSL_PARAM *)p;
2046
                }
2047
# endif
2048
0
                break;
2049
0
            case 's':
2050
0
                switch(s[1]) {
2051
0
                default:
2052
0
                    break;
2053
0
                case 'a':
2054
0
                    if (ossl_likely(strcmp("ltlen", s + 2) == 0)) {
2055
                        /* SIGNATURE_PARAM_PSS_SALTLEN */
2056
0
                        if (ossl_unlikely(r->slen != NULL)) {
2057
0
                            ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
2058
0
                                           "param %s is repeated", s);
2059
0
                            return 0;
2060
0
                        }
2061
0
                        r->slen = (OSSL_PARAM *)p;
2062
0
                    }
2063
0
                    break;
2064
0
                case 'i':
2065
# if defined(FIPS_MODULE)
2066
                    if (ossl_likely(strcmp("gn-x931-pad-check", s + 2) == 0)) {
2067
                        /* SIGNATURE_PARAM_FIPS_SIGN_X931_PAD_CHECK */
2068
                        if (ossl_unlikely(r->ind_xpad != NULL)) {
2069
                            ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
2070
                                           "param %s is repeated", s);
2071
                            return 0;
2072
                        }
2073
                        r->ind_xpad = (OSSL_PARAM *)p;
2074
                    }
2075
# endif
2076
0
                    break;
2077
0
                }
2078
0
                break;
2079
0
            }
2080
0
    return 1;
2081
0
}
2082
#endif
2083
/* End of machine generated */
2084
2085
static int rsa_set_ctx_params(void *vprsactx, const OSSL_PARAM params[])
2086
0
{
2087
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
2088
0
    struct rsa_set_ctx_params_st p;
2089
0
    int pad_mode;
2090
0
    int saltlen;
2091
0
    char mdname[OSSL_MAX_NAME_SIZE] = "", *pmdname = NULL;
2092
0
    char mdprops[OSSL_MAX_PROPQUERY_SIZE] = "", *pmdprops = NULL;
2093
0
    char mgf1mdname[OSSL_MAX_NAME_SIZE] = "", *pmgf1mdname = NULL;
2094
0
    char mgf1mdprops[OSSL_MAX_PROPQUERY_SIZE] = "", *pmgf1mdprops = NULL;
2095
2096
0
    if (prsactx == NULL)
2097
0
        return 0;
2098
    /* The processing code below doesn't handle no parameters properly */
2099
0
    if (ossl_param_is_empty(params))
2100
0
        return 1;
2101
2102
0
    if (prsactx->flag_allow_md) {
2103
0
        if (!rsa_set_ctx_params_decoder(params, &p))
2104
0
            return 0;
2105
0
    } else {
2106
0
        if (!rsa_set_ctx_params_no_digest_decoder(params, &p))
2107
0
            return 0;
2108
0
    }
2109
2110
0
    if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(prsactx, OSSL_FIPS_IND_SETTABLE0,
2111
0
                                          p.ind_k))
2112
0
        return 0;
2113
2114
0
    if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(prsactx, OSSL_FIPS_IND_SETTABLE1,
2115
0
                                          p.ind_d))
2116
0
        return 0;
2117
2118
0
    if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(prsactx, OSSL_FIPS_IND_SETTABLE2,
2119
0
                                          p.ind_xpad))
2120
0
        return 0;
2121
2122
0
    if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(prsactx, OSSL_FIPS_IND_SETTABLE3,
2123
0
                                          p.ind_slen))
2124
0
        return 0;
2125
2126
0
    pad_mode = prsactx->pad_mode;
2127
0
    saltlen = prsactx->saltlen;
2128
2129
0
    if (p.digest != NULL) {
2130
0
        pmdname = mdname;
2131
0
        if (!OSSL_PARAM_get_utf8_string(p.digest, &pmdname, sizeof(mdname)))
2132
0
            return 0;
2133
2134
0
        if (p.propq != NULL) {
2135
0
            pmdprops = mdprops;
2136
0
            if (!OSSL_PARAM_get_utf8_string(p.propq,
2137
0
                                            &pmdprops, sizeof(mdprops)))
2138
0
                return 0;
2139
0
        }
2140
0
    }
2141
2142
0
    if (p.pad != NULL) {
2143
0
        const char *err_extra_text = NULL;
2144
2145
0
        if (p.pad->data_type != OSSL_PARAM_UTF8_STRING) {
2146
            /* Support for legacy pad mode number */
2147
0
            if (!OSSL_PARAM_get_int(p.pad, &pad_mode))
2148
0
                return 0;
2149
0
        } else {
2150
0
            int i;
2151
2152
0
            if (p.pad->data == NULL)
2153
0
                return 0;
2154
2155
0
            for (i = 0; padding_item[i].id != 0; i++) {
2156
0
                if (strcmp(p.pad->data, padding_item[i].ptr) == 0) {
2157
0
                    pad_mode = padding_item[i].id;
2158
0
                    break;
2159
0
                }
2160
0
            }
2161
0
        }
2162
2163
0
        switch (pad_mode) {
2164
0
        case RSA_PKCS1_OAEP_PADDING:
2165
            /*
2166
             * OAEP padding is for asymmetric cipher only so is not compatible
2167
             * with signature use.
2168
             */
2169
0
            err_extra_text = "OAEP padding not allowed for signing / verifying";
2170
0
            goto bad_pad;
2171
0
        case RSA_PKCS1_PSS_PADDING:
2172
0
            if ((prsactx->operation
2173
0
                 & (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_SIGNMSG
2174
0
                    | EVP_PKEY_OP_VERIFY | EVP_PKEY_OP_VERIFYMSG)) == 0) {
2175
0
                err_extra_text =
2176
0
                    "PSS padding only allowed for sign and verify operations";
2177
0
                goto bad_pad;
2178
0
            }
2179
0
            break;
2180
0
        case RSA_PKCS1_PADDING:
2181
0
            err_extra_text = "PKCS#1 padding not allowed with RSA-PSS";
2182
0
            goto cont;
2183
0
        case RSA_NO_PADDING:
2184
0
            err_extra_text = "No padding not allowed with RSA-PSS";
2185
0
            goto cont;
2186
0
        case RSA_X931_PADDING:
2187
#ifdef FIPS_MODULE
2188
            /* X9.31 only allows sizes of 1024 + 256 * s (bits) */
2189
            if ((RSA_bits(prsactx->rsa) & 0xFF) != 0) {
2190
                ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
2191
                return 0;
2192
            }
2193
            /* RSA Signing with X9.31 padding is not allowed in FIPS 140-3 */
2194
            if (!rsa_x931_padding_allowed(prsactx))
2195
                return 0;
2196
#endif
2197
0
            err_extra_text = "X.931 padding not allowed with RSA-PSS";
2198
0
        cont:
2199
0
            if (RSA_test_flags(prsactx->rsa,
2200
0
                               RSA_FLAG_TYPE_MASK) == RSA_FLAG_TYPE_RSA)
2201
0
                break;
2202
            /* FALLTHRU */
2203
0
        default:
2204
0
        bad_pad:
2205
0
            if (err_extra_text == NULL)
2206
0
                ERR_raise(ERR_LIB_PROV,
2207
0
                          PROV_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
2208
0
            else
2209
0
                ERR_raise_data(ERR_LIB_PROV,
2210
0
                               PROV_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE,
2211
0
                               err_extra_text);
2212
0
            return 0;
2213
0
        }
2214
0
    }
2215
2216
0
    if (p.slen != NULL) {
2217
0
        if (pad_mode != RSA_PKCS1_PSS_PADDING) {
2218
0
            ERR_raise_data(ERR_LIB_PROV, PROV_R_NOT_SUPPORTED,
2219
0
                           "PSS saltlen can only be specified if "
2220
0
                           "PSS padding has been specified first");
2221
0
            return 0;
2222
0
        }
2223
2224
0
        if (p.slen->data_type != OSSL_PARAM_UTF8_STRING) {
2225
            /* Support for legacy pad mode number */
2226
0
            if (!OSSL_PARAM_get_int(p.slen, &saltlen))
2227
0
                return 0;
2228
0
        } else {
2229
0
            if (strcmp(p.slen->data, OSSL_PKEY_RSA_PSS_SALT_LEN_DIGEST) == 0)
2230
0
                saltlen = RSA_PSS_SALTLEN_DIGEST;
2231
0
            else if (strcmp(p.slen->data, OSSL_PKEY_RSA_PSS_SALT_LEN_MAX) == 0)
2232
0
                saltlen = RSA_PSS_SALTLEN_MAX;
2233
0
            else if (strcmp(p.slen->data, OSSL_PKEY_RSA_PSS_SALT_LEN_AUTO) == 0)
2234
0
                saltlen = RSA_PSS_SALTLEN_AUTO;
2235
0
            else if (strcmp(p.slen->data, OSSL_PKEY_RSA_PSS_SALT_LEN_AUTO_DIGEST_MAX) == 0)
2236
0
                saltlen = RSA_PSS_SALTLEN_AUTO_DIGEST_MAX;
2237
0
            else
2238
0
                saltlen = atoi(p.slen->data);
2239
0
        }
2240
2241
        /*
2242
         * RSA_PSS_SALTLEN_AUTO_DIGEST_MAX seems curiously named in this check.
2243
         * Contrary to what it's name suggests, it's the currently lowest
2244
         * saltlen number possible.
2245
         */
2246
0
        if (saltlen < RSA_PSS_SALTLEN_AUTO_DIGEST_MAX) {
2247
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH);
2248
0
            return 0;
2249
0
        }
2250
2251
0
        if (rsa_pss_restricted(prsactx)) {
2252
0
            switch (saltlen) {
2253
0
            case RSA_PSS_SALTLEN_AUTO:
2254
0
            case RSA_PSS_SALTLEN_AUTO_DIGEST_MAX:
2255
0
                if ((prsactx->operation
2256
0
                     & (EVP_PKEY_OP_VERIFY | EVP_PKEY_OP_VERIFYMSG)) == 0) {
2257
0
                    ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH,
2258
0
                                   "Cannot use autodetected salt length");
2259
0
                    return 0;
2260
0
                }
2261
0
                break;
2262
0
            case RSA_PSS_SALTLEN_DIGEST:
2263
0
                if (prsactx->min_saltlen > EVP_MD_get_size(prsactx->md)) {
2264
0
                    ERR_raise_data(ERR_LIB_PROV,
2265
0
                                   PROV_R_PSS_SALTLEN_TOO_SMALL,
2266
0
                                   "Should be more than %d, but would be "
2267
0
                                   "set to match digest size (%d)",
2268
0
                                   prsactx->min_saltlen,
2269
0
                                   EVP_MD_get_size(prsactx->md));
2270
0
                    return 0;
2271
0
                }
2272
0
                break;
2273
0
            default:
2274
0
                if (saltlen >= 0 && saltlen < prsactx->min_saltlen) {
2275
0
                    ERR_raise_data(ERR_LIB_PROV,
2276
0
                                   PROV_R_PSS_SALTLEN_TOO_SMALL,
2277
0
                                   "Should be more than %d, "
2278
0
                                   "but would be set to %d",
2279
0
                                   prsactx->min_saltlen, saltlen);
2280
0
                    return 0;
2281
0
                }
2282
0
            }
2283
0
        }
2284
0
    }
2285
2286
0
    if (p.mgf1 != NULL) {
2287
0
        pmgf1mdname = mgf1mdname;
2288
0
        if (!OSSL_PARAM_get_utf8_string(p.mgf1, &pmgf1mdname, sizeof(mgf1mdname)))
2289
0
            return 0;
2290
2291
0
        if (p.mgf1pq != NULL) {
2292
0
            pmgf1mdprops = mgf1mdprops;
2293
0
            if (!OSSL_PARAM_get_utf8_string(p.mgf1pq,
2294
0
                                            &pmgf1mdprops, sizeof(mgf1mdprops)))
2295
0
                return 0;
2296
0
        }
2297
2298
0
        if (pad_mode != RSA_PKCS1_PSS_PADDING) {
2299
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MGF1_MD);
2300
0
            return  0;
2301
0
        }
2302
0
    }
2303
2304
0
    prsactx->saltlen = saltlen;
2305
0
    prsactx->pad_mode = pad_mode;
2306
2307
0
    if (prsactx->md == NULL && pmdname == NULL
2308
0
        && pad_mode == RSA_PKCS1_PSS_PADDING)
2309
0
        pmdname = RSA_DEFAULT_DIGEST_NAME;
2310
2311
0
    if (pmgf1mdname != NULL
2312
0
        && !rsa_setup_mgf1_md(prsactx, pmgf1mdname, pmgf1mdprops))
2313
0
        return 0;
2314
2315
0
    if (pmdname != NULL) {
2316
0
        if (!rsa_setup_md(prsactx, pmdname, pmdprops, "RSA Sign Set Ctx"))
2317
0
            return 0;
2318
0
    } else {
2319
0
        if (!rsa_check_padding(prsactx, NULL, NULL, prsactx->mdnid))
2320
0
            return 0;
2321
0
    }
2322
0
    return 1;
2323
0
}
2324
2325
static const OSSL_PARAM *rsa_settable_ctx_params(void *vprsactx,
2326
                                                 ossl_unused void *provctx)
2327
0
{
2328
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
2329
2330
0
    if (prsactx != NULL && !prsactx->flag_allow_md)
2331
0
        return rsa_set_ctx_params_no_digest_list;
2332
0
    return rsa_set_ctx_params_list;
2333
0
}
2334
2335
static int rsa_get_ctx_md_params(void *vprsactx, OSSL_PARAM *params)
2336
0
{
2337
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
2338
2339
0
    if (prsactx->mdctx == NULL)
2340
0
        return 0;
2341
2342
0
    return EVP_MD_CTX_get_params(prsactx->mdctx, params);
2343
0
}
2344
2345
static const OSSL_PARAM *rsa_gettable_ctx_md_params(void *vprsactx)
2346
0
{
2347
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
2348
2349
0
    if (prsactx->md == NULL)
2350
0
        return 0;
2351
2352
0
    return EVP_MD_gettable_ctx_params(prsactx->md);
2353
0
}
2354
2355
static int rsa_set_ctx_md_params(void *vprsactx, const OSSL_PARAM params[])
2356
0
{
2357
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
2358
2359
0
    if (prsactx->mdctx == NULL)
2360
0
        return 0;
2361
2362
0
    return EVP_MD_CTX_set_params(prsactx->mdctx, params);
2363
0
}
2364
2365
static const OSSL_PARAM *rsa_settable_ctx_md_params(void *vprsactx)
2366
0
{
2367
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
2368
2369
0
    if (prsactx->md == NULL)
2370
0
        return 0;
2371
2372
0
    return EVP_MD_settable_ctx_params(prsactx->md);
2373
0
}
2374
2375
const OSSL_DISPATCH ossl_rsa_signature_functions[] = {
2376
    { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))rsa_newctx },
2377
    { OSSL_FUNC_SIGNATURE_SIGN_INIT, (void (*)(void))rsa_sign_init },
2378
    { OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))rsa_sign },
2379
    { OSSL_FUNC_SIGNATURE_VERIFY_INIT, (void (*)(void))rsa_verify_init },
2380
    { OSSL_FUNC_SIGNATURE_VERIFY, (void (*)(void))rsa_verify },
2381
    { OSSL_FUNC_SIGNATURE_VERIFY_RECOVER_INIT,
2382
      (void (*)(void))rsa_verify_recover_init },
2383
    { OSSL_FUNC_SIGNATURE_VERIFY_RECOVER,
2384
      (void (*)(void))rsa_verify_recover },
2385
    { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT,
2386
      (void (*)(void))rsa_digest_sign_init },
2387
    { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE,
2388
      (void (*)(void))rsa_digest_sign_update },
2389
    { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL,
2390
      (void (*)(void))rsa_digest_sign_final },
2391
    { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT,
2392
      (void (*)(void))rsa_digest_verify_init },
2393
    { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE,
2394
      (void (*)(void))rsa_digest_verify_update },
2395
    { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL,
2396
      (void (*)(void))rsa_digest_verify_final },
2397
    { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))rsa_freectx },
2398
    { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))rsa_dupctx },
2399
    { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, (void (*)(void))rsa_get_ctx_params },
2400
    { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS,
2401
      (void (*)(void))rsa_gettable_ctx_params },
2402
    { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, (void (*)(void))rsa_set_ctx_params },
2403
    { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS,
2404
      (void (*)(void))rsa_settable_ctx_params },
2405
    { OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS,
2406
      (void (*)(void))rsa_get_ctx_md_params },
2407
    { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS,
2408
      (void (*)(void))rsa_gettable_ctx_md_params },
2409
    { OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS,
2410
      (void (*)(void))rsa_set_ctx_md_params },
2411
    { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS,
2412
      (void (*)(void))rsa_settable_ctx_md_params },
2413
    OSSL_DISPATCH_END
2414
};
2415
2416
/* ------------------------------------------------------------------ */
2417
2418
/*
2419
 * So called sigalgs (composite RSA+hash) implemented below.  They
2420
 * are pretty much hard coded, and rely on the hash implementation
2421
 * being available as per what OPENSSL_NO_ macros allow.
2422
 */
2423
2424
static OSSL_FUNC_signature_query_key_types_fn rsa_sigalg_query_key_types;
2425
static OSSL_FUNC_signature_settable_ctx_params_fn rsa_sigalg_settable_ctx_params;
2426
static OSSL_FUNC_signature_set_ctx_params_fn rsa_sigalg_set_ctx_params;
2427
2428
/*
2429
 * rsa_sigalg_signverify_init() is almost like rsa_digest_signverify_init(),
2430
 * just doesn't allow fetching an MD from whatever the user chooses.
2431
 */
2432
static int rsa_sigalg_signverify_init(void *vprsactx, void *vrsa,
2433
                                      OSSL_FUNC_signature_set_ctx_params_fn *set_ctx_params,
2434
                                      const OSSL_PARAM params[],
2435
                                      const char *mdname,
2436
                                      int operation, int pad_mode,
2437
                                      const char *desc)
2438
0
{
2439
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
2440
2441
0
    if (!ossl_prov_is_running())
2442
0
        return 0;
2443
2444
0
    if (!rsa_signverify_init(prsactx, vrsa, set_ctx_params, params, operation,
2445
0
                             desc))
2446
0
        return 0;
2447
2448
    /* PSS is currently not supported as a sigalg */
2449
0
    if (prsactx->pad_mode == RSA_PKCS1_PSS_PADDING) {
2450
0
        ERR_raise(ERR_LIB_RSA, PROV_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
2451
0
        return 0;
2452
0
    }
2453
2454
0
    if (!rsa_setup_md(prsactx, mdname, NULL, desc))
2455
0
        return 0;
2456
2457
0
    prsactx->pad_mode = pad_mode;
2458
0
    prsactx->flag_sigalg = 1;
2459
0
    prsactx->flag_allow_md = 0;
2460
2461
0
    if (prsactx->mdctx == NULL) {
2462
0
        prsactx->mdctx = EVP_MD_CTX_new();
2463
0
        if (prsactx->mdctx == NULL)
2464
0
            goto error;
2465
0
    }
2466
2467
0
    if (!EVP_DigestInit_ex2(prsactx->mdctx, prsactx->md, params))
2468
0
        goto error;
2469
2470
0
    return 1;
2471
2472
0
 error:
2473
0
    EVP_MD_CTX_free(prsactx->mdctx);
2474
0
    prsactx->mdctx = NULL;
2475
0
    return 0;
2476
0
}
2477
2478
static const char **rsa_sigalg_query_key_types(void)
2479
0
{
2480
0
    static const char *keytypes[] = { "RSA", NULL };
2481
2482
0
    return keytypes;
2483
0
}
2484
2485
/* Machine generated by util/perl/OpenSSL/paramnames.pm */
2486
#ifndef rsa_sigalg_set_ctx_params_list
2487
static const OSSL_PARAM rsa_sigalg_set_ctx_params_list[] = {
2488
    OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_SIGNATURE, NULL, 0),
2489
    OSSL_PARAM_END
2490
};
2491
#endif
2492
2493
#ifndef rsa_sigalg_set_ctx_params_st
2494
struct rsa_sigalg_set_ctx_params_st {
2495
    OSSL_PARAM *sig;
2496
};
2497
#endif
2498
2499
#ifndef rsa_sigalg_set_ctx_params_decoder
2500
static int rsa_sigalg_set_ctx_params_decoder
2501
    (const OSSL_PARAM *p, struct rsa_sigalg_set_ctx_params_st *r)
2502
0
{
2503
0
    const char *s;
2504
2505
0
    memset(r, 0, sizeof(*r));
2506
0
    if (p != NULL)
2507
0
        for (; (s = p->key) != NULL; p++)
2508
0
            if (ossl_likely(strcmp("signature", s + 0) == 0)) {
2509
                /* SIGNATURE_PARAM_SIGNATURE */
2510
0
                if (ossl_unlikely(r->sig != NULL)) {
2511
0
                    ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
2512
0
                                   "param %s is repeated", s);
2513
0
                    return 0;
2514
0
                }
2515
0
                r->sig = (OSSL_PARAM *)p;
2516
0
            }
2517
0
    return 1;
2518
0
}
2519
#endif
2520
/* End of machine generated */
2521
2522
static const OSSL_PARAM *rsa_sigalg_settable_ctx_params(void *vprsactx,
2523
                                                        ossl_unused void *provctx)
2524
0
{
2525
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
2526
2527
0
    if (prsactx != NULL && prsactx->operation == EVP_PKEY_OP_VERIFYMSG)
2528
0
        return rsa_sigalg_set_ctx_params_list;
2529
0
    return NULL;
2530
0
}
2531
2532
static int rsa_sigalg_set_ctx_params(void *vprsactx, const OSSL_PARAM params[])
2533
0
{
2534
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
2535
0
    struct rsa_sigalg_set_ctx_params_st p;
2536
2537
0
    if (prsactx == NULL || !rsa_sigalg_set_ctx_params_decoder(params, &p))
2538
0
        return 0;
2539
2540
0
    if (prsactx->operation == EVP_PKEY_OP_VERIFYMSG) {
2541
0
        if (p.sig != NULL) {
2542
0
            OPENSSL_free(prsactx->sig);
2543
0
            prsactx->sig = NULL;
2544
0
            prsactx->siglen = 0;
2545
0
            if (!OSSL_PARAM_get_octet_string(p.sig, (void **)&prsactx->sig,
2546
0
                                             0, &prsactx->siglen))
2547
0
                return 0;
2548
0
        }
2549
0
    }
2550
0
    return 1;
2551
0
}
2552
2553
#define IMPL_RSA_SIGALG(md, MD)                                         \
2554
    static OSSL_FUNC_signature_sign_init_fn rsa_##md##_sign_init;       \
2555
    static OSSL_FUNC_signature_sign_message_init_fn                     \
2556
        rsa_##md##_sign_message_init;                                   \
2557
    static OSSL_FUNC_signature_verify_init_fn rsa_##md##_verify_init;   \
2558
    static OSSL_FUNC_signature_verify_message_init_fn                   \
2559
        rsa_##md##_verify_message_init;                                 \
2560
                                                                        \
2561
    static int                                                          \
2562
    rsa_##md##_sign_init(void *vprsactx, void *vrsa,                    \
2563
                         const OSSL_PARAM params[])                     \
2564
0
    {                                                                   \
2565
0
        static const char desc[] = "RSA Sigalg Sign Init";              \
2566
0
                                                                        \
2567
0
        return rsa_sigalg_signverify_init(vprsactx, vrsa,               \
2568
0
                                          rsa_sigalg_set_ctx_params,    \
2569
0
                                          params, MD,                  \
2570
0
                                          EVP_PKEY_OP_SIGN,             \
2571
0
                                          RSA_PKCS1_PADDING,            \
2572
0
                                          desc);                        \
2573
0
    }                                                                   \
Unexecuted instantiation: rsa_sig.c:rsa_ripemd160_sign_init
Unexecuted instantiation: rsa_sig.c:rsa_sha1_sign_init
Unexecuted instantiation: rsa_sig.c:rsa_sha224_sign_init
Unexecuted instantiation: rsa_sig.c:rsa_sha256_sign_init
Unexecuted instantiation: rsa_sig.c:rsa_sha384_sign_init
Unexecuted instantiation: rsa_sig.c:rsa_sha512_sign_init
Unexecuted instantiation: rsa_sig.c:rsa_sha512_224_sign_init
Unexecuted instantiation: rsa_sig.c:rsa_sha512_256_sign_init
Unexecuted instantiation: rsa_sig.c:rsa_sha3_224_sign_init
Unexecuted instantiation: rsa_sig.c:rsa_sha3_256_sign_init
Unexecuted instantiation: rsa_sig.c:rsa_sha3_384_sign_init
Unexecuted instantiation: rsa_sig.c:rsa_sha3_512_sign_init
Unexecuted instantiation: rsa_sig.c:rsa_sm3_sign_init
2574
                                                                        \
2575
    static int                                                          \
2576
    rsa_##md##_sign_message_init(void *vprsactx, void *vrsa,            \
2577
                                 const OSSL_PARAM params[])             \
2578
0
    {                                                                   \
2579
0
        static const char desc[] = "RSA Sigalg Sign Message Init";      \
2580
0
                                                                        \
2581
0
        return rsa_sigalg_signverify_init(vprsactx, vrsa,               \
2582
0
                                          rsa_sigalg_set_ctx_params,    \
2583
0
                                          params, MD,                  \
2584
0
                                          EVP_PKEY_OP_SIGNMSG,          \
2585
0
                                          RSA_PKCS1_PADDING,            \
2586
0
                                          desc);                        \
2587
0
    }                                                                   \
Unexecuted instantiation: rsa_sig.c:rsa_ripemd160_sign_message_init
Unexecuted instantiation: rsa_sig.c:rsa_sha1_sign_message_init
Unexecuted instantiation: rsa_sig.c:rsa_sha224_sign_message_init
Unexecuted instantiation: rsa_sig.c:rsa_sha256_sign_message_init
Unexecuted instantiation: rsa_sig.c:rsa_sha384_sign_message_init
Unexecuted instantiation: rsa_sig.c:rsa_sha512_sign_message_init
Unexecuted instantiation: rsa_sig.c:rsa_sha512_224_sign_message_init
Unexecuted instantiation: rsa_sig.c:rsa_sha512_256_sign_message_init
Unexecuted instantiation: rsa_sig.c:rsa_sha3_224_sign_message_init
Unexecuted instantiation: rsa_sig.c:rsa_sha3_256_sign_message_init
Unexecuted instantiation: rsa_sig.c:rsa_sha3_384_sign_message_init
Unexecuted instantiation: rsa_sig.c:rsa_sha3_512_sign_message_init
Unexecuted instantiation: rsa_sig.c:rsa_sm3_sign_message_init
2588
                                                                        \
2589
    static int                                                          \
2590
    rsa_##md##_verify_init(void *vprsactx, void *vrsa,                  \
2591
                           const OSSL_PARAM params[])                   \
2592
0
    {                                                                   \
2593
0
        static const char desc[] = "RSA Sigalg Verify Init";            \
2594
0
                                                                        \
2595
0
        return rsa_sigalg_signverify_init(vprsactx, vrsa,               \
2596
0
                                          rsa_sigalg_set_ctx_params,    \
2597
0
                                          params, MD,                  \
2598
0
                                          EVP_PKEY_OP_VERIFY,           \
2599
0
                                          RSA_PKCS1_PADDING,            \
2600
0
                                          desc);                        \
2601
0
    }                                                                   \
Unexecuted instantiation: rsa_sig.c:rsa_ripemd160_verify_init
Unexecuted instantiation: rsa_sig.c:rsa_sha1_verify_init
Unexecuted instantiation: rsa_sig.c:rsa_sha224_verify_init
Unexecuted instantiation: rsa_sig.c:rsa_sha256_verify_init
Unexecuted instantiation: rsa_sig.c:rsa_sha384_verify_init
Unexecuted instantiation: rsa_sig.c:rsa_sha512_verify_init
Unexecuted instantiation: rsa_sig.c:rsa_sha512_224_verify_init
Unexecuted instantiation: rsa_sig.c:rsa_sha512_256_verify_init
Unexecuted instantiation: rsa_sig.c:rsa_sha3_224_verify_init
Unexecuted instantiation: rsa_sig.c:rsa_sha3_256_verify_init
Unexecuted instantiation: rsa_sig.c:rsa_sha3_384_verify_init
Unexecuted instantiation: rsa_sig.c:rsa_sha3_512_verify_init
Unexecuted instantiation: rsa_sig.c:rsa_sm3_verify_init
2602
                                                                        \
2603
    static int                                                          \
2604
    rsa_##md##_verify_recover_init(void *vprsactx, void *vrsa,          \
2605
                                   const OSSL_PARAM params[])           \
2606
0
    {                                                                   \
2607
0
        static const char desc[] = "RSA Sigalg Verify Recover Init";    \
2608
0
                                                                        \
2609
0
        return rsa_sigalg_signverify_init(vprsactx, vrsa,               \
2610
0
                                          rsa_sigalg_set_ctx_params,    \
2611
0
                                          params, MD,                  \
2612
0
                                          EVP_PKEY_OP_VERIFYRECOVER,    \
2613
0
                                          RSA_PKCS1_PADDING,            \
2614
0
                                          desc);                        \
2615
0
    }                                                                   \
Unexecuted instantiation: rsa_sig.c:rsa_ripemd160_verify_recover_init
Unexecuted instantiation: rsa_sig.c:rsa_sha1_verify_recover_init
Unexecuted instantiation: rsa_sig.c:rsa_sha224_verify_recover_init
Unexecuted instantiation: rsa_sig.c:rsa_sha256_verify_recover_init
Unexecuted instantiation: rsa_sig.c:rsa_sha384_verify_recover_init
Unexecuted instantiation: rsa_sig.c:rsa_sha512_verify_recover_init
Unexecuted instantiation: rsa_sig.c:rsa_sha512_224_verify_recover_init
Unexecuted instantiation: rsa_sig.c:rsa_sha512_256_verify_recover_init
Unexecuted instantiation: rsa_sig.c:rsa_sha3_224_verify_recover_init
Unexecuted instantiation: rsa_sig.c:rsa_sha3_256_verify_recover_init
Unexecuted instantiation: rsa_sig.c:rsa_sha3_384_verify_recover_init
Unexecuted instantiation: rsa_sig.c:rsa_sha3_512_verify_recover_init
Unexecuted instantiation: rsa_sig.c:rsa_sm3_verify_recover_init
2616
                                                                        \
2617
    static int                                                          \
2618
    rsa_##md##_verify_message_init(void *vprsactx, void *vrsa,          \
2619
                                   const OSSL_PARAM params[])           \
2620
0
    {                                                                   \
2621
0
        static const char desc[] = "RSA Sigalg Verify Message Init";    \
2622
0
                                                                        \
2623
0
        return rsa_sigalg_signverify_init(vprsactx, vrsa,               \
2624
0
                                          rsa_sigalg_set_ctx_params,    \
2625
0
                                          params, MD,                  \
2626
0
                                          EVP_PKEY_OP_VERIFYMSG,        \
2627
0
                                          RSA_PKCS1_PADDING,            \
2628
0
                                          desc);                        \
2629
0
    }                                                                   \
Unexecuted instantiation: rsa_sig.c:rsa_ripemd160_verify_message_init
Unexecuted instantiation: rsa_sig.c:rsa_sha1_verify_message_init
Unexecuted instantiation: rsa_sig.c:rsa_sha224_verify_message_init
Unexecuted instantiation: rsa_sig.c:rsa_sha256_verify_message_init
Unexecuted instantiation: rsa_sig.c:rsa_sha384_verify_message_init
Unexecuted instantiation: rsa_sig.c:rsa_sha512_verify_message_init
Unexecuted instantiation: rsa_sig.c:rsa_sha512_224_verify_message_init
Unexecuted instantiation: rsa_sig.c:rsa_sha512_256_verify_message_init
Unexecuted instantiation: rsa_sig.c:rsa_sha3_224_verify_message_init
Unexecuted instantiation: rsa_sig.c:rsa_sha3_256_verify_message_init
Unexecuted instantiation: rsa_sig.c:rsa_sha3_384_verify_message_init
Unexecuted instantiation: rsa_sig.c:rsa_sha3_512_verify_message_init
Unexecuted instantiation: rsa_sig.c:rsa_sm3_verify_message_init
2630
                                                                        \
2631
    const OSSL_DISPATCH ossl_rsa_##md##_signature_functions[] = {       \
2632
        { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))rsa_newctx },     \
2633
        { OSSL_FUNC_SIGNATURE_SIGN_INIT,                                \
2634
          (void (*)(void))rsa_##md##_sign_init },                       \
2635
        { OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))rsa_sign },         \
2636
        { OSSL_FUNC_SIGNATURE_SIGN_MESSAGE_INIT,                        \
2637
          (void (*)(void))rsa_##md##_sign_message_init },               \
2638
        { OSSL_FUNC_SIGNATURE_SIGN_MESSAGE_UPDATE,                      \
2639
          (void (*)(void))rsa_signverify_message_update },              \
2640
        { OSSL_FUNC_SIGNATURE_SIGN_MESSAGE_FINAL,                       \
2641
          (void (*)(void))rsa_sign_message_final },                     \
2642
        { OSSL_FUNC_SIGNATURE_VERIFY_INIT,                              \
2643
          (void (*)(void))rsa_##md##_verify_init },                     \
2644
        { OSSL_FUNC_SIGNATURE_VERIFY,                                   \
2645
          (void (*)(void))rsa_verify },                                 \
2646
        { OSSL_FUNC_SIGNATURE_VERIFY_MESSAGE_INIT,                      \
2647
          (void (*)(void))rsa_##md##_verify_message_init },             \
2648
        { OSSL_FUNC_SIGNATURE_VERIFY_MESSAGE_UPDATE,                    \
2649
          (void (*)(void))rsa_signverify_message_update },              \
2650
        { OSSL_FUNC_SIGNATURE_VERIFY_MESSAGE_FINAL,                     \
2651
          (void (*)(void))rsa_verify_message_final },                   \
2652
        { OSSL_FUNC_SIGNATURE_VERIFY_RECOVER_INIT,                      \
2653
          (void (*)(void))rsa_##md##_verify_recover_init },             \
2654
        { OSSL_FUNC_SIGNATURE_VERIFY_RECOVER,                           \
2655
          (void (*)(void))rsa_verify_recover },                         \
2656
        { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))rsa_freectx },   \
2657
        { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))rsa_dupctx },     \
2658
        { OSSL_FUNC_SIGNATURE_QUERY_KEY_TYPES,                          \
2659
          (void (*)(void))rsa_sigalg_query_key_types },                 \
2660
        { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS,                           \
2661
          (void (*)(void))rsa_get_ctx_params },                         \
2662
        { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS,                      \
2663
          (void (*)(void))rsa_gettable_ctx_params },                    \
2664
        { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS,                           \
2665
          (void (*)(void))rsa_sigalg_set_ctx_params },                  \
2666
        { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS,                      \
2667
          (void (*)(void))rsa_sigalg_settable_ctx_params },             \
2668
        OSSL_DISPATCH_END                                               \
2669
    }
2670
2671
#if !defined(OPENSSL_NO_RMD160) && !defined(FIPS_MODULE)
2672
IMPL_RSA_SIGALG(ripemd160, "RIPEMD160");
2673
#endif
2674
IMPL_RSA_SIGALG(sha1, "SHA1");
2675
IMPL_RSA_SIGALG(sha224, "SHA2-224");
2676
IMPL_RSA_SIGALG(sha256, "SHA2-256");
2677
IMPL_RSA_SIGALG(sha384, "SHA2-384");
2678
IMPL_RSA_SIGALG(sha512, "SHA2-512");
2679
IMPL_RSA_SIGALG(sha512_224, "SHA2-512/224");
2680
IMPL_RSA_SIGALG(sha512_256, "SHA2-512/256");
2681
IMPL_RSA_SIGALG(sha3_224, "SHA3-224");
2682
IMPL_RSA_SIGALG(sha3_256, "SHA3-256");
2683
IMPL_RSA_SIGALG(sha3_384, "SHA3-384");
2684
IMPL_RSA_SIGALG(sha3_512, "SHA3-512");
2685
#if !defined(OPENSSL_NO_SM3) && !defined(FIPS_MODULE)
2686
IMPL_RSA_SIGALG(sm3, "SM3");
2687
#endif