Coverage Report

Created: 2024-07-27 06:36

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