Coverage Report

Created: 2025-08-11 07:04

/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
61.1M
{
20
61.1M
    ossl_asn1_item_embed_free(&val, it, 0);
21
61.1M
}
22
23
void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
24
15.4M
{
25
15.4M
    ossl_asn1_item_embed_free(pval, it, 0);
26
15.4M
}
27
28
void ossl_asn1_item_embed_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
29
321M
{
30
321M
    const ASN1_TEMPLATE *tt = NULL, *seqtt;
31
321M
    const ASN1_EXTERN_FUNCS *ef;
32
321M
    const ASN1_AUX *aux = it->funcs;
33
321M
    ASN1_aux_cb *asn1_cb;
34
321M
    int i;
35
36
321M
    if (pval == NULL)
37
0
        return;
38
321M
    if ((it->itype != ASN1_ITYPE_PRIMITIVE) && *pval == NULL)
39
27.2M
        return;
40
294M
    if (aux && aux->asn1_cb)
41
7.58M
        asn1_cb = aux->asn1_cb;
42
286M
    else
43
286M
        asn1_cb = 0;
44
45
294M
    switch (it->itype) {
46
47
190M
    case ASN1_ITYPE_PRIMITIVE:
48
190M
        if (it->templates)
49
40.2M
            ossl_asn1_template_free(pval, it->templates);
50
150M
        else
51
150M
            ossl_asn1_primitive_free(pval, it, embed);
52
190M
        break;
53
54
33.8M
    case ASN1_ITYPE_MSTRING:
55
33.8M
        ossl_asn1_primitive_free(pval, it, embed);
56
33.8M
        break;
57
58
5.83M
    case ASN1_ITYPE_CHOICE:
59
5.83M
        if (asn1_cb) {
60
391k
            i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
61
391k
            if (i == 2)
62
0
                return;
63
391k
        }
64
5.83M
        i = ossl_asn1_get_choice_selector(pval, it);
65
5.83M
        if ((i >= 0) && (i < it->tcount)) {
66
3.95M
            ASN1_VALUE **pchval;
67
68
3.95M
            tt = it->templates + i;
69
3.95M
            pchval = ossl_asn1_get_field_ptr(pval, tt);
70
3.95M
            ossl_asn1_template_free(pchval, tt);
71
3.95M
        }
72
5.83M
        if (asn1_cb)
73
391k
            asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
74
5.83M
        if (embed == 0) {
75
5.71M
            OPENSSL_free(*pval);
76
5.71M
            *pval = NULL;
77
5.71M
        }
78
5.83M
        break;
79
80
4.38M
    case ASN1_ITYPE_EXTERN:
81
4.38M
        ef = it->funcs;
82
4.38M
        if (ef && ef->asn1_ex_free)
83
4.38M
            ef->asn1_ex_free(pval, it);
84
4.38M
        break;
85
86
907k
    case ASN1_ITYPE_NDEF_SEQUENCE:
87
59.5M
    case ASN1_ITYPE_SEQUENCE:
88
59.5M
        if (ossl_asn1_do_lock(pval, -1, it) != 0) {
89
            /* if error or ref-counter > 0 */
90
395k
            OPENSSL_assert(embed == 0);
91
395k
            *pval = NULL;
92
395k
            return;
93
395k
        }
94
59.1M
        if (asn1_cb) {
95
4.24M
            i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
96
4.24M
            if (i == 2)
97
755k
                return;
98
4.24M
        }
99
58.3M
        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
58.3M
        tt = it->templates + it->tcount;
106
206M
        for (i = 0; i < it->tcount; i++) {
107
148M
            ASN1_VALUE **pseqval;
108
109
148M
            tt--;
110
148M
            seqtt = ossl_asn1_do_adb(*pval, tt, 0);
111
148M
            if (!seqtt)
112
450k
                continue;
113
147M
            pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
114
147M
            ossl_asn1_template_free(pseqval, seqtt);
115
147M
        }
116
58.3M
        if (asn1_cb)
117
3.48M
            asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
118
58.3M
        if (embed == 0) {
119
54.2M
            OPENSSL_free(*pval);
120
54.2M
            *pval = NULL;
121
54.2M
        }
122
58.3M
        break;
123
294M
    }
124
294M
}
125
126
void ossl_asn1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
127
210M
{
128
210M
    int embed = tt->flags & ASN1_TFLG_EMBED;
129
210M
    ASN1_VALUE *tval;
130
210M
    if (embed) {
131
11.2M
        tval = (ASN1_VALUE *)pval;
132
11.2M
        pval = &tval;
133
11.2M
    }
134
210M
    if (tt->flags & ASN1_TFLG_SK_MASK) {
135
51.1M
        STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
136
51.1M
        int i;
137
138
137M
        for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
139
86.0M
            ASN1_VALUE *vtmp = sk_ASN1_VALUE_value(sk, i);
140
141
86.0M
            ossl_asn1_item_embed_free(&vtmp, ASN1_ITEM_ptr(tt->item), embed);
142
86.0M
        }
143
51.1M
        sk_ASN1_VALUE_free(sk);
144
51.1M
        *pval = NULL;
145
159M
    } else {
146
159M
        ossl_asn1_item_embed_free(pval, ASN1_ITEM_ptr(tt->item), embed);
147
159M
    }
148
210M
}
149
150
void ossl_asn1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
151
232M
{
152
232M
    int utype;
153
154
    /* Special case: if 'it' is a primitive with a free_func, use that. */
155
232M
    if (it) {
156
184M
        const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
157
158
184M
        if (embed) {
159
6.91M
            if (pf && pf->prim_clear) {
160
1.36M
                pf->prim_clear(pval, it);
161
1.36M
                return;
162
1.36M
            }
163
177M
        } else if (pf && pf->prim_free) {
164
1.19M
            pf->prim_free(pval, it);
165
1.19M
            return;
166
1.19M
        }
167
184M
    }
168
169
    /* Special case: if 'it' is NULL, free contents of ASN1_TYPE */
170
229M
    if (!it) {
171
47.5M
        ASN1_TYPE *typ = (ASN1_TYPE *)*pval;
172
173
47.5M
        utype = typ->type;
174
47.5M
        pval = &typ->value.asn1_value;
175
47.5M
        if (*pval == NULL)
176
919k
            return;
177
181M
    } else if (it->itype == ASN1_ITYPE_MSTRING) {
178
33.8M
        utype = -1;
179
33.8M
        if (*pval == NULL)
180
0
            return;
181
148M
    } else {
182
148M
        utype = it->utype;
183
148M
        if ((utype != V_ASN1_BOOLEAN) && *pval == NULL)
184
26.0M
            return;
185
148M
    }
186
187
202M
    switch (utype) {
188
45.5M
    case V_ASN1_OBJECT:
189
45.5M
        ASN1_OBJECT_free((ASN1_OBJECT *)*pval);
190
45.5M
        break;
191
192
27.0M
    case V_ASN1_BOOLEAN:
193
27.0M
        if (it)
194
8.86M
            *(ASN1_BOOLEAN *)pval = it->size;
195
18.1M
        else
196
18.1M
            *(ASN1_BOOLEAN *)pval = -1;
197
27.0M
        return;
198
199
12.5k
    case V_ASN1_NULL:
200
12.5k
        break;
201
202
47.5M
    case V_ASN1_ANY:
203
47.5M
        ossl_asn1_primitive_free(pval, NULL, 0);
204
47.5M
        OPENSSL_free(*pval);
205
47.5M
        break;
206
207
82.2M
    default:
208
82.2M
        ossl_asn1_string_embed_free((ASN1_STRING *)*pval, embed);
209
82.2M
        break;
210
202M
    }
211
175M
    *pval = NULL;
212
175M
}