/src/openssl30/crypto/asn1/tasn_fre.c
Line | Count | Source (jump to first uncovered line) |
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 | | |
10 | | #include <stddef.h> |
11 | | #include <openssl/asn1.h> |
12 | | #include <openssl/asn1t.h> |
13 | | #include <openssl/objects.h> |
14 | | #include "asn1_local.h" |
15 | | |
16 | | /* Free up an ASN1 structure */ |
17 | | |
18 | | void ASN1_item_free(ASN1_VALUE *val, const ASN1_ITEM *it) |
19 | 31.4M | { |
20 | 31.4M | ossl_asn1_item_embed_free(&val, it, 0); |
21 | 31.4M | } |
22 | | |
23 | | void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it) |
24 | 7.63M | { |
25 | 7.63M | ossl_asn1_item_embed_free(pval, it, 0); |
26 | 7.63M | } |
27 | | |
28 | | void ossl_asn1_item_embed_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed) |
29 | 93.9M | { |
30 | 93.9M | const ASN1_TEMPLATE *tt = NULL, *seqtt; |
31 | 93.9M | const ASN1_EXTERN_FUNCS *ef; |
32 | 93.9M | const ASN1_AUX *aux = it->funcs; |
33 | 93.9M | ASN1_aux_cb *asn1_cb; |
34 | 93.9M | int i; |
35 | | |
36 | 93.9M | if (pval == NULL) |
37 | 0 | return; |
38 | 93.9M | if ((it->itype != ASN1_ITYPE_PRIMITIVE) && *pval == NULL) |
39 | 9.38M | return; |
40 | 84.6M | if (aux && aux->asn1_cb) |
41 | 2.70M | asn1_cb = aux->asn1_cb; |
42 | 81.9M | else |
43 | 81.9M | asn1_cb = 0; |
44 | | |
45 | 84.6M | switch (it->itype) { |
46 | | |
47 | 51.0M | case ASN1_ITYPE_PRIMITIVE: |
48 | 51.0M | if (it->templates) |
49 | 10.3M | ossl_asn1_template_free(pval, it->templates); |
50 | 40.7M | else |
51 | 40.7M | ossl_asn1_primitive_free(pval, it, embed); |
52 | 51.0M | break; |
53 | | |
54 | 10.4M | case ASN1_ITYPE_MSTRING: |
55 | 10.4M | ossl_asn1_primitive_free(pval, it, embed); |
56 | 10.4M | break; |
57 | | |
58 | 1.59M | case ASN1_ITYPE_CHOICE: |
59 | 1.59M | if (asn1_cb) { |
60 | 130k | i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL); |
61 | 130k | if (i == 2) |
62 | 0 | return; |
63 | 130k | } |
64 | 1.59M | i = ossl_asn1_get_choice_selector(pval, it); |
65 | 1.59M | if ((i >= 0) && (i < it->tcount)) { |
66 | 906k | ASN1_VALUE **pchval; |
67 | | |
68 | 906k | tt = it->templates + i; |
69 | 906k | pchval = ossl_asn1_get_field_ptr(pval, tt); |
70 | 906k | ossl_asn1_template_free(pchval, tt); |
71 | 906k | } |
72 | 1.59M | if (asn1_cb) |
73 | 130k | asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL); |
74 | 1.59M | if (embed == 0) { |
75 | 1.55M | OPENSSL_free(*pval); |
76 | 1.55M | *pval = NULL; |
77 | 1.55M | } |
78 | 1.59M | break; |
79 | | |
80 | 1.90M | case ASN1_ITYPE_EXTERN: |
81 | 1.90M | ef = it->funcs; |
82 | 1.90M | if (ef && ef->asn1_ex_free) |
83 | 1.90M | ef->asn1_ex_free(pval, it); |
84 | 1.90M | break; |
85 | | |
86 | 344k | case ASN1_ITYPE_NDEF_SEQUENCE: |
87 | 19.5M | case ASN1_ITYPE_SEQUENCE: |
88 | 19.5M | if (ossl_asn1_do_lock(pval, -1, it) != 0) /* if error or ref-counter > 0 */ |
89 | 84.4k | return; |
90 | 19.4M | if (asn1_cb) { |
91 | 1.56M | i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL); |
92 | 1.56M | if (i == 2) |
93 | 293k | return; |
94 | 1.56M | } |
95 | 19.1M | ossl_asn1_enc_free(pval, it); |
96 | | /* |
97 | | * If we free up as normal we will invalidate any ANY DEFINED BY |
98 | | * field and we won't be able to determine the type of the field it |
99 | | * defines. So free up in reverse order. |
100 | | */ |
101 | 19.1M | tt = it->templates + it->tcount; |
102 | 68.4M | for (i = 0; i < it->tcount; i++) { |
103 | 49.3M | ASN1_VALUE **pseqval; |
104 | | |
105 | 49.3M | tt--; |
106 | 49.3M | seqtt = ossl_asn1_do_adb(*pval, tt, 0); |
107 | 49.3M | if (!seqtt) |
108 | 163k | continue; |
109 | 49.1M | pseqval = ossl_asn1_get_field_ptr(pval, seqtt); |
110 | 49.1M | ossl_asn1_template_free(pseqval, seqtt); |
111 | 49.1M | } |
112 | 19.1M | if (asn1_cb) |
113 | 1.27M | asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL); |
114 | 19.1M | if (embed == 0) { |
115 | 17.3M | OPENSSL_free(*pval); |
116 | 17.3M | *pval = NULL; |
117 | 17.3M | } |
118 | 19.1M | break; |
119 | 84.6M | } |
120 | 84.6M | } |
121 | | |
122 | | void ossl_asn1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) |
123 | 66.9M | { |
124 | 66.9M | int embed = tt->flags & ASN1_TFLG_EMBED; |
125 | 66.9M | ASN1_VALUE *tval; |
126 | 66.9M | if (embed) { |
127 | 4.11M | tval = (ASN1_VALUE *)pval; |
128 | 4.11M | pval = &tval; |
129 | 4.11M | } |
130 | 66.9M | if (tt->flags & ASN1_TFLG_SK_MASK) { |
131 | 13.9M | STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval; |
132 | 13.9M | int i; |
133 | | |
134 | 29.5M | for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) { |
135 | 15.6M | ASN1_VALUE *vtmp = sk_ASN1_VALUE_value(sk, i); |
136 | | |
137 | 15.6M | ossl_asn1_item_embed_free(&vtmp, ASN1_ITEM_ptr(tt->item), embed); |
138 | 15.6M | } |
139 | 13.9M | sk_ASN1_VALUE_free(sk); |
140 | 13.9M | *pval = NULL; |
141 | 53.0M | } else { |
142 | 53.0M | ossl_asn1_item_embed_free(pval, ASN1_ITEM_ptr(tt->item), embed); |
143 | 53.0M | } |
144 | 66.9M | } |
145 | | |
146 | | void ossl_asn1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed) |
147 | 57.1M | { |
148 | 57.1M | int utype; |
149 | | |
150 | | /* Special case: if 'it' is a primitive with a free_func, use that. */ |
151 | 57.1M | if (it) { |
152 | 51.2M | const ASN1_PRIMITIVE_FUNCS *pf = it->funcs; |
153 | | |
154 | 51.2M | if (embed) { |
155 | 2.27M | if (pf && pf->prim_clear) { |
156 | 488k | pf->prim_clear(pval, it); |
157 | 488k | return; |
158 | 488k | } |
159 | 48.9M | } else if (pf && pf->prim_free) { |
160 | 438k | pf->prim_free(pval, it); |
161 | 438k | return; |
162 | 438k | } |
163 | 51.2M | } |
164 | | |
165 | | /* Special case: if 'it' is NULL, free contents of ASN1_TYPE */ |
166 | 56.1M | if (!it) { |
167 | 5.87M | ASN1_TYPE *typ = (ASN1_TYPE *)*pval; |
168 | | |
169 | 5.87M | utype = typ->type; |
170 | 5.87M | pval = &typ->value.asn1_value; |
171 | 5.87M | if (*pval == NULL) |
172 | 570k | return; |
173 | 50.3M | } else if (it->itype == ASN1_ITYPE_MSTRING) { |
174 | 10.4M | utype = -1; |
175 | 10.4M | if (*pval == NULL) |
176 | 0 | return; |
177 | 39.8M | } else { |
178 | 39.8M | utype = it->utype; |
179 | 39.8M | if ((utype != V_ASN1_BOOLEAN) && *pval == NULL) |
180 | 9.88M | return; |
181 | 39.8M | } |
182 | | |
183 | 45.7M | switch (utype) { |
184 | 14.6M | case V_ASN1_OBJECT: |
185 | 14.6M | ASN1_OBJECT_free((ASN1_OBJECT *)*pval); |
186 | 14.6M | break; |
187 | | |
188 | 2.48M | case V_ASN1_BOOLEAN: |
189 | 2.48M | if (it) |
190 | 2.47M | *(ASN1_BOOLEAN *)pval = it->size; |
191 | 12.0k | else |
192 | 12.0k | *(ASN1_BOOLEAN *)pval = -1; |
193 | 2.48M | return; |
194 | | |
195 | 4.47k | case V_ASN1_NULL: |
196 | 4.47k | break; |
197 | | |
198 | 5.87M | case V_ASN1_ANY: |
199 | 5.87M | ossl_asn1_primitive_free(pval, NULL, 0); |
200 | 5.87M | OPENSSL_free(*pval); |
201 | 5.87M | break; |
202 | | |
203 | 22.6M | default: |
204 | 22.6M | ossl_asn1_string_embed_free((ASN1_STRING *)*pval, embed); |
205 | 22.6M | break; |
206 | 45.7M | } |
207 | 43.2M | *pval = NULL; |
208 | 43.2M | } |