Coverage Report

Created: 2025-08-25 06:30

/src/openssl/providers/implementations/asymciphers/rsa_enc.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2019-2024 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
/*
12
 * RSA low level APIs are deprecated for public use, but still ok for
13
 * internal use.
14
 */
15
#include "internal/deprecated.h"
16
17
#include <openssl/crypto.h>
18
#include <openssl/evp.h>
19
#include <openssl/core_dispatch.h>
20
#include <openssl/core_names.h>
21
#include <openssl/rsa.h>
22
#include <openssl/params.h>
23
#include <openssl/err.h>
24
#include <openssl/proverr.h>
25
/* Just for SSL_MAX_MASTER_KEY_LENGTH */
26
#include <openssl/prov_ssl.h>
27
#include "internal/constant_time.h"
28
#include "internal/cryptlib.h"
29
#include "internal/sizes.h"
30
#include "crypto/rsa.h"
31
#include "prov/provider_ctx.h"
32
#include "prov/implementations.h"
33
#include "prov/providercommon.h"
34
#include "prov/securitycheck.h"
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
#ifdef FIPS_MODULE
162
    if ((prsactx->pad_mode == RSA_PKCS1_PADDING
163
         || prsactx->pad_mode == RSA_PKCS1_WITH_TLS_PADDING)
164
        && !OSSL_FIPS_IND_ON_UNAPPROVED(prsactx, OSSL_FIPS_IND_SETTABLE1,
165
                                        prsactx->libctx, "RSA Encrypt",
166
                                        "PKCS#1 v1.5 padding",
167
                                        ossl_fips_config_rsa_pkcs15_padding_disabled)) {
168
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_PADDING_MODE);
169
        return 0;
170
    }
171
#endif
172
173
0
    if (out == NULL) {
174
0
        size_t len = RSA_size(prsactx->rsa);
175
176
0
        if (len == 0) {
177
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
178
0
            return 0;
179
0
        }
180
0
        *outlen = len;
181
0
        return 1;
182
0
    }
183
184
0
    if (prsactx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
185
0
        int rsasize = RSA_size(prsactx->rsa);
186
0
        unsigned char *tbuf;
187
188
0
        if ((tbuf = OPENSSL_malloc(rsasize)) == NULL)
189
0
            return 0;
190
0
        if (prsactx->oaep_md == NULL) {
191
0
            prsactx->oaep_md = EVP_MD_fetch(prsactx->libctx, "SHA-1", NULL);
192
0
            if (prsactx->oaep_md == NULL) {
193
0
                OPENSSL_free(tbuf);
194
0
                ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
195
0
                return 0;
196
0
            }
197
0
        }
198
0
        ret =
199
0
            ossl_rsa_padding_add_PKCS1_OAEP_mgf1_ex(prsactx->libctx, tbuf,
200
0
                                                    rsasize, in, (int)inlen,
201
0
                                                    prsactx->oaep_label,
202
0
                                                    (int)prsactx->oaep_labellen,
203
0
                                                    prsactx->oaep_md,
204
0
                                                    prsactx->mgf1_md);
205
206
0
        if (!ret) {
207
0
            OPENSSL_free(tbuf);
208
0
            return 0;
209
0
        }
210
0
        ret = RSA_public_encrypt(rsasize, tbuf, out, prsactx->rsa,
211
0
                                 RSA_NO_PADDING);
212
0
        OPENSSL_free(tbuf);
213
0
    } else {
214
0
        ret = RSA_public_encrypt((int)inlen, in, out, prsactx->rsa,
215
0
                                 prsactx->pad_mode);
216
0
    }
217
    /* A ret value of 0 is not an error */
218
0
    if (ret < 0)
219
0
        return ret;
220
0
    *outlen = ret;
221
0
    return 1;
222
0
}
223
224
static int rsa_decrypt(void *vprsactx, unsigned char *out, size_t *outlen,
225
                       size_t outsize, const unsigned char *in, size_t inlen)
226
0
{
227
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
228
0
    int ret;
229
0
    int pad_mode;
230
0
    size_t len = RSA_size(prsactx->rsa);
231
232
0
    if (!ossl_prov_is_running())
233
0
        return 0;
234
235
0
    if (prsactx->pad_mode == RSA_PKCS1_WITH_TLS_PADDING) {
236
0
        if (out == NULL) {
237
0
            *outlen = SSL_MAX_MASTER_KEY_LENGTH;
238
0
            return 1;
239
0
        }
240
0
        if (outsize < SSL_MAX_MASTER_KEY_LENGTH) {
241
0
            ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH);
242
0
            return 0;
243
0
        }
244
0
    } else {
245
0
        if (out == NULL) {
246
0
            if (len == 0) {
247
0
                ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
248
0
                return 0;
249
0
            }
250
0
            *outlen = len;
251
0
            return 1;
252
0
        }
253
254
0
        if (outsize < len) {
255
0
            ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH);
256
0
            return 0;
257
0
        }
258
0
    }
259
260
0
    if (prsactx->pad_mode == RSA_PKCS1_OAEP_PADDING
261
0
            || prsactx->pad_mode == RSA_PKCS1_WITH_TLS_PADDING) {
262
0
        unsigned char *tbuf;
263
264
0
        if ((tbuf = OPENSSL_malloc(len)) == NULL)
265
0
            return 0;
266
0
        ret = RSA_private_decrypt((int)inlen, in, tbuf, prsactx->rsa,
267
0
                                  RSA_NO_PADDING);
268
        /*
269
         * With no padding then, on success ret should be len, otherwise an
270
         * error occurred (non-constant time)
271
         */
272
0
        if (ret != (int)len) {
273
0
            OPENSSL_free(tbuf);
274
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_DECRYPT);
275
0
            return 0;
276
0
        }
277
0
        if (prsactx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
278
0
            if (prsactx->oaep_md == NULL) {
279
0
                prsactx->oaep_md = EVP_MD_fetch(prsactx->libctx, "SHA-1", NULL);
280
0
                if (prsactx->oaep_md == NULL) {
281
0
                    OPENSSL_free(tbuf);
282
0
                    ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
283
0
                    return 0;
284
0
                }
285
0
            }
286
0
            ret = RSA_padding_check_PKCS1_OAEP_mgf1(out, (int)outsize, tbuf,
287
0
                                                    (int)len, (int)len,
288
0
                                                    prsactx->oaep_label,
289
0
                                                    (int)prsactx->oaep_labellen,
290
0
                                                    prsactx->oaep_md,
291
0
                                                    prsactx->mgf1_md);
292
0
        } else {
293
            /* RSA_PKCS1_WITH_TLS_PADDING */
294
0
            if (prsactx->client_version <= 0) {
295
0
                ERR_raise(ERR_LIB_PROV, PROV_R_BAD_TLS_CLIENT_VERSION);
296
0
                OPENSSL_free(tbuf);
297
0
                return 0;
298
0
            }
299
0
            ret = ossl_rsa_padding_check_PKCS1_type_2_TLS(
300
0
                        prsactx->libctx, out, outsize, tbuf, len,
301
0
                        prsactx->client_version, prsactx->alt_version);
302
0
        }
303
0
        OPENSSL_free(tbuf);
304
0
    } else {
305
0
        if ((prsactx->implicit_rejection == 0) &&
306
0
                (prsactx->pad_mode == RSA_PKCS1_PADDING))
307
0
            pad_mode = RSA_PKCS1_NO_IMPLICIT_REJECT_PADDING;
308
0
        else
309
0
            pad_mode = prsactx->pad_mode;
310
0
        ret = RSA_private_decrypt((int)inlen, in, out, prsactx->rsa, pad_mode);
311
0
    }
312
0
    *outlen = constant_time_select_s(constant_time_msb_s(ret), *outlen, ret);
313
0
    ret = constant_time_select_int(constant_time_msb(ret), 0, 1);
314
0
    return ret;
315
0
}
316
317
static void rsa_freectx(void *vprsactx)
318
0
{
319
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
320
321
0
    RSA_free(prsactx->rsa);
322
323
0
    EVP_MD_free(prsactx->oaep_md);
324
0
    EVP_MD_free(prsactx->mgf1_md);
325
0
    OPENSSL_free(prsactx->oaep_label);
326
327
0
    OPENSSL_free(prsactx);
328
0
}
329
330
static void *rsa_dupctx(void *vprsactx)
331
0
{
332
0
    PROV_RSA_CTX *srcctx = (PROV_RSA_CTX *)vprsactx;
333
0
    PROV_RSA_CTX *dstctx;
334
335
0
    if (!ossl_prov_is_running())
336
0
        return NULL;
337
338
0
    dstctx = OPENSSL_zalloc(sizeof(*srcctx));
339
0
    if (dstctx == NULL)
340
0
        return NULL;
341
342
0
    *dstctx = *srcctx;
343
0
    if (dstctx->rsa != NULL && !RSA_up_ref(dstctx->rsa)) {
344
0
        OPENSSL_free(dstctx);
345
0
        return NULL;
346
0
    }
347
348
0
    if (dstctx->oaep_md != NULL && !EVP_MD_up_ref(dstctx->oaep_md)) {
349
0
        RSA_free(dstctx->rsa);
350
0
        OPENSSL_free(dstctx);
351
0
        return NULL;
352
0
    }
353
354
0
    if (dstctx->mgf1_md != NULL && !EVP_MD_up_ref(dstctx->mgf1_md)) {
355
0
        RSA_free(dstctx->rsa);
356
0
        EVP_MD_free(dstctx->oaep_md);
357
0
        OPENSSL_free(dstctx);
358
0
        return NULL;
359
0
    }
360
361
0
    return dstctx;
362
0
}
363
364
/* Machine generated by util/perl/OpenSSL/paramnames.pm */
365
#ifndef rsa_get_ctx_params_list
366
static const OSSL_PARAM rsa_get_ctx_params_list[] = {
367
    OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, NULL, 0),
368
    OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_PAD_MODE, NULL, 0),
369
    OSSL_PARAM_int(OSSL_ASYM_CIPHER_PARAM_PAD_MODE, NULL),
370
    OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST, NULL, 0),
371
    OSSL_PARAM_octet_ptr(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL, NULL, 0),
372
    OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION, NULL),
373
    OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION, NULL),
374
    OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_IMPLICIT_REJECTION, NULL),
375
# if defined(FIPS_MODULE)
376
    OSSL_PARAM_int(OSSL_ASYM_CIPHER_PARAM_FIPS_APPROVED_INDICATOR, NULL),
377
# endif
378
    OSSL_PARAM_END
379
};
380
#endif
381
382
#ifndef rsa_get_ctx_params_st
383
struct rsa_get_ctx_params_st {
384
    OSSL_PARAM *imrej;
385
# if defined(FIPS_MODULE)
386
    OSSL_PARAM *ind;
387
# endif
388
    OSSL_PARAM *label;
389
    OSSL_PARAM *mgf1;
390
    OSSL_PARAM *negver;
391
    OSSL_PARAM *oaep;
392
    OSSL_PARAM *pad;
393
    OSSL_PARAM *tlsver;
394
};
395
#endif
396
397
#ifndef rsa_get_ctx_params_decoder
398
static int rsa_get_ctx_params_decoder
399
    (const OSSL_PARAM *p, struct rsa_get_ctx_params_st *r)
400
0
{
401
0
    const char *s;
402
403
0
    memset(r, 0, sizeof(*r));
404
0
    if (p != NULL)
405
0
        for (; (s = p->key) != NULL; p++)
406
0
            switch(s[0]) {
407
0
            default:
408
0
                break;
409
0
            case 'd':
410
0
                if (ossl_likely(strcmp("igest", s + 1) == 0)) {
411
                    /* ASYM_CIPHER_PARAM_OAEP_DIGEST */
412
0
                    if (ossl_unlikely(r->oaep != NULL)) {
413
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
414
0
                                       "param %s is repeated", s);
415
0
                        return 0;
416
0
                    }
417
0
                    r->oaep = (OSSL_PARAM *)p;
418
0
                }
419
0
                break;
420
0
            case 'f':
421
# if defined(FIPS_MODULE)
422
                if (ossl_likely(strcmp("ips-indicator", s + 1) == 0)) {
423
                    /* ASYM_CIPHER_PARAM_FIPS_APPROVED_INDICATOR */
424
                    if (ossl_unlikely(r->ind != NULL)) {
425
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
426
                                       "param %s is repeated", s);
427
                        return 0;
428
                    }
429
                    r->ind = (OSSL_PARAM *)p;
430
                }
431
# endif
432
0
                break;
433
0
            case 'i':
434
0
                if (ossl_likely(strcmp("mplicit-rejection", s + 1) == 0)) {
435
                    /* ASYM_CIPHER_PARAM_IMPLICIT_REJECTION */
436
0
                    if (ossl_unlikely(r->imrej != NULL)) {
437
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
438
0
                                       "param %s is repeated", s);
439
0
                        return 0;
440
0
                    }
441
0
                    r->imrej = (OSSL_PARAM *)p;
442
0
                }
443
0
                break;
444
0
            case 'm':
445
0
                if (ossl_likely(strcmp("gf1-digest", s + 1) == 0)) {
446
                    /* ASYM_CIPHER_PARAM_MGF1_DIGEST */
447
0
                    if (ossl_unlikely(r->mgf1 != NULL)) {
448
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
449
0
                                       "param %s is repeated", s);
450
0
                        return 0;
451
0
                    }
452
0
                    r->mgf1 = (OSSL_PARAM *)p;
453
0
                }
454
0
                break;
455
0
            case 'o':
456
0
                if (ossl_likely(strcmp("aep-label", s + 1) == 0)) {
457
                    /* ASYM_CIPHER_PARAM_OAEP_LABEL */
458
0
                    if (ossl_unlikely(r->label != NULL)) {
459
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
460
0
                                       "param %s is repeated", s);
461
0
                        return 0;
462
0
                    }
463
0
                    r->label = (OSSL_PARAM *)p;
464
0
                }
465
0
                break;
466
0
            case 'p':
467
0
                if (ossl_likely(strcmp("ad-mode", s + 1) == 0)) {
468
                    /* ASYM_CIPHER_PARAM_PAD_MODE */
469
0
                    if (ossl_unlikely(r->pad != NULL)) {
470
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
471
0
                                       "param %s is repeated", s);
472
0
                        return 0;
473
0
                    }
474
0
                    r->pad = (OSSL_PARAM *)p;
475
0
                }
476
0
                break;
477
0
            case 't':
478
0
                switch(s[1]) {
479
0
                default:
480
0
                    break;
481
0
                case 'l':
482
0
                    switch(s[2]) {
483
0
                    default:
484
0
                        break;
485
0
                    case 's':
486
0
                        switch(s[3]) {
487
0
                        default:
488
0
                            break;
489
0
                        case '-':
490
0
                            switch(s[4]) {
491
0
                            default:
492
0
                                break;
493
0
                            case 'c':
494
0
                                if (ossl_likely(strcmp("lient-version", s + 5) == 0)) {
495
                                    /* ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION */
496
0
                                    if (ossl_unlikely(r->tlsver != NULL)) {
497
0
                                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
498
0
                                                       "param %s is repeated", s);
499
0
                                        return 0;
500
0
                                    }
501
0
                                    r->tlsver = (OSSL_PARAM *)p;
502
0
                                }
503
0
                                break;
504
0
                            case 'n':
505
0
                                if (ossl_likely(strcmp("egotiated-version", s + 5) == 0)) {
506
                                    /* ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION */
507
0
                                    if (ossl_unlikely(r->negver != NULL)) {
508
0
                                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
509
0
                                                       "param %s is repeated", s);
510
0
                                        return 0;
511
0
                                    }
512
0
                                    r->negver = (OSSL_PARAM *)p;
513
0
                                }
514
0
                            }
515
0
                        }
516
0
                    }
517
0
                }
518
0
            }
519
0
    return 1;
520
0
}
521
#endif
522
/* End of machine generated */
523
524
static int rsa_get_ctx_params(void *vprsactx, OSSL_PARAM *params)
525
0
{
526
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
527
0
    struct rsa_get_ctx_params_st p;
528
529
0
    if (prsactx == NULL || !rsa_get_ctx_params_decoder(params, &p))
530
0
        return 0;
531
532
0
    if (p.pad != NULL) {
533
0
        if (p.pad->data_type != OSSL_PARAM_UTF8_STRING) {
534
            /* Support for legacy pad mode number */
535
0
            if (!OSSL_PARAM_set_int(p.pad, prsactx->pad_mode))
536
0
                return 0;
537
0
        } else {
538
0
            int i;
539
0
            const char *word = NULL;
540
541
0
            for (i = 0; padding_item[i].id != 0; i++) {
542
0
                if (prsactx->pad_mode == (int)padding_item[i].id) {
543
0
                    word = padding_item[i].ptr;
544
0
                    break;
545
0
                }
546
0
            }
547
548
0
            if (word != NULL) {
549
0
                if (!OSSL_PARAM_set_utf8_string(p.pad, word))
550
0
                    return 0;
551
0
            } else {
552
0
                ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
553
0
            }
554
0
        }
555
0
    }
556
557
0
    if (p.oaep != NULL && !OSSL_PARAM_set_utf8_string(p.oaep, prsactx->oaep_md == NULL
558
0
                                                              ? ""
559
0
                                                              : EVP_MD_get0_name(prsactx->oaep_md)))
560
0
        return 0;
561
562
0
    if (p.mgf1 != NULL) {
563
0
        EVP_MD *mgf1_md = prsactx->mgf1_md == NULL ? prsactx->oaep_md
564
0
                                                   : prsactx->mgf1_md;
565
566
0
        if (!OSSL_PARAM_set_utf8_string(p.mgf1, mgf1_md == NULL
567
0
                                                ? ""
568
0
                                                : EVP_MD_get0_name(mgf1_md)))
569
0
        return 0;
570
0
    }
571
572
0
    if (p.label != NULL
573
0
            && !OSSL_PARAM_set_octet_ptr(p.label, prsactx->oaep_label,
574
0
                                         prsactx->oaep_labellen))
575
0
        return 0;
576
577
0
    if (p.tlsver != NULL
578
0
            && !OSSL_PARAM_set_uint(p.tlsver, prsactx->client_version))
579
0
        return 0;
580
581
0
    if (p.negver != NULL
582
0
            && !OSSL_PARAM_set_uint(p.negver, prsactx->alt_version))
583
0
        return 0;
584
585
0
    if (p.imrej != NULL
586
0
            && !OSSL_PARAM_set_uint(p.imrej, prsactx->implicit_rejection))
587
0
        return 0;
588
589
0
    if (!OSSL_FIPS_IND_GET_CTX_FROM_PARAM(prsactx, p.ind))
590
0
        return 0;
591
592
0
    return 1;
593
0
}
594
595
596
static const OSSL_PARAM *rsa_gettable_ctx_params(ossl_unused void *vprsactx,
597
                                                 ossl_unused void *provctx)
598
0
{
599
0
    return rsa_get_ctx_params_list;
600
0
}
601
602
/* Machine generated by util/perl/OpenSSL/paramnames.pm */
603
#ifndef rsa_set_ctx_params_list
604
static const OSSL_PARAM rsa_set_ctx_params_list[] = {
605
    OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, NULL, 0),
606
    OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST_PROPS, NULL, 0),
607
    OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_PAD_MODE, NULL, 0),
608
    OSSL_PARAM_int(OSSL_ASYM_CIPHER_PARAM_PAD_MODE, NULL),
609
    OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST, NULL, 0),
610
    OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST_PROPS, NULL, 0),
611
    OSSL_PARAM_octet_string(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL, NULL, 0),
612
    OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION, NULL),
613
    OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION, NULL),
614
    OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_IMPLICIT_REJECTION, NULL),
615
# if defined(FIPS_MODULE)
616
    OSSL_PARAM_int(OSSL_ASYM_CIPHER_PARAM_FIPS_KEY_CHECK, NULL),
617
# endif
618
# if defined(FIPS_MODULE)
619
    OSSL_PARAM_int(OSSL_ASYM_CIPHER_PARAM_FIPS_RSA_PKCS15_PAD_DISABLED, NULL),
620
# endif
621
    OSSL_PARAM_END
622
};
623
#endif
624
625
#ifndef rsa_set_ctx_params_st
626
struct rsa_set_ctx_params_st {
627
    OSSL_PARAM *imrej;
628
# if defined(FIPS_MODULE)
629
    OSSL_PARAM *ind_k;
630
# endif
631
# if defined(FIPS_MODULE)
632
    OSSL_PARAM *ind_pad;
633
# endif
634
    OSSL_PARAM *label;
635
    OSSL_PARAM *mgf1;
636
    OSSL_PARAM *mgf1_pq;
637
    OSSL_PARAM *negver;
638
    OSSL_PARAM *oaep;
639
    OSSL_PARAM *oaep_pq;
640
    OSSL_PARAM *pad;
641
    OSSL_PARAM *tlsver;
642
};
643
#endif
644
645
#ifndef rsa_set_ctx_params_decoder
646
static int rsa_set_ctx_params_decoder
647
    (const OSSL_PARAM *p, struct rsa_set_ctx_params_st *r)
648
0
{
649
0
    const char *s;
650
651
0
    memset(r, 0, sizeof(*r));
652
0
    if (p != NULL)
653
0
        for (; (s = p->key) != NULL; p++)
654
0
            switch(s[0]) {
655
0
            default:
656
0
                break;
657
0
            case 'd':
658
0
                switch(s[1]) {
659
0
                default:
660
0
                    break;
661
0
                case 'i':
662
0
                    switch(s[2]) {
663
0
                    default:
664
0
                        break;
665
0
                    case 'g':
666
0
                        switch(s[3]) {
667
0
                        default:
668
0
                            break;
669
0
                        case 'e':
670
0
                            switch(s[4]) {
671
0
                            default:
672
0
                                break;
673
0
                            case 's':
674
0
                                switch(s[5]) {
675
0
                                default:
676
0
                                    break;
677
0
                                case 't':
678
0
                                    switch(s[6]) {
679
0
                                    default:
680
0
                                        break;
681
0
                                    case '-':
682
0
                                        if (ossl_likely(strcmp("props", s + 7) == 0)) {
683
                                            /* ASYM_CIPHER_PARAM_OAEP_DIGEST_PROPS */
684
0
                                            if (ossl_unlikely(r->oaep_pq != NULL)) {
685
0
                                                ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
686
0
                                                               "param %s is repeated", s);
687
0
                                                return 0;
688
0
                                            }
689
0
                                            r->oaep_pq = (OSSL_PARAM *)p;
690
0
                                        }
691
0
                                        break;
692
0
                                    case '\0':
693
0
                                        if (ossl_unlikely(r->oaep != NULL)) {
694
0
                                            ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
695
0
                                                           "param %s is repeated", s);
696
0
                                            return 0;
697
0
                                        }
698
0
                                        r->oaep = (OSSL_PARAM *)p;
699
0
                                    }
700
0
                                }
701
0
                            }
702
0
                        }
703
0
                    }
704
0
                }
705
0
                break;
706
0
            case 'i':
707
0
                if (ossl_likely(strcmp("mplicit-rejection", s + 1) == 0)) {
708
                    /* ASYM_CIPHER_PARAM_IMPLICIT_REJECTION */
709
0
                    if (ossl_unlikely(r->imrej != NULL)) {
710
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
711
0
                                       "param %s is repeated", s);
712
0
                        return 0;
713
0
                    }
714
0
                    r->imrej = (OSSL_PARAM *)p;
715
0
                }
716
0
                break;
717
0
            case 'k':
718
# if defined(FIPS_MODULE)
719
                if (ossl_likely(strcmp("ey-check", s + 1) == 0)) {
720
                    /* ASYM_CIPHER_PARAM_FIPS_KEY_CHECK */
721
                    if (ossl_unlikely(r->ind_k != NULL)) {
722
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
723
                                       "param %s is repeated", s);
724
                        return 0;
725
                    }
726
                    r->ind_k = (OSSL_PARAM *)p;
727
                }
728
# endif
729
0
                break;
730
0
            case 'm':
731
0
                switch(s[1]) {
732
0
                default:
733
0
                    break;
734
0
                case 'g':
735
0
                    switch(s[2]) {
736
0
                    default:
737
0
                        break;
738
0
                    case 'f':
739
0
                        switch(s[3]) {
740
0
                        default:
741
0
                            break;
742
0
                        case '1':
743
0
                            switch(s[4]) {
744
0
                            default:
745
0
                                break;
746
0
                            case '-':
747
0
                                switch(s[5]) {
748
0
                                default:
749
0
                                    break;
750
0
                                case 'd':
751
0
                                    if (ossl_likely(strcmp("igest", s + 6) == 0)) {
752
                                        /* ASYM_CIPHER_PARAM_MGF1_DIGEST */
753
0
                                        if (ossl_unlikely(r->mgf1 != NULL)) {
754
0
                                            ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
755
0
                                                           "param %s is repeated", s);
756
0
                                            return 0;
757
0
                                        }
758
0
                                        r->mgf1 = (OSSL_PARAM *)p;
759
0
                                    }
760
0
                                    break;
761
0
                                case 'p':
762
0
                                    if (ossl_likely(strcmp("roperties", s + 6) == 0)) {
763
                                        /* ASYM_CIPHER_PARAM_MGF1_DIGEST_PROPS */
764
0
                                        if (ossl_unlikely(r->mgf1_pq != NULL)) {
765
0
                                            ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
766
0
                                                           "param %s is repeated", s);
767
0
                                            return 0;
768
0
                                        }
769
0
                                        r->mgf1_pq = (OSSL_PARAM *)p;
770
0
                                    }
771
0
                                }
772
0
                            }
773
0
                        }
774
0
                    }
775
0
                }
776
0
                break;
777
0
            case 'o':
778
0
                if (ossl_likely(strcmp("aep-label", s + 1) == 0)) {
779
                    /* ASYM_CIPHER_PARAM_OAEP_LABEL */
780
0
                    if (ossl_unlikely(r->label != NULL)) {
781
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
782
0
                                       "param %s is repeated", s);
783
0
                        return 0;
784
0
                    }
785
0
                    r->label = (OSSL_PARAM *)p;
786
0
                }
787
0
                break;
788
0
            case 'p':
789
0
                if (ossl_likely(strcmp("ad-mode", s + 1) == 0)) {
790
                    /* ASYM_CIPHER_PARAM_PAD_MODE */
791
0
                    if (ossl_unlikely(r->pad != NULL)) {
792
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
793
0
                                       "param %s is repeated", s);
794
0
                        return 0;
795
0
                    }
796
0
                    r->pad = (OSSL_PARAM *)p;
797
0
                }
798
0
                break;
799
0
            case 'r':
800
# if defined(FIPS_MODULE)
801
                if (ossl_likely(strcmp("sa-pkcs15-pad-disabled", s + 1) == 0)) {
802
                    /* ASYM_CIPHER_PARAM_FIPS_RSA_PKCS15_PAD_DISABLED */
803
                    if (ossl_unlikely(r->ind_pad != NULL)) {
804
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
805
                                       "param %s is repeated", s);
806
                        return 0;
807
                    }
808
                    r->ind_pad = (OSSL_PARAM *)p;
809
                }
810
# endif
811
0
                break;
812
0
            case 't':
813
0
                switch(s[1]) {
814
0
                default:
815
0
                    break;
816
0
                case 'l':
817
0
                    switch(s[2]) {
818
0
                    default:
819
0
                        break;
820
0
                    case 's':
821
0
                        switch(s[3]) {
822
0
                        default:
823
0
                            break;
824
0
                        case '-':
825
0
                            switch(s[4]) {
826
0
                            default:
827
0
                                break;
828
0
                            case 'c':
829
0
                                if (ossl_likely(strcmp("lient-version", s + 5) == 0)) {
830
                                    /* ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION */
831
0
                                    if (ossl_unlikely(r->tlsver != NULL)) {
832
0
                                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
833
0
                                                       "param %s is repeated", s);
834
0
                                        return 0;
835
0
                                    }
836
0
                                    r->tlsver = (OSSL_PARAM *)p;
837
0
                                }
838
0
                                break;
839
0
                            case 'n':
840
0
                                if (ossl_likely(strcmp("egotiated-version", s + 5) == 0)) {
841
                                    /* ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION */
842
0
                                    if (ossl_unlikely(r->negver != NULL)) {
843
0
                                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
844
0
                                                       "param %s is repeated", s);
845
0
                                        return 0;
846
0
                                    }
847
0
                                    r->negver = (OSSL_PARAM *)p;
848
0
                                }
849
0
                            }
850
0
                        }
851
0
                    }
852
0
                }
853
0
            }
854
0
    return 1;
855
0
}
856
#endif
857
/* End of machine generated */
858
859
static int rsa_set_ctx_params(void *vprsactx, const OSSL_PARAM params[])
860
0
{
861
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
862
0
    struct rsa_set_ctx_params_st p;
863
0
    char mdname[OSSL_MAX_NAME_SIZE];
864
0
    char mdprops[OSSL_MAX_PROPQUERY_SIZE] = { '\0' };
865
0
    char *str = NULL;
866
867
0
    if (prsactx == NULL || !rsa_set_ctx_params_decoder(params, &p))
868
0
        return 0;
869
870
0
    if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(prsactx, OSSL_FIPS_IND_SETTABLE0, p.ind_k))
871
0
        return 0;
872
0
    if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(prsactx, OSSL_FIPS_IND_SETTABLE1, p.ind_pad))
873
0
        return 0;
874
875
0
    if (p.oaep != NULL) {
876
0
        str = mdname;
877
0
        if (!OSSL_PARAM_get_utf8_string(p.oaep, &str, sizeof(mdname)))
878
0
            return 0;
879
880
0
        if (p.oaep_pq != NULL) {
881
0
            str = mdprops;
882
0
            if (!OSSL_PARAM_get_utf8_string(p.oaep_pq, &str, sizeof(mdprops)))
883
0
                return 0;
884
0
        }
885
886
0
        EVP_MD_free(prsactx->oaep_md);
887
0
        prsactx->oaep_md = EVP_MD_fetch(prsactx->libctx, mdname, mdprops);
888
889
0
        if (prsactx->oaep_md == NULL)
890
0
            return 0;
891
0
    }
892
893
0
    if (p.pad != NULL) {
894
0
        int pad_mode = 0;
895
896
0
        if (p.pad->data_type != OSSL_PARAM_UTF8_STRING) {
897
            /* Support for legacy pad mode as a number */
898
0
            if (!OSSL_PARAM_get_int(p.pad, &pad_mode))
899
0
                return 0;
900
0
        } else {
901
0
            int i;
902
903
0
            if (p.pad->data == NULL)
904
0
                return 0;
905
906
0
            for (i = 0; padding_item[i].id != 0; i++) {
907
0
                if (strcmp(p.pad->data, padding_item[i].ptr) == 0) {
908
0
                    pad_mode = padding_item[i].id;
909
0
                    break;
910
0
                }
911
0
            }
912
0
        }
913
914
        /*
915
         * PSS padding is for signatures only so is not compatible with
916
         * asymmetric cipher use.
917
         */
918
0
        if (pad_mode == RSA_PKCS1_PSS_PADDING)
919
0
            return 0;
920
0
        if (pad_mode == RSA_PKCS1_OAEP_PADDING && prsactx->oaep_md == NULL) {
921
0
            prsactx->oaep_md = EVP_MD_fetch(prsactx->libctx, "SHA1", mdprops);
922
0
            if (prsactx->oaep_md == NULL)
923
0
                return 0;
924
0
        }
925
0
        prsactx->pad_mode = pad_mode;
926
0
    }
927
928
0
    if (p.mgf1 != NULL) {
929
0
        str = mdname;
930
0
        if (!OSSL_PARAM_get_utf8_string(p.mgf1, &str, sizeof(mdname)))
931
0
            return 0;
932
933
0
        if (p.mgf1_pq != NULL) {
934
0
            str = mdprops;
935
0
            if (!OSSL_PARAM_get_utf8_string(p.mgf1_pq, &str, sizeof(mdprops)))
936
0
                return 0;
937
0
        } else {
938
0
            str = NULL;
939
0
        }
940
941
0
        EVP_MD_free(prsactx->mgf1_md);
942
0
        prsactx->mgf1_md = EVP_MD_fetch(prsactx->libctx, mdname, str);
943
944
0
        if (prsactx->mgf1_md == NULL)
945
0
            return 0;
946
0
    }
947
948
0
    if (p.label != NULL) {
949
0
        void *tmp_label = NULL;
950
0
        size_t tmp_labellen;
951
952
0
        if (!OSSL_PARAM_get_octet_string(p.label, &tmp_label, 0, &tmp_labellen))
953
0
            return 0;
954
0
        OPENSSL_free(prsactx->oaep_label);
955
0
        prsactx->oaep_label = (unsigned char *)tmp_label;
956
0
        prsactx->oaep_labellen = tmp_labellen;
957
0
    }
958
959
0
    if (p.tlsver != NULL) {
960
0
        unsigned int client_version;
961
962
0
        if (!OSSL_PARAM_get_uint(p.tlsver, &client_version))
963
0
            return 0;
964
0
        prsactx->client_version = client_version;
965
0
    }
966
967
0
    if (p.negver != NULL) {
968
0
        unsigned int alt_version;
969
970
0
        if (!OSSL_PARAM_get_uint(p.negver, &alt_version))
971
0
            return 0;
972
0
        prsactx->alt_version = alt_version;
973
0
    }
974
975
0
    if (p.imrej != NULL) {
976
0
        unsigned int implicit_rejection;
977
978
0
        if (!OSSL_PARAM_get_uint(p.imrej, &implicit_rejection))
979
0
            return 0;
980
0
        prsactx->implicit_rejection = implicit_rejection;
981
0
    }
982
0
    return 1;
983
0
}
984
985
static const OSSL_PARAM *rsa_settable_ctx_params(ossl_unused void *vprsactx,
986
                                                 ossl_unused void *provctx)
987
0
{
988
0
    return rsa_set_ctx_params_list;
989
0
}
990
991
const OSSL_DISPATCH ossl_rsa_asym_cipher_functions[] = {
992
    { OSSL_FUNC_ASYM_CIPHER_NEWCTX, (void (*)(void))rsa_newctx },
993
    { OSSL_FUNC_ASYM_CIPHER_ENCRYPT_INIT, (void (*)(void))rsa_encrypt_init },
994
    { OSSL_FUNC_ASYM_CIPHER_ENCRYPT, (void (*)(void))rsa_encrypt },
995
    { OSSL_FUNC_ASYM_CIPHER_DECRYPT_INIT, (void (*)(void))rsa_decrypt_init },
996
    { OSSL_FUNC_ASYM_CIPHER_DECRYPT, (void (*)(void))rsa_decrypt },
997
    { OSSL_FUNC_ASYM_CIPHER_FREECTX, (void (*)(void))rsa_freectx },
998
    { OSSL_FUNC_ASYM_CIPHER_DUPCTX, (void (*)(void))rsa_dupctx },
999
    { OSSL_FUNC_ASYM_CIPHER_GET_CTX_PARAMS,
1000
      (void (*)(void))rsa_get_ctx_params },
1001
    { OSSL_FUNC_ASYM_CIPHER_GETTABLE_CTX_PARAMS,
1002
      (void (*)(void))rsa_gettable_ctx_params },
1003
    { OSSL_FUNC_ASYM_CIPHER_SET_CTX_PARAMS,
1004
      (void (*)(void))rsa_set_ctx_params },
1005
    { OSSL_FUNC_ASYM_CIPHER_SETTABLE_CTX_PARAMS,
1006
      (void (*)(void))rsa_settable_ctx_params },
1007
    OSSL_DISPATCH_END
1008
};