Coverage Report

Created: 2026-07-23 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl33/crypto/asn1/tasn_fre.c
Line
Count
Source
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
81.6M
{
20
81.6M
    ossl_asn1_item_embed_free(&val, it, 0);
21
81.6M
}
22
23
void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
24
14.3M
{
25
14.3M
    ossl_asn1_item_embed_free(pval, it, 0);
26
14.3M
}
27
28
void ossl_asn1_item_embed_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
29
399M
{
30
399M
    const ASN1_TEMPLATE *tt = NULL, *seqtt;
31
399M
    const ASN1_EXTERN_FUNCS *ef;
32
399M
    const ASN1_AUX *aux = it->funcs;
33
399M
    ASN1_aux_cb *asn1_cb;
34
399M
    int i;
35
36
399M
    if (pval == NULL)
37
0
        return;
38
399M
    if ((it->itype != ASN1_ITYPE_PRIMITIVE) && *pval == NULL)
39
34.1M
        return;
40
365M
    if (aux && aux->asn1_cb)
41
8.09M
        asn1_cb = aux->asn1_cb;
42
357M
    else
43
357M
        asn1_cb = 0;
44
45
365M
    switch (it->itype) {
46
47
233M
    case ASN1_ITYPE_PRIMITIVE:
48
233M
        if (it->templates)
49
55.3M
            ossl_asn1_template_free(pval, it->templates);
50
178M
        else
51
178M
            ossl_asn1_primitive_free(pval, it, embed);
52
233M
        break;
53
54
45.2M
    case ASN1_ITYPE_MSTRING:
55
45.2M
        ossl_asn1_primitive_free(pval, it, embed);
56
45.2M
        break;
57
58
5.58M
    case ASN1_ITYPE_CHOICE:
59
5.58M
        if (asn1_cb) {
60
390k
            i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
61
390k
            if (i == 2)
62
0
                return;
63
390k
        }
64
5.58M
        i = ossl_asn1_get_choice_selector(pval, it);
65
5.58M
        if ((i >= 0) && (i < it->tcount)) {
66
3.71M
            ASN1_VALUE **pchval;
67
68
3.71M
            tt = it->templates + i;
69
3.71M
            pchval = ossl_asn1_get_field_ptr(pval, tt);
70
3.71M
            ossl_asn1_template_free(pchval, tt);
71
3.71M
        }
72
5.58M
        if (asn1_cb)
73
390k
            asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
74
5.58M
        if (embed == 0) {
75
5.46M
            OPENSSL_free(*pval);
76
5.46M
            *pval = NULL;
77
5.46M
        }
78
5.58M
        break;
79
80
5.92M
    case ASN1_ITYPE_EXTERN:
81
5.92M
        ef = it->funcs;
82
5.92M
        if (ef && ef->asn1_ex_free)
83
5.92M
            ef->asn1_ex_free(pval, it);
84
5.92M
        break;
85
86
898k
    case ASN1_ITYPE_NDEF_SEQUENCE:
87
74.9M
    case ASN1_ITYPE_SEQUENCE:
88
74.9M
        if (ossl_asn1_do_lock(pval, -1, it) != 0) {
89
            /* if error or ref-counter > 0 */
90
480k
            OPENSSL_assert(embed == 0);
91
480k
            *pval = NULL;
92
480k
            return;
93
480k
        }
94
74.4M
        if (asn1_cb) {
95
4.72M
            i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
96
4.72M
            if (i == 2)
97
729k
                return;
98
4.72M
        }
99
73.7M
        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
73.7M
        tt = it->templates + it->tcount;
106
258M
        for (i = 0; i < it->tcount; i++) {
107
184M
            ASN1_VALUE **pseqval;
108
109
184M
            tt--;
110
184M
            seqtt = ossl_asn1_do_adb(*pval, tt, 0);
111
184M
            if (!seqtt)
112
444k
                continue;
113
184M
            pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
114
184M
            ossl_asn1_template_free(pseqval, seqtt);
115
184M
        }
116
73.7M
        if (asn1_cb)
117
4.00M
            asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
118
73.7M
        if (embed == 0) {
119
68.0M
            OPENSSL_free(*pval);
120
68.0M
            *pval = NULL;
121
68.0M
        }
122
73.7M
        break;
123
365M
    }
124
365M
}
125
126
void ossl_asn1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
127
266M
{
128
266M
    int embed = tt->flags & ASN1_TFLG_EMBED;
129
266M
    ASN1_VALUE *tval;
130
266M
    if (embed) {
131
14.3M
        tval = (ASN1_VALUE *)pval;
132
14.3M
        pval = &tval;
133
14.3M
    }
134
266M
    if (tt->flags & ASN1_TFLG_SK_MASK) {
135
68.1M
        STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
136
68.1M
        int i;
137
138
173M
        for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
139
105M
            ASN1_VALUE *vtmp = sk_ASN1_VALUE_value(sk, i);
140
141
105M
            ossl_asn1_item_embed_free(&vtmp, ASN1_ITEM_ptr(tt->item), embed);
142
105M
        }
143
68.1M
        sk_ASN1_VALUE_free(sk);
144
68.1M
        *pval = NULL;
145
198M
    } else {
146
198M
        ossl_asn1_item_embed_free(pval, ASN1_ITEM_ptr(tt->item), embed);
147
198M
    }
148
266M
}
149
150
void ossl_asn1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
151
278M
{
152
278M
    int utype;
153
154
    /* Special case: if 'it' is a primitive with a free_func, use that. */
155
278M
    if (it) {
156
223M
        const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
157
158
223M
        if (embed) {
159
8.56M
            if (pf && pf->prim_clear) {
160
1.32M
                pf->prim_clear(pval, it);
161
1.32M
                return;
162
1.32M
            }
163
215M
        } else if (pf && pf->prim_free) {
164
1.16M
            pf->prim_free(pval, it);
165
1.16M
            return;
166
1.16M
        }
167
223M
    }
168
169
    /* Special case: if 'it' is NULL, free contents of ASN1_TYPE */
170
276M
    if (!it) {
171
54.6M
        ASN1_TYPE *typ = (ASN1_TYPE *)*pval;
172
173
54.6M
        utype = typ->type;
174
54.6M
        pval = &typ->value.asn1_value;
175
54.6M
        if (*pval == NULL)
176
1.06M
            return;
177
221M
    } else if (it->itype == ASN1_ITYPE_MSTRING) {
178
45.2M
        utype = -1;
179
45.2M
        if (*pval == NULL)
180
0
            return;
181
176M
    } else {
182
176M
        utype = it->utype;
183
176M
        if ((utype != V_ASN1_BOOLEAN) && *pval == NULL)
184
31.3M
            return;
185
176M
    }
186
187
243M
    switch (utype) {
188
60.2M
    case V_ASN1_OBJECT:
189
60.2M
        ASN1_OBJECT_free((ASN1_OBJECT *)*pval);
190
60.2M
        break;
191
192
18.2M
    case V_ASN1_BOOLEAN:
193
18.2M
        if (it)
194
10.3M
            *(ASN1_BOOLEAN *)pval = it->size;
195
7.90M
        else
196
7.90M
            *(ASN1_BOOLEAN *)pval = -1;
197
18.2M
        return;
198
199
23.8k
    case V_ASN1_NULL:
200
23.8k
        break;
201
202
54.6M
    case V_ASN1_ANY:
203
54.6M
        ossl_asn1_primitive_free(pval, NULL, 0);
204
54.6M
        OPENSSL_free(*pval);
205
54.6M
        break;
206
207
110M
    default:
208
110M
        ossl_asn1_string_embed_free((ASN1_STRING *)*pval, embed);
209
110M
        break;
210
243M
    }
211
225M
    *pval = NULL;
212
225M
}