Coverage Report

Created: 2026-05-24 07:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/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
82.3M
{
20
82.3M
    ossl_asn1_item_embed_free(&val, it, 0);
21
82.3M
}
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
420M
{
30
420M
    const ASN1_TEMPLATE *tt = NULL, *seqtt;
31
420M
    const ASN1_EXTERN_FUNCS *ef;
32
420M
    const ASN1_AUX *aux = it->funcs;
33
420M
    ASN1_aux_cb *asn1_cb;
34
420M
    int i;
35
36
420M
    if (pval == NULL)
37
0
        return;
38
420M
    if ((it->itype != ASN1_ITYPE_PRIMITIVE) && *pval == NULL)
39
35.6M
        return;
40
385M
    if (aux && aux->asn1_cb)
41
9.15M
        asn1_cb = aux->asn1_cb;
42
375M
    else
43
375M
        asn1_cb = 0;
44
45
385M
    switch (it->itype) {
46
47
250M
    case ASN1_ITYPE_PRIMITIVE:
48
250M
        if (it->templates)
49
58.2M
            ossl_asn1_template_free(pval, it->templates);
50
191M
        else
51
191M
            ossl_asn1_primitive_free(pval, it, embed);
52
250M
        break;
53
54
44.5M
    case ASN1_ITYPE_MSTRING:
55
44.5M
        ossl_asn1_primitive_free(pval, it, embed);
56
44.5M
        break;
57
58
6.71M
    case ASN1_ITYPE_CHOICE:
59
6.71M
        if (asn1_cb) {
60
431k
            i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
61
431k
            if (i == 2)
62
0
                return;
63
431k
        }
64
6.71M
        i = ossl_asn1_get_choice_selector(pval, it);
65
6.71M
        if ((i >= 0) && (i < it->tcount)) {
66
4.58M
            ASN1_VALUE **pchval;
67
68
4.58M
            tt = it->templates + i;
69
4.58M
            pchval = ossl_asn1_get_field_ptr(pval, tt);
70
4.58M
            ossl_asn1_template_free(pchval, tt);
71
4.58M
        }
72
6.71M
        if (asn1_cb)
73
431k
            asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
74
6.71M
        if (embed == 0) {
75
6.57M
            OPENSSL_free(*pval);
76
6.57M
            *pval = NULL;
77
6.57M
        }
78
6.71M
        break;
79
80
6.46M
    case ASN1_ITYPE_EXTERN:
81
6.46M
        ef = it->funcs;
82
6.46M
        if (ef && ef->asn1_ex_free)
83
6.46M
            ef->asn1_ex_free(pval, it);
84
6.46M
        break;
85
86
1.04M
    case ASN1_ITYPE_NDEF_SEQUENCE:
87
77.2M
    case ASN1_ITYPE_SEQUENCE:
88
77.2M
        if (ossl_asn1_do_lock(pval, -1, it) != 0) {
89
            /* if error or ref-counter > 0 */
90
510k
            OPENSSL_assert(embed == 0);
91
510k
            *pval = NULL;
92
510k
            return;
93
510k
        }
94
76.7M
        if (asn1_cb) {
95
5.34M
            i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
96
5.34M
            if (i == 2)
97
834k
                return;
98
5.34M
        }
99
75.9M
        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.9M
        tt = it->templates + it->tcount;
106
268M
        for (i = 0; i < it->tcount; i++) {
107
192M
            ASN1_VALUE **pseqval;
108
109
192M
            tt--;
110
192M
            seqtt = ossl_asn1_do_adb(*pval, tt, 0);
111
192M
            if (!seqtt)
112
513k
                continue;
113
191M
            pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
114
191M
            ossl_asn1_template_free(pseqval, seqtt);
115
191M
        }
116
75.9M
        if (asn1_cb)
117
4.50M
            asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
118
75.9M
        if (embed == 0) {
119
69.7M
            OPENSSL_free(*pval);
120
69.7M
            *pval = NULL;
121
69.7M
        }
122
75.9M
        break;
123
385M
    }
124
385M
}
125
126
void ossl_asn1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
127
279M
{
128
279M
    int embed = tt->flags & ASN1_TFLG_EMBED;
129
279M
    ASN1_VALUE *tval;
130
279M
    if (embed) {
131
15.6M
        tval = (ASN1_VALUE *)pval;
132
15.6M
        pval = &tval;
133
15.6M
    }
134
279M
    if (tt->flags & ASN1_TFLG_SK_MASK) {
135
72.1M
        STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
136
72.1M
        int i;
137
138
186M
        for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
139
114M
            ASN1_VALUE *vtmp = sk_ASN1_VALUE_value(sk, i);
140
141
114M
            ossl_asn1_item_embed_free(&vtmp, ASN1_ITEM_ptr(tt->item), embed);
142
114M
        }
143
72.1M
        sk_ASN1_VALUE_free(sk);
144
72.1M
        *pval = NULL;
145
206M
    } else {
146
206M
        ossl_asn1_item_embed_free(pval, ASN1_ITEM_ptr(tt->item), embed);
147
206M
    }
148
279M
}
149
150
void ossl_asn1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
151
295M
{
152
295M
    int utype;
153
154
    /* Special case: if 'it' is a primitive with a free_func, use that. */
155
295M
    if (it) {
156
236M
        const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
157
158
236M
        if (embed) {
159
9.31M
            if (pf && pf->prim_clear) {
160
1.52M
                pf->prim_clear(pval, it);
161
1.52M
                return;
162
1.52M
            }
163
227M
        } else if (pf && pf->prim_free) {
164
1.34M
            pf->prim_free(pval, it);
165
1.34M
            return;
166
1.34M
        }
167
236M
    }
168
169
    /* Special case: if 'it' is NULL, free contents of ASN1_TYPE */
170
292M
    if (!it) {
171
59.5M
        ASN1_TYPE *typ = (ASN1_TYPE *)*pval;
172
173
59.5M
        utype = typ->type;
174
59.5M
        pval = &typ->value.asn1_value;
175
59.5M
        if (*pval == NULL)
176
1.09M
            return;
177
233M
    } else if (it->itype == ASN1_ITYPE_MSTRING) {
178
44.5M
        utype = -1;
179
44.5M
        if (*pval == NULL)
180
0
            return;
181
188M
    } else {
182
188M
        utype = it->utype;
183
188M
        if ((utype != V_ASN1_BOOLEAN) && *pval == NULL)
184
34.0M
            return;
185
188M
    }
186
187
257M
    switch (utype) {
188
60.9M
    case V_ASN1_OBJECT:
189
60.9M
        ASN1_OBJECT_free((ASN1_OBJECT *)*pval);
190
60.9M
        break;
191
192
18.7M
    case V_ASN1_BOOLEAN:
193
18.7M
        if (it)
194
11.1M
            *(ASN1_BOOLEAN *)pval = it->size;
195
7.59M
        else
196
7.59M
            *(ASN1_BOOLEAN *)pval = -1;
197
18.7M
        return;
198
199
22.3k
    case V_ASN1_NULL:
200
22.3k
        break;
201
202
59.5M
    case V_ASN1_ANY:
203
59.5M
        ossl_asn1_primitive_free(pval, NULL, 0);
204
59.5M
        OPENSSL_free(*pval);
205
59.5M
        break;
206
207
118M
    default:
208
118M
        ossl_asn1_string_embed_free((ASN1_STRING *)*pval, embed);
209
118M
        break;
210
257M
    }
211
239M
    *pval = NULL;
212
239M
}