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