Coverage Report

Created: 2025-12-31 06:58

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