Coverage Report

Created: 2025-08-28 07:07

/src/openssl35/crypto/asn1/tasn_fre.c
Line
Count
Source (jump to first uncovered line)
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
66.8M
{
20
66.8M
    ossl_asn1_item_embed_free(&val, it, 0);
21
66.8M
}
22
23
void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
24
15.7M
{
25
15.7M
    ossl_asn1_item_embed_free(pval, it, 0);
26
15.7M
}
27
28
void ossl_asn1_item_embed_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
29
352M
{
30
352M
    const ASN1_TEMPLATE *tt = NULL, *seqtt;
31
352M
    const ASN1_EXTERN_FUNCS *ef;
32
352M
    const ASN1_AUX *aux = it->funcs;
33
352M
    ASN1_aux_cb *asn1_cb;
34
352M
    int i;
35
36
352M
    if (pval == NULL)
37
0
        return;
38
352M
    if ((it->itype != ASN1_ITYPE_PRIMITIVE) && *pval == NULL)
39
28.6M
        return;
40
324M
    if (aux && aux->asn1_cb)
41
7.85M
        asn1_cb = aux->asn1_cb;
42
316M
    else
43
316M
        asn1_cb = 0;
44
45
324M
    switch (it->itype) {
46
47
211M
    case ASN1_ITYPE_PRIMITIVE:
48
211M
        if (it->templates)
49
42.2M
            ossl_asn1_template_free(pval, it->templates);
50
169M
        else
51
169M
            ossl_asn1_primitive_free(pval, it, embed);
52
211M
        break;
53
54
37.1M
    case ASN1_ITYPE_MSTRING:
55
37.1M
        ossl_asn1_primitive_free(pval, it, embed);
56
37.1M
        break;
57
58
6.74M
    case ASN1_ITYPE_CHOICE:
59
6.74M
        if (asn1_cb) {
60
407k
            i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
61
407k
            if (i == 2)
62
0
                return;
63
407k
        }
64
6.74M
        i = ossl_asn1_get_choice_selector(pval, it);
65
6.74M
        if ((i >= 0) && (i < it->tcount)) {
66
4.80M
            ASN1_VALUE **pchval;
67
68
4.80M
            tt = it->templates + i;
69
4.80M
            pchval = ossl_asn1_get_field_ptr(pval, tt);
70
4.80M
            ossl_asn1_template_free(pchval, tt);
71
4.80M
        }
72
6.74M
        if (asn1_cb)
73
407k
            asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
74
6.74M
        if (embed == 0) {
75
6.62M
            OPENSSL_free(*pval);
76
6.62M
            *pval = NULL;
77
6.62M
        }
78
6.74M
        break;
79
80
4.77M
    case ASN1_ITYPE_EXTERN:
81
4.77M
        ef = it->funcs;
82
4.77M
        if (ef && ef->asn1_ex_free)
83
4.77M
            ef->asn1_ex_free(pval, it);
84
4.77M
        break;
85
86
930k
    case ASN1_ITYPE_NDEF_SEQUENCE:
87
64.2M
    case ASN1_ITYPE_SEQUENCE:
88
64.2M
        if (ossl_asn1_do_lock(pval, -1, it) != 0) {
89
            /* if error or ref-counter > 0 */
90
418k
            OPENSSL_assert(embed == 0);
91
418k
            *pval = NULL;
92
418k
            return;
93
418k
        }
94
63.7M
        if (asn1_cb) {
95
4.42M
            i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
96
4.42M
            if (i == 2)
97
762k
                return;
98
4.42M
        }
99
63.0M
        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
63.0M
        tt = it->templates + it->tcount;
106
221M
        for (i = 0; i < it->tcount; i++) {
107
158M
            ASN1_VALUE **pseqval;
108
109
158M
            tt--;
110
158M
            seqtt = ossl_asn1_do_adb(*pval, tt, 0);
111
158M
            if (!seqtt)
112
456k
                continue;
113
158M
            pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
114
158M
            ossl_asn1_template_free(pseqval, seqtt);
115
158M
        }
116
63.0M
        if (asn1_cb)
117
3.66M
            asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
118
63.0M
        if (embed == 0) {
119
58.4M
            OPENSSL_free(*pval);
120
58.4M
            *pval = NULL;
121
58.4M
        }
122
63.0M
        break;
123
324M
    }
124
324M
}
125
126
void ossl_asn1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
127
225M
{
128
225M
    int embed = tt->flags & ASN1_TFLG_EMBED;
129
225M
    ASN1_VALUE *tval;
130
225M
    if (embed) {
131
12.1M
        tval = (ASN1_VALUE *)pval;
132
12.1M
        pval = &tval;
133
12.1M
    }
134
225M
    if (tt->flags & ASN1_TFLG_SK_MASK) {
135
53.8M
        STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
136
53.8M
        int i;
137
138
152M
        for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
139
99.1M
            ASN1_VALUE *vtmp = sk_ASN1_VALUE_value(sk, i);
140
141
99.1M
            ossl_asn1_item_embed_free(&vtmp, ASN1_ITEM_ptr(tt->item), embed);
142
99.1M
        }
143
53.8M
        sk_ASN1_VALUE_free(sk);
144
53.8M
        *pval = NULL;
145
171M
    } else {
146
171M
        ossl_asn1_item_embed_free(pval, ASN1_ITEM_ptr(tt->item), embed);
147
171M
    }
148
225M
}
149
150
void ossl_asn1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
151
264M
{
152
264M
    int utype;
153
154
    /* Special case: if 'it' is a primitive with a free_func, use that. */
155
264M
    if (it) {
156
206M
        const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
157
158
206M
        if (embed) {
159
7.45M
            if (pf && pf->prim_clear) {
160
1.38M
                pf->prim_clear(pval, it);
161
1.38M
                return;
162
1.38M
            }
163
198M
        } else if (pf && pf->prim_free) {
164
1.21M
            pf->prim_free(pval, it);
165
1.21M
            return;
166
1.21M
        }
167
206M
    }
168
169
    /* Special case: if 'it' is NULL, free contents of ASN1_TYPE */
170
262M
    if (!it) {
171
58.8M
        ASN1_TYPE *typ = (ASN1_TYPE *)*pval;
172
173
58.8M
        utype = typ->type;
174
58.8M
        pval = &typ->value.asn1_value;
175
58.8M
        if (*pval == NULL)
176
883k
            return;
177
203M
    } else if (it->itype == ASN1_ITYPE_MSTRING) {
178
37.1M
        utype = -1;
179
37.1M
        if (*pval == NULL)
180
0
            return;
181
166M
    } else {
182
166M
        utype = it->utype;
183
166M
        if ((utype != V_ASN1_BOOLEAN) && *pval == NULL)
184
27.3M
            return;
185
166M
    }
186
187
234M
    switch (utype) {
188
49.8M
    case V_ASN1_OBJECT:
189
49.8M
        ASN1_OBJECT_free((ASN1_OBJECT *)*pval);
190
49.8M
        break;
191
192
27.5M
    case V_ASN1_BOOLEAN:
193
27.5M
        if (it)
194
9.43M
            *(ASN1_BOOLEAN *)pval = it->size;
195
18.1M
        else
196
18.1M
            *(ASN1_BOOLEAN *)pval = -1;
197
27.5M
        return;
198
199
13.7k
    case V_ASN1_NULL:
200
13.7k
        break;
201
202
58.8M
    case V_ASN1_ANY:
203
58.8M
        ossl_asn1_primitive_free(pval, NULL, 0);
204
58.8M
        OPENSSL_free(*pval);
205
58.8M
        break;
206
207
97.9M
    default:
208
97.9M
        ossl_asn1_string_embed_free((ASN1_STRING *)*pval, embed);
209
97.9M
        break;
210
234M
    }
211
206M
    *pval = NULL;
212
206M
}