Coverage Report

Created: 2025-12-10 06:24

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