Coverage Report

Created: 2025-12-31 06:58

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