tasn_fre.c
Go to the documentation of this file.
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 
57 #include <openssl/asn1.h>
58 
59 #include <assert.h>
60 
61 #include <openssl/asn1t.h>
62 #include <openssl/mem.h>
63 
64 #include "internal.h"
65 
66 /* Free up an ASN1 structure */
67 
69 {
70  asn1_item_combine_free(&val, it, 0);
71 }
72 
74 {
75  asn1_item_combine_free(pval, it, 0);
76 }
77 
78 void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int combine)
79 {
80  const ASN1_TEMPLATE *tt = NULL, *seqtt;
81  const ASN1_EXTERN_FUNCS *ef;
82  int i;
83  if (!pval)
84  return;
85  if ((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval)
86  return;
87 
88  switch (it->itype) {
89 
91  if (it->templates)
92  ASN1_template_free(pval, it->templates);
93  else
94  ASN1_primitive_free(pval, it);
95  break;
96 
97  case ASN1_ITYPE_MSTRING:
98  ASN1_primitive_free(pval, it);
99  break;
100 
101  case ASN1_ITYPE_CHOICE: {
102  const ASN1_AUX *aux = it->funcs;
103  ASN1_aux_cb *asn1_cb = aux != NULL ? aux->asn1_cb : NULL;
104  if (asn1_cb) {
105  i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
106  if (i == 2)
107  return;
108  }
109  i = asn1_get_choice_selector(pval, it);
110  if ((i >= 0) && (i < it->tcount)) {
111  ASN1_VALUE **pchval;
112  tt = it->templates + i;
113  pchval = asn1_get_field_ptr(pval, tt);
114  ASN1_template_free(pchval, tt);
115  }
116  if (asn1_cb)
117  asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
118  if (!combine) {
119  OPENSSL_free(*pval);
120  *pval = NULL;
121  }
122  break;
123  }
124 
125  case ASN1_ITYPE_EXTERN:
126  ef = it->funcs;
127  if (ef && ef->asn1_ex_free)
128  ef->asn1_ex_free(pval, it);
129  break;
130 
131  case ASN1_ITYPE_SEQUENCE: {
133  return;
134  const ASN1_AUX *aux = it->funcs;
135  ASN1_aux_cb *asn1_cb = aux != NULL ? aux->asn1_cb : NULL;
136  if (asn1_cb) {
137  i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
138  if (i == 2)
139  return;
140  }
141  asn1_enc_free(pval, it);
142  /*
143  * If we free up as normal we will invalidate any ANY DEFINED BY
144  * field and we wont be able to determine the type of the field it
145  * defines. So free up in reverse order.
146  */
147  tt = it->templates + it->tcount - 1;
148  for (i = 0; i < it->tcount; tt--, i++) {
149  ASN1_VALUE **pseqval;
150  seqtt = asn1_do_adb(pval, tt, 0);
151  if (!seqtt)
152  continue;
153  pseqval = asn1_get_field_ptr(pval, seqtt);
154  ASN1_template_free(pseqval, seqtt);
155  }
156  if (asn1_cb)
157  asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
158  if (!combine) {
159  OPENSSL_free(*pval);
160  *pval = NULL;
161  }
162  break;
163  }
164  }
165 }
166 
168 {
169  size_t i;
170  if (tt->flags & ASN1_TFLG_SK_MASK) {
171  STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
172  for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
173  ASN1_VALUE *vtmp;
174  vtmp = sk_ASN1_VALUE_value(sk, i);
175  asn1_item_combine_free(&vtmp, ASN1_ITEM_ptr(tt->item), 0);
176  }
177  sk_ASN1_VALUE_free(sk);
178  *pval = NULL;
179  } else
181  tt->flags & ASN1_TFLG_COMBINE);
182 }
183 
185 {
186  int utype;
187  /* Historically, |it->funcs| for primitive types contained an
188  * |ASN1_PRIMITIVE_FUNCS| table of calbacks. */
189  assert(it == NULL || it->funcs == NULL);
190  /* Special case: if 'it' is NULL free contents of ASN1_TYPE */
191  if (!it) {
192  ASN1_TYPE *typ = (ASN1_TYPE *)*pval;
193  utype = typ->type;
194  pval = &typ->value.asn1_value;
195  if (utype != V_ASN1_BOOLEAN && !*pval)
196  return;
197  } else if (it->itype == ASN1_ITYPE_MSTRING) {
198  utype = -1;
199  if (!*pval)
200  return;
201  } else {
202  utype = it->utype;
203  if ((utype != V_ASN1_BOOLEAN) && !*pval)
204  return;
205  }
206 
207  switch (utype) {
208  case V_ASN1_OBJECT:
209  ASN1_OBJECT_free((ASN1_OBJECT *)*pval);
210  break;
211 
212  case V_ASN1_BOOLEAN:
213  if (it)
214  *(ASN1_BOOLEAN *)pval = it->size;
215  else
216  *(ASN1_BOOLEAN *)pval = -1;
217  return;
218 
219  case V_ASN1_NULL:
220  break;
221 
222  case V_ASN1_ANY:
223  ASN1_primitive_free(pval, NULL);
224  OPENSSL_free(*pval);
225  break;
226 
227  default:
228  ASN1_STRING_free((ASN1_STRING *)*pval);
229  *pval = NULL;
230  break;
231  }
232  *pval = NULL;
233 }
ASN1_ITYPE_CHOICE
#define ASN1_ITYPE_CHOICE
Definition: asn1t.h:509
V_ASN1_ANY
#define V_ASN1_ANY
Definition: asn1.h:121
ASN1_TEMPLATE_st::item
ASN1_ITEM_EXP * item
Definition: asn1t.h:356
ASN1_TFLG_SK_MASK
#define ASN1_TFLG_SK_MASK
Definition: asn1t.h:396
regen-readme.it
it
Definition: regen-readme.py:15
ASN1_EXTERN_FUNCS_st::asn1_ex_free
ASN1_ex_free_func * asn1_ex_free
Definition: asn1t.h:550
ASN1_BOOLEAN
int ASN1_BOOLEAN
Definition: base.h:335
ASN1_EXTERN_FUNCS_st
Definition: asn1t.h:547
asn1_type_st::asn1_value
ASN1_VALUE * asn1_value
Definition: asn1.h:1505
asn1_cb
static int asn1_cb(const char *elem, int len, void *bitstr)
Definition: asn1_gen.c:287
ASN1_ITYPE_SEQUENCE
#define ASN1_ITYPE_SEQUENCE
Definition: asn1t.h:507
ASN1_STRING_free
#define ASN1_STRING_free
Definition: boringssl_prefix_symbols.h:680
asn1_do_adb
#define asn1_do_adb
Definition: boringssl_prefix_symbols.h:2828
ASN1_ITEM_ptr
#define ASN1_ITEM_ptr(iptr)
Definition: asn1.h:316
asn1_object_st
Definition: third_party/boringssl-with-bazel/src/crypto/asn1/internal.h:102
asn1_enc_free
#define asn1_enc_free
Definition: boringssl_prefix_symbols.h:2829
ASN1_TFLG_COMBINE
#define ASN1_TFLG_COMBINE
Definition: asn1t.h:455
asn1_get_field_ptr
#define asn1_get_field_ptr
Definition: boringssl_prefix_symbols.h:2835
ASN1_AUX_st::asn1_cb
ASN1_aux_cb * asn1_cb
Definition: asn1t.h:582
asn1_type_st::type
int type
Definition: asn1.h:1482
ASN1_ITEM_st
Definition: asn1t.h:459
internal.h
STACK_OF
#define STACK_OF(type)
Definition: stack.h:125
ASN1_item_ex_free
void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
Definition: tasn_fre.c:73
asn1t.h
ASN1_ITYPE_EXTERN
#define ASN1_ITYPE_EXTERN
Definition: asn1t.h:511
ASN1_OBJECT_free
#define ASN1_OBJECT_free
Definition: boringssl_prefix_symbols.h:655
ASN1_OP_FREE_PRE
#define ASN1_OP_FREE_PRE
Definition: asn1t.h:597
V_ASN1_BOOLEAN
#define V_ASN1_BOOLEAN
Definition: asn1.h:125
asn1_refcount_dec_and_test_zero
#define asn1_refcount_dec_and_test_zero
Definition: boringssl_prefix_symbols.h:2839
ASN1_aux_cb
int ASN1_aux_cb(int operation, ASN1_VALUE **in, const ASN1_ITEM *it, void *exarg)
Definition: asn1t.h:575
asn1_item_combine_free
void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int combine)
Definition: tasn_fre.c:78
ASN1_TEMPLATE_st
Definition: asn1t.h:351
V_ASN1_NULL
#define V_ASN1_NULL
Definition: asn1.h:129
ASN1_VALUE
struct ASN1_VALUE_st ASN1_VALUE
Definition: asn1.h:320
ASN1_ITYPE_MSTRING
#define ASN1_ITYPE_MSTRING
Definition: asn1t.h:513
ASN1_TEMPLATE_st::flags
unsigned long flags
Definition: asn1t.h:352
asn1_type_st::value
union asn1_type_st::@361 value
ASN1_ITYPE_PRIMITIVE
#define ASN1_ITYPE_PRIMITIVE
Definition: asn1t.h:505
mem.h
ASN1_OP_FREE_POST
#define ASN1_OP_FREE_POST
Definition: asn1t.h:598
asn1_type_st
Definition: asn1.h:1481
ASN1_item_free
void ASN1_item_free(ASN1_VALUE *val, const ASN1_ITEM *it)
Definition: tasn_fre.c:68
asn1_get_choice_selector
#define asn1_get_choice_selector
Definition: boringssl_prefix_symbols.h:2834
ASN1_AUX_st
Definition: asn1t.h:578
OPENSSL_free
#define OPENSSL_free
Definition: boringssl_prefix_symbols.h:1869
V_ASN1_OBJECT
#define V_ASN1_OBJECT
Definition: asn1.h:130
asn1_string_st
Definition: asn1.h:543
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
asn1.h
ASN1_template_free
void ASN1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
Definition: tasn_fre.c:167
ASN1_primitive_free
void ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
Definition: tasn_fre.c:184


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:25