Coverage Report

Created: 2025-11-16 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl35/providers/implementations/asymciphers/rsa_enc.c
Line
Count
Source
1
/*
2
 * Copyright 2019-2025 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
/*
11
 * RSA low level APIs are deprecated for public use, but still ok for
12
 * internal use.
13
 */
14
#include "internal/deprecated.h"
15
16
#include <openssl/crypto.h>
17
#include <openssl/evp.h>
18
#include <openssl/core_dispatch.h>
19
#include <openssl/core_names.h>
20
#include <openssl/rsa.h>
21
#include <openssl/params.h>
22
#include <openssl/err.h>
23
#include <openssl/proverr.h>
24
/* Just for SSL_MAX_MASTER_KEY_LENGTH */
25
#include <openssl/prov_ssl.h>
26
#include "internal/constant_time.h"
27
#include "internal/sizes.h"
28
#include "crypto/rsa.h"
29
#include "prov/provider_ctx.h"
30
#include "prov/implementations.h"
31
#include "prov/providercommon.h"
32
#include "prov/securitycheck.h"
33
#include <stdlib.h>
34
35
static OSSL_FUNC_asym_cipher_newctx_fn rsa_newctx;
36
static OSSL_FUNC_asym_cipher_encrypt_init_fn rsa_encrypt_init;
37
static OSSL_FUNC_asym_cipher_encrypt_fn rsa_encrypt;
38
static OSSL_FUNC_asym_cipher_decrypt_init_fn rsa_decrypt_init;
39
static OSSL_FUNC_asym_cipher_decrypt_fn rsa_decrypt;
40
static OSSL_FUNC_asym_cipher_freectx_fn rsa_freectx;
41
static OSSL_FUNC_asym_cipher_dupctx_fn rsa_dupctx;
42
static OSSL_FUNC_asym_cipher_get_ctx_params_fn rsa_get_ctx_params;
43
static OSSL_FUNC_asym_cipher_gettable_ctx_params_fn rsa_gettable_ctx_params;
44
static OSSL_FUNC_asym_cipher_set_ctx_params_fn rsa_set_ctx_params;
45
static OSSL_FUNC_asym_cipher_settable_ctx_params_fn rsa_settable_ctx_params;
46
47
static OSSL_ITEM padding_item[] = {
48
    { RSA_PKCS1_PADDING,        OSSL_PKEY_RSA_PAD_MODE_PKCSV15 },
49
    { RSA_NO_PADDING,           OSSL_PKEY_RSA_PAD_MODE_NONE },
50
    { RSA_PKCS1_OAEP_PADDING,   OSSL_PKEY_RSA_PAD_MODE_OAEP }, /* Correct spelling first */
51
    { RSA_PKCS1_OAEP_PADDING,   "oeap"   },
52
    { 0,                        NULL     }
53
};
54
55
/*
56
 * What's passed as an actual key is defined by the KEYMGMT interface.
57
 * We happen to know that our KEYMGMT simply passes RSA structures, so
58
 * we use that here too.
59
 */
60
61
typedef struct {
62
    OSSL_LIB_CTX *libctx;
63
    RSA *rsa;
64
    int pad_mode;
65
    int operation;
66
    /* OAEP message digest */
67
    EVP_MD *oaep_md;
68
    /* message digest for MGF1 */
69
    EVP_MD *mgf1_md;
70
    /* OAEP label */
71
    unsigned char *oaep_label;
72
    size_t oaep_labellen;
73
    /* TLS padding */
74
    unsigned int client_version;
75
    unsigned int alt_version;
76
    /* PKCS#1 v1.5 decryption mode */
77
    unsigned int implicit_rejection;
78
    OSSL_FIPS_IND_DECLARE
79
} PROV_RSA_CTX;
80
81
static void *rsa_newctx(void *provctx)
82
15.3k
{
83
15.3k
    PROV_RSA_CTX *prsactx;
84
85
15.3k
    if (!ossl_prov_is_running())
86
0
        return NULL;
87
15.3k
    prsactx = OPENSSL_zalloc(sizeof(PROV_RSA_CTX));
88
15.3k
    if (prsactx == NULL)
89
0
        return NULL;
90
15.3k
    prsactx->libctx = PROV_LIBCTX_OF(provctx);
91
15.3k
    OSSL_FIPS_IND_INIT(prsactx)
92
93
15.3k
    return prsactx;
94
15.3k
}
95
96
static int rsa_init(void *vprsactx, void *vrsa, const OSSL_PARAM params[],
97
                    int operation, const char *desc)
98
9.42k
{
99
9.42k
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
100
9.42k
    int protect = 0;
101
102
9.42k
    if (!ossl_prov_is_running() || prsactx == NULL || vrsa == NULL)
103
0
        return 0;
104
105
9.42k
    if (!ossl_rsa_key_op_get_protect(vrsa, operation, &protect))
106
0
        return 0;
107
9.42k
    if (!RSA_up_ref(vrsa))
108
0
        return 0;
109
9.42k
    RSA_free(prsactx->rsa);
110
9.42k
    prsactx->rsa = vrsa;
111
9.42k
    prsactx->operation = operation;
112
9.42k
    prsactx->implicit_rejection = 1;
113
114
9.42k
    switch (RSA_test_flags(prsactx->rsa, RSA_FLAG_TYPE_MASK)) {
115
9.42k
    case RSA_FLAG_TYPE_RSA:
116
9.42k
        prsactx->pad_mode = RSA_PKCS1_PADDING;
117
9.42k
        break;
118
0
    default:
119
        /* This should not happen due to the check above */
120
0
        ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
121
0
        return 0;
122
9.42k
    }
123
124
9.42k
    OSSL_FIPS_IND_SET_APPROVED(prsactx)
125
9.42k
    if (!rsa_set_ctx_params(prsactx, params))
126
0
        return 0;
127
#ifdef FIPS_MODULE
128
    if (!ossl_fips_ind_rsa_key_check(OSSL_FIPS_IND_GET(prsactx),
129
                                     OSSL_FIPS_IND_SETTABLE0, prsactx->libctx,
130
                                     prsactx->rsa, desc, protect))
131
        return 0;
132
#endif
133
9.42k
    return 1;
134
9.42k
}
135
136
static int rsa_encrypt_init(void *vprsactx, void *vrsa,
137
                            const OSSL_PARAM params[])
138
5.41k
{
139
5.41k
    return rsa_init(vprsactx, vrsa, params, EVP_PKEY_OP_ENCRYPT,
140
5.41k
                    "RSA Encrypt Init");
141
5.41k
}
142
143
static int rsa_decrypt_init(void *vprsactx, void *vrsa,
144
                            const OSSL_PARAM params[])
145
9.90k
{
146
9.90k
    return rsa_init(vprsactx, vrsa, params, EVP_PKEY_OP_DECRYPT,
147
9.90k
                    "RSA Decrypt Init");
148
9.90k
}
149
150
static int rsa_encrypt(void *vprsactx, unsigned char *out, size_t *outlen,
151
                       size_t outsize, const unsigned char *in, size_t inlen)
152
10.8k
{
153
10.8k
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
154
10.8k
    size_t len = RSA_size(prsactx->rsa);
155
10.8k
    int ret;
156
157
10.8k
    if (!ossl_prov_is_running())
158
0
        return 0;
159
160
#ifdef FIPS_MODULE
161
    if ((prsactx->pad_mode == RSA_PKCS1_PADDING
162
         || prsactx->pad_mode == RSA_PKCS1_WITH_TLS_PADDING)
163
        && !OSSL_FIPS_IND_ON_UNAPPROVED(prsactx, OSSL_FIPS_IND_SETTABLE1,
164
                                        prsactx->libctx, "RSA Encrypt",
165
                                        "PKCS#1 v1.5 padding",
166
                                        ossl_fips_config_rsa_pkcs15_padding_disabled)) {
167
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_PADDING_MODE);
168
        return 0;
169
    }
170
#endif
171
172
10.8k
    if (len == 0) {
173
8
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
174
8
        return 0;
175
8
    }
176
177
10.8k
    if (out == NULL) {
178
5.40k
        *outlen = len;
179
5.40k
        return 1;
180
5.40k
    }
181
182
5.40k
    if (outsize < len) {
183
0
        ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
184
0
        return 0;
185
0
    }
186
187
5.40k
    if (prsactx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
188
0
        int rsasize = RSA_size(prsactx->rsa);
189
0
        unsigned char *tbuf;
190
191
0
        if ((tbuf = OPENSSL_malloc(rsasize)) == NULL)
192
0
            return 0;
193
0
        if (prsactx->oaep_md == NULL) {
194
0
            prsactx->oaep_md = EVP_MD_fetch(prsactx->libctx, "SHA-1", NULL);
195
0
            if (prsactx->oaep_md == NULL) {
196
0
                OPENSSL_free(tbuf);
197
0
                ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
198
0
                return 0;
199
0
            }
200
0
        }
201
0
        ret =
202
0
            ossl_rsa_padding_add_PKCS1_OAEP_mgf1_ex(prsactx->libctx, tbuf,
203
0
                                                    rsasize, in, inlen,
204
0
                                                    prsactx->oaep_label,
205
0
                                                    prsactx->oaep_labellen,
206
0
                                                    prsactx->oaep_md,
207
0
                                                    prsactx->mgf1_md);
208
209
0
        if (!ret) {
210
0
            OPENSSL_free(tbuf);
211
0
            return 0;
212
0
        }
213
0
        ret = RSA_public_encrypt(rsasize, tbuf, out, prsactx->rsa,
214
0
                                 RSA_NO_PADDING);
215
0
        OPENSSL_free(tbuf);
216
5.40k
    } else {
217
5.40k
        ret = RSA_public_encrypt(inlen, in, out, prsactx->rsa,
218
5.40k
                                 prsactx->pad_mode);
219
5.40k
    }
220
    /* A ret value of 0 is not an error */
221
5.40k
    if (ret < 0)
222
705
        return ret;
223
4.69k
    *outlen = ret;
224
4.69k
    return 1;
225
5.40k
}
226
227
static int rsa_decrypt(void *vprsactx, unsigned char *out, size_t *outlen,
228
                       size_t outsize, const unsigned char *in, size_t inlen)
229
9.32k
{
230
9.32k
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
231
9.32k
    int ret;
232
9.32k
    int pad_mode;
233
9.32k
    size_t len = RSA_size(prsactx->rsa);
234
235
9.32k
    if (!ossl_prov_is_running())
236
0
        return 0;
237
238
9.32k
    if (prsactx->pad_mode == RSA_PKCS1_WITH_TLS_PADDING) {
239
9.32k
        if (out == NULL) {
240
0
            *outlen = SSL_MAX_MASTER_KEY_LENGTH;
241
0
            return 1;
242
0
        }
243
9.32k
        if (outsize < SSL_MAX_MASTER_KEY_LENGTH) {
244
0
            ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH);
245
0
            return 0;
246
0
        }
247
9.32k
    } else {
248
0
        if (out == NULL) {
249
0
            if (len == 0) {
250
0
                ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
251
0
                return 0;
252
0
            }
253
0
            *outlen = len;
254
0
            return 1;
255
0
        }
256
257
0
        if (outsize < len) {
258
0
            ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH);
259
0
            return 0;
260
0
        }
261
0
    }
262
263
9.32k
    if (prsactx->pad_mode == RSA_PKCS1_OAEP_PADDING
264
9.32k
            || prsactx->pad_mode == RSA_PKCS1_WITH_TLS_PADDING) {
265
9.32k
        unsigned char *tbuf;
266
267
9.32k
        if ((tbuf = OPENSSL_malloc(len)) == NULL)
268
0
            return 0;
269
9.32k
        ret = RSA_private_decrypt(inlen, in, tbuf, prsactx->rsa,
270
9.32k
                                  RSA_NO_PADDING);
271
        /*
272
         * With no padding then, on success ret should be len, otherwise an
273
         * error occurred (non-constant time)
274
         */
275
9.32k
        if (ret != (int)len) {
276
25
            OPENSSL_free(tbuf);
277
25
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_DECRYPT);
278
25
            return 0;
279
25
        }
280
9.30k
        if (prsactx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
281
0
            if (prsactx->oaep_md == NULL) {
282
0
                prsactx->oaep_md = EVP_MD_fetch(prsactx->libctx, "SHA-1", NULL);
283
0
                if (prsactx->oaep_md == NULL) {
284
0
                    OPENSSL_free(tbuf);
285
0
                    ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
286
0
                    return 0;
287
0
                }
288
0
            }
289
0
            ret = RSA_padding_check_PKCS1_OAEP_mgf1(out, outsize, tbuf,
290
0
                                                    len, len,
291
0
                                                    prsactx->oaep_label,
292
0
                                                    prsactx->oaep_labellen,
293
0
                                                    prsactx->oaep_md,
294
0
                                                    prsactx->mgf1_md);
295
9.30k
        } else {
296
            /* RSA_PKCS1_WITH_TLS_PADDING */
297
9.30k
            if (prsactx->client_version <= 0) {
298
23
                ERR_raise(ERR_LIB_PROV, PROV_R_BAD_TLS_CLIENT_VERSION);
299
23
                OPENSSL_free(tbuf);
300
23
                return 0;
301
23
            }
302
9.28k
            ret = ossl_rsa_padding_check_PKCS1_type_2_TLS(
303
9.28k
                        prsactx->libctx, out, outsize, tbuf, len,
304
9.28k
                        prsactx->client_version, prsactx->alt_version);
305
9.28k
        }
306
9.28k
        OPENSSL_free(tbuf);
307
9.28k
    } else {
308
0
        if ((prsactx->implicit_rejection == 0) &&
309
0
                (prsactx->pad_mode == RSA_PKCS1_PADDING))
310
0
            pad_mode = RSA_PKCS1_NO_IMPLICIT_REJECT_PADDING;
311
0
        else
312
0
            pad_mode = prsactx->pad_mode;
313
0
        ret = RSA_private_decrypt(inlen, in, out, prsactx->rsa, pad_mode);
314
0
    }
315
9.28k
    *outlen = constant_time_select_s(constant_time_msb_s(ret), *outlen, ret);
316
9.28k
    ret = constant_time_select_int(constant_time_msb(ret), 0, 1);
317
9.28k
    return ret;
318
9.32k
}
319
320
static void rsa_freectx(void *vprsactx)
321
15.3k
{
322
15.3k
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
323
324
15.3k
    RSA_free(prsactx->rsa);
325
326
15.3k
    EVP_MD_free(prsactx->oaep_md);
327
15.3k
    EVP_MD_free(prsactx->mgf1_md);
328
15.3k
    OPENSSL_free(prsactx->oaep_label);
329
330
15.3k
    OPENSSL_free(prsactx);
331
15.3k
}
332
333
static void *rsa_dupctx(void *vprsactx)
334
0
{
335
0
    PROV_RSA_CTX *srcctx = (PROV_RSA_CTX *)vprsactx;
336
0
    PROV_RSA_CTX *dstctx;
337
338
0
    if (!ossl_prov_is_running())
339
0
        return NULL;
340
341
0
    dstctx = OPENSSL_zalloc(sizeof(*srcctx));
342
0
    if (dstctx == NULL)
343
0
        return NULL;
344
345
0
    *dstctx = *srcctx;
346
0
    if (dstctx->rsa != NULL && !RSA_up_ref(dstctx->rsa)) {
347
0
        OPENSSL_free(dstctx);
348
0
        return NULL;
349
0
    }
350
351
0
    if (dstctx->oaep_md != NULL && !EVP_MD_up_ref(dstctx->oaep_md)) {
352
0
        RSA_free(dstctx->rsa);
353
0
        OPENSSL_free(dstctx);
354
0
        return NULL;
355
0
    }
356
357
0
    if (dstctx->mgf1_md != NULL && !EVP_MD_up_ref(dstctx->mgf1_md)) {
358
0
        RSA_free(dstctx->rsa);
359
0
        EVP_MD_free(dstctx->oaep_md);
360
0
        OPENSSL_free(dstctx);
361
0
        return NULL;
362
0
    }
363
364
0
    return dstctx;
365
0
}
366
367
static int rsa_get_ctx_params(void *vprsactx, OSSL_PARAM *params)
368
0
{
369
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
370
0
    OSSL_PARAM *p;
371
372
0
    if (prsactx == NULL)
373
0
        return 0;
374
375
0
    p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_PAD_MODE);
376
0
    if (p != NULL)
377
0
        switch (p->data_type) {
378
0
        case OSSL_PARAM_INTEGER: /* Support for legacy pad mode number */
379
0
            if (!OSSL_PARAM_set_int(p, prsactx->pad_mode))
380
0
                return 0;
381
0
            break;
382
0
        case OSSL_PARAM_UTF8_STRING:
383
0
            {
384
0
                int i;
385
0
                const char *word = NULL;
386
387
0
                for (i = 0; padding_item[i].id != 0; i++) {
388
0
                    if (prsactx->pad_mode == (int)padding_item[i].id) {
389
0
                        word = padding_item[i].ptr;
390
0
                        break;
391
0
                    }
392
0
                }
393
394
0
                if (word != NULL) {
395
0
                    if (!OSSL_PARAM_set_utf8_string(p, word))
396
0
                        return 0;
397
0
                } else {
398
0
                    ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
399
0
                }
400
0
            }
401
0
            break;
402
0
        default:
403
0
            return 0;
404
0
        }
405
406
0
    p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST);
407
0
    if (p != NULL && !OSSL_PARAM_set_utf8_string(p, prsactx->oaep_md == NULL
408
0
                                                    ? ""
409
0
                                                    : EVP_MD_get0_name(prsactx->oaep_md)))
410
0
        return 0;
411
412
0
    p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST);
413
0
    if (p != NULL) {
414
0
        EVP_MD *mgf1_md = prsactx->mgf1_md == NULL ? prsactx->oaep_md
415
0
                                                   : prsactx->mgf1_md;
416
417
0
        if (!OSSL_PARAM_set_utf8_string(p, mgf1_md == NULL
418
0
                                           ? ""
419
0
                                           : EVP_MD_get0_name(mgf1_md)))
420
0
        return 0;
421
0
    }
422
423
0
    p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL);
424
0
    if (p != NULL &&
425
0
        !OSSL_PARAM_set_octet_ptr(p, prsactx->oaep_label,
426
0
                                  prsactx->oaep_labellen))
427
0
        return 0;
428
429
0
    p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION);
430
0
    if (p != NULL && !OSSL_PARAM_set_uint(p, prsactx->client_version))
431
0
        return 0;
432
433
0
    p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION);
434
0
    if (p != NULL && !OSSL_PARAM_set_uint(p, prsactx->alt_version))
435
0
        return 0;
436
437
0
    p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_IMPLICIT_REJECTION);
438
0
    if (p != NULL && !OSSL_PARAM_set_uint(p, prsactx->implicit_rejection))
439
0
        return 0;
440
0
    if (!OSSL_FIPS_IND_GET_CTX_PARAM(prsactx, params))
441
0
        return 0;
442
0
    return 1;
443
0
}
444
445
static const OSSL_PARAM known_gettable_ctx_params[] = {
446
    OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, NULL, 0),
447
    OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_PAD_MODE, NULL, 0),
448
    OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST, NULL, 0),
449
    OSSL_PARAM_DEFN(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL, OSSL_PARAM_OCTET_PTR,
450
                    NULL, 0),
451
    OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION, NULL),
452
    OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION, NULL),
453
    OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_IMPLICIT_REJECTION, NULL),
454
    OSSL_FIPS_IND_GETTABLE_CTX_PARAM()
455
    OSSL_PARAM_END
456
};
457
458
static const OSSL_PARAM *rsa_gettable_ctx_params(ossl_unused void *vprsactx,
459
                                                 ossl_unused void *provctx)
460
0
{
461
0
    return known_gettable_ctx_params;
462
0
}
463
464
static int rsa_set_ctx_params(void *vprsactx, const OSSL_PARAM params[])
465
8.72k
{
466
8.72k
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
467
8.72k
    const OSSL_PARAM *p;
468
8.72k
    char mdname[OSSL_MAX_NAME_SIZE];
469
8.72k
    char mdprops[OSSL_MAX_PROPQUERY_SIZE] = { '\0' };
470
8.72k
    char *str = NULL;
471
472
8.72k
    if (prsactx == NULL)
473
0
        return 0;
474
8.72k
    if (ossl_param_is_empty(params))
475
4.52k
        return 1;
476
477
4.19k
    if (!OSSL_FIPS_IND_SET_CTX_PARAM(prsactx, OSSL_FIPS_IND_SETTABLE0, params,
478
4.19k
                                     OSSL_ASYM_CIPHER_PARAM_FIPS_KEY_CHECK))
479
0
        return 0;
480
4.19k
    if (!OSSL_FIPS_IND_SET_CTX_PARAM(prsactx, OSSL_FIPS_IND_SETTABLE1, params,
481
4.19k
                                     OSSL_ASYM_CIPHER_PARAM_FIPS_RSA_PKCS15_PAD_DISABLED))
482
0
        return 0;
483
484
4.19k
    p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST);
485
4.19k
    if (p != NULL) {
486
0
        str = mdname;
487
0
        if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdname)))
488
0
            return 0;
489
490
0
        p = OSSL_PARAM_locate_const(params,
491
0
                                    OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST_PROPS);
492
0
        if (p != NULL) {
493
0
            str = mdprops;
494
0
            if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdprops)))
495
0
                return 0;
496
0
        }
497
498
0
        EVP_MD_free(prsactx->oaep_md);
499
0
        prsactx->oaep_md = EVP_MD_fetch(prsactx->libctx, mdname, mdprops);
500
501
0
        if (prsactx->oaep_md == NULL)
502
0
            return 0;
503
0
    }
504
505
4.19k
    p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_PAD_MODE);
506
4.19k
    if (p != NULL) {
507
2.09k
        int pad_mode = 0;
508
509
2.09k
        switch (p->data_type) {
510
2.09k
        case OSSL_PARAM_INTEGER: /* Support for legacy pad mode number */
511
2.09k
            if (!OSSL_PARAM_get_int(p, &pad_mode))
512
0
                return 0;
513
2.09k
            break;
514
2.09k
        case OSSL_PARAM_UTF8_STRING:
515
0
            {
516
0
                int i;
517
518
0
                if (p->data == NULL)
519
0
                    return 0;
520
521
0
                for (i = 0; padding_item[i].id != 0; i++) {
522
0
                    if (strcmp(p->data, padding_item[i].ptr) == 0) {
523
0
                        pad_mode = padding_item[i].id;
524
0
                        break;
525
0
                    }
526
0
                }
527
0
            }
528
0
            break;
529
0
        default:
530
0
            return 0;
531
2.09k
        }
532
533
        /*
534
         * PSS padding is for signatures only so is not compatible with
535
         * asymmetric cipher use.
536
         */
537
2.09k
        if (pad_mode == RSA_PKCS1_PSS_PADDING)
538
0
            return 0;
539
2.09k
        if (pad_mode == RSA_PKCS1_OAEP_PADDING && prsactx->oaep_md == NULL) {
540
0
            prsactx->oaep_md = EVP_MD_fetch(prsactx->libctx, "SHA1", mdprops);
541
0
            if (prsactx->oaep_md == NULL)
542
0
                return 0;
543
0
        }
544
2.09k
        prsactx->pad_mode = pad_mode;
545
2.09k
    }
546
547
4.19k
    p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST);
548
4.19k
    if (p != NULL) {
549
0
        str = mdname;
550
0
        if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdname)))
551
0
            return 0;
552
553
0
        p = OSSL_PARAM_locate_const(params,
554
0
                                    OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST_PROPS);
555
0
        if (p != NULL) {
556
0
            str = mdprops;
557
0
            if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdprops)))
558
0
                return 0;
559
0
        } else {
560
0
            str = NULL;
561
0
        }
562
563
0
        EVP_MD_free(prsactx->mgf1_md);
564
0
        prsactx->mgf1_md = EVP_MD_fetch(prsactx->libctx, mdname, str);
565
566
0
        if (prsactx->mgf1_md == NULL)
567
0
            return 0;
568
0
    }
569
570
4.19k
    p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL);
571
4.19k
    if (p != NULL) {
572
0
        void *tmp_label = NULL;
573
0
        size_t tmp_labellen;
574
575
0
        if (!OSSL_PARAM_get_octet_string(p, &tmp_label, 0, &tmp_labellen))
576
0
            return 0;
577
0
        OPENSSL_free(prsactx->oaep_label);
578
0
        prsactx->oaep_label = (unsigned char *)tmp_label;
579
0
        prsactx->oaep_labellen = tmp_labellen;
580
0
    }
581
582
4.19k
    p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION);
583
4.19k
    if (p != NULL) {
584
2.09k
        unsigned int client_version;
585
586
2.09k
        if (!OSSL_PARAM_get_uint(p, &client_version))
587
0
            return 0;
588
2.09k
        prsactx->client_version = client_version;
589
2.09k
    }
590
591
4.19k
    p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION);
592
4.19k
    if (p != NULL) {
593
0
        unsigned int alt_version;
594
595
0
        if (!OSSL_PARAM_get_uint(p, &alt_version))
596
0
            return 0;
597
0
        prsactx->alt_version = alt_version;
598
0
    }
599
4.19k
    p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_IMPLICIT_REJECTION);
600
4.19k
    if (p != NULL) {
601
0
        unsigned int implicit_rejection;
602
603
0
        if (!OSSL_PARAM_get_uint(p, &implicit_rejection))
604
0
            return 0;
605
0
        prsactx->implicit_rejection = implicit_rejection;
606
0
    }
607
4.19k
    return 1;
608
4.19k
}
609
610
static const OSSL_PARAM known_settable_ctx_params[] = {
611
    OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, NULL, 0),
612
    OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST_PROPS, NULL, 0),
613
    OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_PAD_MODE, NULL, 0),
614
    OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST, NULL, 0),
615
    OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST_PROPS, NULL, 0),
616
    OSSL_PARAM_octet_string(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL, NULL, 0),
617
    OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION, NULL),
618
    OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION, NULL),
619
    OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_IMPLICIT_REJECTION, NULL),
620
    OSSL_FIPS_IND_SETTABLE_CTX_PARAM(OSSL_ASYM_CIPHER_PARAM_FIPS_KEY_CHECK)
621
    OSSL_FIPS_IND_SETTABLE_CTX_PARAM(OSSL_ASYM_CIPHER_PARAM_FIPS_RSA_PKCS15_PAD_DISABLED)
622
    OSSL_PARAM_END
623
};
624
625
static const OSSL_PARAM *rsa_settable_ctx_params(ossl_unused void *vprsactx,
626
                                                 ossl_unused void *provctx)
627
9.92k
{
628
9.92k
    return known_settable_ctx_params;
629
9.92k
}
630
631
const OSSL_DISPATCH ossl_rsa_asym_cipher_functions[] = {
632
    { OSSL_FUNC_ASYM_CIPHER_NEWCTX, (void (*)(void))rsa_newctx },
633
    { OSSL_FUNC_ASYM_CIPHER_ENCRYPT_INIT, (void (*)(void))rsa_encrypt_init },
634
    { OSSL_FUNC_ASYM_CIPHER_ENCRYPT, (void (*)(void))rsa_encrypt },
635
    { OSSL_FUNC_ASYM_CIPHER_DECRYPT_INIT, (void (*)(void))rsa_decrypt_init },
636
    { OSSL_FUNC_ASYM_CIPHER_DECRYPT, (void (*)(void))rsa_decrypt },
637
    { OSSL_FUNC_ASYM_CIPHER_FREECTX, (void (*)(void))rsa_freectx },
638
    { OSSL_FUNC_ASYM_CIPHER_DUPCTX, (void (*)(void))rsa_dupctx },
639
    { OSSL_FUNC_ASYM_CIPHER_GET_CTX_PARAMS,
640
      (void (*)(void))rsa_get_ctx_params },
641
    { OSSL_FUNC_ASYM_CIPHER_GETTABLE_CTX_PARAMS,
642
      (void (*)(void))rsa_gettable_ctx_params },
643
    { OSSL_FUNC_ASYM_CIPHER_SET_CTX_PARAMS,
644
      (void (*)(void))rsa_set_ctx_params },
645
    { OSSL_FUNC_ASYM_CIPHER_SETTABLE_CTX_PARAMS,
646
      (void (*)(void))rsa_settable_ctx_params },
647
    OSSL_DISPATCH_END
648
};