rsa_lib.c 41 KB

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