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