]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/rsa/rsa_lib.c
en EVP_PKEY_CTX_set_rsa_keygen_pubexp() BIGNUM management
[thirdparty/openssl.git] / crypto / rsa / rsa_lib.c
1 /*
2 * Copyright 1995-2020 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 <openssl/crypto.h>
18 #include <openssl/core_names.h>
19 #include <openssl/engine.h>
20 #include <openssl/evp.h>
21 #include "internal/cryptlib.h"
22 #include "internal/refcount.h"
23 #include "openssl/param_build.h"
24 #include "crypto/bn.h"
25 #include "crypto/evp.h"
26 #include "crypto/rsa.h"
27 #include "crypto/security_bits.h"
28 #include "rsa_local.h"
29
30 static RSA *rsa_new_intern(ENGINE *engine, OPENSSL_CTX *libctx);
31
32 #ifndef FIPS_MODULE
33 RSA *RSA_new(void)
34 {
35 return rsa_new_intern(NULL, NULL);
36 }
37
38 const RSA_METHOD *RSA_get_method(const RSA *rsa)
39 {
40 return rsa->meth;
41 }
42
43 int RSA_set_method(RSA *rsa, const RSA_METHOD *meth)
44 {
45 /*
46 * NB: The caller is specifically setting a method, so it's not up to us
47 * to deal with which ENGINE it comes from.
48 */
49 const RSA_METHOD *mtmp;
50 mtmp = rsa->meth;
51 if (mtmp->finish)
52 mtmp->finish(rsa);
53 #ifndef OPENSSL_NO_ENGINE
54 ENGINE_finish(rsa->engine);
55 rsa->engine = NULL;
56 #endif
57 rsa->meth = meth;
58 if (meth->init)
59 meth->init(rsa);
60 return 1;
61 }
62
63 RSA *RSA_new_method(ENGINE *engine)
64 {
65 return rsa_new_intern(engine, NULL);
66 }
67 #endif
68
69 RSA *rsa_new_with_ctx(OPENSSL_CTX *libctx)
70 {
71 return rsa_new_intern(NULL, libctx);
72 }
73
74 static RSA *rsa_new_intern(ENGINE *engine, OPENSSL_CTX *libctx)
75 {
76 RSA *ret = OPENSSL_zalloc(sizeof(*ret));
77
78 if (ret == NULL) {
79 RSAerr(0, ERR_R_MALLOC_FAILURE);
80 return NULL;
81 }
82
83 ret->references = 1;
84 ret->lock = CRYPTO_THREAD_lock_new();
85 if (ret->lock == NULL) {
86 RSAerr(0, ERR_R_MALLOC_FAILURE);
87 OPENSSL_free(ret);
88 return NULL;
89 }
90
91 ret->libctx = libctx;
92 ret->meth = RSA_get_default_method();
93 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
94 ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
95 if (engine) {
96 if (!ENGINE_init(engine)) {
97 RSAerr(0, ERR_R_ENGINE_LIB);
98 goto err;
99 }
100 ret->engine = engine;
101 } else {
102 ret->engine = ENGINE_get_default_RSA();
103 }
104 if (ret->engine) {
105 ret->meth = ENGINE_get_RSA(ret->engine);
106 if (ret->meth == NULL) {
107 RSAerr(0, ERR_R_ENGINE_LIB);
108 goto err;
109 }
110 }
111 #endif
112
113 ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
114 #ifndef FIPS_MODULE
115 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data)) {
116 goto err;
117 }
118 #endif
119
120 if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
121 RSAerr(0, ERR_R_INIT_FAIL);
122 goto err;
123 }
124
125 return ret;
126
127 err:
128 RSA_free(ret);
129 return NULL;
130 }
131
132 void RSA_free(RSA *r)
133 {
134 int i;
135
136 if (r == NULL)
137 return;
138
139 CRYPTO_DOWN_REF(&r->references, &i, r->lock);
140 REF_PRINT_COUNT("RSA", r);
141 if (i > 0)
142 return;
143 REF_ASSERT_ISNT(i < 0);
144
145 if (r->meth != NULL && r->meth->finish != NULL)
146 r->meth->finish(r);
147 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
148 ENGINE_finish(r->engine);
149 #endif
150
151 #ifndef FIPS_MODULE
152 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data);
153 #endif
154
155 CRYPTO_THREAD_lock_free(r->lock);
156
157 BN_free(r->n);
158 BN_free(r->e);
159 BN_clear_free(r->d);
160 BN_clear_free(r->p);
161 BN_clear_free(r->q);
162 BN_clear_free(r->dmp1);
163 BN_clear_free(r->dmq1);
164 BN_clear_free(r->iqmp);
165
166 #if defined(FIPS_MODULE) && !defined(OPENSSL_NO_ACVP_TESTS)
167 rsa_acvp_test_free(r->acvp_test);
168 #endif
169
170 #ifndef FIPS_MODULE
171 RSA_PSS_PARAMS_free(r->pss);
172 sk_RSA_PRIME_INFO_pop_free(r->prime_infos, rsa_multip_info_free);
173 #endif
174 BN_BLINDING_free(r->blinding);
175 BN_BLINDING_free(r->mt_blinding);
176 OPENSSL_free(r->bignum_data);
177 OPENSSL_free(r);
178 }
179
180 int RSA_up_ref(RSA *r)
181 {
182 int i;
183
184 if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
185 return 0;
186
187 REF_PRINT_COUNT("RSA", r);
188 REF_ASSERT_ISNT(i < 2);
189 return i > 1 ? 1 : 0;
190 }
191
192 OPENSSL_CTX *rsa_get0_libctx(RSA *r)
193 {
194 return r->libctx;
195 }
196
197 #ifndef FIPS_MODULE
198 int RSA_set_ex_data(RSA *r, int idx, void *arg)
199 {
200 return CRYPTO_set_ex_data(&r->ex_data, idx, arg);
201 }
202
203 void *RSA_get_ex_data(const RSA *r, int idx)
204 {
205 return CRYPTO_get_ex_data(&r->ex_data, idx);
206 }
207 #endif
208
209 /*
210 * Define a scaling constant for our fixed point arithmetic.
211 * This value must be a power of two because the base two logarithm code
212 * makes this assumption. The exponent must also be a multiple of three so
213 * that the scale factor has an exact cube root. Finally, the scale factor
214 * should not be so large that a multiplication of two scaled numbers
215 * overflows a 64 bit unsigned integer.
216 */
217 static const unsigned int scale = 1 << 18;
218 static const unsigned int cbrt_scale = 1 << (2 * 18 / 3);
219
220 /* Define some constants, none exceed 32 bits */
221 static const unsigned int log_2 = 0x02c5c8; /* scale * log(2) */
222 static const unsigned int log_e = 0x05c551; /* scale * log2(M_E) */
223 static const unsigned int c1_923 = 0x07b126; /* scale * 1.923 */
224 static const unsigned int c4_690 = 0x12c28f; /* scale * 4.690 */
225
226 /*
227 * Multiply two scaled integers together and rescale the result.
228 */
229 static ossl_inline uint64_t mul2(uint64_t a, uint64_t b)
230 {
231 return a * b / scale;
232 }
233
234 /*
235 * Calculate the cube root of a 64 bit scaled integer.
236 * Although the cube root of a 64 bit number does fit into a 32 bit unsigned
237 * integer, this is not guaranteed after scaling, so this function has a
238 * 64 bit return. This uses the shifting nth root algorithm with some
239 * algebraic simplifications.
240 */
241 static uint64_t icbrt64(uint64_t x)
242 {
243 uint64_t r = 0;
244 uint64_t b;
245 int s;
246
247 for (s = 63; s >= 0; s -= 3) {
248 r <<= 1;
249 b = 3 * r * (r + 1) + 1;
250 if ((x >> s) >= b) {
251 x -= b << s;
252 r++;
253 }
254 }
255 return r * cbrt_scale;
256 }
257
258 /*
259 * Calculate the natural logarithm of a 64 bit scaled integer.
260 * This is done by calculating a base two logarithm and scaling.
261 * The maximum logarithm (base 2) is 64 and this reduces base e, so
262 * a 32 bit result should not overflow. The argument passed must be
263 * greater than unity so we don't need to handle negative results.
264 */
265 static uint32_t ilog_e(uint64_t v)
266 {
267 uint32_t i, r = 0;
268
269 /*
270 * Scale down the value into the range 1 .. 2.
271 *
272 * If fractional numbers need to be processed, another loop needs
273 * to go here that checks v < scale and if so multiplies it by 2 and
274 * reduces r by scale. This also means making r signed.
275 */
276 while (v >= 2 * scale) {
277 v >>= 1;
278 r += scale;
279 }
280 for (i = scale / 2; i != 0; i /= 2) {
281 v = mul2(v, v);
282 if (v >= 2 * scale) {
283 v >>= 1;
284 r += i;
285 }
286 }
287 r = (r * (uint64_t)scale) / log_e;
288 return r;
289 }
290
291 /*
292 * NIST SP 800-56B rev 2 Appendix D: Maximum Security Strength Estimates for IFC
293 * Modulus Lengths.
294 *
295 * Note that this formula is also referred to in SP800-56A rev3 Appendix D:
296 * for FFC safe prime groups for modp and ffdhe.
297 * After Table 25 and Table 26 it refers to
298 * "The maximum security strength estimates were calculated using the formula in
299 * Section 7.5 of the FIPS 140 IG and rounded to the nearest multiple of eight
300 * bits".
301 *
302 * The formula is:
303 *
304 * E = \frac{1.923 \sqrt[3]{nBits \cdot log_e(2)}
305 * \cdot(log_e(nBits \cdot log_e(2))^{2/3} - 4.69}{log_e(2)}
306 * The two cube roots are merged together here.
307 */
308 uint16_t ifc_ffc_compute_security_bits(int n)
309 {
310 uint64_t x;
311 uint32_t lx;
312 uint16_t y;
313
314 /* Look for common values as listed in SP 800-56B rev 2 Appendix D */
315 switch (n) {
316 case 2048:
317 return 112;
318 case 3072:
319 return 128;
320 case 4096:
321 return 152;
322 case 6144:
323 return 176;
324 case 8192:
325 return 200;
326 }
327 /*
328 * The first incorrect result (i.e. not accurate or off by one low) occurs
329 * for n = 699668. The true value here is 1200. Instead of using this n
330 * as the check threshold, the smallest n such that the correct result is
331 * 1200 is used instead.
332 */
333 if (n >= 687737)
334 return 1200;
335 if (n < 8)
336 return 0;
337
338 x = n * (uint64_t)log_2;
339 lx = ilog_e(x);
340 y = (uint16_t)((mul2(c1_923, icbrt64(mul2(mul2(x, lx), lx))) - c4_690)
341 / log_2);
342 return (y + 4) & ~7;
343 }
344
345
346
347 int RSA_security_bits(const RSA *rsa)
348 {
349 int bits = BN_num_bits(rsa->n);
350
351 #ifndef FIPS_MODULE
352 if (rsa->version == RSA_ASN1_VERSION_MULTI) {
353 /* This ought to mean that we have private key at hand. */
354 int ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos);
355
356 if (ex_primes <= 0 || (ex_primes + 2) > rsa_multip_cap(bits))
357 return 0;
358 }
359 #endif
360 return ifc_ffc_compute_security_bits(bits);
361 }
362
363 int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
364 {
365 /* If the fields n and e in r are NULL, the corresponding input
366 * parameters MUST be non-NULL for n and e. d may be
367 * left NULL (in case only the public key is used).
368 */
369 if ((r->n == NULL && n == NULL)
370 || (r->e == NULL && e == NULL))
371 return 0;
372
373 if (n != NULL) {
374 BN_free(r->n);
375 r->n = n;
376 }
377 if (e != NULL) {
378 BN_free(r->e);
379 r->e = e;
380 }
381 if (d != NULL) {
382 BN_clear_free(r->d);
383 r->d = d;
384 BN_set_flags(r->d, BN_FLG_CONSTTIME);
385 }
386 r->dirty_cnt++;
387
388 return 1;
389 }
390
391 int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
392 {
393 /* If the fields p and q in r are NULL, the corresponding input
394 * parameters MUST be non-NULL.
395 */
396 if ((r->p == NULL && p == NULL)
397 || (r->q == NULL && q == NULL))
398 return 0;
399
400 if (p != NULL) {
401 BN_clear_free(r->p);
402 r->p = p;
403 BN_set_flags(r->p, BN_FLG_CONSTTIME);
404 }
405 if (q != NULL) {
406 BN_clear_free(r->q);
407 r->q = q;
408 BN_set_flags(r->q, BN_FLG_CONSTTIME);
409 }
410 r->dirty_cnt++;
411
412 return 1;
413 }
414
415 int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
416 {
417 /* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input
418 * parameters MUST be non-NULL.
419 */
420 if ((r->dmp1 == NULL && dmp1 == NULL)
421 || (r->dmq1 == NULL && dmq1 == NULL)
422 || (r->iqmp == NULL && iqmp == NULL))
423 return 0;
424
425 if (dmp1 != NULL) {
426 BN_clear_free(r->dmp1);
427 r->dmp1 = dmp1;
428 BN_set_flags(r->dmp1, BN_FLG_CONSTTIME);
429 }
430 if (dmq1 != NULL) {
431 BN_clear_free(r->dmq1);
432 r->dmq1 = dmq1;
433 BN_set_flags(r->dmq1, BN_FLG_CONSTTIME);
434 }
435 if (iqmp != NULL) {
436 BN_clear_free(r->iqmp);
437 r->iqmp = iqmp;
438 BN_set_flags(r->iqmp, BN_FLG_CONSTTIME);
439 }
440 r->dirty_cnt++;
441
442 return 1;
443 }
444
445 #ifndef FIPS_MODULE
446 /*
447 * Is it better to export RSA_PRIME_INFO structure
448 * and related functions to let user pass a triplet?
449 */
450 int RSA_set0_multi_prime_params(RSA *r, BIGNUM *primes[], BIGNUM *exps[],
451 BIGNUM *coeffs[], int pnum)
452 {
453 STACK_OF(RSA_PRIME_INFO) *prime_infos, *old = NULL;
454 RSA_PRIME_INFO *pinfo;
455 int i;
456
457 if (primes == NULL || exps == NULL || coeffs == NULL || pnum == 0)
458 return 0;
459
460 prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum);
461 if (prime_infos == NULL)
462 return 0;
463
464 if (r->prime_infos != NULL)
465 old = r->prime_infos;
466
467 for (i = 0; i < pnum; i++) {
468 pinfo = rsa_multip_info_new();
469 if (pinfo == NULL)
470 goto err;
471 if (primes[i] != NULL && exps[i] != NULL && coeffs[i] != NULL) {
472 BN_clear_free(pinfo->r);
473 BN_clear_free(pinfo->d);
474 BN_clear_free(pinfo->t);
475 pinfo->r = primes[i];
476 pinfo->d = exps[i];
477 pinfo->t = coeffs[i];
478 BN_set_flags(pinfo->r, BN_FLG_CONSTTIME);
479 BN_set_flags(pinfo->d, BN_FLG_CONSTTIME);
480 BN_set_flags(pinfo->t, BN_FLG_CONSTTIME);
481 } else {
482 rsa_multip_info_free(pinfo);
483 goto err;
484 }
485 (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo);
486 }
487
488 r->prime_infos = prime_infos;
489
490 if (!rsa_multip_calc_product(r)) {
491 r->prime_infos = old;
492 goto err;
493 }
494
495 if (old != NULL) {
496 /*
497 * This is hard to deal with, since the old infos could
498 * also be set by this function and r, d, t should not
499 * be freed in that case. So currently, stay consistent
500 * with other *set0* functions: just free it...
501 */
502 sk_RSA_PRIME_INFO_pop_free(old, rsa_multip_info_free);
503 }
504
505 r->version = RSA_ASN1_VERSION_MULTI;
506 r->dirty_cnt++;
507
508 return 1;
509 err:
510 /* r, d, t should not be freed */
511 sk_RSA_PRIME_INFO_pop_free(prime_infos, rsa_multip_info_free_ex);
512 return 0;
513 }
514 #endif
515
516 void RSA_get0_key(const RSA *r,
517 const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
518 {
519 if (n != NULL)
520 *n = r->n;
521 if (e != NULL)
522 *e = r->e;
523 if (d != NULL)
524 *d = r->d;
525 }
526
527 void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
528 {
529 if (p != NULL)
530 *p = r->p;
531 if (q != NULL)
532 *q = r->q;
533 }
534
535 #ifndef FIPS_MODULE
536 int RSA_get_multi_prime_extra_count(const RSA *r)
537 {
538 int pnum;
539
540 pnum = sk_RSA_PRIME_INFO_num(r->prime_infos);
541 if (pnum <= 0)
542 pnum = 0;
543 return pnum;
544 }
545
546 int RSA_get0_multi_prime_factors(const RSA *r, const BIGNUM *primes[])
547 {
548 int pnum, i;
549 RSA_PRIME_INFO *pinfo;
550
551 if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0)
552 return 0;
553
554 /*
555 * return other primes
556 * it's caller's responsibility to allocate oth_primes[pnum]
557 */
558 for (i = 0; i < pnum; i++) {
559 pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
560 primes[i] = pinfo->r;
561 }
562
563 return 1;
564 }
565 #endif
566
567 void RSA_get0_crt_params(const RSA *r,
568 const BIGNUM **dmp1, const BIGNUM **dmq1,
569 const BIGNUM **iqmp)
570 {
571 if (dmp1 != NULL)
572 *dmp1 = r->dmp1;
573 if (dmq1 != NULL)
574 *dmq1 = r->dmq1;
575 if (iqmp != NULL)
576 *iqmp = r->iqmp;
577 }
578
579 #ifndef FIPS_MODULE
580 int RSA_get0_multi_prime_crt_params(const RSA *r, const BIGNUM *exps[],
581 const BIGNUM *coeffs[])
582 {
583 int pnum;
584
585 if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0)
586 return 0;
587
588 /* return other primes */
589 if (exps != NULL || coeffs != NULL) {
590 RSA_PRIME_INFO *pinfo;
591 int i;
592
593 /* it's the user's job to guarantee the buffer length */
594 for (i = 0; i < pnum; i++) {
595 pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
596 if (exps != NULL)
597 exps[i] = pinfo->d;
598 if (coeffs != NULL)
599 coeffs[i] = pinfo->t;
600 }
601 }
602
603 return 1;
604 }
605 #endif
606
607 const BIGNUM *RSA_get0_n(const RSA *r)
608 {
609 return r->n;
610 }
611
612 const BIGNUM *RSA_get0_e(const RSA *r)
613 {
614 return r->e;
615 }
616
617 const BIGNUM *RSA_get0_d(const RSA *r)
618 {
619 return r->d;
620 }
621
622 const BIGNUM *RSA_get0_p(const RSA *r)
623 {
624 return r->p;
625 }
626
627 const BIGNUM *RSA_get0_q(const RSA *r)
628 {
629 return r->q;
630 }
631
632 const BIGNUM *RSA_get0_dmp1(const RSA *r)
633 {
634 return r->dmp1;
635 }
636
637 const BIGNUM *RSA_get0_dmq1(const RSA *r)
638 {
639 return r->dmq1;
640 }
641
642 const BIGNUM *RSA_get0_iqmp(const RSA *r)
643 {
644 return r->iqmp;
645 }
646
647 const RSA_PSS_PARAMS *RSA_get0_pss_params(const RSA *r)
648 {
649 #ifdef FIPS_MODULE
650 return NULL;
651 #else
652 return r->pss;
653 #endif
654 }
655
656 /* Internal */
657 RSA_PSS_PARAMS_30 *rsa_get0_pss_params_30(RSA *r)
658 {
659 return &r->pss_params;
660 }
661
662 void RSA_clear_flags(RSA *r, int flags)
663 {
664 r->flags &= ~flags;
665 }
666
667 int RSA_test_flags(const RSA *r, int flags)
668 {
669 return r->flags & flags;
670 }
671
672 void RSA_set_flags(RSA *r, int flags)
673 {
674 r->flags |= flags;
675 }
676
677 int RSA_get_version(RSA *r)
678 {
679 /* { two-prime(0), multi(1) } */
680 return r->version;
681 }
682
683 #ifndef FIPS_MODULE
684 ENGINE *RSA_get0_engine(const RSA *r)
685 {
686 return r->engine;
687 }
688
689 int RSA_pkey_ctx_ctrl(EVP_PKEY_CTX *ctx, int optype, int cmd, int p1, void *p2)
690 {
691 /* If key type not RSA or RSA-PSS return error */
692 if (ctx != NULL && ctx->pmeth != NULL
693 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
694 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
695 return -1;
696 return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, p1, p2);
697 }
698 #endif
699
700 DEFINE_STACK_OF(BIGNUM)
701
702 int rsa_set0_all_params(RSA *r, const STACK_OF(BIGNUM) *primes,
703 const STACK_OF(BIGNUM) *exps,
704 const STACK_OF(BIGNUM) *coeffs)
705 {
706 #ifndef FIPS_MODULE
707 STACK_OF(RSA_PRIME_INFO) *prime_infos, *old_infos = NULL;
708 #endif
709 int pnum;
710
711 if (primes == NULL || exps == NULL || coeffs == NULL)
712 return 0;
713
714 pnum = sk_BIGNUM_num(primes);
715 if (pnum < 2
716 || pnum != sk_BIGNUM_num(exps)
717 || pnum != sk_BIGNUM_num(coeffs) + 1)
718 return 0;
719
720 if (!RSA_set0_factors(r, sk_BIGNUM_value(primes, 0),
721 sk_BIGNUM_value(primes, 1))
722 || !RSA_set0_crt_params(r, sk_BIGNUM_value(exps, 0),
723 sk_BIGNUM_value(exps, 1),
724 sk_BIGNUM_value(coeffs, 0)))
725 return 0;
726
727 #ifndef FIPS_MODULE
728 old_infos = r->prime_infos;
729 #endif
730
731 if (pnum > 2) {
732 #ifndef FIPS_MODULE
733 int i;
734
735 prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum);
736 if (prime_infos == NULL)
737 return 0;
738
739 for (i = 2; i < pnum; i++) {
740 BIGNUM *prime = sk_BIGNUM_value(primes, i);
741 BIGNUM *exp = sk_BIGNUM_value(exps, i);
742 BIGNUM *coeff = sk_BIGNUM_value(coeffs, i - 1);
743 RSA_PRIME_INFO *pinfo = NULL;
744
745 if (!ossl_assert(prime != NULL && exp != NULL && coeff != NULL))
746 goto err;
747
748 /* Using rsa_multip_info_new() is wasteful, so allocate directly */
749 if ((pinfo = OPENSSL_zalloc(sizeof(*pinfo))) == NULL) {
750 ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
751 goto err;
752 }
753
754 pinfo->r = prime;
755 pinfo->d = exp;
756 pinfo->t = coeff;
757 BN_set_flags(pinfo->r, BN_FLG_CONSTTIME);
758 BN_set_flags(pinfo->d, BN_FLG_CONSTTIME);
759 BN_set_flags(pinfo->t, BN_FLG_CONSTTIME);
760 (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo);
761 }
762
763 r->prime_infos = prime_infos;
764
765 if (!rsa_multip_calc_product(r)) {
766 r->prime_infos = old_infos;
767 goto err;
768 }
769 #else
770 return 0;
771 #endif
772 }
773
774 #ifndef FIPS_MODULE
775 if (old_infos != NULL) {
776 /*
777 * This is hard to deal with, since the old infos could
778 * also be set by this function and r, d, t should not
779 * be freed in that case. So currently, stay consistent
780 * with other *set0* functions: just free it...
781 */
782 sk_RSA_PRIME_INFO_pop_free(old_infos, rsa_multip_info_free);
783 }
784 #endif
785
786 r->version = pnum > 2 ? RSA_ASN1_VERSION_MULTI : RSA_ASN1_VERSION_DEFAULT;
787 r->dirty_cnt++;
788
789 return 1;
790 #ifndef FIPS_MODULE
791 err:
792 /* r, d, t should not be freed */
793 sk_RSA_PRIME_INFO_pop_free(prime_infos, rsa_multip_info_free_ex);
794 return 0;
795 #endif
796 }
797
798 DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
799
800 int rsa_get0_all_params(RSA *r, STACK_OF(BIGNUM_const) *primes,
801 STACK_OF(BIGNUM_const) *exps,
802 STACK_OF(BIGNUM_const) *coeffs)
803 {
804 #ifndef FIPS_MODULE
805 RSA_PRIME_INFO *pinfo;
806 int i, pnum;
807 #endif
808
809 if (r == NULL)
810 return 0;
811
812 /* If |p| is NULL, there are no CRT parameters */
813 if (RSA_get0_p(r) == NULL)
814 return 1;
815
816 sk_BIGNUM_const_push(primes, RSA_get0_p(r));
817 sk_BIGNUM_const_push(primes, RSA_get0_q(r));
818 sk_BIGNUM_const_push(exps, RSA_get0_dmp1(r));
819 sk_BIGNUM_const_push(exps, RSA_get0_dmq1(r));
820 sk_BIGNUM_const_push(coeffs, RSA_get0_iqmp(r));
821
822 #ifndef FIPS_MODULE
823 pnum = RSA_get_multi_prime_extra_count(r);
824 for (i = 0; i < pnum; i++) {
825 pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
826 sk_BIGNUM_const_push(primes, pinfo->r);
827 sk_BIGNUM_const_push(exps, pinfo->d);
828 sk_BIGNUM_const_push(coeffs, pinfo->t);
829 }
830 #endif
831
832 return 1;
833 }
834
835 #ifndef FIPS_MODULE
836 int EVP_PKEY_CTX_set_rsa_padding(EVP_PKEY_CTX *ctx, int pad_mode)
837 {
838 OSSL_PARAM pad_params[2], *p = pad_params;
839
840 if (ctx == NULL) {
841 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
842 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
843 return -2;
844 }
845
846 /* If key type not RSA or RSA-PSS return error */
847 if (ctx->pmeth != NULL
848 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
849 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
850 return -1;
851
852 /* TODO(3.0): Remove this eventually when no more legacy */
853 if ((!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
854 || ctx->op.ciph.ciphprovctx == NULL)
855 && (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
856 || ctx->op.sig.sigprovctx == NULL))
857 return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_RSA_PADDING,
858 pad_mode, NULL);
859
860 *p++ = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_PAD_MODE, &pad_mode);
861 *p++ = OSSL_PARAM_construct_end();
862
863 return EVP_PKEY_CTX_set_params(ctx, pad_params);
864 }
865
866 int EVP_PKEY_CTX_get_rsa_padding(EVP_PKEY_CTX *ctx, int *pad_mode)
867 {
868 OSSL_PARAM pad_params[2], *p = pad_params;
869
870 if (ctx == NULL || pad_mode == NULL) {
871 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
872 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
873 return -2;
874 }
875
876 /* If key type not RSA or RSA-PSS return error */
877 if (ctx->pmeth != NULL
878 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
879 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
880 return -1;
881
882 /* TODO(3.0): Remove this eventually when no more legacy */
883 if ((!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
884 || ctx->op.ciph.ciphprovctx == NULL)
885 && (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
886 || ctx->op.sig.sigprovctx == NULL))
887 return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_GET_RSA_PADDING, 0,
888 pad_mode);
889
890 *p++ = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_PAD_MODE, pad_mode);
891 *p++ = OSSL_PARAM_construct_end();
892
893 if (!EVP_PKEY_CTX_get_params(ctx, pad_params))
894 return 0;
895
896 return 1;
897
898 }
899
900 int EVP_PKEY_CTX_set_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
901 {
902 const char *name;
903
904 if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
905 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
906 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
907 return -2;
908 }
909
910 /* If key type not RSA return error */
911 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
912 return -1;
913
914 /* TODO(3.0): Remove this eventually when no more legacy */
915 if (ctx->op.ciph.ciphprovctx == NULL)
916 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
917 EVP_PKEY_CTRL_RSA_OAEP_MD, 0, (void *)md);
918
919 name = (md == NULL) ? "" : EVP_MD_name(md);
920
921 return EVP_PKEY_CTX_set_rsa_oaep_md_name(ctx, name, NULL);
922 }
923
924 int EVP_PKEY_CTX_set_rsa_oaep_md_name(EVP_PKEY_CTX *ctx, const char *mdname,
925 const char *mdprops)
926 {
927 OSSL_PARAM rsa_params[3], *p = rsa_params;
928
929 if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
930 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
931 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
932 return -2;
933 }
934
935 /* If key type not RSA return error */
936 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
937 return -1;
938
939
940 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST,
941 /*
942 * Cast away the const. This is read
943 * only so should be safe
944 */
945 (char *)mdname, 0);
946 if (mdprops != NULL) {
947 *p++ = OSSL_PARAM_construct_utf8_string(
948 OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST_PROPS,
949 /*
950 * Cast away the const. This is read
951 * only so should be safe
952 */
953 (char *)mdprops, 0);
954 }
955 *p++ = OSSL_PARAM_construct_end();
956
957 return EVP_PKEY_CTX_set_params(ctx, rsa_params);
958 }
959
960 int EVP_PKEY_CTX_get_rsa_oaep_md_name(EVP_PKEY_CTX *ctx, char *name,
961 size_t namelen)
962 {
963 OSSL_PARAM rsa_params[2], *p = rsa_params;
964
965 if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
966 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
967 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
968 return -2;
969 }
970
971 /* If key type not RSA return error */
972 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
973 return -1;
974
975 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST,
976 name, namelen);
977 *p++ = OSSL_PARAM_construct_end();
978
979 if (!EVP_PKEY_CTX_get_params(ctx, rsa_params))
980 return -1;
981
982 return 1;
983 }
984
985 int EVP_PKEY_CTX_get_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
986 {
987 /* 80 should be big enough */
988 char name[80] = "";
989
990 if (ctx == NULL || md == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
991 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
992 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
993 return -2;
994 }
995
996 /* If key type not RSA return error */
997 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
998 return -1;
999
1000 /* TODO(3.0): Remove this eventually when no more legacy */
1001 if (ctx->op.ciph.ciphprovctx == NULL)
1002 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
1003 EVP_PKEY_CTRL_GET_RSA_OAEP_MD, 0, (void *)md);
1004
1005 if (EVP_PKEY_CTX_get_rsa_oaep_md_name(ctx, name, sizeof(name)) <= 0)
1006 return -1;
1007
1008 /* May be NULL meaning "unknown" */
1009 *md = evp_get_digestbyname_ex(ctx->libctx, name);
1010
1011 return 1;
1012 }
1013
1014 static int int_set_rsa_mgf1_md(EVP_PKEY_CTX *ctx,
1015 /* For EVP_PKEY_CTX_ctrl() */
1016 int keytype, int optype, int cmd,
1017 const EVP_MD *md,
1018 /* For EVP_PKEY_CTX_set_params() */
1019 const char *mdname, const char *mdprops)
1020 {
1021 OSSL_PARAM rsa_params[3], *p = rsa_params;
1022
1023 if (ctx == NULL || (ctx->operation & optype) == 0) {
1024 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1025 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1026 return -2;
1027 }
1028
1029 /* If key type not RSA return error */
1030 if (ctx->pmeth != NULL
1031 && (keytype == -1
1032 ? (ctx->pmeth->pkey_id != EVP_PKEY_RSA
1033 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1034 : ctx->pmeth->pkey_id != keytype))
1035 return -1;
1036
1037 /* TODO(3.0): Remove this eventually when no more legacy */
1038 if (cmd != -1) {
1039 if ((EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1040 && ctx->op.ciph.ciphprovctx == NULL)
1041 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
1042 && ctx->op.sig.sigprovctx == NULL)
1043 || (EVP_PKEY_CTX_IS_GEN_OP(ctx)
1044 && ctx->op.keymgmt.genctx == NULL))
1045 return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, (void *)md);
1046
1047 mdname = (md == NULL) ? "" : EVP_MD_name(md);
1048 }
1049
1050
1051 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_MGF1_DIGEST,
1052 /*
1053 * Cast away the const. This is
1054 * read only so should be safe
1055 */
1056 (char *)mdname, 0);
1057 if (mdprops != NULL) {
1058 *p++ =
1059 OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_MGF1_PROPERTIES,
1060 /*
1061 * Cast away the const. This is
1062 * read only so should be safe
1063 */
1064 (char *)mdprops, 0);
1065 }
1066 *p++ = OSSL_PARAM_construct_end();
1067
1068 return EVP_PKEY_CTX_set_params(ctx, rsa_params);
1069 }
1070
1071 int EVP_PKEY_CTX_set_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
1072 {
1073 return int_set_rsa_mgf1_md(ctx, -1,
1074 EVP_PKEY_OP_TYPE_CRYPT | EVP_PKEY_OP_TYPE_SIG,
1075 EVP_PKEY_CTRL_RSA_MGF1_MD, md, NULL, NULL);
1076 }
1077
1078 int EVP_PKEY_CTX_set_rsa_mgf1_md_name(EVP_PKEY_CTX *ctx, const char *mdname,
1079 const char *mdprops)
1080 {
1081 return int_set_rsa_mgf1_md(ctx, -1,
1082 EVP_PKEY_OP_TYPE_CRYPT | EVP_PKEY_OP_TYPE_SIG,
1083 -1, NULL, mdname, mdprops);
1084 }
1085
1086 int EVP_PKEY_CTX_set_rsa_pss_keygen_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
1087 {
1088 return int_set_rsa_mgf1_md(ctx, EVP_PKEY_RSA_PSS,
1089 EVP_PKEY_OP_KEYGEN, EVP_PKEY_CTRL_RSA_MGF1_MD,
1090 md, NULL, NULL);
1091 }
1092
1093 int EVP_PKEY_CTX_set_rsa_pss_keygen_mgf1_md_name(EVP_PKEY_CTX *ctx,
1094 const char *mdname)
1095 {
1096 return int_set_rsa_mgf1_md(ctx, EVP_PKEY_RSA_PSS,
1097 EVP_PKEY_OP_TYPE_CRYPT | EVP_PKEY_OP_TYPE_SIG,
1098 -1, NULL, mdname, NULL);
1099 }
1100
1101 int EVP_PKEY_CTX_get_rsa_mgf1_md_name(EVP_PKEY_CTX *ctx, char *name,
1102 size_t namelen)
1103 {
1104 OSSL_PARAM rsa_params[2], *p = rsa_params;
1105
1106 if (ctx == NULL
1107 || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1108 && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) {
1109 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1110 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1111 return -2;
1112 }
1113
1114 /* If key type not RSA or RSA-PSS return error */
1115 if (ctx->pmeth != NULL
1116 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1117 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1118 return -1;
1119
1120 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_MGF1_DIGEST,
1121 name, namelen);
1122 *p++ = OSSL_PARAM_construct_end();
1123
1124 if (!EVP_PKEY_CTX_get_params(ctx, rsa_params))
1125 return -1;
1126
1127 return 1;
1128 }
1129
1130 int EVP_PKEY_CTX_get_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
1131 {
1132 /* 80 should be big enough */
1133 char name[80] = "";
1134
1135 if (ctx == NULL
1136 || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1137 && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) {
1138 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1139 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1140 return -2;
1141 }
1142
1143 /* If key type not RSA or RSA-PSS return error */
1144 if (ctx->pmeth != NULL
1145 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1146 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1147 return -1;
1148
1149 /* TODO(3.0): Remove this eventually when no more legacy */
1150 if ((EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1151 && ctx->op.ciph.ciphprovctx == NULL)
1152 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
1153 && ctx->op.sig.sigprovctx == NULL))
1154 return EVP_PKEY_CTX_ctrl(ctx, -1,
1155 EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
1156 EVP_PKEY_CTRL_GET_RSA_MGF1_MD, 0, (void *)md);
1157
1158 if (EVP_PKEY_CTX_get_rsa_mgf1_md_name(ctx, name, sizeof(name)) <= 0)
1159 return -1;
1160
1161 /* May be NULL meaning "unknown" */
1162 *md = evp_get_digestbyname_ex(ctx->libctx, name);
1163
1164 return 1;
1165 }
1166
1167 int EVP_PKEY_CTX_set0_rsa_oaep_label(EVP_PKEY_CTX *ctx, void *label, int llen)
1168 {
1169 OSSL_PARAM rsa_params[2], *p = rsa_params;
1170
1171 if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
1172 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1173 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1174 return -2;
1175 }
1176
1177 /* If key type not RSA return error */
1178 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
1179 return -1;
1180
1181 /* TODO(3.0): Remove this eventually when no more legacy */
1182 if (ctx->op.ciph.ciphprovctx == NULL)
1183 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
1184 EVP_PKEY_CTRL_RSA_OAEP_LABEL, llen,
1185 (void *)label);
1186
1187 *p++ = OSSL_PARAM_construct_octet_string(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL,
1188 /*
1189 * Cast away the const. This is
1190 * read only so should be safe
1191 */
1192 (void *)label,
1193 (size_t)llen);
1194 *p++ = OSSL_PARAM_construct_end();
1195
1196 if (!EVP_PKEY_CTX_set_params(ctx, rsa_params))
1197 return 0;
1198
1199 OPENSSL_free(label);
1200 return 1;
1201 }
1202
1203 int EVP_PKEY_CTX_get0_rsa_oaep_label(EVP_PKEY_CTX *ctx, unsigned char **label)
1204 {
1205 OSSL_PARAM rsa_params[3], *p = rsa_params;
1206 size_t labellen;
1207
1208 if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
1209 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1210 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1211 return -2;
1212 }
1213
1214 /* If key type not RSA return error */
1215 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
1216 return -1;
1217
1218 /* TODO(3.0): Remove this eventually when no more legacy */
1219 if (ctx->op.ciph.ciphprovctx == NULL)
1220 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
1221 EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL, 0,
1222 (void *)label);
1223
1224 *p++ = OSSL_PARAM_construct_octet_ptr(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL,
1225 (void **)label, 0);
1226 *p++ = OSSL_PARAM_construct_size_t(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL_LEN,
1227 &labellen);
1228 *p++ = OSSL_PARAM_construct_end();
1229
1230 if (!EVP_PKEY_CTX_get_params(ctx, rsa_params))
1231 return -1;
1232
1233 if (labellen > INT_MAX)
1234 return -1;
1235
1236 return (int)labellen;
1237 }
1238
1239 static int int_set_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int saltlen,
1240 int keytype, int optype)
1241 {
1242 OSSL_PARAM pad_params[2], *p = pad_params;
1243
1244 if (ctx == NULL || (ctx->operation & optype) == 0) {
1245 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1246 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1247 return -2;
1248 }
1249
1250 /* If key type not RSA or RSA-PSS return error */
1251 if (ctx->pmeth != NULL
1252 && (keytype == -1
1253 ? (ctx->pmeth->pkey_id != EVP_PKEY_RSA
1254 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1255 : ctx->pmeth->pkey_id != keytype))
1256 return -1;
1257
1258 /* TODO(3.0): Remove this eventually when no more legacy */
1259 if ((EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
1260 && ctx->op.sig.sigprovctx == NULL)
1261 || (EVP_PKEY_CTX_IS_GEN_OP(ctx)
1262 && ctx->op.keymgmt.genctx == NULL))
1263 return EVP_PKEY_CTX_ctrl(ctx, keytype, optype,
1264 EVP_PKEY_CTRL_RSA_PSS_SALTLEN,
1265 saltlen, NULL);
1266
1267 *p++ =
1268 OSSL_PARAM_construct_int(OSSL_SIGNATURE_PARAM_PSS_SALTLEN, &saltlen);
1269 *p++ = OSSL_PARAM_construct_end();
1270
1271 return EVP_PKEY_CTX_set_params(ctx, pad_params);
1272 }
1273
1274 int EVP_PKEY_CTX_set_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int saltlen)
1275 {
1276 return int_set_rsa_pss_saltlen(ctx, saltlen, -1, EVP_PKEY_OP_TYPE_SIG);
1277 }
1278
1279 int EVP_PKEY_CTX_set_rsa_pss_keygen_saltlen(EVP_PKEY_CTX *ctx, int saltlen)
1280 {
1281 return int_set_rsa_pss_saltlen(ctx, saltlen, EVP_PKEY_RSA_PSS,
1282 EVP_PKEY_OP_KEYGEN);
1283 }
1284
1285 int EVP_PKEY_CTX_get_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int *saltlen)
1286 {
1287 OSSL_PARAM pad_params[2], *p = pad_params;
1288
1289 if (ctx == NULL || saltlen == NULL) {
1290 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1291 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1292 return -2;
1293 }
1294
1295 /* If key type not RSA or RSA-PSS return error */
1296 if (ctx->pmeth != NULL
1297 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1298 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1299 return -1;
1300
1301 /* TODO(3.0): Remove this eventually when no more legacy */
1302 if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
1303 || ctx->op.sig.sigprovctx == NULL)
1304 return EVP_PKEY_CTX_ctrl(ctx, -1, -1,
1305 EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN,
1306 0, saltlen);
1307
1308 *p++ =
1309 OSSL_PARAM_construct_int(OSSL_SIGNATURE_PARAM_PSS_SALTLEN, saltlen);
1310 *p++ = OSSL_PARAM_construct_end();
1311
1312 if (!EVP_PKEY_CTX_get_params(ctx, pad_params))
1313 return 0;
1314
1315 return 1;
1316
1317 }
1318
1319 int EVP_PKEY_CTX_set_rsa_keygen_bits(EVP_PKEY_CTX *ctx, int bits)
1320 {
1321 OSSL_PARAM params[2], *p = params;
1322 size_t bits2 = bits;
1323
1324 if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
1325 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1326 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1327 return -2;
1328 }
1329
1330 /* If key type not RSA return error */
1331 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
1332 return -1;
1333
1334 /* TODO(3.0): Remove this eventually when no more legacy */
1335 if (ctx->op.keymgmt.genctx == NULL)
1336 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN,
1337 EVP_PKEY_CTRL_RSA_KEYGEN_BITS, bits, NULL);
1338
1339 *p++ = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_RSA_BITS, &bits2);
1340 *p++ = OSSL_PARAM_construct_end();
1341
1342 if (!EVP_PKEY_CTX_set_params(ctx, params))
1343 return 0;
1344
1345 return 1;
1346 }
1347
1348 static int evp_pkey_ctx_set_rsa_keygen_pubexp_intern(EVP_PKEY_CTX *ctx,
1349 BIGNUM *pubexp,
1350 int copy)
1351 {
1352 OSSL_PARAM_BLD *tmpl;
1353 OSSL_PARAM *params;
1354 int ret;
1355
1356 if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
1357 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1358 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1359 return -2;
1360 }
1361
1362 /* If key type not RSA return error */
1363 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
1364 return -1;
1365
1366 /* TODO(3.0): Remove this eventually when no more legacy */
1367 if (ctx->op.keymgmt.genctx == NULL) {
1368 if (copy == 1)
1369 pubexp = BN_dup(pubexp);
1370 ret = EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN,
1371 EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP, 0, pubexp);
1372 if ((copy == 1) && (ret <= 0))
1373 BN_free(pubexp);
1374 return ret;
1375 }
1376
1377 if ((tmpl = OSSL_PARAM_BLD_new()) == NULL)
1378 return 0;
1379 if (!OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_RSA_E, pubexp)
1380 || (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL) {
1381 OSSL_PARAM_BLD_free(tmpl);
1382 return 0;
1383 }
1384 OSSL_PARAM_BLD_free(tmpl);
1385
1386 ret = EVP_PKEY_CTX_set_params(ctx, params);
1387 OSSL_PARAM_BLD_free_params(params);
1388
1389 /*
1390 * Satisfy memory semantics for pre-3.0 callers of
1391 * EVP_PKEY_CTX_set_rsa_keygen_pubexp(): their expectation is that input
1392 * pubexp BIGNUM becomes managed by the EVP_PKEY_CTX on success.
1393 */
1394 if ((copy == 0) && (ret > 0))
1395 ctx->rsa_pubexp = pubexp;
1396
1397 return ret;
1398 }
1399
1400 int EVP_PKEY_CTX_set_rsa_keygen_pubexp(EVP_PKEY_CTX *ctx, BIGNUM *pubexp)
1401 {
1402 return evp_pkey_ctx_set_rsa_keygen_pubexp_intern(ctx, pubexp, 0);
1403 }
1404
1405 int EVP_PKEY_CTX_set1_rsa_keygen_pubexp(EVP_PKEY_CTX *ctx, BIGNUM *pubexp)
1406 {
1407 return evp_pkey_ctx_set_rsa_keygen_pubexp_intern(ctx, pubexp, 1);
1408 }
1409
1410 int EVP_PKEY_CTX_set_rsa_keygen_primes(EVP_PKEY_CTX *ctx, int primes)
1411 {
1412 OSSL_PARAM params[2], *p = params;
1413 size_t primes2 = primes;
1414
1415 if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
1416 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1417 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1418 return -2;
1419 }
1420
1421 /* If key type not RSA return error */
1422 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
1423 return -1;
1424
1425 /* TODO(3.0): Remove this eventually when no more legacy */
1426 if (ctx->op.keymgmt.genctx == NULL)
1427 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN,
1428 EVP_PKEY_CTRL_RSA_KEYGEN_PRIMES, primes,
1429 NULL);
1430
1431 *p++ = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_RSA_PRIMES, &primes2);
1432 *p++ = OSSL_PARAM_construct_end();
1433
1434 if (!EVP_PKEY_CTX_set_params(ctx, params))
1435 return 0;
1436
1437 return 1;
1438 }
1439 #endif