/src/openssl111/crypto/asn1/tasn_fre.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the OpenSSL license (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 | 695k | { |
20 | 695k | asn1_item_embed_free(&val, it, 0); |
21 | 695k | } |
22 | | |
23 | | void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it) |
24 | 45.4k | { |
25 | 45.4k | asn1_item_embed_free(pval, it, 0); |
26 | 45.4k | } |
27 | | |
28 | | void asn1_item_embed_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed) |
29 | 4.78M | { |
30 | 4.78M | const ASN1_TEMPLATE *tt = NULL, *seqtt; |
31 | 4.78M | const ASN1_EXTERN_FUNCS *ef; |
32 | 4.78M | const ASN1_AUX *aux = it->funcs; |
33 | 4.78M | ASN1_aux_cb *asn1_cb; |
34 | 4.78M | int i; |
35 | | |
36 | 4.78M | if (!pval) |
37 | 0 | return; |
38 | 4.78M | if ((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval) |
39 | 117k | return; |
40 | 4.66M | if (aux && aux->asn1_cb) |
41 | 28.1k | asn1_cb = aux->asn1_cb; |
42 | 4.64M | else |
43 | 4.64M | asn1_cb = 0; |
44 | | |
45 | 4.66M | switch (it->itype) { |
46 | | |
47 | 3.24M | case ASN1_ITYPE_PRIMITIVE: |
48 | 3.24M | if (it->templates) |
49 | 1.87M | asn1_template_free(pval, it->templates); |
50 | 1.37M | else |
51 | 1.37M | asn1_primitive_free(pval, it, embed); |
52 | 3.24M | break; |
53 | | |
54 | 547k | case ASN1_ITYPE_MSTRING: |
55 | 547k | asn1_primitive_free(pval, it, embed); |
56 | 547k | break; |
57 | | |
58 | 81.0k | case ASN1_ITYPE_CHOICE: |
59 | 81.0k | if (asn1_cb) { |
60 | 8.70k | i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL); |
61 | 8.70k | if (i == 2) |
62 | 0 | return; |
63 | 8.70k | } |
64 | 81.0k | i = asn1_get_choice_selector(pval, it); |
65 | 81.0k | if ((i >= 0) && (i < it->tcount)) { |
66 | 62.4k | ASN1_VALUE **pchval; |
67 | | |
68 | 62.4k | tt = it->templates + i; |
69 | 62.4k | pchval = asn1_get_field_ptr(pval, tt); |
70 | 62.4k | asn1_template_free(pchval, tt); |
71 | 62.4k | } |
72 | 81.0k | if (asn1_cb) |
73 | 8.70k | asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL); |
74 | 81.0k | if (embed == 0) { |
75 | 81.0k | OPENSSL_free(*pval); |
76 | 81.0k | *pval = NULL; |
77 | 81.0k | } |
78 | 81.0k | break; |
79 | | |
80 | 22.5k | case ASN1_ITYPE_EXTERN: |
81 | 22.5k | ef = it->funcs; |
82 | 22.5k | if (ef && ef->asn1_ex_free) |
83 | 22.5k | ef->asn1_ex_free(pval, it); |
84 | 22.5k | break; |
85 | | |
86 | 0 | case ASN1_ITYPE_NDEF_SEQUENCE: |
87 | 768k | case ASN1_ITYPE_SEQUENCE: |
88 | 768k | if (asn1_do_lock(pval, -1, it) != 0) /* if error or ref-counter > 0 */ |
89 | 0 | return; |
90 | 768k | if (asn1_cb) { |
91 | 18.5k | i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL); |
92 | 18.5k | if (i == 2) |
93 | 87 | return; |
94 | 18.5k | } |
95 | 768k | 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 | 768k | tt = it->templates + it->tcount; |
102 | 2.56M | for (i = 0; i < it->tcount; i++) { |
103 | 1.79M | ASN1_VALUE **pseqval; |
104 | | |
105 | 1.79M | tt--; |
106 | 1.79M | seqtt = asn1_do_adb(pval, tt, 0); |
107 | 1.79M | if (!seqtt) |
108 | 585 | continue; |
109 | 1.79M | pseqval = asn1_get_field_ptr(pval, seqtt); |
110 | 1.79M | asn1_template_free(pseqval, seqtt); |
111 | 1.79M | } |
112 | 768k | if (asn1_cb) |
113 | 18.4k | asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL); |
114 | 768k | if (embed == 0) { |
115 | 731k | OPENSSL_free(*pval); |
116 | 731k | *pval = NULL; |
117 | 731k | } |
118 | 768k | break; |
119 | 4.66M | } |
120 | 4.66M | } |
121 | | |
122 | | void asn1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) |
123 | 3.96M | { |
124 | 3.96M | int embed = tt->flags & ASN1_TFLG_EMBED; |
125 | 3.96M | ASN1_VALUE *tval; |
126 | 3.96M | if (embed) { |
127 | 164k | tval = (ASN1_VALUE *)pval; |
128 | 164k | pval = &tval; |
129 | 164k | } |
130 | 3.96M | if (tt->flags & ASN1_TFLG_SK_MASK) { |
131 | 1.94M | STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval; |
132 | 1.94M | int i; |
133 | | |
134 | 3.96M | for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) { |
135 | 2.02M | ASN1_VALUE *vtmp = sk_ASN1_VALUE_value(sk, i); |
136 | | |
137 | 2.02M | asn1_item_embed_free(&vtmp, ASN1_ITEM_ptr(tt->item), embed); |
138 | 2.02M | } |
139 | 1.94M | sk_ASN1_VALUE_free(sk); |
140 | 1.94M | *pval = NULL; |
141 | 2.02M | } else { |
142 | 2.02M | asn1_item_embed_free(pval, ASN1_ITEM_ptr(tt->item), embed); |
143 | 2.02M | } |
144 | 3.96M | } |
145 | | |
146 | | void asn1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed) |
147 | 1.92M | { |
148 | 1.92M | int utype; |
149 | | |
150 | | /* Special case: if 'it' is a primitive with a free_func, use that. */ |
151 | 1.92M | if (it) { |
152 | 1.91M | const ASN1_PRIMITIVE_FUNCS *pf = it->funcs; |
153 | | |
154 | 1.91M | if (embed) { |
155 | 127k | if (pf && pf->prim_clear) { |
156 | 693 | pf->prim_clear(pval, it); |
157 | 693 | return; |
158 | 693 | } |
159 | 1.79M | } else if (pf && pf->prim_free) { |
160 | 174 | pf->prim_free(pval, it); |
161 | 174 | return; |
162 | 174 | } |
163 | 1.91M | } |
164 | | |
165 | | /* Special case: if 'it' is NULL, free contents of ASN1_TYPE */ |
166 | 1.92M | if (!it) { |
167 | 7.87k | ASN1_TYPE *typ = (ASN1_TYPE *)*pval; |
168 | | |
169 | 7.87k | utype = typ->type; |
170 | 7.87k | pval = &typ->value.asn1_value; |
171 | 7.87k | if (!*pval) |
172 | 1.01k | return; |
173 | 1.91M | } else if (it->itype == ASN1_ITYPE_MSTRING) { |
174 | 547k | utype = -1; |
175 | 547k | if (!*pval) |
176 | 0 | return; |
177 | 1.36M | } else { |
178 | 1.36M | utype = it->utype; |
179 | 1.36M | if ((utype != V_ASN1_BOOLEAN) && !*pval) |
180 | 160k | return; |
181 | 1.36M | } |
182 | | |
183 | 1.76M | switch (utype) { |
184 | 680k | case V_ASN1_OBJECT: |
185 | 680k | ASN1_OBJECT_free((ASN1_OBJECT *)*pval); |
186 | 680k | break; |
187 | | |
188 | 299k | case V_ASN1_BOOLEAN: |
189 | 299k | if (it) |
190 | 299k | *(ASN1_BOOLEAN *)pval = it->size; |
191 | 24 | else |
192 | 24 | *(ASN1_BOOLEAN *)pval = -1; |
193 | 299k | return; |
194 | | |
195 | 586 | case V_ASN1_NULL: |
196 | 586 | break; |
197 | | |
198 | 7.87k | case V_ASN1_ANY: |
199 | 7.87k | asn1_primitive_free(pval, NULL, 0); |
200 | 7.87k | OPENSSL_free(*pval); |
201 | 7.87k | break; |
202 | | |
203 | 775k | default: |
204 | 775k | asn1_string_embed_free((ASN1_STRING *)*pval, embed); |
205 | 775k | break; |
206 | 1.76M | } |
207 | 1.46M | *pval = NULL; |
208 | 1.46M | } |