]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/rsa/rsa_ameth.c
RSA: Be less strict on PSS parameters when exporting to provider
[thirdparty/openssl.git] / crypto / rsa / rsa_ameth.c
CommitLineData
0f113f3e 1/*
33388b44 2 * Copyright 2006-2020 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>
3c27208f 21#include <openssl/cms.h>
29be6023 22#include <openssl/core_names.h>
96ebe52e 23#include <openssl/param_build.h>
25f2138b
DMSP
24#include "crypto/asn1.h"
25#include "crypto/evp.h"
29be6023 26#include "crypto/rsa.h"
706457b7 27#include "rsa_local.h"
448be743 28
e968561d 29#ifndef OPENSSL_NO_CMS
0574cadf
DSH
30static int rsa_cms_sign(CMS_SignerInfo *si);
31static int rsa_cms_verify(CMS_SignerInfo *si);
32static int rsa_cms_decrypt(CMS_RecipientInfo *ri);
33static int rsa_cms_encrypt(CMS_RecipientInfo *ri);
e968561d 34#endif
0574cadf 35
b35b8d11 36static RSA_PSS_PARAMS *rsa_pss_decode(const X509_ALGOR *alg);
a6495479 37static int rsa_sync_to_pss_params_30(RSA *rsa);
b35b8d11 38
42009ae8
DSH
39/* Set any parameters associated with pkey */
40static int rsa_param_encode(const EVP_PKEY *pkey,
41 ASN1_STRING **pstr, int *pstrtype)
42{
43 const RSA *rsa = pkey->pkey.rsa;
52ad523c 44
42009ae8
DSH
45 *pstr = NULL;
46 /* If RSA it's just NULL type */
484d1a73 47 if (RSA_test_flags(rsa, RSA_FLAG_TYPE_MASK) != RSA_FLAG_TYPE_RSASSAPSS) {
42009ae8
DSH
48 *pstrtype = V_ASN1_NULL;
49 return 1;
50 }
51 /* If no PSS parameters we omit parameters entirely */
52 if (rsa->pss == NULL) {
53 *pstrtype = V_ASN1_UNDEF;
54 return 1;
55 }
56 /* Encode PSS parameters */
f291138b 57 if (ASN1_item_pack(rsa->pss, ASN1_ITEM_rptr(RSA_PSS_PARAMS), pstr) == NULL)
42009ae8 58 return 0;
42009ae8
DSH
59
60 *pstrtype = V_ASN1_SEQUENCE;
61 return 1;
62}
63/* Decode any parameters and set them in RSA structure */
64static int rsa_param_decode(RSA *rsa, const X509_ALGOR *alg)
65{
66 const ASN1_OBJECT *algoid;
67 const void *algp;
68 int algptype;
69
70 X509_ALGOR_get0(&algoid, &algptype, &algp, alg);
1f483a69 71 if (OBJ_obj2nid(algoid) != EVP_PKEY_RSA_PSS)
42009ae8
DSH
72 return 1;
73 if (algptype == V_ASN1_UNDEF)
74 return 1;
635fe50f
DSH
75 if (algptype != V_ASN1_SEQUENCE) {
76 RSAerr(RSA_F_RSA_PARAM_DECODE, RSA_R_INVALID_PSS_PARAMETERS);
42009ae8 77 return 0;
635fe50f 78 }
b35b8d11 79 rsa->pss = rsa_pss_decode(alg);
42009ae8
DSH
80 if (rsa->pss == NULL)
81 return 0;
a6495479
RL
82 if (!rsa_sync_to_pss_params_30(rsa))
83 return 0;
42009ae8
DSH
84 return 1;
85}
86
6f81892e 87static int rsa_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
0f113f3e
MC
88{
89 unsigned char *penc = NULL;
90 int penclen;
42009ae8
DSH
91 ASN1_STRING *str;
92 int strtype;
52ad523c 93
42009ae8
DSH
94 if (!rsa_param_encode(pkey, &str, &strtype))
95 return 0;
0f113f3e
MC
96 penclen = i2d_RSAPublicKey(pkey->pkey.rsa, &penc);
97 if (penclen <= 0)
98 return 0;
42009ae8
DSH
99 if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(pkey->ameth->pkey_id),
100 strtype, str, penc, penclen))
0f113f3e
MC
101 return 1;
102
103 OPENSSL_free(penc);
104 return 0;
105}
448be743 106
7674e923 107static int rsa_pub_decode(EVP_PKEY *pkey, const X509_PUBKEY *pubkey)
0f113f3e
MC
108{
109 const unsigned char *p;
110 int pklen;
42009ae8 111 X509_ALGOR *alg;
0f113f3e 112 RSA *rsa = NULL;
75ebbd9a 113
42009ae8 114 if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &alg, pubkey))
0f113f3e 115 return 0;
75ebbd9a 116 if ((rsa = d2i_RSAPublicKey(NULL, &p, pklen)) == NULL) {
0f113f3e
MC
117 RSAerr(RSA_F_RSA_PUB_DECODE, ERR_R_RSA_LIB);
118 return 0;
119 }
42009ae8
DSH
120 if (!rsa_param_decode(rsa, alg)) {
121 RSA_free(rsa);
122 return 0;
123 }
a6495479
RL
124
125 RSA_clear_flags(rsa, RSA_FLAG_TYPE_MASK);
126 switch (pkey->ameth->pkey_id) {
127 case EVP_PKEY_RSA:
128 RSA_set_flags(rsa, RSA_FLAG_TYPE_RSA);
129 break;
130 case EVP_PKEY_RSA_PSS:
131 RSA_set_flags(rsa, RSA_FLAG_TYPE_RSASSAPSS);
132 break;
133 default:
134 /* Leave the type bits zero */
135 break;
136 }
137
1f483a69
BE
138 if (!EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, rsa)) {
139 RSA_free(rsa);
140 return 0;
141 }
0f113f3e
MC
142 return 1;
143}
448be743 144
6f81892e 145static int rsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
0f113f3e
MC
146{
147 if (BN_cmp(b->pkey.rsa->n, a->pkey.rsa->n) != 0
148 || BN_cmp(b->pkey.rsa->e, a->pkey.rsa->e) != 0)
149 return 0;
150 return 1;
151}
6f81892e 152
e4263314 153static int old_rsa_priv_decode(EVP_PKEY *pkey,
0f113f3e
MC
154 const unsigned char **pder, int derlen)
155{
156 RSA *rsa;
75ebbd9a
RS
157
158 if ((rsa = d2i_RSAPrivateKey(NULL, pder, derlen)) == NULL) {
0f113f3e
MC
159 RSAerr(RSA_F_OLD_RSA_PRIV_DECODE, ERR_R_RSA_LIB);
160 return 0;
161 }
faa02fe2 162 EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, rsa);
0f113f3e
MC
163 return 1;
164}
448be743 165
e4263314 166static int old_rsa_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
0f113f3e
MC
167{
168 return i2d_RSAPrivateKey(pkey->pkey.rsa, pder);
169}
e4263314 170
6f81892e 171static int rsa_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
0f113f3e
MC
172{
173 unsigned char *rk = NULL;
174 int rklen;
42009ae8
DSH
175 ASN1_STRING *str;
176 int strtype;
52ad523c 177
42009ae8
DSH
178 if (!rsa_param_encode(pkey, &str, &strtype))
179 return 0;
0f113f3e
MC
180 rklen = i2d_RSAPrivateKey(pkey->pkey.rsa, &rk);
181
182 if (rklen <= 0) {
183 RSAerr(RSA_F_RSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
285c7d9c 184 ASN1_STRING_free(str);
0f113f3e
MC
185 return 0;
186 }
187
faa02fe2 188 if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(pkey->ameth->pkey_id), 0,
42009ae8 189 strtype, str, rk, rklen)) {
0f113f3e 190 RSAerr(RSA_F_RSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
285c7d9c 191 ASN1_STRING_free(str);
0f113f3e
MC
192 return 0;
193 }
194
195 return 1;
196}
448be743 197
245c6bc3 198static int rsa_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8)
0f113f3e
MC
199{
200 const unsigned char *p;
42009ae8 201 RSA *rsa;
0f113f3e 202 int pklen;
42009ae8
DSH
203 const X509_ALGOR *alg;
204
205 if (!PKCS8_pkey_get0(NULL, &p, &pklen, &alg, p8))
206 return 0;
207 rsa = d2i_RSAPrivateKey(NULL, &p, pklen);
208 if (rsa == NULL) {
209 RSAerr(RSA_F_RSA_PRIV_DECODE, ERR_R_RSA_LIB);
210 return 0;
211 }
212 if (!rsa_param_decode(rsa, alg)) {
213 RSA_free(rsa);
0f113f3e 214 return 0;
42009ae8 215 }
484d1a73
RL
216
217 RSA_clear_flags(rsa, RSA_FLAG_TYPE_MASK);
218 switch (pkey->ameth->pkey_id) {
219 case EVP_PKEY_RSA:
220 RSA_set_flags(rsa, RSA_FLAG_TYPE_RSA);
221 break;
222 case EVP_PKEY_RSA_PSS:
223 RSA_set_flags(rsa, RSA_FLAG_TYPE_RSASSAPSS);
224 break;
225 default:
226 /* Leave the type bits zero */
227 break;
228 }
229
42009ae8
DSH
230 EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, rsa);
231 return 1;
0f113f3e 232}
e4263314 233
6f81892e 234static int int_rsa_size(const EVP_PKEY *pkey)
0f113f3e
MC
235{
236 return RSA_size(pkey->pkey.rsa);
237}
6f81892e
DSH
238
239static int rsa_bits(const EVP_PKEY *pkey)
0f113f3e
MC
240{
241 return BN_num_bits(pkey->pkey.rsa->n);
242}
6f81892e 243
2514fa79 244static int rsa_security_bits(const EVP_PKEY *pkey)
0f113f3e
MC
245{
246 return RSA_security_bits(pkey->pkey.rsa);
247}
2514fa79 248
6f81892e 249static void int_rsa_free(EVP_PKEY *pkey)
0f113f3e
MC
250{
251 RSA_free(pkey->pkey.rsa);
252}
35208f36 253
9503ed8b
DSH
254static X509_ALGOR *rsa_mgf1_decode(X509_ALGOR *alg)
255{
256 if (OBJ_obj2nid(alg->algorithm) != NID_mgf1)
257 return NULL;
258 return ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(X509_ALGOR),
259 alg->parameter);
260}
261
262static int rsa_pss_param_print(BIO *bp, int pss_key, RSA_PSS_PARAMS *pss,
263 int indent)
264{
265 int rv = 0;
266 X509_ALGOR *maskHash = NULL;
52ad523c 267
9503ed8b
DSH
268 if (!BIO_indent(bp, indent, 128))
269 goto err;
270 if (pss_key) {
271 if (pss == NULL) {
272 if (BIO_puts(bp, "No PSS parameter restrictions\n") <= 0)
273 return 0;
274 return 1;
275 } else {
276 if (BIO_puts(bp, "PSS parameter restrictions:") <= 0)
277 return 0;
278 }
279 } else if (pss == NULL) {
280 if (BIO_puts(bp,"(INVALID PSS PARAMETERS)\n") <= 0)
281 return 0;
282 return 1;
283 }
284 if (BIO_puts(bp, "\n") <= 0)
285 goto err;
286 if (pss_key)
287 indent += 2;
288 if (!BIO_indent(bp, indent, 128))
289 goto err;
290 if (BIO_puts(bp, "Hash Algorithm: ") <= 0)
291 goto err;
292
293 if (pss->hashAlgorithm) {
294 if (i2a_ASN1_OBJECT(bp, pss->hashAlgorithm->algorithm) <= 0)
295 goto err;
90862ab4 296 } else if (BIO_puts(bp, "sha1 (default)") <= 0) {
9503ed8b 297 goto err;
90862ab4 298 }
9503ed8b
DSH
299
300 if (BIO_puts(bp, "\n") <= 0)
301 goto err;
302
303 if (!BIO_indent(bp, indent, 128))
304 goto err;
305
306 if (BIO_puts(bp, "Mask Algorithm: ") <= 0)
307 goto err;
308 if (pss->maskGenAlgorithm) {
309 if (i2a_ASN1_OBJECT(bp, pss->maskGenAlgorithm->algorithm) <= 0)
310 goto err;
311 if (BIO_puts(bp, " with ") <= 0)
312 goto err;
313 maskHash = rsa_mgf1_decode(pss->maskGenAlgorithm);
314 if (maskHash != NULL) {
315 if (i2a_ASN1_OBJECT(bp, maskHash->algorithm) <= 0)
316 goto err;
90862ab4 317 } else if (BIO_puts(bp, "INVALID") <= 0) {
9503ed8b 318 goto err;
90862ab4
PY
319 }
320 } else if (BIO_puts(bp, "mgf1 with sha1 (default)") <= 0) {
9503ed8b 321 goto err;
90862ab4 322 }
9503ed8b
DSH
323 BIO_puts(bp, "\n");
324
325 if (!BIO_indent(bp, indent, 128))
326 goto err;
327 if (BIO_printf(bp, "%s Salt Length: 0x", pss_key ? "Minimum" : "") <= 0)
328 goto err;
329 if (pss->saltLength) {
330 if (i2a_ASN1_INTEGER(bp, pss->saltLength) <= 0)
331 goto err;
90862ab4 332 } else if (BIO_puts(bp, "14 (default)") <= 0) {
9503ed8b 333 goto err;
90862ab4 334 }
9503ed8b
DSH
335 BIO_puts(bp, "\n");
336
337 if (!BIO_indent(bp, indent, 128))
338 goto err;
339 if (BIO_puts(bp, "Trailer Field: 0x") <= 0)
340 goto err;
341 if (pss->trailerField) {
342 if (i2a_ASN1_INTEGER(bp, pss->trailerField) <= 0)
343 goto err;
90862ab4 344 } else if (BIO_puts(bp, "BC (default)") <= 0) {
9503ed8b 345 goto err;
90862ab4 346 }
9503ed8b
DSH
347 BIO_puts(bp, "\n");
348
349 rv = 1;
350
351 err:
352 X509_ALGOR_free(maskHash);
353 return rv;
354
355}
356
357static int pkey_rsa_print(BIO *bp, const EVP_PKEY *pkey, int off, int priv)
0f113f3e 358{
9503ed8b 359 const RSA *x = pkey->pkey.rsa;
0f113f3e
MC
360 char *str;
361 const char *s;
665d899f 362 int ret = 0, mod_len = 0, ex_primes;
0f113f3e
MC
363
364 if (x->n != NULL)
365 mod_len = BN_num_bits(x->n);
665d899f 366 ex_primes = sk_RSA_PRIME_INFO_num(x->prime_infos);
0f113f3e
MC
367
368 if (!BIO_indent(bp, off, 128))
369 goto err;
370
87ee7b22 371 if (BIO_printf(bp, "%s ", pkey_is_pss(pkey) ? "RSA-PSS" : "RSA") <= 0)
9503ed8b
DSH
372 goto err;
373
0f113f3e 374 if (priv && x->d) {
665d899f
PY
375 if (BIO_printf(bp, "Private-Key: (%d bit, %d primes)\n",
376 mod_len, ex_primes <= 0 ? 2 : ex_primes + 2) <= 0)
0f113f3e
MC
377 goto err;
378 str = "modulus:";
379 s = "publicExponent:";
380 } else {
a773b52a 381 if (BIO_printf(bp, "Public-Key: (%d bit)\n", mod_len) <= 0)
0f113f3e
MC
382 goto err;
383 str = "Modulus:";
384 s = "Exponent:";
385 }
a773b52a 386 if (!ASN1_bn_print(bp, str, x->n, NULL, off))
0f113f3e 387 goto err;
a773b52a 388 if (!ASN1_bn_print(bp, s, x->e, NULL, off))
0f113f3e
MC
389 goto err;
390 if (priv) {
665d899f
PY
391 int i;
392
a773b52a 393 if (!ASN1_bn_print(bp, "privateExponent:", x->d, NULL, off))
0f113f3e 394 goto err;
a773b52a 395 if (!ASN1_bn_print(bp, "prime1:", x->p, NULL, off))
0f113f3e 396 goto err;
a773b52a 397 if (!ASN1_bn_print(bp, "prime2:", x->q, NULL, off))
0f113f3e 398 goto err;
a773b52a 399 if (!ASN1_bn_print(bp, "exponent1:", x->dmp1, NULL, off))
0f113f3e 400 goto err;
a773b52a 401 if (!ASN1_bn_print(bp, "exponent2:", x->dmq1, NULL, off))
0f113f3e 402 goto err;
a773b52a 403 if (!ASN1_bn_print(bp, "coefficient:", x->iqmp, NULL, off))
0f113f3e 404 goto err;
665d899f
PY
405 for (i = 0; i < sk_RSA_PRIME_INFO_num(x->prime_infos); i++) {
406 /* print multi-prime info */
407 BIGNUM *bn = NULL;
408 RSA_PRIME_INFO *pinfo;
409 int j;
410
411 pinfo = sk_RSA_PRIME_INFO_value(x->prime_infos, i);
412 for (j = 0; j < 3; j++) {
413 if (!BIO_indent(bp, off, 128))
414 goto err;
415 switch (j) {
416 case 0:
417 if (BIO_printf(bp, "prime%d:", i + 3) <= 0)
418 goto err;
419 bn = pinfo->r;
420 break;
421 case 1:
422 if (BIO_printf(bp, "exponent%d:", i + 3) <= 0)
423 goto err;
424 bn = pinfo->d;
425 break;
426 case 2:
427 if (BIO_printf(bp, "coefficient%d:", i + 3) <= 0)
428 goto err;
429 bn = pinfo->t;
430 break;
431 default:
432 break;
433 }
434 if (!ASN1_bn_print(bp, "", bn, NULL, off))
435 goto err;
436 }
437 }
0f113f3e 438 }
87ee7b22 439 if (pkey_is_pss(pkey) && !rsa_pss_param_print(bp, 1, x->pss, off))
9503ed8b 440 goto err;
0f113f3e
MC
441 ret = 1;
442 err:
9503ed8b 443 return ret;
0f113f3e 444}
35208f36
DSH
445
446static int rsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
0f113f3e
MC
447 ASN1_PCTX *ctx)
448{
9503ed8b 449 return pkey_rsa_print(bp, pkey, indent, 0);
0f113f3e 450}
35208f36
DSH
451
452static int rsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
0f113f3e
MC
453 ASN1_PCTX *ctx)
454{
9503ed8b 455 return pkey_rsa_print(bp, pkey, indent, 1);
0f113f3e 456}
0574cadf 457
6745a1ff 458static RSA_PSS_PARAMS *rsa_pss_decode(const X509_ALGOR *alg)
0f113f3e 459{
0f113f3e
MC
460 RSA_PSS_PARAMS *pss;
461
e93c8748
DSH
462 pss = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(RSA_PSS_PARAMS),
463 alg->parameter);
0f113f3e 464
6745a1ff 465 if (pss == NULL)
0f113f3e
MC
466 return NULL;
467
6745a1ff
DSH
468 if (pss->maskGenAlgorithm != NULL) {
469 pss->maskHash = rsa_mgf1_decode(pss->maskGenAlgorithm);
470 if (pss->maskHash == NULL) {
471 RSA_PSS_PARAMS_free(pss);
472 return NULL;
473 }
474 }
0f113f3e
MC
475
476 return pss;
477}
478
ff04bbe3 479static int rsa_sig_print(BIO *bp, const X509_ALGOR *sigalg,
0f113f3e
MC
480 const ASN1_STRING *sig, int indent, ASN1_PCTX *pctx)
481{
ffc6fad5 482 if (OBJ_obj2nid(sigalg->algorithm) == EVP_PKEY_RSA_PSS) {
0f113f3e 483 int rv;
52ad523c 484 RSA_PSS_PARAMS *pss = rsa_pss_decode(sigalg);
c82bafc5 485
9503ed8b 486 rv = rsa_pss_param_print(bp, 0, pss, indent);
25aaa98a 487 RSA_PSS_PARAMS_free(pss);
0f113f3e
MC
488 if (!rv)
489 return 0;
a4c467c9 490 } else if (BIO_puts(bp, "\n") <= 0) {
0f113f3e 491 return 0;
52ad523c 492 }
0f113f3e
MC
493 if (sig)
494 return X509_signature_dump(bp, sig, indent);
495 return 1;
496}
492a9e24
DSH
497
498static int rsa_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
0f113f3e
MC
499{
500 X509_ALGOR *alg = NULL;
9bcc9f97
MC
501 const EVP_MD *md;
502 const EVP_MD *mgf1md;
503 int min_saltlen;
52ad523c 504
0f113f3e
MC
505 switch (op) {
506
507 case ASN1_PKEY_CTRL_PKCS7_SIGN:
508 if (arg1 == 0)
509 PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, NULL, &alg);
510 break;
511
512 case ASN1_PKEY_CTRL_PKCS7_ENCRYPT:
53d2260c
DSH
513 if (pkey_is_pss(pkey))
514 return -2;
0f113f3e
MC
515 if (arg1 == 0)
516 PKCS7_RECIP_INFO_get0_alg(arg2, &alg);
517 break;
8931b30d 518#ifndef OPENSSL_NO_CMS
0f113f3e
MC
519 case ASN1_PKEY_CTRL_CMS_SIGN:
520 if (arg1 == 0)
521 return rsa_cms_sign(arg2);
522 else if (arg1 == 1)
523 return rsa_cms_verify(arg2);
524 break;
525
526 case ASN1_PKEY_CTRL_CMS_ENVELOPE:
53d2260c
DSH
527 if (pkey_is_pss(pkey))
528 return -2;
0f113f3e
MC
529 if (arg1 == 0)
530 return rsa_cms_encrypt(arg2);
531 else if (arg1 == 1)
532 return rsa_cms_decrypt(arg2);
533 break;
534
535 case ASN1_PKEY_CTRL_CMS_RI_TYPE:
53d2260c
DSH
536 if (pkey_is_pss(pkey))
537 return -2;
0f113f3e
MC
538 *(int *)arg2 = CMS_RECIPINFO_TRANS;
539 return 1;
8931b30d 540#endif
a78568b7 541
0f113f3e 542 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
9bcc9f97
MC
543 if (pkey->pkey.rsa->pss != NULL) {
544 if (!rsa_pss_get_param(pkey->pkey.rsa->pss, &md, &mgf1md,
545 &min_saltlen)) {
546 RSAerr(0, ERR_R_INTERNAL_ERROR);
547 return 0;
548 }
549 *(int *)arg2 = EVP_MD_type(md);
550 /* Return of 2 indicates this MD is mandatory */
551 return 2;
552 }
0f113f3e
MC
553 *(int *)arg2 = NID_sha256;
554 return 1;
03919683 555
0f113f3e
MC
556 default:
557 return -2;
492a9e24 558
0f113f3e 559 }
492a9e24 560
0f113f3e
MC
561 if (alg)
562 X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption), V_ASN1_NULL, 0);
4f1aa191 563
0f113f3e 564 return 1;
4f1aa191 565
0f113f3e 566}
492a9e24 567
0574cadf
DSH
568/* allocate and set algorithm ID from EVP_MD, default SHA1 */
569static int rsa_md_to_algor(X509_ALGOR **palg, const EVP_MD *md)
0f113f3e 570{
47e42b3c 571 if (md == NULL || EVP_MD_type(md) == NID_sha1)
0f113f3e
MC
572 return 1;
573 *palg = X509_ALGOR_new();
90945fa3 574 if (*palg == NULL)
0f113f3e
MC
575 return 0;
576 X509_ALGOR_set_md(*palg, md);
577 return 1;
578}
0574cadf
DSH
579
580/* Allocate and set MGF1 algorithm ID from EVP_MD */
581static int rsa_md_to_mgf1(X509_ALGOR **palg, const EVP_MD *mgf1md)
0f113f3e
MC
582{
583 X509_ALGOR *algtmp = NULL;
584 ASN1_STRING *stmp = NULL;
52ad523c 585
0f113f3e 586 *palg = NULL;
47e42b3c 587 if (mgf1md == NULL || EVP_MD_type(mgf1md) == NID_sha1)
0f113f3e
MC
588 return 1;
589 /* need to embed algorithm ID inside another */
590 if (!rsa_md_to_algor(&algtmp, mgf1md))
591 goto err;
f291138b 592 if (ASN1_item_pack(algtmp, ASN1_ITEM_rptr(X509_ALGOR), &stmp) == NULL)
0f113f3e
MC
593 goto err;
594 *palg = X509_ALGOR_new();
90945fa3 595 if (*palg == NULL)
0f113f3e
MC
596 goto err;
597 X509_ALGOR_set0(*palg, OBJ_nid2obj(NID_mgf1), V_ASN1_SEQUENCE, stmp);
598 stmp = NULL;
599 err:
0dfb9398 600 ASN1_STRING_free(stmp);
222561fe 601 X509_ALGOR_free(algtmp);
0f113f3e
MC
602 if (*palg)
603 return 1;
604 return 0;
605}
0574cadf
DSH
606
607/* convert algorithm ID to EVP_MD, default SHA1 */
608static const EVP_MD *rsa_algor_to_md(X509_ALGOR *alg)
0f113f3e
MC
609{
610 const EVP_MD *md;
52ad523c 611
0f113f3e
MC
612 if (!alg)
613 return EVP_sha1();
614 md = EVP_get_digestbyobj(alg->algorithm);
615 if (md == NULL)
616 RSAerr(RSA_F_RSA_ALGOR_TO_MD, RSA_R_UNKNOWN_DIGEST);
617 return md;
618}
619
0f113f3e 620/*
47e42b3c 621 * Convert EVP_PKEY_CTX in PSS mode into corresponding algorithm parameter,
0574cadf 622 * suitable for setting an AlgorithmIdentifier.
31904ecd
DSH
623 */
624
47e42b3c 625static RSA_PSS_PARAMS *rsa_ctx_to_pss(EVP_PKEY_CTX *pkctx)
0f113f3e
MC
626{
627 const EVP_MD *sigmd, *mgf1md;
0f113f3e 628 EVP_PKEY *pk = EVP_PKEY_CTX_get0_pkey(pkctx);
9767a3dc 629 RSA *rsa = EVP_PKEY_get0_RSA(pk);
47e42b3c 630 int saltlen;
52ad523c 631
0f113f3e 632 if (EVP_PKEY_CTX_get_signature_md(pkctx, &sigmd) <= 0)
47e42b3c 633 return NULL;
0f113f3e 634 if (EVP_PKEY_CTX_get_rsa_mgf1_md(pkctx, &mgf1md) <= 0)
47e42b3c 635 return NULL;
0f113f3e 636 if (!EVP_PKEY_CTX_get_rsa_pss_saltlen(pkctx, &saltlen))
47e42b3c 637 return NULL;
90862ab4 638 if (saltlen == -1) {
0f113f3e 639 saltlen = EVP_MD_size(sigmd);
491360e7 640 } else if (saltlen == -2 || saltlen == -3) {
9767a3dc 641 saltlen = RSA_size(rsa) - EVP_MD_size(sigmd) - 2;
137096a7 642 if ((EVP_PKEY_bits(pk) & 0x7) == 1)
0f113f3e 643 saltlen--;
491360e7
BE
644 if (saltlen < 0)
645 return NULL;
0f113f3e 646 }
47e42b3c
DSH
647
648 return rsa_pss_params_create(sigmd, mgf1md, saltlen);
649}
650
651RSA_PSS_PARAMS *rsa_pss_params_create(const EVP_MD *sigmd,
652 const EVP_MD *mgf1md, int saltlen)
653{
654 RSA_PSS_PARAMS *pss = RSA_PSS_PARAMS_new();
52ad523c 655
90945fa3 656 if (pss == NULL)
0f113f3e
MC
657 goto err;
658 if (saltlen != 20) {
659 pss->saltLength = ASN1_INTEGER_new();
90945fa3 660 if (pss->saltLength == NULL)
0f113f3e
MC
661 goto err;
662 if (!ASN1_INTEGER_set(pss->saltLength, saltlen))
663 goto err;
664 }
665 if (!rsa_md_to_algor(&pss->hashAlgorithm, sigmd))
666 goto err;
47e42b3c 667 if (mgf1md == NULL)
b6b885c6 668 mgf1md = sigmd;
0f113f3e
MC
669 if (!rsa_md_to_mgf1(&pss->maskGenAlgorithm, mgf1md))
670 goto err;
74753357
DSH
671 if (!rsa_md_to_algor(&pss->maskHash, mgf1md))
672 goto err;
47e42b3c 673 return pss;
0f113f3e 674 err:
25aaa98a 675 RSA_PSS_PARAMS_free(pss);
0f113f3e
MC
676 return NULL;
677}
678
47e42b3c
DSH
679static ASN1_STRING *rsa_ctx_to_pss_string(EVP_PKEY_CTX *pkctx)
680{
681 RSA_PSS_PARAMS *pss = rsa_ctx_to_pss(pkctx);
f291138b 682 ASN1_STRING *os;
52ad523c 683
47e42b3c
DSH
684 if (pss == NULL)
685 return NULL;
686
f291138b 687 os = ASN1_item_pack(pss, ASN1_ITEM_rptr(RSA_PSS_PARAMS), NULL);
47e42b3c
DSH
688 RSA_PSS_PARAMS_free(pss);
689 return os;
690}
691
0f113f3e
MC
692/*
693 * From PSS AlgorithmIdentifier set public key parameters. If pkey isn't NULL
0d4fb843 694 * then the EVP_MD_CTX is setup and initialised. If it is NULL parameters are
0f113f3e 695 * passed to pkctx instead.
0574cadf 696 */
31904ecd 697
0574cadf 698static int rsa_pss_to_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pkctx,
0f113f3e
MC
699 X509_ALGOR *sigalg, EVP_PKEY *pkey)
700{
701 int rv = -1;
702 int saltlen;
703 const EVP_MD *mgf1md = NULL, *md = NULL;
704 RSA_PSS_PARAMS *pss;
52ad523c 705
0f113f3e 706 /* Sanity check: make sure it is PSS */
ffc6fad5 707 if (OBJ_obj2nid(sigalg->algorithm) != EVP_PKEY_RSA_PSS) {
0f113f3e
MC
708 RSAerr(RSA_F_RSA_PSS_TO_CTX, RSA_R_UNSUPPORTED_SIGNATURE_TYPE);
709 return -1;
710 }
711 /* Decode PSS parameters */
6745a1ff 712 pss = rsa_pss_decode(sigalg);
0f113f3e 713
cfd81c6d 714 if (!rsa_pss_get_param(pss, &md, &mgf1md, &saltlen)) {
0f113f3e
MC
715 RSAerr(RSA_F_RSA_PSS_TO_CTX, RSA_R_INVALID_PSS_PARAMETERS);
716 goto err;
717 }
0f113f3e
MC
718
719 /* We have all parameters now set up context */
0f113f3e
MC
720 if (pkey) {
721 if (!EVP_DigestVerifyInit(ctx, &pkctx, md, NULL, pkey))
722 goto err;
723 } else {
724 const EVP_MD *checkmd;
725 if (EVP_PKEY_CTX_get_signature_md(pkctx, &checkmd) <= 0)
726 goto err;
727 if (EVP_MD_type(md) != EVP_MD_type(checkmd)) {
728 RSAerr(RSA_F_RSA_PSS_TO_CTX, RSA_R_DIGEST_DOES_NOT_MATCH);
729 goto err;
730 }
731 }
732
733 if (EVP_PKEY_CTX_set_rsa_padding(pkctx, RSA_PKCS1_PSS_PADDING) <= 0)
734 goto err;
735
736 if (EVP_PKEY_CTX_set_rsa_pss_saltlen(pkctx, saltlen) <= 0)
737 goto err;
738
739 if (EVP_PKEY_CTX_set_rsa_mgf1_md(pkctx, mgf1md) <= 0)
740 goto err;
741 /* Carry on */
742 rv = 1;
743
744 err:
745 RSA_PSS_PARAMS_free(pss);
0f113f3e
MC
746 return rv;
747}
492a9e24 748
a6495479
RL
749static int rsa_pss_verify_param(const EVP_MD **pmd, const EVP_MD **pmgf1md,
750 int *psaltlen, int *ptrailerField)
751{
752 if (psaltlen != NULL && *psaltlen < 0) {
753 ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_SALT_LENGTH);
754 return 0;
755 }
756 /*
757 * low-level routines support only trailer field 0xbc (value 1) and
758 * PKCS#1 says we should reject any other value anyway.
759 */
760 if (ptrailerField != NULL && *ptrailerField != 1) {
761 ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_TRAILER);
762 return 0;
763 }
764 return 1;
765}
766
767static int rsa_pss_get_param_unverified(const RSA_PSS_PARAMS *pss,
768 const EVP_MD **pmd,
769 const EVP_MD **pmgf1md,
770 int *psaltlen, int *ptrailerField)
cfd81c6d 771{
a6495479
RL
772 RSA_PSS_PARAMS_30 pss_params;
773
774 /* Get the defaults from the ONE place */
775 (void)rsa_pss_params_30_set_defaults(&pss_params);
776
cfd81c6d
DSH
777 if (pss == NULL)
778 return 0;
779 *pmd = rsa_algor_to_md(pss->hashAlgorithm);
780 if (*pmd == NULL)
781 return 0;
782 *pmgf1md = rsa_algor_to_md(pss->maskHash);
783 if (*pmgf1md == NULL)
784 return 0;
a6495479 785 if (pss->saltLength)
cfd81c6d 786 *psaltlen = ASN1_INTEGER_get(pss->saltLength);
a6495479
RL
787 else
788 *psaltlen = rsa_pss_params_30_saltlen(&pss_params);
789 if (pss->trailerField)
790 *ptrailerField = ASN1_INTEGER_get(pss->trailerField);
791 else
792 *ptrailerField = rsa_pss_params_30_trailerfield(&pss_params);;
793
794 return 1;
795}
cfd81c6d 796
a6495479
RL
797int rsa_pss_get_param(const RSA_PSS_PARAMS *pss, const EVP_MD **pmd,
798 const EVP_MD **pmgf1md, int *psaltlen)
799{
cfd81c6d 800 /*
a6495479
RL
801 * Callers do not care about the trailer field, and yet, we must
802 * pass it from get_param to verify_param, since the latter checks
803 * its value.
804 *
805 * When callers start caring, it's a simple thing to add another
806 * argument to this function.
cfd81c6d 807 */
a6495479 808 int trailerField = 0;
cfd81c6d 809
a6495479
RL
810 return rsa_pss_get_param_unverified(pss, pmd, pmgf1md, psaltlen,
811 &trailerField)
812 && rsa_pss_verify_param(pmd, pmgf1md, psaltlen, &trailerField);
813}
814
815static int rsa_sync_to_pss_params_30(RSA *rsa)
816{
817 if (rsa != NULL && rsa->pss != NULL) {
818 const EVP_MD *md = NULL, *mgf1md = NULL;
819 int md_nid, mgf1md_nid, saltlen, trailerField;
820 RSA_PSS_PARAMS_30 pss_params;
821
822 /*
823 * We don't care about the validity of the fields here, we just
824 * want to synchronise values. Verifying here makes it impossible
825 * to even read a key with invalid values, making it hard to test
826 * a bad situation.
827 *
828 * Other routines use rsa_pss_get_param(), so the values will be
829 * checked, eventually.
830 */
831 if (!rsa_pss_get_param_unverified(rsa->pss, &md, &mgf1md,
832 &saltlen, &trailerField))
833 return 0;
834 md_nid = EVP_MD_type(md);
835 mgf1md_nid = EVP_MD_type(mgf1md);
836 if (!rsa_pss_params_30_set_defaults(&pss_params)
837 || !rsa_pss_params_30_set_hashalg(&pss_params, md_nid)
838 || !rsa_pss_params_30_set_maskgenhashalg(&pss_params, mgf1md_nid)
839 || !rsa_pss_params_30_set_saltlen(&pss_params, saltlen)
840 || !rsa_pss_params_30_set_trailerfield(&pss_params, trailerField))
841 return 0;
842 rsa->pss_params = pss_params;
843 }
cfd81c6d
DSH
844 return 1;
845}
846
e968561d 847#ifndef OPENSSL_NO_CMS
0574cadf 848static int rsa_cms_verify(CMS_SignerInfo *si)
0f113f3e
MC
849{
850 int nid, nid2;
851 X509_ALGOR *alg;
852 EVP_PKEY_CTX *pkctx = CMS_SignerInfo_get0_pkey_ctx(si);
52ad523c 853
0f113f3e
MC
854 CMS_SignerInfo_get0_algs(si, NULL, NULL, NULL, &alg);
855 nid = OBJ_obj2nid(alg->algorithm);
ffc6fad5 856 if (nid == EVP_PKEY_RSA_PSS)
0f113f3e 857 return rsa_pss_to_ctx(NULL, pkctx, alg, NULL);
08be0331
DSH
858 /* Only PSS allowed for PSS keys */
859 if (pkey_ctx_is_pss(pkctx)) {
860 RSAerr(RSA_F_RSA_CMS_VERIFY, RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
861 return 0;
862 }
863 if (nid == NID_rsaEncryption)
864 return 1;
0f113f3e
MC
865 /* Workaround for some implementation that use a signature OID */
866 if (OBJ_find_sigid_algs(nid, NULL, &nid2)) {
867 if (nid2 == NID_rsaEncryption)
868 return 1;
869 }
870 return 0;
871}
e968561d 872#endif
0f113f3e
MC
873
874/*
875 * Customised RSA item verification routine. This is called when a signature
876 * is encountered requiring special handling. We currently only handle PSS.
0574cadf
DSH
877 */
878
0574cadf 879static int rsa_item_verify(EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn,
0f113f3e
MC
880 X509_ALGOR *sigalg, ASN1_BIT_STRING *sig,
881 EVP_PKEY *pkey)
882{
883 /* Sanity check: make sure it is PSS */
ffc6fad5 884 if (OBJ_obj2nid(sigalg->algorithm) != EVP_PKEY_RSA_PSS) {
0f113f3e
MC
885 RSAerr(RSA_F_RSA_ITEM_VERIFY, RSA_R_UNSUPPORTED_SIGNATURE_TYPE);
886 return -1;
887 }
09f06923 888 if (rsa_pss_to_ctx(ctx, NULL, sigalg, pkey) > 0) {
0f113f3e
MC
889 /* Carry on */
890 return 2;
09f06923 891 }
0f113f3e
MC
892 return -1;
893}
0574cadf 894
e968561d 895#ifndef OPENSSL_NO_CMS
0574cadf 896static int rsa_cms_sign(CMS_SignerInfo *si)
0f113f3e
MC
897{
898 int pad_mode = RSA_PKCS1_PADDING;
899 X509_ALGOR *alg;
900 EVP_PKEY_CTX *pkctx = CMS_SignerInfo_get0_pkey_ctx(si);
901 ASN1_STRING *os = NULL;
52ad523c 902
0f113f3e
MC
903 CMS_SignerInfo_get0_algs(si, NULL, NULL, NULL, &alg);
904 if (pkctx) {
905 if (EVP_PKEY_CTX_get_rsa_padding(pkctx, &pad_mode) <= 0)
906 return 0;
907 }
908 if (pad_mode == RSA_PKCS1_PADDING) {
909 X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption), V_ASN1_NULL, 0);
910 return 1;
911 }
912 /* We don't support it */
913 if (pad_mode != RSA_PKCS1_PSS_PADDING)
914 return 0;
47e42b3c 915 os = rsa_ctx_to_pss_string(pkctx);
0f113f3e
MC
916 if (!os)
917 return 0;
ffc6fad5 918 X509_ALGOR_set0(alg, OBJ_nid2obj(EVP_PKEY_RSA_PSS), V_ASN1_SEQUENCE, os);
0f113f3e
MC
919 return 1;
920}
e968561d 921#endif
0574cadf 922
17c63d1c 923static int rsa_item_sign(EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn,
0f113f3e
MC
924 X509_ALGOR *alg1, X509_ALGOR *alg2,
925 ASN1_BIT_STRING *sig)
926{
927 int pad_mode;
6e59a892 928 EVP_PKEY_CTX *pkctx = EVP_MD_CTX_pkey_ctx(ctx);
52ad523c 929
0f113f3e
MC
930 if (EVP_PKEY_CTX_get_rsa_padding(pkctx, &pad_mode) <= 0)
931 return 0;
932 if (pad_mode == RSA_PKCS1_PADDING)
933 return 2;
934 if (pad_mode == RSA_PKCS1_PSS_PADDING) {
935 ASN1_STRING *os1 = NULL;
47e42b3c 936 os1 = rsa_ctx_to_pss_string(pkctx);
0f113f3e
MC
937 if (!os1)
938 return 0;
939 /* Duplicate parameters if we have to */
940 if (alg2) {
941 ASN1_STRING *os2 = ASN1_STRING_dup(os1);
942 if (!os2) {
943 ASN1_STRING_free(os1);
944 return 0;
945 }
ffc6fad5 946 X509_ALGOR_set0(alg2, OBJ_nid2obj(EVP_PKEY_RSA_PSS),
0f113f3e
MC
947 V_ASN1_SEQUENCE, os2);
948 }
ffc6fad5 949 X509_ALGOR_set0(alg1, OBJ_nid2obj(EVP_PKEY_RSA_PSS),
0f113f3e
MC
950 V_ASN1_SEQUENCE, os1);
951 return 3;
952 }
953 return 2;
954}
17c63d1c 955
629e369c
DSH
956static int rsa_sig_info_set(X509_SIG_INFO *siginf, const X509_ALGOR *sigalg,
957 const ASN1_STRING *sig)
958{
959 int rv = 0;
960 int mdnid, saltlen;
961 uint32_t flags;
962 const EVP_MD *mgf1md = NULL, *md = NULL;
963 RSA_PSS_PARAMS *pss;
b744f915 964 int secbits;
629e369c
DSH
965
966 /* Sanity check: make sure it is PSS */
967 if (OBJ_obj2nid(sigalg->algorithm) != EVP_PKEY_RSA_PSS)
968 return 0;
969 /* Decode PSS parameters */
970 pss = rsa_pss_decode(sigalg);
971 if (!rsa_pss_get_param(pss, &md, &mgf1md, &saltlen))
972 goto err;
973 mdnid = EVP_MD_type(md);
974 /*
975 * For TLS need SHA256, SHA384 or SHA512, digest and MGF1 digest must
976 * match and salt length must equal digest size
977 */
978 if ((mdnid == NID_sha256 || mdnid == NID_sha384 || mdnid == NID_sha512)
979 && mdnid == EVP_MD_type(mgf1md) && saltlen == EVP_MD_size(md))
980 flags = X509_SIG_INFO_TLS;
981 else
982 flags = 0;
983 /* Note: security bits half number of digest bits */
b744f915
KR
984 secbits = EVP_MD_size(md) * 4;
985 /*
986 * SHA1 and MD5 are known to be broken. Reduce security bits so that
987 * they're no longer accepted at security level 1. The real values don't
988 * really matter as long as they're lower than 80, which is our security
989 * level 1.
990 * https://eprint.iacr.org/2020/014 puts a chosen-prefix attack for SHA1 at
991 * 2^63.4
992 * https://documents.epfl.ch/users/l/le/lenstra/public/papers/lat.pdf
993 * puts a chosen-prefix attack for MD5 at 2^39.
994 */
995 if (mdnid == NID_sha1)
996 secbits = 64;
997 else if (mdnid == NID_md5_sha1)
998 secbits = 68;
999 else if (mdnid == NID_md5)
1000 secbits = 39;
1001 X509_SIG_INFO_set(siginf, mdnid, EVP_PKEY_RSA_PSS, secbits,
629e369c
DSH
1002 flags);
1003 rv = 1;
1004 err:
1005 RSA_PSS_PARAMS_free(pss);
1006 return rv;
1007}
1008
e968561d 1009#ifndef OPENSSL_NO_CMS
6745a1ff 1010static RSA_OAEP_PARAMS *rsa_oaep_decode(const X509_ALGOR *alg)
0f113f3e 1011{
6745a1ff 1012 RSA_OAEP_PARAMS *oaep;
0574cadf 1013
6745a1ff 1014 oaep = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(RSA_OAEP_PARAMS),
52ad523c 1015 alg->parameter);
0574cadf 1016
6745a1ff 1017 if (oaep == NULL)
0f113f3e 1018 return NULL;
0574cadf 1019
6745a1ff
DSH
1020 if (oaep->maskGenFunc != NULL) {
1021 oaep->maskHash = rsa_mgf1_decode(oaep->maskGenFunc);
1022 if (oaep->maskHash == NULL) {
1023 RSA_OAEP_PARAMS_free(oaep);
1024 return NULL;
1025 }
1026 }
1027 return oaep;
0f113f3e 1028}
0574cadf
DSH
1029
1030static int rsa_cms_decrypt(CMS_RecipientInfo *ri)
0f113f3e
MC
1031{
1032 EVP_PKEY_CTX *pkctx;
1033 X509_ALGOR *cmsalg;
1034 int nid;
1035 int rv = -1;
1036 unsigned char *label = NULL;
1037 int labellen = 0;
1038 const EVP_MD *mgf1md = NULL, *md = NULL;
1039 RSA_OAEP_PARAMS *oaep;
52ad523c 1040
0f113f3e 1041 pkctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
52ad523c 1042 if (pkctx == NULL)
0f113f3e
MC
1043 return 0;
1044 if (!CMS_RecipientInfo_ktri_get0_algs(ri, NULL, NULL, &cmsalg))
1045 return -1;
1046 nid = OBJ_obj2nid(cmsalg->algorithm);
1047 if (nid == NID_rsaEncryption)
1048 return 1;
1049 if (nid != NID_rsaesOaep) {
1050 RSAerr(RSA_F_RSA_CMS_DECRYPT, RSA_R_UNSUPPORTED_ENCRYPTION_TYPE);
1051 return -1;
1052 }
1053 /* Decode OAEP parameters */
6745a1ff 1054 oaep = rsa_oaep_decode(cmsalg);
0f113f3e
MC
1055
1056 if (oaep == NULL) {
1057 RSAerr(RSA_F_RSA_CMS_DECRYPT, RSA_R_INVALID_OAEP_PARAMETERS);
1058 goto err;
1059 }
1060
6745a1ff 1061 mgf1md = rsa_algor_to_md(oaep->maskHash);
52ad523c 1062 if (mgf1md == NULL)
0f113f3e
MC
1063 goto err;
1064 md = rsa_algor_to_md(oaep->hashFunc);
52ad523c 1065 if (md == NULL)
0f113f3e
MC
1066 goto err;
1067
52ad523c 1068 if (oaep->pSourceFunc != NULL) {
0f113f3e 1069 X509_ALGOR *plab = oaep->pSourceFunc;
52ad523c 1070
0f113f3e
MC
1071 if (OBJ_obj2nid(plab->algorithm) != NID_pSpecified) {
1072 RSAerr(RSA_F_RSA_CMS_DECRYPT, RSA_R_UNSUPPORTED_LABEL_SOURCE);
1073 goto err;
1074 }
1075 if (plab->parameter->type != V_ASN1_OCTET_STRING) {
1076 RSAerr(RSA_F_RSA_CMS_DECRYPT, RSA_R_INVALID_LABEL);
1077 goto err;
1078 }
1079
1080 label = plab->parameter->value.octet_string->data;
1081 /* Stop label being freed when OAEP parameters are freed */
1082 plab->parameter->value.octet_string->data = NULL;
1083 labellen = plab->parameter->value.octet_string->length;
1084 }
1085
1086 if (EVP_PKEY_CTX_set_rsa_padding(pkctx, RSA_PKCS1_OAEP_PADDING) <= 0)
1087 goto err;
1088 if (EVP_PKEY_CTX_set_rsa_oaep_md(pkctx, md) <= 0)
1089 goto err;
1090 if (EVP_PKEY_CTX_set_rsa_mgf1_md(pkctx, mgf1md) <= 0)
1091 goto err;
9c44916c
BK
1092 if (label != NULL
1093 && EVP_PKEY_CTX_set0_rsa_oaep_label(pkctx, label, labellen) <= 0)
0f113f3e
MC
1094 goto err;
1095 /* Carry on */
1096 rv = 1;
1097
1098 err:
1099 RSA_OAEP_PARAMS_free(oaep);
0f113f3e
MC
1100 return rv;
1101}
0574cadf
DSH
1102
1103static int rsa_cms_encrypt(CMS_RecipientInfo *ri)
0f113f3e
MC
1104{
1105 const EVP_MD *md, *mgf1md;
1106 RSA_OAEP_PARAMS *oaep = NULL;
1107 ASN1_STRING *os = NULL;
1108 X509_ALGOR *alg;
1109 EVP_PKEY_CTX *pkctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
1110 int pad_mode = RSA_PKCS1_PADDING, rv = 0, labellen;
1111 unsigned char *label;
52ad523c 1112
178989b4
BS
1113 if (CMS_RecipientInfo_ktri_get0_algs(ri, NULL, NULL, &alg) <= 0)
1114 return 0;
0f113f3e
MC
1115 if (pkctx) {
1116 if (EVP_PKEY_CTX_get_rsa_padding(pkctx, &pad_mode) <= 0)
1117 return 0;
1118 }
1119 if (pad_mode == RSA_PKCS1_PADDING) {
1120 X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption), V_ASN1_NULL, 0);
1121 return 1;
1122 }
1123 /* Not supported */
1124 if (pad_mode != RSA_PKCS1_OAEP_PADDING)
1125 return 0;
1126 if (EVP_PKEY_CTX_get_rsa_oaep_md(pkctx, &md) <= 0)
1127 goto err;
1128 if (EVP_PKEY_CTX_get_rsa_mgf1_md(pkctx, &mgf1md) <= 0)
1129 goto err;
1130 labellen = EVP_PKEY_CTX_get0_rsa_oaep_label(pkctx, &label);
1131 if (labellen < 0)
1132 goto err;
1133 oaep = RSA_OAEP_PARAMS_new();
90945fa3 1134 if (oaep == NULL)
0f113f3e
MC
1135 goto err;
1136 if (!rsa_md_to_algor(&oaep->hashFunc, md))
1137 goto err;
1138 if (!rsa_md_to_mgf1(&oaep->maskGenFunc, mgf1md))
1139 goto err;
1140 if (labellen > 0) {
5e8129f2 1141 ASN1_OCTET_STRING *los;
0f113f3e 1142 oaep->pSourceFunc = X509_ALGOR_new();
90945fa3 1143 if (oaep->pSourceFunc == NULL)
0f113f3e 1144 goto err;
5e8129f2 1145 los = ASN1_OCTET_STRING_new();
90945fa3 1146 if (los == NULL)
0f113f3e
MC
1147 goto err;
1148 if (!ASN1_OCTET_STRING_set(los, label, labellen)) {
1149 ASN1_OCTET_STRING_free(los);
1150 goto err;
1151 }
1152 X509_ALGOR_set0(oaep->pSourceFunc, OBJ_nid2obj(NID_pSpecified),
1153 V_ASN1_OCTET_STRING, los);
1154 }
1155 /* create string with pss parameter encoding. */
1156 if (!ASN1_item_pack(oaep, ASN1_ITEM_rptr(RSA_OAEP_PARAMS), &os))
1157 goto err;
1158 X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaesOaep), V_ASN1_SEQUENCE, os);
1159 os = NULL;
1160 rv = 1;
1161 err:
25aaa98a 1162 RSA_OAEP_PARAMS_free(oaep);
0dfb9398 1163 ASN1_STRING_free(os);
0f113f3e
MC
1164 return rv;
1165}
e968561d 1166#endif
0f113f3e 1167
2aee35d3
PY
1168static int rsa_pkey_check(const EVP_PKEY *pkey)
1169{
1170 return RSA_check_key_ex(pkey->pkey.rsa, NULL);
1171}
1172
29be6023
RL
1173static size_t rsa_pkey_dirty_cnt(const EVP_PKEY *pkey)
1174{
1175 return pkey->pkey.rsa->dirty_cnt;
1176}
1177
967cc3f9
RL
1178/*
1179 * For the moment, we trust the call path, where keys going through
1180 * rsa_pkey_export_to() match a KEYMGMT for the "RSA" keytype, while
1181 * keys going through rsa_pss_pkey_export_to() match a KEYMGMT for the
1182 * "RSA-PSS" keytype.
1183 * TODO(3.0) Investigate whether we should simply continue to trust the
1184 * call path, or if we should strengthen this function by checking that
1185 * |rsa_type| matches the RSA key subtype. The latter requires ensuring
1186 * that the type flag for the RSA key is properly set by other functions
1187 * in this file.
1188 */
1189static int rsa_int_export_to(const EVP_PKEY *from, int rsa_type,
1190 void *to_keydata, EVP_KEYMGMT *to_keymgmt,
1191 OPENSSL_CTX *libctx, const char *propq)
29be6023 1192{
b305452f 1193 RSA *rsa = from->pkey.rsa;
6d4e6009 1194 OSSL_PARAM_BLD *tmpl = OSSL_PARAM_BLD_new();
29be6023 1195 OSSL_PARAM *params = NULL;
0996cff9 1196 int selection = 0;
b305452f 1197 int rv = 0;
29be6023 1198
6d4e6009
P
1199 if (tmpl == NULL)
1200 return 0;
df13defd
RL
1201 /*
1202 * If the RSA method is foreign, then we can't be sure of anything, and
1203 * can therefore not export or pretend to export.
1204 */
1205 if (RSA_get_method(rsa) != RSA_PKCS1_OpenSSL())
6d4e6009 1206 goto err;
df13defd 1207
b305452f 1208 /* Public parameters must always be present */
645a541a 1209 if (RSA_get0_n(rsa) == NULL || RSA_get0_e(rsa) == NULL)
29be6023
RL
1210 goto err;
1211
645a541a 1212 if (!rsa_todata(rsa, tmpl, NULL))
29be6023 1213 goto err;
b305452f 1214
645a541a
RL
1215 selection |= OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
1216 if (RSA_get0_d(rsa) != NULL)
0996cff9 1217 selection |= OSSL_KEYMGMT_SELECT_PRIVATE_KEY;
29be6023 1218
967cc3f9
RL
1219 if (rsa->pss != NULL) {
1220 const EVP_MD *md = NULL, *mgf1md = NULL;
6ce6ad39 1221 int md_nid, mgf1md_nid, saltlen, trailerfield;
967cc3f9
RL
1222 RSA_PSS_PARAMS_30 pss_params;
1223
6ce6ad39
RL
1224 if (!rsa_pss_get_param_unverified(rsa->pss, &md, &mgf1md,
1225 &saltlen, &trailerfield))
967cc3f9
RL
1226 goto err;
1227 md_nid = EVP_MD_type(md);
1228 mgf1md_nid = EVP_MD_type(mgf1md);
1229 if (!rsa_pss_params_30_set_defaults(&pss_params)
1230 || !rsa_pss_params_30_set_hashalg(&pss_params, md_nid)
1231 || !rsa_pss_params_30_set_maskgenhashalg(&pss_params, mgf1md_nid)
1232 || !rsa_pss_params_30_set_saltlen(&pss_params, saltlen)
1233 || !rsa_pss_params_30_todata(&pss_params, propq, tmpl, NULL))
1234 goto err;
1235 selection |= OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS;
29be6023
RL
1236 }
1237
6d4e6009 1238 if ((params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL)
29be6023
RL
1239 goto err;
1240
1241 /* We export, the provider imports */
0996cff9 1242 rv = evp_keymgmt_import(to_keymgmt, to_keydata, selection, params);
29be6023
RL
1243
1244 err:
6d4e6009
P
1245 OSSL_PARAM_BLD_free_params(params);
1246 OSSL_PARAM_BLD_free(tmpl);
b305452f 1247 return rv;
29be6023
RL
1248}
1249
967cc3f9
RL
1250static int rsa_int_import_from(const OSSL_PARAM params[], void *vpctx,
1251 int rsa_type)
0abae163 1252{
629c72db
MC
1253 EVP_PKEY_CTX *pctx = vpctx;
1254 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(pctx);
1255 RSA *rsa = rsa_new_with_ctx(pctx->libctx);
967cc3f9
RL
1256 RSA_PSS_PARAMS_30 rsa_pss_params = { 0, };
1257 int ok = 0;
0abae163
RL
1258
1259 if (rsa == NULL) {
1260 ERR_raise(ERR_LIB_DH, ERR_R_MALLOC_FAILURE);
1261 return 0;
1262 }
1263
967cc3f9
RL
1264 RSA_clear_flags(rsa, RSA_FLAG_TYPE_MASK);
1265 RSA_set_flags(rsa, rsa_type);
1266
1267 if (!rsa_pss_params_30_fromdata(&rsa_pss_params, params, pctx->libctx))
1268 goto err;
1269
1270 switch (rsa_type) {
1271 case RSA_FLAG_TYPE_RSA:
1272 /*
1273 * Were PSS parameters filled in?
1274 * In that case, something's wrong
1275 */
1276 if (!rsa_pss_params_30_is_unrestricted(&rsa_pss_params))
1277 goto err;
1278 break;
1279 case RSA_FLAG_TYPE_RSASSAPSS:
1280 /*
1281 * Were PSS parameters filled in? In that case, create the old
1282 * RSA_PSS_PARAMS structure. Otherwise, this is an unrestricted key.
1283 */
1284 if (!rsa_pss_params_30_is_unrestricted(&rsa_pss_params)) {
1285 /* Create the older RSA_PSS_PARAMS from RSA_PSS_PARAMS_30 data */
1286 int mdnid = rsa_pss_params_30_hashalg(&rsa_pss_params);
1287 int mgf1mdnid = rsa_pss_params_30_maskgenhashalg(&rsa_pss_params);
1288 int saltlen = rsa_pss_params_30_saltlen(&rsa_pss_params);
1289 const EVP_MD *md = EVP_get_digestbynid(mdnid);
1290 const EVP_MD *mgf1md = EVP_get_digestbynid(mgf1mdnid);
1291
1292 if ((rsa->pss = rsa_pss_params_create(md, mgf1md, saltlen)) == NULL)
1293 goto err;
1294 }
1295 break;
1296 default:
1297 /* RSA key sub-types we don't know how to handle yet */
1298 goto err;
0abae163 1299 }
967cc3f9
RL
1300
1301 if (!rsa_fromdata(rsa, params))
1302 goto err;
1303
1304 switch (rsa_type) {
1305 case RSA_FLAG_TYPE_RSA:
1306 ok = EVP_PKEY_assign_RSA(pkey, rsa);
1307 break;
1308 case RSA_FLAG_TYPE_RSASSAPSS:
1309 ok = EVP_PKEY_assign(pkey, EVP_PKEY_RSA_PSS, rsa);
1310 break;
1311 }
1312
1313 err:
1314 if (!ok)
1315 RSA_free(rsa);
1316 return ok;
1317}
1318
1319static int rsa_pkey_export_to(const EVP_PKEY *from, void *to_keydata,
1320 EVP_KEYMGMT *to_keymgmt, OPENSSL_CTX *libctx,
1321 const char *propq)
1322{
1323 return rsa_int_export_to(from, RSA_FLAG_TYPE_RSA, to_keydata,
1324 to_keymgmt, libctx, propq);
1325}
1326
1327static int rsa_pss_pkey_export_to(const EVP_PKEY *from, void *to_keydata,
1328 EVP_KEYMGMT *to_keymgmt, OPENSSL_CTX *libctx,
1329 const char *propq)
1330{
1331 return rsa_int_export_to(from, RSA_FLAG_TYPE_RSASSAPSS, to_keydata,
1332 to_keymgmt, libctx, propq);
1333}
1334
1335static int rsa_pkey_import_from(const OSSL_PARAM params[], void *vpctx)
1336{
1337 return rsa_int_import_from(params, vpctx, RSA_FLAG_TYPE_RSA);
1338}
1339
1340static int rsa_pss_pkey_import_from(const OSSL_PARAM params[], void *vpctx)
1341{
1342 return rsa_int_import_from(params, vpctx, RSA_FLAG_TYPE_RSASSAPSS);
0abae163
RL
1343}
1344
578b5514 1345const EVP_PKEY_ASN1_METHOD rsa_asn1_meths[2] = {
0f113f3e
MC
1346 {
1347 EVP_PKEY_RSA,
1348 EVP_PKEY_RSA,
1349 ASN1_PKEY_SIGPARAM_NULL,
1350
1351 "RSA",
1352 "OpenSSL RSA method",
1353
1354 rsa_pub_decode,
1355 rsa_pub_encode,
1356 rsa_pub_cmp,
1357 rsa_pub_print,
1358
1359 rsa_priv_decode,
1360 rsa_priv_encode,
1361 rsa_priv_print,
1362
1363 int_rsa_size,
1364 rsa_bits,
1365 rsa_security_bits,
1366
1367 0, 0, 0, 0, 0, 0,
1368
1369 rsa_sig_print,
1370 int_rsa_free,
1371 rsa_pkey_ctrl,
1372 old_rsa_priv_decode,
1373 old_rsa_priv_encode,
1374 rsa_item_verify,
629e369c 1375 rsa_item_sign,
2aee35d3 1376 rsa_sig_info_set,
29be6023
RL
1377 rsa_pkey_check,
1378
1379 0, 0,
1380 0, 0, 0, 0,
1381
1382 rsa_pkey_dirty_cnt,
0abae163
RL
1383 rsa_pkey_export_to,
1384 rsa_pkey_import_from
629e369c 1385 },
0f113f3e
MC
1386
1387 {
1388 EVP_PKEY_RSA2,
1389 EVP_PKEY_RSA,
1390 ASN1_PKEY_ALIAS}
1391};
4e8ba747
DSH
1392
1393const EVP_PKEY_ASN1_METHOD rsa_pss_asn1_meth = {
1394 EVP_PKEY_RSA_PSS,
1395 EVP_PKEY_RSA_PSS,
1396 ASN1_PKEY_SIGPARAM_NULL,
1397
1398 "RSA-PSS",
1399 "OpenSSL RSA-PSS method",
1400
1401 rsa_pub_decode,
1402 rsa_pub_encode,
1403 rsa_pub_cmp,
1404 rsa_pub_print,
1405
1406 rsa_priv_decode,
1407 rsa_priv_encode,
1408 rsa_priv_print,
1409
1410 int_rsa_size,
1411 rsa_bits,
1412 rsa_security_bits,
1413
1414 0, 0, 0, 0, 0, 0,
1415
1416 rsa_sig_print,
1417 int_rsa_free,
1418 rsa_pkey_ctrl,
1419 0, 0,
1420 rsa_item_verify,
1421 rsa_item_sign,
2aee35d3 1422 0,
29be6023
RL
1423 rsa_pkey_check,
1424
1425 0, 0,
1426 0, 0, 0, 0,
1427
1428 rsa_pkey_dirty_cnt,
967cc3f9
RL
1429 rsa_pss_pkey_export_to,
1430 rsa_pss_pkey_import_from
4e8ba747 1431};