Coverage Report

Created: 2025-04-22 06:18

/src/openssl/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
47.1k
{
20
47.1k
    ossl_asn1_item_embed_free(&val, it, 0);
21
47.1k
}
22
23
void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
24
1.45k
{
25
1.45k
    ossl_asn1_item_embed_free(pval, it, 0);
26
1.45k
}
27
28
void ossl_asn1_item_embed_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
29
129k
{
30
129k
    const ASN1_TEMPLATE *tt = NULL, *seqtt;
31
129k
    const ASN1_EXTERN_FUNCS *ef;
32
129k
    const ASN1_AUX *aux = it->funcs;
33
129k
    ASN1_aux_cb *asn1_cb;
34
129k
    int i;
35
36
129k
    if (pval == NULL)
37
0
        return;
38
129k
    if ((it->itype != ASN1_ITYPE_PRIMITIVE) && *pval == NULL)
39
24.8k
        return;
40
104k
    if (aux && aux->asn1_cb)
41
2.34k
        asn1_cb = aux->asn1_cb;
42
102k
    else
43
102k
        asn1_cb = 0;
44
45
104k
    switch (it->itype) {
46
47
59.2k
    case ASN1_ITYPE_PRIMITIVE:
48
59.2k
        if (it->templates)
49
8.51k
            ossl_asn1_template_free(pval, it->templates);
50
50.7k
        else
51
50.7k
            ossl_asn1_primitive_free(pval, it, embed);
52
59.2k
        break;
53
54
11.1k
    case ASN1_ITYPE_MSTRING:
55
11.1k
        ossl_asn1_primitive_free(pval, it, embed);
56
11.1k
        break;
57
58
210
    case ASN1_ITYPE_CHOICE:
59
210
        if (asn1_cb) {
60
0
            i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
61
0
            if (i == 2)
62
0
                return;
63
0
        }
64
210
        i = ossl_asn1_get_choice_selector(pval, it);
65
210
        if ((i >= 0) && (i < it->tcount)) {
66
0
            ASN1_VALUE **pchval;
67
68
0
            tt = it->templates + i;
69
0
            pchval = ossl_asn1_get_field_ptr(pval, tt);
70
0
            ossl_asn1_template_free(pchval, tt);
71
0
        }
72
210
        if (asn1_cb)
73
0
            asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
74
210
        if (embed == 0) {
75
210
            OPENSSL_free(*pval);
76
210
            *pval = NULL;
77
210
        }
78
210
        break;
79
80
7.40k
    case ASN1_ITYPE_EXTERN:
81
7.40k
        ef = it->funcs;
82
7.40k
        if (ef && ef->asn1_ex_free)
83
7.40k
            ef->asn1_ex_free(pval, it);
84
7.40k
        break;
85
86
0
    case ASN1_ITYPE_NDEF_SEQUENCE:
87
26.3k
    case ASN1_ITYPE_SEQUENCE:
88
26.3k
        if (ossl_asn1_do_lock(pval, -1, it) != 0) {
89
            /* if error or ref-counter > 0 */
90
0
            OPENSSL_assert(embed == 0);
91
0
            *pval = NULL;
92
0
            return;
93
0
        }
94
26.3k
        if (asn1_cb) {
95
2.08k
            i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
96
2.08k
            if (i == 2)
97
11
                return;
98
2.08k
        }
99
26.3k
        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
26.3k
        tt = it->templates + it->tcount;
106
101k
        for (i = 0; i < it->tcount; i++) {
107
74.6k
            ASN1_VALUE **pseqval;
108
109
74.6k
            tt--;
110
74.6k
            seqtt = ossl_asn1_do_adb(*pval, tt, 0);
111
74.6k
            if (!seqtt)
112
202
                continue;
113
74.4k
            pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
114
74.4k
            ossl_asn1_template_free(pseqval, seqtt);
115
74.4k
        }
116
26.3k
        if (asn1_cb)
117
2.07k
            asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
118
26.3k
        if (embed == 0) {
119
18.0k
            OPENSSL_free(*pval);
120
18.0k
            *pval = NULL;
121
18.0k
        }
122
26.3k
        break;
123
104k
    }
124
104k
}
125
126
void ossl_asn1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
127
89.7k
{
128
89.7k
    int embed = tt->flags & ASN1_TFLG_EMBED;
129
89.7k
    ASN1_VALUE *tval;
130
89.7k
    if (embed) {
131
14.9k
        tval = (ASN1_VALUE *)pval;
132
14.9k
        pval = &tval;
133
14.9k
    }
134
89.7k
    if (tt->flags & ASN1_TFLG_SK_MASK) {
135
11.3k
        STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
136
11.3k
        int i;
137
138
13.6k
        for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
139
2.24k
            ASN1_VALUE *vtmp = sk_ASN1_VALUE_value(sk, i);
140
141
2.24k
            ossl_asn1_item_embed_free(&vtmp, ASN1_ITEM_ptr(tt->item), embed);
142
2.24k
        }
143
11.3k
        sk_ASN1_VALUE_free(sk);
144
11.3k
        *pval = NULL;
145
78.3k
    } else {
146
78.3k
        ossl_asn1_item_embed_free(pval, ASN1_ITEM_ptr(tt->item), embed);
147
78.3k
    }
148
89.7k
}
149
150
void ossl_asn1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
151
69.0k
{
152
69.0k
    int utype;
153
154
    /* Special case: if 'it' is a primitive with a free_func, use that. */
155
69.0k
    if (it) {
156
61.8k
        const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
157
158
61.8k
        if (embed) {
159
6.62k
            if (pf && pf->prim_clear) {
160
210
                pf->prim_clear(pval, it);
161
210
                return;
162
210
            }
163
55.2k
        } else if (pf && pf->prim_free) {
164
54
            pf->prim_free(pval, it);
165
54
            return;
166
54
        }
167
61.8k
    }
168
169
    /* Special case: if 'it' is NULL, free contents of ASN1_TYPE */
170
68.7k
    if (!it) {
171
7.16k
        ASN1_TYPE *typ = (ASN1_TYPE *)*pval;
172
173
7.16k
        utype = typ->type;
174
7.16k
        pval = &typ->value.asn1_value;
175
7.16k
        if (*pval == NULL)
176
3.29k
            return;
177
61.6k
    } else if (it->itype == ASN1_ITYPE_MSTRING) {
178
11.1k
        utype = -1;
179
11.1k
        if (*pval == NULL)
180
0
            return;
181
50.5k
    } else {
182
50.5k
        utype = it->utype;
183
50.5k
        if ((utype != V_ASN1_BOOLEAN) && *pval == NULL)
184
11.8k
            return;
185
50.5k
    }
186
187
53.6k
    switch (utype) {
188
21.0k
    case V_ASN1_OBJECT:
189
21.0k
        ASN1_OBJECT_free((ASN1_OBJECT *)*pval);
190
21.0k
        break;
191
192
3.80k
    case V_ASN1_BOOLEAN:
193
3.80k
        if (it)
194
3.79k
            *(ASN1_BOOLEAN *)pval = it->size;
195
15
        else
196
15
            *(ASN1_BOOLEAN *)pval = -1;
197
3.80k
        return;
198
199
0
    case V_ASN1_NULL:
200
0
        break;
201
202
7.16k
    case V_ASN1_ANY:
203
7.16k
        ossl_asn1_primitive_free(pval, NULL, 0);
204
7.16k
        OPENSSL_free(*pval);
205
7.16k
        break;
206
207
21.6k
    default:
208
21.6k
        ossl_asn1_string_embed_free((ASN1_STRING *)*pval, embed);
209
21.6k
        break;
210
53.6k
    }
211
49.8k
    *pval = NULL;
212
49.8k
}