Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl41/crypto/asn1/tasn_fre.c
Line
Count
Source
1
/*
2
 * Copyright 2000-2026 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
60.0M
{
20
60.0M
    ossl_asn1_item_embed_free(&val, it, 0);
21
60.0M
}
22
23
void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
24
12.4M
{
25
12.4M
    ossl_asn1_item_embed_free(pval, it, 0);
26
12.4M
}
27
28
void ossl_asn1_item_embed_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
29
306M
{
30
306M
    const ASN1_TEMPLATE *tt = NULL, *seqtt;
31
306M
    const ASN1_EXTERN_FUNCS *ef;
32
306M
    const ASN1_AUX *aux = it->funcs;
33
306M
    ASN1_aux_cb *asn1_cb;
34
306M
    int i;
35
36
306M
    if (pval == NULL)
37
0
        return;
38
306M
    if ((it->itype != ASN1_ITYPE_PRIMITIVE) && *pval == NULL)
39
29.1M
        return;
40
277M
    if (aux && aux->asn1_cb)
41
6.73M
        asn1_cb = aux->asn1_cb;
42
271M
    else
43
271M
        asn1_cb = 0;
44
45
277M
    switch (it->itype) {
46
47
179M
    case ASN1_ITYPE_PRIMITIVE:
48
179M
        if (it->templates)
49
46.8M
            ossl_asn1_template_free(pval, it->templates);
50
132M
        else
51
132M
            ossl_asn1_primitive_free(pval, it, embed);
52
179M
        break;
53
54
32.0M
    case ASN1_ITYPE_MSTRING:
55
32.0M
        ossl_asn1_primitive_free(pval, it, embed);
56
32.0M
        break;
57
58
4.60M
    case ASN1_ITYPE_CHOICE:
59
4.60M
        if (asn1_cb) {
60
335k
            i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
61
335k
            if (i == 2)
62
0
                return;
63
335k
        }
64
4.60M
        i = ossl_asn1_get_choice_selector(pval, it);
65
4.60M
        if ((i >= 0) && (i < it->tcount)) {
66
3.03M
            ASN1_VALUE **pchval;
67
68
3.03M
            tt = it->templates + i;
69
3.03M
            pchval = ossl_asn1_get_field_ptr(pval, tt);
70
3.03M
            ossl_asn1_template_free(pchval, tt);
71
3.03M
        }
72
4.60M
        if (asn1_cb)
73
335k
            asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
74
4.60M
        if (embed == 0) {
75
4.49M
            OPENSSL_free(*pval);
76
4.49M
            *pval = NULL;
77
4.49M
        }
78
4.60M
        break;
79
80
4.66M
    case ASN1_ITYPE_EXTERN:
81
4.66M
        ef = it->funcs;
82
4.66M
        if (ef && ef->asn1_ex_free)
83
4.66M
            ef->asn1_ex_free(pval, it);
84
4.66M
        break;
85
86
782k
    case ASN1_ITYPE_NDEF_SEQUENCE:
87
56.7M
    case ASN1_ITYPE_SEQUENCE:
88
56.7M
        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
56.3M
        if (asn1_cb) {
95
3.91M
            i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
96
3.91M
            if (i == 2)
97
573k
                return;
98
3.91M
        }
99
55.7M
        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
55.7M
        tt = it->templates + it->tcount;
106
198M
        for (i = 0; i < it->tcount; i++) {
107
143M
            ASN1_VALUE **pseqval;
108
109
143M
            tt--;
110
143M
            seqtt = ossl_asn1_do_adb(*pval, tt, 0);
111
143M
            if (!seqtt)
112
372k
                continue;
113
142M
            pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
114
142M
            ossl_asn1_template_free(pseqval, seqtt);
115
142M
        }
116
55.7M
        if (asn1_cb)
117
3.34M
            asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
118
55.7M
        if (embed == 0) {
119
51.3M
            OPENSSL_free(*pval);
120
51.3M
            *pval = NULL;
121
51.3M
        }
122
55.7M
        break;
123
277M
    }
124
277M
}
125
126
void ossl_asn1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
127
212M
{
128
212M
    int embed = tt->flags & ASN1_TFLG_EMBED;
129
212M
    ASN1_VALUE *tval;
130
212M
    if (embed) {
131
11.4M
        tval = (ASN1_VALUE *)pval;
132
11.4M
        pval = &tval;
133
11.4M
    }
134
212M
    if (tt->flags & ASN1_TFLG_SK_MASK) {
135
57.7M
        STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
136
57.7M
        int i;
137
138
137M
        for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
139
79.4M
            ASN1_VALUE *vtmp = sk_ASN1_VALUE_value(sk, i);
140
141
79.4M
            ossl_asn1_item_embed_free(&vtmp, ASN1_ITEM_ptr(tt->item), embed);
142
79.4M
        }
143
57.7M
        sk_ASN1_VALUE_free(sk);
144
57.7M
        *pval = NULL;
145
154M
    } else {
146
154M
        ossl_asn1_item_embed_free(pval, ASN1_ITEM_ptr(tt->item), embed);
147
154M
    }
148
212M
}
149
150
void ossl_asn1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
151
200M
{
152
200M
    int utype;
153
154
    /* Special case: if 'it' is a primitive with a free_func, use that. */
155
200M
    if (it) {
156
164M
        const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
157
158
164M
        if (embed) {
159
6.89M
            if (pf && pf->prim_clear) {
160
1.08M
                pf->prim_clear(pval, it);
161
1.08M
                return;
162
1.08M
            }
163
157M
        } else if (pf && pf->prim_free) {
164
991k
            pf->prim_free(pval, it);
165
991k
            return;
166
991k
        }
167
164M
    }
168
169
    /* Special case: if 'it' is NULL, free contents of ASN1_TYPE */
170
198M
    if (!it) {
171
35.6M
        ASN1_TYPE *typ = (ASN1_TYPE *)*pval;
172
173
35.6M
        utype = typ->type;
174
35.6M
        pval = &typ->value.asn1_value;
175
35.6M
        if (*pval == NULL)
176
868k
            return;
177
162M
    } else if (it->itype == ASN1_ITYPE_MSTRING) {
178
32.0M
        utype = -1;
179
32.0M
        if (*pval == NULL)
180
0
            return;
181
130M
    } else {
182
130M
        utype = it->utype;
183
130M
        if ((utype != V_ASN1_BOOLEAN) && *pval == NULL)
184
26.3M
            return;
185
130M
    }
186
187
171M
    switch (utype) {
188
44.4M
    case V_ASN1_OBJECT:
189
44.4M
        ASN1_OBJECT_free((ASN1_OBJECT *)*pval);
190
44.4M
        break;
191
192
10.6M
    case V_ASN1_BOOLEAN:
193
10.6M
        if (it)
194
8.57M
            *(ASN1_BOOLEAN *)pval = it->size;
195
2.11M
        else
196
2.11M
            *(ASN1_BOOLEAN *)pval = -1;
197
10.6M
        return;
198
199
17.5k
    case V_ASN1_NULL:
200
17.5k
        break;
201
202
35.6M
    case V_ASN1_ANY:
203
35.6M
        ossl_asn1_primitive_free(pval, NULL, 0);
204
35.6M
        OPENSSL_free(*pval);
205
35.6M
        break;
206
207
80.4M
    default:
208
80.4M
        ossl_asn1_string_free_internal((ASN1_STRING *)*pval, 0, embed);
209
80.4M
        break;
210
171M
    }
211
160M
    *pval = NULL;
212
160M
}