Coverage Report

Created: 2025-12-10 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/rsa/rsa_pmeth.c
Line
Count
Source
1
/*
2
 * Copyright 2006-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 "internal/constant_time.h"
17
18
#include <stdio.h>
19
#include "internal/cryptlib.h"
20
#include <openssl/asn1t.h>
21
#include <openssl/x509.h>
22
#include <openssl/rsa.h>
23
#include <openssl/bn.h>
24
#include <openssl/evp.h>
25
#include <openssl/x509v3.h>
26
#include <openssl/cms.h>
27
#include "crypto/evp.h"
28
#include "crypto/rsa.h"
29
#include "rsa_local.h"
30
31
/* RSA pkey context structure */
32
33
typedef struct {
34
    /* Key gen parameters */
35
    int nbits;
36
    BIGNUM *pub_exp;
37
    int primes;
38
    /* Keygen callback info */
39
    int gentmp[2];
40
    /* RSA padding mode */
41
    int pad_mode;
42
    /* message digest */
43
    const EVP_MD *md;
44
    /* message digest for MGF1 */
45
    const EVP_MD *mgf1md;
46
    /* PSS salt length */
47
    int saltlen;
48
    /* Minimum salt length or -1 if no PSS parameter restriction */
49
    int min_saltlen;
50
    /* Temp buffer */
51
    unsigned char *tbuf;
52
    /* OAEP label */
53
    unsigned char *oaep_label;
54
    size_t oaep_labellen;
55
    /* if to use implicit rejection in PKCS#1 v1.5 decryption */
56
    int implicit_rejection;
57
} RSA_PKEY_CTX;
58
59
/* True if PSS parameters are restricted */
60
0
#define rsa_pss_restricted(rctx) (rctx->min_saltlen != -1)
61
62
static int pkey_rsa_init(EVP_PKEY_CTX *ctx)
63
0
{
64
0
    RSA_PKEY_CTX *rctx = OPENSSL_zalloc(sizeof(*rctx));
65
66
0
    if (rctx == NULL)
67
0
        return 0;
68
0
    rctx->nbits = 2048;
69
0
    rctx->primes = RSA_DEFAULT_PRIME_NUM;
70
0
    if (pkey_ctx_is_pss(ctx))
71
0
        rctx->pad_mode = RSA_PKCS1_PSS_PADDING;
72
0
    else
73
0
        rctx->pad_mode = RSA_PKCS1_PADDING;
74
    /* Maximum for sign, auto for verify */
75
0
    rctx->saltlen = RSA_PSS_SALTLEN_AUTO;
76
0
    rctx->min_saltlen = -1;
77
0
    rctx->implicit_rejection = 1;
78
0
    ctx->data = rctx;
79
0
    ctx->keygen_info = rctx->gentmp;
80
0
    ctx->keygen_info_count = 2;
81
82
0
    return 1;
83
0
}
84
85
static int pkey_rsa_copy(EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src)
86
0
{
87
0
    RSA_PKEY_CTX *dctx, *sctx;
88
89
0
    if (!pkey_rsa_init(dst))
90
0
        return 0;
91
0
    sctx = src->data;
92
0
    dctx = dst->data;
93
0
    dctx->nbits = sctx->nbits;
94
0
    if (sctx->pub_exp) {
95
0
        dctx->pub_exp = BN_dup(sctx->pub_exp);
96
0
        if (!dctx->pub_exp)
97
0
            return 0;
98
0
    }
99
0
    dctx->pad_mode = sctx->pad_mode;
100
0
    dctx->md = sctx->md;
101
0
    dctx->mgf1md = sctx->mgf1md;
102
0
    dctx->saltlen = sctx->saltlen;
103
0
    dctx->implicit_rejection = sctx->implicit_rejection;
104
0
    if (sctx->oaep_label) {
105
0
        OPENSSL_free(dctx->oaep_label);
106
0
        dctx->oaep_label = OPENSSL_memdup(sctx->oaep_label, sctx->oaep_labellen);
107
0
        if (!dctx->oaep_label)
108
0
            return 0;
109
0
        dctx->oaep_labellen = sctx->oaep_labellen;
110
0
    }
111
0
    return 1;
112
0
}
113
114
static int setup_tbuf(RSA_PKEY_CTX *ctx, EVP_PKEY_CTX *pk)
115
0
{
116
0
    if (ctx->tbuf != NULL)
117
0
        return 1;
118
0
    if ((ctx->tbuf = OPENSSL_malloc(RSA_size(EVP_PKEY_get0_RSA(pk->pkey)))) == NULL)
119
0
        return 0;
120
0
    return 1;
121
0
}
122
123
static void pkey_rsa_cleanup(EVP_PKEY_CTX *ctx)
124
0
{
125
0
    RSA_PKEY_CTX *rctx = ctx->data;
126
0
    if (rctx) {
127
0
        BN_free(rctx->pub_exp);
128
0
        OPENSSL_free(rctx->tbuf);
129
0
        OPENSSL_free(rctx->oaep_label);
130
0
        OPENSSL_free(rctx);
131
0
    }
132
0
}
133
134
static int pkey_rsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig,
135
    size_t *siglen, const unsigned char *tbs,
136
    size_t tbslen)
137
0
{
138
0
    int ret;
139
0
    RSA_PKEY_CTX *rctx = ctx->data;
140
    /*
141
     * Discard const. Its marked as const because this may be a cached copy of
142
     * the "real" key. These calls don't make any modifications that need to
143
     * be reflected back in the "original" key.
144
     */
145
0
    RSA *rsa = (RSA *)EVP_PKEY_get0_RSA(ctx->pkey);
146
0
    int md_size;
147
148
0
    if (rctx->md) {
149
0
        md_size = EVP_MD_get_size(rctx->md);
150
0
        if (md_size <= 0) {
151
0
            ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_DIGEST_LENGTH);
152
0
            return -1;
153
0
        }
154
155
0
        if (tbslen != (size_t)md_size) {
156
0
            ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_DIGEST_LENGTH);
157
0
            return -1;
158
0
        }
159
160
0
        if (EVP_MD_get_type(rctx->md) == NID_mdc2) {
161
0
            unsigned int sltmp;
162
0
            if (rctx->pad_mode != RSA_PKCS1_PADDING)
163
0
                return -1;
164
0
            ret = RSA_sign_ASN1_OCTET_STRING(0, tbs, (int)tbslen, sig, &sltmp, rsa);
165
166
0
            if (ret <= 0)
167
0
                return ret;
168
0
            ret = sltmp;
169
0
        } else if (rctx->pad_mode == RSA_X931_PADDING) {
170
0
            if ((size_t)RSA_size(rsa) < tbslen + 1) {
171
0
                ERR_raise(ERR_LIB_RSA, RSA_R_KEY_SIZE_TOO_SMALL);
172
0
                return -1;
173
0
            }
174
0
            if (!setup_tbuf(rctx, ctx)) {
175
0
                ERR_raise(ERR_LIB_RSA, ERR_R_RSA_LIB);
176
0
                return -1;
177
0
            }
178
0
            memcpy(rctx->tbuf, tbs, tbslen);
179
0
            rctx->tbuf[tbslen] = RSA_X931_hash_id(EVP_MD_get_type(rctx->md));
180
0
            ret = RSA_private_encrypt((int)(tbslen + 1), rctx->tbuf,
181
0
                sig, rsa, RSA_X931_PADDING);
182
0
        } else if (rctx->pad_mode == RSA_PKCS1_PADDING) {
183
0
            unsigned int sltmp;
184
0
            ret = RSA_sign(EVP_MD_get_type(rctx->md),
185
0
                tbs, (unsigned int)tbslen, sig, &sltmp, rsa);
186
0
            if (ret <= 0)
187
0
                return ret;
188
0
            ret = sltmp;
189
0
        } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) {
190
0
            if (!setup_tbuf(rctx, ctx))
191
0
                return -1;
192
0
            if (!RSA_padding_add_PKCS1_PSS_mgf1(rsa,
193
0
                    rctx->tbuf, tbs,
194
0
                    rctx->md, rctx->mgf1md,
195
0
                    rctx->saltlen))
196
0
                return -1;
197
0
            ret = RSA_private_encrypt(RSA_size(rsa), rctx->tbuf,
198
0
                sig, rsa, RSA_NO_PADDING);
199
0
        } else {
200
0
            return -1;
201
0
        }
202
0
    } else {
203
0
        ret = RSA_private_encrypt((int)tbslen, tbs, sig, rsa, rctx->pad_mode);
204
0
    }
205
0
    if (ret < 0)
206
0
        return ret;
207
0
    *siglen = ret;
208
0
    return 1;
209
0
}
210
211
static int pkey_rsa_verifyrecover(EVP_PKEY_CTX *ctx,
212
    unsigned char *rout, size_t *routlen,
213
    const unsigned char *sig, size_t siglen)
214
0
{
215
0
    int ret;
216
0
    RSA_PKEY_CTX *rctx = ctx->data;
217
    /*
218
     * Discard const. Its marked as const because this may be a cached copy of
219
     * the "real" key. These calls don't make any modifications that need to
220
     * be reflected back in the "original" key.
221
     */
222
0
    RSA *rsa = (RSA *)EVP_PKEY_get0_RSA(ctx->pkey);
223
224
0
    if (rctx->md) {
225
0
        if (rctx->pad_mode == RSA_X931_PADDING) {
226
0
            if (!setup_tbuf(rctx, ctx))
227
0
                return -1;
228
0
            ret = RSA_public_decrypt((int)siglen, sig, rctx->tbuf, rsa,
229
0
                RSA_X931_PADDING);
230
0
            if (ret <= 0)
231
0
                return 0;
232
0
            ret--;
233
0
            if (rctx->tbuf[ret] != RSA_X931_hash_id(EVP_MD_get_type(rctx->md))) {
234
0
                ERR_raise(ERR_LIB_RSA, RSA_R_ALGORITHM_MISMATCH);
235
0
                return 0;
236
0
            }
237
0
            if (ret != EVP_MD_get_size(rctx->md)) {
238
0
                ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_DIGEST_LENGTH);
239
0
                return 0;
240
0
            }
241
0
            if (rout)
242
0
                memcpy(rout, rctx->tbuf, ret);
243
0
        } else if (rctx->pad_mode == RSA_PKCS1_PADDING) {
244
0
            size_t sltmp;
245
0
            ret = ossl_rsa_verify(EVP_MD_get_type(rctx->md),
246
0
                NULL, 0, rout, &sltmp,
247
0
                sig, siglen, rsa);
248
0
            if (ret <= 0)
249
0
                return 0;
250
0
            ret = (int)sltmp;
251
0
        } else {
252
0
            return -1;
253
0
        }
254
0
    } else {
255
0
        ret = RSA_public_decrypt((int)siglen, sig, rout, rsa, rctx->pad_mode);
256
0
    }
257
0
    if (ret <= 0)
258
0
        return ret;
259
0
    *routlen = ret;
260
0
    return 1;
261
0
}
262
263
static int pkey_rsa_verify(EVP_PKEY_CTX *ctx,
264
    const unsigned char *sig, size_t siglen,
265
    const unsigned char *tbs, size_t tbslen)
266
0
{
267
0
    RSA_PKEY_CTX *rctx = ctx->data;
268
    /*
269
     * Discard const. Its marked as const because this may be a cached copy of
270
     * the "real" key. These calls don't make any modifications that need to
271
     * be reflected back in the "original" key.
272
     */
273
0
    RSA *rsa = (RSA *)EVP_PKEY_get0_RSA(ctx->pkey);
274
0
    size_t rslen;
275
0
    int md_size;
276
277
0
    if (rctx->md) {
278
0
        if (rctx->pad_mode == RSA_PKCS1_PADDING)
279
0
            return RSA_verify(EVP_MD_get_type(rctx->md), tbs, (unsigned int)tbslen,
280
0
                sig, (unsigned int)siglen, rsa);
281
0
        md_size = EVP_MD_get_size(rctx->md);
282
0
        if (md_size <= 0) {
283
0
            ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_DIGEST_LENGTH);
284
0
            return -1;
285
0
        }
286
0
        if (tbslen != (size_t)md_size) {
287
0
            ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_DIGEST_LENGTH);
288
0
            return -1;
289
0
        }
290
0
        if (rctx->pad_mode == RSA_X931_PADDING) {
291
0
            if (pkey_rsa_verifyrecover(ctx, NULL, &rslen, sig, siglen) <= 0)
292
0
                return 0;
293
0
        } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) {
294
0
            int ret;
295
0
            if (!setup_tbuf(rctx, ctx))
296
0
                return -1;
297
0
            ret = RSA_public_decrypt((int)siglen, sig, rctx->tbuf,
298
0
                rsa, RSA_NO_PADDING);
299
0
            if (ret <= 0)
300
0
                return 0;
301
0
            ret = RSA_verify_PKCS1_PSS_mgf1(rsa, tbs,
302
0
                rctx->md, rctx->mgf1md,
303
0
                rctx->tbuf, rctx->saltlen);
304
0
            if (ret <= 0)
305
0
                return 0;
306
0
            return 1;
307
0
        } else {
308
0
            return -1;
309
0
        }
310
0
    } else {
311
0
        if (!setup_tbuf(rctx, ctx))
312
0
            return -1;
313
0
        rslen = RSA_public_decrypt((int)siglen, sig, rctx->tbuf,
314
0
            rsa, rctx->pad_mode);
315
0
        if (rslen <= 0)
316
0
            return 0;
317
0
    }
318
319
0
    if ((rslen != tbslen) || memcmp(tbs, rctx->tbuf, rslen))
320
0
        return 0;
321
322
0
    return 1;
323
0
}
324
325
static int pkey_rsa_encrypt(EVP_PKEY_CTX *ctx,
326
    unsigned char *out, size_t *outlen,
327
    const unsigned char *in, size_t inlen)
328
0
{
329
0
    int ret;
330
0
    RSA_PKEY_CTX *rctx = ctx->data;
331
    /*
332
     * Discard const. Its marked as const because this may be a cached copy of
333
     * the "real" key. These calls don't make any modifications that need to
334
     * be reflected back in the "original" key.
335
     */
336
0
    RSA *rsa = (RSA *)EVP_PKEY_get0_RSA(ctx->pkey);
337
338
0
    if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
339
0
        int klen = RSA_size(rsa);
340
0
        if (!setup_tbuf(rctx, ctx))
341
0
            return -1;
342
0
        if (!RSA_padding_add_PKCS1_OAEP_mgf1(rctx->tbuf, klen,
343
0
                in, (int)inlen,
344
0
                rctx->oaep_label,
345
0
                (int)rctx->oaep_labellen,
346
0
                rctx->md, rctx->mgf1md))
347
0
            return -1;
348
0
        ret = RSA_public_encrypt(klen, rctx->tbuf, out, rsa, RSA_NO_PADDING);
349
0
    } else {
350
0
        ret = RSA_public_encrypt((int)inlen, in, out, rsa, rctx->pad_mode);
351
0
    }
352
0
    if (ret < 0)
353
0
        return ret;
354
0
    *outlen = ret;
355
0
    return 1;
356
0
}
357
358
static int pkey_rsa_decrypt(EVP_PKEY_CTX *ctx,
359
    unsigned char *out, size_t *outlen,
360
    const unsigned char *in, size_t inlen)
361
0
{
362
0
    int ret;
363
0
    int pad_mode;
364
0
    RSA_PKEY_CTX *rctx = ctx->data;
365
    /*
366
     * Discard const. Its marked as const because this may be a cached copy of
367
     * the "real" key. These calls don't make any modifications that need to
368
     * be reflected back in the "original" key.
369
     */
370
0
    RSA *rsa = (RSA *)EVP_PKEY_get0_RSA(ctx->pkey);
371
372
0
    if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
373
0
        if (!setup_tbuf(rctx, ctx))
374
0
            return -1;
375
0
        ret = RSA_private_decrypt((int)inlen, in, rctx->tbuf, rsa, RSA_NO_PADDING);
376
0
        if (ret <= 0)
377
0
            return ret;
378
0
        ret = RSA_padding_check_PKCS1_OAEP_mgf1(out, ret, rctx->tbuf,
379
0
            ret, ret,
380
0
            rctx->oaep_label,
381
0
            (int)rctx->oaep_labellen,
382
0
            rctx->md, rctx->mgf1md);
383
0
    } else {
384
0
        if (rctx->pad_mode == RSA_PKCS1_PADDING && rctx->implicit_rejection == 0)
385
0
            pad_mode = RSA_PKCS1_NO_IMPLICIT_REJECT_PADDING;
386
0
        else
387
0
            pad_mode = rctx->pad_mode;
388
0
        ret = RSA_private_decrypt((int)inlen, in, out, rsa, pad_mode);
389
0
    }
390
0
    *outlen = constant_time_select_s(constant_time_msb_s(ret), *outlen, ret);
391
0
    ret = constant_time_select_int(constant_time_msb(ret), ret, 1);
392
0
    return ret;
393
0
}
394
395
static int check_padding_md(const EVP_MD *md, int padding)
396
0
{
397
0
    int mdnid;
398
399
0
    if (!md)
400
0
        return 1;
401
402
0
    mdnid = EVP_MD_get_type(md);
403
404
0
    if (padding == RSA_NO_PADDING) {
405
0
        ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PADDING_MODE);
406
0
        return 0;
407
0
    }
408
409
0
    if (padding == RSA_X931_PADDING) {
410
0
        if (RSA_X931_hash_id(mdnid) == -1) {
411
0
            ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_X931_DIGEST);
412
0
            return 0;
413
0
        }
414
0
    } else {
415
0
        switch (mdnid) {
416
        /* List of all supported RSA digests */
417
0
        case NID_sha1:
418
0
        case NID_sha224:
419
0
        case NID_sha256:
420
0
        case NID_sha384:
421
0
        case NID_sha512:
422
0
        case NID_sha512_224:
423
0
        case NID_sha512_256:
424
0
        case NID_md5:
425
0
        case NID_md5_sha1:
426
0
        case NID_md2:
427
0
        case NID_md4:
428
0
        case NID_mdc2:
429
0
        case NID_ripemd160:
430
0
        case NID_sha3_224:
431
0
        case NID_sha3_256:
432
0
        case NID_sha3_384:
433
0
        case NID_sha3_512:
434
0
            return 1;
435
436
0
        default:
437
0
            ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_DIGEST);
438
0
            return 0;
439
0
        }
440
0
    }
441
442
0
    return 1;
443
0
}
444
445
static int pkey_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
446
0
{
447
0
    RSA_PKEY_CTX *rctx = ctx->data;
448
0
    int md_size;
449
450
0
    switch (type) {
451
0
    case EVP_PKEY_CTRL_RSA_PADDING:
452
0
        if ((p1 >= RSA_PKCS1_PADDING) && (p1 <= RSA_PKCS1_PSS_PADDING)) {
453
0
            if (!check_padding_md(rctx->md, p1))
454
0
                return 0;
455
0
            if (p1 == RSA_PKCS1_PSS_PADDING) {
456
0
                if (!(ctx->operation & (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY)))
457
0
                    goto bad_pad;
458
0
                if (!rctx->md)
459
0
                    rctx->md = EVP_sha1();
460
0
            } else if (pkey_ctx_is_pss(ctx)) {
461
0
                goto bad_pad;
462
0
            }
463
0
            if (p1 == RSA_PKCS1_OAEP_PADDING) {
464
0
                if (!(ctx->operation & EVP_PKEY_OP_TYPE_CRYPT))
465
0
                    goto bad_pad;
466
0
                if (!rctx->md)
467
0
                    rctx->md = EVP_sha1();
468
0
            }
469
0
            rctx->pad_mode = p1;
470
0
            return 1;
471
0
        }
472
0
    bad_pad:
473
0
        ERR_raise(ERR_LIB_RSA, RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
474
0
        return -2;
475
476
0
    case EVP_PKEY_CTRL_GET_RSA_PADDING:
477
0
        *(int *)p2 = rctx->pad_mode;
478
0
        return 1;
479
480
0
    case EVP_PKEY_CTRL_RSA_PSS_SALTLEN:
481
0
    case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN:
482
0
        if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING) {
483
0
            ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PSS_SALTLEN);
484
0
            return -2;
485
0
        }
486
0
        if (type == EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN) {
487
0
            *(int *)p2 = rctx->saltlen;
488
0
        } else {
489
0
            if (p1 < RSA_PSS_SALTLEN_MAX)
490
0
                return -2;
491
0
            if (rsa_pss_restricted(rctx)) {
492
0
                if (p1 == RSA_PSS_SALTLEN_AUTO
493
0
                    && ctx->operation == EVP_PKEY_OP_VERIFY) {
494
0
                    ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PSS_SALTLEN);
495
0
                    return -2;
496
0
                }
497
0
                md_size = EVP_MD_get_size(rctx->md);
498
0
                if (md_size <= 0) {
499
0
                    ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_DIGEST_LENGTH);
500
0
                    return -2;
501
0
                }
502
0
                if ((p1 == RSA_PSS_SALTLEN_DIGEST
503
0
                        && rctx->min_saltlen > md_size)
504
0
                    || (p1 >= 0 && p1 < rctx->min_saltlen)) {
505
0
                    ERR_raise(ERR_LIB_RSA, RSA_R_PSS_SALTLEN_TOO_SMALL);
506
0
                    return 0;
507
0
                }
508
0
            }
509
0
            rctx->saltlen = p1;
510
0
        }
511
0
        return 1;
512
513
0
    case EVP_PKEY_CTRL_RSA_KEYGEN_BITS:
514
0
        if (p1 < RSA_MIN_MODULUS_BITS) {
515
0
            ERR_raise(ERR_LIB_RSA, RSA_R_KEY_SIZE_TOO_SMALL);
516
0
            return -2;
517
0
        }
518
0
        rctx->nbits = p1;
519
0
        return 1;
520
521
0
    case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP:
522
0
        if (p2 == NULL || !BN_is_odd((BIGNUM *)p2) || BN_is_one((BIGNUM *)p2)) {
523
0
            ERR_raise(ERR_LIB_RSA, RSA_R_BAD_E_VALUE);
524
0
            return -2;
525
0
        }
526
0
        BN_free(rctx->pub_exp);
527
0
        rctx->pub_exp = p2;
528
0
        return 1;
529
530
0
    case EVP_PKEY_CTRL_RSA_KEYGEN_PRIMES:
531
0
        if (p1 < RSA_DEFAULT_PRIME_NUM || p1 > RSA_MAX_PRIME_NUM) {
532
0
            ERR_raise(ERR_LIB_RSA, RSA_R_KEY_PRIME_NUM_INVALID);
533
0
            return -2;
534
0
        }
535
0
        rctx->primes = p1;
536
0
        return 1;
537
538
0
    case EVP_PKEY_CTRL_RSA_OAEP_MD:
539
0
    case EVP_PKEY_CTRL_GET_RSA_OAEP_MD:
540
0
        if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
541
0
            ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PADDING_MODE);
542
0
            return -2;
543
0
        }
544
0
        if (type == EVP_PKEY_CTRL_GET_RSA_OAEP_MD)
545
0
            *(const EVP_MD **)p2 = rctx->md;
546
0
        else
547
0
            rctx->md = p2;
548
0
        return 1;
549
550
0
    case EVP_PKEY_CTRL_MD:
551
0
        if (!check_padding_md(p2, rctx->pad_mode))
552
0
            return 0;
553
0
        if (rsa_pss_restricted(rctx)) {
554
0
            if (EVP_MD_get_type(rctx->md) == EVP_MD_get_type(p2))
555
0
                return 1;
556
0
            ERR_raise(ERR_LIB_RSA, RSA_R_DIGEST_NOT_ALLOWED);
557
0
            return 0;
558
0
        }
559
0
        rctx->md = p2;
560
0
        return 1;
561
562
0
    case EVP_PKEY_CTRL_GET_MD:
563
0
        *(const EVP_MD **)p2 = rctx->md;
564
0
        return 1;
565
566
0
    case EVP_PKEY_CTRL_RSA_MGF1_MD:
567
0
    case EVP_PKEY_CTRL_GET_RSA_MGF1_MD:
568
0
        if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING
569
0
            && rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
570
0
            ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_MGF1_MD);
571
0
            return -2;
572
0
        }
573
0
        if (type == EVP_PKEY_CTRL_GET_RSA_MGF1_MD) {
574
0
            if (rctx->mgf1md)
575
0
                *(const EVP_MD **)p2 = rctx->mgf1md;
576
0
            else
577
0
                *(const EVP_MD **)p2 = rctx->md;
578
0
        } else {
579
0
            if (rsa_pss_restricted(rctx)) {
580
0
                if (EVP_MD_get_type(rctx->mgf1md) == EVP_MD_get_type(p2))
581
0
                    return 1;
582
0
                ERR_raise(ERR_LIB_RSA, RSA_R_MGF1_DIGEST_NOT_ALLOWED);
583
0
                return 0;
584
0
            }
585
0
            rctx->mgf1md = p2;
586
0
        }
587
0
        return 1;
588
589
0
    case EVP_PKEY_CTRL_RSA_OAEP_LABEL:
590
0
        if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
591
0
            ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PADDING_MODE);
592
0
            return -2;
593
0
        }
594
0
        OPENSSL_free(rctx->oaep_label);
595
0
        if (p2 && p1 > 0) {
596
0
            rctx->oaep_label = p2;
597
0
            rctx->oaep_labellen = p1;
598
0
        } else {
599
0
            rctx->oaep_label = NULL;
600
0
            rctx->oaep_labellen = 0;
601
0
        }
602
0
        return 1;
603
604
0
    case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL:
605
0
        if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
606
0
            ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PADDING_MODE);
607
0
            return -2;
608
0
        }
609
0
        if (p2 == NULL) {
610
0
            ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
611
0
            return 0;
612
0
        }
613
0
        *(unsigned char **)p2 = rctx->oaep_label;
614
0
        return (int)rctx->oaep_labellen;
615
616
0
    case EVP_PKEY_CTRL_RSA_IMPLICIT_REJECTION:
617
0
        if (rctx->pad_mode != RSA_PKCS1_PADDING) {
618
0
            ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PADDING_MODE);
619
0
            return -2;
620
0
        }
621
0
        rctx->implicit_rejection = p1;
622
0
        return 1;
623
624
0
    case EVP_PKEY_CTRL_DIGESTINIT:
625
0
    case EVP_PKEY_CTRL_PKCS7_SIGN:
626
0
#ifndef OPENSSL_NO_CMS
627
0
    case EVP_PKEY_CTRL_CMS_SIGN:
628
0
#endif
629
0
        return 1;
630
631
0
    case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
632
0
    case EVP_PKEY_CTRL_PKCS7_DECRYPT:
633
0
#ifndef OPENSSL_NO_CMS
634
0
    case EVP_PKEY_CTRL_CMS_DECRYPT:
635
0
    case EVP_PKEY_CTRL_CMS_ENCRYPT:
636
0
#endif
637
0
        if (!pkey_ctx_is_pss(ctx))
638
0
            return 1;
639
    /* fall through */
640
0
    case EVP_PKEY_CTRL_PEER_KEY:
641
0
        ERR_raise(ERR_LIB_RSA, RSA_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
642
0
        return -2;
643
644
0
    default:
645
0
        return -2;
646
0
    }
647
0
}
648
649
static int pkey_rsa_ctrl_str(EVP_PKEY_CTX *ctx,
650
    const char *type, const char *value)
651
0
{
652
0
    if (value == NULL) {
653
0
        ERR_raise(ERR_LIB_RSA, RSA_R_VALUE_MISSING);
654
0
        return 0;
655
0
    }
656
0
    if (strcmp(type, "rsa_padding_mode") == 0) {
657
0
        int pm;
658
659
0
        if (strcmp(value, "pkcs1") == 0) {
660
0
            pm = RSA_PKCS1_PADDING;
661
0
        } else if (strcmp(value, "none") == 0) {
662
0
            pm = RSA_NO_PADDING;
663
0
        } else if (strcmp(value, "oeap") == 0) {
664
0
            pm = RSA_PKCS1_OAEP_PADDING;
665
0
        } else if (strcmp(value, "oaep") == 0) {
666
0
            pm = RSA_PKCS1_OAEP_PADDING;
667
0
        } else if (strcmp(value, "x931") == 0) {
668
0
            pm = RSA_X931_PADDING;
669
0
        } else if (strcmp(value, "pss") == 0) {
670
0
            pm = RSA_PKCS1_PSS_PADDING;
671
0
        } else {
672
0
            ERR_raise(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE);
673
0
            return -2;
674
0
        }
675
0
        return EVP_PKEY_CTX_set_rsa_padding(ctx, pm);
676
0
    }
677
678
0
    if (strcmp(type, "rsa_pss_saltlen") == 0) {
679
0
        int saltlen;
680
681
0
        if (!strcmp(value, "digest"))
682
0
            saltlen = RSA_PSS_SALTLEN_DIGEST;
683
0
        else if (!strcmp(value, "max"))
684
0
            saltlen = RSA_PSS_SALTLEN_MAX;
685
0
        else if (!strcmp(value, "auto"))
686
0
            saltlen = RSA_PSS_SALTLEN_AUTO;
687
0
        else
688
0
            saltlen = atoi(value);
689
0
        return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, saltlen);
690
0
    }
691
692
0
    if (strcmp(type, "rsa_keygen_bits") == 0) {
693
0
        int nbits = atoi(value);
694
695
0
        return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, nbits);
696
0
    }
697
698
0
    if (strcmp(type, "rsa_keygen_pubexp") == 0) {
699
0
        int ret;
700
701
0
        BIGNUM *pubexp = NULL;
702
0
        if (!BN_asc2bn(&pubexp, value))
703
0
            return 0;
704
0
        ret = EVP_PKEY_CTX_set1_rsa_keygen_pubexp(ctx, pubexp);
705
0
        BN_free(pubexp);
706
0
        return ret;
707
0
    }
708
709
0
    if (strcmp(type, "rsa_keygen_primes") == 0) {
710
0
        int nprimes = atoi(value);
711
712
0
        return EVP_PKEY_CTX_set_rsa_keygen_primes(ctx, nprimes);
713
0
    }
714
715
0
    if (strcmp(type, "rsa_mgf1_md") == 0)
716
0
        return EVP_PKEY_CTX_md(ctx,
717
0
            EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
718
0
            EVP_PKEY_CTRL_RSA_MGF1_MD, value);
719
720
0
    if (pkey_ctx_is_pss(ctx)) {
721
722
0
        if (strcmp(type, "rsa_pss_keygen_mgf1_md") == 0)
723
0
            return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_KEYGEN,
724
0
                EVP_PKEY_CTRL_RSA_MGF1_MD, value);
725
726
0
        if (strcmp(type, "rsa_pss_keygen_md") == 0)
727
0
            return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_KEYGEN,
728
0
                EVP_PKEY_CTRL_MD, value);
729
730
0
        if (strcmp(type, "rsa_pss_keygen_saltlen") == 0) {
731
0
            int saltlen = atoi(value);
732
733
0
            return EVP_PKEY_CTX_set_rsa_pss_keygen_saltlen(ctx, saltlen);
734
0
        }
735
0
    }
736
737
0
    if (strcmp(type, "rsa_oaep_md") == 0)
738
0
        return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_CRYPT,
739
0
            EVP_PKEY_CTRL_RSA_OAEP_MD, value);
740
741
0
    if (strcmp(type, "rsa_oaep_label") == 0) {
742
0
        unsigned char *lab;
743
0
        long lablen;
744
0
        int ret;
745
746
0
        lab = OPENSSL_hexstr2buf(value, &lablen);
747
0
        if (!lab)
748
0
            return 0;
749
0
        ret = EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, lab, lablen);
750
0
        if (ret <= 0)
751
0
            OPENSSL_free(lab);
752
0
        return ret;
753
0
    }
754
755
0
    return -2;
756
0
}
757
758
/* Set PSS parameters when generating a key, if necessary */
759
static int rsa_set_pss_param(RSA *rsa, EVP_PKEY_CTX *ctx)
760
0
{
761
0
    RSA_PKEY_CTX *rctx = ctx->data;
762
763
0
    if (!pkey_ctx_is_pss(ctx))
764
0
        return 1;
765
    /* If all parameters are default values don't set pss */
766
0
    if (rctx->md == NULL && rctx->mgf1md == NULL && rctx->saltlen == -2)
767
0
        return 1;
768
0
    rsa->pss = ossl_rsa_pss_params_create(rctx->md, rctx->mgf1md,
769
0
        rctx->saltlen == -2
770
0
            ? 0
771
0
            : rctx->saltlen);
772
0
    if (rsa->pss == NULL)
773
0
        return 0;
774
0
    return 1;
775
0
}
776
777
static int pkey_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
778
0
{
779
0
    RSA *rsa = NULL;
780
0
    RSA_PKEY_CTX *rctx = ctx->data;
781
0
    BN_GENCB *pcb;
782
0
    int ret;
783
784
0
    if (rctx->pub_exp == NULL) {
785
0
        rctx->pub_exp = BN_new();
786
0
        if (rctx->pub_exp == NULL || !BN_set_word(rctx->pub_exp, RSA_F4))
787
0
            return 0;
788
0
    }
789
0
    rsa = RSA_new();
790
0
    if (rsa == NULL)
791
0
        return 0;
792
0
    if (ctx->pkey_gencb) {
793
0
        pcb = BN_GENCB_new();
794
0
        if (pcb == NULL) {
795
0
            RSA_free(rsa);
796
0
            return 0;
797
0
        }
798
0
        evp_pkey_set_cb_translate(pcb, ctx);
799
0
    } else {
800
0
        pcb = NULL;
801
0
    }
802
0
    ret = RSA_generate_multi_prime_key(rsa, rctx->nbits, rctx->primes,
803
0
        rctx->pub_exp, pcb);
804
0
    BN_GENCB_free(pcb);
805
0
    if (ret > 0 && !rsa_set_pss_param(rsa, ctx)) {
806
0
        RSA_free(rsa);
807
0
        return 0;
808
0
    }
809
0
    if (ret > 0)
810
0
        EVP_PKEY_assign(pkey, ctx->pmeth->pkey_id, rsa);
811
0
    else
812
0
        RSA_free(rsa);
813
0
    return ret;
814
0
}
815
816
static const EVP_PKEY_METHOD rsa_pkey_meth = {
817
    EVP_PKEY_RSA,
818
    EVP_PKEY_FLAG_AUTOARGLEN,
819
    pkey_rsa_init,
820
    pkey_rsa_copy,
821
    pkey_rsa_cleanup,
822
823
    0, 0,
824
825
    0,
826
    pkey_rsa_keygen,
827
828
    0,
829
    pkey_rsa_sign,
830
831
    0,
832
    pkey_rsa_verify,
833
834
    0,
835
    pkey_rsa_verifyrecover,
836
837
    0, 0, 0, 0,
838
839
    0,
840
    pkey_rsa_encrypt,
841
842
    0,
843
    pkey_rsa_decrypt,
844
845
    0, 0,
846
847
    pkey_rsa_ctrl,
848
    pkey_rsa_ctrl_str
849
};
850
851
const EVP_PKEY_METHOD *ossl_rsa_pkey_method(void)
852
0
{
853
0
    return &rsa_pkey_meth;
854
0
}
855
856
/*
857
 * Called for PSS sign or verify initialisation: checks PSS parameter
858
 * sanity and sets any restrictions on key usage.
859
 */
860
861
static int pkey_pss_init(EVP_PKEY_CTX *ctx)
862
0
{
863
0
    const RSA *rsa;
864
0
    RSA_PKEY_CTX *rctx = ctx->data;
865
0
    const EVP_MD *md;
866
0
    const EVP_MD *mgf1md;
867
0
    int min_saltlen, max_saltlen, md_size;
868
869
    /* Should never happen */
870
0
    if (!pkey_ctx_is_pss(ctx))
871
0
        return 0;
872
0
    rsa = EVP_PKEY_get0_RSA(ctx->pkey);
873
    /* If no restrictions just return */
874
0
    if (rsa->pss == NULL)
875
0
        return 1;
876
    /* Get and check parameters */
877
0
    if (!ossl_rsa_pss_get_param(rsa->pss, &md, &mgf1md, &min_saltlen))
878
0
        return 0;
879
880
    /* See if minimum salt length exceeds maximum possible */
881
0
    md_size = EVP_MD_get_size(md);
882
0
    if (md_size <= 0) {
883
0
        ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_DIGEST_LENGTH);
884
0
        return 0;
885
0
    }
886
0
    max_saltlen = RSA_size(rsa) - md_size;
887
0
    if ((RSA_bits(rsa) & 0x7) == 1)
888
0
        max_saltlen--;
889
0
    if (min_saltlen > max_saltlen) {
890
0
        ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_SALT_LENGTH);
891
0
        return 0;
892
0
    }
893
894
0
    rctx->min_saltlen = min_saltlen;
895
896
    /*
897
     * Set PSS restrictions as defaults: we can then block any attempt to
898
     * use invalid values in pkey_rsa_ctrl
899
     */
900
901
0
    rctx->md = md;
902
0
    rctx->mgf1md = mgf1md;
903
0
    rctx->saltlen = min_saltlen;
904
905
0
    return 1;
906
0
}
907
908
static const EVP_PKEY_METHOD rsa_pss_pkey_meth = {
909
    EVP_PKEY_RSA_PSS,
910
    EVP_PKEY_FLAG_AUTOARGLEN,
911
    pkey_rsa_init,
912
    pkey_rsa_copy,
913
    pkey_rsa_cleanup,
914
915
    0, 0,
916
917
    0,
918
    pkey_rsa_keygen,
919
920
    pkey_pss_init,
921
    pkey_rsa_sign,
922
923
    pkey_pss_init,
924
    pkey_rsa_verify,
925
926
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
927
928
    pkey_rsa_ctrl,
929
    pkey_rsa_ctrl_str
930
};
931
932
const EVP_PKEY_METHOD *ossl_rsa_pss_pkey_method(void)
933
0
{
934
0
    return &rsa_pss_pkey_meth;
935
0
}