rsa_lib.c 40 KB

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