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