tasn_fre.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Copyright 2000-2024 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stddef.h>
  10. #include <openssl/asn1.h>
  11. #include <openssl/asn1t.h>
  12. #include <openssl/objects.h>
  13. #include "asn1_local.h"
  14. /* Free up an ASN1 structure */
  15. void ASN1_item_free(ASN1_VALUE *val, const ASN1_ITEM *it)
  16. {
  17. ossl_asn1_item_embed_free(&val, it, 0);
  18. }
  19. void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
  20. {
  21. ossl_asn1_item_embed_free(pval, it, 0);
  22. }
  23. void ossl_asn1_item_embed_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
  24. {
  25. const ASN1_TEMPLATE *tt = NULL, *seqtt;
  26. const ASN1_EXTERN_FUNCS *ef;
  27. const ASN1_AUX *aux = it->funcs;
  28. ASN1_aux_cb *asn1_cb;
  29. int i;
  30. if (pval == NULL)
  31. return;
  32. if ((it->itype != ASN1_ITYPE_PRIMITIVE) && *pval == NULL)
  33. return;
  34. if (aux && aux->asn1_cb)
  35. asn1_cb = aux->asn1_cb;
  36. else
  37. asn1_cb = 0;
  38. switch (it->itype) {
  39. case ASN1_ITYPE_PRIMITIVE:
  40. if (it->templates)
  41. ossl_asn1_template_free(pval, it->templates);
  42. else
  43. ossl_asn1_primitive_free(pval, it, embed);
  44. break;
  45. case ASN1_ITYPE_MSTRING:
  46. ossl_asn1_primitive_free(pval, it, embed);
  47. break;
  48. case ASN1_ITYPE_CHOICE:
  49. if (asn1_cb) {
  50. i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
  51. if (i == 2)
  52. return;
  53. }
  54. i = ossl_asn1_get_choice_selector(pval, it);
  55. if ((i >= 0) && (i < it->tcount)) {
  56. ASN1_VALUE **pchval;
  57. tt = it->templates + i;
  58. pchval = ossl_asn1_get_field_ptr(pval, tt);
  59. ossl_asn1_template_free(pchval, tt);
  60. }
  61. if (asn1_cb)
  62. asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
  63. if (embed == 0) {
  64. OPENSSL_free(*pval);
  65. *pval = NULL;
  66. }
  67. break;
  68. case ASN1_ITYPE_EXTERN:
  69. ef = it->funcs;
  70. if (ef && ef->asn1_ex_free)
  71. ef->asn1_ex_free(pval, it);
  72. break;
  73. case ASN1_ITYPE_NDEF_SEQUENCE:
  74. case ASN1_ITYPE_SEQUENCE:
  75. if (ossl_asn1_do_lock(pval, -1, it) != 0) {
  76. /* if error or ref-counter > 0 */
  77. OPENSSL_assert(embed == 0);
  78. *pval = NULL;
  79. return;
  80. }
  81. if (asn1_cb) {
  82. i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
  83. if (i == 2)
  84. return;
  85. }
  86. ossl_asn1_enc_free(pval, it);
  87. /*
  88. * If we free up as normal we will invalidate any ANY DEFINED BY
  89. * field and we won't be able to determine the type of the field it
  90. * defines. So free up in reverse order.
  91. */
  92. tt = it->templates + it->tcount;
  93. for (i = 0; i < it->tcount; i++) {
  94. ASN1_VALUE **pseqval;
  95. tt--;
  96. seqtt = ossl_asn1_do_adb(*pval, tt, 0);
  97. if (!seqtt)
  98. continue;
  99. pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
  100. ossl_asn1_template_free(pseqval, seqtt);
  101. }
  102. if (asn1_cb)
  103. asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
  104. if (embed == 0) {
  105. OPENSSL_free(*pval);
  106. *pval = NULL;
  107. }
  108. break;
  109. }
  110. }
  111. void ossl_asn1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
  112. {
  113. int embed = tt->flags & ASN1_TFLG_EMBED;
  114. ASN1_VALUE *tval;
  115. if (embed) {
  116. tval = (ASN1_VALUE *)pval;
  117. pval = &tval;
  118. }
  119. if (tt->flags & ASN1_TFLG_SK_MASK) {
  120. STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
  121. int i;
  122. for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
  123. ASN1_VALUE *vtmp = sk_ASN1_VALUE_value(sk, i);
  124. ossl_asn1_item_embed_free(&vtmp, ASN1_ITEM_ptr(tt->item), embed);
  125. }
  126. sk_ASN1_VALUE_free(sk);
  127. *pval = NULL;
  128. } else {
  129. ossl_asn1_item_embed_free(pval, ASN1_ITEM_ptr(tt->item), embed);
  130. }
  131. }
  132. void ossl_asn1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
  133. {
  134. int utype;
  135. /* Special case: if 'it' is a primitive with a free_func, use that. */
  136. if (it) {
  137. const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
  138. if (embed) {
  139. if (pf && pf->prim_clear) {
  140. pf->prim_clear(pval, it);
  141. return;
  142. }
  143. } else if (pf && pf->prim_free) {
  144. pf->prim_free(pval, it);
  145. return;
  146. }
  147. }
  148. /* Special case: if 'it' is NULL, free contents of ASN1_TYPE */
  149. if (!it) {
  150. ASN1_TYPE *typ = (ASN1_TYPE *)*pval;
  151. utype = typ->type;
  152. pval = &typ->value.asn1_value;
  153. if (*pval == NULL)
  154. return;
  155. } else if (it->itype == ASN1_ITYPE_MSTRING) {
  156. utype = -1;
  157. if (*pval == NULL)
  158. return;
  159. } else {
  160. utype = it->utype;
  161. if ((utype != V_ASN1_BOOLEAN) && *pval == NULL)
  162. return;
  163. }
  164. switch (utype) {
  165. case V_ASN1_OBJECT:
  166. ASN1_OBJECT_free((ASN1_OBJECT *)*pval);
  167. break;
  168. case V_ASN1_BOOLEAN:
  169. if (it)
  170. *(ASN1_BOOLEAN *)pval = it->size;
  171. else
  172. *(ASN1_BOOLEAN *)pval = -1;
  173. return;
  174. case V_ASN1_NULL:
  175. break;
  176. case V_ASN1_ANY:
  177. ossl_asn1_primitive_free(pval, NULL, 0);
  178. OPENSSL_free(*pval);
  179. break;
  180. default:
  181. ossl_asn1_string_embed_free((ASN1_STRING *)*pval, embed);
  182. break;
  183. }
  184. *pval = NULL;
  185. }