Coverage Report

Created: 2026-04-09 06:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl36/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
78.7M
{
20
78.7M
    ossl_asn1_item_embed_free(&val, it, 0);
21
78.7M
}
22
23
void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
24
16.4M
{
25
16.4M
    ossl_asn1_item_embed_free(pval, it, 0);
26
16.4M
}
27
28
void ossl_asn1_item_embed_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
29
409M
{
30
409M
    const ASN1_TEMPLATE *tt = NULL, *seqtt;
31
409M
    const ASN1_EXTERN_FUNCS *ef;
32
409M
    const ASN1_AUX *aux = it->funcs;
33
409M
    ASN1_aux_cb *asn1_cb;
34
409M
    int i;
35
36
409M
    if (pval == NULL)
37
0
        return;
38
409M
    if ((it->itype != ASN1_ITYPE_PRIMITIVE) && *pval == NULL)
39
35.7M
        return;
40
373M
    if (aux && aux->asn1_cb)
41
8.84M
        asn1_cb = aux->asn1_cb;
42
365M
    else
43
365M
        asn1_cb = 0;
44
45
373M
    switch (it->itype) {
46
47
240M
    case ASN1_ITYPE_PRIMITIVE:
48
240M
        if (it->templates)
49
55.3M
            ossl_asn1_template_free(pval, it->templates);
50
185M
        else
51
185M
            ossl_asn1_primitive_free(pval, it, embed);
52
240M
        break;
53
54
44.3M
    case ASN1_ITYPE_MSTRING:
55
44.3M
        ossl_asn1_primitive_free(pval, it, embed);
56
44.3M
        break;
57
58
6.63M
    case ASN1_ITYPE_CHOICE:
59
6.63M
        if (asn1_cb) {
60
421k
            i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
61
421k
            if (i == 2)
62
0
                return;
63
421k
        }
64
6.63M
        i = ossl_asn1_get_choice_selector(pval, it);
65
6.63M
        if ((i >= 0) && (i < it->tcount)) {
66
4.53M
            ASN1_VALUE **pchval;
67
68
4.53M
            tt = it->templates + i;
69
4.53M
            pchval = ossl_asn1_get_field_ptr(pval, tt);
70
4.53M
            ossl_asn1_template_free(pchval, tt);
71
4.53M
        }
72
6.63M
        if (asn1_cb)
73
421k
            asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
74
6.63M
        if (embed == 0) {
75
6.49M
            OPENSSL_free(*pval);
76
6.49M
            *pval = NULL;
77
6.49M
        }
78
6.63M
        break;
79
80
6.07M
    case ASN1_ITYPE_EXTERN:
81
6.07M
        ef = it->funcs;
82
6.07M
        if (ef && ef->asn1_ex_free)
83
6.07M
            ef->asn1_ex_free(pval, it);
84
6.07M
        break;
85
86
1.03M
    case ASN1_ITYPE_NDEF_SEQUENCE:
87
76.4M
    case ASN1_ITYPE_SEQUENCE:
88
76.4M
        if (ossl_asn1_do_lock(pval, -1, it) != 0) {
89
            /* if error or ref-counter > 0 */
90
491k
            OPENSSL_assert(embed == 0);
91
491k
            *pval = NULL;
92
491k
            return;
93
491k
        }
94
75.9M
        if (asn1_cb) {
95
5.13M
            i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
96
5.13M
            if (i == 2)
97
811k
                return;
98
5.13M
        }
99
75.1M
        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
75.1M
        tt = it->templates + it->tcount;
106
265M
        for (i = 0; i < it->tcount; i++) {
107
190M
            ASN1_VALUE **pseqval;
108
109
190M
            tt--;
110
190M
            seqtt = ossl_asn1_do_adb(*pval, tt, 0);
111
190M
            if (!seqtt)
112
499k
                continue;
113
189M
            pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
114
189M
            ossl_asn1_template_free(pseqval, seqtt);
115
189M
        }
116
75.1M
        if (asn1_cb)
117
4.32M
            asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
118
75.1M
        if (embed == 0) {
119
69.3M
            OPENSSL_free(*pval);
120
69.3M
            *pval = NULL;
121
69.3M
        }
122
75.1M
        break;
123
373M
    }
124
373M
}
125
126
void ossl_asn1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
127
275M
{
128
275M
    int embed = tt->flags & ASN1_TFLG_EMBED;
129
275M
    ASN1_VALUE *tval;
130
275M
    if (embed) {
131
15.0M
        tval = (ASN1_VALUE *)pval;
132
15.0M
        pval = &tval;
133
15.0M
    }
134
275M
    if (tt->flags & ASN1_TFLG_SK_MASK) {
135
69.1M
        STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
136
69.1M
        int i;
137
138
177M
        for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
139
108M
            ASN1_VALUE *vtmp = sk_ASN1_VALUE_value(sk, i);
140
141
108M
            ossl_asn1_item_embed_free(&vtmp, ASN1_ITEM_ptr(tt->item), embed);
142
108M
        }
143
69.1M
        sk_ASN1_VALUE_free(sk);
144
69.1M
        *pval = NULL;
145
205M
    } else {
146
205M
        ossl_asn1_item_embed_free(pval, ASN1_ITEM_ptr(tt->item), embed);
147
205M
    }
148
275M
}
149
150
void ossl_asn1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
151
283M
{
152
283M
    int utype;
153
154
    /* Special case: if 'it' is a primitive with a free_func, use that. */
155
283M
    if (it) {
156
229M
        const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
157
158
229M
        if (embed) {
159
9.06M
            if (pf && pf->prim_clear) {
160
1.48M
                pf->prim_clear(pval, it);
161
1.48M
                return;
162
1.48M
            }
163
220M
        } else if (pf && pf->prim_free) {
164
1.30M
            pf->prim_free(pval, it);
165
1.30M
            return;
166
1.30M
        }
167
229M
    }
168
169
    /* Special case: if 'it' is NULL, free contents of ASN1_TYPE */
170
280M
    if (!it) {
171
54.1M
        ASN1_TYPE *typ = (ASN1_TYPE *)*pval;
172
173
54.1M
        utype = typ->type;
174
54.1M
        pval = &typ->value.asn1_value;
175
54.1M
        if (*pval == NULL)
176
1.12M
            return;
177
226M
    } else if (it->itype == ASN1_ITYPE_MSTRING) {
178
44.3M
        utype = -1;
179
44.3M
        if (*pval == NULL)
180
0
            return;
181
182M
    } else {
182
182M
        utype = it->utype;
183
182M
        if ((utype != V_ASN1_BOOLEAN) && *pval == NULL)
184
33.7M
            return;
185
182M
    }
186
187
245M
    switch (utype) {
188
60.3M
    case V_ASN1_OBJECT:
189
60.3M
        ASN1_OBJECT_free((ASN1_OBJECT *)*pval);
190
60.3M
        break;
191
192
14.2M
    case V_ASN1_BOOLEAN:
193
14.2M
        if (it)
194
11.0M
            *(ASN1_BOOLEAN *)pval = it->size;
195
3.21M
        else
196
3.21M
            *(ASN1_BOOLEAN *)pval = -1;
197
14.2M
        return;
198
199
20.3k
    case V_ASN1_NULL:
200
20.3k
        break;
201
202
54.1M
    case V_ASN1_ANY:
203
54.1M
        ossl_asn1_primitive_free(pval, NULL, 0);
204
54.1M
        OPENSSL_free(*pval);
205
54.1M
        break;
206
207
117M
    default:
208
117M
        ossl_asn1_string_embed_free((ASN1_STRING *)*pval, embed);
209
117M
        break;
210
245M
    }
211
231M
    *pval = NULL;
212
231M
}