Coverage Report

Created: 2026-09-12 06:55

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
45.9k
#define rsa_pss_restricted(prsactx) (prsactx->min_saltlen != -1)
167
168
static int rsa_get_md_size(const PROV_RSA_CTX *prsactx)
169
26.7k
{
170
26.7k
    int md_size;
171
172
26.7k
    if (prsactx->md != NULL) {
173
26.7k
        md_size = EVP_MD_get_size(prsactx->md);
174
26.7k
        if (md_size <= 0)
175
0
            return 0;
176
26.7k
        return md_size;
177
26.7k
    }
178
0
    return 0;
179
26.7k
}
180
181
static int rsa_check_padding(const PROV_RSA_CTX *prsactx,
182
    const char *mdname, const char *mgf1_mdname,
183
    int mdnid)
184
87.3k
{
185
87.3k
    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
42.0k
    case RSA_PKCS1_PSS_PADDING:
199
42.0k
        if (rsa_pss_restricted(prsactx))
200
1.80k
            if ((mdname != NULL && !EVP_MD_is_a(prsactx->md, mdname))
201
1.77k
                || (mgf1_mdname != NULL
202
437
                    && !EVP_MD_is_a(prsactx->mgf1_md, mgf1_mdname))) {
203
26
                ERR_raise(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED);
204
26
                return 0;
205
26
            }
206
42.0k
        break;
207
45.2k
    default:
208
45.2k
        break;
209
87.3k
    }
210
211
87.3k
    return 1;
212
87.3k
}
213
214
static int rsa_check_parameters(PROV_RSA_CTX *prsactx, int min_saltlen)
215
505
{
216
505
    if (prsactx->pad_mode == RSA_PKCS1_PSS_PADDING) {
217
505
        int max_saltlen;
218
219
        /* See if minimum salt length exceeds maximum possible */
220
505
        max_saltlen = RSA_size(prsactx->rsa) - EVP_MD_get_size(prsactx->md);
221
505
        if ((RSA_bits(prsactx->rsa) & 0x7) == 1)
222
174
            max_saltlen--;
223
505
        if (min_saltlen < 0 || min_saltlen > max_saltlen) {
224
24
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH);
225
24
            return 0;
226
24
        }
227
481
        prsactx->min_saltlen = min_saltlen;
228
481
    }
229
481
    return 1;
230
505
}
231
232
static void *rsa_newctx(void *provctx, const char *propq)
233
45.8k
{
234
45.8k
    PROV_RSA_CTX *prsactx = NULL;
235
45.8k
    char *propq_copy = NULL;
236
237
45.8k
    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
45.8k
    if ((prsactx = OPENSSL_zalloc(sizeof(PROV_RSA_CTX))) == NULL
247
45.8k
        || (propq != NULL
248
0
            && (propq_copy = OPENSSL_strdup(propq)) == NULL)) {
249
0
        OPENSSL_free(prsactx);
250
0
        return NULL;
251
0
    }
252
253
45.8k
    OSSL_FIPS_IND_INIT(prsactx)
254
45.8k
    prsactx->libctx = PROV_LIBCTX_OF(provctx);
255
45.8k
    prsactx->flag_allow_md = 1;
256
#ifdef FIPS_MODULE
257
    prsactx->verify_message = 1;
258
#endif
259
45.8k
    prsactx->propq = propq_copy;
260
    /* Maximum up to digest length for sign, auto for verify */
261
45.8k
    prsactx->saltlen = RSA_PSS_SALTLEN_AUTO_DIGEST_MAX;
262
45.8k
    prsactx->min_saltlen = -1;
263
45.8k
    return prsactx;
264
45.8k
}
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
45.9k
{
384
45.9k
    EVP_MD *md = NULL;
385
386
45.9k
    if (mdprops == NULL)
387
45.9k
        mdprops = ctx->propq;
388
389
45.9k
    if (mdname != NULL) {
390
45.9k
        int md_nid;
391
45.9k
        size_t mdname_len = strlen(mdname);
392
393
45.9k
        md = EVP_MD_fetch(ctx->libctx, mdname, mdprops);
394
395
45.9k
        if (md == NULL) {
396
65
            ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
397
65
                "%s could not be fetched", mdname);
398
65
            goto err;
399
65
        }
400
45.8k
        md_nid = ossl_digest_rsa_sign_get_md_nid(md);
401
45.8k
        if (md_nid == NID_undef) {
402
0
            ERR_raise_data(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED,
403
0
                "digest=%s", mdname);
404
0
            goto err;
405
0
        }
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
45.8k
        if (EVP_MD_xof(md)
412
45.8k
            /* && 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
45.8k
        if (!rsa_check_padding(ctx, mdname, NULL, md_nid))
433
26
            goto err;
434
45.8k
        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
45.8k
        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
45.8k
        if (!ctx->mgf1_md_set) {
451
45.3k
            if (!EVP_MD_up_ref(md)) {
452
0
                goto err;
453
0
            }
454
45.3k
            EVP_MD_free(ctx->mgf1_md);
455
45.3k
            ctx->mgf1_md = md;
456
45.3k
            ctx->mgf1_mdnid = md_nid;
457
45.3k
            OPENSSL_strlcpy(ctx->mgf1_mdname, mdname, sizeof(ctx->mgf1_mdname));
458
45.3k
        }
459
460
45.8k
        EVP_MD_CTX_free(ctx->mdctx);
461
45.8k
        EVP_MD_free(ctx->md);
462
463
45.8k
        ctx->mdctx = NULL;
464
45.8k
        ctx->md = md;
465
45.8k
        ctx->mdnid = md_nid;
466
45.8k
        OPENSSL_strlcpy(ctx->mdname, mdname, sizeof(ctx->mdname));
467
45.8k
    }
468
469
45.8k
    return 1;
470
91
err:
471
91
    EVP_MD_free(md);
472
91
    return 0;
473
45.9k
}
474
475
static int rsa_setup_mgf1_md(PROV_RSA_CTX *ctx, const char *mdname,
476
    const char *mdprops)
477
3.33k
{
478
3.33k
    size_t len;
479
3.33k
    EVP_MD *md = NULL;
480
3.33k
    int mdnid;
481
482
3.33k
    if (mdprops == NULL)
483
3.33k
        mdprops = ctx->propq;
484
485
3.33k
    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
3.33k
    if ((mdnid = ossl_digest_rsa_sign_get_md_nid(md)) <= 0
492
3.33k
        || !rsa_check_padding(ctx, NULL, mdname, mdnid)) {
493
0
        if (mdnid <= 0)
494
0
            ERR_raise_data(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED,
495
0
                "digest=%s", mdname);
496
0
        EVP_MD_free(md);
497
0
        return 0;
498
0
    }
499
3.33k
    len = OPENSSL_strlcpy(ctx->mgf1_mdname, mdname, sizeof(ctx->mgf1_mdname));
500
3.33k
    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
3.33k
    EVP_MD_free(ctx->mgf1_md);
508
3.33k
    ctx->mgf1_md = md;
509
3.33k
    ctx->mgf1_mdnid = mdnid;
510
3.33k
    ctx->mgf1_md_set = 1;
511
3.33k
    return 1;
512
3.33k
}
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
45.8k
{
520
45.8k
    int protect;
521
522
45.8k
    if (!ossl_prov_is_running() || prsactx == NULL)
523
0
        return 0;
524
525
45.8k
    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
45.8k
    if (vrsa != NULL) {
531
45.8k
        if (!RSA_up_ref(vrsa))
532
0
            return 0;
533
45.8k
        RSA_free(prsactx->rsa);
534
45.8k
        prsactx->rsa = vrsa;
535
45.8k
    }
536
45.8k
    if (!ossl_rsa_key_op_get_protect(prsactx->rsa, operation, &protect))
537
0
        return 0;
538
539
45.8k
    prsactx->operation = operation;
540
45.8k
    prsactx->flag_allow_update = 1;
541
45.8k
    prsactx->flag_allow_final = 1;
542
45.8k
    prsactx->flag_allow_oneshot = 1;
543
544
    /* Maximize up to digest length for sign, auto for verify */
545
45.8k
    prsactx->saltlen = RSA_PSS_SALTLEN_AUTO_DIGEST_MAX;
546
45.8k
    prsactx->min_saltlen = -1;
547
548
45.8k
    switch (RSA_test_flags(prsactx->rsa, RSA_FLAG_TYPE_MASK)) {
549
45.3k
    case RSA_FLAG_TYPE_RSA:
550
45.3k
        prsactx->pad_mode = RSA_PKCS1_PADDING;
551
45.3k
        break;
552
534
    case RSA_FLAG_TYPE_RSASSAPSS:
553
534
        prsactx->pad_mode = RSA_PKCS1_PSS_PADDING;
554
555
534
        {
556
534
            const RSA_PSS_PARAMS_30 *pss = ossl_rsa_get0_pss_params_30(prsactx->rsa);
557
558
534
            if (!ossl_rsa_pss_params_30_is_unrestricted(pss)) {
559
509
                int md_nid = ossl_rsa_pss_params_30_hashalg(pss);
560
509
                int mgf1md_nid = ossl_rsa_pss_params_30_maskgenhashalg(pss);
561
509
                int min_saltlen = ossl_rsa_pss_params_30_saltlen(pss);
562
509
                const char *mdname, *mgf1mdname;
563
509
                size_t len;
564
565
509
                mdname = ossl_rsa_oaeppss_nid2name(md_nid);
566
509
                mgf1mdname = ossl_rsa_oaeppss_nid2name(mgf1md_nid);
567
568
509
                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
507
                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
505
                len = OPENSSL_strlcpy(prsactx->mdname, mdname,
580
505
                    sizeof(prsactx->mdname));
581
505
                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
505
                len = OPENSSL_strlcpy(prsactx->mgf1_mdname, mgf1mdname,
587
505
                    sizeof(prsactx->mgf1_mdname));
588
505
                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
505
                prsactx->saltlen = min_saltlen;
594
595
                /* call rsa_setup_mgf1_md before rsa_setup_md to avoid duplication */
596
505
                if (!rsa_setup_mgf1_md(prsactx, mgf1mdname, prsactx->propq)
597
505
                    || !rsa_setup_md(prsactx, mdname, prsactx->propq, desc)
598
505
                    || !rsa_check_parameters(prsactx, min_saltlen))
599
24
                    return 0;
600
505
            }
601
534
        }
602
603
506
        break;
604
506
    default:
605
0
        ERR_raise(ERR_LIB_PROV, PROV_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
606
0
        return 0;
607
45.8k
    }
608
609
45.8k
    OSSL_FIPS_IND_SET_APPROVED(prsactx)
610
45.8k
    if (!set_ctx_params(prsactx, params))
611
4
        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
45.8k
    return 1;
619
45.8k
}
620
621
static int setup_tbuf(PROV_RSA_CTX *ctx)
622
17.6k
{
623
17.6k
    if (ctx->tbuf != NULL)
624
0
        return 1;
625
17.6k
    if ((ctx->tbuf = OPENSSL_malloc(RSA_size(ctx->rsa))) == NULL)
626
18
        return 0;
627
17.6k
    return 1;
628
17.6k
}
629
630
static void clean_tbuf(PROV_RSA_CTX *ctx)
631
74.4k
{
632
74.4k
    if (ctx->tbuf != NULL)
633
18.9k
        OPENSSL_cleanse(ctx->tbuf, RSA_size(ctx->rsa));
634
74.4k
}
635
636
static void free_tbuf(PROV_RSA_CTX *ctx)
637
73.1k
{
638
73.1k
    clean_tbuf(ctx);
639
73.1k
    OPENSSL_free(ctx->tbuf);
640
73.1k
    ctx->tbuf = NULL;
641
73.1k
}
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
10.3k
{
691
10.3k
    int ret;
692
10.3k
    size_t rsasize = RSA_size(prsactx->rsa);
693
10.3k
    size_t mdsize = rsa_get_md_size(prsactx);
694
695
10.3k
    if (!ossl_prov_is_running())
696
0
        return 0;
697
698
10.3k
    if (sig == NULL) {
699
5.19k
        *siglen = rsasize;
700
5.19k
        return 1;
701
5.19k
    }
702
703
5.19k
    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
5.19k
    if (mdsize != 0) {
710
5.19k
        if (tbslen != mdsize) {
711
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_LENGTH);
712
0
            return 0;
713
0
        }
714
715
5.19k
#ifndef FIPS_MODULE
716
5.19k
        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
5.19k
#endif
735
5.19k
        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
3.88k
        case RSA_PKCS1_PADDING: {
754
3.88k
            unsigned int sltmp;
755
756
3.88k
            ret = RSA_sign(prsactx->mdnid, tbs, (unsigned int)tbslen,
757
3.88k
                sig, &sltmp, prsactx->rsa);
758
3.88k
            if (ret <= 0) {
759
0
                ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
760
0
                return 0;
761
0
            }
762
3.88k
            ret = sltmp;
763
3.88k
        } break;
764
765
1.30k
        case RSA_PKCS1_PSS_PADDING: {
766
1.30k
            int saltlen;
767
768
            /* Check PSS restrictions */
769
1.30k
            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.30k
            if (!setup_tbuf(prsactx))
797
0
                return 0;
798
1.30k
            saltlen = prsactx->saltlen;
799
1.30k
            if (!ossl_rsa_padding_add_PKCS1_PSS_mgf1(prsactx->rsa,
800
1.30k
                    prsactx->tbuf, tbs,
801
1.30k
                    prsactx->md, prsactx->mgf1_md,
802
1.30k
                    &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.30k
            ret = RSA_private_encrypt(RSA_size(prsactx->rsa), prsactx->tbuf,
811
1.30k
                sig, prsactx->rsa, RSA_NO_PADDING);
812
1.30k
            clean_tbuf(prsactx);
813
1.30k
        } 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
5.19k
        }
820
5.19k
    } else {
821
0
        ret = RSA_private_encrypt((int)tbslen, tbs, sig, prsactx->rsa,
822
0
            prsactx->pad_mode);
823
0
    }
824
825
5.19k
#ifndef FIPS_MODULE
826
5.19k
end:
827
5.19k
#endif
828
5.19k
    if (ret <= 0) {
829
0
        ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
830
0
        return 0;
831
0
    }
832
833
5.19k
    *siglen = ret;
834
5.19k
    return 1;
835
5.19k
}
836
837
static int rsa_signverify_message_update(void *vprsactx,
838
    const unsigned char *data,
839
    size_t datalen)
840
27.2k
{
841
27.2k
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
842
843
27.2k
    if (prsactx == NULL || prsactx->mdctx == NULL)
844
0
        return 0;
845
846
27.2k
    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
27.2k
    prsactx->flag_allow_oneshot = 0;
851
852
27.2k
    return EVP_DigestUpdate(prsactx->mdctx, data, datalen);
853
27.2k
}
854
855
static int rsa_sign_message_final(void *vprsactx, unsigned char *sig,
856
    size_t *siglen, size_t sigsize)
857
10.3k
{
858
10.3k
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
859
10.3k
    unsigned char digest[EVP_MAX_MD_SIZE];
860
10.3k
    unsigned int dlen = 0;
861
862
10.3k
    if (!ossl_prov_is_running() || prsactx == NULL)
863
0
        return 0;
864
10.3k
    if (prsactx->mdctx == NULL)
865
0
        return 0;
866
10.3k
    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
10.3k
    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
5.19k
        if (!EVP_DigestFinal_ex(prsactx->mdctx, digest, &dlen))
881
0
            return 0;
882
883
5.19k
        prsactx->flag_allow_update = 0;
884
5.19k
        prsactx->flag_allow_oneshot = 0;
885
5.19k
        prsactx->flag_allow_final = 0;
886
5.19k
    }
887
888
10.3k
    return rsa_sign_directly(prsactx, sig, siglen, sigsize, digest, dlen);
889
10.3k
}
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
677
{
1047
677
    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
677
    return rsa_signverify_init(prsactx, vrsa, rsa_set_ctx_params, params,
1055
677
        EVP_PKEY_OP_VERIFY, "RSA Verify Init");
1056
677
}
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
22.7k
{
1062
22.7k
    size_t rslen;
1063
1064
22.7k
    if (!ossl_prov_is_running())
1065
0
        return 0;
1066
22.7k
    if (prsactx->md != NULL) {
1067
22.7k
        switch (prsactx->pad_mode) {
1068
6.34k
        case RSA_PKCS1_PADDING:
1069
6.34k
            if (!RSA_verify(prsactx->mdnid, tbs, (unsigned int)tbslen,
1070
6.34k
                    sig, (unsigned int)siglen, prsactx->rsa)) {
1071
5.82k
                ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
1072
5.82k
                return 0;
1073
5.82k
            }
1074
523
            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
16.3k
        case RSA_PKCS1_PSS_PADDING: {
1084
16.3k
            int ret;
1085
16.3k
            int saltlen;
1086
16.3k
            size_t mdsize;
1087
1088
            /*
1089
             * We need to check this for the RSA_verify_PKCS1_PSS_mgf1()
1090
             * call
1091
             */
1092
16.3k
            mdsize = rsa_get_md_size(prsactx);
1093
16.3k
            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
16.3k
            if (!setup_tbuf(prsactx))
1101
18
                return 0;
1102
16.3k
            ret = RSA_public_decrypt((int)siglen, sig, prsactx->tbuf,
1103
16.3k
                prsactx->rsa, RSA_NO_PADDING);
1104
16.3k
            if (ret <= 0) {
1105
4.20k
                ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
1106
4.20k
                return 0;
1107
4.20k
            }
1108
12.1k
            saltlen = prsactx->saltlen;
1109
12.1k
            ret = ossl_rsa_verify_PKCS1_PSS_mgf1(prsactx->rsa, tbs,
1110
12.1k
                prsactx->md, prsactx->mgf1_md,
1111
12.1k
                prsactx->tbuf,
1112
12.1k
                &saltlen);
1113
12.1k
            if (ret <= 0) {
1114
12.1k
                ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
1115
12.1k
                return 0;
1116
12.1k
            }
1117
#ifdef FIPS_MODULE
1118
            if (!rsa_pss_saltlen_check_passed(prsactx, "RSA Verify", saltlen))
1119
                return 0;
1120
#endif
1121
4
            return 1;
1122
12.1k
        }
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
22.7k
        }
1128
22.7k
    } 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
22.0k
{
1151
22.0k
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1152
22.0k
    OSSL_PARAM params[2];
1153
1154
22.0k
    params[0] = OSSL_PARAM_construct_octet_string(OSSL_SIGNATURE_PARAM_SIGNATURE,
1155
22.0k
        (unsigned char *)sig, siglen);
1156
22.0k
    params[1] = OSSL_PARAM_construct_end();
1157
22.0k
    return rsa_sigalg_set_ctx_params(prsactx, params);
1158
22.0k
}
1159
1160
static int rsa_verify_message_final(void *vprsactx)
1161
22.0k
{
1162
22.0k
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1163
22.0k
    unsigned char digest[EVP_MAX_MD_SIZE];
1164
22.0k
    unsigned int dlen = 0;
1165
1166
22.0k
    if (!ossl_prov_is_running() || prsactx == NULL)
1167
0
        return 0;
1168
22.0k
    if (prsactx->mdctx == NULL)
1169
0
        return 0;
1170
22.0k
    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
22.0k
    if (!EVP_DigestFinal_ex(prsactx->mdctx, digest, &dlen))
1180
0
        return 0;
1181
1182
22.0k
    prsactx->flag_allow_update = 0;
1183
22.0k
    prsactx->flag_allow_final = 0;
1184
22.0k
    prsactx->flag_allow_oneshot = 0;
1185
1186
22.0k
    return rsa_verify_directly(prsactx, prsactx->sig, prsactx->siglen,
1187
22.0k
        digest, dlen);
1188
22.0k
}
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
677
{
1198
677
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1199
1200
677
    if (!ossl_prov_is_running() || prsactx == NULL)
1201
0
        return 0;
1202
677
    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
677
    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
677
    return rsa_verify_directly(prsactx, sig, siglen, tbs, tbslen);
1212
677
}
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
45.2k
{
1220
45.2k
    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
45.2k
    if (!rsa_signverify_init(prsactx, vrsa, rsa_set_ctx_params, params,
1228
45.2k
            operation, desc))
1229
32
        return 0;
1230
1231
45.1k
    if (mdname != NULL
1232
        /* was rsa_setup_md already called in rsa_signverify_init()? */
1233
45.1k
        && (mdname[0] == '\0' || OPENSSL_strcasecmp(prsactx->mdname, mdname) != 0)
1234
39.3k
        && !rsa_setup_md(prsactx, mdname, prsactx->propq, desc))
1235
87
        return 0;
1236
1237
45.0k
    prsactx->flag_allow_md = 0;
1238
1239
45.0k
    if (prsactx->mdctx == NULL) {
1240
45.0k
        prsactx->mdctx = EVP_MD_CTX_new();
1241
45.0k
        if (prsactx->mdctx == NULL)
1242
0
            goto error;
1243
45.0k
    }
1244
1245
45.0k
    if (!EVP_DigestInit_ex2(prsactx->mdctx, prsactx->md, params))
1246
0
        goto error;
1247
1248
45.0k
    return 1;
1249
1250
0
error:
1251
0
    EVP_MD_CTX_free(prsactx->mdctx);
1252
0
    prsactx->mdctx = NULL;
1253
0
    return 0;
1254
45.0k
}
1255
1256
static int rsa_digest_sign_init(void *vprsactx, const char *mdname,
1257
    void *vrsa, const OSSL_PARAM params[])
1258
23.0k
{
1259
23.0k
    if (!ossl_prov_is_running())
1260
0
        return 0;
1261
23.0k
    return rsa_digest_signverify_init(vprsactx, mdname, vrsa,
1262
23.0k
        params, EVP_PKEY_OP_SIGNMSG,
1263
23.0k
        "RSA Digest Sign Init");
1264
23.0k
}
1265
1266
static int rsa_digest_sign_update(void *vprsactx, const unsigned char *data,
1267
    size_t datalen)
1268
5.19k
{
1269
5.19k
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1270
1271
5.19k
    if (prsactx == NULL)
1272
0
        return 0;
1273
    /* Sigalg implementations shouldn't do digest_sign */
1274
5.19k
    if (prsactx->flag_sigalg)
1275
0
        return 0;
1276
1277
5.19k
    return rsa_signverify_message_update(prsactx, data, datalen);
1278
5.19k
}
1279
1280
static int rsa_digest_sign_final(void *vprsactx, unsigned char *sig,
1281
    size_t *siglen, size_t sigsize)
1282
10.3k
{
1283
10.3k
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1284
10.3k
    int ok = 0;
1285
1286
10.3k
    if (prsactx == NULL)
1287
0
        return 0;
1288
    /* Sigalg implementations shouldn't do digest_sign */
1289
10.3k
    if (prsactx->flag_sigalg)
1290
0
        return 0;
1291
1292
10.3k
    if (rsa_sign_message_final(prsactx, sig, siglen, sigsize))
1293
10.3k
        ok = 1;
1294
1295
10.3k
    prsactx->flag_allow_md = 1;
1296
1297
10.3k
    return ok;
1298
10.3k
}
1299
1300
static int rsa_digest_verify_init(void *vprsactx, const char *mdname,
1301
    void *vrsa, const OSSL_PARAM params[])
1302
22.1k
{
1303
22.1k
    if (!ossl_prov_is_running())
1304
0
        return 0;
1305
22.1k
    return rsa_digest_signverify_init(vprsactx, mdname, vrsa,
1306
22.1k
        params, EVP_PKEY_OP_VERIFYMSG,
1307
22.1k
        "RSA Digest Verify Init");
1308
22.1k
}
1309
1310
static int rsa_digest_verify_update(void *vprsactx, const unsigned char *data,
1311
    size_t datalen)
1312
22.0k
{
1313
22.0k
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1314
1315
22.0k
    if (prsactx == NULL)
1316
0
        return 0;
1317
    /* Sigalg implementations shouldn't do digest_sign */
1318
22.0k
    if (prsactx->flag_sigalg)
1319
0
        return 0;
1320
1321
22.0k
    return rsa_signverify_message_update(prsactx, data, datalen);
1322
22.0k
}
1323
1324
int rsa_digest_verify_final(void *vprsactx, const unsigned char *sig,
1325
    size_t siglen)
1326
22.0k
{
1327
22.0k
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1328
22.0k
    int ok = 0;
1329
1330
22.0k
    if (prsactx == NULL)
1331
0
        return 0;
1332
    /* Sigalg implementations shouldn't do digest_verify */
1333
22.0k
    if (prsactx->flag_sigalg)
1334
0
        return 0;
1335
1336
22.0k
    if (rsa_verify_set_sig(prsactx, sig, siglen)
1337
22.0k
        && rsa_verify_message_final(vprsactx))
1338
489
        ok = 1;
1339
1340
22.0k
    prsactx->flag_allow_md = 1;
1341
1342
22.0k
    return ok;
1343
22.0k
}
1344
1345
static void rsa_freectx(void *vprsactx)
1346
73.1k
{
1347
73.1k
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1348
1349
73.1k
    if (prsactx == NULL)
1350
0
        return;
1351
1352
73.1k
    EVP_MD_CTX_free(prsactx->mdctx);
1353
73.1k
    EVP_MD_free(prsactx->md);
1354
73.1k
    EVP_MD_free(prsactx->mgf1_md);
1355
73.1k
    OPENSSL_free(prsactx->sig);
1356
73.1k
    OPENSSL_free(prsactx->propq);
1357
73.1k
    free_tbuf(prsactx);
1358
73.1k
    RSA_free(prsactx->rsa);
1359
1360
73.1k
    OPENSSL_clear_free(prsactx, sizeof(*prsactx));
1361
73.1k
}
1362
1363
static void *rsa_dupctx(void *vprsactx)
1364
27.2k
{
1365
27.2k
    PROV_RSA_CTX *srcctx = (PROV_RSA_CTX *)vprsactx;
1366
27.2k
    PROV_RSA_CTX *dstctx;
1367
1368
27.2k
    if (!ossl_prov_is_running())
1369
0
        return NULL;
1370
1371
27.2k
    dstctx = OPENSSL_zalloc(sizeof(*srcctx));
1372
27.2k
    if (dstctx == NULL)
1373
0
        return NULL;
1374
1375
27.2k
    *dstctx = *srcctx;
1376
27.2k
    dstctx->rsa = NULL;
1377
27.2k
    dstctx->md = NULL;
1378
27.2k
    dstctx->mgf1_md = NULL;
1379
27.2k
    dstctx->mdctx = NULL;
1380
27.2k
    dstctx->tbuf = NULL;
1381
27.2k
    dstctx->propq = NULL;
1382
27.2k
    dstctx->sig = NULL;
1383
1384
27.2k
    if (srcctx->rsa != NULL && !RSA_up_ref(srcctx->rsa))
1385
0
        goto err;
1386
27.2k
    dstctx->rsa = srcctx->rsa;
1387
1388
27.2k
    if (srcctx->md != NULL && !EVP_MD_up_ref(srcctx->md))
1389
0
        goto err;
1390
27.2k
    dstctx->md = srcctx->md;
1391
1392
27.2k
    if (srcctx->mgf1_md != NULL && !EVP_MD_up_ref(srcctx->mgf1_md))
1393
0
        goto err;
1394
27.2k
    dstctx->mgf1_md = srcctx->mgf1_md;
1395
1396
27.2k
    if (srcctx->mdctx != NULL) {
1397
27.2k
        dstctx->mdctx = EVP_MD_CTX_new();
1398
27.2k
        if (dstctx->mdctx == NULL
1399
27.2k
            || !EVP_MD_CTX_copy_ex(dstctx->mdctx, srcctx->mdctx))
1400
0
            goto err;
1401
27.2k
    }
1402
1403
27.2k
    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
27.2k
    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
27.2k
    return dstctx;
1416
0
err:
1417
0
    rsa_freectx(dstctx);
1418
0
    return NULL;
1419
27.2k
}
1420
1421
static int rsa_get_ctx_params(void *vprsactx, OSSL_PARAM *params)
1422
0
{
1423
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1424
0
    struct rsa_get_ctx_params_st p;
1425
1426
0
    if (prsactx == NULL || !rsa_get_ctx_params_decoder(params, &p))
1427
0
        return 0;
1428
1429
0
    if (p.algid != NULL) {
1430
        /* The Algorithm Identifier of the combined signature algorithm */
1431
0
        unsigned char aid_buf[128];
1432
0
        unsigned char *aid;
1433
0
        size_t aid_len;
1434
1435
0
        aid = rsa_generate_signature_aid(prsactx, aid_buf,
1436
0
            sizeof(aid_buf), &aid_len);
1437
0
        if (aid == NULL || !OSSL_PARAM_set_octet_string(p.algid, aid, aid_len))
1438
0
            return 0;
1439
0
    }
1440
1441
0
    if (p.pad != NULL) {
1442
0
        if (p.pad->data_type != OSSL_PARAM_UTF8_STRING) {
1443
0
            if (!OSSL_PARAM_set_int(p.pad, prsactx->pad_mode))
1444
0
                return 0;
1445
0
        } else {
1446
0
            int i;
1447
0
            const char *word = NULL;
1448
1449
0
            for (i = 0; padding_item[i].id != 0; i++) {
1450
0
                if (prsactx->pad_mode == (int)padding_item[i].id) {
1451
0
                    word = padding_item[i].ptr;
1452
0
                    break;
1453
0
                }
1454
0
            }
1455
1456
0
            if (word != NULL) {
1457
0
                if (!OSSL_PARAM_set_utf8_string(p.pad, word))
1458
0
                    return 0;
1459
0
            } else {
1460
0
                ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
1461
0
            }
1462
0
        }
1463
0
    }
1464
1465
0
    if (p.digest != NULL && !OSSL_PARAM_set_utf8_string(p.digest, prsactx->mdname))
1466
0
        return 0;
1467
1468
0
    if (p.mgf1 != NULL && !OSSL_PARAM_set_utf8_string(p.mgf1, prsactx->mgf1_mdname))
1469
0
        return 0;
1470
1471
0
    if (p.slen != NULL) {
1472
0
        if (p.slen->data_type != OSSL_PARAM_UTF8_STRING) {
1473
0
            if (!OSSL_PARAM_set_int(p.slen, prsactx->saltlen))
1474
0
                return 0;
1475
0
        } else {
1476
0
            const char *value = NULL;
1477
1478
0
            switch (prsactx->saltlen) {
1479
0
            case RSA_PSS_SALTLEN_DIGEST:
1480
0
                value = OSSL_PKEY_RSA_PSS_SALT_LEN_DIGEST;
1481
0
                break;
1482
0
            case RSA_PSS_SALTLEN_MAX:
1483
0
                value = OSSL_PKEY_RSA_PSS_SALT_LEN_MAX;
1484
0
                break;
1485
0
            case RSA_PSS_SALTLEN_AUTO:
1486
0
                value = OSSL_PKEY_RSA_PSS_SALT_LEN_AUTO;
1487
0
                break;
1488
0
            case RSA_PSS_SALTLEN_AUTO_DIGEST_MAX:
1489
0
                value = OSSL_PKEY_RSA_PSS_SALT_LEN_AUTO_DIGEST_MAX;
1490
0
                break;
1491
0
            default: {
1492
0
                int len = BIO_snprintf(p.slen->data, p.slen->data_size, "%d",
1493
0
                    prsactx->saltlen);
1494
1495
0
                if (len <= 0)
1496
0
                    return 0;
1497
0
                p.slen->return_size = len;
1498
0
                break;
1499
0
            }
1500
0
            }
1501
0
            if (value != NULL
1502
0
                && !OSSL_PARAM_set_utf8_string(p.slen, value))
1503
0
                return 0;
1504
0
        }
1505
0
    }
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
0
    if (!OSSL_FIPS_IND_GET_CTX_FROM_PARAM(prsactx, p.ind))
1513
0
        return 0;
1514
0
    return 1;
1515
0
}
1516
1517
static const OSSL_PARAM *rsa_gettable_ctx_params(ossl_unused void *vprsactx,
1518
    ossl_unused void *provctx)
1519
0
{
1520
0
    return rsa_get_ctx_params_list;
1521
0
}
1522
1523
#ifdef FIPS_MODULE
1524
static int rsa_x931_padding_allowed(PROV_RSA_CTX *ctx)
1525
{
1526
    if ((ctx->operation
1527
            & (EVP_PKEY_OP_SIGNMSG | EVP_PKEY_OP_SIGN))
1528
        != 0) {
1529
        if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE2,
1530
                ctx->libctx,
1531
                "RSA Sign set ctx", "X931 Padding",
1532
                ossl_fips_config_rsa_sign_x931_disallowed)) {
1533
            ERR_raise(ERR_LIB_PROV,
1534
                PROV_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
1535
            return 0;
1536
        }
1537
    }
1538
    return 1;
1539
}
1540
#endif
1541
1542
static int rsa_set_ctx_params(void *vprsactx, const OSSL_PARAM params[])
1543
12.2k
{
1544
12.2k
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1545
12.2k
    struct rsa_set_ctx_params_st p;
1546
12.2k
    int pad_mode;
1547
12.2k
    int saltlen;
1548
12.2k
    int count = 0;
1549
12.2k
    char mdname[OSSL_MAX_NAME_SIZE] = "", *pmdname = NULL;
1550
12.2k
    char mdprops[OSSL_MAX_PROPQUERY_SIZE] = "", *pmdprops = NULL;
1551
12.2k
    char mgf1mdname[OSSL_MAX_NAME_SIZE] = "", *pmgf1mdname = NULL;
1552
12.2k
    char mgf1mdprops[OSSL_MAX_PROPQUERY_SIZE] = "", *pmgf1mdprops = NULL;
1553
1554
12.2k
    if (prsactx == NULL)
1555
0
        return 0;
1556
    /* The processing code below doesn't handle no parameters properly */
1557
12.2k
    if (ossl_param_is_empty(params))
1558
4.65k
        return 1;
1559
1560
7.57k
    if (prsactx->flag_allow_md) {
1561
2.31k
        if (!rsa_set_ctx_params_decoder(params, &p, &count))
1562
0
            return 0;
1563
5.26k
    } else {
1564
5.26k
        if (!rsa_set_ctx_params_no_digest_decoder(params, &p, &count))
1565
0
            return 0;
1566
5.26k
    }
1567
7.57k
    if (count == 0)
1568
0
        return 1;
1569
1570
7.57k
    if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(prsactx, OSSL_FIPS_IND_SETTABLE0,
1571
7.57k
            p.ind_k))
1572
0
        return 0;
1573
1574
7.57k
    if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(prsactx, OSSL_FIPS_IND_SETTABLE1,
1575
7.57k
            p.ind_d))
1576
0
        return 0;
1577
1578
7.57k
    if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(prsactx, OSSL_FIPS_IND_SETTABLE2,
1579
7.57k
            p.ind_xpad))
1580
0
        return 0;
1581
1582
7.57k
    if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(prsactx, OSSL_FIPS_IND_SETTABLE3,
1583
7.57k
            p.ind_slen))
1584
0
        return 0;
1585
1586
7.57k
    pad_mode = prsactx->pad_mode;
1587
7.57k
    saltlen = prsactx->saltlen;
1588
1589
7.57k
    if (p.digest != NULL) {
1590
2.31k
        pmdname = mdname;
1591
2.31k
        if (!OSSL_PARAM_get_utf8_string(p.digest, &pmdname, sizeof(mdname)))
1592
0
            return 0;
1593
1594
2.31k
        if (p.propq != NULL) {
1595
0
            pmdprops = mdprops;
1596
0
            if (!OSSL_PARAM_get_utf8_string(p.propq,
1597
0
                    &pmdprops, sizeof(mdprops)))
1598
0
                return 0;
1599
0
        }
1600
2.31k
    }
1601
1602
7.57k
    if (p.pad != NULL) {
1603
2.58k
        const char *err_extra_text = NULL;
1604
1605
2.58k
        if (p.pad->data_type != OSSL_PARAM_UTF8_STRING) {
1606
            /* Support for legacy pad mode number */
1607
2.58k
            if (!OSSL_PARAM_get_int(p.pad, &pad_mode))
1608
0
                return 0;
1609
2.58k
        } else {
1610
0
            int i;
1611
1612
0
            if (p.pad->data == NULL)
1613
0
                return 0;
1614
1615
0
            for (i = 0; padding_item[i].id != 0; i++) {
1616
0
                if (strcmp(p.pad->data, padding_item[i].ptr) == 0) {
1617
0
                    pad_mode = padding_item[i].id;
1618
0
                    break;
1619
0
                }
1620
0
            }
1621
0
        }
1622
1623
2.58k
        switch (pad_mode) {
1624
0
        case RSA_PKCS1_OAEP_PADDING:
1625
            /*
1626
             * OAEP padding is for asymmetric cipher only so is not compatible
1627
             * with signature use.
1628
             */
1629
0
            err_extra_text = "OAEP padding not allowed for signing / verifying";
1630
0
            goto bad_pad;
1631
2.58k
        case RSA_PKCS1_PSS_PADDING:
1632
2.58k
            if ((prsactx->operation
1633
2.58k
                    & (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_SIGNMSG
1634
2.58k
                        | EVP_PKEY_OP_VERIFY | EVP_PKEY_OP_VERIFYMSG))
1635
2.58k
                == 0) {
1636
0
                err_extra_text = "PSS padding only allowed for sign and verify operations";
1637
0
                goto bad_pad;
1638
0
            }
1639
2.58k
            break;
1640
2.58k
        case RSA_PKCS1_PADDING:
1641
0
            err_extra_text = "PKCS#1 padding not allowed with RSA-PSS";
1642
0
            goto cont;
1643
0
        case RSA_NO_PADDING:
1644
0
            err_extra_text = "No padding not allowed with RSA-PSS";
1645
0
            goto cont;
1646
0
        case RSA_X931_PADDING:
1647
#ifdef FIPS_MODULE
1648
            /* X9.31 only allows sizes of 1024 + 256 * s (bits) */
1649
            if ((RSA_bits(prsactx->rsa) & 0xFF) != 0) {
1650
                ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
1651
                return 0;
1652
            }
1653
            /* RSA Signing with X9.31 padding is not allowed in FIPS 140-3 */
1654
            if (!rsa_x931_padding_allowed(prsactx))
1655
                return 0;
1656
#endif
1657
0
            err_extra_text = "X.931 padding not allowed with RSA-PSS";
1658
0
        cont:
1659
0
            if (RSA_test_flags(prsactx->rsa,
1660
0
                    RSA_FLAG_TYPE_MASK)
1661
0
                == RSA_FLAG_TYPE_RSA)
1662
0
                break;
1663
            /* FALLTHRU */
1664
0
        default:
1665
0
        bad_pad:
1666
0
            if (err_extra_text == NULL)
1667
0
                ERR_raise(ERR_LIB_PROV,
1668
0
                    PROV_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
1669
0
            else
1670
0
                ERR_raise_data(ERR_LIB_PROV,
1671
0
                    PROV_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE,
1672
0
                    err_extra_text);
1673
0
            return 0;
1674
2.58k
        }
1675
2.58k
    }
1676
1677
7.57k
    if (p.slen != NULL) {
1678
2.58k
        if (pad_mode != RSA_PKCS1_PSS_PADDING) {
1679
0
            ERR_raise_data(ERR_LIB_PROV, PROV_R_NOT_SUPPORTED,
1680
0
                "PSS saltlen can only be specified if "
1681
0
                "PSS padding has been specified first");
1682
0
            return 0;
1683
0
        }
1684
1685
2.58k
        if (p.slen->data_type != OSSL_PARAM_UTF8_STRING) {
1686
            /* Support for legacy pad mode number */
1687
0
            if (!OSSL_PARAM_get_int(p.slen, &saltlen))
1688
0
                return 0;
1689
2.58k
        } else {
1690
2.58k
            if (strcmp(p.slen->data, OSSL_PKEY_RSA_PSS_SALT_LEN_DIGEST) == 0)
1691
2.49k
                saltlen = RSA_PSS_SALTLEN_DIGEST;
1692
92
            else if (strcmp(p.slen->data, OSSL_PKEY_RSA_PSS_SALT_LEN_MAX) == 0)
1693
0
                saltlen = RSA_PSS_SALTLEN_MAX;
1694
92
            else if (strcmp(p.slen->data, OSSL_PKEY_RSA_PSS_SALT_LEN_AUTO) == 0)
1695
0
                saltlen = RSA_PSS_SALTLEN_AUTO;
1696
92
            else if (strcmp(p.slen->data, OSSL_PKEY_RSA_PSS_SALT_LEN_AUTO_DIGEST_MAX) == 0)
1697
0
                saltlen = RSA_PSS_SALTLEN_AUTO_DIGEST_MAX;
1698
92
            else
1699
92
                saltlen = atoi(p.slen->data);
1700
2.58k
        }
1701
1702
        /*
1703
         * RSA_PSS_SALTLEN_AUTO_DIGEST_MAX seems curiously named in this check.
1704
         * Contrary to what it's name suggests, it's the currently lowest
1705
         * saltlen number possible.
1706
         */
1707
2.58k
        if (saltlen < RSA_PSS_SALTLEN_AUTO_DIGEST_MAX) {
1708
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH);
1709
0
            return 0;
1710
0
        }
1711
1712
2.58k
        if (rsa_pss_restricted(prsactx)) {
1713
2
            switch (saltlen) {
1714
0
            case RSA_PSS_SALTLEN_AUTO:
1715
0
            case RSA_PSS_SALTLEN_AUTO_DIGEST_MAX:
1716
0
                if ((prsactx->operation
1717
0
                        & (EVP_PKEY_OP_VERIFY | EVP_PKEY_OP_VERIFYMSG))
1718
0
                    == 0) {
1719
0
                    ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH,
1720
0
                        "Cannot use autodetected salt length");
1721
0
                    return 0;
1722
0
                }
1723
0
                break;
1724
0
            case RSA_PSS_SALTLEN_DIGEST:
1725
0
                if (prsactx->min_saltlen > EVP_MD_get_size(prsactx->md)) {
1726
0
                    ERR_raise_data(ERR_LIB_PROV,
1727
0
                        PROV_R_PSS_SALTLEN_TOO_SMALL,
1728
0
                        "Should be more than %d, but would be "
1729
0
                        "set to match digest size (%d)",
1730
0
                        prsactx->min_saltlen,
1731
0
                        EVP_MD_get_size(prsactx->md));
1732
0
                    return 0;
1733
0
                }
1734
0
                break;
1735
2
            default:
1736
2
                if (saltlen >= 0 && saltlen < prsactx->min_saltlen) {
1737
0
                    ERR_raise_data(ERR_LIB_PROV,
1738
0
                        PROV_R_PSS_SALTLEN_TOO_SMALL,
1739
0
                        "Should be more than %d, "
1740
0
                        "but would be set to %d",
1741
0
                        prsactx->min_saltlen, saltlen);
1742
0
                    return 0;
1743
0
                }
1744
2
            }
1745
2
        }
1746
2.58k
    }
1747
1748
7.57k
    if (p.mgf1 != NULL) {
1749
92
        pmgf1mdname = mgf1mdname;
1750
92
        if (!OSSL_PARAM_get_utf8_string(p.mgf1, &pmgf1mdname, sizeof(mgf1mdname)))
1751
0
            return 0;
1752
1753
92
        if (p.mgf1pq != NULL) {
1754
0
            pmgf1mdprops = mgf1mdprops;
1755
0
            if (!OSSL_PARAM_get_utf8_string(p.mgf1pq,
1756
0
                    &pmgf1mdprops, sizeof(mgf1mdprops)))
1757
0
                return 0;
1758
0
        }
1759
1760
92
        if (pad_mode != RSA_PKCS1_PSS_PADDING) {
1761
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MGF1_MD);
1762
0
            return 0;
1763
0
        }
1764
92
    }
1765
1766
7.57k
    prsactx->saltlen = saltlen;
1767
7.57k
    prsactx->pad_mode = pad_mode;
1768
1769
7.57k
    if (prsactx->md == NULL && pmdname == NULL
1770
0
        && pad_mode == RSA_PKCS1_PSS_PADDING)
1771
0
        pmdname = RSA_DEFAULT_DIGEST_NAME;
1772
1773
7.57k
    if (pmgf1mdname != NULL
1774
92
        && !rsa_setup_mgf1_md(prsactx, pmgf1mdname, pmgf1mdprops))
1775
0
        return 0;
1776
1777
7.57k
    if (pmdname != NULL) {
1778
2.31k
        if (!rsa_setup_md(prsactx, pmdname, pmdprops, "RSA Sign Set Ctx"))
1779
3
            return 0;
1780
5.26k
    } else {
1781
5.26k
        if (!rsa_check_padding(prsactx, NULL, NULL, prsactx->mdnid))
1782
0
            return 0;
1783
5.26k
    }
1784
7.56k
    return 1;
1785
7.57k
}
1786
1787
static const OSSL_PARAM *rsa_settable_ctx_params(void *vprsactx,
1788
    ossl_unused void *provctx)
1789
38.1k
{
1790
38.1k
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1791
1792
38.1k
    if (prsactx != NULL && !prsactx->flag_allow_md)
1793
38.1k
        return rsa_set_ctx_params_no_digest_list;
1794
15
    return rsa_set_ctx_params_list;
1795
38.1k
}
1796
1797
static int rsa_get_ctx_md_params(void *vprsactx, OSSL_PARAM *params)
1798
0
{
1799
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1800
1801
0
    if (prsactx->mdctx == NULL)
1802
0
        return 0;
1803
1804
0
    return EVP_MD_CTX_get_params(prsactx->mdctx, params);
1805
0
}
1806
1807
static const OSSL_PARAM *rsa_gettable_ctx_md_params(void *vprsactx)
1808
0
{
1809
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1810
1811
0
    if (prsactx->md == NULL)
1812
0
        return 0;
1813
1814
0
    return EVP_MD_gettable_ctx_params(prsactx->md);
1815
0
}
1816
1817
static int rsa_set_ctx_md_params(void *vprsactx, const OSSL_PARAM params[])
1818
0
{
1819
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1820
1821
0
    if (prsactx->mdctx == NULL)
1822
0
        return 0;
1823
1824
0
    return EVP_MD_CTX_set_params(prsactx->mdctx, params);
1825
0
}
1826
1827
static const OSSL_PARAM *rsa_settable_ctx_md_params(void *vprsactx)
1828
0
{
1829
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1830
1831
0
    if (prsactx->md == NULL)
1832
0
        return 0;
1833
1834
0
    return EVP_MD_settable_ctx_params(prsactx->md);
1835
0
}
1836
1837
const OSSL_DISPATCH ossl_rsa_signature_functions[] = {
1838
    { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))rsa_newctx },
1839
    { OSSL_FUNC_SIGNATURE_SIGN_INIT, (void (*)(void))rsa_sign_init },
1840
    { OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))rsa_sign },
1841
    { OSSL_FUNC_SIGNATURE_VERIFY_INIT, (void (*)(void))rsa_verify_init },
1842
    { OSSL_FUNC_SIGNATURE_VERIFY, (void (*)(void))rsa_verify },
1843
    { OSSL_FUNC_SIGNATURE_VERIFY_RECOVER_INIT,
1844
        (void (*)(void))rsa_verify_recover_init },
1845
    { OSSL_FUNC_SIGNATURE_VERIFY_RECOVER,
1846
        (void (*)(void))rsa_verify_recover },
1847
    { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT,
1848
        (void (*)(void))rsa_digest_sign_init },
1849
    { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE,
1850
        (void (*)(void))rsa_digest_sign_update },
1851
    { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL,
1852
        (void (*)(void))rsa_digest_sign_final },
1853
    { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT,
1854
        (void (*)(void))rsa_digest_verify_init },
1855
    { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE,
1856
        (void (*)(void))rsa_digest_verify_update },
1857
    { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL,
1858
        (void (*)(void))rsa_digest_verify_final },
1859
    { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))rsa_freectx },
1860
    { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))rsa_dupctx },
1861
    { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, (void (*)(void))rsa_get_ctx_params },
1862
    { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS,
1863
        (void (*)(void))rsa_gettable_ctx_params },
1864
    { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, (void (*)(void))rsa_set_ctx_params },
1865
    { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS,
1866
        (void (*)(void))rsa_settable_ctx_params },
1867
    { OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS,
1868
        (void (*)(void))rsa_get_ctx_md_params },
1869
    { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS,
1870
        (void (*)(void))rsa_gettable_ctx_md_params },
1871
    { OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS,
1872
        (void (*)(void))rsa_set_ctx_md_params },
1873
    { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS,
1874
        (void (*)(void))rsa_settable_ctx_md_params },
1875
    OSSL_DISPATCH_END
1876
};
1877
1878
/* ------------------------------------------------------------------ */
1879
1880
/*
1881
 * So called sigalgs (composite RSA+hash) implemented below.  They
1882
 * are pretty much hard coded, and rely on the hash implementation
1883
 * being available as per what OPENSSL_NO_ macros allow.
1884
 */
1885
1886
static OSSL_FUNC_signature_query_key_types_fn rsa_sigalg_query_key_types;
1887
static OSSL_FUNC_signature_settable_ctx_params_fn rsa_sigalg_settable_ctx_params;
1888
static OSSL_FUNC_signature_set_ctx_params_fn rsa_sigalg_set_ctx_params;
1889
1890
/*
1891
 * rsa_sigalg_signverify_init() is almost like rsa_digest_signverify_init(),
1892
 * just doesn't allow fetching an MD from whatever the user chooses.
1893
 */
1894
static int rsa_sigalg_signverify_init(void *vprsactx, void *vrsa,
1895
    OSSL_FUNC_signature_set_ctx_params_fn *set_ctx_params,
1896
    const OSSL_PARAM params[],
1897
    const char *mdname,
1898
    int operation, int pad_mode,
1899
    const char *desc)
1900
0
{
1901
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1902
1903
0
    if (!ossl_prov_is_running())
1904
0
        return 0;
1905
1906
0
    if (!rsa_signverify_init(prsactx, vrsa, set_ctx_params, params, operation,
1907
0
            desc))
1908
0
        return 0;
1909
1910
    /* PSS is currently not supported as a sigalg */
1911
0
    if (prsactx->pad_mode == RSA_PKCS1_PSS_PADDING) {
1912
0
        ERR_raise(ERR_LIB_PROV, PROV_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
1913
0
        return 0;
1914
0
    }
1915
1916
0
    if (!rsa_setup_md(prsactx, mdname, NULL, desc))
1917
0
        return 0;
1918
1919
0
    prsactx->pad_mode = pad_mode;
1920
0
    prsactx->flag_sigalg = 1;
1921
0
    prsactx->flag_allow_md = 0;
1922
1923
0
    if (prsactx->mdctx == NULL) {
1924
0
        prsactx->mdctx = EVP_MD_CTX_new();
1925
0
        if (prsactx->mdctx == NULL)
1926
0
            goto error;
1927
0
    }
1928
1929
0
    if (!EVP_DigestInit_ex2(prsactx->mdctx, prsactx->md, params))
1930
0
        goto error;
1931
1932
0
    return 1;
1933
1934
0
error:
1935
0
    EVP_MD_CTX_free(prsactx->mdctx);
1936
0
    prsactx->mdctx = NULL;
1937
0
    return 0;
1938
0
}
1939
1940
static const char **rsa_sigalg_query_key_types(void)
1941
0
{
1942
0
    static const char *keytypes[] = { "RSA", NULL };
1943
1944
0
    return keytypes;
1945
0
}
1946
1947
static const OSSL_PARAM *rsa_sigalg_settable_ctx_params(void *vprsactx,
1948
    ossl_unused void *provctx)
1949
5
{
1950
5
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1951
1952
5
    if (prsactx != NULL && prsactx->operation == EVP_PKEY_OP_VERIFYMSG)
1953
0
        return rsa_sigalg_set_ctx_params_list;
1954
5
    return NULL;
1955
5
}
1956
1957
static int rsa_sigalg_set_ctx_params(void *vprsactx, const OSSL_PARAM params[])
1958
13.0k
{
1959
13.0k
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1960
13.0k
    struct rsa_sigalg_set_ctx_params_st p;
1961
1962
13.0k
    if (prsactx == NULL || !rsa_sigalg_set_ctx_params_decoder(params, &p))
1963
0
        return 0;
1964
1965
13.0k
    if (prsactx->operation == EVP_PKEY_OP_VERIFYMSG) {
1966
13.0k
        if (p.sig != NULL) {
1967
13.0k
            OPENSSL_free(prsactx->sig);
1968
13.0k
            prsactx->sig = NULL;
1969
13.0k
            prsactx->siglen = 0;
1970
13.0k
            if (!OSSL_PARAM_get_octet_string(p.sig, (void **)&prsactx->sig,
1971
13.0k
                    0, &prsactx->siglen))
1972
0
                return 0;
1973
13.0k
        }
1974
13.0k
    }
1975
13.0k
    return 1;
1976
13.0k
}
1977
1978
#define IMPL_RSA_SIGALG(md, MD)                                       \
1979
    static OSSL_FUNC_signature_sign_init_fn rsa_##md##_sign_init;     \
1980
    static OSSL_FUNC_signature_sign_message_init_fn                   \
1981
        rsa_##md##_sign_message_init;                                 \
1982
    static OSSL_FUNC_signature_verify_init_fn rsa_##md##_verify_init; \
1983
    static OSSL_FUNC_signature_verify_message_init_fn                 \
1984
        rsa_##md##_verify_message_init;                               \
1985
                                                                      \
1986
    static int                                                        \
1987
    rsa_##md##_sign_init(void *vprsactx, void *vrsa,                  \
1988
        const OSSL_PARAM params[])                                    \
1989
0
    {                                                                 \
1990
0
        static const char desc[] = "RSA Sigalg Sign Init";            \
1991
0
                                                                      \
1992
0
        return rsa_sigalg_signverify_init(vprsactx, vrsa,             \
1993
0
            rsa_sigalg_set_ctx_params,                                \
1994
0
            params, MD,                                               \
1995
0
            EVP_PKEY_OP_SIGN,                                         \
1996
0
            RSA_PKCS1_PADDING,                                        \
1997
0
            desc);                                                    \
1998
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
1999
                                                                      \
2000
    static int                                                        \
2001
    rsa_##md##_sign_message_init(void *vprsactx, void *vrsa,          \
2002
        const OSSL_PARAM params[])                                    \
2003
0
    {                                                                 \
2004
0
        static const char desc[] = "RSA Sigalg Sign Message Init";    \
2005
0
                                                                      \
2006
0
        return rsa_sigalg_signverify_init(vprsactx, vrsa,             \
2007
0
            rsa_sigalg_set_ctx_params,                                \
2008
0
            params, MD,                                               \
2009
0
            EVP_PKEY_OP_SIGNMSG,                                      \
2010
0
            RSA_PKCS1_PADDING,                                        \
2011
0
            desc);                                                    \
2012
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
2013
                                                                      \
2014
    static int                                                        \
2015
    rsa_##md##_verify_init(void *vprsactx, void *vrsa,                \
2016
        const OSSL_PARAM params[])                                    \
2017
0
    {                                                                 \
2018
0
        static const char desc[] = "RSA Sigalg Verify Init";          \
2019
0
                                                                      \
2020
0
        return rsa_sigalg_signverify_init(vprsactx, vrsa,             \
2021
0
            rsa_sigalg_set_ctx_params,                                \
2022
0
            params, MD,                                               \
2023
0
            EVP_PKEY_OP_VERIFY,                                       \
2024
0
            RSA_PKCS1_PADDING,                                        \
2025
0
            desc);                                                    \
2026
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
2027
                                                                      \
2028
    static int                                                        \
2029
    rsa_##md##_verify_recover_init(void *vprsactx, void *vrsa,        \
2030
        const OSSL_PARAM params[])                                    \
2031
0
    {                                                                 \
2032
0
        static const char desc[] = "RSA Sigalg Verify Recover Init";  \
2033
0
                                                                      \
2034
0
        return rsa_sigalg_signverify_init(vprsactx, vrsa,             \
2035
0
            rsa_sigalg_set_ctx_params,                                \
2036
0
            params, MD,                                               \
2037
0
            EVP_PKEY_OP_VERIFYRECOVER,                                \
2038
0
            RSA_PKCS1_PADDING,                                        \
2039
0
            desc);                                                    \
2040
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
2041
                                                                      \
2042
    static int                                                        \
2043
    rsa_##md##_verify_message_init(void *vprsactx, void *vrsa,        \
2044
        const OSSL_PARAM params[])                                    \
2045
0
    {                                                                 \
2046
0
        static const char desc[] = "RSA Sigalg Verify Message Init";  \
2047
0
                                                                      \
2048
0
        return rsa_sigalg_signverify_init(vprsactx, vrsa,             \
2049
0
            rsa_sigalg_set_ctx_params,                                \
2050
0
            params, MD,                                               \
2051
0
            EVP_PKEY_OP_VERIFYMSG,                                    \
2052
0
            RSA_PKCS1_PADDING,                                        \
2053
0
            desc);                                                    \
2054
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
2055
                                                                      \
2056
    const OSSL_DISPATCH ossl_rsa_##md##_signature_functions[] = {     \
2057
        { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))rsa_newctx },   \
2058
        { OSSL_FUNC_SIGNATURE_SIGN_INIT,                              \
2059
            (void (*)(void))rsa_##md##_sign_init },                   \
2060
        { OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))rsa_sign },       \
2061
        { OSSL_FUNC_SIGNATURE_SIGN_MESSAGE_INIT,                      \
2062
            (void (*)(void))rsa_##md##_sign_message_init },           \
2063
        { OSSL_FUNC_SIGNATURE_SIGN_MESSAGE_UPDATE,                    \
2064
            (void (*)(void))rsa_signverify_message_update },          \
2065
        { OSSL_FUNC_SIGNATURE_SIGN_MESSAGE_FINAL,                     \
2066
            (void (*)(void))rsa_sign_message_final },                 \
2067
        { OSSL_FUNC_SIGNATURE_VERIFY_INIT,                            \
2068
            (void (*)(void))rsa_##md##_verify_init },                 \
2069
        { OSSL_FUNC_SIGNATURE_VERIFY,                                 \
2070
            (void (*)(void))rsa_verify },                             \
2071
        { OSSL_FUNC_SIGNATURE_VERIFY_MESSAGE_INIT,                    \
2072
            (void (*)(void))rsa_##md##_verify_message_init },         \
2073
        { OSSL_FUNC_SIGNATURE_VERIFY_MESSAGE_UPDATE,                  \
2074
            (void (*)(void))rsa_signverify_message_update },          \
2075
        { OSSL_FUNC_SIGNATURE_VERIFY_MESSAGE_FINAL,                   \
2076
            (void (*)(void))rsa_verify_message_final },               \
2077
        { OSSL_FUNC_SIGNATURE_VERIFY_RECOVER_INIT,                    \
2078
            (void (*)(void))rsa_##md##_verify_recover_init },         \
2079
        { OSSL_FUNC_SIGNATURE_VERIFY_RECOVER,                         \
2080
            (void (*)(void))rsa_verify_recover },                     \
2081
        { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))rsa_freectx }, \
2082
        { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))rsa_dupctx },   \
2083
        { OSSL_FUNC_SIGNATURE_QUERY_KEY_TYPES,                        \
2084
            (void (*)(void))rsa_sigalg_query_key_types },             \
2085
        { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS,                         \
2086
            (void (*)(void))rsa_get_ctx_params },                     \
2087
        { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS,                    \
2088
            (void (*)(void))rsa_gettable_ctx_params },                \
2089
        { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS,                         \
2090
            (void (*)(void))rsa_sigalg_set_ctx_params },              \
2091
        { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS,                    \
2092
            (void (*)(void))rsa_sigalg_settable_ctx_params },         \
2093
        OSSL_DISPATCH_END                                             \
2094
    }
2095
2096
#if !defined(OPENSSL_NO_RMD160) && !defined(FIPS_MODULE)
2097
IMPL_RSA_SIGALG(ripemd160, "RIPEMD160");
2098
#endif
2099
IMPL_RSA_SIGALG(sha1, "SHA1");
2100
IMPL_RSA_SIGALG(sha224, "SHA2-224");
2101
IMPL_RSA_SIGALG(sha256, "SHA2-256");
2102
IMPL_RSA_SIGALG(sha384, "SHA2-384");
2103
IMPL_RSA_SIGALG(sha512, "SHA2-512");
2104
IMPL_RSA_SIGALG(sha512_224, "SHA2-512/224");
2105
IMPL_RSA_SIGALG(sha512_256, "SHA2-512/256");
2106
IMPL_RSA_SIGALG(sha3_224, "SHA3-224");
2107
IMPL_RSA_SIGALG(sha3_256, "SHA3-256");
2108
IMPL_RSA_SIGALG(sha3_384, "SHA3-384");
2109
IMPL_RSA_SIGALG(sha3_512, "SHA3-512");
2110
#if !defined(OPENSSL_NO_SM3) && !defined(FIPS_MODULE)
2111
IMPL_RSA_SIGALG(sm3, "SM3");
2112
#endif