Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

349 rader
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_refcount_dec_and_test_zero(&dsa->references)) {
  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. BN_clear_free(dsa->p);
  116. BN_clear_free(dsa->q);
  117. BN_clear_free(dsa->g);
  118. BN_clear_free(dsa->pub_key);
  119. BN_clear_free(dsa->priv_key);
  120. BN_clear_free(dsa->kinv);
  121. BN_clear_free(dsa->r);
  122. CRYPTO_MUTEX_cleanup(&dsa->method_mont_p_lock);
  123. OPENSSL_free(dsa);
  124. }
  125. int DSA_up_ref(DSA *dsa) {
  126. CRYPTO_refcount_inc(&dsa->references);
  127. return 1;
  128. }
  129. int DSA_generate_parameters_ex(DSA *dsa, unsigned bits, const uint8_t *seed_in,
  130. size_t seed_len, int *out_counter,
  131. unsigned long *out_h, BN_GENCB *cb) {
  132. if (dsa->meth->generate_parameters) {
  133. return dsa->meth->generate_parameters(dsa, bits, seed_in, seed_len,
  134. out_counter, out_h, cb);
  135. }
  136. return DSA_default_method.generate_parameters(dsa, bits, seed_in, seed_len,
  137. out_counter, out_h, cb);
  138. }
  139. int DSA_generate_key(DSA *dsa) {
  140. if (dsa->meth->keygen) {
  141. return dsa->meth->keygen(dsa);
  142. }
  143. return DSA_default_method.keygen(dsa);
  144. }
  145. DSA_SIG *DSA_SIG_new(void) {
  146. DSA_SIG *sig;
  147. sig = OPENSSL_malloc(sizeof(DSA_SIG));
  148. if (!sig) {
  149. return NULL;
  150. }
  151. sig->r = NULL;
  152. sig->s = NULL;
  153. return sig;
  154. }
  155. void DSA_SIG_free(DSA_SIG *sig) {
  156. if (!sig) {
  157. return;
  158. }
  159. BN_free(sig->r);
  160. BN_free(sig->s);
  161. OPENSSL_free(sig);
  162. }
  163. DSA_SIG *DSA_do_sign(const uint8_t *digest, size_t digest_len, DSA *dsa) {
  164. if (dsa->meth->sign) {
  165. return dsa->meth->sign(digest, digest_len, dsa);
  166. }
  167. return DSA_default_method.sign(digest, digest_len, dsa);
  168. }
  169. int DSA_do_verify(const uint8_t *digest, size_t digest_len, DSA_SIG *sig,
  170. const DSA *dsa) {
  171. int valid;
  172. if (!DSA_do_check_signature(&valid, digest, digest_len, sig, dsa)) {
  173. return -1;
  174. }
  175. return valid;
  176. }
  177. int DSA_do_check_signature(int *out_valid, const uint8_t *digest,
  178. size_t digest_len, DSA_SIG *sig, const DSA *dsa) {
  179. if (dsa->meth->verify) {
  180. return dsa->meth->verify(out_valid, digest, digest_len, sig, dsa);
  181. }
  182. return DSA_default_method.verify(out_valid, digest, digest_len, sig, dsa);
  183. }
  184. int DSA_sign(int type, const uint8_t *digest, size_t digest_len,
  185. uint8_t *out_sig, unsigned int *out_siglen, DSA *dsa) {
  186. DSA_SIG *s;
  187. s = DSA_do_sign(digest, digest_len, dsa);
  188. if (s == NULL) {
  189. *out_siglen = 0;
  190. return 0;
  191. }
  192. *out_siglen = i2d_DSA_SIG(s, &out_sig);
  193. DSA_SIG_free(s);
  194. return 1;
  195. }
  196. int DSA_verify(int type, const uint8_t *digest, size_t digest_len,
  197. const uint8_t *sig, size_t sig_len, const DSA *dsa) {
  198. int valid;
  199. if (!DSA_check_signature(&valid, digest, digest_len, sig, sig_len, dsa)) {
  200. return -1;
  201. }
  202. return valid;
  203. }
  204. int DSA_check_signature(int *out_valid, const uint8_t *digest,
  205. size_t digest_len, const uint8_t *sig, size_t sig_len,
  206. const DSA *dsa) {
  207. DSA_SIG *s = NULL;
  208. int ret = 0;
  209. uint8_t *der = NULL;
  210. s = DSA_SIG_new();
  211. if (s == NULL) {
  212. goto err;
  213. }
  214. const uint8_t *sigp = sig;
  215. if (d2i_DSA_SIG(&s, &sigp, sig_len) == NULL || sigp != sig + sig_len) {
  216. goto err;
  217. }
  218. /* Ensure that the signature uses DER and doesn't have trailing garbage. */
  219. int der_len = i2d_DSA_SIG(s, &der);
  220. if (der_len < 0 || (size_t)der_len != sig_len || memcmp(sig, der, sig_len)) {
  221. goto err;
  222. }
  223. ret = DSA_do_check_signature(out_valid, digest, digest_len, s, dsa);
  224. err:
  225. OPENSSL_free(der);
  226. DSA_SIG_free(s);
  227. return ret;
  228. }
  229. int DSA_size(const DSA *dsa) {
  230. int ret, i;
  231. ASN1_INTEGER bs;
  232. unsigned char buf[4]; /* 4 bytes looks really small.
  233. However, i2d_ASN1_INTEGER() will not look
  234. beyond the first byte, as long as the second
  235. parameter is NULL. */
  236. i = BN_num_bits(dsa->q);
  237. bs.length = (i + 7) / 8;
  238. bs.data = buf;
  239. bs.type = V_ASN1_INTEGER;
  240. /* If the top bit is set the asn1 encoding is 1 larger. */
  241. buf[0] = 0xff;
  242. i = i2d_ASN1_INTEGER(&bs, NULL);
  243. i += i; /* r and s */
  244. ret = ASN1_object_size(1, i, V_ASN1_SEQUENCE);
  245. return ret;
  246. }
  247. int DSA_sign_setup(const DSA *dsa, BN_CTX *ctx, BIGNUM **out_kinv,
  248. BIGNUM **out_r) {
  249. if (dsa->meth->sign_setup) {
  250. return dsa->meth->sign_setup(dsa, ctx, out_kinv, out_r, NULL, 0);
  251. }
  252. return DSA_default_method.sign_setup(dsa, ctx, out_kinv, out_r, NULL, 0);
  253. }
  254. int DSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
  255. CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
  256. int index;
  257. if (!CRYPTO_get_ex_new_index(&g_ex_data_class, &index, argl, argp, new_func,
  258. dup_func, free_func)) {
  259. return -1;
  260. }
  261. return index;
  262. }
  263. int DSA_set_ex_data(DSA *d, int idx, void *arg) {
  264. return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
  265. }
  266. void *DSA_get_ex_data(const DSA *d, int idx) {
  267. return CRYPTO_get_ex_data(&d->ex_data, idx);
  268. }
  269. DH *DSA_dup_DH(const DSA *r) {
  270. DH *ret = NULL;
  271. if (r == NULL) {
  272. goto err;
  273. }
  274. ret = DH_new();
  275. if (ret == NULL) {
  276. goto err;
  277. }
  278. if (r->q != NULL) {
  279. ret->priv_length = BN_num_bits(r->q);
  280. if ((ret->q = BN_dup(r->q)) == NULL) {
  281. goto err;
  282. }
  283. }
  284. if ((r->p != NULL && (ret->p = BN_dup(r->p)) == NULL) ||
  285. (r->g != NULL && (ret->g = BN_dup(r->g)) == NULL) ||
  286. (r->pub_key != NULL && (ret->pub_key = BN_dup(r->pub_key)) == NULL) ||
  287. (r->priv_key != NULL && (ret->priv_key = BN_dup(r->priv_key)) == NULL)) {
  288. goto err;
  289. }
  290. return ret;
  291. err:
  292. DH_free(ret);
  293. return NULL;
  294. }