Coverage Report

Created: 2025-11-16 06:40

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
71.4M
{
20
71.4M
    ossl_asn1_item_embed_free(&val, it, 0);
21
71.4M
}
22
23
void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
24
16.3M
{
25
16.3M
    ossl_asn1_item_embed_free(pval, it, 0);
26
16.3M
}
27
28
void ossl_asn1_item_embed_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
29
373M
{
30
373M
    const ASN1_TEMPLATE *tt = NULL, *seqtt;
31
373M
    const ASN1_EXTERN_FUNCS *ef;
32
373M
    const ASN1_AUX *aux = it->funcs;
33
373M
    ASN1_aux_cb *asn1_cb;
34
373M
    int i;
35
36
373M
    if (pval == NULL)
37
0
        return;
38
373M
    if ((it->itype != ASN1_ITYPE_PRIMITIVE) && *pval == NULL)
39
30.7M
        return;
40
342M
    if (aux && aux->asn1_cb)
41
8.45M
        asn1_cb = aux->asn1_cb;
42
334M
    else
43
334M
        asn1_cb = 0;
44
45
342M
    switch (it->itype) {
46
47
222M
    case ASN1_ITYPE_PRIMITIVE:
48
222M
        if (it->templates)
49
42.1M
            ossl_asn1_template_free(pval, it->templates);
50
180M
        else
51
180M
            ossl_asn1_primitive_free(pval, it, embed);
52
222M
        break;
53
54
38.8M
    case ASN1_ITYPE_MSTRING:
55
38.8M
        ossl_asn1_primitive_free(pval, it, embed);
56
38.8M
        break;
57
58
7.45M
    case ASN1_ITYPE_CHOICE:
59
7.45M
        if (asn1_cb) {
60
451k
            i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
61
451k
            if (i == 2)
62
0
                return;
63
451k
        }
64
7.45M
        i = ossl_asn1_get_choice_selector(pval, it);
65
7.45M
        if ((i >= 0) && (i < it->tcount)) {
66
5.34M
            ASN1_VALUE **pchval;
67
68
5.34M
            tt = it->templates + i;
69
5.34M
            pchval = ossl_asn1_get_field_ptr(pval, tt);
70
5.34M
            ossl_asn1_template_free(pchval, tt);
71
5.34M
        }
72
7.45M
        if (asn1_cb)
73
451k
            asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
74
7.45M
        if (embed == 0) {
75
7.32M
            OPENSSL_free(*pval);
76
7.32M
            *pval = NULL;
77
7.32M
        }
78
7.45M
        break;
79
80
5.38M
    case ASN1_ITYPE_EXTERN:
81
5.38M
        ef = it->funcs;
82
5.38M
        if (ef && ef->asn1_ex_free)
83
5.38M
            ef->asn1_ex_free(pval, it);
84
5.38M
        break;
85
86
1.00M
    case ASN1_ITYPE_NDEF_SEQUENCE:
87
68.3M
    case ASN1_ITYPE_SEQUENCE:
88
68.3M
        if (ossl_asn1_do_lock(pval, -1, it) != 0) {
89
            /* if error or ref-counter > 0 */
90
437k
            OPENSSL_assert(embed == 0);
91
437k
            *pval = NULL;
92
437k
            return;
93
437k
        }
94
67.9M
        if (asn1_cb) {
95
4.78M
            i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
96
4.78M
            if (i == 2)
97
810k
                return;
98
4.78M
        }
99
67.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
67.0M
        tt = it->templates + it->tcount;
106
237M
        for (i = 0; i < it->tcount; i++) {
107
170M
            ASN1_VALUE **pseqval;
108
109
170M
            tt--;
110
170M
            seqtt = ossl_asn1_do_adb(*pval, tt, 0);
111
170M
            if (!seqtt)
112
489k
                continue;
113
169M
            pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
114
169M
            ossl_asn1_template_free(pseqval, seqtt);
115
169M
        }
116
67.0M
        if (asn1_cb)
117
3.97M
            asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
118
67.0M
        if (embed == 0) {
119
61.9M
            OPENSSL_free(*pval);
120
61.9M
            *pval = NULL;
121
61.9M
        }
122
67.0M
        break;
123
342M
    }
124
342M
}
125
126
void ossl_asn1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
127
238M
{
128
238M
    int embed = tt->flags & ASN1_TFLG_EMBED;
129
238M
    ASN1_VALUE *tval;
130
238M
    if (embed) {
131
13.6M
        tval = (ASN1_VALUE *)pval;
132
13.6M
        pval = &tval;
133
13.6M
    }
134
238M
    if (tt->flags & ASN1_TFLG_SK_MASK) {
135
54.7M
        STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
136
54.7M
        int i;
137
138
156M
        for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
139
101M
            ASN1_VALUE *vtmp = sk_ASN1_VALUE_value(sk, i);
140
141
101M
            ossl_asn1_item_embed_free(&vtmp, ASN1_ITEM_ptr(tt->item), embed);
142
101M
        }
143
54.7M
        sk_ASN1_VALUE_free(sk);
144
54.7M
        *pval = NULL;
145
183M
    } else {
146
183M
        ossl_asn1_item_embed_free(pval, ASN1_ITEM_ptr(tt->item), embed);
147
183M
    }
148
238M
}
149
150
void ossl_asn1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
151
280M
{
152
280M
    int utype;
153
154
    /* Special case: if 'it' is a primitive with a free_func, use that. */
155
280M
    if (it) {
156
219M
        const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
157
158
219M
        if (embed) {
159
8.43M
            if (pf && pf->prim_clear) {
160
1.48M
                pf->prim_clear(pval, it);
161
1.48M
                return;
162
1.48M
            }
163
211M
        } else if (pf && pf->prim_free) {
164
1.29M
            pf->prim_free(pval, it);
165
1.29M
            return;
166
1.29M
        }
167
219M
    }
168
169
    /* Special case: if 'it' is NULL, free contents of ASN1_TYPE */
170
277M
    if (!it) {
171
60.6M
        ASN1_TYPE *typ = (ASN1_TYPE *)*pval;
172
173
60.6M
        utype = typ->type;
174
60.6M
        pval = &typ->value.asn1_value;
175
60.6M
        if (*pval == NULL)
176
943k
            return;
177
216M
    } else if (it->itype == ASN1_ITYPE_MSTRING) {
178
38.8M
        utype = -1;
179
38.8M
        if (*pval == NULL)
180
0
            return;
181
177M
    } else {
182
177M
        utype = it->utype;
183
177M
        if ((utype != V_ASN1_BOOLEAN) && *pval == NULL)
184
29.9M
            return;
185
177M
    }
186
187
246M
    switch (utype) {
188
53.2M
    case V_ASN1_OBJECT:
189
53.2M
        ASN1_OBJECT_free((ASN1_OBJECT *)*pval);
190
53.2M
        break;
191
192
27.9M
    case V_ASN1_BOOLEAN:
193
27.9M
        if (it)
194
10.7M
            *(ASN1_BOOLEAN *)pval = it->size;
195
17.2M
        else
196
17.2M
            *(ASN1_BOOLEAN *)pval = -1;
197
27.9M
        return;
198
199
19.0k
    case V_ASN1_NULL:
200
19.0k
        break;
201
202
60.6M
    case V_ASN1_ANY:
203
60.6M
        ossl_asn1_primitive_free(pval, NULL, 0);
204
60.6M
        OPENSSL_free(*pval);
205
60.6M
        break;
206
207
104M
    default:
208
104M
        ossl_asn1_string_embed_free((ASN1_STRING *)*pval, embed);
209
104M
        break;
210
246M
    }
211
218M
    *pval = NULL;
212
218M
}