Coverage Report

Created: 2025-12-31 06:58

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
75.8M
{
20
75.8M
    ossl_asn1_item_embed_free(&val, it, 0);
21
75.8M
}
22
23
void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
24
16.9M
{
25
16.9M
    ossl_asn1_item_embed_free(pval, it, 0);
26
16.9M
}
27
28
void ossl_asn1_item_embed_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
29
394M
{
30
394M
    const ASN1_TEMPLATE *tt = NULL, *seqtt;
31
394M
    const ASN1_EXTERN_FUNCS *ef;
32
394M
    const ASN1_AUX *aux = it->funcs;
33
394M
    ASN1_aux_cb *asn1_cb;
34
394M
    int i;
35
36
394M
    if (pval == NULL)
37
0
        return;
38
394M
    if ((it->itype != ASN1_ITYPE_PRIMITIVE) && *pval == NULL)
39
33.0M
        return;
40
361M
    if (aux && aux->asn1_cb)
41
8.72M
        asn1_cb = aux->asn1_cb;
42
352M
    else
43
352M
        asn1_cb = 0;
44
45
361M
    switch (it->itype) {
46
47
234M
    case ASN1_ITYPE_PRIMITIVE:
48
234M
        if (it->templates)
49
49.6M
            ossl_asn1_template_free(pval, it->templates);
50
184M
        else
51
184M
            ossl_asn1_primitive_free(pval, it, embed);
52
234M
        break;
53
54
42.0M
    case ASN1_ITYPE_MSTRING:
55
42.0M
        ossl_asn1_primitive_free(pval, it, embed);
56
42.0M
        break;
57
58
6.88M
    case ASN1_ITYPE_CHOICE:
59
6.88M
        if (asn1_cb) {
60
474k
            i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
61
474k
            if (i == 2)
62
0
                return;
63
474k
        }
64
6.88M
        i = ossl_asn1_get_choice_selector(pval, it);
65
6.88M
        if ((i >= 0) && (i < it->tcount)) {
66
4.74M
            ASN1_VALUE **pchval;
67
68
4.74M
            tt = it->templates + i;
69
4.74M
            pchval = ossl_asn1_get_field_ptr(pval, tt);
70
4.74M
            ossl_asn1_template_free(pchval, tt);
71
4.74M
        }
72
6.88M
        if (asn1_cb)
73
474k
            asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
74
6.88M
        if (embed == 0) {
75
6.74M
            OPENSSL_free(*pval);
76
6.74M
            *pval = NULL;
77
6.74M
        }
78
6.88M
        break;
79
80
5.49M
    case ASN1_ITYPE_EXTERN:
81
5.49M
        ef = it->funcs;
82
5.49M
        if (ef && ef->asn1_ex_free)
83
5.49M
            ef->asn1_ex_free(pval, it);
84
5.49M
        break;
85
86
1.00M
    case ASN1_ITYPE_NDEF_SEQUENCE:
87
72.8M
    case ASN1_ITYPE_SEQUENCE:
88
72.8M
        if (ossl_asn1_do_lock(pval, -1, it) != 0) {
89
            /* if error or ref-counter > 0 */
90
485k
            OPENSSL_assert(embed == 0);
91
485k
            *pval = NULL;
92
485k
            return;
93
485k
        }
94
72.3M
        if (asn1_cb) {
95
5.00M
            i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
96
5.00M
            if (i == 2)
97
824k
                return;
98
5.00M
        }
99
71.5M
        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
71.5M
        tt = it->templates + it->tcount;
106
252M
        for (i = 0; i < it->tcount; i++) {
107
181M
            ASN1_VALUE **pseqval;
108
109
181M
            tt--;
110
181M
            seqtt = ossl_asn1_do_adb(*pval, tt, 0);
111
181M
            if (!seqtt)
112
496k
                continue;
113
180M
            pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
114
180M
            ossl_asn1_template_free(pseqval, seqtt);
115
180M
        }
116
71.5M
        if (asn1_cb)
117
4.17M
            asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
118
71.5M
        if (embed == 0) {
119
66.2M
            OPENSSL_free(*pval);
120
66.2M
            *pval = NULL;
121
66.2M
        }
122
71.5M
        break;
123
361M
    }
124
361M
}
125
126
void ossl_asn1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
127
258M
{
128
258M
    int embed = tt->flags & ASN1_TFLG_EMBED;
129
258M
    ASN1_VALUE *tval;
130
258M
    if (embed) {
131
14.3M
        tval = (ASN1_VALUE *)pval;
132
14.3M
        pval = &tval;
133
14.3M
    }
134
258M
    if (tt->flags & ASN1_TFLG_SK_MASK) {
135
63.0M
        STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
136
63.0M
        int i;
137
138
169M
        for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
139
106M
            ASN1_VALUE *vtmp = sk_ASN1_VALUE_value(sk, i);
140
141
106M
            ossl_asn1_item_embed_free(&vtmp, ASN1_ITEM_ptr(tt->item), embed);
142
106M
        }
143
63.0M
        sk_ASN1_VALUE_free(sk);
144
63.0M
        *pval = NULL;
145
195M
    } else {
146
195M
        ossl_asn1_item_embed_free(pval, ASN1_ITEM_ptr(tt->item), embed);
147
195M
    }
148
258M
}
149
150
void ossl_asn1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
151
284M
{
152
284M
    int utype;
153
154
    /* Special case: if 'it' is a primitive with a free_func, use that. */
155
284M
    if (it) {
156
226M
        const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
157
158
226M
        if (embed) {
159
8.90M
            if (pf && pf->prim_clear) {
160
1.48M
                pf->prim_clear(pval, it);
161
1.48M
                return;
162
1.48M
            }
163
217M
        } else if (pf && pf->prim_free) {
164
1.28M
            pf->prim_free(pval, it);
165
1.28M
            return;
166
1.28M
        }
167
226M
    }
168
169
    /* Special case: if 'it' is NULL, free contents of ASN1_TYPE */
170
282M
    if (!it) {
171
58.4M
        ASN1_TYPE *typ = (ASN1_TYPE *)*pval;
172
173
58.4M
        utype = typ->type;
174
58.4M
        pval = &typ->value.asn1_value;
175
58.4M
        if (*pval == NULL)
176
1.02M
            return;
177
223M
    } else if (it->itype == ASN1_ITYPE_MSTRING) {
178
42.0M
        utype = -1;
179
42.0M
        if (*pval == NULL)
180
0
            return;
181
181M
    } else {
182
181M
        utype = it->utype;
183
181M
        if ((utype != V_ASN1_BOOLEAN) && *pval == NULL)
184
31.3M
            return;
185
181M
    }
186
187
249M
    switch (utype) {
188
57.1M
    case V_ASN1_OBJECT:
189
57.1M
        ASN1_OBJECT_free((ASN1_OBJECT *)*pval);
190
57.1M
        break;
191
192
23.3M
    case V_ASN1_BOOLEAN:
193
23.3M
        if (it)
194
11.5M
            *(ASN1_BOOLEAN *)pval = it->size;
195
11.8M
        else
196
11.8M
            *(ASN1_BOOLEAN *)pval = -1;
197
23.3M
        return;
198
199
17.9k
    case V_ASN1_NULL:
200
17.9k
        break;
201
202
58.4M
    case V_ASN1_ANY:
203
58.4M
        ossl_asn1_primitive_free(pval, NULL, 0);
204
58.4M
        OPENSSL_free(*pval);
205
58.4M
        break;
206
207
110M
    default:
208
110M
        ossl_asn1_string_embed_free((ASN1_STRING *)*pval, embed);
209
110M
        break;
210
249M
    }
211
226M
    *pval = NULL;
212
226M
}