summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/asn1/tasn_fre.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/asn1/tasn_fre.c')
-rw-r--r--src/lib/libcrypto/asn1/tasn_fre.c149
1 files changed, 93 insertions, 56 deletions
diff --git a/src/lib/libcrypto/asn1/tasn_fre.c b/src/lib/libcrypto/asn1/tasn_fre.c
index 2dd844159e..bb7c1e2af4 100644
--- a/src/lib/libcrypto/asn1/tasn_fre.c
+++ b/src/lib/libcrypto/asn1/tasn_fre.c
@@ -67,33 +67,40 @@ static void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int c
67/* Free up an ASN1 structure */ 67/* Free up an ASN1 structure */
68 68
69void ASN1_item_free(ASN1_VALUE *val, const ASN1_ITEM *it) 69void ASN1_item_free(ASN1_VALUE *val, const ASN1_ITEM *it)
70{ 70 {
71 asn1_item_combine_free(&val, it, 0); 71 asn1_item_combine_free(&val, it, 0);
72} 72 }
73 73
74void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it) 74void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
75{ 75 {
76 asn1_item_combine_free(pval, it, 0); 76 asn1_item_combine_free(pval, it, 0);
77} 77 }
78 78
79static void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int combine) 79static void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int combine)
80{ 80 {
81 const ASN1_TEMPLATE *tt = NULL, *seqtt; 81 const ASN1_TEMPLATE *tt = NULL, *seqtt;
82 const ASN1_EXTERN_FUNCS *ef; 82 const ASN1_EXTERN_FUNCS *ef;
83 const ASN1_COMPAT_FUNCS *cf; 83 const ASN1_COMPAT_FUNCS *cf;
84 const ASN1_AUX *aux = it->funcs; 84 const ASN1_AUX *aux = it->funcs;
85 ASN1_aux_cb *asn1_cb; 85 ASN1_aux_cb *asn1_cb;
86 int i; 86 int i;
87 if(!pval) return; 87 if (!pval)
88 if((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval) return; 88 return;
89 if(aux && aux->asn1_cb) asn1_cb = aux->asn1_cb; 89 if ((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval)
90 else asn1_cb = 0; 90 return;
91 if (aux && aux->asn1_cb)
92 asn1_cb = aux->asn1_cb;
93 else
94 asn1_cb = 0;
91 95
92 switch(it->itype) { 96 switch(it->itype)
97 {
93 98
94 case ASN1_ITYPE_PRIMITIVE: 99 case ASN1_ITYPE_PRIMITIVE:
95 if(it->templates) ASN1_template_free(pval, it->templates); 100 if (it->templates)
96 else ASN1_primitive_free(pval, it); 101 ASN1_template_free(pval, it->templates);
102 else
103 ASN1_primitive_free(pval, it);
97 break; 104 break;
98 105
99 case ASN1_ITYPE_MSTRING: 106 case ASN1_ITYPE_MSTRING:
@@ -101,41 +108,51 @@ static void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int c
101 break; 108 break;
102 109
103 case ASN1_ITYPE_CHOICE: 110 case ASN1_ITYPE_CHOICE:
104 if(asn1_cb) { 111 if (asn1_cb)
112 {
105 i = asn1_cb(ASN1_OP_FREE_PRE, pval, it); 113 i = asn1_cb(ASN1_OP_FREE_PRE, pval, it);
106 if(i == 2) return; 114 if (i == 2)
107 } 115 return;
116 }
108 i = asn1_get_choice_selector(pval, it); 117 i = asn1_get_choice_selector(pval, it);
109 if(asn1_cb) asn1_cb(ASN1_OP_FREE_PRE, pval, it); 118 if ((i >= 0) && (i < it->tcount))
110 if((i >= 0) && (i < it->tcount)) { 119 {
111 ASN1_VALUE **pchval; 120 ASN1_VALUE **pchval;
112 tt = it->templates + i; 121 tt = it->templates + i;
113 pchval = asn1_get_field_ptr(pval, tt); 122 pchval = asn1_get_field_ptr(pval, tt);
114 ASN1_template_free(pchval, tt); 123 ASN1_template_free(pchval, tt);
115 } 124 }
116 if(asn1_cb) asn1_cb(ASN1_OP_FREE_POST, pval, it); 125 if (asn1_cb)
117 if(!combine) { 126 asn1_cb(ASN1_OP_FREE_POST, pval, it);
127 if (!combine)
128 {
118 OPENSSL_free(*pval); 129 OPENSSL_free(*pval);
119 *pval = NULL; 130 *pval = NULL;
120 } 131 }
121 break; 132 break;
122 133
123 case ASN1_ITYPE_COMPAT: 134 case ASN1_ITYPE_COMPAT:
124 cf = it->funcs; 135 cf = it->funcs;
125 if(cf && cf->asn1_free) cf->asn1_free(*pval); 136 if (cf && cf->asn1_free)
137 cf->asn1_free(*pval);
126 break; 138 break;
127 139
128 case ASN1_ITYPE_EXTERN: 140 case ASN1_ITYPE_EXTERN:
129 ef = it->funcs; 141 ef = it->funcs;
130 if(ef && ef->asn1_ex_free) ef->asn1_ex_free(pval, it); 142 if (ef && ef->asn1_ex_free)
143 ef->asn1_ex_free(pval, it);
131 break; 144 break;
132 145
146 case ASN1_ITYPE_NDEF_SEQUENCE:
133 case ASN1_ITYPE_SEQUENCE: 147 case ASN1_ITYPE_SEQUENCE:
134 if(asn1_do_lock(pval, -1, it) > 0) return; 148 if (asn1_do_lock(pval, -1, it) > 0)
135 if(asn1_cb) { 149 return;
150 if (asn1_cb)
151 {
136 i = asn1_cb(ASN1_OP_FREE_PRE, pval, it); 152 i = asn1_cb(ASN1_OP_FREE_PRE, pval, it);
137 if(i == 2) return; 153 if (i == 2)
138 } 154 return;
155 }
139 asn1_enc_free(pval, it); 156 asn1_enc_free(pval, it);
140 /* If we free up as normal we will invalidate any 157 /* If we free up as normal we will invalidate any
141 * ANY DEFINED BY field and we wont be able to 158 * ANY DEFINED BY field and we wont be able to
@@ -143,64 +160,84 @@ static void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int c
143 * free up in reverse order. 160 * free up in reverse order.
144 */ 161 */
145 tt = it->templates + it->tcount - 1; 162 tt = it->templates + it->tcount - 1;
146 for(i = 0; i < it->tcount; tt--, i++) { 163 for (i = 0; i < it->tcount; tt--, i++)
164 {
147 ASN1_VALUE **pseqval; 165 ASN1_VALUE **pseqval;
148 seqtt = asn1_do_adb(pval, tt, 0); 166 seqtt = asn1_do_adb(pval, tt, 0);
149 if(!seqtt) continue; 167 if (!seqtt)
168 continue;
150 pseqval = asn1_get_field_ptr(pval, seqtt); 169 pseqval = asn1_get_field_ptr(pval, seqtt);
151 ASN1_template_free(pseqval, seqtt); 170 ASN1_template_free(pseqval, seqtt);
152 } 171 }
153 if(asn1_cb) asn1_cb(ASN1_OP_FREE_POST, pval, it); 172 if (asn1_cb)
154 if(!combine) { 173 asn1_cb(ASN1_OP_FREE_POST, pval, it);
174 if (!combine)
175 {
155 OPENSSL_free(*pval); 176 OPENSSL_free(*pval);
156 *pval = NULL; 177 *pval = NULL;
157 } 178 }
158 break; 179 break;
180 }
159 } 181 }
160}
161 182
162void ASN1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) 183void ASN1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
163{ 184 {
164 int i; 185 int i;
165 if(tt->flags & ASN1_TFLG_SK_MASK) { 186 if (tt->flags & ASN1_TFLG_SK_MASK)
187 {
166 STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval; 188 STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
167 for(i = 0; i < sk_ASN1_VALUE_num(sk); i++) { 189 for (i = 0; i < sk_ASN1_VALUE_num(sk); i++)
190 {
168 ASN1_VALUE *vtmp; 191 ASN1_VALUE *vtmp;
169 vtmp = sk_ASN1_VALUE_value(sk, i); 192 vtmp = sk_ASN1_VALUE_value(sk, i);
170 asn1_item_combine_free(&vtmp, ASN1_ITEM_ptr(tt->item), 0); 193 asn1_item_combine_free(&vtmp, ASN1_ITEM_ptr(tt->item),
171 } 194 0);
195 }
172 sk_ASN1_VALUE_free(sk); 196 sk_ASN1_VALUE_free(sk);
173 *pval = NULL; 197 *pval = NULL;
174 } else asn1_item_combine_free(pval, ASN1_ITEM_ptr(tt->item), 198 }
199 else
200 asn1_item_combine_free(pval, ASN1_ITEM_ptr(tt->item),
175 tt->flags & ASN1_TFLG_COMBINE); 201 tt->flags & ASN1_TFLG_COMBINE);
176} 202 }
177 203
178void ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it) 204void ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
179{ 205 {
180 int utype; 206 int utype;
181 if(it) { 207 if (it)
208 {
182 const ASN1_PRIMITIVE_FUNCS *pf; 209 const ASN1_PRIMITIVE_FUNCS *pf;
183 pf = it->funcs; 210 pf = it->funcs;
184 if(pf && pf->prim_free) { 211 if (pf && pf->prim_free)
212 {
185 pf->prim_free(pval, it); 213 pf->prim_free(pval, it);
186 return; 214 return;
215 }
187 } 216 }
188 }
189 /* Special case: if 'it' is NULL free contents of ASN1_TYPE */ 217 /* Special case: if 'it' is NULL free contents of ASN1_TYPE */
190 if(!it) { 218 if (!it)
219 {
191 ASN1_TYPE *typ = (ASN1_TYPE *)*pval; 220 ASN1_TYPE *typ = (ASN1_TYPE *)*pval;
192 utype = typ->type; 221 utype = typ->type;
193 pval = (ASN1_VALUE **)&typ->value.ptr; 222 pval = &typ->value.asn1_value;
194 if(!*pval) return; 223 if (!*pval)
195 } else if(it->itype == ASN1_ITYPE_MSTRING) { 224 return;
225 }
226 else if (it->itype == ASN1_ITYPE_MSTRING)
227 {
196 utype = -1; 228 utype = -1;
197 if(!*pval) return; 229 if (!*pval)
198 } else { 230 return;
231 }
232 else
233 {
199 utype = it->utype; 234 utype = it->utype;
200 if((utype != V_ASN1_BOOLEAN) && !*pval) return; 235 if ((utype != V_ASN1_BOOLEAN) && !*pval)
201 } 236 return;
237 }
202 238
203 switch(utype) { 239 switch(utype)
240 {
204 case V_ASN1_OBJECT: 241 case V_ASN1_OBJECT:
205 ASN1_OBJECT_free((ASN1_OBJECT *)*pval); 242 ASN1_OBJECT_free((ASN1_OBJECT *)*pval);
206 break; 243 break;
@@ -224,6 +261,6 @@ void ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
224 ASN1_STRING_free((ASN1_STRING *)*pval); 261 ASN1_STRING_free((ASN1_STRING *)*pval);
225 *pval = NULL; 262 *pval = NULL;
226 break; 263 break;
227 } 264 }
228 *pval = NULL; 265 *pval = NULL;
229} 266 }