Coverage Report

Created: 2025-06-13 06:58

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