Coverage Report

Created: 2026-02-14 07:20

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