A local copy of OpenSSL from GitHub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

343 lines
7.6 KiB

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