dsa_lib.c 7.7 KB

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