Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

247 lignes
8.0 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 <openssl/asn1t.h>
  58. #include <openssl/mem.h>
  59. static void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it,
  60. int combine);
  61. /* Free up an ASN1 structure */
  62. void ASN1_item_free(ASN1_VALUE *val, const ASN1_ITEM *it)
  63. {
  64. asn1_item_combine_free(&val, it, 0);
  65. }
  66. void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
  67. {
  68. asn1_item_combine_free(pval, it, 0);
  69. }
  70. static void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it,
  71. int combine)
  72. {
  73. const ASN1_TEMPLATE *tt = NULL, *seqtt;
  74. const ASN1_EXTERN_FUNCS *ef;
  75. const ASN1_COMPAT_FUNCS *cf;
  76. const ASN1_AUX *aux = it->funcs;
  77. ASN1_aux_cb *asn1_cb;
  78. int i;
  79. if (!pval)
  80. return;
  81. if ((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval)
  82. return;
  83. if (aux && aux->asn1_cb)
  84. asn1_cb = aux->asn1_cb;
  85. else
  86. asn1_cb = 0;
  87. switch (it->itype) {
  88. case ASN1_ITYPE_PRIMITIVE:
  89. if (it->templates)
  90. ASN1_template_free(pval, it->templates);
  91. else
  92. ASN1_primitive_free(pval, it);
  93. break;
  94. case ASN1_ITYPE_MSTRING:
  95. ASN1_primitive_free(pval, it);
  96. break;
  97. case ASN1_ITYPE_CHOICE:
  98. if (asn1_cb) {
  99. i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
  100. if (i == 2)
  101. return;
  102. }
  103. i = asn1_get_choice_selector(pval, it);
  104. if ((i >= 0) && (i < it->tcount)) {
  105. ASN1_VALUE **pchval;
  106. tt = it->templates + i;
  107. pchval = asn1_get_field_ptr(pval, tt);
  108. ASN1_template_free(pchval, tt);
  109. }
  110. if (asn1_cb)
  111. asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
  112. if (!combine) {
  113. OPENSSL_free(*pval);
  114. *pval = NULL;
  115. }
  116. break;
  117. case ASN1_ITYPE_COMPAT:
  118. cf = it->funcs;
  119. if (cf && cf->asn1_free)
  120. cf->asn1_free(*pval);
  121. break;
  122. case ASN1_ITYPE_EXTERN:
  123. ef = it->funcs;
  124. if (ef && ef->asn1_ex_free)
  125. ef->asn1_ex_free(pval, it);
  126. break;
  127. case ASN1_ITYPE_NDEF_SEQUENCE:
  128. case ASN1_ITYPE_SEQUENCE:
  129. if (!asn1_refcount_dec_and_test_zero(pval, it))
  130. return;
  131. if (asn1_cb) {
  132. i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
  133. if (i == 2)
  134. return;
  135. }
  136. asn1_enc_free(pval, it);
  137. /*
  138. * If we free up as normal we will invalidate any ANY DEFINED BY
  139. * field and we wont be able to determine the type of the field it
  140. * defines. So free up in reverse order.
  141. */
  142. tt = it->templates + it->tcount - 1;
  143. for (i = 0; i < it->tcount; tt--, i++) {
  144. ASN1_VALUE **pseqval;
  145. seqtt = asn1_do_adb(pval, tt, 0);
  146. if (!seqtt)
  147. continue;
  148. pseqval = asn1_get_field_ptr(pval, seqtt);
  149. ASN1_template_free(pseqval, seqtt);
  150. }
  151. if (asn1_cb)
  152. asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
  153. if (!combine) {
  154. OPENSSL_free(*pval);
  155. *pval = NULL;
  156. }
  157. break;
  158. }
  159. }
  160. void ASN1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
  161. {
  162. size_t i;
  163. if (tt->flags & ASN1_TFLG_SK_MASK) {
  164. STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
  165. for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
  166. ASN1_VALUE *vtmp;
  167. vtmp = sk_ASN1_VALUE_value(sk, i);
  168. asn1_item_combine_free(&vtmp, ASN1_ITEM_ptr(tt->item), 0);
  169. }
  170. sk_ASN1_VALUE_free(sk);
  171. *pval = NULL;
  172. } else
  173. asn1_item_combine_free(pval, ASN1_ITEM_ptr(tt->item),
  174. tt->flags & ASN1_TFLG_COMBINE);
  175. }
  176. void ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
  177. {
  178. int utype;
  179. if (it) {
  180. const ASN1_PRIMITIVE_FUNCS *pf;
  181. pf = it->funcs;
  182. if (pf && pf->prim_free) {
  183. pf->prim_free(pval, it);
  184. return;
  185. }
  186. }
  187. /* Special case: if 'it' is NULL free contents of ASN1_TYPE */
  188. if (!it) {
  189. ASN1_TYPE *typ = (ASN1_TYPE *)*pval;
  190. utype = typ->type;
  191. pval = &typ->value.asn1_value;
  192. if (!*pval)
  193. return;
  194. } else if (it->itype == ASN1_ITYPE_MSTRING) {
  195. utype = -1;
  196. if (!*pval)
  197. return;
  198. } else {
  199. utype = it->utype;
  200. if ((utype != V_ASN1_BOOLEAN) && !*pval)
  201. return;
  202. }
  203. switch (utype) {
  204. case V_ASN1_OBJECT:
  205. ASN1_OBJECT_free((ASN1_OBJECT *)*pval);
  206. break;
  207. case V_ASN1_BOOLEAN:
  208. if (it)
  209. *(ASN1_BOOLEAN *)pval = it->size;
  210. else
  211. *(ASN1_BOOLEAN *)pval = -1;
  212. return;
  213. case V_ASN1_NULL:
  214. break;
  215. case V_ASN1_ANY:
  216. ASN1_primitive_free(pval, NULL);
  217. OPENSSL_free(*pval);
  218. break;
  219. default:
  220. ASN1_STRING_free((ASN1_STRING *)*pval);
  221. *pval = NULL;
  222. break;
  223. }
  224. *pval = NULL;
  225. }