Coverage Report

Created: 2023-09-25 06:41

/src/openssl30/crypto/asn1/tasn_fre.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2000-2021 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
395k
{
20
395k
    ossl_asn1_item_embed_free(&val, it, 0);
21
395k
}
22
23
void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
24
50.3k
{
25
50.3k
    ossl_asn1_item_embed_free(pval, it, 0);
26
50.3k
}
27
28
void ossl_asn1_item_embed_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
29
2.93M
{
30
2.93M
    const ASN1_TEMPLATE *tt = NULL, *seqtt;
31
2.93M
    const ASN1_EXTERN_FUNCS *ef;
32
2.93M
    const ASN1_AUX *aux = it->funcs;
33
2.93M
    ASN1_aux_cb *asn1_cb;
34
2.93M
    int i;
35
36
2.93M
    if (pval == NULL)
37
0
        return;
38
2.93M
    if ((it->itype != ASN1_ITYPE_PRIMITIVE) && *pval == NULL)
39
208k
        return;
40
2.72M
    if (aux && aux->asn1_cb)
41
16.6k
        asn1_cb = aux->asn1_cb;
42
2.71M
    else
43
2.71M
        asn1_cb = 0;
44
45
2.72M
    switch (it->itype) {
46
47
2.16M
    case ASN1_ITYPE_PRIMITIVE:
48
2.16M
        if (it->templates)
49
1.31M
            ossl_asn1_template_free(pval, it->templates);
50
857k
        else
51
857k
            ossl_asn1_primitive_free(pval, it, embed);
52
2.16M
        break;
53
54
134k
    case ASN1_ITYPE_MSTRING:
55
134k
        ossl_asn1_primitive_free(pval, it, embed);
56
134k
        break;
57
58
32.5k
    case ASN1_ITYPE_CHOICE:
59
32.5k
        if (asn1_cb) {
60
2.41k
            i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
61
2.41k
            if (i == 2)
62
0
                return;
63
2.41k
        }
64
32.5k
        i = ossl_asn1_get_choice_selector(pval, it);
65
32.5k
        if ((i >= 0) && (i < it->tcount)) {
66
18.8k
            ASN1_VALUE **pchval;
67
68
18.8k
            tt = it->templates + i;
69
18.8k
            pchval = ossl_asn1_get_field_ptr(pval, tt);
70
18.8k
            ossl_asn1_template_free(pchval, tt);
71
18.8k
        }
72
32.5k
        if (asn1_cb)
73
2.41k
            asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
74
32.5k
        if (embed == 0) {
75
32.5k
            OPENSSL_free(*pval);
76
32.5k
            *pval = NULL;
77
32.5k
        }
78
32.5k
        break;
79
80
42.8k
    case ASN1_ITYPE_EXTERN:
81
42.8k
        ef = it->funcs;
82
42.8k
        if (ef && ef->asn1_ex_free)
83
42.8k
            ef->asn1_ex_free(pval, it);
84
42.8k
        break;
85
86
0
    case ASN1_ITYPE_NDEF_SEQUENCE:
87
349k
    case ASN1_ITYPE_SEQUENCE:
88
349k
        if (ossl_asn1_do_lock(pval, -1, it) != 0) /* if error or ref-counter > 0 */
89
884
            return;
90
348k
        if (asn1_cb) {
91
11.7k
            i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
92
11.7k
            if (i == 2)
93
332
                return;
94
11.7k
        }
95
347k
        ossl_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
347k
        tt = it->templates + it->tcount;
102
1.29M
        for (i = 0; i < it->tcount; i++) {
103
942k
            ASN1_VALUE **pseqval;
104
105
942k
            tt--;
106
942k
            seqtt = ossl_asn1_do_adb(*pval, tt, 0);
107
942k
            if (!seqtt)
108
566
                continue;
109
941k
            pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
110
941k
            ossl_asn1_template_free(pseqval, seqtt);
111
941k
        }
112
347k
        if (asn1_cb)
113
11.4k
            asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
114
347k
        if (embed == 0) {
115
303k
            OPENSSL_free(*pval);
116
303k
            *pval = NULL;
117
303k
        }
118
347k
        break;
119
2.72M
    }
120
2.72M
}
121
122
void ossl_asn1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
123
2.48M
{
124
2.48M
    int embed = tt->flags & ASN1_TFLG_EMBED;
125
2.48M
    ASN1_VALUE *tval;
126
2.48M
    if (embed) {
127
169k
        tval = (ASN1_VALUE *)pval;
128
169k
        pval = &tval;
129
169k
    }
130
2.48M
    if (tt->flags & ASN1_TFLG_SK_MASK) {
131
1.37M
        STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
132
1.37M
        int i;
133
134
2.75M
        for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
135
1.37M
            ASN1_VALUE *vtmp = sk_ASN1_VALUE_value(sk, i);
136
137
1.37M
            ossl_asn1_item_embed_free(&vtmp, ASN1_ITEM_ptr(tt->item), embed);
138
1.37M
        }
139
1.37M
        sk_ASN1_VALUE_free(sk);
140
1.37M
        *pval = NULL;
141
1.37M
    } else {
142
1.11M
        ossl_asn1_item_embed_free(pval, ASN1_ITEM_ptr(tt->item), embed);
143
1.11M
    }
144
2.48M
}
145
146
void ossl_asn1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
147
1.00M
{
148
1.00M
    int utype;
149
150
    /* Special case: if 'it' is a primitive with a free_func, use that. */
151
1.00M
    if (it) {
152
992k
        const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
153
154
992k
        if (embed) {
155
125k
            if (pf && pf->prim_clear) {
156
1.54k
                pf->prim_clear(pval, it);
157
1.54k
                return;
158
1.54k
            }
159
866k
        } else if (pf && pf->prim_free) {
160
71
            pf->prim_free(pval, it);
161
71
            return;
162
71
        }
163
992k
    }
164
165
    /* Special case: if 'it' is NULL, free contents of ASN1_TYPE */
166
1.00M
    if (!it) {
167
14.9k
        ASN1_TYPE *typ = (ASN1_TYPE *)*pval;
168
169
14.9k
        utype = typ->type;
170
14.9k
        pval = &typ->value.asn1_value;
171
14.9k
        if (*pval == NULL)
172
2.55k
            return;
173
990k
    } else if (it->itype == ASN1_ITYPE_MSTRING) {
174
134k
        utype = -1;
175
134k
        if (*pval == NULL)
176
0
            return;
177
856k
    } else {
178
856k
        utype = it->utype;
179
856k
        if ((utype != V_ASN1_BOOLEAN) && *pval == NULL)
180
151k
            return;
181
856k
    }
182
183
851k
    switch (utype) {
184
266k
    case V_ASN1_OBJECT:
185
266k
        ASN1_OBJECT_free((ASN1_OBJECT *)*pval);
186
266k
        break;
187
188
246k
    case V_ASN1_BOOLEAN:
189
246k
        if (it)
190
246k
            *(ASN1_BOOLEAN *)pval = it->size;
191
17
        else
192
17
            *(ASN1_BOOLEAN *)pval = -1;
193
246k
        return;
194
195
770
    case V_ASN1_NULL:
196
770
        break;
197
198
14.9k
    case V_ASN1_ANY:
199
14.9k
        ossl_asn1_primitive_free(pval, NULL, 0);
200
14.9k
        OPENSSL_free(*pval);
201
14.9k
        break;
202
203
322k
    default:
204
322k
        ossl_asn1_string_embed_free((ASN1_STRING *)*pval, embed);
205
322k
        break;
206
851k
    }
207
605k
    *pval = NULL;
208
605k
}