/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 | 272k | { |
20 | 272k | asn1_item_embed_free(&val, it, 0); |
21 | 272k | } |
22 | | |
23 | | void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it) |
24 | 4.64k | { |
25 | 4.64k | asn1_item_embed_free(pval, it, 0); |
26 | 4.64k | } |
27 | | |
28 | | void asn1_item_embed_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed) |
29 | 506k | { |
30 | 506k | const ASN1_TEMPLATE *tt = NULL, *seqtt; |
31 | 506k | const ASN1_EXTERN_FUNCS *ef; |
32 | 506k | const ASN1_AUX *aux = it->funcs; |
33 | 506k | ASN1_aux_cb *asn1_cb; |
34 | 506k | int i; |
35 | | |
36 | 506k | if (!pval) |
37 | 0 | return; |
38 | 506k | if ((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval) |
39 | 213k | return; |
40 | 293k | if (aux && aux->asn1_cb) |
41 | 17.2k | asn1_cb = aux->asn1_cb; |
42 | 276k | else |
43 | 276k | asn1_cb = 0; |
44 | | |
45 | 293k | switch (it->itype) { |
46 | | |
47 | 161k | case ASN1_ITYPE_PRIMITIVE: |
48 | 161k | if (it->templates) |
49 | 20.3k | asn1_template_free(pval, it->templates); |
50 | 141k | else |
51 | 141k | asn1_primitive_free(pval, it, embed); |
52 | 161k | break; |
53 | | |
54 | 39.3k | case ASN1_ITYPE_MSTRING: |
55 | 39.3k | asn1_primitive_free(pval, it, embed); |
56 | 39.3k | break; |
57 | | |
58 | 995 | case ASN1_ITYPE_CHOICE: |
59 | 995 | if (asn1_cb) { |
60 | 67 | i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL); |
61 | 67 | if (i == 2) |
62 | 0 | return; |
63 | 67 | } |
64 | 995 | i = asn1_get_choice_selector(pval, it); |
65 | 995 | if ((i >= 0) && (i < it->tcount)) { |
66 | 221 | ASN1_VALUE **pchval; |
67 | | |
68 | 221 | tt = it->templates + i; |
69 | 221 | pchval = asn1_get_field_ptr(pval, tt); |
70 | 221 | asn1_template_free(pchval, tt); |
71 | 221 | } |
72 | 995 | if (asn1_cb) |
73 | 67 | asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL); |
74 | 995 | if (embed == 0) { |
75 | 995 | OPENSSL_free(*pval); |
76 | 995 | *pval = NULL; |
77 | 995 | } |
78 | 995 | break; |
79 | | |
80 | 9.23k | case ASN1_ITYPE_EXTERN: |
81 | 9.23k | ef = it->funcs; |
82 | 9.23k | if (ef && ef->asn1_ex_free) |
83 | 9.23k | ef->asn1_ex_free(pval, it); |
84 | 9.23k | break; |
85 | | |
86 | 0 | case ASN1_ITYPE_NDEF_SEQUENCE: |
87 | 82.0k | case ASN1_ITYPE_SEQUENCE: |
88 | 82.0k | if (asn1_do_lock(pval, -1, it) != 0) /* if error or ref-counter > 0 */ |
89 | 6.98k | return; |
90 | 75.0k | if (asn1_cb) { |
91 | 9.37k | i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL); |
92 | 9.37k | if (i == 2) |
93 | 477 | return; |
94 | 9.37k | } |
95 | 74.5k | 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 | 74.5k | tt = it->templates + it->tcount; |
102 | 276k | for (i = 0; i < it->tcount; i++) { |
103 | 202k | ASN1_VALUE **pseqval; |
104 | | |
105 | 202k | tt--; |
106 | 202k | seqtt = asn1_do_adb(pval, tt, 0); |
107 | 202k | if (!seqtt) |
108 | 406 | continue; |
109 | 201k | pseqval = asn1_get_field_ptr(pval, seqtt); |
110 | 201k | asn1_template_free(pseqval, seqtt); |
111 | 201k | } |
112 | 74.5k | if (asn1_cb) |
113 | 8.90k | asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL); |
114 | 74.5k | if (embed == 0) { |
115 | 56.8k | OPENSSL_free(*pval); |
116 | 56.8k | *pval = NULL; |
117 | 56.8k | } |
118 | 74.5k | break; |
119 | 293k | } |
120 | 293k | } |
121 | | |
122 | | void asn1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) |
123 | 243k | { |
124 | 243k | int embed = tt->flags & ASN1_TFLG_EMBED; |
125 | 243k | ASN1_VALUE *tval; |
126 | 243k | if (embed) { |
127 | 36.5k | tval = (ASN1_VALUE *)pval; |
128 | 36.5k | pval = &tval; |
129 | 36.5k | } |
130 | 243k | if (tt->flags & ASN1_TFLG_SK_MASK) { |
131 | 26.2k | STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval; |
132 | 26.2k | int i; |
133 | | |
134 | 37.9k | for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) { |
135 | 11.7k | ASN1_VALUE *vtmp = sk_ASN1_VALUE_value(sk, i); |
136 | | |
137 | 11.7k | asn1_item_embed_free(&vtmp, ASN1_ITEM_ptr(tt->item), embed); |
138 | 11.7k | } |
139 | 26.2k | sk_ASN1_VALUE_free(sk); |
140 | 26.2k | *pval = NULL; |
141 | 217k | } else { |
142 | 217k | asn1_item_embed_free(pval, ASN1_ITEM_ptr(tt->item), embed); |
143 | 217k | } |
144 | 243k | } |
145 | | |
146 | | void asn1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed) |
147 | 190k | { |
148 | 190k | int utype; |
149 | | |
150 | | /* Special case: if 'it' is a primitive with a free_func, use that. */ |
151 | 190k | if (it) { |
152 | 180k | const ASN1_PRIMITIVE_FUNCS *pf = it->funcs; |
153 | | |
154 | 180k | if (embed) { |
155 | 18.7k | if (pf && pf->prim_clear) { |
156 | 543 | pf->prim_clear(pval, it); |
157 | 543 | return; |
158 | 543 | } |
159 | 162k | } else if (pf && pf->prim_free) { |
160 | 265 | pf->prim_free(pval, it); |
161 | 265 | return; |
162 | 265 | } |
163 | 180k | } |
164 | | |
165 | | /* Special case: if 'it' is NULL, free contents of ASN1_TYPE */ |
166 | 189k | if (!it) { |
167 | 9.26k | ASN1_TYPE *typ = (ASN1_TYPE *)*pval; |
168 | | |
169 | 9.26k | utype = typ->type; |
170 | 9.26k | pval = &typ->value.asn1_value; |
171 | 9.26k | if (!*pval) |
172 | 5.41k | return; |
173 | 179k | } else if (it->itype == ASN1_ITYPE_MSTRING) { |
174 | 39.3k | utype = -1; |
175 | 39.3k | if (!*pval) |
176 | 0 | return; |
177 | 140k | } else { |
178 | 140k | utype = it->utype; |
179 | 140k | if ((utype != V_ASN1_BOOLEAN) && !*pval) |
180 | 28.6k | return; |
181 | 140k | } |
182 | | |
183 | 155k | switch (utype) { |
184 | 54.1k | case V_ASN1_OBJECT: |
185 | 54.1k | ASN1_OBJECT_free((ASN1_OBJECT *)*pval); |
186 | 54.1k | break; |
187 | | |
188 | 19.8k | case V_ASN1_BOOLEAN: |
189 | 19.8k | if (it) |
190 | 19.7k | *(ASN1_BOOLEAN *)pval = it->size; |
191 | 22 | else |
192 | 22 | *(ASN1_BOOLEAN *)pval = -1; |
193 | 19.8k | return; |
194 | | |
195 | 3 | case V_ASN1_NULL: |
196 | 3 | break; |
197 | | |
198 | 9.26k | case V_ASN1_ANY: |
199 | 9.26k | asn1_primitive_free(pval, NULL, 0); |
200 | 9.26k | OPENSSL_free(*pval); |
201 | 9.26k | break; |
202 | | |
203 | 71.9k | default: |
204 | 71.9k | asn1_string_embed_free((ASN1_STRING *)*pval, embed); |
205 | 71.9k | break; |
206 | 155k | } |
207 | 135k | *pval = NULL; |
208 | 135k | } |