67
67
/* Free up an ASN1 structure */
69
69
void ASN1_item_free(ASN1_VALUE *val, const ASN1_ITEM *it)
71
71
asn1_item_combine_free(&val, it, 0);
74
74
void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
76
76
asn1_item_combine_free(pval, it, 0);
79
79
static void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int combine)
81
81
const ASN1_TEMPLATE *tt = NULL, *seqtt;
82
82
const ASN1_EXTERN_FUNCS *ef;
83
83
const ASN1_COMPAT_FUNCS *cf;
84
84
const ASN1_AUX *aux = it->funcs;
85
85
ASN1_aux_cb *asn1_cb;
88
if((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval) return;
89
if(aux && aux->asn1_cb) asn1_cb = aux->asn1_cb;
89
if ((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval)
91
if (aux && aux->asn1_cb)
92
asn1_cb = aux->asn1_cb;
94
99
case ASN1_ITYPE_PRIMITIVE:
95
if(it->templates) ASN1_template_free(pval, it->templates);
96
else ASN1_primitive_free(pval, it);
101
ASN1_template_free(pval, it->templates);
103
ASN1_primitive_free(pval, it);
99
106
case ASN1_ITYPE_MSTRING:
103
110
case ASN1_ITYPE_CHOICE:
105
113
i = asn1_cb(ASN1_OP_FREE_PRE, pval, it);
108
117
i = asn1_get_choice_selector(pval, it);
109
if(asn1_cb) asn1_cb(ASN1_OP_FREE_PRE, pval, it);
110
if((i >= 0) && (i < it->tcount)) {
119
asn1_cb(ASN1_OP_FREE_PRE, pval, it);
120
if ((i >= 0) && (i < it->tcount))
111
122
ASN1_VALUE **pchval;
112
123
tt = it->templates + i;
113
124
pchval = asn1_get_field_ptr(pval, tt);
114
125
ASN1_template_free(pchval, tt);
116
if(asn1_cb) asn1_cb(ASN1_OP_FREE_POST, pval, it);
128
asn1_cb(ASN1_OP_FREE_POST, pval, it);
118
131
OPENSSL_free(*pval);
123
136
case ASN1_ITYPE_COMPAT:
125
if(cf && cf->asn1_free) cf->asn1_free(*pval);
138
if (cf && cf->asn1_free)
139
cf->asn1_free(*pval);
128
142
case ASN1_ITYPE_EXTERN:
130
if(ef && ef->asn1_ex_free) ef->asn1_ex_free(pval, it);
144
if (ef && ef->asn1_ex_free)
145
ef->asn1_ex_free(pval, it);
148
case ASN1_ITYPE_NDEF_SEQUENCE:
133
149
case ASN1_ITYPE_SEQUENCE:
134
if(asn1_do_lock(pval, -1, it) > 0) return;
150
if (asn1_do_lock(pval, -1, it) > 0)
136
154
i = asn1_cb(ASN1_OP_FREE_PRE, pval, it);
139
158
asn1_enc_free(pval, it);
140
159
/* If we free up as normal we will invalidate any
141
160
* ANY DEFINED BY field and we wont be able to
143
162
* free up in reverse order.
145
164
tt = it->templates + it->tcount - 1;
146
for(i = 0; i < it->tcount; tt--, i++) {
165
for (i = 0; i < it->tcount; tt--, i++)
147
167
ASN1_VALUE **pseqval;
148
168
seqtt = asn1_do_adb(pval, tt, 0);
150
171
pseqval = asn1_get_field_ptr(pval, seqtt);
151
172
ASN1_template_free(pseqval, seqtt);
153
if(asn1_cb) asn1_cb(ASN1_OP_FREE_POST, pval, it);
175
asn1_cb(ASN1_OP_FREE_POST, pval, it);
155
178
OPENSSL_free(*pval);
162
185
void ASN1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
165
if(tt->flags & ASN1_TFLG_SK_MASK) {
188
if (tt->flags & ASN1_TFLG_SK_MASK)
166
190
STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
167
for(i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
191
for (i = 0; i < sk_ASN1_VALUE_num(sk); i++)
168
193
ASN1_VALUE *vtmp;
169
194
vtmp = sk_ASN1_VALUE_value(sk, i);
170
asn1_item_combine_free(&vtmp, ASN1_ITEM_ptr(tt->item), 0);
195
asn1_item_combine_free(&vtmp, ASN1_ITEM_ptr(tt->item),
172
198
sk_ASN1_VALUE_free(sk);
174
} else asn1_item_combine_free(pval, ASN1_ITEM_ptr(tt->item),
202
asn1_item_combine_free(pval, ASN1_ITEM_ptr(tt->item),
175
203
tt->flags & ASN1_TFLG_COMBINE);
178
206
void ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
182
211
const ASN1_PRIMITIVE_FUNCS *pf;
184
if(pf && pf->prim_free) {
213
if (pf && pf->prim_free)
185
215
pf->prim_free(pval, it);
189
219
/* Special case: if 'it' is NULL free contents of ASN1_TYPE */
191
222
ASN1_TYPE *typ = (ASN1_TYPE *)*pval;
192
223
utype = typ->type;
193
224
pval = (ASN1_VALUE **)&typ->value.ptr;
195
} else if(it->itype == ASN1_ITYPE_MSTRING) {
228
else if (it->itype == ASN1_ITYPE_MSTRING)
199
236
utype = it->utype;
200
if((utype != V_ASN1_BOOLEAN) && !*pval) return;
237
if ((utype != V_ASN1_BOOLEAN) && !*pval)
204
243
case V_ASN1_OBJECT:
205
244
ASN1_OBJECT_free((ASN1_OBJECT *)*pval);