dsa_lib.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * Copyright 1995-2018 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. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include "internal/refcount.h"
  12. #include <openssl/bn.h>
  13. #include "dsa_local.h"
  14. #include <openssl/asn1.h>
  15. #include <openssl/engine.h>
  16. #include <openssl/dh.h>
  17. #include "crypto/dsa.h"
  18. #ifndef FIPS_MODE
  19. int DSA_set_ex_data(DSA *d, int idx, void *arg)
  20. {
  21. return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
  22. }
  23. void *DSA_get_ex_data(DSA *d, int idx)
  24. {
  25. return CRYPTO_get_ex_data(&d->ex_data, idx);
  26. }
  27. #ifndef OPENSSL_NO_DH
  28. DH *DSA_dup_DH(const DSA *r)
  29. {
  30. /*
  31. * DSA has p, q, g, optional pub_key, optional priv_key. DH has p,
  32. * optional length, g, optional pub_key, optional priv_key, optional q.
  33. */
  34. DH *ret = NULL;
  35. BIGNUM *p = NULL, *q = NULL, *g = NULL, *pub_key = NULL, *priv_key = NULL;
  36. if (r == NULL)
  37. goto err;
  38. ret = DH_new();
  39. if (ret == NULL)
  40. goto err;
  41. if (r->p != NULL || r->g != NULL || r->q != NULL) {
  42. if (r->p == NULL || r->g == NULL || r->q == NULL) {
  43. /* Shouldn't happen */
  44. goto err;
  45. }
  46. p = BN_dup(r->p);
  47. g = BN_dup(r->g);
  48. q = BN_dup(r->q);
  49. if (p == NULL || g == NULL || q == NULL || !DH_set0_pqg(ret, p, q, g))
  50. goto err;
  51. p = g = q = NULL;
  52. }
  53. if (r->pub_key != NULL) {
  54. pub_key = BN_dup(r->pub_key);
  55. if (pub_key == NULL)
  56. goto err;
  57. if (r->priv_key != NULL) {
  58. priv_key = BN_dup(r->priv_key);
  59. if (priv_key == NULL)
  60. goto err;
  61. }
  62. if (!DH_set0_key(ret, pub_key, priv_key))
  63. goto err;
  64. } else if (r->priv_key != NULL) {
  65. /* Shouldn't happen */
  66. goto err;
  67. }
  68. return ret;
  69. err:
  70. BN_free(p);
  71. BN_free(g);
  72. BN_free(q);
  73. BN_free(pub_key);
  74. BN_free(priv_key);
  75. DH_free(ret);
  76. return NULL;
  77. }
  78. #endif
  79. const BIGNUM *DSA_get0_p(const DSA *d)
  80. {
  81. return d->p;
  82. }
  83. const BIGNUM *DSA_get0_q(const DSA *d)
  84. {
  85. return d->q;
  86. }
  87. const BIGNUM *DSA_get0_g(const DSA *d)
  88. {
  89. return d->g;
  90. }
  91. const BIGNUM *DSA_get0_pub_key(const DSA *d)
  92. {
  93. return d->pub_key;
  94. }
  95. const BIGNUM *DSA_get0_priv_key(const DSA *d)
  96. {
  97. return d->priv_key;
  98. }
  99. void DSA_clear_flags(DSA *d, int flags)
  100. {
  101. d->flags &= ~flags;
  102. }
  103. int DSA_test_flags(const DSA *d, int flags)
  104. {
  105. return d->flags & flags;
  106. }
  107. void DSA_set_flags(DSA *d, int flags)
  108. {
  109. d->flags |= flags;
  110. }
  111. ENGINE *DSA_get0_engine(DSA *d)
  112. {
  113. return d->engine;
  114. }
  115. int DSA_set_method(DSA *dsa, const DSA_METHOD *meth)
  116. {
  117. /*
  118. * NB: The caller is specifically setting a method, so it's not up to us
  119. * to deal with which ENGINE it comes from.
  120. */
  121. const DSA_METHOD *mtmp;
  122. mtmp = dsa->meth;
  123. if (mtmp->finish)
  124. mtmp->finish(dsa);
  125. #ifndef OPENSSL_NO_ENGINE
  126. ENGINE_finish(dsa->engine);
  127. dsa->engine = NULL;
  128. #endif
  129. dsa->meth = meth;
  130. if (meth->init)
  131. meth->init(dsa);
  132. return 1;
  133. }
  134. #endif /* FIPS_MODE */
  135. const DSA_METHOD *DSA_get_method(DSA *d)
  136. {
  137. return d->meth;
  138. }
  139. static DSA *dsa_new_method(OPENSSL_CTX *libctx, ENGINE *engine)
  140. {
  141. DSA *ret = OPENSSL_zalloc(sizeof(*ret));
  142. if (ret == NULL) {
  143. DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
  144. return NULL;
  145. }
  146. ret->references = 1;
  147. ret->lock = CRYPTO_THREAD_lock_new();
  148. if (ret->lock == NULL) {
  149. DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
  150. OPENSSL_free(ret);
  151. return NULL;
  152. }
  153. ret->meth = DSA_get_default_method();
  154. #if !defined(FIPS_MODE) && !defined(OPENSSL_NO_ENGINE)
  155. ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW; /* early default init */
  156. if (engine) {
  157. if (!ENGINE_init(engine)) {
  158. DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_ENGINE_LIB);
  159. goto err;
  160. }
  161. ret->engine = engine;
  162. } else
  163. ret->engine = ENGINE_get_default_DSA();
  164. if (ret->engine) {
  165. ret->meth = ENGINE_get_DSA(ret->engine);
  166. if (ret->meth == NULL) {
  167. DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_ENGINE_LIB);
  168. goto err;
  169. }
  170. }
  171. #endif
  172. ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW;
  173. #ifndef FIPS_MODE
  174. if (!crypto_new_ex_data_ex(libctx, CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data))
  175. goto err;
  176. #endif
  177. if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
  178. DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_INIT_FAIL);
  179. goto err;
  180. }
  181. return ret;
  182. err:
  183. DSA_free(ret);
  184. return NULL;
  185. }
  186. DSA *DSA_new_method(ENGINE *engine)
  187. {
  188. return dsa_new_method(NULL, engine);
  189. }
  190. DSA *DSA_new(void)
  191. {
  192. return DSA_new_method(NULL);
  193. }
  194. void DSA_free(DSA *r)
  195. {
  196. int i;
  197. if (r == NULL)
  198. return;
  199. CRYPTO_DOWN_REF(&r->references, &i, r->lock);
  200. REF_PRINT_COUNT("DSA", r);
  201. if (i > 0)
  202. return;
  203. REF_ASSERT_ISNT(i < 0);
  204. if (r->meth != NULL && r->meth->finish != NULL)
  205. r->meth->finish(r);
  206. #if !defined(FIPS_MODE) && !defined(OPENSSL_NO_ENGINE)
  207. ENGINE_finish(r->engine);
  208. #endif
  209. #ifndef FIPS_MODE
  210. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, r, &r->ex_data);
  211. #endif
  212. CRYPTO_THREAD_lock_free(r->lock);
  213. BN_clear_free(r->p);
  214. BN_clear_free(r->q);
  215. BN_clear_free(r->g);
  216. BN_clear_free(r->pub_key);
  217. BN_clear_free(r->priv_key);
  218. OPENSSL_free(r);
  219. }
  220. int DSA_up_ref(DSA *r)
  221. {
  222. int i;
  223. if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
  224. return 0;
  225. REF_PRINT_COUNT("DSA", r);
  226. REF_ASSERT_ISNT(i < 2);
  227. return ((i > 1) ? 1 : 0);
  228. }
  229. void DSA_get0_pqg(const DSA *d,
  230. const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
  231. {
  232. if (p != NULL)
  233. *p = d->p;
  234. if (q != NULL)
  235. *q = d->q;
  236. if (g != NULL)
  237. *g = d->g;
  238. }
  239. int DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g)
  240. {
  241. /* If the fields p, q and g in d are NULL, the corresponding input
  242. * parameters MUST be non-NULL.
  243. */
  244. if ((d->p == NULL && p == NULL)
  245. || (d->q == NULL && q == NULL)
  246. || (d->g == NULL && g == NULL))
  247. return 0;
  248. if (p != NULL) {
  249. BN_free(d->p);
  250. d->p = p;
  251. }
  252. if (q != NULL) {
  253. BN_free(d->q);
  254. d->q = q;
  255. }
  256. if (g != NULL) {
  257. BN_free(d->g);
  258. d->g = g;
  259. }
  260. d->dirty_cnt++;
  261. return 1;
  262. }
  263. void DSA_get0_key(const DSA *d,
  264. const BIGNUM **pub_key, const BIGNUM **priv_key)
  265. {
  266. if (pub_key != NULL)
  267. *pub_key = d->pub_key;
  268. if (priv_key != NULL)
  269. *priv_key = d->priv_key;
  270. }
  271. int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key)
  272. {
  273. /* If the field pub_key in d is NULL, the corresponding input
  274. * parameters MUST be non-NULL. The priv_key field may
  275. * be left NULL.
  276. */
  277. if (d->pub_key == NULL && pub_key == NULL)
  278. return 0;
  279. if (pub_key != NULL) {
  280. BN_free(d->pub_key);
  281. d->pub_key = pub_key;
  282. }
  283. if (priv_key != NULL) {
  284. BN_free(d->priv_key);
  285. d->priv_key = priv_key;
  286. }
  287. d->dirty_cnt++;
  288. return 1;
  289. }
  290. int DSA_security_bits(const DSA *d)
  291. {
  292. if (d->p && d->q)
  293. return BN_security_bits(BN_num_bits(d->p), BN_num_bits(d->q));
  294. return -1;
  295. }
  296. int DSA_bits(const DSA *dsa)
  297. {
  298. return BN_num_bits(dsa->p);
  299. }