Coverage Report

Created: 2026-05-30 06:06

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