2
0

dsa_lib.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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_local.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. goto err;
  83. }
  84. return ret;
  85. err:
  86. DSA_free(ret);
  87. return NULL;
  88. }
  89. void DSA_free(DSA *r)
  90. {
  91. int i;
  92. if (r == NULL)
  93. return;
  94. CRYPTO_DOWN_REF(&r->references, &i, r->lock);
  95. REF_PRINT_COUNT("DSA", r);
  96. if (i > 0)
  97. return;
  98. REF_ASSERT_ISNT(i < 0);
  99. if (r->meth != NULL && r->meth->finish != NULL)
  100. r->meth->finish(r);
  101. #ifndef OPENSSL_NO_ENGINE
  102. ENGINE_finish(r->engine);
  103. #endif
  104. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, r, &r->ex_data);
  105. CRYPTO_THREAD_lock_free(r->lock);
  106. BN_clear_free(r->p);
  107. BN_clear_free(r->q);
  108. BN_clear_free(r->g);
  109. BN_clear_free(r->pub_key);
  110. BN_clear_free(r->priv_key);
  111. OPENSSL_free(r);
  112. }
  113. int DSA_up_ref(DSA *r)
  114. {
  115. int i;
  116. if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
  117. return 0;
  118. REF_PRINT_COUNT("DSA", r);
  119. REF_ASSERT_ISNT(i < 2);
  120. return ((i > 1) ? 1 : 0);
  121. }
  122. int DSA_size(const DSA *r)
  123. {
  124. int ret, i;
  125. ASN1_INTEGER bs;
  126. unsigned char buf[4]; /* 4 bytes looks really small. However,
  127. * i2d_ASN1_INTEGER() will not look beyond
  128. * the first byte, as long as the second
  129. * parameter is NULL. */
  130. i = BN_num_bits(r->q);
  131. bs.length = (i + 7) / 8;
  132. bs.data = buf;
  133. bs.type = V_ASN1_INTEGER;
  134. /* If the top bit is set the asn1 encoding is 1 larger. */
  135. buf[0] = 0xff;
  136. i = i2d_ASN1_INTEGER(&bs, NULL);
  137. i += i; /* r and s */
  138. ret = ASN1_object_size(1, i, V_ASN1_SEQUENCE);
  139. return ret;
  140. }
  141. int DSA_set_ex_data(DSA *d, int idx, void *arg)
  142. {
  143. return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
  144. }
  145. void *DSA_get_ex_data(DSA *d, int idx)
  146. {
  147. return CRYPTO_get_ex_data(&d->ex_data, idx);
  148. }
  149. int DSA_security_bits(const DSA *d)
  150. {
  151. if (d->p && d->q)
  152. return BN_security_bits(BN_num_bits(d->p), BN_num_bits(d->q));
  153. return -1;
  154. }
  155. #ifndef OPENSSL_NO_DH
  156. DH *DSA_dup_DH(const DSA *r)
  157. {
  158. /*
  159. * DSA has p, q, g, optional pub_key, optional priv_key. DH has p,
  160. * optional length, g, optional pub_key, optional priv_key, optional q.
  161. */
  162. DH *ret = NULL;
  163. BIGNUM *p = NULL, *q = NULL, *g = NULL, *pub_key = NULL, *priv_key = NULL;
  164. if (r == NULL)
  165. goto err;
  166. ret = DH_new();
  167. if (ret == NULL)
  168. goto err;
  169. if (r->p != NULL || r->g != NULL || r->q != NULL) {
  170. if (r->p == NULL || r->g == NULL || r->q == NULL) {
  171. /* Shouldn't happen */
  172. goto err;
  173. }
  174. p = BN_dup(r->p);
  175. g = BN_dup(r->g);
  176. q = BN_dup(r->q);
  177. if (p == NULL || g == NULL || q == NULL || !DH_set0_pqg(ret, p, q, g))
  178. goto err;
  179. p = g = q = NULL;
  180. }
  181. if (r->pub_key != NULL) {
  182. pub_key = BN_dup(r->pub_key);
  183. if (pub_key == NULL)
  184. goto err;
  185. if (r->priv_key != NULL) {
  186. priv_key = BN_dup(r->priv_key);
  187. if (priv_key == NULL)
  188. goto err;
  189. }
  190. if (!DH_set0_key(ret, pub_key, priv_key))
  191. goto err;
  192. } else if (r->priv_key != NULL) {
  193. /* Shouldn't happen */
  194. goto err;
  195. }
  196. return ret;
  197. err:
  198. BN_free(p);
  199. BN_free(g);
  200. BN_free(q);
  201. BN_free(pub_key);
  202. BN_free(priv_key);
  203. DH_free(ret);
  204. return NULL;
  205. }
  206. #endif
  207. void DSA_get0_pqg(const DSA *d,
  208. const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
  209. {
  210. if (p != NULL)
  211. *p = d->p;
  212. if (q != NULL)
  213. *q = d->q;
  214. if (g != NULL)
  215. *g = d->g;
  216. }
  217. int DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g)
  218. {
  219. /* If the fields p, q and g in d are NULL, the corresponding input
  220. * parameters MUST be non-NULL.
  221. */
  222. if ((d->p == NULL && p == NULL)
  223. || (d->q == NULL && q == NULL)
  224. || (d->g == NULL && g == NULL))
  225. return 0;
  226. if (p != NULL) {
  227. BN_free(d->p);
  228. d->p = p;
  229. }
  230. if (q != NULL) {
  231. BN_free(d->q);
  232. d->q = q;
  233. }
  234. if (g != NULL) {
  235. BN_free(d->g);
  236. d->g = g;
  237. }
  238. return 1;
  239. }
  240. void DSA_get0_key(const DSA *d,
  241. const BIGNUM **pub_key, const BIGNUM **priv_key)
  242. {
  243. if (pub_key != NULL)
  244. *pub_key = d->pub_key;
  245. if (priv_key != NULL)
  246. *priv_key = d->priv_key;
  247. }
  248. int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key)
  249. {
  250. /* If the field pub_key in d is NULL, the corresponding input
  251. * parameters MUST be non-NULL. The priv_key field may
  252. * be left NULL.
  253. */
  254. if (d->pub_key == NULL && pub_key == NULL)
  255. return 0;
  256. if (pub_key != NULL) {
  257. BN_free(d->pub_key);
  258. d->pub_key = pub_key;
  259. }
  260. if (priv_key != NULL) {
  261. BN_free(d->priv_key);
  262. d->priv_key = priv_key;
  263. }
  264. return 1;
  265. }
  266. const BIGNUM *DSA_get0_p(const DSA *d)
  267. {
  268. return d->p;
  269. }
  270. const BIGNUM *DSA_get0_q(const DSA *d)
  271. {
  272. return d->q;
  273. }
  274. const BIGNUM *DSA_get0_g(const DSA *d)
  275. {
  276. return d->g;
  277. }
  278. const BIGNUM *DSA_get0_pub_key(const DSA *d)
  279. {
  280. return d->pub_key;
  281. }
  282. const BIGNUM *DSA_get0_priv_key(const DSA *d)
  283. {
  284. return d->priv_key;
  285. }
  286. void DSA_clear_flags(DSA *d, int flags)
  287. {
  288. d->flags &= ~flags;
  289. }
  290. int DSA_test_flags(const DSA *d, int flags)
  291. {
  292. return d->flags & flags;
  293. }
  294. void DSA_set_flags(DSA *d, int flags)
  295. {
  296. d->flags |= flags;
  297. }
  298. ENGINE *DSA_get0_engine(DSA *d)
  299. {
  300. return d->engine;
  301. }
  302. int DSA_bits(const DSA *dsa)
  303. {
  304. return BN_num_bits(dsa->p);
  305. }