Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

340 рядки
9.0 KiB

  1. /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  2. * project 2000. */
  3. /* ====================================================================
  4. * Copyright (c) 2000-2005 The OpenSSL Project. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. *
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. *
  18. * 3. All advertising materials mentioning features or use of this
  19. * software must display the following acknowledgment:
  20. * "This product includes software developed by the OpenSSL Project
  21. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  22. *
  23. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  24. * endorse or promote products derived from this software without
  25. * prior written permission. For written permission, please contact
  26. * licensing@OpenSSL.org.
  27. *
  28. * 5. Products derived from this software may not be called "OpenSSL"
  29. * nor may "OpenSSL" appear in their names without prior written
  30. * permission of the OpenSSL Project.
  31. *
  32. * 6. Redistributions of any form whatsoever must retain the following
  33. * acknowledgment:
  34. * "This product includes software developed by the OpenSSL Project
  35. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  36. *
  37. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  38. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  39. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  40. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  41. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  42. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  43. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  44. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  45. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  46. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  47. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  48. * OF THE POSSIBILITY OF SUCH DAMAGE.
  49. * ====================================================================
  50. *
  51. * This product includes cryptographic software written by Eric Young
  52. * (eay@cryptsoft.com). This product includes software written by Tim
  53. * Hudson (tjh@cryptsoft.com). */
  54. #include <openssl/dsa.h>
  55. #include <assert.h>
  56. #include <openssl/bn.h>
  57. #include <openssl/bytestring.h>
  58. #include <openssl/err.h>
  59. #include <openssl/mem.h>
  60. #include "../bytestring/internal.h"
  61. static int parse_integer(CBS *cbs, BIGNUM **out) {
  62. assert(*out == NULL);
  63. *out = BN_new();
  64. if (*out == NULL) {
  65. return 0;
  66. }
  67. return BN_parse_asn1_unsigned(cbs, *out);
  68. }
  69. static int marshal_integer(CBB *cbb, BIGNUM *bn) {
  70. if (bn == NULL) {
  71. // A DSA object may be missing some components.
  72. OPENSSL_PUT_ERROR(DSA, ERR_R_PASSED_NULL_PARAMETER);
  73. return 0;
  74. }
  75. return BN_marshal_asn1(cbb, bn);
  76. }
  77. DSA_SIG *DSA_SIG_parse(CBS *cbs) {
  78. DSA_SIG *ret = DSA_SIG_new();
  79. if (ret == NULL) {
  80. return NULL;
  81. }
  82. CBS child;
  83. if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE) ||
  84. !parse_integer(&child, &ret->r) ||
  85. !parse_integer(&child, &ret->s) ||
  86. CBS_len(&child) != 0) {
  87. OPENSSL_PUT_ERROR(DSA, DSA_R_DECODE_ERROR);
  88. DSA_SIG_free(ret);
  89. return NULL;
  90. }
  91. return ret;
  92. }
  93. int DSA_SIG_marshal(CBB *cbb, const DSA_SIG *sig) {
  94. CBB child;
  95. if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) ||
  96. !marshal_integer(&child, sig->r) ||
  97. !marshal_integer(&child, sig->s) ||
  98. !CBB_flush(cbb)) {
  99. OPENSSL_PUT_ERROR(DSA, DSA_R_ENCODE_ERROR);
  100. return 0;
  101. }
  102. return 1;
  103. }
  104. DSA *DSA_parse_public_key(CBS *cbs) {
  105. DSA *ret = DSA_new();
  106. if (ret == NULL) {
  107. return NULL;
  108. }
  109. CBS child;
  110. if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE) ||
  111. !parse_integer(&child, &ret->pub_key) ||
  112. !parse_integer(&child, &ret->p) ||
  113. !parse_integer(&child, &ret->q) ||
  114. !parse_integer(&child, &ret->g) ||
  115. CBS_len(&child) != 0) {
  116. OPENSSL_PUT_ERROR(DSA, DSA_R_DECODE_ERROR);
  117. DSA_free(ret);
  118. return NULL;
  119. }
  120. return ret;
  121. }
  122. int DSA_marshal_public_key(CBB *cbb, const DSA *dsa) {
  123. CBB child;
  124. if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) ||
  125. !marshal_integer(&child, dsa->pub_key) ||
  126. !marshal_integer(&child, dsa->p) ||
  127. !marshal_integer(&child, dsa->q) ||
  128. !marshal_integer(&child, dsa->g) ||
  129. !CBB_flush(cbb)) {
  130. OPENSSL_PUT_ERROR(DSA, DSA_R_ENCODE_ERROR);
  131. return 0;
  132. }
  133. return 1;
  134. }
  135. DSA *DSA_parse_parameters(CBS *cbs) {
  136. DSA *ret = DSA_new();
  137. if (ret == NULL) {
  138. return NULL;
  139. }
  140. CBS child;
  141. if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE) ||
  142. !parse_integer(&child, &ret->p) ||
  143. !parse_integer(&child, &ret->q) ||
  144. !parse_integer(&child, &ret->g) ||
  145. CBS_len(&child) != 0) {
  146. OPENSSL_PUT_ERROR(DSA, DSA_R_DECODE_ERROR);
  147. DSA_free(ret);
  148. return NULL;
  149. }
  150. return ret;
  151. }
  152. int DSA_marshal_parameters(CBB *cbb, const DSA *dsa) {
  153. CBB child;
  154. if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) ||
  155. !marshal_integer(&child, dsa->p) ||
  156. !marshal_integer(&child, dsa->q) ||
  157. !marshal_integer(&child, dsa->g) ||
  158. !CBB_flush(cbb)) {
  159. OPENSSL_PUT_ERROR(DSA, DSA_R_ENCODE_ERROR);
  160. return 0;
  161. }
  162. return 1;
  163. }
  164. DSA *DSA_parse_private_key(CBS *cbs) {
  165. DSA *ret = DSA_new();
  166. if (ret == NULL) {
  167. return NULL;
  168. }
  169. CBS child;
  170. uint64_t version;
  171. if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE) ||
  172. !CBS_get_asn1_uint64(&child, &version)) {
  173. OPENSSL_PUT_ERROR(DSA, DSA_R_DECODE_ERROR);
  174. goto err;
  175. }
  176. if (version != 0) {
  177. OPENSSL_PUT_ERROR(DSA, DSA_R_BAD_VERSION);
  178. goto err;
  179. }
  180. if (!parse_integer(&child, &ret->p) ||
  181. !parse_integer(&child, &ret->q) ||
  182. !parse_integer(&child, &ret->g) ||
  183. !parse_integer(&child, &ret->pub_key) ||
  184. !parse_integer(&child, &ret->priv_key) ||
  185. CBS_len(&child) != 0) {
  186. OPENSSL_PUT_ERROR(DSA, DSA_R_DECODE_ERROR);
  187. goto err;
  188. }
  189. return ret;
  190. err:
  191. DSA_free(ret);
  192. return NULL;
  193. }
  194. int DSA_marshal_private_key(CBB *cbb, const DSA *dsa) {
  195. CBB child;
  196. if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) ||
  197. !CBB_add_asn1_uint64(&child, 0 /* version */) ||
  198. !marshal_integer(&child, dsa->p) ||
  199. !marshal_integer(&child, dsa->q) ||
  200. !marshal_integer(&child, dsa->g) ||
  201. !marshal_integer(&child, dsa->pub_key) ||
  202. !marshal_integer(&child, dsa->priv_key) ||
  203. !CBB_flush(cbb)) {
  204. OPENSSL_PUT_ERROR(DSA, DSA_R_ENCODE_ERROR);
  205. return 0;
  206. }
  207. return 1;
  208. }
  209. DSA_SIG *d2i_DSA_SIG(DSA_SIG **out_sig, const uint8_t **inp, long len) {
  210. if (len < 0) {
  211. return NULL;
  212. }
  213. CBS cbs;
  214. CBS_init(&cbs, *inp, (size_t)len);
  215. DSA_SIG *ret = DSA_SIG_parse(&cbs);
  216. if (ret == NULL) {
  217. return NULL;
  218. }
  219. if (out_sig != NULL) {
  220. DSA_SIG_free(*out_sig);
  221. *out_sig = ret;
  222. }
  223. *inp = CBS_data(&cbs);
  224. return ret;
  225. }
  226. int i2d_DSA_SIG(const DSA_SIG *in, uint8_t **outp) {
  227. CBB cbb;
  228. if (!CBB_init(&cbb, 0) ||
  229. !DSA_SIG_marshal(&cbb, in)) {
  230. CBB_cleanup(&cbb);
  231. return -1;
  232. }
  233. return CBB_finish_i2d(&cbb, outp);
  234. }
  235. DSA *d2i_DSAPublicKey(DSA **out, const uint8_t **inp, long len) {
  236. if (len < 0) {
  237. return NULL;
  238. }
  239. CBS cbs;
  240. CBS_init(&cbs, *inp, (size_t)len);
  241. DSA *ret = DSA_parse_public_key(&cbs);
  242. if (ret == NULL) {
  243. return NULL;
  244. }
  245. if (out != NULL) {
  246. DSA_free(*out);
  247. *out = ret;
  248. }
  249. *inp = CBS_data(&cbs);
  250. return ret;
  251. }
  252. int i2d_DSAPublicKey(const DSA *in, uint8_t **outp) {
  253. CBB cbb;
  254. if (!CBB_init(&cbb, 0) ||
  255. !DSA_marshal_public_key(&cbb, in)) {
  256. CBB_cleanup(&cbb);
  257. return -1;
  258. }
  259. return CBB_finish_i2d(&cbb, outp);
  260. }
  261. DSA *d2i_DSAPrivateKey(DSA **out, const uint8_t **inp, long len) {
  262. if (len < 0) {
  263. return NULL;
  264. }
  265. CBS cbs;
  266. CBS_init(&cbs, *inp, (size_t)len);
  267. DSA *ret = DSA_parse_private_key(&cbs);
  268. if (ret == NULL) {
  269. return NULL;
  270. }
  271. if (out != NULL) {
  272. DSA_free(*out);
  273. *out = ret;
  274. }
  275. *inp = CBS_data(&cbs);
  276. return ret;
  277. }
  278. int i2d_DSAPrivateKey(const DSA *in, uint8_t **outp) {
  279. CBB cbb;
  280. if (!CBB_init(&cbb, 0) ||
  281. !DSA_marshal_private_key(&cbb, in)) {
  282. CBB_cleanup(&cbb);
  283. return -1;
  284. }
  285. return CBB_finish_i2d(&cbb, outp);
  286. }
  287. DSA *d2i_DSAparams(DSA **out, const uint8_t **inp, long len) {
  288. if (len < 0) {
  289. return NULL;
  290. }
  291. CBS cbs;
  292. CBS_init(&cbs, *inp, (size_t)len);
  293. DSA *ret = DSA_parse_parameters(&cbs);
  294. if (ret == NULL) {
  295. return NULL;
  296. }
  297. if (out != NULL) {
  298. DSA_free(*out);
  299. *out = ret;
  300. }
  301. *inp = CBS_data(&cbs);
  302. return ret;
  303. }
  304. int i2d_DSAparams(const DSA *in, uint8_t **outp) {
  305. CBB cbb;
  306. if (!CBB_init(&cbb, 0) ||
  307. !DSA_marshal_parameters(&cbb, in)) {
  308. CBB_cleanup(&cbb);
  309. return -1;
  310. }
  311. return CBB_finish_i2d(&cbb, outp);
  312. }