dsa_lib.c 7.7 KB

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