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.
 
 
 
 
 
 

373 lines
10 KiB

  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. *
  57. * The DSS routines are based on patches supplied by
  58. * Steven Schoch <schoch@sheba.arc.nasa.gov>. */
  59. #include <openssl/dsa.h>
  60. #include <string.h>
  61. #include <openssl/asn1.h>
  62. #include <openssl/dh.h>
  63. #include <openssl/engine.h>
  64. #include <openssl/err.h>
  65. #include <openssl/ex_data.h>
  66. #include <openssl/mem.h>
  67. #include <openssl/thread.h>
  68. #include "internal.h"
  69. #include "../internal.h"
  70. extern const DSA_METHOD DSA_default_method;
  71. static CRYPTO_EX_DATA_CLASS g_ex_data_class = CRYPTO_EX_DATA_CLASS_INIT;
  72. DSA *DSA_new(void) { return DSA_new_method(NULL); }
  73. DSA *DSA_new_method(const ENGINE *engine) {
  74. DSA *dsa = (DSA *)OPENSSL_malloc(sizeof(DSA));
  75. if (dsa == NULL) {
  76. OPENSSL_PUT_ERROR(DSA, DSA_new_method, ERR_R_MALLOC_FAILURE);
  77. return NULL;
  78. }
  79. memset(dsa, 0, sizeof(DSA));
  80. if (engine) {
  81. dsa->meth = ENGINE_get_DSA_method(engine);
  82. }
  83. if (dsa->meth == NULL) {
  84. dsa->meth = (DSA_METHOD*) &DSA_default_method;
  85. }
  86. METHOD_ref(dsa->meth);
  87. dsa->write_params = 1;
  88. dsa->references = 1;
  89. CRYPTO_MUTEX_init(&dsa->method_mont_p_lock);
  90. if (!CRYPTO_new_ex_data(&g_ex_data_class, dsa, &dsa->ex_data)) {
  91. METHOD_unref(dsa->meth);
  92. OPENSSL_free(dsa);
  93. return NULL;
  94. }
  95. if (dsa->meth->init && !dsa->meth->init(dsa)) {
  96. CRYPTO_free_ex_data(&g_ex_data_class, dsa, &dsa->ex_data);
  97. METHOD_unref(dsa->meth);
  98. OPENSSL_free(dsa);
  99. return NULL;
  100. }
  101. return dsa;
  102. }
  103. void DSA_free(DSA *dsa) {
  104. if (dsa == NULL) {
  105. return;
  106. }
  107. if (CRYPTO_add(&dsa->references, -1, CRYPTO_LOCK_DSA) > 0) {
  108. return;
  109. }
  110. if (dsa->meth->finish) {
  111. dsa->meth->finish(dsa);
  112. }
  113. METHOD_unref(dsa->meth);
  114. CRYPTO_free_ex_data(&g_ex_data_class, dsa, &dsa->ex_data);
  115. if (dsa->p != NULL) {
  116. BN_clear_free(dsa->p);
  117. }
  118. if (dsa->q != NULL) {
  119. BN_clear_free(dsa->q);
  120. }
  121. if (dsa->g != NULL) {
  122. BN_clear_free(dsa->g);
  123. }
  124. if (dsa->pub_key != NULL) {
  125. BN_clear_free(dsa->pub_key);
  126. }
  127. if (dsa->priv_key != NULL) {
  128. BN_clear_free(dsa->priv_key);
  129. }
  130. if (dsa->kinv != NULL) {
  131. BN_clear_free(dsa->kinv);
  132. }
  133. if (dsa->r != NULL) {
  134. BN_clear_free(dsa->r);
  135. }
  136. CRYPTO_MUTEX_cleanup(&dsa->method_mont_p_lock);
  137. OPENSSL_free(dsa);
  138. }
  139. int DSA_up_ref(DSA *dsa) {
  140. CRYPTO_add(&dsa->references, 1, CRYPTO_LOCK_DSA);
  141. return 1;
  142. }
  143. int DSA_generate_parameters_ex(DSA *dsa, unsigned bits, const uint8_t *seed_in,
  144. size_t seed_len, int *out_counter,
  145. unsigned long *out_h, BN_GENCB *cb) {
  146. if (dsa->meth->generate_parameters) {
  147. return dsa->meth->generate_parameters(dsa, bits, seed_in, seed_len,
  148. out_counter, out_h, cb);
  149. }
  150. return DSA_default_method.generate_parameters(dsa, bits, seed_in, seed_len,
  151. out_counter, out_h, cb);
  152. }
  153. int DSA_generate_key(DSA *dsa) {
  154. if (dsa->meth->keygen) {
  155. return dsa->meth->keygen(dsa);
  156. }
  157. return DSA_default_method.keygen(dsa);
  158. }
  159. DSA_SIG *DSA_SIG_new(void) {
  160. DSA_SIG *sig;
  161. sig = OPENSSL_malloc(sizeof(DSA_SIG));
  162. if (!sig) {
  163. return NULL;
  164. }
  165. sig->r = NULL;
  166. sig->s = NULL;
  167. return sig;
  168. }
  169. void DSA_SIG_free(DSA_SIG *sig) {
  170. if (!sig) {
  171. return;
  172. }
  173. if (sig->r) {
  174. BN_free(sig->r);
  175. }
  176. if (sig->s) {
  177. BN_free(sig->s);
  178. }
  179. OPENSSL_free(sig);
  180. }
  181. DSA_SIG *DSA_do_sign(const uint8_t *digest, size_t digest_len, DSA *dsa) {
  182. if (dsa->meth->sign) {
  183. return dsa->meth->sign(digest, digest_len, dsa);
  184. }
  185. return DSA_default_method.sign(digest, digest_len, dsa);
  186. }
  187. int DSA_do_verify(const uint8_t *digest, size_t digest_len, DSA_SIG *sig,
  188. const DSA *dsa) {
  189. int valid;
  190. if (!DSA_do_check_signature(&valid, digest, digest_len, sig, dsa)) {
  191. return -1;
  192. }
  193. return valid;
  194. }
  195. int DSA_do_check_signature(int *out_valid, const uint8_t *digest,
  196. size_t digest_len, DSA_SIG *sig, const DSA *dsa) {
  197. if (dsa->meth->verify) {
  198. return dsa->meth->verify(out_valid, digest, digest_len, sig, dsa);
  199. }
  200. return DSA_default_method.verify(out_valid, digest, digest_len, sig, dsa);
  201. }
  202. int DSA_sign(int type, const uint8_t *digest, size_t digest_len,
  203. uint8_t *out_sig, unsigned int *out_siglen, DSA *dsa) {
  204. DSA_SIG *s;
  205. s = DSA_do_sign(digest, digest_len, dsa);
  206. if (s == NULL) {
  207. *out_siglen = 0;
  208. return 0;
  209. }
  210. *out_siglen = i2d_DSA_SIG(s, &out_sig);
  211. DSA_SIG_free(s);
  212. return 1;
  213. }
  214. int DSA_verify(int type, const uint8_t *digest, size_t digest_len,
  215. const uint8_t *sig, size_t sig_len, const DSA *dsa) {
  216. int valid;
  217. if (!DSA_check_signature(&valid, digest, digest_len, sig, sig_len, dsa)) {
  218. return -1;
  219. }
  220. return valid;
  221. }
  222. int DSA_check_signature(int *out_valid, const uint8_t *digest,
  223. size_t digest_len, const uint8_t *sig, size_t sig_len,
  224. const DSA *dsa) {
  225. DSA_SIG *s = NULL;
  226. int ret = 0;
  227. uint8_t *der = NULL;
  228. s = DSA_SIG_new();
  229. if (s == NULL) {
  230. goto err;
  231. }
  232. const uint8_t *sigp = sig;
  233. if (d2i_DSA_SIG(&s, &sigp, sig_len) == NULL || sigp != sig + sig_len) {
  234. goto err;
  235. }
  236. /* Ensure that the signature uses DER and doesn't have trailing garbage. */
  237. int der_len = i2d_DSA_SIG(s, &der);
  238. if (der_len < 0 || (size_t)der_len != sig_len || memcmp(sig, der, sig_len)) {
  239. goto err;
  240. }
  241. ret = DSA_do_check_signature(out_valid, digest, digest_len, s, dsa);
  242. err:
  243. if (der != NULL) {
  244. OPENSSL_free(der);
  245. }
  246. if (s) {
  247. DSA_SIG_free(s);
  248. }
  249. return ret;
  250. }
  251. int DSA_size(const DSA *dsa) {
  252. int ret, i;
  253. ASN1_INTEGER bs;
  254. unsigned char buf[4]; /* 4 bytes looks really small.
  255. However, i2d_ASN1_INTEGER() will not look
  256. beyond the first byte, as long as the second
  257. parameter is NULL. */
  258. i = BN_num_bits(dsa->q);
  259. bs.length = (i + 7) / 8;
  260. bs.data = buf;
  261. bs.type = V_ASN1_INTEGER;
  262. /* If the top bit is set the asn1 encoding is 1 larger. */
  263. buf[0] = 0xff;
  264. i = i2d_ASN1_INTEGER(&bs, NULL);
  265. i += i; /* r and s */
  266. ret = ASN1_object_size(1, i, V_ASN1_SEQUENCE);
  267. return ret;
  268. }
  269. int DSA_sign_setup(const DSA *dsa, BN_CTX *ctx, BIGNUM **out_kinv,
  270. BIGNUM **out_r) {
  271. if (dsa->meth->sign_setup) {
  272. return dsa->meth->sign_setup(dsa, ctx, out_kinv, out_r, NULL, 0);
  273. }
  274. return DSA_default_method.sign_setup(dsa, ctx, out_kinv, out_r, NULL, 0);
  275. }
  276. int DSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
  277. CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
  278. int index;
  279. if (!CRYPTO_get_ex_new_index(&g_ex_data_class, &index, argl, argp, new_func,
  280. dup_func, free_func)) {
  281. return -1;
  282. }
  283. return index;
  284. }
  285. int DSA_set_ex_data(DSA *d, int idx, void *arg) {
  286. return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
  287. }
  288. void *DSA_get_ex_data(const DSA *d, int idx) {
  289. return CRYPTO_get_ex_data(&d->ex_data, idx);
  290. }
  291. DH *DSA_dup_DH(const DSA *r) {
  292. DH *ret = NULL;
  293. if (r == NULL) {
  294. goto err;
  295. }
  296. ret = DH_new();
  297. if (ret == NULL) {
  298. goto err;
  299. }
  300. if (r->q != NULL) {
  301. ret->priv_length = BN_num_bits(r->q);
  302. if ((ret->q = BN_dup(r->q)) == NULL) {
  303. goto err;
  304. }
  305. }
  306. if ((r->p != NULL && (ret->p = BN_dup(r->p)) == NULL) ||
  307. (r->g != NULL && (ret->g = BN_dup(r->g)) == NULL) ||
  308. (r->pub_key != NULL && (ret->pub_key = BN_dup(r->pub_key)) == NULL) ||
  309. (r->priv_key != NULL && (ret->priv_key = BN_dup(r->priv_key)) == NULL)) {
  310. goto err;
  311. }
  312. return ret;
  313. err:
  314. if (ret != NULL) {
  315. DH_free(ret);
  316. }
  317. return NULL;
  318. }