Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

368 linhas
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 <openssl/asn1.h>
  61. #include <openssl/dh.h>
  62. #include <openssl/engine.h>
  63. #include <openssl/err.h>
  64. #include <openssl/ex_data.h>
  65. #include <openssl/mem.h>
  66. #include "internal.h"
  67. extern const DSA_METHOD DSA_default_method;
  68. DSA *DSA_new(void) { return DSA_new_method(NULL); }
  69. DSA *DSA_new_method(const ENGINE *engine) {
  70. DSA *dsa = (DSA *)OPENSSL_malloc(sizeof(DSA));
  71. if (dsa == NULL) {
  72. OPENSSL_PUT_ERROR(DSA, DSA_new_method, ERR_R_MALLOC_FAILURE);
  73. return NULL;
  74. }
  75. memset(dsa, 0, sizeof(DSA));
  76. if (engine) {
  77. dsa->meth = ENGINE_get_DSA_method(engine);
  78. }
  79. if (dsa->meth == NULL) {
  80. dsa->meth = (DSA_METHOD*) &DSA_default_method;
  81. }
  82. METHOD_ref(dsa->meth);
  83. dsa->write_params = 1;
  84. dsa->references = 1;
  85. if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DSA, dsa, &dsa->ex_data)) {
  86. METHOD_unref(dsa->meth);
  87. OPENSSL_free(dsa);
  88. return NULL;
  89. }
  90. if (dsa->meth->init && !dsa->meth->init(dsa)) {
  91. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, dsa, &dsa->ex_data);
  92. METHOD_unref(dsa->meth);
  93. OPENSSL_free(dsa);
  94. return NULL;
  95. }
  96. return dsa;
  97. }
  98. void DSA_free(DSA *dsa) {
  99. if (dsa == NULL) {
  100. return;
  101. }
  102. if (CRYPTO_add(&dsa->references, -1, CRYPTO_LOCK_DSA) > 0) {
  103. return;
  104. }
  105. if (dsa->meth->finish) {
  106. dsa->meth->finish(dsa);
  107. }
  108. METHOD_unref(dsa->meth);
  109. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, dsa, &dsa->ex_data);
  110. if (dsa->p != NULL)
  111. BN_clear_free(dsa->p);
  112. if (dsa->q != NULL)
  113. BN_clear_free(dsa->q);
  114. if (dsa->g != NULL)
  115. BN_clear_free(dsa->g);
  116. if (dsa->pub_key != NULL)
  117. BN_clear_free(dsa->pub_key);
  118. if (dsa->priv_key != NULL)
  119. BN_clear_free(dsa->priv_key);
  120. if (dsa->kinv != NULL)
  121. BN_clear_free(dsa->kinv);
  122. if (dsa->r != NULL)
  123. BN_clear_free(dsa->r);
  124. OPENSSL_free(dsa);
  125. }
  126. int DSA_up_ref(DSA *dsa) {
  127. CRYPTO_add(&dsa->references, 1, CRYPTO_LOCK_DSA);
  128. return 1;
  129. }
  130. int DSA_generate_parameters_ex(DSA *dsa, unsigned bits, const uint8_t *seed_in,
  131. size_t seed_len, int *out_counter,
  132. unsigned long *out_h, BN_GENCB *cb) {
  133. if (dsa->meth->generate_parameters) {
  134. return dsa->meth->generate_parameters(dsa, bits, seed_in, seed_len,
  135. out_counter, out_h, cb);
  136. }
  137. return DSA_default_method.generate_parameters(dsa, bits, seed_in, seed_len,
  138. out_counter, out_h, cb);
  139. }
  140. int DSA_generate_key(DSA *dsa) {
  141. if (dsa->meth->keygen) {
  142. return dsa->meth->keygen(dsa);
  143. }
  144. return DSA_default_method.keygen(dsa);
  145. }
  146. DSA_SIG *DSA_SIG_new(void) {
  147. DSA_SIG *sig;
  148. sig = OPENSSL_malloc(sizeof(DSA_SIG));
  149. if (!sig) {
  150. return NULL;
  151. }
  152. sig->r = NULL;
  153. sig->s = NULL;
  154. return sig;
  155. }
  156. void DSA_SIG_free(DSA_SIG *sig) {
  157. if (!sig) {
  158. return;
  159. }
  160. if (sig->r) {
  161. BN_free(sig->r);
  162. }
  163. if (sig->s) {
  164. BN_free(sig->s);
  165. }
  166. OPENSSL_free(sig);
  167. }
  168. DSA_SIG *DSA_do_sign(const uint8_t *digest, size_t digest_len, DSA *dsa) {
  169. if (dsa->meth->sign) {
  170. return dsa->meth->sign(digest, digest_len, dsa);
  171. }
  172. return DSA_default_method.sign(digest, digest_len, dsa);
  173. }
  174. int DSA_do_verify(const uint8_t *digest, size_t digest_len, DSA_SIG *sig,
  175. const DSA *dsa) {
  176. int valid, ret;
  177. if (dsa->meth->verify) {
  178. ret = dsa->meth->verify(&valid, digest, digest_len, sig, dsa);
  179. } else {
  180. ret = DSA_default_method.verify(&valid, digest, digest_len, sig, dsa);
  181. }
  182. if (!ret) {
  183. return -1;
  184. } else if (!valid) {
  185. return 0;
  186. }
  187. return 1;
  188. }
  189. int DSA_do_check_signature(int *out_valid, const uint8_t *digest,
  190. size_t digest_len, DSA_SIG *sig, const DSA *dsa) {
  191. if (dsa->meth->verify) {
  192. return dsa->meth->verify(out_valid, digest, digest_len, sig, dsa);
  193. }
  194. return DSA_default_method.verify(out_valid, digest, digest_len, sig, dsa);
  195. }
  196. int DSA_sign(int type, const uint8_t *digest, size_t digest_len,
  197. uint8_t *out_sig, unsigned int *out_siglen, DSA *dsa) {
  198. DSA_SIG *s;
  199. s = DSA_do_sign(digest, digest_len, dsa);
  200. if (s == NULL) {
  201. *out_siglen = 0;
  202. return 0;
  203. }
  204. *out_siglen = i2d_DSA_SIG(s, &out_sig);
  205. DSA_SIG_free(s);
  206. return 1;
  207. }
  208. int DSA_verify(int type, const uint8_t *digest, size_t digest_len,
  209. const uint8_t *sig, size_t sig_len, const DSA *dsa) {
  210. DSA_SIG *s = NULL;
  211. int ret = -1, valid;
  212. s = DSA_SIG_new();
  213. if (s == NULL) {
  214. goto err;
  215. }
  216. if (d2i_DSA_SIG(&s, &sig, sig_len) == NULL) {
  217. goto err;
  218. }
  219. if (!DSA_do_check_signature(&valid, digest, digest_len, s, dsa)) {
  220. goto err;
  221. }
  222. ret = valid;
  223. err:
  224. if (s) {
  225. DSA_SIG_free(s);
  226. }
  227. return ret;
  228. }
  229. int DSA_check_signature(int *out_valid, const uint8_t *digest,
  230. size_t digest_len, const uint8_t *sig, size_t sig_len,
  231. const DSA *dsa) {
  232. DSA_SIG *s = NULL;
  233. int ret = 0;
  234. s = DSA_SIG_new();
  235. if (s == NULL) {
  236. goto err;
  237. }
  238. if (d2i_DSA_SIG(&s, &sig, sig_len) == NULL) {
  239. goto err;
  240. }
  241. ret = DSA_do_check_signature(out_valid, digest, digest_len, s, dsa);
  242. err:
  243. if (s) {
  244. DSA_SIG_free(s);
  245. }
  246. return ret;
  247. }
  248. int DSA_size(const DSA *dsa) {
  249. int ret, i;
  250. ASN1_INTEGER bs;
  251. unsigned char buf[4]; /* 4 bytes looks really small.
  252. However, i2d_ASN1_INTEGER() will not look
  253. beyond the first byte, as long as the second
  254. parameter is NULL. */
  255. i = BN_num_bits(dsa->q);
  256. bs.length = (i + 7) / 8;
  257. bs.data = buf;
  258. bs.type = V_ASN1_INTEGER;
  259. /* If the top bit is set the asn1 encoding is 1 larger. */
  260. buf[0] = 0xff;
  261. i = i2d_ASN1_INTEGER(&bs, NULL);
  262. i += i; /* r and s */
  263. ret = ASN1_object_size(1, i, V_ASN1_SEQUENCE);
  264. return ret;
  265. }
  266. int DSA_sign_setup(const DSA *dsa, BN_CTX *ctx, BIGNUM **out_kinv,
  267. BIGNUM **out_r) {
  268. if (dsa->meth->sign_setup) {
  269. return dsa->meth->sign_setup(dsa, ctx, out_kinv, out_r, NULL, 0);
  270. }
  271. return DSA_default_method.sign_setup(dsa, ctx, out_kinv, out_r, NULL, 0);
  272. }
  273. int DSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
  274. CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
  275. return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DSA, argl, argp, new_func,
  276. dup_func, free_func);
  277. }
  278. int DSA_set_ex_data(DSA *d, int idx, void *arg) {
  279. return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
  280. }
  281. void *DSA_get_ex_data(const DSA *d, int idx) {
  282. return CRYPTO_get_ex_data(&d->ex_data, idx);
  283. }
  284. DH *DSA_dup_DH(const DSA *r) {
  285. DH *ret = NULL;
  286. if (r == NULL) {
  287. goto err;
  288. }
  289. ret = DH_new();
  290. if (ret == NULL) {
  291. goto err;
  292. }
  293. if (r->q != NULL) {
  294. ret->priv_length = BN_num_bits(r->q);
  295. if ((ret->q = BN_dup(r->q)) == NULL) {
  296. goto err;
  297. }
  298. }
  299. if ((r->p != NULL && (ret->p = BN_dup(r->p)) == NULL) ||
  300. (r->g != NULL && (ret->g = BN_dup(r->g)) == NULL) ||
  301. (r->pub_key != NULL && (ret->pub_key = BN_dup(r->pub_key)) == NULL) ||
  302. (r->priv_key != NULL && (ret->priv_key = BN_dup(r->priv_key)) == NULL)) {
  303. goto err;
  304. }
  305. return ret;
  306. err:
  307. if (ret != NULL) {
  308. DH_free(ret);
  309. }
  310. return NULL;
  311. }