Coverage Report

Created: 2026-04-01 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl34/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
79.2M
{
20
79.2M
    ossl_asn1_item_embed_free(&val, it, 0);
21
79.2M
}
22
23
void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
24
16.7M
{
25
16.7M
    ossl_asn1_item_embed_free(pval, it, 0);
26
16.7M
}
27
28
void ossl_asn1_item_embed_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
29
422M
{
30
422M
    const ASN1_TEMPLATE *tt = NULL, *seqtt;
31
422M
    const ASN1_EXTERN_FUNCS *ef;
32
422M
    const ASN1_AUX *aux = it->funcs;
33
422M
    ASN1_aux_cb *asn1_cb;
34
422M
    int i;
35
36
422M
    if (pval == NULL)
37
0
        return;
38
422M
    if ((it->itype != ASN1_ITYPE_PRIMITIVE) && *pval == NULL)
39
35.4M
        return;
40
387M
    if (aux && aux->asn1_cb)
41
8.89M
        asn1_cb = aux->asn1_cb;
42
378M
    else
43
378M
        asn1_cb = 0;
44
45
387M
    switch (it->itype) {
46
47
252M
    case ASN1_ITYPE_PRIMITIVE:
48
252M
        if (it->templates)
49
57.0M
            ossl_asn1_template_free(pval, it->templates);
50
195M
        else
51
195M
            ossl_asn1_primitive_free(pval, it, embed);
52
252M
        break;
53
54
44.4M
    case ASN1_ITYPE_MSTRING:
55
44.4M
        ossl_asn1_primitive_free(pval, it, embed);
56
44.4M
        break;
57
58
7.60M
    case ASN1_ITYPE_CHOICE:
59
7.60M
        if (asn1_cb) {
60
426k
            i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
61
426k
            if (i == 2)
62
0
                return;
63
426k
        }
64
7.60M
        i = ossl_asn1_get_choice_selector(pval, it);
65
7.60M
        if ((i >= 0) && (i < it->tcount)) {
66
5.48M
            ASN1_VALUE **pchval;
67
68
5.48M
            tt = it->templates + i;
69
5.48M
            pchval = ossl_asn1_get_field_ptr(pval, tt);
70
5.48M
            ossl_asn1_template_free(pchval, tt);
71
5.48M
        }
72
7.60M
        if (asn1_cb)
73
426k
            asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
74
7.60M
        if (embed == 0) {
75
7.46M
            OPENSSL_free(*pval);
76
7.46M
            *pval = NULL;
77
7.46M
        }
78
7.60M
        break;
79
80
5.98M
    case ASN1_ITYPE_EXTERN:
81
5.98M
        ef = it->funcs;
82
5.98M
        if (ef && ef->asn1_ex_free)
83
5.98M
            ef->asn1_ex_free(pval, it);
84
5.98M
        break;
85
86
1.04M
    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
487k
            OPENSSL_assert(embed == 0);
91
487k
            *pval = NULL;
92
487k
            return;
93
487k
        }
94
75.9M
        if (asn1_cb) {
95
5.16M
            i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
96
5.16M
            if (i == 2)
97
824k
                return;
98
5.16M
        }
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
504k
                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.33M
            asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
118
75.1M
        if (embed == 0) {
119
69.4M
            OPENSSL_free(*pval);
120
69.4M
            *pval = NULL;
121
69.4M
        }
122
75.1M
        break;
123
387M
    }
124
387M
}
125
126
void ossl_asn1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
127
277M
{
128
277M
    int embed = tt->flags & ASN1_TFLG_EMBED;
129
277M
    ASN1_VALUE *tval;
130
277M
    if (embed) {
131
14.9M
        tval = (ASN1_VALUE *)pval;
132
14.9M
        pval = &tval;
133
14.9M
    }
134
277M
    if (tt->flags & ASN1_TFLG_SK_MASK) {
135
70.8M
        STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
136
70.8M
        int i;
137
138
191M
        for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
139
120M
            ASN1_VALUE *vtmp = sk_ASN1_VALUE_value(sk, i);
140
141
120M
            ossl_asn1_item_embed_free(&vtmp, ASN1_ITEM_ptr(tt->item), embed);
142
120M
        }
143
70.8M
        sk_ASN1_VALUE_free(sk);
144
70.8M
        *pval = NULL;
145
206M
    } else {
146
206M
        ossl_asn1_item_embed_free(pval, ASN1_ITEM_ptr(tt->item), embed);
147
206M
    }
148
277M
}
149
150
void ossl_asn1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
151
304M
{
152
304M
    int utype;
153
154
    /* Special case: if 'it' is a primitive with a free_func, use that. */
155
304M
    if (it) {
156
239M
        const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
157
158
239M
        if (embed) {
159
9.08M
            if (pf && pf->prim_clear) {
160
1.49M
                pf->prim_clear(pval, it);
161
1.49M
                return;
162
1.49M
            }
163
230M
        } else if (pf && pf->prim_free) {
164
1.31M
            pf->prim_free(pval, it);
165
1.31M
            return;
166
1.31M
        }
167
239M
    }
168
169
    /* Special case: if 'it' is NULL, free contents of ASN1_TYPE */
170
301M
    if (!it) {
171
64.1M
        ASN1_TYPE *typ = (ASN1_TYPE *)*pval;
172
173
64.1M
        utype = typ->type;
174
64.1M
        pval = &typ->value.asn1_value;
175
64.1M
        if (*pval == NULL)
176
1.12M
            return;
177
237M
    } else if (it->itype == ASN1_ITYPE_MSTRING) {
178
44.4M
        utype = -1;
179
44.4M
        if (*pval == NULL)
180
0
            return;
181
192M
    } else {
182
192M
        utype = it->utype;
183
192M
        if ((utype != V_ASN1_BOOLEAN) && *pval == NULL)
184
33.4M
            return;
185
192M
    }
186
187
266M
    switch (utype) {
188
60.3M
    case V_ASN1_OBJECT:
189
60.3M
        ASN1_OBJECT_free((ASN1_OBJECT *)*pval);
190
60.3M
        break;
191
192
18.8M
    case V_ASN1_BOOLEAN:
193
18.8M
        if (it)
194
11.0M
            *(ASN1_BOOLEAN *)pval = it->size;
195
7.74M
        else
196
7.74M
            *(ASN1_BOOLEAN *)pval = -1;
197
18.8M
        return;
198
199
20.1k
    case V_ASN1_NULL:
200
20.1k
        break;
201
202
64.1M
    case V_ASN1_ANY:
203
64.1M
        ossl_asn1_primitive_free(pval, NULL, 0);
204
64.1M
        OPENSSL_free(*pval);
205
64.1M
        break;
206
207
123M
    default:
208
123M
        ossl_asn1_string_embed_free((ASN1_STRING *)*pval, embed);
209
123M
        break;
210
266M
    }
211
247M
    *pval = NULL;
212
247M
}