/src/openssl/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_locl.h" |
15 | | |
16 | | /* Free up an ASN1 structure */ |
17 | | |
18 | | void ASN1_item_free(ASN1_VALUE *val, const ASN1_ITEM *it) |
19 | 574k | { |
20 | 574k | asn1_item_embed_free(&val, it, 0); |
21 | 574k | } |
22 | | |
23 | | void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it) |
24 | 118k | { |
25 | 118k | asn1_item_embed_free(pval, it, 0); |
26 | 118k | } |
27 | | |
28 | | void asn1_item_embed_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed) |
29 | 2.32M | { |
30 | 2.32M | const ASN1_TEMPLATE *tt = NULL, *seqtt; |
31 | 2.32M | const ASN1_EXTERN_FUNCS *ef; |
32 | 2.32M | const ASN1_AUX *aux = it->funcs; |
33 | 2.32M | ASN1_aux_cb *asn1_cb; |
34 | 2.32M | int i; |
35 | 2.32M | |
36 | 2.32M | if (!pval) |
37 | 0 | return; |
38 | 2.32M | if ((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval) |
39 | 550k | return; |
40 | 1.77M | if (aux && aux->asn1_cb) |
41 | 127k | asn1_cb = aux->asn1_cb; |
42 | 1.64M | else |
43 | 1.64M | asn1_cb = 0; |
44 | 1.77M | |
45 | 1.77M | switch (it->itype) { |
46 | 1.77M | |
47 | 1.77M | case ASN1_ITYPE_PRIMITIVE: |
48 | 1.53M | if (it->templates) |
49 | 0 | asn1_template_free(pval, it->templates); |
50 | 1.53M | else |
51 | 1.53M | asn1_primitive_free(pval, it, embed); |
52 | 1.53M | break; |
53 | 1.77M | |
54 | 1.77M | case ASN1_ITYPE_MSTRING: |
55 | 0 | asn1_primitive_free(pval, it, embed); |
56 | 0 | break; |
57 | 1.77M | |
58 | 1.77M | case ASN1_ITYPE_CHOICE: |
59 | 0 | if (asn1_cb) { |
60 | 0 | i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL); |
61 | 0 | if (i == 2) |
62 | 0 | return; |
63 | 0 | } |
64 | 0 | i = asn1_get_choice_selector(pval, it); |
65 | 0 | if ((i >= 0) && (i < it->tcount)) { |
66 | 0 | ASN1_VALUE **pchval; |
67 | 0 |
|
68 | 0 | tt = it->templates + i; |
69 | 0 | pchval = asn1_get_field_ptr(pval, tt); |
70 | 0 | asn1_template_free(pchval, tt); |
71 | 0 | } |
72 | 0 | if (asn1_cb) |
73 | 0 | asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL); |
74 | 0 | if (embed == 0) { |
75 | 0 | OPENSSL_free(*pval); |
76 | 0 | *pval = NULL; |
77 | 0 | } |
78 | 0 | break; |
79 | 0 |
|
80 | 0 | case ASN1_ITYPE_EXTERN: |
81 | 0 | ef = it->funcs; |
82 | 0 | if (ef && ef->asn1_ex_free) |
83 | 0 | ef->asn1_ex_free(pval, it); |
84 | 0 | break; |
85 | 0 |
|
86 | 235k | case ASN1_ITYPE_NDEF_SEQUENCE: |
87 | 235k | case ASN1_ITYPE_SEQUENCE: |
88 | 235k | if (asn1_do_lock(pval, -1, it) != 0) /* if error or ref-counter > 0 */ |
89 | 0 | return; |
90 | 235k | if (asn1_cb) { |
91 | 121k | i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL); |
92 | 121k | if (i == 2) |
93 | 62.1k | return; |
94 | 173k | } |
95 | 173k | asn1_enc_free(pval, it); |
96 | 173k | /* |
97 | 173k | * If we free up as normal we will invalidate any ANY DEFINED BY |
98 | 173k | * field and we won't be able to determine the type of the field it |
99 | 173k | * defines. So free up in reverse order. |
100 | 173k | */ |
101 | 173k | tt = it->templates + it->tcount; |
102 | 641k | for (i = 0; i < it->tcount; i++) { |
103 | 468k | ASN1_VALUE **pseqval; |
104 | 468k | |
105 | 468k | tt--; |
106 | 468k | seqtt = asn1_do_adb(pval, tt, 0); |
107 | 468k | if (!seqtt) |
108 | 0 | continue; |
109 | 468k | pseqval = asn1_get_field_ptr(pval, seqtt); |
110 | 468k | asn1_template_free(pseqval, seqtt); |
111 | 468k | } |
112 | 173k | if (asn1_cb) |
113 | 59.7k | asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL); |
114 | 173k | if (embed == 0) { |
115 | 173k | OPENSSL_free(*pval); |
116 | 173k | *pval = NULL; |
117 | 173k | } |
118 | 173k | break; |
119 | 1.77M | } |
120 | 1.77M | } |
121 | | |
122 | | void asn1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) |
123 | 525k | { |
124 | 525k | int embed = tt->flags & ASN1_TFLG_EMBED; |
125 | 525k | ASN1_VALUE *tval; |
126 | 525k | if (embed) { |
127 | 0 | tval = (ASN1_VALUE *)pval; |
128 | 0 | pval = &tval; |
129 | 0 | } |
130 | 525k | if (tt->flags & ASN1_TFLG_SK_MASK) { |
131 | 151k | STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval; |
132 | 151k | int i; |
133 | 151k | |
134 | 1.40M | for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) { |
135 | 1.25M | ASN1_VALUE *vtmp = sk_ASN1_VALUE_value(sk, i); |
136 | 1.25M | |
137 | 1.25M | asn1_item_embed_free(&vtmp, ASN1_ITEM_ptr(tt->item), embed); |
138 | 1.25M | } |
139 | 151k | sk_ASN1_VALUE_free(sk); |
140 | 151k | *pval = NULL; |
141 | 374k | } else { |
142 | 374k | asn1_item_embed_free(pval, ASN1_ITEM_ptr(tt->item), embed); |
143 | 374k | } |
144 | 525k | } |
145 | | |
146 | | void asn1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed) |
147 | 2.75M | { |
148 | 2.75M | int utype; |
149 | 2.75M | |
150 | 2.75M | /* Special case: if 'it' is a primitive with a free_func, use that. */ |
151 | 2.75M | if (it) { |
152 | 1.53M | const ASN1_PRIMITIVE_FUNCS *pf = it->funcs; |
153 | 1.53M | |
154 | 1.53M | if (embed) { |
155 | 0 | if (pf && pf->prim_clear) { |
156 | 0 | pf->prim_clear(pval, it); |
157 | 0 | return; |
158 | 0 | } |
159 | 1.53M | } else if (pf && pf->prim_free) { |
160 | 5.93k | pf->prim_free(pval, it); |
161 | 5.93k | return; |
162 | 5.93k | } |
163 | 2.75M | } |
164 | 2.75M | |
165 | 2.75M | /* Special case: if 'it' is NULL, free contents of ASN1_TYPE */ |
166 | 2.75M | if (!it) { |
167 | 1.21M | ASN1_TYPE *typ = (ASN1_TYPE *)*pval; |
168 | 1.21M | |
169 | 1.21M | utype = typ->type; |
170 | 1.21M | pval = &typ->value.asn1_value; |
171 | 1.21M | if (!*pval) |
172 | 24.2k | return; |
173 | 1.53M | } else if (it->itype == ASN1_ITYPE_MSTRING) { |
174 | 0 | utype = -1; |
175 | 0 | if (!*pval) |
176 | 0 | return; |
177 | 1.53M | } else { |
178 | 1.53M | utype = it->utype; |
179 | 1.53M | if ((utype != V_ASN1_BOOLEAN) && !*pval) |
180 | 81.5k | return; |
181 | 2.64M | } |
182 | 2.64M | |
183 | 2.64M | switch (utype) { |
184 | 2.64M | case V_ASN1_OBJECT: |
185 | 166k | ASN1_OBJECT_free((ASN1_OBJECT *)*pval); |
186 | 166k | break; |
187 | 2.64M | |
188 | 2.64M | case V_ASN1_BOOLEAN: |
189 | 3.24k | if (it) |
190 | 0 | *(ASN1_BOOLEAN *)pval = it->size; |
191 | 3.24k | else |
192 | 3.24k | *(ASN1_BOOLEAN *)pval = -1; |
193 | 3.24k | return; |
194 | 2.64M | |
195 | 2.64M | case V_ASN1_NULL: |
196 | 0 | break; |
197 | 2.64M | |
198 | 2.64M | case V_ASN1_ANY: |
199 | 1.21M | asn1_primitive_free(pval, NULL, 0); |
200 | 1.21M | OPENSSL_free(*pval); |
201 | 1.21M | break; |
202 | 2.64M | |
203 | 2.64M | default: |
204 | 1.25M | asn1_string_embed_free((ASN1_STRING *)*pval, embed); |
205 | 1.25M | break; |
206 | 2.64M | } |
207 | 2.64M | *pval = NULL; |
208 | 2.64M | } |