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.
 
 
 
 
 
 

401 linhas
9.8 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. #include <openssl/asn1.h>
  57. #include <string.h>
  58. #include <openssl/asn1t.h>
  59. #include <openssl/err.h>
  60. #include <openssl/mem.h>
  61. #include <openssl/obj.h>
  62. static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
  63. int combine);
  64. static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
  65. static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
  66. static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
  67. ASN1_VALUE *ASN1_item_new(const ASN1_ITEM *it)
  68. {
  69. ASN1_VALUE *ret = NULL;
  70. if (ASN1_item_ex_new(&ret, it) > 0)
  71. return ret;
  72. return NULL;
  73. }
  74. /* Allocate an ASN1 structure */
  75. int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
  76. {
  77. return asn1_item_ex_combine_new(pval, it, 0);
  78. }
  79. static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
  80. int combine)
  81. {
  82. const ASN1_TEMPLATE *tt = NULL;
  83. const ASN1_COMPAT_FUNCS *cf;
  84. const ASN1_EXTERN_FUNCS *ef;
  85. const ASN1_AUX *aux = it->funcs;
  86. ASN1_aux_cb *asn1_cb;
  87. ASN1_VALUE **pseqval;
  88. int i;
  89. if (aux && aux->asn1_cb)
  90. asn1_cb = aux->asn1_cb;
  91. else
  92. asn1_cb = 0;
  93. if (!combine) *pval = NULL;
  94. #ifdef CRYPTO_MDEBUG
  95. if (it->sname)
  96. CRYPTO_push_info(it->sname);
  97. #endif
  98. switch(it->itype)
  99. {
  100. case ASN1_ITYPE_EXTERN:
  101. ef = it->funcs;
  102. if (ef && ef->asn1_ex_new)
  103. {
  104. if (!ef->asn1_ex_new(pval, it))
  105. goto memerr;
  106. }
  107. break;
  108. case ASN1_ITYPE_COMPAT:
  109. cf = it->funcs;
  110. if (cf && cf->asn1_new) {
  111. *pval = cf->asn1_new();
  112. if (!*pval)
  113. goto memerr;
  114. }
  115. break;
  116. case ASN1_ITYPE_PRIMITIVE:
  117. if (it->templates)
  118. {
  119. if (!ASN1_template_new(pval, it->templates))
  120. goto memerr;
  121. }
  122. else if (!ASN1_primitive_new(pval, it))
  123. goto memerr;
  124. break;
  125. case ASN1_ITYPE_MSTRING:
  126. if (!ASN1_primitive_new(pval, it))
  127. goto memerr;
  128. break;
  129. case ASN1_ITYPE_CHOICE:
  130. if (asn1_cb)
  131. {
  132. i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
  133. if (!i)
  134. goto auxerr;
  135. if (i==2)
  136. {
  137. #ifdef CRYPTO_MDEBUG
  138. if (it->sname)
  139. CRYPTO_pop_info();
  140. #endif
  141. return 1;
  142. }
  143. }
  144. if (!combine)
  145. {
  146. *pval = OPENSSL_malloc(it->size);
  147. if (!*pval)
  148. goto memerr;
  149. memset(*pval, 0, it->size);
  150. }
  151. asn1_set_choice_selector(pval, -1, it);
  152. if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
  153. goto auxerr;
  154. break;
  155. case ASN1_ITYPE_NDEF_SEQUENCE:
  156. case ASN1_ITYPE_SEQUENCE:
  157. if (asn1_cb)
  158. {
  159. i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
  160. if (!i)
  161. goto auxerr;
  162. if (i==2)
  163. {
  164. #ifdef CRYPTO_MDEBUG
  165. if (it->sname)
  166. CRYPTO_pop_info();
  167. #endif
  168. return 1;
  169. }
  170. }
  171. if (!combine)
  172. {
  173. *pval = OPENSSL_malloc(it->size);
  174. if (!*pval)
  175. goto memerr;
  176. memset(*pval, 0, it->size);
  177. asn1_do_lock(pval, 0, it);
  178. asn1_enc_init(pval, it);
  179. }
  180. for (i = 0, tt = it->templates; i < it->tcount; tt++, i++)
  181. {
  182. pseqval = asn1_get_field_ptr(pval, tt);
  183. if (!ASN1_template_new(pseqval, tt))
  184. goto memerr;
  185. }
  186. if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
  187. goto auxerr;
  188. break;
  189. }
  190. #ifdef CRYPTO_MDEBUG
  191. if (it->sname) CRYPTO_pop_info();
  192. #endif
  193. return 1;
  194. memerr:
  195. OPENSSL_PUT_ERROR(ASN1, asn1_item_ex_combine_new, ERR_R_MALLOC_FAILURE);
  196. ASN1_item_ex_free(pval, it);
  197. #ifdef CRYPTO_MDEBUG
  198. if (it->sname) CRYPTO_pop_info();
  199. #endif
  200. return 0;
  201. auxerr:
  202. OPENSSL_PUT_ERROR(ASN1, asn1_item_ex_combine_new, ASN1_R_AUX_ERROR);
  203. ASN1_item_ex_free(pval, it);
  204. #ifdef CRYPTO_MDEBUG
  205. if (it->sname) CRYPTO_pop_info();
  206. #endif
  207. return 0;
  208. }
  209. static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
  210. {
  211. const ASN1_EXTERN_FUNCS *ef;
  212. switch(it->itype)
  213. {
  214. case ASN1_ITYPE_EXTERN:
  215. ef = it->funcs;
  216. if (ef && ef->asn1_ex_clear)
  217. ef->asn1_ex_clear(pval, it);
  218. else *pval = NULL;
  219. break;
  220. case ASN1_ITYPE_PRIMITIVE:
  221. if (it->templates)
  222. asn1_template_clear(pval, it->templates);
  223. else
  224. asn1_primitive_clear(pval, it);
  225. break;
  226. case ASN1_ITYPE_MSTRING:
  227. asn1_primitive_clear(pval, it);
  228. break;
  229. case ASN1_ITYPE_COMPAT:
  230. case ASN1_ITYPE_CHOICE:
  231. case ASN1_ITYPE_SEQUENCE:
  232. case ASN1_ITYPE_NDEF_SEQUENCE:
  233. *pval = NULL;
  234. break;
  235. }
  236. }
  237. int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
  238. {
  239. const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item);
  240. int ret;
  241. if (tt->flags & ASN1_TFLG_OPTIONAL)
  242. {
  243. asn1_template_clear(pval, tt);
  244. return 1;
  245. }
  246. /* If ANY DEFINED BY nothing to do */
  247. if (tt->flags & ASN1_TFLG_ADB_MASK)
  248. {
  249. *pval = NULL;
  250. return 1;
  251. }
  252. #ifdef CRYPTO_MDEBUG
  253. if (tt->field_name)
  254. CRYPTO_push_info(tt->field_name);
  255. #endif
  256. /* If SET OF or SEQUENCE OF, its a STACK */
  257. if (tt->flags & ASN1_TFLG_SK_MASK)
  258. {
  259. STACK_OF(ASN1_VALUE) *skval;
  260. skval = sk_ASN1_VALUE_new_null();
  261. if (!skval)
  262. {
  263. OPENSSL_PUT_ERROR(ASN1, ASN1_template_new, ERR_R_MALLOC_FAILURE);
  264. ret = 0;
  265. goto done;
  266. }
  267. *pval = (ASN1_VALUE *)skval;
  268. ret = 1;
  269. goto done;
  270. }
  271. /* Otherwise pass it back to the item routine */
  272. ret = asn1_item_ex_combine_new(pval, it, tt->flags & ASN1_TFLG_COMBINE);
  273. done:
  274. #ifdef CRYPTO_MDEBUG
  275. if (it->sname)
  276. CRYPTO_pop_info();
  277. #endif
  278. return ret;
  279. }
  280. static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
  281. {
  282. /* If ADB or STACK just NULL the field */
  283. if (tt->flags & (ASN1_TFLG_ADB_MASK|ASN1_TFLG_SK_MASK))
  284. *pval = NULL;
  285. else
  286. asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item));
  287. }
  288. /* NB: could probably combine most of the real XXX_new() behaviour and junk
  289. * all the old functions.
  290. */
  291. int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
  292. {
  293. ASN1_TYPE *typ;
  294. ASN1_STRING *str;
  295. int utype;
  296. if (!it)
  297. return 0;
  298. if (it->funcs)
  299. {
  300. const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
  301. if (pf->prim_new)
  302. return pf->prim_new(pval, it);
  303. }
  304. if (it->itype == ASN1_ITYPE_MSTRING)
  305. utype = -1;
  306. else
  307. utype = it->utype;
  308. switch(utype)
  309. {
  310. case V_ASN1_OBJECT:
  311. *pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef);
  312. return 1;
  313. case V_ASN1_BOOLEAN:
  314. *(ASN1_BOOLEAN *)pval = it->size;
  315. return 1;
  316. case V_ASN1_NULL:
  317. *pval = (ASN1_VALUE *)1;
  318. return 1;
  319. case V_ASN1_ANY:
  320. typ = OPENSSL_malloc(sizeof(ASN1_TYPE));
  321. if (!typ)
  322. return 0;
  323. typ->value.ptr = NULL;
  324. typ->type = -1;
  325. *pval = (ASN1_VALUE *)typ;
  326. break;
  327. default:
  328. str = ASN1_STRING_type_new(utype);
  329. if (it->itype == ASN1_ITYPE_MSTRING && str)
  330. str->flags |= ASN1_STRING_FLAG_MSTRING;
  331. *pval = (ASN1_VALUE *)str;
  332. break;
  333. }
  334. if (*pval)
  335. return 1;
  336. return 0;
  337. }
  338. static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
  339. {
  340. int utype;
  341. if (it && it->funcs)
  342. {
  343. const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
  344. if (pf->prim_clear)
  345. pf->prim_clear(pval, it);
  346. else
  347. *pval = NULL;
  348. return;
  349. }
  350. if (!it || (it->itype == ASN1_ITYPE_MSTRING))
  351. utype = -1;
  352. else
  353. utype = it->utype;
  354. if (utype == V_ASN1_BOOLEAN)
  355. *(ASN1_BOOLEAN *)pval = it->size;
  356. else *pval = NULL;
  357. }