tasn_fre.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /* tasn_fre.c */
  2. /*
  3. * Written by Dr Stephen N Henson ([email protected]) for the OpenSSL project
  4. * 2000.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * [email protected].
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * ([email protected]). This product includes software written by Tim
  56. * Hudson ([email protected]).
  57. *
  58. */
  59. #include <stddef.h>
  60. #include <openssl/asn1.h>
  61. #include <openssl/asn1t.h>
  62. #include <openssl/objects.h>
  63. #include "asn1_int.h"
  64. /* Free up an ASN1 structure */
  65. void ASN1_item_free(ASN1_VALUE *val, const ASN1_ITEM *it)
  66. {
  67. asn1_item_combine_free(&val, it, 0);
  68. }
  69. void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
  70. {
  71. asn1_item_combine_free(pval, it, 0);
  72. }
  73. void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int combine)
  74. {
  75. const ASN1_TEMPLATE *tt = NULL, *seqtt;
  76. const ASN1_EXTERN_FUNCS *ef;
  77. const ASN1_COMPAT_FUNCS *cf;
  78. const ASN1_AUX *aux = it->funcs;
  79. ASN1_aux_cb *asn1_cb;
  80. int i;
  81. if (!pval)
  82. return;
  83. if ((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval)
  84. return;
  85. if (aux && aux->asn1_cb)
  86. asn1_cb = aux->asn1_cb;
  87. else
  88. asn1_cb = 0;
  89. switch (it->itype) {
  90. case ASN1_ITYPE_PRIMITIVE:
  91. if (it->templates)
  92. ASN1_template_free(pval, it->templates);
  93. else
  94. ASN1_primitive_free(pval, it);
  95. break;
  96. case ASN1_ITYPE_MSTRING:
  97. ASN1_primitive_free(pval, it);
  98. break;
  99. case ASN1_ITYPE_CHOICE:
  100. if (asn1_cb) {
  101. i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
  102. if (i == 2)
  103. return;
  104. }
  105. i = asn1_get_choice_selector(pval, it);
  106. if ((i >= 0) && (i < it->tcount)) {
  107. ASN1_VALUE **pchval;
  108. tt = it->templates + i;
  109. pchval = asn1_get_field_ptr(pval, tt);
  110. ASN1_template_free(pchval, tt);
  111. }
  112. if (asn1_cb)
  113. asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
  114. if (!combine) {
  115. OPENSSL_free(*pval);
  116. *pval = NULL;
  117. }
  118. break;
  119. case ASN1_ITYPE_COMPAT:
  120. cf = it->funcs;
  121. if (cf && cf->asn1_free)
  122. cf->asn1_free(*pval);
  123. break;
  124. case ASN1_ITYPE_EXTERN:
  125. ef = it->funcs;
  126. if (ef && ef->asn1_ex_free)
  127. ef->asn1_ex_free(pval, it);
  128. break;
  129. case ASN1_ITYPE_NDEF_SEQUENCE:
  130. case ASN1_ITYPE_SEQUENCE:
  131. if (asn1_do_lock(pval, -1, it) > 0)
  132. return;
  133. if (asn1_cb) {
  134. i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
  135. if (i == 2)
  136. return;
  137. }
  138. asn1_enc_free(pval, it);
  139. /*
  140. * If we free up as normal we will invalidate any ANY DEFINED BY
  141. * field and we wont be able to determine the type of the field it
  142. * defines. So free up in reverse order.
  143. */
  144. tt = it->templates + it->tcount - 1;
  145. for (i = 0; i < it->tcount; tt--, i++) {
  146. ASN1_VALUE **pseqval;
  147. seqtt = asn1_do_adb(pval, tt, 0);
  148. if (!seqtt)
  149. continue;
  150. pseqval = asn1_get_field_ptr(pval, seqtt);
  151. ASN1_template_free(pseqval, seqtt);
  152. }
  153. if (asn1_cb)
  154. asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
  155. if (!combine) {
  156. OPENSSL_free(*pval);
  157. *pval = NULL;
  158. }
  159. break;
  160. }
  161. }
  162. void ASN1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
  163. {
  164. int i;
  165. if (tt->flags & ASN1_TFLG_SK_MASK) {
  166. STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
  167. for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
  168. ASN1_VALUE *vtmp;
  169. vtmp = sk_ASN1_VALUE_value(sk, i);
  170. asn1_item_combine_free(&vtmp, ASN1_ITEM_ptr(tt->item), 0);
  171. }
  172. sk_ASN1_VALUE_free(sk);
  173. *pval = NULL;
  174. } else
  175. asn1_item_combine_free(pval, ASN1_ITEM_ptr(tt->item),
  176. tt->flags & ASN1_TFLG_COMBINE);
  177. }
  178. void ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
  179. {
  180. int utype;
  181. if (it) {
  182. const ASN1_PRIMITIVE_FUNCS *pf;
  183. pf = it->funcs;
  184. if (pf && pf->prim_free) {
  185. pf->prim_free(pval, it);
  186. return;
  187. }
  188. }
  189. /* Special case: if 'it' is NULL free contents of ASN1_TYPE */
  190. if (!it) {
  191. ASN1_TYPE *typ = (ASN1_TYPE *)*pval;
  192. utype = typ->type;
  193. pval = &typ->value.asn1_value;
  194. if (!*pval)
  195. return;
  196. } else if (it->itype == ASN1_ITYPE_MSTRING) {
  197. utype = -1;
  198. if (!*pval)
  199. return;
  200. } else {
  201. utype = it->utype;
  202. if ((utype != V_ASN1_BOOLEAN) && !*pval)
  203. return;
  204. }
  205. switch (utype) {
  206. case V_ASN1_OBJECT:
  207. ASN1_OBJECT_free((ASN1_OBJECT *)*pval);
  208. break;
  209. case V_ASN1_BOOLEAN:
  210. if (it)
  211. *(ASN1_BOOLEAN *)pval = it->size;
  212. else
  213. *(ASN1_BOOLEAN *)pval = -1;
  214. return;
  215. case V_ASN1_NULL:
  216. break;
  217. case V_ASN1_ANY:
  218. ASN1_primitive_free(pval, NULL);
  219. OPENSSL_free(*pval);
  220. break;
  221. default:
  222. ASN1_STRING_free((ASN1_STRING *)*pval);
  223. *pval = NULL;
  224. break;
  225. }
  226. *pval = NULL;
  227. }