Coverage Report

Created: 2026-10-28 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl40/providers/implementations/signature/rsa_sig.c
Line
Count
Source
1
/*
2
 * Copyright 2019-2026 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
/*
11
 * RSA low level APIs are deprecated for public use, but still ok for
12
 * internal use.
13
 */
14
#include "internal/deprecated.h"
15
16
#include <string.h>
17
#include <openssl/crypto.h>
18
#include <openssl/core_dispatch.h>
19
#include <openssl/core_names.h>
20
#include <openssl/err.h>
21
#include <openssl/obj_mac.h>
22
#include <openssl/rsa.h>
23
#include <openssl/params.h>
24
#include <openssl/evp.h>
25
#include <openssl/proverr.h>
26
#include "internal/cryptlib.h"
27
#include "internal/nelem.h"
28
#include "internal/sizes.h"
29
#include "crypto/rsa.h"
30
#include "prov/providercommon.h"
31
#include "prov/implementations.h"
32
#include "prov/provider_ctx.h"
33
#include "prov/der_rsa.h"
34
#include "prov/securitycheck.h"
35
#include "internal/fips.h"
36
37
#define rsa_set_ctx_params_no_digest_st rsa_set_ctx_params_st
38
39
#include "providers/implementations/signature/rsa_sig.inc"
40
41
0
#define RSA_DEFAULT_DIGEST_NAME OSSL_DIGEST_NAME_SHA1
42
43
static OSSL_FUNC_signature_newctx_fn rsa_newctx;
44
static OSSL_FUNC_signature_sign_init_fn rsa_sign_init;
45
static OSSL_FUNC_signature_verify_init_fn rsa_verify_init;
46
static OSSL_FUNC_signature_verify_recover_init_fn rsa_verify_recover_init;
47
static OSSL_FUNC_signature_sign_fn rsa_sign;
48
static OSSL_FUNC_signature_sign_message_update_fn rsa_signverify_message_update;
49
static OSSL_FUNC_signature_sign_message_final_fn rsa_sign_message_final;
50
static OSSL_FUNC_signature_verify_fn rsa_verify;
51
static OSSL_FUNC_signature_verify_recover_fn rsa_verify_recover;
52
static OSSL_FUNC_signature_verify_message_update_fn rsa_signverify_message_update;
53
static OSSL_FUNC_signature_verify_message_final_fn rsa_verify_message_final;
54
static OSSL_FUNC_signature_digest_sign_init_fn rsa_digest_sign_init;
55
static OSSL_FUNC_signature_digest_sign_update_fn rsa_digest_sign_update;
56
static OSSL_FUNC_signature_digest_sign_final_fn rsa_digest_sign_final;
57
static OSSL_FUNC_signature_digest_verify_init_fn rsa_digest_verify_init;
58
static OSSL_FUNC_signature_digest_verify_update_fn rsa_digest_verify_update;
59
static OSSL_FUNC_signature_digest_verify_final_fn rsa_digest_verify_final;
60
static OSSL_FUNC_signature_freectx_fn rsa_freectx;
61
static OSSL_FUNC_signature_dupctx_fn rsa_dupctx;
62
static OSSL_FUNC_signature_query_key_types_fn rsa_sigalg_query_key_types;
63
static OSSL_FUNC_signature_get_ctx_params_fn rsa_get_ctx_params;
64
static OSSL_FUNC_signature_gettable_ctx_params_fn rsa_gettable_ctx_params;
65
static OSSL_FUNC_signature_set_ctx_params_fn rsa_set_ctx_params;
66
static OSSL_FUNC_signature_settable_ctx_params_fn rsa_settable_ctx_params;
67
static OSSL_FUNC_signature_get_ctx_md_params_fn rsa_get_ctx_md_params;
68
static OSSL_FUNC_signature_gettable_ctx_md_params_fn rsa_gettable_ctx_md_params;
69
static OSSL_FUNC_signature_set_ctx_md_params_fn rsa_set_ctx_md_params;
70
static OSSL_FUNC_signature_settable_ctx_md_params_fn rsa_settable_ctx_md_params;
71
static OSSL_FUNC_signature_set_ctx_params_fn rsa_sigalg_set_ctx_params;
72
static OSSL_FUNC_signature_settable_ctx_params_fn rsa_sigalg_settable_ctx_params;
73
74
static OSSL_ITEM padding_item[] = {
75
    { RSA_PKCS1_PADDING, OSSL_PKEY_RSA_PAD_MODE_PKCSV15 },
76
    { RSA_NO_PADDING, OSSL_PKEY_RSA_PAD_MODE_NONE },
77
    { RSA_X931_PADDING, OSSL_PKEY_RSA_PAD_MODE_X931 },
78
    { RSA_PKCS1_PSS_PADDING, OSSL_PKEY_RSA_PAD_MODE_PSS },
79
    { 0, NULL }
80
};
81
82
/*
83
 * What's passed as an actual key is defined by the KEYMGMT interface.
84
 * We happen to know that our KEYMGMT simply passes RSA structures, so
85
 * we use that here too.
86
 */
87
88
typedef struct {
89
    OSSL_LIB_CTX *libctx;
90
    char *propq;
91
    RSA *rsa;
92
    int operation;
93
94
    /*
95
     * Flag to determine if a full sigalg is run (1) or if a composable
96
     * signature algorithm is run (0).
97
     *
98
     * When a full sigalg is run (1), this currently affects the following
99
     * other flags, which are to remain untouched after their initialization:
100
     *
101
     * - flag_allow_md (initialized to 0)
102
     */
103
    unsigned int flag_sigalg : 1;
104
    /*
105
     * Flag to determine if the hash function can be changed (1) or not (0)
106
     * Because it's dangerous to change during a DigestSign or DigestVerify
107
     * operation, this flag is cleared by their Init function, and set again
108
     * by their Final function.
109
     * Implementations of full sigalgs (such as RSA-SHA256) hard-code this
110
     * flag to not allow changes (0).
111
     */
112
    unsigned int flag_allow_md : 1;
113
    unsigned int mgf1_md_set : 1;
114
    /*
115
     * Flags to say what are the possible next external calls in what
116
     * constitutes the life cycle of an algorithm.  The relevant calls are:
117
     * - init
118
     * - update
119
     * - final
120
     * - oneshot
121
     * All other external calls are regarded as utilitarian and are allowed
122
     * at any time (they may be affected by other flags, like flag_allow_md,
123
     * though).
124
     */
125
    unsigned int flag_allow_update : 1;
126
    unsigned int flag_allow_final : 1;
127
    unsigned int flag_allow_oneshot : 1;
128
129
    /* main digest */
130
    EVP_MD *md;
131
    EVP_MD_CTX *mdctx;
132
    int mdnid;
133
    char mdname[OSSL_MAX_NAME_SIZE]; /* Purely informational */
134
135
    /* RSA padding mode */
136
    int pad_mode;
137
    /* message digest for MGF1 */
138
    EVP_MD *mgf1_md;
139
    int mgf1_mdnid;
140
    char mgf1_mdname[OSSL_MAX_NAME_SIZE]; /* Purely informational */
141
    /* PSS salt length */
142
    int saltlen;
143
    /* Minimum salt length or -1 if no PSS parameter restriction */
144
    int min_saltlen;
145
146
    /* Signature, for verification */
147
    unsigned char *sig;
148
    size_t siglen;
149
150
#ifdef FIPS_MODULE
151
    /*
152
     * FIPS 140-3 IG 2.4.B mandates that verification based on a digest of a
153
     * message is not permitted.  However, signing based on a digest is still
154
     * permitted.
155
     */
156
    int verify_message;
157
#endif
158
159
    /* Temp buffer */
160
    unsigned char *tbuf;
161
162
    OSSL_FIPS_IND_DECLARE
163
} PROV_RSA_CTX;
164
165
/* True if PSS parameters are restricted */
166
61.4k
#define rsa_pss_restricted(prsactx) (prsactx->min_saltlen != -1)
167
168
static int rsa_get_md_size(const PROV_RSA_CTX *prsactx)
169
34.5k
{
170
34.5k
    int md_size;
171
172
34.5k
    if (prsactx->md != NULL) {
173
34.5k
        md_size = EVP_MD_get_size(prsactx->md);
174
34.5k
        if (md_size <= 0)
175
0
            return 0;
176
34.5k
        return md_size;
177
34.5k
    }
178
0
    return 0;
179
34.5k
}
180
181
static int rsa_check_padding(const PROV_RSA_CTX *prsactx,
182
    const char *mdname, const char *mgf1_mdname,
183
    int mdnid)
184
118k
{
185
118k
    switch (prsactx->pad_mode) {
186
0
    case RSA_NO_PADDING:
187
0
        if (mdname != NULL || mdnid != NID_undef) {
188
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_PADDING_MODE);
189
0
            return 0;
190
0
        }
191
0
        break;
192
0
    case RSA_X931_PADDING:
193
0
        if (RSA_X931_hash_id(mdnid) == -1) {
194
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_X931_DIGEST);
195
0
            return 0;
196
0
        }
197
0
        break;
198
56.8k
    case RSA_PKCS1_PSS_PADDING:
199
56.8k
        if (rsa_pss_restricted(prsactx))
200
3.12k
            if ((mdname != NULL && !EVP_MD_is_a(prsactx->md, mdname))
201
3.09k
                || (mgf1_mdname != NULL
202
766
                    && !EVP_MD_is_a(prsactx->mgf1_md, mgf1_mdname))) {
203
30
                ERR_raise(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED);
204
30
                return 0;
205
30
            }
206
56.8k
        break;
207
61.4k
    default:
208
61.4k
        break;
209
118k
    }
210
211
118k
    return 1;
212
118k
}
213
214
static int rsa_check_parameters(PROV_RSA_CTX *prsactx, int min_saltlen)
215
856
{
216
856
    if (prsactx->pad_mode == RSA_PKCS1_PSS_PADDING) {
217
856
        int max_saltlen;
218
219
        /* See if minimum salt length exceeds maximum possible */
220
856
        max_saltlen = RSA_size(prsactx->rsa) - EVP_MD_get_size(prsactx->md);
221
856
        if ((RSA_bits(prsactx->rsa) & 0x7) == 1)
222
364
            max_saltlen--;
223
856
        if (min_saltlen < 0 || min_saltlen > max_saltlen) {
224
42
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH);
225
42
            return 0;
226
42
        }
227
814
        prsactx->min_saltlen = min_saltlen;
228
814
    }
229
814
    return 1;
230
856
}
231
232
static void *rsa_newctx(void *provctx, const char *propq)
233
62.4k
{
234
62.4k
    PROV_RSA_CTX *prsactx = NULL;
235
62.4k
    char *propq_copy = NULL;
236
237
62.4k
    if (!ossl_prov_is_running())
238
0
        return NULL;
239
240
#ifdef FIPS_MODULE
241
    if (!ossl_deferred_self_test(PROV_LIBCTX_OF(provctx),
242
            ST_ID_SIG_RSA_SHA256))
243
        return NULL;
244
#endif
245
246
62.4k
    if ((prsactx = OPENSSL_zalloc(sizeof(PROV_RSA_CTX))) == NULL
247
62.4k
        || (propq != NULL
248
0
            && (propq_copy = OPENSSL_strdup(propq)) == NULL)) {
249
0
        OPENSSL_free(prsactx);
250
0
        return NULL;
251
0
    }
252
253
62.4k
    OSSL_FIPS_IND_INIT(prsactx)
254
62.4k
    prsactx->libctx = PROV_LIBCTX_OF(provctx);
255
62.4k
    prsactx->flag_allow_md = 1;
256
#ifdef FIPS_MODULE
257
    prsactx->verify_message = 1;
258
#endif
259
62.4k
    prsactx->propq = propq_copy;
260
    /* Maximum up to digest length for sign, auto for verify */
261
62.4k
    prsactx->saltlen = RSA_PSS_SALTLEN_AUTO_DIGEST_MAX;
262
62.4k
    prsactx->min_saltlen = -1;
263
62.4k
    return prsactx;
264
62.4k
}
265
266
static int rsa_pss_compute_saltlen(PROV_RSA_CTX *ctx)
267
0
{
268
0
    int saltlen = ctx->saltlen;
269
0
    int saltlenMax = -1;
270
271
    /* FIPS 186-4 section 5 "The RSA Digital Signature Algorithm", subsection
272
     * 5.5 "PKCS #1" says: "For RSASSA-PSS […] the length (in bytes) of the
273
     * salt (sLen) shall satisfy 0 <= sLen <= hLen, where hLen is the length of
274
     * the hash function output block (in bytes)."
275
     *
276
     * Provide a way to use at most the digest length, so that the default does
277
     * not violate FIPS 186-4. */
278
0
    if (saltlen == RSA_PSS_SALTLEN_DIGEST) {
279
0
        if ((saltlen = EVP_MD_get_size(ctx->md)) <= 0) {
280
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST);
281
0
            return -1;
282
0
        }
283
0
    } else if (saltlen == RSA_PSS_SALTLEN_AUTO_DIGEST_MAX) {
284
0
        saltlen = RSA_PSS_SALTLEN_MAX;
285
0
        if ((saltlenMax = EVP_MD_get_size(ctx->md)) <= 0) {
286
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST);
287
0
            return -1;
288
0
        }
289
0
    }
290
0
    if (saltlen == RSA_PSS_SALTLEN_MAX || saltlen == RSA_PSS_SALTLEN_AUTO) {
291
0
        int mdsize, rsasize;
292
293
0
        if ((mdsize = EVP_MD_get_size(ctx->md)) <= 0) {
294
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST);
295
0
            return -1;
296
0
        }
297
0
        if ((rsasize = RSA_size(ctx->rsa)) <= 2 || rsasize - 2 < mdsize) {
298
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
299
0
            return -1;
300
0
        }
301
0
        saltlen = rsasize - mdsize - 2;
302
0
        if ((RSA_bits(ctx->rsa) & 0x7) == 1)
303
0
            saltlen--;
304
0
        if (saltlenMax >= 0 && saltlen > saltlenMax)
305
0
            saltlen = saltlenMax;
306
0
    }
307
0
    if (saltlen < 0) {
308
0
        ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
309
0
        return -1;
310
0
    } else if (saltlen < ctx->min_saltlen) {
311
0
        ERR_raise_data(ERR_LIB_PROV, PROV_R_PSS_SALTLEN_TOO_SMALL,
312
0
            "minimum salt length: %d, actual salt length: %d",
313
0
            ctx->min_saltlen, saltlen);
314
0
        return -1;
315
0
    }
316
0
    return saltlen;
317
0
}
318
319
static unsigned char *rsa_generate_signature_aid(PROV_RSA_CTX *ctx,
320
    unsigned char *aid_buf,
321
    size_t buf_len,
322
    size_t *aid_len)
323
0
{
324
0
    WPACKET pkt;
325
0
    unsigned char *aid = NULL;
326
0
    int saltlen;
327
0
    RSA_PSS_PARAMS_30 pss_params;
328
0
    int ret;
329
330
0
    if (!WPACKET_init_der(&pkt, aid_buf, buf_len)) {
331
0
        ERR_raise(ERR_LIB_PROV, ERR_R_CRYPTO_LIB);
332
0
        return NULL;
333
0
    }
334
335
0
    switch (ctx->pad_mode) {
336
0
    case RSA_PKCS1_PADDING:
337
0
        ret = ossl_DER_w_algorithmIdentifier_MDWithRSAEncryption(&pkt, -1,
338
0
            ctx->mdnid);
339
340
0
        if (ret > 0) {
341
0
            break;
342
0
        } else if (ret == 0) {
343
0
            ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
344
0
            goto cleanup;
345
0
        }
346
0
        ERR_raise_data(ERR_LIB_PROV, ERR_R_UNSUPPORTED,
347
0
            "Algorithm ID generation - md NID: %d",
348
0
            ctx->mdnid);
349
0
        goto cleanup;
350
0
    case RSA_PKCS1_PSS_PADDING:
351
0
        saltlen = rsa_pss_compute_saltlen(ctx);
352
0
        if (saltlen < 0)
353
0
            goto cleanup;
354
0
        if (!ossl_rsa_pss_params_30_set_defaults(&pss_params)
355
0
            || !ossl_rsa_pss_params_30_set_hashalg(&pss_params, ctx->mdnid)
356
0
            || !ossl_rsa_pss_params_30_set_maskgenhashalg(&pss_params,
357
0
                ctx->mgf1_mdnid)
358
0
            || !ossl_rsa_pss_params_30_set_saltlen(&pss_params, saltlen)
359
0
            || !ossl_DER_w_algorithmIdentifier_RSA_PSS(&pkt, -1,
360
0
                RSA_FLAG_TYPE_RSASSAPSS,
361
0
                &pss_params)) {
362
0
            ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
363
0
            goto cleanup;
364
0
        }
365
0
        break;
366
0
    default:
367
0
        ERR_raise_data(ERR_LIB_PROV, ERR_R_UNSUPPORTED,
368
0
            "Algorithm ID generation - pad mode: %d",
369
0
            ctx->pad_mode);
370
0
        goto cleanup;
371
0
    }
372
0
    if (WPACKET_finish(&pkt)) {
373
0
        WPACKET_get_total_written(&pkt, aid_len);
374
0
        aid = WPACKET_get_curr(&pkt);
375
0
    }
376
0
cleanup:
377
0
    WPACKET_cleanup(&pkt);
378
0
    return aid;
379
0
}
380
381
static int rsa_setup_md(PROV_RSA_CTX *ctx, const char *mdname,
382
    const char *mdprops, const char *desc)
383
62.4k
{
384
62.4k
    EVP_MD *md = NULL;
385
386
62.4k
    if (mdprops == NULL)
387
62.4k
        mdprops = ctx->propq;
388
389
62.4k
    if (mdname != NULL) {
390
62.4k
        int md_nid;
391
62.4k
        size_t mdname_len = strlen(mdname);
392
393
62.4k
        md = EVP_MD_fetch(ctx->libctx, mdname, mdprops);
394
395
62.4k
        if (md == NULL) {
396
80
            ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
397
80
                "%s could not be fetched", mdname);
398
80
            goto err;
399
80
        }
400
62.3k
        md_nid = ossl_digest_rsa_sign_get_md_nid(md);
401
62.3k
        if (md_nid == NID_undef) {
402
2
            ERR_raise_data(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED,
403
2
                "digest=%s", mdname);
404
2
            goto err;
405
2
        }
406
        /*
407
         * XOF digests are not allowed except for RSA PSS.
408
         * We don't support XOF digests with RSA PSS (yet), so just fail.
409
         * When we do support them, uncomment the second clause.
410
         */
411
62.3k
        if (EVP_MD_xof(md)
412
62.3k
            /* && ctx->pad_mode != RSA_PKCS1_PSS_PADDING */) {
413
0
            ERR_raise(ERR_LIB_PROV, PROV_R_XOF_DIGESTS_NOT_ALLOWED);
414
0
            goto err;
415
0
        }
416
#ifdef FIPS_MODULE
417
        {
418
            int sha1_allowed
419
                = ((ctx->operation
420
                       & (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_SIGNMSG))
421
                    == 0);
422
423
            if (!ossl_fips_ind_digest_sign_check(OSSL_FIPS_IND_GET(ctx),
424
                    OSSL_FIPS_IND_SETTABLE1,
425
                    ctx->libctx,
426
                    md_nid, sha1_allowed, 1, desc,
427
                    ossl_fips_config_signature_digest_check))
428
                goto err;
429
        }
430
#endif
431
432
62.3k
        if (!rsa_check_padding(ctx, mdname, NULL, md_nid))
433
30
            goto err;
434
62.3k
        if (mdname_len >= sizeof(ctx->mdname)) {
435
0
            ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
436
0
                "%s exceeds name buffer length", mdname);
437
0
            goto err;
438
0
        }
439
440
62.3k
        if (!ctx->flag_allow_md) {
441
0
            if (ctx->mdname[0] != '\0' && !EVP_MD_is_a(md, ctx->mdname)) {
442
0
                ERR_raise_data(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED,
443
0
                    "digest %s != %s", mdname, ctx->mdname);
444
0
                goto err;
445
0
            }
446
0
            EVP_MD_free(md);
447
0
            return 1;
448
0
        }
449
450
62.3k
        if (!ctx->mgf1_md_set) {
451
61.4k
            if (!EVP_MD_up_ref(md)) {
452
0
                goto err;
453
0
            }
454
61.4k
            EVP_MD_free(ctx->mgf1_md);
455
61.4k
            ctx->mgf1_md = md;
456
61.4k
            ctx->mgf1_mdnid = md_nid;
457
61.4k
            OPENSSL_strlcpy(ctx->mgf1_mdname, mdname, sizeof(ctx->mgf1_mdname));
458
61.4k
        }
459
460
62.3k
        EVP_MD_CTX_free(ctx->mdctx);
461
62.3k
        EVP_MD_free(ctx->md);
462
463
62.3k
        ctx->mdctx = NULL;
464
62.3k
        ctx->md = md;
465
62.3k
        ctx->mdnid = md_nid;
466
62.3k
        OPENSSL_strlcpy(ctx->mdname, mdname, sizeof(ctx->mdname));
467
62.3k
    }
468
469
62.3k
    return 1;
470
112
err:
471
112
    EVP_MD_free(md);
472
112
    return 0;
473
62.4k
}
474
475
static int rsa_setup_mgf1_md(PROV_RSA_CTX *ctx, const char *mdname,
476
    const char *mdprops)
477
5.07k
{
478
5.07k
    size_t len;
479
5.07k
    EVP_MD *md = NULL;
480
5.07k
    int mdnid;
481
482
5.07k
    if (mdprops == NULL)
483
5.07k
        mdprops = ctx->propq;
484
485
5.07k
    if ((md = EVP_MD_fetch(ctx->libctx, mdname, mdprops)) == NULL) {
486
0
        ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
487
0
            "%s could not be fetched", mdname);
488
0
        return 0;
489
0
    }
490
    /* The default for mgf1 is SHA1 - so allow SHA1 */
491
5.07k
    if ((mdnid = ossl_digest_rsa_sign_get_md_nid(md)) <= 0
492
5.06k
        || !rsa_check_padding(ctx, NULL, mdname, mdnid)) {
493
1
        if (mdnid <= 0)
494
1
            ERR_raise_data(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED,
495
1
                "digest=%s", mdname);
496
1
        EVP_MD_free(md);
497
1
        return 0;
498
1
    }
499
5.06k
    len = OPENSSL_strlcpy(ctx->mgf1_mdname, mdname, sizeof(ctx->mgf1_mdname));
500
5.06k
    if (len >= sizeof(ctx->mgf1_mdname)) {
501
0
        ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
502
0
            "%s exceeds name buffer length", mdname);
503
0
        EVP_MD_free(md);
504
0
        return 0;
505
0
    }
506
507
5.06k
    EVP_MD_free(ctx->mgf1_md);
508
5.06k
    ctx->mgf1_md = md;
509
5.06k
    ctx->mgf1_mdnid = mdnid;
510
5.06k
    ctx->mgf1_md_set = 1;
511
5.06k
    return 1;
512
5.06k
}
513
514
static int
515
rsa_signverify_init(PROV_RSA_CTX *prsactx, void *vrsa,
516
    OSSL_FUNC_signature_set_ctx_params_fn *set_ctx_params,
517
    const OSSL_PARAM params[], int operation,
518
    const char *desc)
519
62.4k
{
520
62.4k
    int protect;
521
522
62.4k
    if (!ossl_prov_is_running() || prsactx == NULL)
523
0
        return 0;
524
525
62.4k
    if (vrsa == NULL && prsactx->rsa == NULL) {
526
0
        ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
527
0
        return 0;
528
0
    }
529
530
62.4k
    if (vrsa != NULL) {
531
62.4k
        if (!RSA_up_ref(vrsa))
532
0
            return 0;
533
62.4k
        RSA_free(prsactx->rsa);
534
62.4k
        prsactx->rsa = vrsa;
535
62.4k
    }
536
62.4k
    if (!ossl_rsa_key_op_get_protect(prsactx->rsa, operation, &protect))
537
0
        return 0;
538
539
62.4k
    prsactx->operation = operation;
540
62.4k
    prsactx->flag_allow_update = 1;
541
62.4k
    prsactx->flag_allow_final = 1;
542
62.4k
    prsactx->flag_allow_oneshot = 1;
543
544
    /* Maximize up to digest length for sign, auto for verify */
545
62.4k
    prsactx->saltlen = RSA_PSS_SALTLEN_AUTO_DIGEST_MAX;
546
62.4k
    prsactx->min_saltlen = -1;
547
548
62.4k
    switch (RSA_test_flags(prsactx->rsa, RSA_FLAG_TYPE_MASK)) {
549
61.5k
    case RSA_FLAG_TYPE_RSA:
550
61.5k
        prsactx->pad_mode = RSA_PKCS1_PADDING;
551
61.5k
        break;
552
885
    case RSA_FLAG_TYPE_RSASSAPSS:
553
885
        prsactx->pad_mode = RSA_PKCS1_PSS_PADDING;
554
555
885
        {
556
885
            const RSA_PSS_PARAMS_30 *pss = ossl_rsa_get0_pss_params_30(prsactx->rsa);
557
558
885
            if (!ossl_rsa_pss_params_30_is_unrestricted(pss)) {
559
860
                int md_nid = ossl_rsa_pss_params_30_hashalg(pss);
560
860
                int mgf1md_nid = ossl_rsa_pss_params_30_maskgenhashalg(pss);
561
860
                int min_saltlen = ossl_rsa_pss_params_30_saltlen(pss);
562
860
                const char *mdname, *mgf1mdname;
563
860
                size_t len;
564
565
860
                mdname = ossl_rsa_oaeppss_nid2name(md_nid);
566
860
                mgf1mdname = ossl_rsa_oaeppss_nid2name(mgf1md_nid);
567
568
860
                if (mdname == NULL) {
569
2
                    ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
570
2
                        "PSS restrictions lack hash algorithm");
571
2
                    return 0;
572
2
                }
573
858
                if (mgf1mdname == NULL) {
574
2
                    ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
575
2
                        "PSS restrictions lack MGF1 hash algorithm");
576
2
                    return 0;
577
2
                }
578
579
856
                len = OPENSSL_strlcpy(prsactx->mdname, mdname,
580
856
                    sizeof(prsactx->mdname));
581
856
                if (len >= sizeof(prsactx->mdname)) {
582
0
                    ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
583
0
                        "hash algorithm name too long");
584
0
                    return 0;
585
0
                }
586
856
                len = OPENSSL_strlcpy(prsactx->mgf1_mdname, mgf1mdname,
587
856
                    sizeof(prsactx->mgf1_mdname));
588
856
                if (len >= sizeof(prsactx->mgf1_mdname)) {
589
0
                    ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
590
0
                        "MGF1 hash algorithm name too long");
591
0
                    return 0;
592
0
                }
593
856
                prsactx->saltlen = min_saltlen;
594
595
                /* call rsa_setup_mgf1_md before rsa_setup_md to avoid duplication */
596
856
                if (!rsa_setup_mgf1_md(prsactx, mgf1mdname, prsactx->propq)
597
856
                    || !rsa_setup_md(prsactx, mdname, prsactx->propq, desc)
598
856
                    || !rsa_check_parameters(prsactx, min_saltlen))
599
42
                    return 0;
600
856
            }
601
885
        }
602
603
839
        break;
604
839
    default:
605
0
        ERR_raise(ERR_LIB_PROV, PROV_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
606
0
        return 0;
607
62.4k
    }
608
609
62.3k
    OSSL_FIPS_IND_SET_APPROVED(prsactx)
610
62.3k
    if (!set_ctx_params(prsactx, params))
611
5
        return 0;
612
#ifdef FIPS_MODULE
613
    if (!ossl_fips_ind_rsa_key_check(OSSL_FIPS_IND_GET(prsactx),
614
            OSSL_FIPS_IND_SETTABLE0, prsactx->libctx,
615
            prsactx->rsa, desc, protect))
616
        return 0;
617
#endif
618
62.3k
    return 1;
619
62.3k
}
620
621
static int setup_tbuf(PROV_RSA_CTX *ctx)
622
23.3k
{
623
23.3k
    if (ctx->tbuf != NULL)
624
0
        return 1;
625
23.3k
    if ((ctx->tbuf = OPENSSL_malloc(RSA_size(ctx->rsa))) == NULL)
626
22
        return 0;
627
23.2k
    return 1;
628
23.3k
}
629
630
static void clean_tbuf(PROV_RSA_CTX *ctx)
631
99.3k
{
632
99.3k
    if (ctx->tbuf != NULL)
633
24.7k
        OPENSSL_cleanse(ctx->tbuf, RSA_size(ctx->rsa));
634
99.3k
}
635
636
static void free_tbuf(PROV_RSA_CTX *ctx)
637
97.9k
{
638
97.9k
    clean_tbuf(ctx);
639
97.9k
    OPENSSL_free(ctx->tbuf);
640
97.9k
    ctx->tbuf = NULL;
641
97.9k
}
642
643
#ifdef FIPS_MODULE
644
static int rsa_pss_saltlen_check_passed(PROV_RSA_CTX *ctx, const char *algoname, int saltlen)
645
{
646
    int mdsize = rsa_get_md_size(ctx);
647
    /*
648
     * Perform the check if the salt length is compliant to FIPS 186-5.
649
     *
650
     * According to FIPS 186-5 5.4 (g), the salt length shall be between zero
651
     * and the output block length of the digest function (inclusive).
652
     */
653
    int approved = (saltlen >= 0 && saltlen <= mdsize);
654
655
    if (!approved) {
656
        if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE3,
657
                ctx->libctx,
658
                algoname, "PSS Salt Length",
659
                ossl_fips_config_rsa_pss_saltlen_check)) {
660
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH);
661
            return 0;
662
        }
663
    }
664
665
    return 1;
666
}
667
#endif
668
669
static int rsa_sign_init(void *vprsactx, void *vrsa, const OSSL_PARAM params[])
670
0
{
671
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
672
673
#ifdef FIPS_MODULE
674
    if (prsactx != NULL)
675
        prsactx->verify_message = 1;
676
#endif
677
678
0
    return rsa_signverify_init(prsactx, vrsa, rsa_set_ctx_params, params,
679
0
        EVP_PKEY_OP_SIGN, "RSA Sign Init");
680
0
}
681
682
/*
683
 * Sign tbs without digesting it first.  This is suitable for "primitive"
684
 * signing and signing the digest of a message, i.e. should be used with
685
 * implementations of the keytype related algorithms.
686
 */
687
static int rsa_sign_directly(PROV_RSA_CTX *prsactx,
688
    unsigned char *sig, size_t *siglen, size_t sigsize,
689
    const unsigned char *tbs, size_t tbslen)
690
12.6k
{
691
12.6k
    int ret;
692
12.6k
    size_t rsasize = RSA_size(prsactx->rsa);
693
12.6k
    size_t mdsize = rsa_get_md_size(prsactx);
694
695
12.6k
    if (!ossl_prov_is_running())
696
0
        return 0;
697
698
12.6k
    if (sig == NULL) {
699
6.34k
        *siglen = rsasize;
700
6.34k
        return 1;
701
6.34k
    }
702
703
6.34k
    if (sigsize < rsasize) {
704
0
        ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_SIGNATURE_SIZE,
705
0
            "is %zu, should be at least %zu", sigsize, rsasize);
706
0
        return 0;
707
0
    }
708
709
6.34k
    if (mdsize != 0) {
710
6.34k
        if (tbslen != mdsize) {
711
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_LENGTH);
712
0
            return 0;
713
0
        }
714
715
6.34k
#ifndef FIPS_MODULE
716
6.34k
        if (EVP_MD_is_a(prsactx->md, OSSL_DIGEST_NAME_MDC2)) {
717
0
            unsigned int sltmp;
718
719
0
            if (prsactx->pad_mode != RSA_PKCS1_PADDING) {
720
0
                ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_PADDING_MODE,
721
0
                    "only PKCS#1 padding supported with MDC2");
722
0
                return 0;
723
0
            }
724
0
            ret = RSA_sign_ASN1_OCTET_STRING(0, tbs, (unsigned int)tbslen, sig,
725
0
                &sltmp, prsactx->rsa);
726
727
0
            if (ret <= 0) {
728
0
                ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
729
0
                return 0;
730
0
            }
731
0
            ret = sltmp;
732
0
            goto end;
733
0
        }
734
6.34k
#endif
735
6.34k
        switch (prsactx->pad_mode) {
736
0
        case RSA_X931_PADDING:
737
0
            if ((size_t)RSA_size(prsactx->rsa) < tbslen + 1) {
738
0
                ERR_raise_data(ERR_LIB_PROV, PROV_R_KEY_SIZE_TOO_SMALL,
739
0
                    "RSA key size = %d, expected minimum = %d",
740
0
                    RSA_size(prsactx->rsa), tbslen + 1);
741
0
                return 0;
742
0
            }
743
0
            if (!setup_tbuf(prsactx)) {
744
0
                ERR_raise(ERR_LIB_PROV, ERR_R_PROV_LIB);
745
0
                return 0;
746
0
            }
747
0
            memcpy(prsactx->tbuf, tbs, tbslen);
748
0
            prsactx->tbuf[tbslen] = RSA_X931_hash_id(prsactx->mdnid);
749
0
            ret = RSA_private_encrypt((int)(tbslen + 1), prsactx->tbuf,
750
0
                sig, prsactx->rsa, RSA_X931_PADDING);
751
0
            clean_tbuf(prsactx);
752
0
            break;
753
4.85k
        case RSA_PKCS1_PADDING: {
754
4.85k
            unsigned int sltmp;
755
756
4.85k
            ret = RSA_sign(prsactx->mdnid, tbs, (unsigned int)tbslen,
757
4.85k
                sig, &sltmp, prsactx->rsa);
758
4.85k
            if (ret <= 0) {
759
0
                ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
760
0
                return 0;
761
0
            }
762
4.85k
            ret = sltmp;
763
4.85k
        } break;
764
765
1.48k
        case RSA_PKCS1_PSS_PADDING: {
766
1.48k
            int saltlen;
767
768
            /* Check PSS restrictions */
769
1.48k
            if (rsa_pss_restricted(prsactx)) {
770
0
                switch (prsactx->saltlen) {
771
0
                case RSA_PSS_SALTLEN_DIGEST:
772
0
                    if (prsactx->min_saltlen > EVP_MD_get_size(prsactx->md)) {
773
0
                        ERR_raise_data(ERR_LIB_PROV,
774
0
                            PROV_R_PSS_SALTLEN_TOO_SMALL,
775
0
                            "minimum salt length set to %d, "
776
0
                            "but the digest only gives %d",
777
0
                            prsactx->min_saltlen,
778
0
                            EVP_MD_get_size(prsactx->md));
779
0
                        return 0;
780
0
                    }
781
                    /* FALLTHRU */
782
0
                default:
783
0
                    if (prsactx->saltlen >= 0
784
0
                        && prsactx->saltlen < prsactx->min_saltlen) {
785
0
                        ERR_raise_data(ERR_LIB_PROV,
786
0
                            PROV_R_PSS_SALTLEN_TOO_SMALL,
787
0
                            "minimum salt length set to %d, but the"
788
0
                            "actual salt length is only set to %d",
789
0
                            prsactx->min_saltlen,
790
0
                            prsactx->saltlen);
791
0
                        return 0;
792
0
                    }
793
0
                    break;
794
0
                }
795
0
            }
796
1.48k
            if (!setup_tbuf(prsactx))
797
0
                return 0;
798
1.48k
            saltlen = prsactx->saltlen;
799
1.48k
            if (!ossl_rsa_padding_add_PKCS1_PSS_mgf1(prsactx->rsa,
800
1.48k
                    prsactx->tbuf, tbs,
801
1.48k
                    prsactx->md, prsactx->mgf1_md,
802
1.48k
                    &saltlen)) {
803
0
                ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
804
0
                return 0;
805
0
            }
806
#ifdef FIPS_MODULE
807
            if (!rsa_pss_saltlen_check_passed(prsactx, "RSA Sign", saltlen))
808
                return 0;
809
#endif
810
1.48k
            ret = RSA_private_encrypt(RSA_size(prsactx->rsa), prsactx->tbuf,
811
1.48k
                sig, prsactx->rsa, RSA_NO_PADDING);
812
1.48k
            clean_tbuf(prsactx);
813
1.48k
        } break;
814
815
0
        default:
816
0
            ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_PADDING_MODE,
817
0
                "Only X.931, PKCS#1 v1.5 or PSS padding allowed");
818
0
            return 0;
819
6.34k
        }
820
6.34k
    } else {
821
0
        ret = RSA_private_encrypt((int)tbslen, tbs, sig, prsactx->rsa,
822
0
            prsactx->pad_mode);
823
0
    }
824
825
6.34k
#ifndef FIPS_MODULE
826
6.34k
end:
827
6.34k
#endif
828
6.34k
    if (ret <= 0) {
829
0
        ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
830
0
        return 0;
831
0
    }
832
833
6.34k
    *siglen = ret;
834
6.34k
    return 1;
835
6.34k
}
836
837
static int rsa_signverify_message_update(void *vprsactx,
838
    const unsigned char *data,
839
    size_t datalen)
840
35.4k
{
841
35.4k
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
842
843
35.4k
    if (prsactx == NULL || prsactx->mdctx == NULL)
844
0
        return 0;
845
846
35.4k
    if (!prsactx->flag_allow_update) {
847
0
        ERR_raise(ERR_LIB_PROV, PROV_R_UPDATE_CALL_OUT_OF_ORDER);
848
0
        return 0;
849
0
    }
850
35.4k
    prsactx->flag_allow_oneshot = 0;
851
852
35.4k
    return EVP_DigestUpdate(prsactx->mdctx, data, datalen);
853
35.4k
}
854
855
static int rsa_sign_message_final(void *vprsactx, unsigned char *sig,
856
    size_t *siglen, size_t sigsize)
857
12.6k
{
858
12.6k
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
859
12.6k
    unsigned char digest[EVP_MAX_MD_SIZE];
860
12.6k
    unsigned int dlen = 0;
861
862
12.6k
    if (!ossl_prov_is_running() || prsactx == NULL)
863
0
        return 0;
864
12.6k
    if (prsactx->mdctx == NULL)
865
0
        return 0;
866
12.6k
    if (!prsactx->flag_allow_final) {
867
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FINAL_CALL_OUT_OF_ORDER);
868
0
        return 0;
869
0
    }
870
871
    /*
872
     * If sig is NULL then we're just finding out the sig size. Other fields
873
     * are ignored. Defer to rsa_sign.
874
     */
875
12.6k
    if (sig != NULL) {
876
        /*
877
         * The digests used here are all known (see rsa_get_md_nid()), so they
878
         * should not exceed the internal buffer size of EVP_MAX_MD_SIZE.
879
         */
880
6.34k
        if (!EVP_DigestFinal_ex(prsactx->mdctx, digest, &dlen))
881
0
            return 0;
882
883
6.34k
        prsactx->flag_allow_update = 0;
884
6.34k
        prsactx->flag_allow_oneshot = 0;
885
6.34k
        prsactx->flag_allow_final = 0;
886
6.34k
    }
887
888
12.6k
    return rsa_sign_directly(prsactx, sig, siglen, sigsize, digest, dlen);
889
12.6k
}
890
891
/*
892
 * If signing a message, digest tbs and sign the result.
893
 * Otherwise, sign tbs directly.
894
 */
895
static int rsa_sign(void *vprsactx, unsigned char *sig, size_t *siglen,
896
    size_t sigsize, const unsigned char *tbs, size_t tbslen)
897
0
{
898
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
899
900
0
    if (!ossl_prov_is_running() || prsactx == NULL)
901
0
        return 0;
902
0
    if (!prsactx->flag_allow_oneshot) {
903
0
        ERR_raise(ERR_LIB_PROV, PROV_R_ONESHOT_CALL_OUT_OF_ORDER);
904
0
        return 0;
905
0
    }
906
907
0
    if (prsactx->operation == EVP_PKEY_OP_SIGNMSG) {
908
        /*
909
         * If |sig| is NULL, the caller is only looking for the sig length.
910
         * DO NOT update the input in this case.
911
         */
912
0
        if (sig == NULL)
913
0
            return rsa_sign_message_final(prsactx, sig, siglen, sigsize);
914
915
0
        return rsa_signverify_message_update(prsactx, tbs, tbslen)
916
0
            && rsa_sign_message_final(prsactx, sig, siglen, sigsize);
917
0
    }
918
0
    return rsa_sign_directly(prsactx, sig, siglen, sigsize, tbs, tbslen);
919
0
}
920
921
static int rsa_verify_recover_init(void *vprsactx, void *vrsa,
922
    const OSSL_PARAM params[])
923
0
{
924
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
925
926
#ifdef FIPS_MODULE
927
    if (prsactx != NULL)
928
        prsactx->verify_message = 0;
929
#endif
930
931
0
    return rsa_signverify_init(prsactx, vrsa, rsa_set_ctx_params, params,
932
0
        EVP_PKEY_OP_VERIFYRECOVER, "RSA VerifyRecover Init");
933
0
}
934
935
/*
936
 * There is no message variant of verify recover, so no need for
937
 * 'rsa_verify_recover_directly', just use this function, er, directly.
938
 */
939
static int rsa_verify_recover(void *vprsactx,
940
    unsigned char *rout, size_t *routlen,
941
    size_t routsize,
942
    const unsigned char *sig, size_t siglen)
943
0
{
944
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
945
0
    int ret;
946
947
0
    if (!ossl_prov_is_running())
948
0
        return 0;
949
950
0
    if (rout == NULL) {
951
0
        *routlen = RSA_size(prsactx->rsa);
952
0
        return 1;
953
0
    }
954
955
0
    if (prsactx->md != NULL) {
956
0
        switch (prsactx->pad_mode) {
957
0
        case RSA_X931_PADDING:
958
0
            if (!setup_tbuf(prsactx))
959
0
                return 0;
960
0
            ret = RSA_public_decrypt((int)siglen, sig, prsactx->tbuf, prsactx->rsa,
961
0
                RSA_X931_PADDING);
962
0
            if (ret <= 0) {
963
0
                ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
964
0
                return 0;
965
0
            }
966
0
            ret--;
967
0
            if (prsactx->tbuf[ret] != RSA_X931_hash_id(prsactx->mdnid)) {
968
0
                ERR_raise(ERR_LIB_PROV, PROV_R_ALGORITHM_MISMATCH);
969
0
                return 0;
970
0
            }
971
0
            if (ret != EVP_MD_get_size(prsactx->md)) {
972
0
                ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_LENGTH,
973
0
                    "Should be %d, but got %d",
974
0
                    EVP_MD_get_size(prsactx->md), ret);
975
0
                return 0;
976
0
            }
977
978
0
            *routlen = ret;
979
0
            if (rout != prsactx->tbuf) {
980
0
                if (routsize < (size_t)ret) {
981
0
                    ERR_raise_data(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL,
982
0
                        "buffer size is %d, should be %d",
983
0
                        routsize, ret);
984
0
                    return 0;
985
0
                }
986
0
                memcpy(rout, prsactx->tbuf, ret);
987
0
            }
988
0
            break;
989
990
0
        case RSA_PKCS1_PADDING: {
991
0
            int mdsize = EVP_MD_get_size(prsactx->md);
992
0
            size_t sltmp;
993
994
0
            if (mdsize <= 0) {
995
0
                ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_LENGTH);
996
0
                return 0;
997
0
            }
998
0
            if (routsize < (size_t)mdsize) {
999
0
                ERR_raise_data(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL,
1000
0
                    "buffer size is %d, should be %d",
1001
0
                    routsize, mdsize);
1002
0
                return 0;
1003
0
            }
1004
0
            ret = ossl_rsa_verify(prsactx->mdnid, NULL, 0, rout, &sltmp,
1005
0
                sig, siglen, prsactx->rsa);
1006
0
            if (ret <= 0) {
1007
0
                ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
1008
0
                return 0;
1009
0
            }
1010
0
            ret = (int)sltmp;
1011
0
        } break;
1012
1013
0
        default:
1014
0
            ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_PADDING_MODE,
1015
0
                "Only X.931 or PKCS#1 v1.5 padding allowed");
1016
0
            return 0;
1017
0
        }
1018
0
    } else {
1019
0
        int rsasize = RSA_size(prsactx->rsa);
1020
1021
0
        if (routsize < (size_t)rsasize) {
1022
0
            ERR_raise_data(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL,
1023
0
                "buffer size is %d, should be %d",
1024
0
                routsize, rsasize);
1025
0
            return 0;
1026
0
        }
1027
0
        ret = RSA_public_decrypt((int)siglen, sig, rout, prsactx->rsa,
1028
0
            prsactx->pad_mode);
1029
        /*
1030
         * RSA_public_decrypt() returns -1 on error and otherwise the number
1031
         * of recovered bytes, which may legitimately be zero for a raw
1032
         * PKCS#1 v1.5 signature that encodes an empty payload.  Treat only
1033
         * a negative result as an error.
1034
         */
1035
0
        if (ret < 0) {
1036
0
            ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
1037
0
            return 0;
1038
0
        }
1039
0
    }
1040
0
    *routlen = ret;
1041
0
    return 1;
1042
0
}
1043
1044
static int rsa_verify_init(void *vprsactx, void *vrsa,
1045
    const OSSL_PARAM params[])
1046
1.93k
{
1047
1.93k
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1048
1049
#ifdef FIPS_MODULE
1050
    if (prsactx != NULL)
1051
        prsactx->verify_message = 0;
1052
#endif
1053
1054
1.93k
    return rsa_signverify_init(prsactx, vrsa, rsa_set_ctx_params, params,
1055
1.93k
        EVP_PKEY_OP_VERIFY, "RSA Verify Init");
1056
1.93k
}
1057
1058
static int rsa_verify_directly(PROV_RSA_CTX *prsactx,
1059
    const unsigned char *sig, size_t siglen,
1060
    const unsigned char *tbs, size_t tbslen)
1061
31.0k
{
1062
31.0k
    size_t rslen;
1063
1064
31.0k
    if (!ossl_prov_is_running())
1065
0
        return 0;
1066
31.0k
    if (prsactx->md != NULL) {
1067
31.0k
        switch (prsactx->pad_mode) {
1068
9.22k
        case RSA_PKCS1_PADDING:
1069
9.22k
            if (!RSA_verify(prsactx->mdnid, tbs, (unsigned int)tbslen,
1070
9.22k
                    sig, (unsigned int)siglen, prsactx->rsa)) {
1071
8.54k
                ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
1072
8.54k
                return 0;
1073
8.54k
            }
1074
686
            return 1;
1075
0
        case RSA_X931_PADDING:
1076
0
            if (!setup_tbuf(prsactx))
1077
0
                return 0;
1078
0
            if (rsa_verify_recover(prsactx, prsactx->tbuf, &rslen, 0,
1079
0
                    sig, siglen)
1080
0
                <= 0)
1081
0
                return 0;
1082
0
            break;
1083
21.8k
        case RSA_PKCS1_PSS_PADDING: {
1084
21.8k
            int ret;
1085
21.8k
            int saltlen;
1086
21.8k
            size_t mdsize;
1087
1088
            /*
1089
             * We need to check this for the RSA_verify_PKCS1_PSS_mgf1()
1090
             * call
1091
             */
1092
21.8k
            mdsize = rsa_get_md_size(prsactx);
1093
21.8k
            if (tbslen != mdsize) {
1094
0
                ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_LENGTH,
1095
0
                    "Should be %d, but got %d",
1096
0
                    mdsize, tbslen);
1097
0
                return 0;
1098
0
            }
1099
1100
21.8k
            if (!setup_tbuf(prsactx))
1101
22
                return 0;
1102
21.8k
            ret = RSA_public_decrypt((int)siglen, sig, prsactx->tbuf,
1103
21.8k
                prsactx->rsa, RSA_NO_PADDING);
1104
21.8k
            if (ret <= 0) {
1105
5.11k
                ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
1106
5.11k
                return 0;
1107
5.11k
            }
1108
16.6k
            saltlen = prsactx->saltlen;
1109
16.6k
            ret = ossl_rsa_verify_PKCS1_PSS_mgf1(prsactx->rsa, tbs,
1110
16.6k
                prsactx->md, prsactx->mgf1_md,
1111
16.6k
                prsactx->tbuf,
1112
16.6k
                &saltlen);
1113
16.6k
            if (ret <= 0) {
1114
16.6k
                ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
1115
16.6k
                return 0;
1116
16.6k
            }
1117
#ifdef FIPS_MODULE
1118
            if (!rsa_pss_saltlen_check_passed(prsactx, "RSA Verify", saltlen))
1119
                return 0;
1120
#endif
1121
18
            return 1;
1122
16.6k
        }
1123
0
        default:
1124
0
            ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_PADDING_MODE,
1125
0
                "Only X.931, PKCS#1 v1.5 or PSS padding allowed");
1126
0
            return 0;
1127
31.0k
        }
1128
31.0k
    } else {
1129
0
        int ret;
1130
1131
0
        if (!setup_tbuf(prsactx))
1132
0
            return 0;
1133
0
        ret = RSA_public_decrypt((int)siglen, sig, prsactx->tbuf, prsactx->rsa,
1134
0
            prsactx->pad_mode);
1135
0
        if (ret <= 0) {
1136
0
            ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
1137
0
            return 0;
1138
0
        }
1139
0
        rslen = (size_t)ret;
1140
0
    }
1141
1142
0
    if ((rslen != tbslen) || memcmp(tbs, prsactx->tbuf, rslen))
1143
0
        return 0;
1144
1145
0
    return 1;
1146
0
}
1147
1148
static int rsa_verify_set_sig(void *vprsactx,
1149
    const unsigned char *sig, size_t siglen)
1150
29.1k
{
1151
29.1k
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1152
29.1k
    OSSL_PARAM params[2];
1153
1154
29.1k
    params[0] = OSSL_PARAM_construct_octet_string(OSSL_SIGNATURE_PARAM_SIGNATURE,
1155
29.1k
        (unsigned char *)sig, siglen);
1156
29.1k
    params[1] = OSSL_PARAM_construct_end();
1157
29.1k
    return rsa_sigalg_set_ctx_params(prsactx, params);
1158
29.1k
}
1159
1160
static int rsa_verify_message_final(void *vprsactx)
1161
29.1k
{
1162
29.1k
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1163
29.1k
    unsigned char digest[EVP_MAX_MD_SIZE];
1164
29.1k
    unsigned int dlen = 0;
1165
1166
29.1k
    if (!ossl_prov_is_running() || prsactx == NULL)
1167
0
        return 0;
1168
29.1k
    if (prsactx->mdctx == NULL)
1169
0
        return 0;
1170
29.1k
    if (!prsactx->flag_allow_final) {
1171
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FINAL_CALL_OUT_OF_ORDER);
1172
0
        return 0;
1173
0
    }
1174
1175
    /*
1176
     * The digests used here are all known (see rsa_get_md_nid()), so they
1177
     * should not exceed the internal buffer size of EVP_MAX_MD_SIZE.
1178
     */
1179
29.1k
    if (!EVP_DigestFinal_ex(prsactx->mdctx, digest, &dlen))
1180
0
        return 0;
1181
1182
29.1k
    prsactx->flag_allow_update = 0;
1183
29.1k
    prsactx->flag_allow_final = 0;
1184
29.1k
    prsactx->flag_allow_oneshot = 0;
1185
1186
29.1k
    return rsa_verify_directly(prsactx, prsactx->sig, prsactx->siglen,
1187
29.1k
        digest, dlen);
1188
29.1k
}
1189
1190
/*
1191
 * If verifying a message, digest tbs and verify the result.
1192
 * Otherwise, verify tbs directly.
1193
 */
1194
static int rsa_verify(void *vprsactx,
1195
    const unsigned char *sig, size_t siglen,
1196
    const unsigned char *tbs, size_t tbslen)
1197
1.91k
{
1198
1.91k
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1199
1200
1.91k
    if (!ossl_prov_is_running() || prsactx == NULL)
1201
0
        return 0;
1202
1.91k
    if (!prsactx->flag_allow_oneshot) {
1203
0
        ERR_raise(ERR_LIB_PROV, PROV_R_ONESHOT_CALL_OUT_OF_ORDER);
1204
0
        return 0;
1205
0
    }
1206
1207
1.91k
    if (prsactx->operation == EVP_PKEY_OP_VERIFYMSG)
1208
0
        return rsa_verify_set_sig(prsactx, sig, siglen)
1209
0
            && rsa_signverify_message_update(prsactx, tbs, tbslen)
1210
0
            && rsa_verify_message_final(prsactx);
1211
1.91k
    return rsa_verify_directly(prsactx, sig, siglen, tbs, tbslen);
1212
1.91k
}
1213
1214
/* DigestSign/DigestVerify wrappers */
1215
1216
static int rsa_digest_signverify_init(void *vprsactx, const char *mdname,
1217
    void *vrsa, const OSSL_PARAM params[],
1218
    int operation, const char *desc)
1219
60.4k
{
1220
60.4k
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1221
1222
#ifdef FIPS_MODULE
1223
    if (prsactx != NULL)
1224
        prsactx->verify_message = 1;
1225
#endif
1226
1227
60.4k
    if (!rsa_signverify_init(prsactx, vrsa, rsa_set_ctx_params, params,
1228
60.4k
            operation, desc))
1229
51
        return 0;
1230
1231
60.4k
    if (mdname != NULL
1232
        /* was rsa_setup_md already called in rsa_signverify_init()? */
1233
60.4k
        && (mdname[0] == '\0' || OPENSSL_strcasecmp(prsactx->mdname, mdname) != 0)
1234
51.5k
        && !rsa_setup_md(prsactx, mdname, prsactx->propq, desc))
1235
102
        return 0;
1236
1237
60.3k
    prsactx->flag_allow_md = 0;
1238
1239
60.3k
    if (prsactx->mdctx == NULL) {
1240
60.3k
        prsactx->mdctx = EVP_MD_CTX_new();
1241
60.3k
        if (prsactx->mdctx == NULL)
1242
0
            goto error;
1243
60.3k
    }
1244
1245
60.3k
    if (!EVP_DigestInit_ex2(prsactx->mdctx, prsactx->md, params))
1246
0
        goto error;
1247
1248
60.3k
    return 1;
1249
1250
0
error:
1251
0
    EVP_MD_CTX_free(prsactx->mdctx);
1252
0
    prsactx->mdctx = NULL;
1253
0
    return 0;
1254
60.3k
}
1255
1256
static int rsa_digest_sign_init(void *vprsactx, const char *mdname,
1257
    void *vrsa, const OSSL_PARAM params[])
1258
31.1k
{
1259
31.1k
    if (!ossl_prov_is_running())
1260
0
        return 0;
1261
31.1k
    return rsa_digest_signverify_init(vprsactx, mdname, vrsa,
1262
31.1k
        params, EVP_PKEY_OP_SIGNMSG,
1263
31.1k
        "RSA Digest Sign Init");
1264
31.1k
}
1265
1266
static int rsa_digest_sign_update(void *vprsactx, const unsigned char *data,
1267
    size_t datalen)
1268
6.34k
{
1269
6.34k
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1270
1271
6.34k
    if (prsactx == NULL)
1272
0
        return 0;
1273
    /* Sigalg implementations shouldn't do digest_sign */
1274
6.34k
    if (prsactx->flag_sigalg)
1275
0
        return 0;
1276
1277
6.34k
    return rsa_signverify_message_update(prsactx, data, datalen);
1278
6.34k
}
1279
1280
static int rsa_digest_sign_final(void *vprsactx, unsigned char *sig,
1281
    size_t *siglen, size_t sigsize)
1282
12.6k
{
1283
12.6k
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1284
12.6k
    int ok = 0;
1285
1286
12.6k
    if (prsactx == NULL)
1287
0
        return 0;
1288
    /* Sigalg implementations shouldn't do digest_sign */
1289
12.6k
    if (prsactx->flag_sigalg)
1290
0
        return 0;
1291
1292
12.6k
    if (rsa_sign_message_final(prsactx, sig, siglen, sigsize))
1293
12.6k
        ok = 1;
1294
1295
12.6k
    prsactx->flag_allow_md = 1;
1296
1297
12.6k
    return ok;
1298
12.6k
}
1299
1300
static int rsa_digest_verify_init(void *vprsactx, const char *mdname,
1301
    void *vrsa, const OSSL_PARAM params[])
1302
29.3k
{
1303
29.3k
    if (!ossl_prov_is_running())
1304
0
        return 0;
1305
29.3k
    return rsa_digest_signverify_init(vprsactx, mdname, vrsa,
1306
29.3k
        params, EVP_PKEY_OP_VERIFYMSG,
1307
29.3k
        "RSA Digest Verify Init");
1308
29.3k
}
1309
1310
static int rsa_digest_verify_update(void *vprsactx, const unsigned char *data,
1311
    size_t datalen)
1312
29.1k
{
1313
29.1k
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1314
1315
29.1k
    if (prsactx == NULL)
1316
0
        return 0;
1317
    /* Sigalg implementations shouldn't do digest_sign */
1318
29.1k
    if (prsactx->flag_sigalg)
1319
0
        return 0;
1320
1321
29.1k
    return rsa_signverify_message_update(prsactx, data, datalen);
1322
29.1k
}
1323
1324
int rsa_digest_verify_final(void *vprsactx, const unsigned char *sig,
1325
    size_t siglen)
1326
29.1k
{
1327
29.1k
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1328
29.1k
    int ok = 0;
1329
1330
29.1k
    if (prsactx == NULL)
1331
0
        return 0;
1332
    /* Sigalg implementations shouldn't do digest_verify */
1333
29.1k
    if (prsactx->flag_sigalg)
1334
0
        return 0;
1335
1336
29.1k
    if (rsa_verify_set_sig(prsactx, sig, siglen)
1337
29.1k
        && rsa_verify_message_final(vprsactx))
1338
612
        ok = 1;
1339
1340
29.1k
    prsactx->flag_allow_md = 1;
1341
1342
29.1k
    return ok;
1343
29.1k
}
1344
1345
static void rsa_freectx(void *vprsactx)
1346
97.9k
{
1347
97.9k
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1348
1349
97.9k
    if (prsactx == NULL)
1350
0
        return;
1351
1352
97.9k
    EVP_MD_CTX_free(prsactx->mdctx);
1353
97.9k
    EVP_MD_free(prsactx->md);
1354
97.9k
    EVP_MD_free(prsactx->mgf1_md);
1355
97.9k
    OPENSSL_free(prsactx->sig);
1356
97.9k
    OPENSSL_free(prsactx->propq);
1357
97.9k
    free_tbuf(prsactx);
1358
97.9k
    RSA_free(prsactx->rsa);
1359
1360
97.9k
    OPENSSL_clear_free(prsactx, sizeof(*prsactx));
1361
97.9k
}
1362
1363
static void *rsa_dupctx(void *vprsactx)
1364
35.4k
{
1365
35.4k
    PROV_RSA_CTX *srcctx = (PROV_RSA_CTX *)vprsactx;
1366
35.4k
    PROV_RSA_CTX *dstctx;
1367
1368
35.4k
    if (!ossl_prov_is_running())
1369
0
        return NULL;
1370
1371
35.4k
    dstctx = OPENSSL_zalloc(sizeof(*srcctx));
1372
35.4k
    if (dstctx == NULL)
1373
0
        return NULL;
1374
1375
35.4k
    *dstctx = *srcctx;
1376
35.4k
    dstctx->rsa = NULL;
1377
35.4k
    dstctx->md = NULL;
1378
35.4k
    dstctx->mgf1_md = NULL;
1379
35.4k
    dstctx->mdctx = NULL;
1380
35.4k
    dstctx->tbuf = NULL;
1381
35.4k
    dstctx->propq = NULL;
1382
35.4k
    dstctx->sig = NULL;
1383
1384
35.4k
    if (srcctx->rsa != NULL && !RSA_up_ref(srcctx->rsa))
1385
0
        goto err;
1386
35.4k
    dstctx->rsa = srcctx->rsa;
1387
1388
35.4k
    if (srcctx->md != NULL && !EVP_MD_up_ref(srcctx->md))
1389
0
        goto err;
1390
35.4k
    dstctx->md = srcctx->md;
1391
1392
35.4k
    if (srcctx->mgf1_md != NULL && !EVP_MD_up_ref(srcctx->mgf1_md))
1393
0
        goto err;
1394
35.4k
    dstctx->mgf1_md = srcctx->mgf1_md;
1395
1396
35.4k
    if (srcctx->mdctx != NULL) {
1397
35.4k
        dstctx->mdctx = EVP_MD_CTX_new();
1398
35.4k
        if (dstctx->mdctx == NULL
1399
35.4k
            || !EVP_MD_CTX_copy_ex(dstctx->mdctx, srcctx->mdctx))
1400
0
            goto err;
1401
35.4k
    }
1402
1403
35.4k
    if (srcctx->propq != NULL) {
1404
0
        dstctx->propq = OPENSSL_strdup(srcctx->propq);
1405
0
        if (dstctx->propq == NULL)
1406
0
            goto err;
1407
0
    }
1408
1409
35.4k
    if (srcctx->sig != NULL) {
1410
0
        dstctx->sig = OPENSSL_memdup(srcctx->sig, srcctx->siglen);
1411
0
        if (dstctx->sig == NULL)
1412
0
            goto err;
1413
0
    }
1414
1415
35.4k
    return dstctx;
1416
0
err:
1417
0
    rsa_freectx(dstctx);
1418
0
    return NULL;
1419
35.4k
}
1420
1421
static int rsa_get_ctx_params(void *vprsactx, OSSL_PARAM *params)
1422
{
1423
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1424
    struct rsa_get_ctx_params_st p;
1425
1426
    if (prsactx == NULL || !rsa_get_ctx_params_decoder(params, &p))
1427
        return 0;
1428
1429
    if (p.algid != NULL) {
1430
        /* The Algorithm Identifier of the combined signature algorithm */
1431
        unsigned char aid_buf[128];
1432
        unsigned char *aid;
1433
        size_t aid_len;
1434
1435
        aid = rsa_generate_signature_aid(prsactx, aid_buf,
1436
            sizeof(aid_buf), &aid_len);
1437
        if (aid == NULL || !OSSL_PARAM_set_octet_string(p.algid, aid, aid_len))
1438
            return 0;
1439
    }
1440
1441
    if (p.pad != NULL) {
1442
        if (p.pad->data_type != OSSL_PARAM_UTF8_STRING) {
1443
            if (!OSSL_PARAM_set_int(p.pad, prsactx->pad_mode))
1444
                return 0;
1445
        } else {
1446
            int i;
1447
            const char *word = NULL;
1448
1449
            for (i = 0; padding_item[i].id != 0; i++) {
1450
                if (prsactx->pad_mode == (int)padding_item[i].id) {
1451
                    word = padding_item[i].ptr;
1452
                    break;
1453
                }
1454
            }
1455
1456
            if (word != NULL) {
1457
                if (!OSSL_PARAM_set_utf8_string(p.pad, word))
1458
                    return 0;
1459
            } else {
1460
                ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
1461
            }
1462
        }
1463
    }
1464
1465
    if (p.digest != NULL && !OSSL_PARAM_set_utf8_string(p.digest, prsactx->mdname))
1466
        return 0;
1467
1468
    if (p.mgf1 != NULL && !OSSL_PARAM_set_utf8_string(p.mgf1, prsactx->mgf1_mdname))
1469
        return 0;
1470
1471
    if (p.slen != NULL) {
1472
        if (p.slen->data_type != OSSL_PARAM_UTF8_STRING) {
1473
            if (!OSSL_PARAM_set_int(p.slen, prsactx->saltlen))
1474
                return 0;
1475
        } else {
1476
            const char *value = NULL;
1477
1478
            switch (prsactx->saltlen) {
1479
            case RSA_PSS_SALTLEN_DIGEST:
1480
                value = OSSL_PKEY_RSA_PSS_SALT_LEN_DIGEST;
1481
                break;
1482
            case RSA_PSS_SALTLEN_MAX:
1483
                value = OSSL_PKEY_RSA_PSS_SALT_LEN_MAX;
1484
                break;
1485
            case RSA_PSS_SALTLEN_AUTO:
1486
                value = OSSL_PKEY_RSA_PSS_SALT_LEN_AUTO;
1487
                break;
1488
            case RSA_PSS_SALTLEN_AUTO_DIGEST_MAX:
1489
                value = OSSL_PKEY_RSA_PSS_SALT_LEN_AUTO_DIGEST_MAX;
1490
                break;
1491
            default: {
1492
                int len = BIO_snprintf(p.slen->data, p.slen->data_size, "%d",
1493
                    prsactx->saltlen);
1494
1495
                if (len <= 0)
1496
                    return 0;
1497
                p.slen->return_size = len;
1498
                break;
1499
            }
1500
            }
1501
            if (value != NULL
1502
                && !OSSL_PARAM_set_utf8_string(p.slen, value))
1503
                return 0;
1504
        }
1505
    }
1506
1507
#ifdef FIPS_MODULE
1508
    if (p.verify != NULL && !OSSL_PARAM_set_uint(p.verify, prsactx->verify_message))
1509
        return 0;
1510
#endif
1511
1512
    if (!OSSL_FIPS_IND_GET_CTX_FROM_PARAM_CONDITIONAL(prsactx, p.ind,
1513
            prsactx->verify_message))
1514
        return 0;
1515
    return 1;
1516
}
1517
1518
static const OSSL_PARAM *rsa_gettable_ctx_params(ossl_unused void *vprsactx,
1519
    ossl_unused void *provctx)
1520
0
{
1521
0
    return rsa_get_ctx_params_list;
1522
0
}
1523
1524
#ifdef FIPS_MODULE
1525
static int rsa_x931_padding_allowed(PROV_RSA_CTX *ctx)
1526
{
1527
    if ((ctx->operation
1528
            & (EVP_PKEY_OP_SIGNMSG | EVP_PKEY_OP_SIGN))
1529
        != 0) {
1530
        if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE2,
1531
                ctx->libctx,
1532
                "RSA Sign set ctx", "X931 Padding",
1533
                ossl_fips_config_rsa_sign_x931_disallowed)) {
1534
            ERR_raise(ERR_LIB_PROV,
1535
                PROV_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
1536
            return 0;
1537
        }
1538
    }
1539
    return 1;
1540
}
1541
#endif
1542
1543
static int rsa_set_ctx_params(void *vprsactx, const OSSL_PARAM params[])
1544
13.7k
{
1545
13.7k
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1546
13.7k
    struct rsa_set_ctx_params_st p;
1547
13.7k
    int pad_mode;
1548
13.7k
    int saltlen;
1549
13.7k
    int count = 0;
1550
13.7k
    char mdname[OSSL_MAX_NAME_SIZE] = "", *pmdname = NULL;
1551
13.7k
    char mdprops[OSSL_MAX_PROPQUERY_SIZE] = "", *pmdprops = NULL;
1552
13.7k
    char mgf1mdname[OSSL_MAX_NAME_SIZE] = "", *pmgf1mdname = NULL;
1553
13.7k
    char mgf1mdprops[OSSL_MAX_PROPQUERY_SIZE] = "", *pmgf1mdprops = NULL;
1554
1555
13.7k
    if (prsactx == NULL)
1556
0
        return 0;
1557
    /* The processing code below doesn't handle no parameters properly */
1558
13.7k
    if (ossl_param_is_empty(params))
1559
4.52k
        return 1;
1560
1561
9.18k
    if (prsactx->flag_allow_md) {
1562
2.73k
        if (!rsa_set_ctx_params_decoder(params, &p, &count))
1563
0
            return 0;
1564
6.45k
    } else {
1565
6.45k
        if (!rsa_set_ctx_params_no_digest_decoder(params, &p, &count))
1566
0
            return 0;
1567
6.45k
    }
1568
9.18k
    if (count == 0)
1569
0
        return 1;
1570
1571
9.18k
    if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(prsactx, OSSL_FIPS_IND_SETTABLE0,
1572
9.18k
            p.ind_k))
1573
0
        return 0;
1574
1575
9.18k
    if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(prsactx, OSSL_FIPS_IND_SETTABLE1,
1576
9.18k
            p.ind_d))
1577
0
        return 0;
1578
1579
9.18k
    if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(prsactx, OSSL_FIPS_IND_SETTABLE2,
1580
9.18k
            p.ind_xpad))
1581
0
        return 0;
1582
1583
9.18k
    if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(prsactx, OSSL_FIPS_IND_SETTABLE3,
1584
9.18k
            p.ind_slen))
1585
0
        return 0;
1586
1587
9.18k
    pad_mode = prsactx->pad_mode;
1588
9.18k
    saltlen = prsactx->saltlen;
1589
1590
9.18k
    if (p.digest != NULL) {
1591
2.73k
        pmdname = mdname;
1592
2.73k
        if (!OSSL_PARAM_get_utf8_string(p.digest, &pmdname, sizeof(mdname)))
1593
0
            return 0;
1594
1595
2.73k
        if (p.propq != NULL) {
1596
0
            pmdprops = mdprops;
1597
0
            if (!OSSL_PARAM_get_utf8_string(p.propq,
1598
0
                    &pmdprops, sizeof(mdprops)))
1599
0
                return 0;
1600
0
        }
1601
2.73k
    }
1602
1603
9.18k
    if (p.pad != NULL) {
1604
3.12k
        const char *err_extra_text = NULL;
1605
1606
3.12k
        if (p.pad->data_type != OSSL_PARAM_UTF8_STRING) {
1607
            /* Support for legacy pad mode number */
1608
3.12k
            if (!OSSL_PARAM_get_int(p.pad, &pad_mode))
1609
0
                return 0;
1610
3.12k
        } else {
1611
0
            int i;
1612
1613
0
            if (p.pad->data == NULL)
1614
0
                return 0;
1615
1616
0
            for (i = 0; padding_item[i].id != 0; i++) {
1617
0
                if (strcmp(p.pad->data, padding_item[i].ptr) == 0) {
1618
0
                    pad_mode = padding_item[i].id;
1619
0
                    break;
1620
0
                }
1621
0
            }
1622
0
        }
1623
1624
3.12k
        switch (pad_mode) {
1625
0
        case RSA_PKCS1_OAEP_PADDING:
1626
            /*
1627
             * OAEP padding is for asymmetric cipher only so is not compatible
1628
             * with signature use.
1629
             */
1630
0
            err_extra_text = "OAEP padding not allowed for signing / verifying";
1631
0
            goto bad_pad;
1632
3.12k
        case RSA_PKCS1_PSS_PADDING:
1633
3.12k
            if ((prsactx->operation
1634
3.12k
                    & (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_SIGNMSG
1635
3.12k
                        | EVP_PKEY_OP_VERIFY | EVP_PKEY_OP_VERIFYMSG))
1636
3.12k
                == 0) {
1637
0
                err_extra_text = "PSS padding only allowed for sign and verify operations";
1638
0
                goto bad_pad;
1639
0
            }
1640
3.12k
            break;
1641
3.12k
        case RSA_PKCS1_PADDING:
1642
0
            err_extra_text = "PKCS#1 padding not allowed with RSA-PSS";
1643
0
            goto cont;
1644
0
        case RSA_NO_PADDING:
1645
0
            err_extra_text = "No padding not allowed with RSA-PSS";
1646
0
            goto cont;
1647
0
        case RSA_X931_PADDING:
1648
#ifdef FIPS_MODULE
1649
            /* X9.31 only allows sizes of 1024 + 256 * s (bits) */
1650
            if ((RSA_bits(prsactx->rsa) & 0xFF) != 0) {
1651
                ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
1652
                return 0;
1653
            }
1654
            /* RSA Signing with X9.31 padding is not allowed in FIPS 140-3 */
1655
            if (!rsa_x931_padding_allowed(prsactx))
1656
                return 0;
1657
#endif
1658
0
            err_extra_text = "X.931 padding not allowed with RSA-PSS";
1659
0
        cont:
1660
0
            if (RSA_test_flags(prsactx->rsa,
1661
0
                    RSA_FLAG_TYPE_MASK)
1662
0
                == RSA_FLAG_TYPE_RSA)
1663
0
                break;
1664
            /* FALLTHRU */
1665
0
        default:
1666
0
        bad_pad:
1667
0
            if (err_extra_text == NULL)
1668
0
                ERR_raise(ERR_LIB_PROV,
1669
0
                    PROV_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
1670
0
            else
1671
0
                ERR_raise_data(ERR_LIB_PROV,
1672
0
                    PROV_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE,
1673
0
                    err_extra_text);
1674
0
            return 0;
1675
3.12k
        }
1676
3.12k
    }
1677
1678
9.18k
    if (p.slen != NULL) {
1679
3.12k
        if (pad_mode != RSA_PKCS1_PSS_PADDING) {
1680
0
            ERR_raise_data(ERR_LIB_PROV, PROV_R_NOT_SUPPORTED,
1681
0
                "PSS saltlen can only be specified if "
1682
0
                "PSS padding has been specified first");
1683
0
            return 0;
1684
0
        }
1685
1686
3.12k
        if (p.slen->data_type != OSSL_PARAM_UTF8_STRING) {
1687
            /* Support for legacy pad mode number */
1688
0
            if (!OSSL_PARAM_get_int(p.slen, &saltlen))
1689
0
                return 0;
1690
3.12k
        } else {
1691
3.12k
            if (strcmp(p.slen->data, OSSL_PKEY_RSA_PSS_SALT_LEN_DIGEST) == 0)
1692
2.90k
                saltlen = RSA_PSS_SALTLEN_DIGEST;
1693
217
            else if (strcmp(p.slen->data, OSSL_PKEY_RSA_PSS_SALT_LEN_MAX) == 0)
1694
0
                saltlen = RSA_PSS_SALTLEN_MAX;
1695
217
            else if (strcmp(p.slen->data, OSSL_PKEY_RSA_PSS_SALT_LEN_AUTO) == 0)
1696
0
                saltlen = RSA_PSS_SALTLEN_AUTO;
1697
217
            else if (strcmp(p.slen->data, OSSL_PKEY_RSA_PSS_SALT_LEN_AUTO_DIGEST_MAX) == 0)
1698
0
                saltlen = RSA_PSS_SALTLEN_AUTO_DIGEST_MAX;
1699
217
            else
1700
217
                saltlen = atoi(p.slen->data);
1701
3.12k
        }
1702
1703
        /*
1704
         * RSA_PSS_SALTLEN_AUTO_DIGEST_MAX seems curiously named in this check.
1705
         * Contrary to what it's name suggests, it's the currently lowest
1706
         * saltlen number possible.
1707
         */
1708
3.12k
        if (saltlen < RSA_PSS_SALTLEN_AUTO_DIGEST_MAX) {
1709
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH);
1710
0
            return 0;
1711
0
        }
1712
1713
3.12k
        if (rsa_pss_restricted(prsactx)) {
1714
2
            switch (saltlen) {
1715
0
            case RSA_PSS_SALTLEN_AUTO:
1716
0
            case RSA_PSS_SALTLEN_AUTO_DIGEST_MAX:
1717
0
                if ((prsactx->operation
1718
0
                        & (EVP_PKEY_OP_VERIFY | EVP_PKEY_OP_VERIFYMSG))
1719
0
                    == 0) {
1720
0
                    ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH,
1721
0
                        "Cannot use autodetected salt length");
1722
0
                    return 0;
1723
0
                }
1724
0
                break;
1725
0
            case RSA_PSS_SALTLEN_DIGEST:
1726
0
                if (prsactx->min_saltlen > EVP_MD_get_size(prsactx->md)) {
1727
0
                    ERR_raise_data(ERR_LIB_PROV,
1728
0
                        PROV_R_PSS_SALTLEN_TOO_SMALL,
1729
0
                        "Should be more than %d, but would be "
1730
0
                        "set to match digest size (%d)",
1731
0
                        prsactx->min_saltlen,
1732
0
                        EVP_MD_get_size(prsactx->md));
1733
0
                    return 0;
1734
0
                }
1735
0
                break;
1736
2
            default:
1737
2
                if (saltlen >= 0 && saltlen < prsactx->min_saltlen) {
1738
0
                    ERR_raise_data(ERR_LIB_PROV,
1739
0
                        PROV_R_PSS_SALTLEN_TOO_SMALL,
1740
0
                        "Should be more than %d, "
1741
0
                        "but would be set to %d",
1742
0
                        prsactx->min_saltlen, saltlen);
1743
0
                    return 0;
1744
0
                }
1745
2
            }
1746
2
        }
1747
3.12k
    }
1748
1749
9.18k
    if (p.mgf1 != NULL) {
1750
217
        pmgf1mdname = mgf1mdname;
1751
217
        if (!OSSL_PARAM_get_utf8_string(p.mgf1, &pmgf1mdname, sizeof(mgf1mdname)))
1752
0
            return 0;
1753
1754
217
        if (p.mgf1pq != NULL) {
1755
0
            pmgf1mdprops = mgf1mdprops;
1756
0
            if (!OSSL_PARAM_get_utf8_string(p.mgf1pq,
1757
0
                    &pmgf1mdprops, sizeof(mgf1mdprops)))
1758
0
                return 0;
1759
0
        }
1760
1761
217
        if (pad_mode != RSA_PKCS1_PSS_PADDING) {
1762
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MGF1_MD);
1763
0
            return 0;
1764
0
        }
1765
217
    }
1766
1767
9.18k
    prsactx->saltlen = saltlen;
1768
9.18k
    prsactx->pad_mode = pad_mode;
1769
1770
9.18k
    if (prsactx->md == NULL && pmdname == NULL
1771
0
        && pad_mode == RSA_PKCS1_PSS_PADDING)
1772
0
        pmdname = RSA_DEFAULT_DIGEST_NAME;
1773
1774
9.18k
    if (pmgf1mdname != NULL
1775
217
        && !rsa_setup_mgf1_md(prsactx, pmgf1mdname, pmgf1mdprops))
1776
0
        return 0;
1777
1778
9.18k
    if (pmdname != NULL) {
1779
2.73k
        if (!rsa_setup_md(prsactx, pmdname, pmdprops, "RSA Sign Set Ctx"))
1780
3
            return 0;
1781
6.45k
    } else {
1782
6.45k
        if (!rsa_check_padding(prsactx, NULL, NULL, prsactx->mdnid))
1783
0
            return 0;
1784
6.45k
    }
1785
9.18k
    return 1;
1786
9.18k
}
1787
1788
static const OSSL_PARAM *rsa_settable_ctx_params(void *vprsactx,
1789
    ossl_unused void *provctx)
1790
50.8k
{
1791
50.8k
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1792
1793
50.8k
    if (prsactx != NULL && !prsactx->flag_allow_md)
1794
50.3k
        return rsa_set_ctx_params_no_digest_list;
1795
573
    return rsa_set_ctx_params_list;
1796
50.8k
}
1797
1798
static int rsa_get_ctx_md_params(void *vprsactx, OSSL_PARAM *params)
1799
0
{
1800
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1801
1802
0
    if (prsactx->mdctx == NULL)
1803
0
        return 0;
1804
1805
0
    return EVP_MD_CTX_get_params(prsactx->mdctx, params);
1806
0
}
1807
1808
static const OSSL_PARAM *rsa_gettable_ctx_md_params(void *vprsactx)
1809
0
{
1810
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1811
1812
0
    if (prsactx->md == NULL)
1813
0
        return 0;
1814
1815
0
    return EVP_MD_gettable_ctx_params(prsactx->md);
1816
0
}
1817
1818
static int rsa_set_ctx_md_params(void *vprsactx, const OSSL_PARAM params[])
1819
0
{
1820
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1821
1822
0
    if (prsactx->mdctx == NULL)
1823
0
        return 0;
1824
1825
0
    return EVP_MD_CTX_set_params(prsactx->mdctx, params);
1826
0
}
1827
1828
static const OSSL_PARAM *rsa_settable_ctx_md_params(void *vprsactx)
1829
0
{
1830
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1831
1832
0
    if (prsactx->md == NULL)
1833
0
        return 0;
1834
1835
0
    return EVP_MD_settable_ctx_params(prsactx->md);
1836
0
}
1837
1838
const OSSL_DISPATCH ossl_rsa_signature_functions[] = {
1839
    { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))rsa_newctx },
1840
    { OSSL_FUNC_SIGNATURE_SIGN_INIT, (void (*)(void))rsa_sign_init },
1841
    { OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))rsa_sign },
1842
    { OSSL_FUNC_SIGNATURE_VERIFY_INIT, (void (*)(void))rsa_verify_init },
1843
    { OSSL_FUNC_SIGNATURE_VERIFY, (void (*)(void))rsa_verify },
1844
    { OSSL_FUNC_SIGNATURE_VERIFY_RECOVER_INIT,
1845
        (void (*)(void))rsa_verify_recover_init },
1846
    { OSSL_FUNC_SIGNATURE_VERIFY_RECOVER,
1847
        (void (*)(void))rsa_verify_recover },
1848
    { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT,
1849
        (void (*)(void))rsa_digest_sign_init },
1850
    { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE,
1851
        (void (*)(void))rsa_digest_sign_update },
1852
    { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL,
1853
        (void (*)(void))rsa_digest_sign_final },
1854
    { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT,
1855
        (void (*)(void))rsa_digest_verify_init },
1856
    { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE,
1857
        (void (*)(void))rsa_digest_verify_update },
1858
    { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL,
1859
        (void (*)(void))rsa_digest_verify_final },
1860
    { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))rsa_freectx },
1861
    { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))rsa_dupctx },
1862
    { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, (void (*)(void))rsa_get_ctx_params },
1863
    { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS,
1864
        (void (*)(void))rsa_gettable_ctx_params },
1865
    { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, (void (*)(void))rsa_set_ctx_params },
1866
    { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS,
1867
        (void (*)(void))rsa_settable_ctx_params },
1868
    { OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS,
1869
        (void (*)(void))rsa_get_ctx_md_params },
1870
    { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS,
1871
        (void (*)(void))rsa_gettable_ctx_md_params },
1872
    { OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS,
1873
        (void (*)(void))rsa_set_ctx_md_params },
1874
    { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS,
1875
        (void (*)(void))rsa_settable_ctx_md_params },
1876
    OSSL_DISPATCH_END
1877
};
1878
1879
/* ------------------------------------------------------------------ */
1880
1881
/*
1882
 * So called sigalgs (composite RSA+hash) implemented below.  They
1883
 * are pretty much hard coded, and rely on the hash implementation
1884
 * being available as per what OPENSSL_NO_ macros allow.
1885
 */
1886
1887
static OSSL_FUNC_signature_query_key_types_fn rsa_sigalg_query_key_types;
1888
static OSSL_FUNC_signature_settable_ctx_params_fn rsa_sigalg_settable_ctx_params;
1889
static OSSL_FUNC_signature_set_ctx_params_fn rsa_sigalg_set_ctx_params;
1890
1891
/*
1892
 * rsa_sigalg_signverify_init() is almost like rsa_digest_signverify_init(),
1893
 * just doesn't allow fetching an MD from whatever the user chooses.
1894
 */
1895
static int rsa_sigalg_signverify_init(void *vprsactx, void *vrsa,
1896
    OSSL_FUNC_signature_set_ctx_params_fn *set_ctx_params,
1897
    const OSSL_PARAM params[],
1898
    const char *mdname,
1899
    int operation, int pad_mode,
1900
    const char *desc)
1901
0
{
1902
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1903
1904
0
    if (!ossl_prov_is_running())
1905
0
        return 0;
1906
1907
0
    if (!rsa_signverify_init(prsactx, vrsa, set_ctx_params, params, operation,
1908
0
            desc))
1909
0
        return 0;
1910
1911
    /* PSS is currently not supported as a sigalg */
1912
0
    if (prsactx->pad_mode == RSA_PKCS1_PSS_PADDING) {
1913
0
        ERR_raise(ERR_LIB_PROV, PROV_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
1914
0
        return 0;
1915
0
    }
1916
1917
0
    if (!rsa_setup_md(prsactx, mdname, NULL, desc))
1918
0
        return 0;
1919
1920
0
    prsactx->pad_mode = pad_mode;
1921
0
    prsactx->flag_sigalg = 1;
1922
0
    prsactx->flag_allow_md = 0;
1923
1924
0
    if (prsactx->mdctx == NULL) {
1925
0
        prsactx->mdctx = EVP_MD_CTX_new();
1926
0
        if (prsactx->mdctx == NULL)
1927
0
            goto error;
1928
0
    }
1929
1930
0
    if (!EVP_DigestInit_ex2(prsactx->mdctx, prsactx->md, params))
1931
0
        goto error;
1932
1933
0
    return 1;
1934
1935
0
error:
1936
0
    EVP_MD_CTX_free(prsactx->mdctx);
1937
0
    prsactx->mdctx = NULL;
1938
0
    return 0;
1939
0
}
1940
1941
static const char **rsa_sigalg_query_key_types(void)
1942
0
{
1943
0
    static const char *keytypes[] = { "RSA", NULL };
1944
1945
0
    return keytypes;
1946
0
}
1947
1948
static const OSSL_PARAM *rsa_sigalg_settable_ctx_params(void *vprsactx,
1949
    ossl_unused void *provctx)
1950
6
{
1951
6
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1952
1953
6
    if (prsactx != NULL && prsactx->operation == EVP_PKEY_OP_VERIFYMSG)
1954
0
        return rsa_sigalg_set_ctx_params_list;
1955
6
    return NULL;
1956
6
}
1957
1958
static int rsa_sigalg_set_ctx_params(void *vprsactx, const OSSL_PARAM params[])
1959
18.8k
{
1960
18.8k
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1961
18.8k
    struct rsa_sigalg_set_ctx_params_st p;
1962
1963
18.8k
    if (prsactx == NULL || !rsa_sigalg_set_ctx_params_decoder(params, &p))
1964
0
        return 0;
1965
1966
18.8k
    if (prsactx->operation == EVP_PKEY_OP_VERIFYMSG) {
1967
18.8k
        if (p.sig != NULL) {
1968
18.8k
            OPENSSL_free(prsactx->sig);
1969
18.8k
            prsactx->sig = NULL;
1970
18.8k
            prsactx->siglen = 0;
1971
18.8k
            if (!OSSL_PARAM_get_octet_string(p.sig, (void **)&prsactx->sig,
1972
18.8k
                    0, &prsactx->siglen))
1973
0
                return 0;
1974
18.8k
        }
1975
18.8k
    }
1976
18.8k
    return 1;
1977
18.8k
}
1978
1979
#define IMPL_RSA_SIGALG(md, MD)                                       \
1980
    static OSSL_FUNC_signature_sign_init_fn rsa_##md##_sign_init;     \
1981
    static OSSL_FUNC_signature_sign_message_init_fn                   \
1982
        rsa_##md##_sign_message_init;                                 \
1983
    static OSSL_FUNC_signature_verify_init_fn rsa_##md##_verify_init; \
1984
    static OSSL_FUNC_signature_verify_message_init_fn                 \
1985
        rsa_##md##_verify_message_init;                               \
1986
                                                                      \
1987
    static int                                                        \
1988
    rsa_##md##_sign_init(void *vprsactx, void *vrsa,                  \
1989
        const OSSL_PARAM params[])                                    \
1990
0
    {                                                                 \
1991
0
        static const char desc[] = "RSA Sigalg Sign Init";            \
1992
0
                                                                      \
1993
0
        return rsa_sigalg_signverify_init(vprsactx, vrsa,             \
1994
0
            rsa_sigalg_set_ctx_params,                                \
1995
0
            params, MD,                                               \
1996
0
            EVP_PKEY_OP_SIGN,                                         \
1997
0
            RSA_PKCS1_PADDING,                                        \
1998
0
            desc);                                                    \
1999
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
2000
                                                                      \
2001
    static int                                                        \
2002
    rsa_##md##_sign_message_init(void *vprsactx, void *vrsa,          \
2003
        const OSSL_PARAM params[])                                    \
2004
0
    {                                                                 \
2005
0
        static const char desc[] = "RSA Sigalg Sign Message Init";    \
2006
0
                                                                      \
2007
0
        return rsa_sigalg_signverify_init(vprsactx, vrsa,             \
2008
0
            rsa_sigalg_set_ctx_params,                                \
2009
0
            params, MD,                                               \
2010
0
            EVP_PKEY_OP_SIGNMSG,                                      \
2011
0
            RSA_PKCS1_PADDING,                                        \
2012
0
            desc);                                                    \
2013
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
2014
                                                                      \
2015
    static int                                                        \
2016
    rsa_##md##_verify_init(void *vprsactx, void *vrsa,                \
2017
        const OSSL_PARAM params[])                                    \
2018
0
    {                                                                 \
2019
0
        static const char desc[] = "RSA Sigalg Verify Init";          \
2020
0
                                                                      \
2021
0
        return rsa_sigalg_signverify_init(vprsactx, vrsa,             \
2022
0
            rsa_sigalg_set_ctx_params,                                \
2023
0
            params, MD,                                               \
2024
0
            EVP_PKEY_OP_VERIFY,                                       \
2025
0
            RSA_PKCS1_PADDING,                                        \
2026
0
            desc);                                                    \
2027
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
2028
                                                                      \
2029
    static int                                                        \
2030
    rsa_##md##_verify_recover_init(void *vprsactx, void *vrsa,        \
2031
        const OSSL_PARAM params[])                                    \
2032
0
    {                                                                 \
2033
0
        static const char desc[] = "RSA Sigalg Verify Recover Init";  \
2034
0
                                                                      \
2035
0
        return rsa_sigalg_signverify_init(vprsactx, vrsa,             \
2036
0
            rsa_sigalg_set_ctx_params,                                \
2037
0
            params, MD,                                               \
2038
0
            EVP_PKEY_OP_VERIFYRECOVER,                                \
2039
0
            RSA_PKCS1_PADDING,                                        \
2040
0
            desc);                                                    \
2041
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
2042
                                                                      \
2043
    static int                                                        \
2044
    rsa_##md##_verify_message_init(void *vprsactx, void *vrsa,        \
2045
        const OSSL_PARAM params[])                                    \
2046
0
    {                                                                 \
2047
0
        static const char desc[] = "RSA Sigalg Verify Message Init";  \
2048
0
                                                                      \
2049
0
        return rsa_sigalg_signverify_init(vprsactx, vrsa,             \
2050
0
            rsa_sigalg_set_ctx_params,                                \
2051
0
            params, MD,                                               \
2052
0
            EVP_PKEY_OP_VERIFYMSG,                                    \
2053
0
            RSA_PKCS1_PADDING,                                        \
2054
0
            desc);                                                    \
2055
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
2056
                                                                      \
2057
    const OSSL_DISPATCH ossl_rsa_##md##_signature_functions[] = {     \
2058
        { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))rsa_newctx },   \
2059
        { OSSL_FUNC_SIGNATURE_SIGN_INIT,                              \
2060
            (void (*)(void))rsa_##md##_sign_init },                   \
2061
        { OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))rsa_sign },       \
2062
        { OSSL_FUNC_SIGNATURE_SIGN_MESSAGE_INIT,                      \
2063
            (void (*)(void))rsa_##md##_sign_message_init },           \
2064
        { OSSL_FUNC_SIGNATURE_SIGN_MESSAGE_UPDATE,                    \
2065
            (void (*)(void))rsa_signverify_message_update },          \
2066
        { OSSL_FUNC_SIGNATURE_SIGN_MESSAGE_FINAL,                     \
2067
            (void (*)(void))rsa_sign_message_final },                 \
2068
        { OSSL_FUNC_SIGNATURE_VERIFY_INIT,                            \
2069
            (void (*)(void))rsa_##md##_verify_init },                 \
2070
        { OSSL_FUNC_SIGNATURE_VERIFY,                                 \
2071
            (void (*)(void))rsa_verify },                             \
2072
        { OSSL_FUNC_SIGNATURE_VERIFY_MESSAGE_INIT,                    \
2073
            (void (*)(void))rsa_##md##_verify_message_init },         \
2074
        { OSSL_FUNC_SIGNATURE_VERIFY_MESSAGE_UPDATE,                  \
2075
            (void (*)(void))rsa_signverify_message_update },          \
2076
        { OSSL_FUNC_SIGNATURE_VERIFY_MESSAGE_FINAL,                   \
2077
            (void (*)(void))rsa_verify_message_final },               \
2078
        { OSSL_FUNC_SIGNATURE_VERIFY_RECOVER_INIT,                    \
2079
            (void (*)(void))rsa_##md##_verify_recover_init },         \
2080
        { OSSL_FUNC_SIGNATURE_VERIFY_RECOVER,                         \
2081
            (void (*)(void))rsa_verify_recover },                     \
2082
        { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))rsa_freectx }, \
2083
        { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))rsa_dupctx },   \
2084
        { OSSL_FUNC_SIGNATURE_QUERY_KEY_TYPES,                        \
2085
            (void (*)(void))rsa_sigalg_query_key_types },             \
2086
        { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS,                         \
2087
            (void (*)(void))rsa_get_ctx_params },                     \
2088
        { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS,                    \
2089
            (void (*)(void))rsa_gettable_ctx_params },                \
2090
        { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS,                         \
2091
            (void (*)(void))rsa_sigalg_set_ctx_params },              \
2092
        { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS,                    \
2093
            (void (*)(void))rsa_sigalg_settable_ctx_params },         \
2094
        OSSL_DISPATCH_END                                             \
2095
    }
2096
2097
#if !defined(OPENSSL_NO_RMD160) && !defined(FIPS_MODULE)
2098
IMPL_RSA_SIGALG(ripemd160, "RIPEMD160");
2099
#endif
2100
IMPL_RSA_SIGALG(sha1, "SHA1");
2101
IMPL_RSA_SIGALG(sha224, "SHA2-224");
2102
IMPL_RSA_SIGALG(sha256, "SHA2-256");
2103
IMPL_RSA_SIGALG(sha384, "SHA2-384");
2104
IMPL_RSA_SIGALG(sha512, "SHA2-512");
2105
IMPL_RSA_SIGALG(sha512_224, "SHA2-512/224");
2106
IMPL_RSA_SIGALG(sha512_256, "SHA2-512/256");
2107
IMPL_RSA_SIGALG(sha3_224, "SHA3-224");
2108
IMPL_RSA_SIGALG(sha3_256, "SHA3-256");
2109
IMPL_RSA_SIGALG(sha3_384, "SHA3-384");
2110
IMPL_RSA_SIGALG(sha3_512, "SHA3-512");
2111
#if !defined(OPENSSL_NO_SM3) && !defined(FIPS_MODULE)
2112
IMPL_RSA_SIGALG(sm3, "SM3");
2113
#endif