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