Coverage Report

Created: 2026-09-12 06:55

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