rsa_lib.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /*
  2. * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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. #include <stdio.h>
  10. #include <openssl/crypto.h>
  11. #include "internal/cryptlib.h"
  12. #include "internal/refcount.h"
  13. #include "internal/bn_int.h"
  14. #include <openssl/engine.h>
  15. #include <openssl/evp.h>
  16. #include "internal/evp_int.h"
  17. #include "rsa_locl.h"
  18. RSA *RSA_new(void)
  19. {
  20. return RSA_new_method(NULL);
  21. }
  22. const RSA_METHOD *RSA_get_method(const RSA *rsa)
  23. {
  24. return rsa->meth;
  25. }
  26. int RSA_set_method(RSA *rsa, const RSA_METHOD *meth)
  27. {
  28. /*
  29. * NB: The caller is specifically setting a method, so it's not up to us
  30. * to deal with which ENGINE it comes from.
  31. */
  32. const RSA_METHOD *mtmp;
  33. mtmp = rsa->meth;
  34. if (mtmp->finish)
  35. mtmp->finish(rsa);
  36. #ifndef OPENSSL_NO_ENGINE
  37. ENGINE_finish(rsa->engine);
  38. rsa->engine = NULL;
  39. #endif
  40. rsa->meth = meth;
  41. if (meth->init)
  42. meth->init(rsa);
  43. return 1;
  44. }
  45. RSA *RSA_new_method(ENGINE *engine)
  46. {
  47. RSA *ret = OPENSSL_zalloc(sizeof(*ret));
  48. if (ret == NULL) {
  49. RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
  50. return NULL;
  51. }
  52. ret->references = 1;
  53. ret->lock = CRYPTO_THREAD_lock_new();
  54. if (ret->lock == NULL) {
  55. RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
  56. OPENSSL_free(ret);
  57. return NULL;
  58. }
  59. ret->meth = RSA_get_default_method();
  60. #ifndef OPENSSL_NO_ENGINE
  61. ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
  62. if (engine) {
  63. if (!ENGINE_init(engine)) {
  64. RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
  65. goto err;
  66. }
  67. ret->engine = engine;
  68. } else {
  69. ret->engine = ENGINE_get_default_RSA();
  70. }
  71. if (ret->engine) {
  72. ret->meth = ENGINE_get_RSA(ret->engine);
  73. if (ret->meth == NULL) {
  74. RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
  75. goto err;
  76. }
  77. }
  78. #endif
  79. ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
  80. if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data)) {
  81. goto err;
  82. }
  83. if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
  84. RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_INIT_FAIL);
  85. goto err;
  86. }
  87. return ret;
  88. err:
  89. RSA_free(ret);
  90. return NULL;
  91. }
  92. void RSA_free(RSA *r)
  93. {
  94. int i;
  95. if (r == NULL)
  96. return;
  97. CRYPTO_DOWN_REF(&r->references, &i, r->lock);
  98. REF_PRINT_COUNT("RSA", r);
  99. if (i > 0)
  100. return;
  101. REF_ASSERT_ISNT(i < 0);
  102. if (r->meth->finish)
  103. r->meth->finish(r);
  104. #ifndef OPENSSL_NO_ENGINE
  105. ENGINE_finish(r->engine);
  106. #endif
  107. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data);
  108. CRYPTO_THREAD_lock_free(r->lock);
  109. BN_clear_free(r->n);
  110. BN_clear_free(r->e);
  111. BN_clear_free(r->d);
  112. BN_clear_free(r->p);
  113. BN_clear_free(r->q);
  114. BN_clear_free(r->dmp1);
  115. BN_clear_free(r->dmq1);
  116. BN_clear_free(r->iqmp);
  117. RSA_PSS_PARAMS_free(r->pss);
  118. sk_RSA_PRIME_INFO_pop_free(r->prime_infos, rsa_multip_info_free);
  119. BN_BLINDING_free(r->blinding);
  120. BN_BLINDING_free(r->mt_blinding);
  121. OPENSSL_free(r->bignum_data);
  122. OPENSSL_free(r);
  123. }
  124. int RSA_up_ref(RSA *r)
  125. {
  126. int i;
  127. if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
  128. return 0;
  129. REF_PRINT_COUNT("RSA", r);
  130. REF_ASSERT_ISNT(i < 2);
  131. return i > 1 ? 1 : 0;
  132. }
  133. int RSA_set_ex_data(RSA *r, int idx, void *arg)
  134. {
  135. return CRYPTO_set_ex_data(&r->ex_data, idx, arg);
  136. }
  137. void *RSA_get_ex_data(const RSA *r, int idx)
  138. {
  139. return CRYPTO_get_ex_data(&r->ex_data, idx);
  140. }
  141. int RSA_security_bits(const RSA *rsa)
  142. {
  143. int bits = BN_num_bits(rsa->n);
  144. if (rsa->version == RSA_ASN1_VERSION_MULTI) {
  145. /* This ought to mean that we have private key at hand. */
  146. int ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos);
  147. if (ex_primes <= 0 || (ex_primes + 2) > rsa_multip_cap(bits))
  148. return 0;
  149. }
  150. return BN_security_bits(bits, -1);
  151. }
  152. int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
  153. {
  154. /* If the fields n and e in r are NULL, the corresponding input
  155. * parameters MUST be non-NULL for n and e. d may be
  156. * left NULL (in case only the public key is used).
  157. */
  158. if ((r->n == NULL && n == NULL)
  159. || (r->e == NULL && e == NULL))
  160. return 0;
  161. if (n != NULL) {
  162. BN_free(r->n);
  163. r->n = n;
  164. }
  165. if (e != NULL) {
  166. BN_free(r->e);
  167. r->e = e;
  168. }
  169. if (d != NULL) {
  170. BN_free(r->d);
  171. r->d = d;
  172. }
  173. return 1;
  174. }
  175. int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
  176. {
  177. /* If the fields p and q in r are NULL, the corresponding input
  178. * parameters MUST be non-NULL.
  179. */
  180. if ((r->p == NULL && p == NULL)
  181. || (r->q == NULL && q == NULL))
  182. return 0;
  183. if (p != NULL) {
  184. BN_free(r->p);
  185. r->p = p;
  186. }
  187. if (q != NULL) {
  188. BN_free(r->q);
  189. r->q = q;
  190. }
  191. return 1;
  192. }
  193. int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
  194. {
  195. /* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input
  196. * parameters MUST be non-NULL.
  197. */
  198. if ((r->dmp1 == NULL && dmp1 == NULL)
  199. || (r->dmq1 == NULL && dmq1 == NULL)
  200. || (r->iqmp == NULL && iqmp == NULL))
  201. return 0;
  202. if (dmp1 != NULL) {
  203. BN_free(r->dmp1);
  204. r->dmp1 = dmp1;
  205. }
  206. if (dmq1 != NULL) {
  207. BN_free(r->dmq1);
  208. r->dmq1 = dmq1;
  209. }
  210. if (iqmp != NULL) {
  211. BN_free(r->iqmp);
  212. r->iqmp = iqmp;
  213. }
  214. return 1;
  215. }
  216. /*
  217. * Is it better to export RSA_PRIME_INFO structure
  218. * and related functions to let user pass a triplet?
  219. */
  220. int RSA_set0_multi_prime_params(RSA *r, BIGNUM *primes[], BIGNUM *exps[],
  221. BIGNUM *coeffs[], int pnum)
  222. {
  223. STACK_OF(RSA_PRIME_INFO) *prime_infos, *old = NULL;
  224. RSA_PRIME_INFO *pinfo;
  225. int i;
  226. if (primes == NULL || exps == NULL || coeffs == NULL || pnum == 0)
  227. return 0;
  228. prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum);
  229. if (prime_infos == NULL)
  230. return 0;
  231. if (r->prime_infos != NULL)
  232. old = r->prime_infos;
  233. for (i = 0; i < pnum; i++) {
  234. pinfo = rsa_multip_info_new();
  235. if (pinfo == NULL)
  236. goto err;
  237. if (primes[i] != NULL && exps[i] != NULL && coeffs[i] != NULL) {
  238. BN_free(pinfo->r);
  239. BN_free(pinfo->d);
  240. BN_free(pinfo->t);
  241. pinfo->r = primes[i];
  242. pinfo->d = exps[i];
  243. pinfo->t = coeffs[i];
  244. } else {
  245. rsa_multip_info_free(pinfo);
  246. goto err;
  247. }
  248. (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo);
  249. }
  250. r->prime_infos = prime_infos;
  251. if (!rsa_multip_calc_product(r)) {
  252. r->prime_infos = old;
  253. goto err;
  254. }
  255. if (old != NULL) {
  256. /*
  257. * This is hard to deal with, since the old infos could
  258. * also be set by this function and r, d, t should not
  259. * be freed in that case. So currently, stay consistent
  260. * with other *set0* functions: just free it...
  261. */
  262. sk_RSA_PRIME_INFO_pop_free(old, rsa_multip_info_free);
  263. }
  264. r->version = RSA_ASN1_VERSION_MULTI;
  265. return 1;
  266. err:
  267. /* r, d, t should not be freed */
  268. sk_RSA_PRIME_INFO_pop_free(prime_infos, rsa_multip_info_free_ex);
  269. return 0;
  270. }
  271. void RSA_get0_key(const RSA *r,
  272. const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
  273. {
  274. if (n != NULL)
  275. *n = r->n;
  276. if (e != NULL)
  277. *e = r->e;
  278. if (d != NULL)
  279. *d = r->d;
  280. }
  281. void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
  282. {
  283. if (p != NULL)
  284. *p = r->p;
  285. if (q != NULL)
  286. *q = r->q;
  287. }
  288. int RSA_get_multi_prime_extra_count(const RSA *r)
  289. {
  290. int pnum;
  291. pnum = sk_RSA_PRIME_INFO_num(r->prime_infos);
  292. if (pnum <= 0)
  293. pnum = 0;
  294. return pnum;
  295. }
  296. int RSA_get0_multi_prime_factors(const RSA *r, const BIGNUM *primes[])
  297. {
  298. int pnum, i;
  299. RSA_PRIME_INFO *pinfo;
  300. if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0)
  301. return 0;
  302. /*
  303. * return other primes
  304. * it's caller's responsibility to allocate oth_primes[pnum]
  305. */
  306. for (i = 0; i < pnum; i++) {
  307. pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
  308. primes[i] = pinfo->r;
  309. }
  310. return 1;
  311. }
  312. void RSA_get0_crt_params(const RSA *r,
  313. const BIGNUM **dmp1, const BIGNUM **dmq1,
  314. const BIGNUM **iqmp)
  315. {
  316. if (dmp1 != NULL)
  317. *dmp1 = r->dmp1;
  318. if (dmq1 != NULL)
  319. *dmq1 = r->dmq1;
  320. if (iqmp != NULL)
  321. *iqmp = r->iqmp;
  322. }
  323. int RSA_get0_multi_prime_crt_params(const RSA *r, const BIGNUM *exps[],
  324. const BIGNUM *coeffs[])
  325. {
  326. int pnum;
  327. if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0)
  328. return 0;
  329. /* return other primes */
  330. if (exps != NULL || coeffs != NULL) {
  331. RSA_PRIME_INFO *pinfo;
  332. int i;
  333. /* it's the user's job to guarantee the buffer length */
  334. for (i = 0; i < pnum; i++) {
  335. pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
  336. if (exps != NULL)
  337. exps[i] = pinfo->d;
  338. if (coeffs != NULL)
  339. coeffs[i] = pinfo->t;
  340. }
  341. }
  342. return 1;
  343. }
  344. const BIGNUM *RSA_get0_n(const RSA *r)
  345. {
  346. return r->n;
  347. }
  348. const BIGNUM *RSA_get0_e(const RSA *r)
  349. {
  350. return r->e;
  351. }
  352. const BIGNUM *RSA_get0_d(const RSA *r)
  353. {
  354. return r->d;
  355. }
  356. const BIGNUM *RSA_get0_p(const RSA *r)
  357. {
  358. return r->p;
  359. }
  360. const BIGNUM *RSA_get0_q(const RSA *r)
  361. {
  362. return r->q;
  363. }
  364. const BIGNUM *RSA_get0_dmp1(const RSA *r)
  365. {
  366. return r->dmp1;
  367. }
  368. const BIGNUM *RSA_get0_dmq1(const RSA *r)
  369. {
  370. return r->dmq1;
  371. }
  372. const BIGNUM *RSA_get0_iqmp(const RSA *r)
  373. {
  374. return r->iqmp;
  375. }
  376. void RSA_clear_flags(RSA *r, int flags)
  377. {
  378. r->flags &= ~flags;
  379. }
  380. int RSA_test_flags(const RSA *r, int flags)
  381. {
  382. return r->flags & flags;
  383. }
  384. void RSA_set_flags(RSA *r, int flags)
  385. {
  386. r->flags |= flags;
  387. }
  388. int RSA_get_version(RSA *r)
  389. {
  390. /* { two-prime(0), multi(1) } */
  391. return r->version;
  392. }
  393. ENGINE *RSA_get0_engine(const RSA *r)
  394. {
  395. return r->engine;
  396. }
  397. int RSA_pkey_ctx_ctrl(EVP_PKEY_CTX *ctx, int optype, int cmd, int p1, void *p2)
  398. {
  399. /* If key type not RSA or RSA-PSS return error */
  400. if (ctx != NULL && ctx->pmeth != NULL
  401. && ctx->pmeth->pkey_id != EVP_PKEY_RSA
  402. && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
  403. return -1;
  404. return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, p1, p2);
  405. }