/src/openssl32/crypto/asn1/tasn_fre.c
Line | Count | Source (jump to first uncovered line) |
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 | | |
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 | 62.8M | { |
20 | 62.8M | ossl_asn1_item_embed_free(&val, it, 0); |
21 | 62.8M | } |
22 | | |
23 | | void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it) |
24 | 11.7M | { |
25 | 11.7M | ossl_asn1_item_embed_free(pval, it, 0); |
26 | 11.7M | } |
27 | | |
28 | | void ossl_asn1_item_embed_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed) |
29 | 291M | { |
30 | 291M | const ASN1_TEMPLATE *tt = NULL, *seqtt; |
31 | 291M | const ASN1_EXTERN_FUNCS *ef; |
32 | 291M | const ASN1_AUX *aux = it->funcs; |
33 | 291M | ASN1_aux_cb *asn1_cb; |
34 | 291M | int i; |
35 | | |
36 | 291M | if (pval == NULL) |
37 | 0 | return; |
38 | 291M | if ((it->itype != ASN1_ITYPE_PRIMITIVE) && *pval == NULL) |
39 | 20.9M | return; |
40 | 270M | if (aux && aux->asn1_cb) |
41 | 6.05M | asn1_cb = aux->asn1_cb; |
42 | 264M | else |
43 | 264M | asn1_cb = 0; |
44 | | |
45 | 270M | switch (it->itype) { |
46 | | |
47 | 161M | case ASN1_ITYPE_PRIMITIVE: |
48 | 161M | if (it->templates) |
49 | 30.2M | ossl_asn1_template_free(pval, it->templates); |
50 | 131M | else |
51 | 131M | ossl_asn1_primitive_free(pval, it, embed); |
52 | 161M | break; |
53 | | |
54 | 39.2M | case ASN1_ITYPE_MSTRING: |
55 | 39.2M | ossl_asn1_primitive_free(pval, it, embed); |
56 | 39.2M | break; |
57 | | |
58 | 5.89M | case ASN1_ITYPE_CHOICE: |
59 | 5.89M | if (asn1_cb) { |
60 | 255k | i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL); |
61 | 255k | if (i == 2) |
62 | 0 | return; |
63 | 255k | } |
64 | 5.89M | i = ossl_asn1_get_choice_selector(pval, it); |
65 | 5.89M | if ((i >= 0) && (i < it->tcount)) { |
66 | 4.39M | ASN1_VALUE **pchval; |
67 | | |
68 | 4.39M | tt = it->templates + i; |
69 | 4.39M | pchval = ossl_asn1_get_field_ptr(pval, tt); |
70 | 4.39M | ossl_asn1_template_free(pchval, tt); |
71 | 4.39M | } |
72 | 5.89M | if (asn1_cb) |
73 | 255k | asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL); |
74 | 5.89M | if (embed == 0) { |
75 | 5.80M | OPENSSL_free(*pval); |
76 | 5.80M | *pval = NULL; |
77 | 5.80M | } |
78 | 5.89M | break; |
79 | | |
80 | 4.19M | case ASN1_ITYPE_EXTERN: |
81 | 4.19M | ef = it->funcs; |
82 | 4.19M | if (ef && ef->asn1_ex_free) |
83 | 4.19M | ef->asn1_ex_free(pval, it); |
84 | 4.19M | break; |
85 | | |
86 | 764k | case ASN1_ITYPE_NDEF_SEQUENCE: |
87 | 59.7M | case ASN1_ITYPE_SEQUENCE: |
88 | 59.7M | if (ossl_asn1_do_lock(pval, -1, it) != 0) { |
89 | | /* if error or ref-counter > 0 */ |
90 | 242k | OPENSSL_assert(embed == 0); |
91 | 242k | *pval = NULL; |
92 | 242k | return; |
93 | 242k | } |
94 | 59.4M | if (asn1_cb) { |
95 | 3.53M | i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL); |
96 | 3.53M | if (i == 2) |
97 | 607k | return; |
98 | 3.53M | } |
99 | 58.8M | ossl_asn1_enc_free(pval, it); |
100 | | /* |
101 | | * If we free up as normal we will invalidate any ANY DEFINED BY |
102 | | * field and we won't be able to determine the type of the field it |
103 | | * defines. So free up in reverse order. |
104 | | */ |
105 | 58.8M | tt = it->templates + it->tcount; |
106 | 201M | for (i = 0; i < it->tcount; i++) { |
107 | 142M | ASN1_VALUE **pseqval; |
108 | | |
109 | 142M | tt--; |
110 | 142M | seqtt = ossl_asn1_do_adb(*pval, tt, 0); |
111 | 142M | if (!seqtt) |
112 | 355k | continue; |
113 | 142M | pseqval = ossl_asn1_get_field_ptr(pval, seqtt); |
114 | 142M | ossl_asn1_template_free(pseqval, seqtt); |
115 | 142M | } |
116 | 58.8M | if (asn1_cb) |
117 | 2.92M | asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL); |
118 | 58.8M | if (embed == 0) { |
119 | 54.7M | OPENSSL_free(*pval); |
120 | 54.7M | *pval = NULL; |
121 | 54.7M | } |
122 | 58.8M | break; |
123 | 270M | } |
124 | 270M | } |
125 | | |
126 | | void ossl_asn1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) |
127 | 191M | { |
128 | 191M | int embed = tt->flags & ASN1_TFLG_EMBED; |
129 | 191M | ASN1_VALUE *tval; |
130 | 191M | if (embed) { |
131 | 9.65M | tval = (ASN1_VALUE *)pval; |
132 | 9.65M | pval = &tval; |
133 | 9.65M | } |
134 | 191M | if (tt->flags & ASN1_TFLG_SK_MASK) { |
135 | 38.5M | STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval; |
136 | 38.5M | int i; |
137 | | |
138 | 103M | for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) { |
139 | 64.8M | ASN1_VALUE *vtmp = sk_ASN1_VALUE_value(sk, i); |
140 | | |
141 | 64.8M | ossl_asn1_item_embed_free(&vtmp, ASN1_ITEM_ptr(tt->item), embed); |
142 | 64.8M | } |
143 | 38.5M | sk_ASN1_VALUE_free(sk); |
144 | 38.5M | *pval = NULL; |
145 | 152M | } else { |
146 | 152M | ossl_asn1_item_embed_free(pval, ASN1_ITEM_ptr(tt->item), embed); |
147 | 152M | } |
148 | 191M | } |
149 | | |
150 | | void ossl_asn1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed) |
151 | 208M | { |
152 | 208M | int utype; |
153 | | |
154 | | /* Special case: if 'it' is a primitive with a free_func, use that. */ |
155 | 208M | if (it) { |
156 | 170M | const ASN1_PRIMITIVE_FUNCS *pf = it->funcs; |
157 | | |
158 | 170M | if (embed) { |
159 | 5.46M | if (pf && pf->prim_clear) { |
160 | 1.07M | pf->prim_clear(pval, it); |
161 | 1.07M | return; |
162 | 1.07M | } |
163 | 165M | } else if (pf && pf->prim_free) { |
164 | 949k | pf->prim_free(pval, it); |
165 | 949k | return; |
166 | 949k | } |
167 | 170M | } |
168 | | |
169 | | /* Special case: if 'it' is NULL, free contents of ASN1_TYPE */ |
170 | 206M | if (!it) { |
171 | 38.0M | ASN1_TYPE *typ = (ASN1_TYPE *)*pval; |
172 | | |
173 | 38.0M | utype = typ->type; |
174 | 38.0M | pval = &typ->value.asn1_value; |
175 | 38.0M | if (*pval == NULL) |
176 | 482k | return; |
177 | 168M | } else if (it->itype == ASN1_ITYPE_MSTRING) { |
178 | 39.2M | utype = -1; |
179 | 39.2M | if (*pval == NULL) |
180 | 0 | return; |
181 | 129M | } else { |
182 | 129M | utype = it->utype; |
183 | 129M | if ((utype != V_ASN1_BOOLEAN) && *pval == NULL) |
184 | 21.8M | return; |
185 | 129M | } |
186 | | |
187 | 184M | switch (utype) { |
188 | 48.7M | case V_ASN1_OBJECT: |
189 | 48.7M | ASN1_OBJECT_free((ASN1_OBJECT *)*pval); |
190 | 48.7M | break; |
191 | | |
192 | 24.2M | case V_ASN1_BOOLEAN: |
193 | 24.2M | if (it) |
194 | 5.75M | *(ASN1_BOOLEAN *)pval = it->size; |
195 | 18.4M | else |
196 | 18.4M | *(ASN1_BOOLEAN *)pval = -1; |
197 | 24.2M | return; |
198 | | |
199 | 12.3k | case V_ASN1_NULL: |
200 | 12.3k | break; |
201 | | |
202 | 38.0M | case V_ASN1_ANY: |
203 | 38.0M | ossl_asn1_primitive_free(pval, NULL, 0); |
204 | 38.0M | OPENSSL_free(*pval); |
205 | 38.0M | break; |
206 | | |
207 | 73.4M | default: |
208 | 73.4M | ossl_asn1_string_embed_free((ASN1_STRING *)*pval, embed); |
209 | 73.4M | break; |
210 | 184M | } |
211 | 160M | *pval = NULL; |
212 | 160M | } |