Coverage Report

Created: 2025-06-13 06:58

/src/openssl30/crypto/rsa/rsa_ameth.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 <stdio.h>
17
#include "internal/cryptlib.h"
18
#include <openssl/asn1t.h>
19
#include <openssl/x509.h>
20
#include <openssl/bn.h>
21
#include <openssl/core_names.h>
22
#include <openssl/param_build.h>
23
#include "crypto/asn1.h"
24
#include "crypto/evp.h"
25
#include "crypto/rsa.h"
26
#include "rsa_local.h"
27
28
/* Set any parameters associated with pkey */
29
static int rsa_param_encode(const EVP_PKEY *pkey,
30
                            ASN1_STRING **pstr, int *pstrtype)
31
2
{
32
2
    const RSA *rsa = pkey->pkey.rsa;
33
34
2
    *pstr = NULL;
35
    /* If RSA it's just NULL type */
36
2
    if (RSA_test_flags(rsa, RSA_FLAG_TYPE_MASK) != RSA_FLAG_TYPE_RSASSAPSS) {
37
0
        *pstrtype = V_ASN1_NULL;
38
0
        return 1;
39
0
    }
40
    /* If no PSS parameters we omit parameters entirely */
41
2
    if (rsa->pss == NULL) {
42
0
        *pstrtype = V_ASN1_UNDEF;
43
0
        return 1;
44
0
    }
45
    /* Encode PSS parameters */
46
2
    if (ASN1_item_pack(rsa->pss, ASN1_ITEM_rptr(RSA_PSS_PARAMS), pstr) == NULL)
47
0
        return 0;
48
49
2
    *pstrtype = V_ASN1_SEQUENCE;
50
2
    return 1;
51
2
}
52
/* Decode any parameters and set them in RSA structure */
53
static int rsa_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
54
0
{
55
0
    unsigned char *penc = NULL;
56
0
    int penclen;
57
0
    ASN1_STRING *str;
58
0
    int strtype;
59
60
0
    if (!rsa_param_encode(pkey, &str, &strtype))
61
0
        return 0;
62
0
    penclen = i2d_RSAPublicKey(pkey->pkey.rsa, &penc);
63
0
    if (penclen <= 0) {
64
0
        ASN1_STRING_free(str);
65
0
        return 0;
66
0
    }
67
0
    if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(pkey->ameth->pkey_id),
68
0
                               strtype, str, penc, penclen))
69
0
        return 1;
70
71
0
    OPENSSL_free(penc);
72
0
    ASN1_STRING_free(str);
73
0
    return 0;
74
0
}
75
76
static int rsa_pub_decode(EVP_PKEY *pkey, const X509_PUBKEY *pubkey)
77
115k
{
78
115k
    const unsigned char *p;
79
115k
    int pklen;
80
115k
    X509_ALGOR *alg;
81
115k
    RSA *rsa = NULL;
82
83
115k
    if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &alg, pubkey))
84
0
        return 0;
85
115k
    if ((rsa = d2i_RSAPublicKey(NULL, &p, pklen)) == NULL)
86
15.2k
        return 0;
87
100k
    if (!ossl_rsa_param_decode(rsa, alg)) {
88
3.02k
        RSA_free(rsa);
89
3.02k
        return 0;
90
3.02k
    }
91
92
97.0k
    RSA_clear_flags(rsa, RSA_FLAG_TYPE_MASK);
93
97.0k
    switch (pkey->ameth->pkey_id) {
94
57.3k
    case EVP_PKEY_RSA:
95
57.3k
        RSA_set_flags(rsa, RSA_FLAG_TYPE_RSA);
96
57.3k
        break;
97
39.6k
    case EVP_PKEY_RSA_PSS:
98
39.6k
        RSA_set_flags(rsa, RSA_FLAG_TYPE_RSASSAPSS);
99
39.6k
        break;
100
0
    default:
101
        /* Leave the type bits zero */
102
0
        break;
103
97.0k
    }
104
105
97.0k
    if (!EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, rsa)) {
106
0
        RSA_free(rsa);
107
0
        return 0;
108
0
    }
109
97.0k
    return 1;
110
97.0k
}
111
112
static int rsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
113
0
{
114
    /*
115
     * Don't check the public/private key, this is mostly for smart
116
     * cards.
117
     */
118
0
    if (((RSA_flags(a->pkey.rsa) & RSA_METHOD_FLAG_NO_CHECK))
119
0
            || (RSA_flags(b->pkey.rsa) & RSA_METHOD_FLAG_NO_CHECK)) {
120
0
        return 1;
121
0
    }
122
123
0
    if (BN_cmp(b->pkey.rsa->n, a->pkey.rsa->n) != 0
124
0
        || BN_cmp(b->pkey.rsa->e, a->pkey.rsa->e) != 0)
125
0
        return 0;
126
0
    return 1;
127
0
}
128
129
static int old_rsa_priv_decode(EVP_PKEY *pkey,
130
                               const unsigned char **pder, int derlen)
131
39.0k
{
132
39.0k
    RSA *rsa;
133
134
39.0k
    if ((rsa = d2i_RSAPrivateKey(NULL, pder, derlen)) == NULL)
135
38.5k
        return 0;
136
476
    EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, rsa);
137
476
    return 1;
138
39.0k
}
139
140
static int old_rsa_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
141
479
{
142
479
    return i2d_RSAPrivateKey(pkey->pkey.rsa, pder);
143
479
}
144
145
static int rsa_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
146
2
{
147
2
    unsigned char *rk = NULL;
148
2
    int rklen;
149
2
    ASN1_STRING *str;
150
2
    int strtype;
151
152
2
    if (!rsa_param_encode(pkey, &str, &strtype))
153
0
        return 0;
154
2
    rklen = i2d_RSAPrivateKey(pkey->pkey.rsa, &rk);
155
156
2
    if (rklen <= 0) {
157
0
        ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
158
0
        ASN1_STRING_free(str);
159
0
        return 0;
160
0
    }
161
162
2
    if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(pkey->ameth->pkey_id), 0,
163
2
                         strtype, str, rk, rklen)) {
164
0
        ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
165
0
        ASN1_STRING_free(str);
166
0
        OPENSSL_clear_free(rk, rklen);
167
0
        return 0;
168
0
    }
169
170
2
    return 1;
171
2
}
172
173
static int rsa_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8)
174
20
{
175
20
    int ret = 0;
176
20
    RSA *rsa = ossl_rsa_key_from_pkcs8(p8, NULL, NULL);
177
178
20
    if (rsa != NULL) {
179
5
        ret = 1;
180
5
        EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, rsa);
181
5
    }
182
20
    return ret;
183
20
}
184
185
static int int_rsa_size(const EVP_PKEY *pkey)
186
1.09k
{
187
1.09k
    return RSA_size(pkey->pkey.rsa);
188
1.09k
}
189
190
static int rsa_bits(const EVP_PKEY *pkey)
191
0
{
192
0
    return BN_num_bits(pkey->pkey.rsa->n);
193
0
}
194
195
static int rsa_security_bits(const EVP_PKEY *pkey)
196
0
{
197
0
    return RSA_security_bits(pkey->pkey.rsa);
198
0
}
199
200
static void int_rsa_free(EVP_PKEY *pkey)
201
188k
{
202
188k
    RSA_free(pkey->pkey.rsa);
203
188k
}
204
205
static int rsa_pss_param_print(BIO *bp, int pss_key, RSA_PSS_PARAMS *pss,
206
                               int indent)
207
3.10k
{
208
3.10k
    int rv = 0;
209
3.10k
    X509_ALGOR *maskHash = NULL;
210
211
3.10k
    if (!BIO_indent(bp, indent, 128))
212
0
        goto err;
213
3.10k
    if (pss_key) {
214
2
        if (pss == NULL) {
215
0
            if (BIO_puts(bp, "No PSS parameter restrictions\n") <= 0)
216
0
                return 0;
217
0
            return 1;
218
2
        } else {
219
2
            if (BIO_puts(bp, "PSS parameter restrictions:") <= 0)
220
0
                return 0;
221
2
        }
222
3.10k
    } else if (pss == NULL) {
223
2.17k
        if (BIO_puts(bp,"(INVALID PSS PARAMETERS)\n") <= 0)
224
0
            return 0;
225
2.17k
        return 1;
226
2.17k
    }
227
931
    if (BIO_puts(bp, "\n") <= 0)
228
0
        goto err;
229
931
    if (pss_key)
230
2
        indent += 2;
231
931
    if (!BIO_indent(bp, indent, 128))
232
0
        goto err;
233
931
    if (BIO_puts(bp, "Hash Algorithm: ") <= 0)
234
0
        goto err;
235
236
931
    if (pss->hashAlgorithm) {
237
133
        if (i2a_ASN1_OBJECT(bp, pss->hashAlgorithm->algorithm) <= 0)
238
0
            goto err;
239
798
    } else if (BIO_puts(bp, "sha1 (default)") <= 0) {
240
0
        goto err;
241
0
    }
242
243
931
    if (BIO_puts(bp, "\n") <= 0)
244
0
        goto err;
245
246
931
    if (!BIO_indent(bp, indent, 128))
247
0
        goto err;
248
249
931
    if (BIO_puts(bp, "Mask Algorithm: ") <= 0)
250
0
        goto err;
251
931
    if (pss->maskGenAlgorithm) {
252
303
        if (i2a_ASN1_OBJECT(bp, pss->maskGenAlgorithm->algorithm) <= 0)
253
0
            goto err;
254
303
        if (BIO_puts(bp, " with ") <= 0)
255
0
            goto err;
256
303
        maskHash = ossl_x509_algor_mgf1_decode(pss->maskGenAlgorithm);
257
303
        if (maskHash != NULL) {
258
303
            if (i2a_ASN1_OBJECT(bp, maskHash->algorithm) <= 0)
259
0
                goto err;
260
303
        } else if (BIO_puts(bp, "INVALID") <= 0) {
261
0
            goto err;
262
0
        }
263
628
    } else if (BIO_puts(bp, "mgf1 with sha1 (default)") <= 0) {
264
0
        goto err;
265
0
    }
266
931
    BIO_puts(bp, "\n");
267
268
931
    if (!BIO_indent(bp, indent, 128))
269
0
        goto err;
270
931
    if (BIO_printf(bp, "%s Salt Length: 0x", pss_key ? "Minimum" : "") <= 0)
271
0
        goto err;
272
931
    if (pss->saltLength) {
273
87
        if (i2a_ASN1_INTEGER(bp, pss->saltLength) <= 0)
274
0
            goto err;
275
844
    } else if (BIO_puts(bp, "14 (default)") <= 0) {
276
0
        goto err;
277
0
    }
278
931
    BIO_puts(bp, "\n");
279
280
931
    if (!BIO_indent(bp, indent, 128))
281
0
        goto err;
282
931
    if (BIO_puts(bp, "Trailer Field: 0x") <= 0)
283
0
        goto err;
284
931
    if (pss->trailerField) {
285
38
        if (i2a_ASN1_INTEGER(bp, pss->trailerField) <= 0)
286
0
            goto err;
287
893
    } else if (BIO_puts(bp, "01 (default)") <= 0) {
288
0
        goto err;
289
0
    }
290
931
    BIO_puts(bp, "\n");
291
292
931
    rv = 1;
293
294
931
 err:
295
931
    X509_ALGOR_free(maskHash);
296
931
    return rv;
297
298
931
}
299
300
static int pkey_rsa_print(BIO *bp, const EVP_PKEY *pkey, int off, int priv)
301
481
{
302
481
    const RSA *x = pkey->pkey.rsa;
303
481
    char *str;
304
481
    const char *s;
305
481
    int ret = 0, mod_len = 0, ex_primes;
306
307
481
    if (x->n != NULL)
308
481
        mod_len = BN_num_bits(x->n);
309
481
    ex_primes = sk_RSA_PRIME_INFO_num(x->prime_infos);
310
311
481
    if (!BIO_indent(bp, off, 128))
312
0
        goto err;
313
314
481
    if (BIO_printf(bp, "%s ", pkey_is_pss(pkey) ?  "RSA-PSS" : "RSA") <= 0)
315
0
        goto err;
316
317
481
    if (priv && x->d) {
318
481
        if (BIO_printf(bp, "Private-Key: (%d bit, %d primes)\n",
319
481
                       mod_len, ex_primes <= 0 ? 2 : ex_primes + 2) <= 0)
320
0
            goto err;
321
481
        str = "modulus:";
322
481
        s = "publicExponent:";
323
481
    } else {
324
0
        if (BIO_printf(bp, "Public-Key: (%d bit)\n", mod_len) <= 0)
325
0
            goto err;
326
0
        str = "Modulus:";
327
0
        s = "Exponent:";
328
0
    }
329
481
    if (!ASN1_bn_print(bp, str, x->n, NULL, off))
330
0
        goto err;
331
481
    if (!ASN1_bn_print(bp, s, x->e, NULL, off))
332
0
        goto err;
333
481
    if (priv) {
334
481
        int i;
335
336
481
        if (!ASN1_bn_print(bp, "privateExponent:", x->d, NULL, off))
337
0
            goto err;
338
481
        if (!ASN1_bn_print(bp, "prime1:", x->p, NULL, off))
339
0
            goto err;
340
481
        if (!ASN1_bn_print(bp, "prime2:", x->q, NULL, off))
341
0
            goto err;
342
481
        if (!ASN1_bn_print(bp, "exponent1:", x->dmp1, NULL, off))
343
0
            goto err;
344
481
        if (!ASN1_bn_print(bp, "exponent2:", x->dmq1, NULL, off))
345
0
            goto err;
346
481
        if (!ASN1_bn_print(bp, "coefficient:", x->iqmp, NULL, off))
347
0
            goto err;
348
29.4k
        for (i = 0; i < sk_RSA_PRIME_INFO_num(x->prime_infos); i++) {
349
            /* print multi-prime info */
350
28.9k
            BIGNUM *bn = NULL;
351
28.9k
            RSA_PRIME_INFO *pinfo;
352
28.9k
            int j;
353
354
28.9k
            pinfo = sk_RSA_PRIME_INFO_value(x->prime_infos, i);
355
115k
            for (j = 0; j < 3; j++) {
356
86.8k
                if (!BIO_indent(bp, off, 128))
357
0
                    goto err;
358
86.8k
                switch (j) {
359
28.9k
                case 0:
360
28.9k
                    if (BIO_printf(bp, "prime%d:", i + 3) <= 0)
361
0
                        goto err;
362
28.9k
                    bn = pinfo->r;
363
28.9k
                    break;
364
28.9k
                case 1:
365
28.9k
                    if (BIO_printf(bp, "exponent%d:", i + 3) <= 0)
366
0
                        goto err;
367
28.9k
                    bn = pinfo->d;
368
28.9k
                    break;
369
28.9k
                case 2:
370
28.9k
                    if (BIO_printf(bp, "coefficient%d:", i + 3) <= 0)
371
0
                        goto err;
372
28.9k
                    bn = pinfo->t;
373
28.9k
                    break;
374
0
                default:
375
0
                    break;
376
86.8k
                }
377
86.8k
                if (!ASN1_bn_print(bp, "", bn, NULL, off))
378
0
                    goto err;
379
86.8k
            }
380
28.9k
        }
381
481
    }
382
481
    if (pkey_is_pss(pkey) && !rsa_pss_param_print(bp, 1, x->pss, off))
383
0
        goto err;
384
481
    ret = 1;
385
481
 err:
386
481
    return ret;
387
481
}
388
389
static int rsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
390
                         ASN1_PCTX *ctx)
391
0
{
392
0
    return pkey_rsa_print(bp, pkey, indent, 0);
393
0
}
394
395
static int rsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
396
                          ASN1_PCTX *ctx)
397
481
{
398
481
    return pkey_rsa_print(bp, pkey, indent, 1);
399
481
}
400
401
static int rsa_sig_print(BIO *bp, const X509_ALGOR *sigalg,
402
                         const ASN1_STRING *sig, int indent, ASN1_PCTX *pctx)
403
7.22k
{
404
7.22k
    if (OBJ_obj2nid(sigalg->algorithm) == EVP_PKEY_RSA_PSS) {
405
3.10k
        int rv;
406
3.10k
        RSA_PSS_PARAMS *pss = ossl_rsa_pss_decode(sigalg);
407
408
3.10k
        rv = rsa_pss_param_print(bp, 0, pss, indent);
409
3.10k
        RSA_PSS_PARAMS_free(pss);
410
3.10k
        if (!rv)
411
0
            return 0;
412
4.12k
    } else if (BIO_puts(bp, "\n") <= 0) {
413
0
        return 0;
414
0
    }
415
7.22k
    if (sig)
416
3.10k
        return X509_signature_dump(bp, sig, indent);
417
4.11k
    return 1;
418
7.22k
}
419
420
static int rsa_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
421
0
{
422
0
    const EVP_MD *md;
423
0
    const EVP_MD *mgf1md;
424
0
    int min_saltlen;
425
426
0
    switch (op) {
427
0
    case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
428
0
        if (pkey->pkey.rsa->pss != NULL) {
429
0
            if (!ossl_rsa_pss_get_param(pkey->pkey.rsa->pss, &md, &mgf1md,
430
0
                                        &min_saltlen)) {
431
0
                ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
432
0
                return 0;
433
0
            }
434
0
            *(int *)arg2 = EVP_MD_get_type(md);
435
            /* Return of 2 indicates this MD is mandatory */
436
0
            return 2;
437
0
        }
438
0
        *(int *)arg2 = NID_sha256;
439
0
        return 1;
440
441
0
    default:
442
0
        return -2;
443
0
    }
444
0
}
445
446
/*
447
 * Convert EVP_PKEY_CTX in PSS mode into corresponding algorithm parameter,
448
 * suitable for setting an AlgorithmIdentifier.
449
 */
450
451
static RSA_PSS_PARAMS *rsa_ctx_to_pss(EVP_PKEY_CTX *pkctx)
452
0
{
453
0
    const EVP_MD *sigmd, *mgf1md;
454
0
    EVP_PKEY *pk = EVP_PKEY_CTX_get0_pkey(pkctx);
455
0
    int saltlen;
456
457
0
    if (EVP_PKEY_CTX_get_signature_md(pkctx, &sigmd) <= 0)
458
0
        return NULL;
459
0
    if (EVP_PKEY_CTX_get_rsa_mgf1_md(pkctx, &mgf1md) <= 0)
460
0
        return NULL;
461
0
    if (EVP_PKEY_CTX_get_rsa_pss_saltlen(pkctx, &saltlen) <= 0)
462
0
        return NULL;
463
0
    if (saltlen == -1) {
464
0
        saltlen = EVP_MD_get_size(sigmd);
465
0
    } else if (saltlen == -2 || saltlen == -3) {
466
0
        saltlen = EVP_PKEY_get_size(pk) - EVP_MD_get_size(sigmd) - 2;
467
0
        if ((EVP_PKEY_get_bits(pk) & 0x7) == 1)
468
0
            saltlen--;
469
0
        if (saltlen < 0)
470
0
            return NULL;
471
0
    }
472
473
0
    return ossl_rsa_pss_params_create(sigmd, mgf1md, saltlen);
474
0
}
475
476
RSA_PSS_PARAMS *ossl_rsa_pss_params_create(const EVP_MD *sigmd,
477
                                           const EVP_MD *mgf1md, int saltlen)
478
0
{
479
0
    RSA_PSS_PARAMS *pss = RSA_PSS_PARAMS_new();
480
481
0
    if (pss == NULL)
482
0
        goto err;
483
0
    if (saltlen != 20) {
484
0
        pss->saltLength = ASN1_INTEGER_new();
485
0
        if (pss->saltLength == NULL)
486
0
            goto err;
487
0
        if (!ASN1_INTEGER_set(pss->saltLength, saltlen))
488
0
            goto err;
489
0
    }
490
0
    if (!ossl_x509_algor_new_from_md(&pss->hashAlgorithm, sigmd))
491
0
        goto err;
492
0
    if (mgf1md == NULL)
493
0
        mgf1md = sigmd;
494
0
    if (!ossl_x509_algor_md_to_mgf1(&pss->maskGenAlgorithm, mgf1md))
495
0
        goto err;
496
0
    if (!ossl_x509_algor_new_from_md(&pss->maskHash, mgf1md))
497
0
        goto err;
498
0
    return pss;
499
0
 err:
500
0
    RSA_PSS_PARAMS_free(pss);
501
0
    return NULL;
502
0
}
503
504
ASN1_STRING *ossl_rsa_ctx_to_pss_string(EVP_PKEY_CTX *pkctx)
505
0
{
506
0
    RSA_PSS_PARAMS *pss = rsa_ctx_to_pss(pkctx);
507
0
    ASN1_STRING *os;
508
509
0
    if (pss == NULL)
510
0
        return NULL;
511
512
0
    os = ASN1_item_pack(pss, ASN1_ITEM_rptr(RSA_PSS_PARAMS), NULL);
513
0
    RSA_PSS_PARAMS_free(pss);
514
0
    return os;
515
0
}
516
517
/*
518
 * From PSS AlgorithmIdentifier set public key parameters. If pkey isn't NULL
519
 * then the EVP_MD_CTX is setup and initialised. If it is NULL parameters are
520
 * passed to pkctx instead.
521
 */
522
523
int ossl_rsa_pss_to_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pkctx,
524
                        const X509_ALGOR *sigalg, EVP_PKEY *pkey)
525
4.10k
{
526
4.10k
    int rv = -1;
527
4.10k
    int saltlen;
528
4.10k
    const EVP_MD *mgf1md = NULL, *md = NULL;
529
4.10k
    RSA_PSS_PARAMS *pss;
530
531
    /* Sanity check: make sure it is PSS */
532
4.10k
    if (OBJ_obj2nid(sigalg->algorithm) != EVP_PKEY_RSA_PSS) {
533
0
        ERR_raise(ERR_LIB_RSA, RSA_R_UNSUPPORTED_SIGNATURE_TYPE);
534
0
        return -1;
535
0
    }
536
    /* Decode PSS parameters */
537
4.10k
    pss = ossl_rsa_pss_decode(sigalg);
538
539
4.10k
    if (!ossl_rsa_pss_get_param(pss, &md, &mgf1md, &saltlen)) {
540
153
        ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PSS_PARAMETERS);
541
153
        goto err;
542
153
    }
543
544
    /* We have all parameters now set up context */
545
3.95k
    if (pkey) {
546
3.95k
        if (!EVP_DigestVerifyInit(ctx, &pkctx, md, NULL, pkey))
547
13
            goto err;
548
3.95k
    } else {
549
0
        const EVP_MD *checkmd;
550
0
        if (EVP_PKEY_CTX_get_signature_md(pkctx, &checkmd) <= 0)
551
0
            goto err;
552
0
        if (EVP_MD_get_type(md) != EVP_MD_get_type(checkmd)) {
553
0
            ERR_raise(ERR_LIB_RSA, RSA_R_DIGEST_DOES_NOT_MATCH);
554
0
            goto err;
555
0
        }
556
0
    }
557
558
3.94k
    if (EVP_PKEY_CTX_set_rsa_padding(pkctx, RSA_PKCS1_PSS_PADDING) <= 0)
559
0
        goto err;
560
561
3.94k
    if (EVP_PKEY_CTX_set_rsa_pss_saltlen(pkctx, saltlen) <= 0)
562
0
        goto err;
563
564
3.94k
    if (EVP_PKEY_CTX_set_rsa_mgf1_md(pkctx, mgf1md) <= 0)
565
0
        goto err;
566
    /* Carry on */
567
3.94k
    rv = 1;
568
569
4.10k
 err:
570
4.10k
    RSA_PSS_PARAMS_free(pss);
571
4.10k
    return rv;
572
3.94k
}
573
574
static int rsa_pss_verify_param(const EVP_MD **pmd, const EVP_MD **pmgf1md,
575
                                int *psaltlen, int *ptrailerField)
576
4.32k
{
577
4.32k
    if (psaltlen != NULL && *psaltlen < 0) {
578
10
        ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_SALT_LENGTH);
579
10
        return 0;
580
10
    }
581
    /*
582
     * low-level routines support only trailer field 0xbc (value 1) and
583
     * PKCS#1 says we should reject any other value anyway.
584
     */
585
4.31k
    if (ptrailerField != NULL && *ptrailerField != 1) {
586
7
        ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_TRAILER);
587
7
        return 0;
588
7
    }
589
4.30k
    return 1;
590
4.31k
}
591
592
int ossl_rsa_pss_get_param(const RSA_PSS_PARAMS *pss, const EVP_MD **pmd,
593
                           const EVP_MD **pmgf1md, int *psaltlen)
594
5.14k
{
595
    /*
596
     * Callers do not care about the trailer field, and yet, we must
597
     * pass it from get_param to verify_param, since the latter checks
598
     * its value.
599
     *
600
     * When callers start caring, it's a simple thing to add another
601
     * argument to this function.
602
     */
603
5.14k
    int trailerField = 0;
604
605
5.14k
    return ossl_rsa_pss_get_param_unverified(pss, pmd, pmgf1md, psaltlen,
606
5.14k
                                             &trailerField)
607
5.14k
        && rsa_pss_verify_param(pmd, pmgf1md, psaltlen, &trailerField);
608
5.14k
}
609
610
/*
611
 * Customised RSA item verification routine. This is called when a signature
612
 * is encountered requiring special handling. We currently only handle PSS.
613
 */
614
615
static int rsa_item_verify(EVP_MD_CTX *ctx, const ASN1_ITEM *it,
616
                           const void *asn, const X509_ALGOR *sigalg,
617
                           const ASN1_BIT_STRING *sig, EVP_PKEY *pkey)
618
0
{
619
    /* Sanity check: make sure it is PSS */
620
0
    if (OBJ_obj2nid(sigalg->algorithm) != EVP_PKEY_RSA_PSS) {
621
0
        ERR_raise(ERR_LIB_RSA, RSA_R_UNSUPPORTED_SIGNATURE_TYPE);
622
0
        return -1;
623
0
    }
624
0
    if (ossl_rsa_pss_to_ctx(ctx, NULL, sigalg, pkey) > 0) {
625
        /* Carry on */
626
0
        return 2;
627
0
    }
628
0
    return -1;
629
0
}
630
631
static int rsa_item_sign(EVP_MD_CTX *ctx, const ASN1_ITEM *it, const void *asn,
632
                         X509_ALGOR *alg1, X509_ALGOR *alg2,
633
                         ASN1_BIT_STRING *sig)
634
0
{
635
0
    int pad_mode;
636
0
    EVP_PKEY_CTX *pkctx = EVP_MD_CTX_get_pkey_ctx(ctx);
637
638
0
    if (EVP_PKEY_CTX_get_rsa_padding(pkctx, &pad_mode) <= 0)
639
0
        return 0;
640
0
    if (pad_mode == RSA_PKCS1_PADDING)
641
0
        return 2;
642
0
    if (pad_mode == RSA_PKCS1_PSS_PADDING) {
643
0
        unsigned char aid[128];
644
0
        size_t aid_len = 0;
645
0
        OSSL_PARAM params[2];
646
647
0
        if (evp_pkey_ctx_is_legacy(pkctx)) {
648
            /* No provider -> we cannot query it for algorithm ID. */
649
0
            ASN1_STRING *os1 = NULL;
650
651
0
            os1 = ossl_rsa_ctx_to_pss_string(pkctx);
652
0
            if (os1 == NULL)
653
0
                return 0;
654
            /* Duplicate parameters if we have to */
655
0
            if (alg2 != NULL) {
656
0
                ASN1_STRING *os2 = ASN1_STRING_dup(os1);
657
658
0
                if (os2 == NULL) {
659
0
                    ASN1_STRING_free(os1);
660
0
                    return 0;
661
0
                }
662
0
                if (!X509_ALGOR_set0(alg2, OBJ_nid2obj(EVP_PKEY_RSA_PSS),
663
0
                                     V_ASN1_SEQUENCE, os2)) {
664
0
                    ASN1_STRING_free(os1);
665
0
                    ASN1_STRING_free(os2);
666
0
                    return 0;
667
0
                }
668
0
            }
669
0
            if (!X509_ALGOR_set0(alg1, OBJ_nid2obj(EVP_PKEY_RSA_PSS),
670
0
                                 V_ASN1_SEQUENCE, os1)) {
671
0
                    ASN1_STRING_free(os1);
672
0
                    return 0;
673
0
            }
674
0
            return 3;
675
0
        }
676
677
0
        params[0] = OSSL_PARAM_construct_octet_string(
678
0
            OSSL_SIGNATURE_PARAM_ALGORITHM_ID, aid, sizeof(aid));
679
0
        params[1] = OSSL_PARAM_construct_end();
680
681
0
        if (EVP_PKEY_CTX_get_params(pkctx, params) <= 0)
682
0
            return 0;
683
0
        if ((aid_len = params[0].return_size) == 0)
684
0
            return 0;
685
686
0
        if (alg1 != NULL) {
687
0
            const unsigned char *pp = aid;
688
689
0
            if (d2i_X509_ALGOR(&alg1, &pp, aid_len) == NULL)
690
0
                return 0;
691
0
        }
692
0
        if (alg2 != NULL) {
693
0
            const unsigned char *pp = aid;
694
695
0
            if (d2i_X509_ALGOR(&alg2, &pp, aid_len) == NULL)
696
0
                return 0;
697
0
        }
698
699
0
        return 3;
700
0
    }
701
0
    return 2;
702
0
}
703
704
static int rsa_sig_info_set(X509_SIG_INFO *siginf, const X509_ALGOR *sigalg,
705
                            const ASN1_STRING *sig)
706
552
{
707
552
    int rv = 0;
708
552
    int mdnid, saltlen;
709
552
    uint32_t flags;
710
552
    const EVP_MD *mgf1md = NULL, *md = NULL;
711
552
    RSA_PSS_PARAMS *pss;
712
552
    int secbits;
713
714
    /* Sanity check: make sure it is PSS */
715
552
    if (OBJ_obj2nid(sigalg->algorithm) != EVP_PKEY_RSA_PSS)
716
0
        return 0;
717
    /* Decode PSS parameters */
718
552
    pss = ossl_rsa_pss_decode(sigalg);
719
552
    if (!ossl_rsa_pss_get_param(pss, &md, &mgf1md, &saltlen))
720
436
        goto err;
721
116
    mdnid = EVP_MD_get_type(md);
722
    /*
723
     * For TLS need SHA256, SHA384 or SHA512, digest and MGF1 digest must
724
     * match and salt length must equal digest size
725
     */
726
116
    if ((mdnid == NID_sha256 || mdnid == NID_sha384 || mdnid == NID_sha512)
727
116
            && mdnid == EVP_MD_get_type(mgf1md)
728
116
            && saltlen == EVP_MD_get_size(md))
729
3
        flags = X509_SIG_INFO_TLS;
730
113
    else
731
113
        flags = 0;
732
    /* Note: security bits half number of digest bits */
733
116
    secbits = EVP_MD_get_size(md) * 4;
734
    /*
735
     * SHA1 and MD5 are known to be broken. Reduce security bits so that
736
     * they're no longer accepted at security level 1. The real values don't
737
     * really matter as long as they're lower than 80, which is our security
738
     * level 1.
739
     * https://eprint.iacr.org/2020/014 puts a chosen-prefix attack for SHA1 at
740
     * 2^63.4
741
     * https://documents.epfl.ch/users/l/le/lenstra/public/papers/lat.pdf
742
     * puts a chosen-prefix attack for MD5 at 2^39.
743
     */
744
116
    if (mdnid == NID_sha1)
745
97
        secbits = 64;
746
19
    else if (mdnid == NID_md5_sha1)
747
0
        secbits = 68;
748
19
    else if (mdnid == NID_md5)
749
2
        secbits = 39;
750
116
    X509_SIG_INFO_set(siginf, mdnid, EVP_PKEY_RSA_PSS, secbits,
751
116
                      flags);
752
116
    rv = 1;
753
552
    err:
754
552
    RSA_PSS_PARAMS_free(pss);
755
552
    return rv;
756
116
}
757
758
static int rsa_pkey_check(const EVP_PKEY *pkey)
759
0
{
760
0
    return RSA_check_key_ex(pkey->pkey.rsa, NULL);
761
0
}
762
763
static size_t rsa_pkey_dirty_cnt(const EVP_PKEY *pkey)
764
148k
{
765
148k
    return pkey->pkey.rsa->dirty_cnt;
766
148k
}
767
768
/*
769
 * There is no need to do RSA_test_flags(rsa, RSA_FLAG_TYPE_RSASSAPSS)
770
 * checks in this method since the caller tests EVP_KEYMGMT_is_a() first.
771
 */
772
static int rsa_int_export_to(const EVP_PKEY *from, int rsa_type,
773
                             void *to_keydata,
774
                             OSSL_FUNC_keymgmt_import_fn *importer,
775
                             OSSL_LIB_CTX *libctx, const char *propq)
776
25.4k
{
777
25.4k
    RSA *rsa = from->pkey.rsa;
778
25.4k
    OSSL_PARAM_BLD *tmpl = OSSL_PARAM_BLD_new();
779
25.4k
    OSSL_PARAM *params = NULL;
780
25.4k
    int selection = 0;
781
25.4k
    int rv = 0;
782
783
25.4k
    if (tmpl == NULL)
784
0
        return 0;
785
    /* Public parameters must always be present */
786
25.4k
    if (RSA_get0_n(rsa) == NULL || RSA_get0_e(rsa) == NULL)
787
0
        goto err;
788
789
25.4k
    if (!ossl_rsa_todata(rsa, tmpl, NULL, 1))
790
0
        goto err;
791
792
25.4k
    selection |= OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
793
25.4k
    if (RSA_get0_d(rsa) != NULL)
794
25.4k
        selection |= OSSL_KEYMGMT_SELECT_PRIVATE_KEY;
795
796
25.4k
    if (rsa->pss != NULL) {
797
0
        const EVP_MD *md = NULL, *mgf1md = NULL;
798
0
        int md_nid, mgf1md_nid, saltlen, trailerfield;
799
0
        RSA_PSS_PARAMS_30 pss_params;
800
801
0
        if (!ossl_rsa_pss_get_param_unverified(rsa->pss, &md, &mgf1md,
802
0
                                               &saltlen, &trailerfield))
803
0
            goto err;
804
0
        md_nid = EVP_MD_get_type(md);
805
0
        mgf1md_nid = EVP_MD_get_type(mgf1md);
806
0
        if (!ossl_rsa_pss_params_30_set_defaults(&pss_params)
807
0
            || !ossl_rsa_pss_params_30_set_hashalg(&pss_params, md_nid)
808
0
            || !ossl_rsa_pss_params_30_set_maskgenhashalg(&pss_params,
809
0
                                                          mgf1md_nid)
810
0
            || !ossl_rsa_pss_params_30_set_saltlen(&pss_params, saltlen)
811
0
            || !ossl_rsa_pss_params_30_todata(&pss_params, tmpl, NULL))
812
0
            goto err;
813
0
        selection |= OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS;
814
0
    }
815
816
25.4k
    if ((params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL)
817
0
        goto err;
818
819
    /* We export, the provider imports */
820
25.4k
    rv = importer(to_keydata, selection, params);
821
822
25.4k
 err:
823
25.4k
    OSSL_PARAM_free(params);
824
25.4k
    OSSL_PARAM_BLD_free(tmpl);
825
25.4k
    return rv;
826
25.4k
}
827
828
static int rsa_int_import_from(const OSSL_PARAM params[], void *vpctx,
829
                               int rsa_type)
830
4.54k
{
831
4.54k
    EVP_PKEY_CTX *pctx = vpctx;
832
4.54k
    EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(pctx);
833
4.54k
    RSA *rsa = ossl_rsa_new_with_ctx(pctx->libctx);
834
4.54k
    RSA_PSS_PARAMS_30 rsa_pss_params = { 0, };
835
4.54k
    int pss_defaults_set = 0;
836
4.54k
    int ok = 0;
837
838
4.54k
    if (rsa == NULL) {
839
0
        ERR_raise(ERR_LIB_DH, ERR_R_MALLOC_FAILURE);
840
0
        return 0;
841
0
    }
842
843
4.54k
    RSA_clear_flags(rsa, RSA_FLAG_TYPE_MASK);
844
4.54k
    RSA_set_flags(rsa, rsa_type);
845
846
4.54k
    if (!ossl_rsa_pss_params_30_fromdata(&rsa_pss_params, &pss_defaults_set,
847
4.54k
                                         params, pctx->libctx))
848
0
        goto err;
849
850
4.54k
    switch (rsa_type) {
851
4.54k
    case RSA_FLAG_TYPE_RSA:
852
        /*
853
         * Were PSS parameters filled in?
854
         * In that case, something's wrong
855
         */
856
4.54k
        if (!ossl_rsa_pss_params_30_is_unrestricted(&rsa_pss_params))
857
0
            goto err;
858
4.54k
        break;
859
4.54k
    case RSA_FLAG_TYPE_RSASSAPSS:
860
        /*
861
         * Were PSS parameters filled in?  In that case, create the old
862
         * RSA_PSS_PARAMS structure.  Otherwise, this is an unrestricted key.
863
         */
864
0
        if (!ossl_rsa_pss_params_30_is_unrestricted(&rsa_pss_params)) {
865
            /* Create the older RSA_PSS_PARAMS from RSA_PSS_PARAMS_30 data */
866
0
            int mdnid = ossl_rsa_pss_params_30_hashalg(&rsa_pss_params);
867
0
            int mgf1mdnid = ossl_rsa_pss_params_30_maskgenhashalg(&rsa_pss_params);
868
0
            int saltlen = ossl_rsa_pss_params_30_saltlen(&rsa_pss_params);
869
0
            const EVP_MD *md = EVP_get_digestbynid(mdnid);
870
0
            const EVP_MD *mgf1md = EVP_get_digestbynid(mgf1mdnid);
871
872
0
            if ((rsa->pss = ossl_rsa_pss_params_create(md, mgf1md,
873
0
                                                       saltlen)) == NULL)
874
0
                goto err;
875
0
        }
876
0
        break;
877
0
    default:
878
        /* RSA key sub-types we don't know how to handle yet */
879
0
        goto err;
880
4.54k
    }
881
882
4.54k
    if (!ossl_rsa_fromdata(rsa, params, 1))
883
0
        goto err;
884
885
4.54k
    switch (rsa_type) {
886
4.54k
    case RSA_FLAG_TYPE_RSA:
887
4.54k
        ok = EVP_PKEY_assign_RSA(pkey, rsa);
888
4.54k
        break;
889
0
    case RSA_FLAG_TYPE_RSASSAPSS:
890
0
        ok = EVP_PKEY_assign(pkey, EVP_PKEY_RSA_PSS, rsa);
891
0
        break;
892
4.54k
    }
893
894
4.54k
 err:
895
4.54k
    if (!ok)
896
0
        RSA_free(rsa);
897
4.54k
    return ok;
898
4.54k
}
899
900
static int rsa_pkey_export_to(const EVP_PKEY *from, void *to_keydata,
901
                              OSSL_FUNC_keymgmt_import_fn *importer,
902
                              OSSL_LIB_CTX *libctx, const char *propq)
903
25.4k
{
904
25.4k
    return rsa_int_export_to(from, RSA_FLAG_TYPE_RSA, to_keydata,
905
25.4k
                             importer, libctx, propq);
906
25.4k
}
907
908
static int rsa_pss_pkey_export_to(const EVP_PKEY *from, void *to_keydata,
909
                                  OSSL_FUNC_keymgmt_import_fn *importer,
910
                                  OSSL_LIB_CTX *libctx, const char *propq)
911
0
{
912
0
    return rsa_int_export_to(from, RSA_FLAG_TYPE_RSASSAPSS, to_keydata,
913
0
                             importer, libctx, propq);
914
0
}
915
916
static int rsa_pkey_import_from(const OSSL_PARAM params[], void *vpctx)
917
4.54k
{
918
4.54k
    return rsa_int_import_from(params, vpctx, RSA_FLAG_TYPE_RSA);
919
4.54k
}
920
921
static int rsa_pss_pkey_import_from(const OSSL_PARAM params[], void *vpctx)
922
0
{
923
0
    return rsa_int_import_from(params, vpctx, RSA_FLAG_TYPE_RSASSAPSS);
924
0
}
925
926
static int rsa_pkey_copy(EVP_PKEY *to, EVP_PKEY *from)
927
0
{
928
0
    RSA *rsa = from->pkey.rsa;
929
0
    RSA *dupkey = NULL;
930
0
    int ret;
931
932
0
    if (rsa != NULL) {
933
0
        dupkey = ossl_rsa_dup(rsa, OSSL_KEYMGMT_SELECT_ALL);
934
0
        if (dupkey == NULL)
935
0
            return 0;
936
0
    }
937
938
0
    ret = EVP_PKEY_assign(to, from->type, dupkey);
939
0
    if (!ret)
940
0
        RSA_free(dupkey);
941
0
    return ret;
942
0
}
943
944
const EVP_PKEY_ASN1_METHOD ossl_rsa_asn1_meths[2] = {
945
    {
946
     EVP_PKEY_RSA,
947
     EVP_PKEY_RSA,
948
     ASN1_PKEY_SIGPARAM_NULL,
949
950
     "RSA",
951
     "OpenSSL RSA method",
952
953
     rsa_pub_decode,
954
     rsa_pub_encode,
955
     rsa_pub_cmp,
956
     rsa_pub_print,
957
958
     rsa_priv_decode,
959
     rsa_priv_encode,
960
     rsa_priv_print,
961
962
     int_rsa_size,
963
     rsa_bits,
964
     rsa_security_bits,
965
966
     0, 0, 0, 0, 0, 0,
967
968
     rsa_sig_print,
969
     int_rsa_free,
970
     rsa_pkey_ctrl,
971
     old_rsa_priv_decode,
972
     old_rsa_priv_encode,
973
     rsa_item_verify,
974
     rsa_item_sign,
975
     rsa_sig_info_set,
976
     rsa_pkey_check,
977
978
     0, 0,
979
     0, 0, 0, 0,
980
981
     rsa_pkey_dirty_cnt,
982
     rsa_pkey_export_to,
983
     rsa_pkey_import_from,
984
     rsa_pkey_copy
985
    },
986
987
    {
988
     EVP_PKEY_RSA2,
989
     EVP_PKEY_RSA,
990
     ASN1_PKEY_ALIAS}
991
};
992
993
const EVP_PKEY_ASN1_METHOD ossl_rsa_pss_asn1_meth = {
994
     EVP_PKEY_RSA_PSS,
995
     EVP_PKEY_RSA_PSS,
996
     ASN1_PKEY_SIGPARAM_NULL,
997
998
     "RSA-PSS",
999
     "OpenSSL RSA-PSS method",
1000
1001
     rsa_pub_decode,
1002
     rsa_pub_encode,
1003
     rsa_pub_cmp,
1004
     rsa_pub_print,
1005
1006
     rsa_priv_decode,
1007
     rsa_priv_encode,
1008
     rsa_priv_print,
1009
1010
     int_rsa_size,
1011
     rsa_bits,
1012
     rsa_security_bits,
1013
1014
     0, 0, 0, 0, 0, 0,
1015
1016
     rsa_sig_print,
1017
     int_rsa_free,
1018
     rsa_pkey_ctrl,
1019
     0, 0,
1020
     rsa_item_verify,
1021
     rsa_item_sign,
1022
     rsa_sig_info_set,
1023
     rsa_pkey_check,
1024
1025
     0, 0,
1026
     0, 0, 0, 0,
1027
1028
     rsa_pkey_dirty_cnt,
1029
     rsa_pss_pkey_export_to,
1030
     rsa_pss_pkey_import_from,
1031
     rsa_pkey_copy
1032
};