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