Coverage Report

Created: 2024-05-21 06:52

/src/openssl/crypto/asn1/tasn_fre.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2000-2021 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
14.3M
{
20
14.3M
    ossl_asn1_item_embed_free(&val, it, 0);
21
14.3M
}
22
23
void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
24
0
{
25
0
    ossl_asn1_item_embed_free(pval, it, 0);
26
0
}
27
28
void ossl_asn1_item_embed_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
29
42.5M
{
30
42.5M
    const ASN1_TEMPLATE *tt = NULL, *seqtt;
31
42.5M
    const ASN1_EXTERN_FUNCS *ef;
32
42.5M
    const ASN1_AUX *aux = it->funcs;
33
42.5M
    ASN1_aux_cb *asn1_cb;
34
42.5M
    int i;
35
36
42.5M
    if (pval == NULL)
37
0
        return;
38
42.5M
    if ((it->itype != ASN1_ITYPE_PRIMITIVE) && *pval == NULL)
39
4.96M
        return;
40
37.5M
    if (aux && aux->asn1_cb)
41
765k
        asn1_cb = aux->asn1_cb;
42
36.7M
    else
43
36.7M
        asn1_cb = 0;
44
45
37.5M
    switch (it->itype) {
46
47
19.2M
    case ASN1_ITYPE_PRIMITIVE:
48
19.2M
        if (it->templates)
49
1.44M
            ossl_asn1_template_free(pval, it->templates);
50
17.7M
        else
51
17.7M
            ossl_asn1_primitive_free(pval, it, embed);
52
19.2M
        break;
53
54
6.07M
    case ASN1_ITYPE_MSTRING:
55
6.07M
        ossl_asn1_primitive_free(pval, it, embed);
56
6.07M
        break;
57
58
113k
    case ASN1_ITYPE_CHOICE:
59
113k
        if (asn1_cb) {
60
44.7k
            i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
61
44.7k
            if (i == 2)
62
0
                return;
63
44.7k
        }
64
113k
        i = ossl_asn1_get_choice_selector(pval, it);
65
113k
        if ((i >= 0) && (i < it->tcount)) {
66
113k
            ASN1_VALUE **pchval;
67
68
113k
            tt = it->templates + i;
69
113k
            pchval = ossl_asn1_get_field_ptr(pval, tt);
70
113k
            ossl_asn1_template_free(pchval, tt);
71
113k
        }
72
113k
        if (asn1_cb)
73
44.7k
            asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
74
113k
        if (embed == 0) {
75
113k
            OPENSSL_free(*pval);
76
113k
            *pval = NULL;
77
113k
        }
78
113k
        break;
79
80
1.45M
    case ASN1_ITYPE_EXTERN:
81
1.45M
        ef = it->funcs;
82
1.45M
        if (ef && ef->asn1_ex_free)
83
1.45M
            ef->asn1_ex_free(pval, it);
84
1.45M
        break;
85
86
0
    case ASN1_ITYPE_NDEF_SEQUENCE:
87
10.6M
    case ASN1_ITYPE_SEQUENCE:
88
10.6M
        if (ossl_asn1_do_lock(pval, -1, it) != 0) /* if error or ref-counter > 0 */
89
360k
            return;
90
10.3M
        if (asn1_cb) {
91
360k
            i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
92
360k
            if (i == 2)
93
0
                return;
94
360k
        }
95
10.3M
        ossl_asn1_enc_free(pval, it);
96
        /*
97
         * If we free up as normal we will invalidate any ANY DEFINED BY
98
         * field and we won't be able to determine the type of the field it
99
         * defines. So free up in reverse order.
100
         */
101
10.3M
        tt = it->templates + it->tcount;
102
35.6M
        for (i = 0; i < it->tcount; i++) {
103
25.2M
            ASN1_VALUE **pseqval;
104
105
25.2M
            tt--;
106
25.2M
            seqtt = ossl_asn1_do_adb(*pval, tt, 0);
107
25.2M
            if (!seqtt)
108
0
                continue;
109
25.2M
            pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
110
25.2M
            ossl_asn1_template_free(pseqval, seqtt);
111
25.2M
        }
112
10.3M
        if (asn1_cb)
113
360k
            asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
114
10.3M
        if (embed == 0) {
115
8.88M
            OPENSSL_free(*pval);
116
8.88M
            *pval = NULL;
117
8.88M
        }
118
10.3M
        break;
119
37.5M
    }
120
37.5M
}
121
122
void ossl_asn1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
123
28.8M
{
124
28.8M
    int embed = tt->flags & ASN1_TFLG_EMBED;
125
28.8M
    ASN1_VALUE *tval;
126
28.8M
    if (embed) {
127
3.41M
        tval = (ASN1_VALUE *)pval;
128
3.41M
        pval = &tval;
129
3.41M
    }
130
28.8M
    if (tt->flags & ASN1_TFLG_SK_MASK) {
131
2.08M
        STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
132
2.08M
        int i;
133
134
3.44M
        for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
135
1.36M
            ASN1_VALUE *vtmp = sk_ASN1_VALUE_value(sk, i);
136
137
1.36M
            ossl_asn1_item_embed_free(&vtmp, ASN1_ITEM_ptr(tt->item), embed);
138
1.36M
        }
139
2.08M
        sk_ASN1_VALUE_free(sk);
140
2.08M
        *pval = NULL;
141
26.7M
    } else {
142
26.7M
        ossl_asn1_item_embed_free(pval, ASN1_ITEM_ptr(tt->item), embed);
143
26.7M
    }
144
28.8M
}
145
146
void ossl_asn1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
147
25.4M
{
148
25.4M
    int utype;
149
150
    /* Special case: if 'it' is a primitive with a free_func, use that. */
151
25.4M
    if (it) {
152
23.8M
        const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
153
154
23.8M
        if (embed) {
155
1.96M
            if (pf && pf->prim_clear) {
156
0
                pf->prim_clear(pval, it);
157
0
                return;
158
0
            }
159
21.8M
        } else if (pf && pf->prim_free) {
160
0
            pf->prim_free(pval, it);
161
0
            return;
162
0
        }
163
23.8M
    }
164
165
    /* Special case: if 'it' is NULL, free contents of ASN1_TYPE */
166
25.4M
    if (!it) {
167
1.62M
        ASN1_TYPE *typ = (ASN1_TYPE *)*pval;
168
169
1.62M
        utype = typ->type;
170
1.62M
        pval = &typ->value.asn1_value;
171
1.62M
        if (*pval == NULL)
172
1.35M
            return;
173
23.8M
    } else if (it->itype == ASN1_ITYPE_MSTRING) {
174
6.07M
        utype = -1;
175
6.07M
        if (*pval == NULL)
176
0
            return;
177
17.7M
    } else {
178
17.7M
        utype = it->utype;
179
17.7M
        if ((utype != V_ASN1_BOOLEAN) && *pval == NULL)
180
2.71M
            return;
181
17.7M
    }
182
183
21.3M
    switch (utype) {
184
8.66M
    case V_ASN1_OBJECT:
185
8.66M
        ASN1_OBJECT_free((ASN1_OBJECT *)*pval);
186
8.66M
        break;
187
188
2.17M
    case V_ASN1_BOOLEAN:
189
2.17M
        if (it)
190
2.17M
            *(ASN1_BOOLEAN *)pval = it->size;
191
0
        else
192
0
            *(ASN1_BOOLEAN *)pval = -1;
193
2.17M
        return;
194
195
0
    case V_ASN1_NULL:
196
0
        break;
197
198
1.62M
    case V_ASN1_ANY:
199
1.62M
        ossl_asn1_primitive_free(pval, NULL, 0);
200
1.62M
        OPENSSL_free(*pval);
201
1.62M
        break;
202
203
8.92M
    default:
204
8.92M
        ossl_asn1_string_embed_free((ASN1_STRING *)*pval, embed);
205
8.92M
        break;
206
21.3M
    }
207
19.2M
    *pval = NULL;
208
19.2M
}