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