Coverage Report

Created: 2026-08-31 06:56

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