Coverage Report

Created: 2023-06-08 06:40

/src/openssl111/crypto/asn1/tasn_fre.c
Line
Count
Source (jump to first uncovered line)
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_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
    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
    asn1_item_embed_free(pval, it, 0);
26
0
}
27
28
void 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)
37
0
        return;
38
0
    if ((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval)
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
            asn1_template_free(pval, it->templates);
50
0
        else
51
0
            asn1_primitive_free(pval, it, embed);
52
0
        break;
53
54
0
    case ASN1_ITYPE_MSTRING:
55
0
        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 = 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 = asn1_get_field_ptr(pval, tt);
70
0
            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 (asn1_do_lock(pval, -1, it) != 0) /* if error or ref-counter > 0 */
89
0
            return;
90
0
        if (asn1_cb) {
91
0
            i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
92
0
            if (i == 2)
93
0
                return;
94
0
        }
95
0
        asn1_enc_free(pval, it);
96
        /*
97
         * If we free up as normal we will invalidate any ANY DEFINED BY
98
         * field and we won't be able to determine the type of the field it
99
         * defines. So free up in reverse order.
100
         */
101
0
        tt = it->templates + it->tcount;
102
0
        for (i = 0; i < it->tcount; i++) {
103
0
            ASN1_VALUE **pseqval;
104
105
0
            tt--;
106
0
            seqtt = asn1_do_adb(pval, tt, 0);
107
0
            if (!seqtt)
108
0
                continue;
109
0
            pseqval = asn1_get_field_ptr(pval, seqtt);
110
0
            asn1_template_free(pseqval, seqtt);
111
0
        }
112
0
        if (asn1_cb)
113
0
            asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
114
0
        if (embed == 0) {
115
0
            OPENSSL_free(*pval);
116
0
            *pval = NULL;
117
0
        }
118
0
        break;
119
0
    }
120
0
}
121
122
void asn1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
123
0
{
124
0
    int embed = tt->flags & ASN1_TFLG_EMBED;
125
0
    ASN1_VALUE *tval;
126
0
    if (embed) {
127
0
        tval = (ASN1_VALUE *)pval;
128
0
        pval = &tval;
129
0
    }
130
0
    if (tt->flags & ASN1_TFLG_SK_MASK) {
131
0
        STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
132
0
        int i;
133
134
0
        for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
135
0
            ASN1_VALUE *vtmp = sk_ASN1_VALUE_value(sk, i);
136
137
0
            asn1_item_embed_free(&vtmp, ASN1_ITEM_ptr(tt->item), embed);
138
0
        }
139
0
        sk_ASN1_VALUE_free(sk);
140
0
        *pval = NULL;
141
0
    } else {
142
0
        asn1_item_embed_free(pval, ASN1_ITEM_ptr(tt->item), embed);
143
0
    }
144
0
}
145
146
void asn1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
147
0
{
148
0
    int utype;
149
150
    /* Special case: if 'it' is a primitive with a free_func, use that. */
151
0
    if (it) {
152
0
        const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
153
154
0
        if (embed) {
155
0
            if (pf && pf->prim_clear) {
156
0
                pf->prim_clear(pval, it);
157
0
                return;
158
0
            }
159
0
        } else if (pf && pf->prim_free) {
160
0
            pf->prim_free(pval, it);
161
0
            return;
162
0
        }
163
0
    }
164
165
    /* Special case: if 'it' is NULL, free contents of ASN1_TYPE */
166
0
    if (!it) {
167
0
        ASN1_TYPE *typ = (ASN1_TYPE *)*pval;
168
169
0
        utype = typ->type;
170
0
        pval = &typ->value.asn1_value;
171
0
        if (!*pval)
172
0
            return;
173
0
    } else if (it->itype == ASN1_ITYPE_MSTRING) {
174
0
        utype = -1;
175
0
        if (!*pval)
176
0
            return;
177
0
    } else {
178
0
        utype = it->utype;
179
0
        if ((utype != V_ASN1_BOOLEAN) && !*pval)
180
0
            return;
181
0
    }
182
183
0
    switch (utype) {
184
0
    case V_ASN1_OBJECT:
185
0
        ASN1_OBJECT_free((ASN1_OBJECT *)*pval);
186
0
        break;
187
188
0
    case V_ASN1_BOOLEAN:
189
0
        if (it)
190
0
            *(ASN1_BOOLEAN *)pval = it->size;
191
0
        else
192
0
            *(ASN1_BOOLEAN *)pval = -1;
193
0
        return;
194
195
0
    case V_ASN1_NULL:
196
0
        break;
197
198
0
    case V_ASN1_ANY:
199
0
        asn1_primitive_free(pval, NULL, 0);
200
0
        OPENSSL_free(*pval);
201
0
        break;
202
203
0
    default:
204
0
        asn1_string_embed_free((ASN1_STRING *)*pval, embed);
205
0
        break;
206
0
    }
207
0
    *pval = NULL;
208
0
}