2
0

tasn_fre.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Copyright 2000-2021 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) /* if error or ref-counter > 0 */
  76. return;
  77. if (asn1_cb) {
  78. i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
  79. if (i == 2)
  80. return;
  81. }
  82. ossl_asn1_enc_free(pval, it);
  83. /*
  84. * If we free up as normal we will invalidate any ANY DEFINED BY
  85. * field and we won't be able to determine the type of the field it
  86. * defines. So free up in reverse order.
  87. */
  88. tt = it->templates + it->tcount;
  89. for (i = 0; i < it->tcount; i++) {
  90. ASN1_VALUE **pseqval;
  91. tt--;
  92. seqtt = ossl_asn1_do_adb(*pval, tt, 0);
  93. if (!seqtt)
  94. continue;
  95. pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
  96. ossl_asn1_template_free(pseqval, seqtt);
  97. }
  98. if (asn1_cb)
  99. asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
  100. if (embed == 0) {
  101. OPENSSL_free(*pval);
  102. *pval = NULL;
  103. }
  104. break;
  105. }
  106. }
  107. void ossl_asn1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
  108. {
  109. int embed = tt->flags & ASN1_TFLG_EMBED;
  110. ASN1_VALUE *tval;
  111. if (embed) {
  112. tval = (ASN1_VALUE *)pval;
  113. pval = &tval;
  114. }
  115. if (tt->flags & ASN1_TFLG_SK_MASK) {
  116. STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
  117. int i;
  118. for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
  119. ASN1_VALUE *vtmp = sk_ASN1_VALUE_value(sk, i);
  120. ossl_asn1_item_embed_free(&vtmp, ASN1_ITEM_ptr(tt->item), embed);
  121. }
  122. sk_ASN1_VALUE_free(sk);
  123. *pval = NULL;
  124. } else {
  125. ossl_asn1_item_embed_free(pval, ASN1_ITEM_ptr(tt->item), embed);
  126. }
  127. }
  128. void ossl_asn1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
  129. {
  130. int utype;
  131. /* Special case: if 'it' is a primitive with a free_func, use that. */
  132. if (it) {
  133. const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
  134. if (embed) {
  135. if (pf && pf->prim_clear) {
  136. pf->prim_clear(pval, it);
  137. return;
  138. }
  139. } else if (pf && pf->prim_free) {
  140. pf->prim_free(pval, it);
  141. return;
  142. }
  143. }
  144. /* Special case: if 'it' is NULL, free contents of ASN1_TYPE */
  145. if (!it) {
  146. ASN1_TYPE *typ = (ASN1_TYPE *)*pval;
  147. utype = typ->type;
  148. pval = &typ->value.asn1_value;
  149. if (*pval == NULL)
  150. return;
  151. } else if (it->itype == ASN1_ITYPE_MSTRING) {
  152. utype = -1;
  153. if (*pval == NULL)
  154. return;
  155. } else {
  156. utype = it->utype;
  157. if ((utype != V_ASN1_BOOLEAN) && *pval == NULL)
  158. return;
  159. }
  160. switch (utype) {
  161. case V_ASN1_OBJECT:
  162. ASN1_OBJECT_free((ASN1_OBJECT *)*pval);
  163. break;
  164. case V_ASN1_BOOLEAN:
  165. if (it)
  166. *(ASN1_BOOLEAN *)pval = it->size;
  167. else
  168. *(ASN1_BOOLEAN *)pval = -1;
  169. return;
  170. case V_ASN1_NULL:
  171. break;
  172. case V_ASN1_ANY:
  173. ossl_asn1_primitive_free(pval, NULL, 0);
  174. OPENSSL_free(*pval);
  175. break;
  176. default:
  177. ossl_asn1_string_embed_free((ASN1_STRING *)*pval, embed);
  178. break;
  179. }
  180. *pval = NULL;
  181. }