Coverage Report

Created: 2025-06-13 06:58

/src/openssl30/providers/implementations/signature/rsa_sig.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2019-2023 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/rsa.h>
22
#include <openssl/params.h>
23
#include <openssl/evp.h>
24
#include <openssl/proverr.h>
25
#include "internal/cryptlib.h"
26
#include "internal/nelem.h"
27
#include "internal/sizes.h"
28
#include "crypto/rsa.h"
29
#include "prov/providercommon.h"
30
#include "prov/implementations.h"
31
#include "prov/provider_ctx.h"
32
#include "prov/der_rsa.h"
33
#include "prov/securitycheck.h"
34
35
0
#define RSA_DEFAULT_DIGEST_NAME OSSL_DIGEST_NAME_SHA1
36
37
static OSSL_FUNC_signature_newctx_fn rsa_newctx;
38
static OSSL_FUNC_signature_sign_init_fn rsa_sign_init;
39
static OSSL_FUNC_signature_verify_init_fn rsa_verify_init;
40
static OSSL_FUNC_signature_verify_recover_init_fn rsa_verify_recover_init;
41
static OSSL_FUNC_signature_sign_fn rsa_sign;
42
static OSSL_FUNC_signature_verify_fn rsa_verify;
43
static OSSL_FUNC_signature_verify_recover_fn rsa_verify_recover;
44
static OSSL_FUNC_signature_digest_sign_init_fn rsa_digest_sign_init;
45
static OSSL_FUNC_signature_digest_sign_update_fn rsa_digest_signverify_update;
46
static OSSL_FUNC_signature_digest_sign_final_fn rsa_digest_sign_final;
47
static OSSL_FUNC_signature_digest_verify_init_fn rsa_digest_verify_init;
48
static OSSL_FUNC_signature_digest_verify_update_fn rsa_digest_signverify_update;
49
static OSSL_FUNC_signature_digest_verify_final_fn rsa_digest_verify_final;
50
static OSSL_FUNC_signature_freectx_fn rsa_freectx;
51
static OSSL_FUNC_signature_dupctx_fn rsa_dupctx;
52
static OSSL_FUNC_signature_get_ctx_params_fn rsa_get_ctx_params;
53
static OSSL_FUNC_signature_gettable_ctx_params_fn rsa_gettable_ctx_params;
54
static OSSL_FUNC_signature_set_ctx_params_fn rsa_set_ctx_params;
55
static OSSL_FUNC_signature_settable_ctx_params_fn rsa_settable_ctx_params;
56
static OSSL_FUNC_signature_get_ctx_md_params_fn rsa_get_ctx_md_params;
57
static OSSL_FUNC_signature_gettable_ctx_md_params_fn rsa_gettable_ctx_md_params;
58
static OSSL_FUNC_signature_set_ctx_md_params_fn rsa_set_ctx_md_params;
59
static OSSL_FUNC_signature_settable_ctx_md_params_fn rsa_settable_ctx_md_params;
60
61
static OSSL_ITEM padding_item[] = {
62
    { RSA_PKCS1_PADDING,        OSSL_PKEY_RSA_PAD_MODE_PKCSV15 },
63
    { RSA_NO_PADDING,           OSSL_PKEY_RSA_PAD_MODE_NONE },
64
    { RSA_X931_PADDING,         OSSL_PKEY_RSA_PAD_MODE_X931 },
65
    { RSA_PKCS1_PSS_PADDING,    OSSL_PKEY_RSA_PAD_MODE_PSS },
66
    { 0,                        NULL     }
67
};
68
69
/*
70
 * What's passed as an actual key is defined by the KEYMGMT interface.
71
 * We happen to know that our KEYMGMT simply passes RSA structures, so
72
 * we use that here too.
73
 */
74
75
typedef struct {
76
    OSSL_LIB_CTX *libctx;
77
    char *propq;
78
    RSA *rsa;
79
    int operation;
80
81
    /*
82
     * Flag to determine if the hash function can be changed (1) or not (0)
83
     * Because it's dangerous to change during a DigestSign or DigestVerify
84
     * operation, this flag is cleared by their Init function, and set again
85
     * by their Final function.
86
     */
87
    unsigned int flag_allow_md : 1;
88
    unsigned int mgf1_md_set : 1;
89
90
    /* main digest */
91
    EVP_MD *md;
92
    EVP_MD_CTX *mdctx;
93
    int mdnid;
94
    char mdname[OSSL_MAX_NAME_SIZE]; /* Purely informational */
95
96
    /* RSA padding mode */
97
    int pad_mode;
98
    /* message digest for MGF1 */
99
    EVP_MD *mgf1_md;
100
    int mgf1_mdnid;
101
    char mgf1_mdname[OSSL_MAX_NAME_SIZE]; /* Purely informational */
102
    /* PSS salt length */
103
    int saltlen;
104
    /* Minimum salt length or -1 if no PSS parameter restriction */
105
    int min_saltlen;
106
107
    /* Temp buffer */
108
    unsigned char *tbuf;
109
110
} PROV_RSA_CTX;
111
112
/* True if PSS parameters are restricted */
113
34.7k
#define rsa_pss_restricted(prsactx) (prsactx->min_saltlen != -1)
114
115
static size_t rsa_get_md_size(const PROV_RSA_CTX *prsactx)
116
10.5k
{
117
10.5k
    if (prsactx->md != NULL)
118
10.5k
        return EVP_MD_get_size(prsactx->md);
119
0
    return 0;
120
10.5k
}
121
122
static int rsa_check_padding(const PROV_RSA_CTX *prsactx,
123
                             const char *mdname, const char *mgf1_mdname,
124
                             int mdnid)
125
83.6k
{
126
83.6k
    switch(prsactx->pad_mode) {
127
0
        case RSA_NO_PADDING:
128
0
            if (mdname != NULL || mdnid != NID_undef) {
129
0
                ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_PADDING_MODE);
130
0
                return 0;
131
0
            }
132
0
            break;
133
0
        case RSA_X931_PADDING:
134
0
            if (RSA_X931_hash_id(mdnid) == -1) {
135
0
                ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_X931_DIGEST);
136
0
                return 0;
137
0
            }
138
0
            break;
139
33.1k
        case RSA_PKCS1_PSS_PADDING:
140
33.1k
            if (rsa_pss_restricted(prsactx))
141
1.39k
                if ((mdname != NULL && !EVP_MD_is_a(prsactx->md, mdname))
142
1.39k
                    || (mgf1_mdname != NULL
143
1.37k
                        && !EVP_MD_is_a(prsactx->mgf1_md, mgf1_mdname))) {
144
22
                    ERR_raise(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED);
145
22
                    return 0;
146
22
                }
147
33.1k
            break;
148
50.4k
        default:
149
50.4k
            break;
150
83.6k
    }
151
152
83.6k
    return 1;
153
83.6k
}
154
155
static int rsa_check_parameters(PROV_RSA_CTX *prsactx, int min_saltlen)
156
385
{
157
385
    if (prsactx->pad_mode == RSA_PKCS1_PSS_PADDING) {
158
385
        int max_saltlen;
159
160
        /* See if minimum salt length exceeds maximum possible */
161
385
        max_saltlen = RSA_size(prsactx->rsa) - EVP_MD_get_size(prsactx->md);
162
385
        if ((RSA_bits(prsactx->rsa) & 0x7) == 1)
163
32
            max_saltlen--;
164
385
        if (min_saltlen < 0 || min_saltlen > max_saltlen) {
165
20
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH);
166
20
            return 0;
167
20
        }
168
365
        prsactx->min_saltlen = min_saltlen;
169
365
    }
170
365
    return 1;
171
385
}
172
173
static void *rsa_newctx(void *provctx, const char *propq)
174
50.9k
{
175
50.9k
    PROV_RSA_CTX *prsactx = NULL;
176
50.9k
    char *propq_copy = NULL;
177
178
50.9k
    if (!ossl_prov_is_running())
179
0
        return NULL;
180
181
50.9k
    if ((prsactx = OPENSSL_zalloc(sizeof(PROV_RSA_CTX))) == NULL
182
50.9k
        || (propq != NULL
183
50.9k
            && (propq_copy = OPENSSL_strdup(propq)) == NULL)) {
184
0
        OPENSSL_free(prsactx);
185
0
        ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
186
0
        return NULL;
187
0
    }
188
189
50.9k
    prsactx->libctx = PROV_LIBCTX_OF(provctx);
190
50.9k
    prsactx->flag_allow_md = 1;
191
50.9k
    prsactx->propq = propq_copy;
192
    /* Maximum for sign, auto for verify */
193
50.9k
    prsactx->saltlen = RSA_PSS_SALTLEN_AUTO;
194
50.9k
    prsactx->min_saltlen = -1;
195
50.9k
    return prsactx;
196
50.9k
}
197
198
static int rsa_pss_compute_saltlen(PROV_RSA_CTX *ctx)
199
0
{
200
0
    int saltlen = ctx->saltlen;
201
 
202
0
    if (saltlen == RSA_PSS_SALTLEN_DIGEST) {
203
0
        saltlen = EVP_MD_get_size(ctx->md);
204
0
    } else if (saltlen == RSA_PSS_SALTLEN_AUTO || saltlen == RSA_PSS_SALTLEN_MAX) {
205
0
        saltlen = RSA_size(ctx->rsa) - EVP_MD_get_size(ctx->md) - 2;
206
0
        if ((RSA_bits(ctx->rsa) & 0x7) == 1)
207
0
            saltlen--;
208
0
    }
209
0
    if (saltlen < 0) {
210
0
        ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
211
0
        return -1;
212
0
    } else if (saltlen < ctx->min_saltlen) {
213
0
        ERR_raise_data(ERR_LIB_PROV, PROV_R_PSS_SALTLEN_TOO_SMALL,
214
0
                       "minimum salt length: %d, actual salt length: %d",
215
0
                       ctx->min_saltlen, saltlen);
216
0
        return -1;
217
0
    }
218
0
    return saltlen;
219
0
}
220
221
static unsigned char *rsa_generate_signature_aid(PROV_RSA_CTX *ctx,
222
                                                 unsigned char *aid_buf,
223
                                                 size_t buf_len,
224
                                                 size_t *aid_len)
225
0
{
226
0
    WPACKET pkt;
227
0
    unsigned char *aid = NULL;
228
0
    int saltlen;
229
0
    RSA_PSS_PARAMS_30 pss_params;
230
0
    int ret;
231
232
0
    if (!WPACKET_init_der(&pkt, aid_buf, buf_len)) {
233
0
        ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
234
0
        return NULL;
235
0
    }
236
237
0
    switch(ctx->pad_mode) {
238
0
    case RSA_PKCS1_PADDING:
239
0
        ret = ossl_DER_w_algorithmIdentifier_MDWithRSAEncryption(&pkt, -1,
240
0
                                                                 ctx->mdnid);
241
242
0
        if (ret > 0) {
243
0
            break;
244
0
        } else if (ret == 0) {
245
0
            ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
246
0
            goto cleanup;
247
0
        }
248
0
        ERR_raise_data(ERR_LIB_PROV, ERR_R_UNSUPPORTED,
249
0
                       "Algorithm ID generation - md NID: %d",
250
0
                       ctx->mdnid);
251
0
        goto cleanup;
252
0
    case RSA_PKCS1_PSS_PADDING:
253
0
        saltlen = rsa_pss_compute_saltlen(ctx);
254
0
        if (saltlen < 0)
255
0
            goto cleanup;
256
0
        if (!ossl_rsa_pss_params_30_set_defaults(&pss_params)
257
0
            || !ossl_rsa_pss_params_30_set_hashalg(&pss_params, ctx->mdnid)
258
0
            || !ossl_rsa_pss_params_30_set_maskgenhashalg(&pss_params,
259
0
                                                          ctx->mgf1_mdnid)
260
0
            || !ossl_rsa_pss_params_30_set_saltlen(&pss_params, saltlen)
261
0
            || !ossl_DER_w_algorithmIdentifier_RSA_PSS(&pkt, -1,
262
0
                                                       RSA_FLAG_TYPE_RSASSAPSS,
263
0
                                                       &pss_params)) {
264
0
            ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
265
0
            goto cleanup;
266
0
        }
267
0
        break;
268
0
    default:
269
0
        ERR_raise_data(ERR_LIB_PROV, ERR_R_UNSUPPORTED,
270
0
                       "Algorithm ID generation - pad mode: %d",
271
0
                       ctx->pad_mode);
272
0
        goto cleanup;
273
0
    }
274
0
    if (WPACKET_finish(&pkt)) {
275
0
        WPACKET_get_total_written(&pkt, aid_len);
276
0
        aid = WPACKET_get_curr(&pkt);
277
0
    }
278
0
 cleanup:
279
0
    WPACKET_cleanup(&pkt);
280
0
    return aid;
281
0
}
282
283
static int rsa_setup_md(PROV_RSA_CTX *ctx, const char *mdname,
284
                        const char *mdprops)
285
35.4k
{
286
35.4k
    if (mdprops == NULL)
287
35.4k
        mdprops = ctx->propq;
288
289
35.4k
    if (mdname != NULL) {
290
35.4k
        EVP_MD *md = EVP_MD_fetch(ctx->libctx, mdname, mdprops);
291
35.4k
        int sha1_allowed = (ctx->operation != EVP_PKEY_OP_SIGN);
292
35.4k
        int md_nid = ossl_digest_rsa_sign_get_md_nid(ctx->libctx, md,
293
35.4k
                                                     sha1_allowed);
294
35.4k
        size_t mdname_len = strlen(mdname);
295
296
35.4k
        if (md == NULL
297
35.4k
            || md_nid <= 0
298
35.4k
            || !rsa_check_padding(ctx, mdname, NULL, md_nid)
299
35.4k
            || mdname_len >= sizeof(ctx->mdname)) {
300
57
            if (md == NULL)
301
45
                ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
302
45
                               "%s could not be fetched", mdname);
303
57
            if (md_nid <= 0)
304
45
                ERR_raise_data(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED,
305
45
                               "digest=%s", mdname);
306
57
            if (mdname_len >= sizeof(ctx->mdname))
307
0
                ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
308
0
                               "%s exceeds name buffer length", mdname);
309
57
            EVP_MD_free(md);
310
57
            return 0;
311
57
        }
312
313
35.3k
        if (!ctx->flag_allow_md) {
314
0
            if (ctx->mdname[0] != '\0' && !EVP_MD_is_a(md, ctx->mdname)) {
315
0
                ERR_raise_data(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED,
316
0
                               "digest %s != %s", mdname, ctx->mdname);
317
0
                EVP_MD_free(md);
318
0
                return 0;
319
0
            }
320
0
            EVP_MD_free(md);
321
0
            return 1;
322
0
        }
323
324
35.3k
        if (!ctx->mgf1_md_set) {
325
35.0k
            if (!EVP_MD_up_ref(md)) {
326
0
                EVP_MD_free(md);
327
0
                return 0;
328
0
            }
329
35.0k
            EVP_MD_free(ctx->mgf1_md);
330
35.0k
            ctx->mgf1_md = md;
331
35.0k
            ctx->mgf1_mdnid = md_nid;
332
35.0k
            OPENSSL_strlcpy(ctx->mgf1_mdname, mdname, sizeof(ctx->mgf1_mdname));
333
35.0k
        }
334
335
35.3k
        EVP_MD_CTX_free(ctx->mdctx);
336
35.3k
        EVP_MD_free(ctx->md);
337
338
35.3k
        ctx->mdctx = NULL;
339
35.3k
        ctx->md = md;
340
35.3k
        ctx->mdnid = md_nid;
341
35.3k
        OPENSSL_strlcpy(ctx->mdname, mdname, sizeof(ctx->mdname));
342
35.3k
    }
343
344
35.3k
    return 1;
345
35.4k
}
346
347
static int rsa_setup_mgf1_md(PROV_RSA_CTX *ctx, const char *mdname,
348
                             const char *mdprops)
349
4.32k
{
350
4.32k
    size_t len;
351
4.32k
    EVP_MD *md = NULL;
352
4.32k
    int mdnid;
353
354
4.32k
    if (mdprops == NULL)
355
4.32k
        mdprops = ctx->propq;
356
357
4.32k
    if ((md = EVP_MD_fetch(ctx->libctx, mdname, mdprops)) == NULL) {
358
0
        ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
359
0
                       "%s could not be fetched", mdname);
360
0
        return 0;
361
0
    }
362
    /* The default for mgf1 is SHA1 - so allow SHA1 */
363
4.32k
    if ((mdnid = ossl_digest_rsa_sign_get_md_nid(ctx->libctx, md, 1)) <= 0
364
4.32k
        || !rsa_check_padding(ctx, NULL, mdname, mdnid)) {
365
0
        if (mdnid <= 0)
366
0
            ERR_raise_data(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED,
367
0
                           "digest=%s", mdname);
368
0
        EVP_MD_free(md);
369
0
        return 0;
370
0
    }
371
4.32k
    len = OPENSSL_strlcpy(ctx->mgf1_mdname, mdname, sizeof(ctx->mgf1_mdname));
372
4.32k
    if (len >= sizeof(ctx->mgf1_mdname)) {
373
0
        ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
374
0
                       "%s exceeds name buffer length", mdname);
375
0
        EVP_MD_free(md);
376
0
        return 0;
377
0
    }
378
379
4.32k
    EVP_MD_free(ctx->mgf1_md);
380
4.32k
    ctx->mgf1_md = md;
381
4.32k
    ctx->mgf1_mdnid = mdnid;
382
4.32k
    ctx->mgf1_md_set = 1;
383
4.32k
    return 1;
384
4.32k
}
385
386
static int rsa_signverify_init(void *vprsactx, void *vrsa,
387
                               const OSSL_PARAM params[], int operation)
388
35.3k
{
389
35.3k
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
390
391
35.3k
    if (!ossl_prov_is_running() || prsactx == NULL)
392
0
        return 0;
393
394
35.3k
    if (vrsa == NULL && prsactx->rsa == NULL) {
395
0
        ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
396
0
        return 0;
397
0
    }
398
399
35.3k
    if (vrsa != NULL) {
400
35.3k
        if (!ossl_rsa_check_key(prsactx->libctx, vrsa, operation))
401
0
            return 0;
402
403
35.3k
        if (!RSA_up_ref(vrsa))
404
0
            return 0;
405
35.3k
        RSA_free(prsactx->rsa);
406
35.3k
        prsactx->rsa = vrsa;
407
35.3k
    }
408
409
35.3k
    prsactx->operation = operation;
410
411
    /* Maximum for sign, auto for verify */
412
35.3k
    prsactx->saltlen = RSA_PSS_SALTLEN_AUTO;
413
35.3k
    prsactx->min_saltlen = -1;
414
415
35.3k
    switch (RSA_test_flags(prsactx->rsa, RSA_FLAG_TYPE_MASK)) {
416
35.0k
    case RSA_FLAG_TYPE_RSA:
417
35.0k
        prsactx->pad_mode = RSA_PKCS1_PADDING;
418
35.0k
        break;
419
340
    case RSA_FLAG_TYPE_RSASSAPSS:
420
340
        prsactx->pad_mode = RSA_PKCS1_PSS_PADDING;
421
422
340
        {
423
340
            const RSA_PSS_PARAMS_30 *pss =
424
340
                ossl_rsa_get0_pss_params_30(prsactx->rsa);
425
426
340
            if (!ossl_rsa_pss_params_30_is_unrestricted(pss)) {
427
338
                int md_nid = ossl_rsa_pss_params_30_hashalg(pss);
428
338
                int mgf1md_nid = ossl_rsa_pss_params_30_maskgenhashalg(pss);
429
338
                int min_saltlen = ossl_rsa_pss_params_30_saltlen(pss);
430
338
                const char *mdname, *mgf1mdname;
431
338
                size_t len;
432
433
338
                mdname = ossl_rsa_oaeppss_nid2name(md_nid);
434
338
                mgf1mdname = ossl_rsa_oaeppss_nid2name(mgf1md_nid);
435
436
338
                if (mdname == NULL) {
437
0
                    ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
438
0
                                   "PSS restrictions lack hash algorithm");
439
0
                    return 0;
440
0
                }
441
338
                if (mgf1mdname == NULL) {
442
0
                    ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
443
0
                                   "PSS restrictions lack MGF1 hash algorithm");
444
0
                    return 0;
445
0
                }
446
447
338
                len = OPENSSL_strlcpy(prsactx->mdname, mdname,
448
338
                                      sizeof(prsactx->mdname));
449
338
                if (len >= sizeof(prsactx->mdname)) {
450
0
                    ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
451
0
                                   "hash algorithm name too long");
452
0
                    return 0;
453
0
                }
454
338
                len = OPENSSL_strlcpy(prsactx->mgf1_mdname, mgf1mdname,
455
338
                                      sizeof(prsactx->mgf1_mdname));
456
338
                if (len >= sizeof(prsactx->mgf1_mdname)) {
457
0
                    ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
458
0
                                   "MGF1 hash algorithm name too long");
459
0
                    return 0;
460
0
                }
461
338
                prsactx->saltlen = min_saltlen;
462
463
                /* call rsa_setup_mgf1_md before rsa_setup_md to avoid duplication */
464
338
                if (!rsa_setup_mgf1_md(prsactx, mgf1mdname, prsactx->propq)
465
338
                    || !rsa_setup_md(prsactx, mdname, prsactx->propq)
466
338
                    || !rsa_check_parameters(prsactx, min_saltlen))
467
15
                    return 0;
468
338
            }
469
340
        }
470
471
325
        break;
472
325
    default:
473
0
        ERR_raise(ERR_LIB_RSA, PROV_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
474
0
        return 0;
475
35.3k
    }
476
477
35.3k
    if (!rsa_set_ctx_params(prsactx, params))
478
0
        return 0;
479
480
35.3k
    return 1;
481
35.3k
}
482
483
static int setup_tbuf(PROV_RSA_CTX *ctx)
484
12.2k
{
485
12.2k
    if (ctx->tbuf != NULL)
486
0
        return 1;
487
12.2k
    if ((ctx->tbuf = OPENSSL_malloc(RSA_size(ctx->rsa))) == NULL) {
488
14
        ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
489
14
        return 0;
490
14
    }
491
12.2k
    return 1;
492
12.2k
}
493
494
static void clean_tbuf(PROV_RSA_CTX *ctx)
495
69.4k
{
496
69.4k
    if (ctx->tbuf != NULL)
497
13.1k
        OPENSSL_cleanse(ctx->tbuf, RSA_size(ctx->rsa));
498
69.4k
}
499
500
static void free_tbuf(PROV_RSA_CTX *ctx)
501
68.5k
{
502
68.5k
    clean_tbuf(ctx);
503
68.5k
    OPENSSL_free(ctx->tbuf);
504
68.5k
    ctx->tbuf = NULL;
505
68.5k
}
506
507
static int rsa_sign_init(void *vprsactx, void *vrsa, const OSSL_PARAM params[])
508
0
{
509
0
    if (!ossl_prov_is_running())
510
0
        return 0;
511
0
    return rsa_signverify_init(vprsactx, vrsa, params, EVP_PKEY_OP_SIGN);
512
0
}
513
514
static int rsa_sign(void *vprsactx, unsigned char *sig, size_t *siglen,
515
                    size_t sigsize, const unsigned char *tbs, size_t tbslen)
516
3.67k
{
517
3.67k
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
518
3.67k
    int ret;
519
3.67k
    size_t rsasize = RSA_size(prsactx->rsa);
520
3.67k
    size_t mdsize = rsa_get_md_size(prsactx);
521
522
3.67k
    if (!ossl_prov_is_running())
523
0
        return 0;
524
525
3.67k
    if (sig == NULL) {
526
1.83k
        *siglen = rsasize;
527
1.83k
        return 1;
528
1.83k
    }
529
530
1.83k
    if (sigsize < rsasize) {
531
0
        ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_SIGNATURE_SIZE,
532
0
                       "is %zu, should be at least %zu", sigsize, rsasize);
533
0
        return 0;
534
0
    }
535
536
1.83k
    if (mdsize != 0) {
537
1.83k
        if (tbslen != mdsize) {
538
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_LENGTH);
539
0
            return 0;
540
0
        }
541
542
1.83k
#ifndef FIPS_MODULE
543
1.83k
        if (EVP_MD_is_a(prsactx->md, OSSL_DIGEST_NAME_MDC2)) {
544
0
            unsigned int sltmp;
545
546
0
            if (prsactx->pad_mode != RSA_PKCS1_PADDING) {
547
0
                ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_PADDING_MODE,
548
0
                               "only PKCS#1 padding supported with MDC2");
549
0
                return 0;
550
0
            }
551
0
            ret = RSA_sign_ASN1_OCTET_STRING(0, tbs, tbslen, sig, &sltmp,
552
0
                                             prsactx->rsa);
553
554
0
            if (ret <= 0) {
555
0
                ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
556
0
                return 0;
557
0
            }
558
0
            ret = sltmp;
559
0
            goto end;
560
0
        }
561
1.83k
#endif
562
1.83k
        switch (prsactx->pad_mode) {
563
0
        case RSA_X931_PADDING:
564
0
            if ((size_t)RSA_size(prsactx->rsa) < tbslen + 1) {
565
0
                ERR_raise_data(ERR_LIB_PROV, PROV_R_KEY_SIZE_TOO_SMALL,
566
0
                               "RSA key size = %d, expected minimum = %d",
567
0
                               RSA_size(prsactx->rsa), tbslen + 1);
568
0
                return 0;
569
0
            }
570
0
            if (!setup_tbuf(prsactx)) {
571
0
                ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
572
0
                return 0;
573
0
            }
574
0
            memcpy(prsactx->tbuf, tbs, tbslen);
575
0
            prsactx->tbuf[tbslen] = RSA_X931_hash_id(prsactx->mdnid);
576
0
            ret = RSA_private_encrypt(tbslen + 1, prsactx->tbuf,
577
0
                                      sig, prsactx->rsa, RSA_X931_PADDING);
578
0
            clean_tbuf(prsactx);
579
0
            break;
580
581
1.47k
        case RSA_PKCS1_PADDING:
582
1.47k
            {
583
1.47k
                unsigned int sltmp;
584
585
1.47k
                ret = RSA_sign(prsactx->mdnid, tbs, tbslen, sig, &sltmp,
586
1.47k
                               prsactx->rsa);
587
1.47k
                if (ret <= 0) {
588
0
                    ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
589
0
                    return 0;
590
0
                }
591
1.47k
                ret = sltmp;
592
1.47k
            }
593
0
            break;
594
595
361
        case RSA_PKCS1_PSS_PADDING:
596
            /* Check PSS restrictions */
597
361
            if (rsa_pss_restricted(prsactx)) {
598
0
                switch (prsactx->saltlen) {
599
0
                case RSA_PSS_SALTLEN_DIGEST:
600
0
                    if (prsactx->min_saltlen > EVP_MD_get_size(prsactx->md)) {
601
0
                        ERR_raise_data(ERR_LIB_PROV,
602
0
                                       PROV_R_PSS_SALTLEN_TOO_SMALL,
603
0
                                       "minimum salt length set to %d, "
604
0
                                       "but the digest only gives %d",
605
0
                                       prsactx->min_saltlen,
606
0
                                       EVP_MD_get_size(prsactx->md));
607
0
                        return 0;
608
0
                    }
609
                    /* FALLTHRU */
610
0
                default:
611
0
                    if (prsactx->saltlen >= 0
612
0
                        && prsactx->saltlen < prsactx->min_saltlen) {
613
0
                        ERR_raise_data(ERR_LIB_PROV,
614
0
                                       PROV_R_PSS_SALTLEN_TOO_SMALL,
615
0
                                       "minimum salt length set to %d, but the"
616
0
                                       "actual salt length is only set to %d",
617
0
                                       prsactx->min_saltlen,
618
0
                                       prsactx->saltlen);
619
0
                        return 0;
620
0
                    }
621
0
                    break;
622
0
                }
623
0
            }
624
361
            if (!setup_tbuf(prsactx))
625
0
                return 0;
626
361
            if (!RSA_padding_add_PKCS1_PSS_mgf1(prsactx->rsa,
627
361
                                                prsactx->tbuf, tbs,
628
361
                                                prsactx->md, prsactx->mgf1_md,
629
361
                                                prsactx->saltlen)) {
630
0
                ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
631
0
                return 0;
632
0
            }
633
361
            ret = RSA_private_encrypt(RSA_size(prsactx->rsa), prsactx->tbuf,
634
361
                                      sig, prsactx->rsa, RSA_NO_PADDING);
635
361
            clean_tbuf(prsactx);
636
361
            break;
637
638
0
        default:
639
0
            ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_PADDING_MODE,
640
0
                           "Only X.931, PKCS#1 v1.5 or PSS padding allowed");
641
0
            return 0;
642
1.83k
        }
643
1.83k
    } else {
644
0
        ret = RSA_private_encrypt(tbslen, tbs, sig, prsactx->rsa,
645
0
                                  prsactx->pad_mode);
646
0
    }
647
648
1.83k
#ifndef FIPS_MODULE
649
1.83k
 end:
650
1.83k
#endif
651
1.83k
    if (ret <= 0) {
652
0
        ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
653
0
        return 0;
654
0
    }
655
656
1.83k
    *siglen = ret;
657
1.83k
    return 1;
658
1.83k
}
659
660
static int rsa_verify_recover_init(void *vprsactx, void *vrsa,
661
                                   const OSSL_PARAM params[])
662
0
{
663
0
    if (!ossl_prov_is_running())
664
0
        return 0;
665
0
    return rsa_signverify_init(vprsactx, vrsa, params,
666
0
                               EVP_PKEY_OP_VERIFYRECOVER);
667
0
}
668
669
static int rsa_verify_recover(void *vprsactx,
670
                              unsigned char *rout,
671
                              size_t *routlen,
672
                              size_t routsize,
673
                              const unsigned char *sig,
674
                              size_t siglen)
675
0
{
676
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
677
0
    int ret;
678
679
0
    if (!ossl_prov_is_running())
680
0
        return 0;
681
682
0
    if (rout == NULL) {
683
0
        *routlen = RSA_size(prsactx->rsa);
684
0
        return 1;
685
0
    }
686
687
0
    if (prsactx->md != NULL) {
688
0
        switch (prsactx->pad_mode) {
689
0
        case RSA_X931_PADDING:
690
0
            if (!setup_tbuf(prsactx))
691
0
                return 0;
692
0
            ret = RSA_public_decrypt(siglen, sig, prsactx->tbuf, prsactx->rsa,
693
0
                                     RSA_X931_PADDING);
694
0
            if (ret < 1) {
695
0
                ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
696
0
                return 0;
697
0
            }
698
0
            ret--;
699
0
            if (prsactx->tbuf[ret] != RSA_X931_hash_id(prsactx->mdnid)) {
700
0
                ERR_raise(ERR_LIB_PROV, PROV_R_ALGORITHM_MISMATCH);
701
0
                return 0;
702
0
            }
703
0
            if (ret != EVP_MD_get_size(prsactx->md)) {
704
0
                ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_LENGTH,
705
0
                               "Should be %d, but got %d",
706
0
                               EVP_MD_get_size(prsactx->md), ret);
707
0
                return 0;
708
0
            }
709
710
0
            *routlen = ret;
711
0
            if (rout != prsactx->tbuf) {
712
0
                if (routsize < (size_t)ret) {
713
0
                    ERR_raise_data(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL,
714
0
                                   "buffer size is %d, should be %d",
715
0
                                   routsize, ret);
716
0
                    return 0;
717
0
                }
718
0
                memcpy(rout, prsactx->tbuf, ret);
719
0
            }
720
0
            break;
721
722
0
        case RSA_PKCS1_PADDING:
723
0
            {
724
0
                size_t sltmp;
725
726
0
                ret = ossl_rsa_verify(prsactx->mdnid, NULL, 0, rout, &sltmp,
727
0
                                      sig, siglen, prsactx->rsa);
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
            }
734
0
            break;
735
736
0
        default:
737
0
            ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_PADDING_MODE,
738
0
                           "Only X.931 or PKCS#1 v1.5 padding allowed");
739
0
            return 0;
740
0
        }
741
0
    } else {
742
0
        ret = RSA_public_decrypt(siglen, sig, rout, prsactx->rsa,
743
0
                                 prsactx->pad_mode);
744
0
        if (ret < 0) {
745
0
            ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
746
0
            return 0;
747
0
        }
748
0
    }
749
0
    *routlen = ret;
750
0
    return 1;
751
0
}
752
753
static int rsa_verify_init(void *vprsactx, void *vrsa,
754
                           const OSSL_PARAM params[])
755
0
{
756
0
    if (!ossl_prov_is_running())
757
0
        return 0;
758
0
    return rsa_signverify_init(vprsactx, vrsa, params, EVP_PKEY_OP_VERIFY);
759
0
}
760
761
static int rsa_verify(void *vprsactx, const unsigned char *sig, size_t siglen,
762
                      const unsigned char *tbs, size_t tbslen)
763
9.10k
{
764
9.10k
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
765
9.10k
    size_t rslen;
766
767
9.10k
    if (!ossl_prov_is_running())
768
0
        return 0;
769
9.10k
    if (prsactx->md != NULL) {
770
9.10k
        switch (prsactx->pad_mode) {
771
2.27k
        case RSA_PKCS1_PADDING:
772
2.27k
            if (!RSA_verify(prsactx->mdnid, tbs, tbslen, sig, siglen,
773
2.27k
                            prsactx->rsa)) {
774
2.01k
                ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
775
2.01k
                return 0;
776
2.01k
            }
777
257
            return 1;
778
0
        case RSA_X931_PADDING:
779
0
            if (!setup_tbuf(prsactx))
780
0
                return 0;
781
0
            if (rsa_verify_recover(prsactx, prsactx->tbuf, &rslen, 0,
782
0
                                   sig, siglen) <= 0)
783
0
                return 0;
784
0
            break;
785
6.83k
        case RSA_PKCS1_PSS_PADDING:
786
6.83k
            {
787
6.83k
                int ret;
788
6.83k
                size_t mdsize;
789
790
                /*
791
                 * We need to check this for the RSA_verify_PKCS1_PSS_mgf1()
792
                 * call
793
                 */
794
6.83k
                mdsize = rsa_get_md_size(prsactx);
795
6.83k
                if (tbslen != mdsize) {
796
0
                    ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_LENGTH,
797
0
                                   "Should be %d, but got %d",
798
0
                                   mdsize, tbslen);
799
0
                    return 0;
800
0
                }
801
802
6.83k
                if (!setup_tbuf(prsactx))
803
10
                    return 0;
804
6.82k
                ret = RSA_public_decrypt(siglen, sig, prsactx->tbuf,
805
6.82k
                                         prsactx->rsa, RSA_NO_PADDING);
806
6.82k
                if (ret <= 0) {
807
1.29k
                    ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
808
1.29k
                    return 0;
809
1.29k
                }
810
5.52k
                ret = RSA_verify_PKCS1_PSS_mgf1(prsactx->rsa, tbs,
811
5.52k
                                                prsactx->md, prsactx->mgf1_md,
812
5.52k
                                                prsactx->tbuf,
813
5.52k
                                                prsactx->saltlen);
814
5.52k
                if (ret <= 0) {
815
5.52k
                    ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
816
5.52k
                    return 0;
817
5.52k
                }
818
7
                return 1;
819
5.52k
            }
820
0
        default:
821
0
            ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_PADDING_MODE,
822
0
                           "Only X.931, PKCS#1 v1.5 or PSS padding allowed");
823
0
            return 0;
824
9.10k
        }
825
9.10k
    } else {
826
0
        int ret;
827
828
0
        if (!setup_tbuf(prsactx))
829
0
            return 0;
830
0
        ret = RSA_public_decrypt(siglen, sig, prsactx->tbuf, prsactx->rsa,
831
0
                                 prsactx->pad_mode);
832
0
        if (ret <= 0) {
833
0
            ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
834
0
            return 0;
835
0
        }
836
0
        rslen = (size_t)ret;
837
0
    }
838
839
0
    if ((rslen != tbslen) || memcmp(tbs, prsactx->tbuf, rslen))
840
0
        return 0;
841
842
0
    return 1;
843
0
}
844
845
static int rsa_digest_signverify_init(void *vprsactx, const char *mdname,
846
                                      void *vrsa, const OSSL_PARAM params[],
847
                                      int operation)
848
35.3k
{
849
35.3k
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
850
851
35.3k
    if (!ossl_prov_is_running())
852
0
        return 0;
853
854
35.3k
    if (!rsa_signverify_init(vprsactx, vrsa, params, operation))
855
15
        return 0;
856
857
35.3k
    if (mdname != NULL
858
        /* was rsa_setup_md already called in rsa_signverify_init()? */
859
35.3k
        && (mdname[0] == '\0' || OPENSSL_strcasecmp(prsactx->mdname, mdname) != 0)
860
35.3k
        && !rsa_setup_md(prsactx, mdname, prsactx->propq))
861
57
        return 0;
862
863
35.3k
    prsactx->flag_allow_md = 0;
864
865
35.3k
    if (prsactx->mdctx == NULL) {
866
35.3k
        prsactx->mdctx = EVP_MD_CTX_new();
867
35.3k
        if (prsactx->mdctx == NULL)
868
0
            goto error;
869
35.3k
    }
870
871
35.3k
    if (!EVP_DigestInit_ex2(prsactx->mdctx, prsactx->md, params))
872
0
        goto error;
873
874
35.3k
    return 1;
875
876
0
 error:
877
0
    EVP_MD_CTX_free(prsactx->mdctx);
878
0
    prsactx->mdctx = NULL;
879
0
    return 0;
880
35.3k
}
881
882
static int rsa_digest_signverify_update(void *vprsactx,
883
                                        const unsigned char *data,
884
                                        size_t datalen)
885
10.9k
{
886
10.9k
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
887
888
10.9k
    if (prsactx == NULL || prsactx->mdctx == NULL)
889
0
        return 0;
890
891
10.9k
    return EVP_DigestUpdate(prsactx->mdctx, data, datalen);
892
10.9k
}
893
894
static int rsa_digest_sign_init(void *vprsactx, const char *mdname,
895
                                void *vrsa, const OSSL_PARAM params[])
896
36.5k
{
897
36.5k
    if (!ossl_prov_is_running())
898
0
        return 0;
899
36.5k
    return rsa_digest_signverify_init(vprsactx, mdname, vrsa,
900
36.5k
                                      params, EVP_PKEY_OP_SIGN);
901
36.5k
}
902
903
static int rsa_digest_sign_final(void *vprsactx, unsigned char *sig,
904
                                 size_t *siglen, size_t sigsize)
905
3.67k
{
906
3.67k
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
907
3.67k
    unsigned char digest[EVP_MAX_MD_SIZE];
908
3.67k
    unsigned int dlen = 0;
909
910
3.67k
    if (!ossl_prov_is_running() || prsactx == NULL)
911
0
        return 0;
912
3.67k
    prsactx->flag_allow_md = 1;
913
3.67k
    if (prsactx->mdctx == NULL)
914
0
        return 0;
915
    /*
916
     * If sig is NULL then we're just finding out the sig size. Other fields
917
     * are ignored. Defer to rsa_sign.
918
     */
919
3.67k
    if (sig != NULL) {
920
        /*
921
         * The digests used here are all known (see rsa_get_md_nid()), so they
922
         * should not exceed the internal buffer size of EVP_MAX_MD_SIZE.
923
         */
924
1.83k
        if (!EVP_DigestFinal_ex(prsactx->mdctx, digest, &dlen))
925
0
            return 0;
926
1.83k
    }
927
928
3.67k
    return rsa_sign(vprsactx, sig, siglen, sigsize, digest, (size_t)dlen);
929
3.67k
}
930
931
static int rsa_digest_verify_init(void *vprsactx, const char *mdname,
932
                                  void *vrsa, const OSSL_PARAM params[])
933
14.3k
{
934
14.3k
    if (!ossl_prov_is_running())
935
0
        return 0;
936
14.3k
    return rsa_digest_signverify_init(vprsactx, mdname, vrsa,
937
14.3k
                                      params, EVP_PKEY_OP_VERIFY);
938
14.3k
}
939
940
int rsa_digest_verify_final(void *vprsactx, const unsigned char *sig,
941
                            size_t siglen)
942
9.10k
{
943
9.10k
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
944
9.10k
    unsigned char digest[EVP_MAX_MD_SIZE];
945
9.10k
    unsigned int dlen = 0;
946
947
9.10k
    if (!ossl_prov_is_running())
948
0
        return 0;
949
950
9.10k
    if (prsactx == NULL)
951
0
        return 0;
952
9.10k
    prsactx->flag_allow_md = 1;
953
9.10k
    if (prsactx->mdctx == NULL)
954
0
        return 0;
955
956
    /*
957
     * The digests used here are all known (see rsa_get_md_nid()), so they
958
     * should not exceed the internal buffer size of EVP_MAX_MD_SIZE.
959
     */
960
9.10k
    if (!EVP_DigestFinal_ex(prsactx->mdctx, digest, &dlen))
961
0
        return 0;
962
963
9.10k
    return rsa_verify(vprsactx, sig, siglen, digest, (size_t)dlen);
964
9.10k
}
965
966
static void rsa_freectx(void *vprsactx)
967
68.5k
{
968
68.5k
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
969
970
68.5k
    if (prsactx == NULL)
971
0
        return;
972
973
68.5k
    EVP_MD_CTX_free(prsactx->mdctx);
974
68.5k
    EVP_MD_free(prsactx->md);
975
68.5k
    EVP_MD_free(prsactx->mgf1_md);
976
68.5k
    OPENSSL_free(prsactx->propq);
977
68.5k
    free_tbuf(prsactx);
978
68.5k
    RSA_free(prsactx->rsa);
979
980
68.5k
    OPENSSL_clear_free(prsactx, sizeof(*prsactx));
981
68.5k
}
982
983
static void *rsa_dupctx(void *vprsactx)
984
17.5k
{
985
17.5k
    PROV_RSA_CTX *srcctx = (PROV_RSA_CTX *)vprsactx;
986
17.5k
    PROV_RSA_CTX *dstctx;
987
988
17.5k
    if (!ossl_prov_is_running())
989
0
        return NULL;
990
991
17.5k
    dstctx = OPENSSL_zalloc(sizeof(*srcctx));
992
17.5k
    if (dstctx == NULL) {
993
0
        ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
994
0
        return NULL;
995
0
    }
996
997
17.5k
    *dstctx = *srcctx;
998
17.5k
    dstctx->rsa = NULL;
999
17.5k
    dstctx->md = NULL;
1000
17.5k
    dstctx->mgf1_md = NULL;
1001
17.5k
    dstctx->mdctx = NULL;
1002
17.5k
    dstctx->tbuf = NULL;
1003
17.5k
    dstctx->propq = NULL;
1004
1005
17.5k
    if (srcctx->rsa != NULL && !RSA_up_ref(srcctx->rsa))
1006
0
        goto err;
1007
17.5k
    dstctx->rsa = srcctx->rsa;
1008
1009
17.5k
    if (srcctx->md != NULL && !EVP_MD_up_ref(srcctx->md))
1010
0
        goto err;
1011
17.5k
    dstctx->md = srcctx->md;
1012
1013
17.5k
    if (srcctx->mgf1_md != NULL && !EVP_MD_up_ref(srcctx->mgf1_md))
1014
0
        goto err;
1015
17.5k
    dstctx->mgf1_md = srcctx->mgf1_md;
1016
1017
17.5k
    if (srcctx->mdctx != NULL) {
1018
17.5k
        dstctx->mdctx = EVP_MD_CTX_new();
1019
17.5k
        if (dstctx->mdctx == NULL
1020
17.5k
                || !EVP_MD_CTX_copy_ex(dstctx->mdctx, srcctx->mdctx))
1021
0
            goto err;
1022
17.5k
    }
1023
1024
17.5k
    if (srcctx->propq != NULL) {
1025
0
        dstctx->propq = OPENSSL_strdup(srcctx->propq);
1026
0
        if (dstctx->propq == NULL)
1027
0
            goto err;
1028
0
    }
1029
1030
17.5k
    return dstctx;
1031
0
 err:
1032
0
    rsa_freectx(dstctx);
1033
0
    return NULL;
1034
17.5k
}
1035
1036
static int rsa_get_ctx_params(void *vprsactx, OSSL_PARAM *params)
1037
0
{
1038
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1039
0
    OSSL_PARAM *p;
1040
1041
0
    if (prsactx == NULL)
1042
0
        return 0;
1043
1044
0
    p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_ALGORITHM_ID);
1045
0
    if (p != NULL) {
1046
        /* The Algorithm Identifier of the combined signature algorithm */
1047
0
        unsigned char aid_buf[128];
1048
0
        unsigned char *aid;
1049
0
        size_t  aid_len;
1050
1051
0
        aid = rsa_generate_signature_aid(prsactx, aid_buf,
1052
0
                                         sizeof(aid_buf), &aid_len);
1053
0
        if (aid == NULL || !OSSL_PARAM_set_octet_string(p, aid, aid_len))
1054
0
            return 0;
1055
0
    }
1056
1057
0
    p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_PAD_MODE);
1058
0
    if (p != NULL)
1059
0
        switch (p->data_type) {
1060
0
        case OSSL_PARAM_INTEGER:
1061
0
            if (!OSSL_PARAM_set_int(p, prsactx->pad_mode))
1062
0
                return 0;
1063
0
            break;
1064
0
        case OSSL_PARAM_UTF8_STRING:
1065
0
            {
1066
0
                int i;
1067
0
                const char *word = NULL;
1068
1069
0
                for (i = 0; padding_item[i].id != 0; i++) {
1070
0
                    if (prsactx->pad_mode == (int)padding_item[i].id) {
1071
0
                        word = padding_item[i].ptr;
1072
0
                        break;
1073
0
                    }
1074
0
                }
1075
1076
0
                if (word != NULL) {
1077
0
                    if (!OSSL_PARAM_set_utf8_string(p, word))
1078
0
                        return 0;
1079
0
                } else {
1080
0
                    ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
1081
0
                }
1082
0
            }
1083
0
            break;
1084
0
        default:
1085
0
            return 0;
1086
0
        }
1087
1088
0
    p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_DIGEST);
1089
0
    if (p != NULL && !OSSL_PARAM_set_utf8_string(p, prsactx->mdname))
1090
0
        return 0;
1091
1092
0
    p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_MGF1_DIGEST);
1093
0
    if (p != NULL && !OSSL_PARAM_set_utf8_string(p, prsactx->mgf1_mdname))
1094
0
        return 0;
1095
1096
0
    p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_PSS_SALTLEN);
1097
0
    if (p != NULL) {
1098
0
        if (p->data_type == OSSL_PARAM_INTEGER) {
1099
0
            if (!OSSL_PARAM_set_int(p, prsactx->saltlen))
1100
0
                return 0;
1101
0
        } else if (p->data_type == OSSL_PARAM_UTF8_STRING) {
1102
0
            const char *value = NULL;
1103
1104
0
            switch (prsactx->saltlen) {
1105
0
            case RSA_PSS_SALTLEN_DIGEST:
1106
0
                value = OSSL_PKEY_RSA_PSS_SALT_LEN_DIGEST;
1107
0
                break;
1108
0
            case RSA_PSS_SALTLEN_MAX:
1109
0
                value = OSSL_PKEY_RSA_PSS_SALT_LEN_MAX;
1110
0
                break;
1111
0
            case RSA_PSS_SALTLEN_AUTO:
1112
0
                value = OSSL_PKEY_RSA_PSS_SALT_LEN_AUTO;
1113
0
                break;
1114
0
            default:
1115
0
                {
1116
0
                    int len = BIO_snprintf(p->data, p->data_size, "%d",
1117
0
                                           prsactx->saltlen);
1118
1119
0
                    if (len <= 0)
1120
0
                        return 0;
1121
0
                    p->return_size = len;
1122
0
                    break;
1123
0
                }
1124
0
            }
1125
0
            if (value != NULL
1126
0
                && !OSSL_PARAM_set_utf8_string(p, value))
1127
0
                return 0;
1128
0
        }
1129
0
    }
1130
1131
0
    return 1;
1132
0
}
1133
1134
static const OSSL_PARAM known_gettable_ctx_params[] = {
1135
    OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_ALGORITHM_ID, NULL, 0),
1136
    OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PAD_MODE, NULL, 0),
1137
    OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),
1138
    OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_MGF1_DIGEST, NULL, 0),
1139
    OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PSS_SALTLEN, NULL, 0),
1140
    OSSL_PARAM_END
1141
};
1142
1143
static const OSSL_PARAM *rsa_gettable_ctx_params(ossl_unused void *vprsactx,
1144
                                                 ossl_unused void *provctx)
1145
0
{
1146
0
    return known_gettable_ctx_params;
1147
0
}
1148
1149
static int rsa_set_ctx_params(void *vprsactx, const OSSL_PARAM params[])
1150
14.9k
{
1151
14.9k
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1152
14.9k
    const OSSL_PARAM *p;
1153
14.9k
    int pad_mode;
1154
14.9k
    int saltlen;
1155
14.9k
    char mdname[OSSL_MAX_NAME_SIZE] = "", *pmdname = NULL;
1156
14.9k
    char mdprops[OSSL_MAX_PROPQUERY_SIZE] = "", *pmdprops = NULL;
1157
14.9k
    char mgf1mdname[OSSL_MAX_NAME_SIZE] = "", *pmgf1mdname = NULL;
1158
14.9k
    char mgf1mdprops[OSSL_MAX_PROPQUERY_SIZE] = "", *pmgf1mdprops = NULL;
1159
1160
14.9k
    if (prsactx == NULL)
1161
0
        return 0;
1162
14.9k
    if (params == NULL)
1163
11.7k
        return 1;
1164
1165
3.25k
    pad_mode = prsactx->pad_mode;
1166
3.25k
    saltlen = prsactx->saltlen;
1167
1168
3.25k
    p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_DIGEST);
1169
3.25k
    if (p != NULL) {
1170
0
        const OSSL_PARAM *propsp =
1171
0
            OSSL_PARAM_locate_const(params,
1172
0
                                    OSSL_SIGNATURE_PARAM_PROPERTIES);
1173
1174
0
        pmdname = mdname;
1175
0
        if (!OSSL_PARAM_get_utf8_string(p, &pmdname, sizeof(mdname)))
1176
0
            return 0;
1177
1178
0
        if (propsp != NULL) {
1179
0
            pmdprops = mdprops;
1180
0
            if (!OSSL_PARAM_get_utf8_string(propsp,
1181
0
                                            &pmdprops, sizeof(mdprops)))
1182
0
                return 0;
1183
0
        }
1184
0
    }
1185
1186
3.25k
    p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_PAD_MODE);
1187
3.25k
    if (p != NULL) {
1188
1.15k
        const char *err_extra_text = NULL;
1189
1190
1.15k
        switch (p->data_type) {
1191
1.15k
        case OSSL_PARAM_INTEGER: /* Support for legacy pad mode number */
1192
1.15k
            if (!OSSL_PARAM_get_int(p, &pad_mode))
1193
0
                return 0;
1194
1.15k
            break;
1195
1.15k
        case OSSL_PARAM_UTF8_STRING:
1196
0
            {
1197
0
                int i;
1198
1199
0
                if (p->data == NULL)
1200
0
                    return 0;
1201
1202
0
                for (i = 0; padding_item[i].id != 0; i++) {
1203
0
                    if (strcmp(p->data, padding_item[i].ptr) == 0) {
1204
0
                        pad_mode = padding_item[i].id;
1205
0
                        break;
1206
0
                    }
1207
0
                }
1208
0
            }
1209
0
            break;
1210
0
        default:
1211
0
            return 0;
1212
1.15k
        }
1213
1214
1.15k
        switch (pad_mode) {
1215
0
        case RSA_PKCS1_OAEP_PADDING:
1216
            /*
1217
             * OAEP padding is for asymmetric cipher only so is not compatible
1218
             * with signature use.
1219
             */
1220
0
            err_extra_text = "OAEP padding not allowed for signing / verifying";
1221
0
            goto bad_pad;
1222
1.15k
        case RSA_PKCS1_PSS_PADDING:
1223
1.15k
            if ((prsactx->operation
1224
1.15k
                 & (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY)) == 0) {
1225
0
                err_extra_text =
1226
0
                    "PSS padding only allowed for sign and verify operations";
1227
0
                goto bad_pad;
1228
0
            }
1229
1.15k
            break;
1230
1.15k
        case RSA_PKCS1_PADDING:
1231
0
            err_extra_text = "PKCS#1 padding not allowed with RSA-PSS";
1232
0
            goto cont;
1233
0
        case RSA_NO_PADDING:
1234
0
            err_extra_text = "No padding not allowed with RSA-PSS";
1235
0
            goto cont;
1236
0
        case RSA_X931_PADDING:
1237
0
            err_extra_text = "X.931 padding not allowed with RSA-PSS";
1238
0
        cont:
1239
0
            if (RSA_test_flags(prsactx->rsa,
1240
0
                               RSA_FLAG_TYPE_MASK) == RSA_FLAG_TYPE_RSA)
1241
0
                break;
1242
            /* FALLTHRU */
1243
0
        default:
1244
0
        bad_pad:
1245
0
            if (err_extra_text == NULL)
1246
0
                ERR_raise(ERR_LIB_PROV,
1247
0
                          PROV_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
1248
0
            else
1249
0
                ERR_raise_data(ERR_LIB_PROV,
1250
0
                               PROV_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE,
1251
0
                               err_extra_text);
1252
0
            return 0;
1253
1.15k
        }
1254
1.15k
    }
1255
1256
3.25k
    p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_PSS_SALTLEN);
1257
3.25k
    if (p != NULL) {
1258
1.15k
        if (pad_mode != RSA_PKCS1_PSS_PADDING) {
1259
0
            ERR_raise_data(ERR_LIB_PROV, PROV_R_NOT_SUPPORTED,
1260
0
                           "PSS saltlen can only be specified if "
1261
0
                           "PSS padding has been specified first");
1262
0
            return 0;
1263
0
        }
1264
1265
1.15k
        switch (p->data_type) {
1266
0
        case OSSL_PARAM_INTEGER: /* Support for legacy pad mode number */
1267
0
            if (!OSSL_PARAM_get_int(p, &saltlen))
1268
0
                return 0;
1269
0
            break;
1270
1.15k
        case OSSL_PARAM_UTF8_STRING:
1271
1.15k
            if (strcmp(p->data, OSSL_PKEY_RSA_PSS_SALT_LEN_DIGEST) == 0)
1272
205
                saltlen = RSA_PSS_SALTLEN_DIGEST;
1273
947
            else if (strcmp(p->data, OSSL_PKEY_RSA_PSS_SALT_LEN_MAX) == 0)
1274
0
                saltlen = RSA_PSS_SALTLEN_MAX;
1275
947
            else if (strcmp(p->data, OSSL_PKEY_RSA_PSS_SALT_LEN_AUTO) == 0)
1276
0
                saltlen = RSA_PSS_SALTLEN_AUTO;
1277
947
            else
1278
947
                saltlen = atoi(p->data);
1279
1.15k
            break;
1280
0
        default:
1281
0
            return 0;
1282
1.15k
        }
1283
1284
        /*
1285
         * RSA_PSS_SALTLEN_MAX seems curiously named in this check.
1286
         * Contrary to what it's name suggests, it's the currently
1287
         * lowest saltlen number possible.
1288
         */
1289
1.15k
        if (saltlen < RSA_PSS_SALTLEN_MAX) {
1290
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH);
1291
0
            return 0;
1292
0
        }
1293
1294
1.15k
        if (rsa_pss_restricted(prsactx)) {
1295
271
            switch (saltlen) {
1296
0
            case RSA_PSS_SALTLEN_AUTO:
1297
0
                if (prsactx->operation == EVP_PKEY_OP_VERIFY) {
1298
0
                    ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH,
1299
0
                                   "Cannot use autodetected salt length");
1300
0
                    return 0;
1301
0
                }
1302
0
                break;
1303
0
            case RSA_PSS_SALTLEN_DIGEST:
1304
0
                if (prsactx->min_saltlen > EVP_MD_get_size(prsactx->md)) {
1305
0
                    ERR_raise_data(ERR_LIB_PROV,
1306
0
                                   PROV_R_PSS_SALTLEN_TOO_SMALL,
1307
0
                                   "Should be more than %d, but would be "
1308
0
                                   "set to match digest size (%d)",
1309
0
                                   prsactx->min_saltlen,
1310
0
                                   EVP_MD_get_size(prsactx->md));
1311
0
                    return 0;
1312
0
                }
1313
0
                break;
1314
271
            default:
1315
271
                if (saltlen >= 0 && saltlen < prsactx->min_saltlen) {
1316
0
                    ERR_raise_data(ERR_LIB_PROV,
1317
0
                                   PROV_R_PSS_SALTLEN_TOO_SMALL,
1318
0
                                   "Should be more than %d, "
1319
0
                                   "but would be set to %d",
1320
0
                                   prsactx->min_saltlen, saltlen);
1321
0
                    return 0;
1322
0
                }
1323
271
            }
1324
271
        }
1325
1.15k
    }
1326
1327
3.25k
    p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_MGF1_DIGEST);
1328
3.25k
    if (p != NULL) {
1329
947
        const OSSL_PARAM *propsp =
1330
947
            OSSL_PARAM_locate_const(params,
1331
947
                                    OSSL_SIGNATURE_PARAM_MGF1_PROPERTIES);
1332
1333
947
        pmgf1mdname = mgf1mdname;
1334
947
        if (!OSSL_PARAM_get_utf8_string(p, &pmgf1mdname, sizeof(mgf1mdname)))
1335
0
            return 0;
1336
1337
947
        if (propsp != NULL) {
1338
0
            pmgf1mdprops = mgf1mdprops;
1339
0
            if (!OSSL_PARAM_get_utf8_string(propsp,
1340
0
                                            &pmgf1mdprops, sizeof(mgf1mdprops)))
1341
0
                return 0;
1342
0
        }
1343
1344
947
        if (pad_mode != RSA_PKCS1_PSS_PADDING) {
1345
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MGF1_MD);
1346
0
            return  0;
1347
0
        }
1348
947
    }
1349
1350
3.25k
    prsactx->saltlen = saltlen;
1351
3.25k
    prsactx->pad_mode = pad_mode;
1352
1353
3.25k
    if (prsactx->md == NULL && pmdname == NULL
1354
3.25k
        && pad_mode == RSA_PKCS1_PSS_PADDING)
1355
0
        pmdname = RSA_DEFAULT_DIGEST_NAME;
1356
1357
3.25k
    if (pmgf1mdname != NULL
1358
3.25k
        && !rsa_setup_mgf1_md(prsactx, pmgf1mdname, pmgf1mdprops))
1359
0
        return 0;
1360
1361
3.25k
    if (pmdname != NULL) {
1362
0
        if (!rsa_setup_md(prsactx, pmdname, pmdprops))
1363
0
            return 0;
1364
3.25k
    } else {
1365
3.25k
        if (!rsa_check_padding(prsactx, NULL, NULL, prsactx->mdnid))
1366
0
            return 0;
1367
3.25k
    }
1368
3.25k
    return 1;
1369
3.25k
}
1370
1371
static const OSSL_PARAM settable_ctx_params[] = {
1372
    OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),
1373
    OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PROPERTIES, NULL, 0),
1374
    OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PAD_MODE, NULL, 0),
1375
    OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_MGF1_DIGEST, NULL, 0),
1376
    OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_MGF1_PROPERTIES, NULL, 0),
1377
    OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PSS_SALTLEN, NULL, 0),
1378
    OSSL_PARAM_END
1379
};
1380
1381
static const OSSL_PARAM settable_ctx_params_no_digest[] = {
1382
    OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PAD_MODE, NULL, 0),
1383
    OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_MGF1_DIGEST, NULL, 0),
1384
    OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_MGF1_PROPERTIES, NULL, 0),
1385
    OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PSS_SALTLEN, NULL, 0),
1386
    OSSL_PARAM_END
1387
};
1388
1389
static const OSSL_PARAM *rsa_settable_ctx_params(void *vprsactx,
1390
                                                 ossl_unused void *provctx)
1391
28.4k
{
1392
28.4k
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1393
1394
28.4k
    if (prsactx != NULL && !prsactx->flag_allow_md)
1395
28.4k
        return settable_ctx_params_no_digest;
1396
1
    return settable_ctx_params;
1397
28.4k
}
1398
1399
static int rsa_get_ctx_md_params(void *vprsactx, OSSL_PARAM *params)
1400
0
{
1401
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1402
1403
0
    if (prsactx->mdctx == NULL)
1404
0
        return 0;
1405
1406
0
    return EVP_MD_CTX_get_params(prsactx->mdctx, params);
1407
0
}
1408
1409
static const OSSL_PARAM *rsa_gettable_ctx_md_params(void *vprsactx)
1410
0
{
1411
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1412
1413
0
    if (prsactx->md == NULL)
1414
0
        return 0;
1415
1416
0
    return EVP_MD_gettable_ctx_params(prsactx->md);
1417
0
}
1418
1419
static int rsa_set_ctx_md_params(void *vprsactx, const OSSL_PARAM params[])
1420
0
{
1421
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1422
1423
0
    if (prsactx->mdctx == NULL)
1424
0
        return 0;
1425
1426
0
    return EVP_MD_CTX_set_params(prsactx->mdctx, params);
1427
0
}
1428
1429
static const OSSL_PARAM *rsa_settable_ctx_md_params(void *vprsactx)
1430
0
{
1431
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1432
1433
0
    if (prsactx->md == NULL)
1434
0
        return 0;
1435
1436
0
    return EVP_MD_settable_ctx_params(prsactx->md);
1437
0
}
1438
1439
const OSSL_DISPATCH ossl_rsa_signature_functions[] = {
1440
    { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))rsa_newctx },
1441
    { OSSL_FUNC_SIGNATURE_SIGN_INIT, (void (*)(void))rsa_sign_init },
1442
    { OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))rsa_sign },
1443
    { OSSL_FUNC_SIGNATURE_VERIFY_INIT, (void (*)(void))rsa_verify_init },
1444
    { OSSL_FUNC_SIGNATURE_VERIFY, (void (*)(void))rsa_verify },
1445
    { OSSL_FUNC_SIGNATURE_VERIFY_RECOVER_INIT,
1446
      (void (*)(void))rsa_verify_recover_init },
1447
    { OSSL_FUNC_SIGNATURE_VERIFY_RECOVER,
1448
      (void (*)(void))rsa_verify_recover },
1449
    { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT,
1450
      (void (*)(void))rsa_digest_sign_init },
1451
    { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE,
1452
      (void (*)(void))rsa_digest_signverify_update },
1453
    { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL,
1454
      (void (*)(void))rsa_digest_sign_final },
1455
    { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT,
1456
      (void (*)(void))rsa_digest_verify_init },
1457
    { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE,
1458
      (void (*)(void))rsa_digest_signverify_update },
1459
    { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL,
1460
      (void (*)(void))rsa_digest_verify_final },
1461
    { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))rsa_freectx },
1462
    { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))rsa_dupctx },
1463
    { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, (void (*)(void))rsa_get_ctx_params },
1464
    { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS,
1465
      (void (*)(void))rsa_gettable_ctx_params },
1466
    { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, (void (*)(void))rsa_set_ctx_params },
1467
    { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS,
1468
      (void (*)(void))rsa_settable_ctx_params },
1469
    { OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS,
1470
      (void (*)(void))rsa_get_ctx_md_params },
1471
    { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS,
1472
      (void (*)(void))rsa_gettable_ctx_md_params },
1473
    { OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS,
1474
      (void (*)(void))rsa_set_ctx_md_params },
1475
    { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS,
1476
      (void (*)(void))rsa_settable_ctx_md_params },
1477
    { 0, NULL }
1478
};