Coverage Report

Created: 2024-07-27 06:39

/src/openssl31/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
51.8M
{
20
51.8M
    ossl_asn1_item_embed_free(&val, it, 0);
21
51.8M
}
22
23
void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
24
10.6M
{
25
10.6M
    ossl_asn1_item_embed_free(pval, it, 0);
26
10.6M
}
27
28
void ossl_asn1_item_embed_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
29
246M
{
30
246M
    const ASN1_TEMPLATE *tt = NULL, *seqtt;
31
246M
    const ASN1_EXTERN_FUNCS *ef;
32
246M
    const ASN1_AUX *aux = it->funcs;
33
246M
    ASN1_aux_cb *asn1_cb;
34
246M
    int i;
35
36
246M
    if (pval == NULL)
37
0
        return;
38
246M
    if ((it->itype != ASN1_ITYPE_PRIMITIVE) && *pval == NULL)
39
18.6M
        return;
40
227M
    if (aux && aux->asn1_cb)
41
5.40M
        asn1_cb = aux->asn1_cb;
42
222M
    else
43
222M
        asn1_cb = 0;
44
45
227M
    switch (it->itype) {
46
47
137M
    case ASN1_ITYPE_PRIMITIVE:
48
137M
        if (it->templates)
49
27.8M
            ossl_asn1_template_free(pval, it->templates);
50
109M
        else
51
109M
            ossl_asn1_primitive_free(pval, it, embed);
52
137M
        break;
53
54
31.1M
    case ASN1_ITYPE_MSTRING:
55
31.1M
        ossl_asn1_primitive_free(pval, it, embed);
56
31.1M
        break;
57
58
5.69M
    case ASN1_ITYPE_CHOICE:
59
5.69M
        if (asn1_cb) {
60
245k
            i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
61
245k
            if (i == 2)
62
0
                return;
63
245k
        }
64
5.69M
        i = ossl_asn1_get_choice_selector(pval, it);
65
5.69M
        if ((i >= 0) && (i < it->tcount)) {
66
4.34M
            ASN1_VALUE **pchval;
67
68
4.34M
            tt = it->templates + i;
69
4.34M
            pchval = ossl_asn1_get_field_ptr(pval, tt);
70
4.34M
            ossl_asn1_template_free(pchval, tt);
71
4.34M
        }
72
5.69M
        if (asn1_cb)
73
245k
            asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
74
5.69M
        if (embed == 0) {
75
5.61M
            OPENSSL_free(*pval);
76
5.61M
            *pval = NULL;
77
5.61M
        }
78
5.69M
        break;
79
80
3.65M
    case ASN1_ITYPE_EXTERN:
81
3.65M
        ef = it->funcs;
82
3.65M
        if (ef && ef->asn1_ex_free)
83
3.65M
            ef->asn1_ex_free(pval, it);
84
3.65M
        break;
85
86
704k
    case ASN1_ITYPE_NDEF_SEQUENCE:
87
49.5M
    case ASN1_ITYPE_SEQUENCE:
88
49.5M
        if (ossl_asn1_do_lock(pval, -1, it) != 0) /* if error or ref-counter > 0 */
89
231k
            return;
90
49.3M
        if (asn1_cb) {
91
3.07M
            i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
92
3.07M
            if (i == 2)
93
563k
                return;
94
3.07M
        }
95
48.7M
        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
48.7M
        tt = it->templates + it->tcount;
102
168M
        for (i = 0; i < it->tcount; i++) {
103
119M
            ASN1_VALUE **pseqval;
104
105
119M
            tt--;
106
119M
            seqtt = ossl_asn1_do_adb(*pval, tt, 0);
107
119M
            if (!seqtt)
108
327k
                continue;
109
119M
            pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
110
119M
            ossl_asn1_template_free(pseqval, seqtt);
111
119M
        }
112
48.7M
        if (asn1_cb)
113
2.51M
            asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
114
48.7M
        if (embed == 0) {
115
45.2M
            OPENSSL_free(*pval);
116
45.2M
            *pval = NULL;
117
45.2M
        }
118
48.7M
        break;
119
227M
    }
120
227M
}
121
122
void ossl_asn1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
123
164M
{
124
164M
    int embed = tt->flags & ASN1_TFLG_EMBED;
125
164M
    ASN1_VALUE *tval;
126
164M
    if (embed) {
127
8.78M
        tval = (ASN1_VALUE *)pval;
128
8.78M
        pval = &tval;
129
8.78M
    }
130
164M
    if (tt->flags & ASN1_TFLG_SK_MASK) {
131
35.1M
        STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
132
35.1M
        int i;
133
134
89.4M
        for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
135
54.2M
            ASN1_VALUE *vtmp = sk_ASN1_VALUE_value(sk, i);
136
137
54.2M
            ossl_asn1_item_embed_free(&vtmp, ASN1_ITEM_ptr(tt->item), embed);
138
54.2M
        }
139
35.1M
        sk_ASN1_VALUE_free(sk);
140
35.1M
        *pval = NULL;
141
129M
    } else {
142
129M
        ossl_asn1_item_embed_free(pval, ASN1_ITEM_ptr(tt->item), embed);
143
129M
    }
144
164M
}
145
146
void ossl_asn1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
147
166M
{
148
166M
    int utype;
149
150
    /* Special case: if 'it' is a primitive with a free_func, use that. */
151
166M
    if (it) {
152
140M
        const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
153
154
140M
        if (embed) {
155
5.13M
            if (pf && pf->prim_clear) {
156
984k
                pf->prim_clear(pval, it);
157
984k
                return;
158
984k
            }
159
135M
        } else if (pf && pf->prim_free) {
160
864k
            pf->prim_free(pval, it);
161
864k
            return;
162
864k
        }
163
140M
    }
164
165
    /* Special case: if 'it' is NULL, free contents of ASN1_TYPE */
166
164M
    if (!it) {
167
25.7M
        ASN1_TYPE *typ = (ASN1_TYPE *)*pval;
168
169
25.7M
        utype = typ->type;
170
25.7M
        pval = &typ->value.asn1_value;
171
25.7M
        if (*pval == NULL)
172
489k
            return;
173
138M
    } else if (it->itype == ASN1_ITYPE_MSTRING) {
174
31.1M
        utype = -1;
175
31.1M
        if (*pval == NULL)
176
0
            return;
177
107M
    } else {
178
107M
        utype = it->utype;
179
107M
        if ((utype != V_ASN1_BOOLEAN) && *pval == NULL)
180
19.3M
            return;
181
107M
    }
182
183
144M
    switch (utype) {
184
39.7M
    case V_ASN1_OBJECT:
185
39.7M
        ASN1_OBJECT_free((ASN1_OBJECT *)*pval);
186
39.7M
        break;
187
188
13.8M
    case V_ASN1_BOOLEAN:
189
13.8M
        if (it)
190
5.84M
            *(ASN1_BOOLEAN *)pval = it->size;
191
7.97M
        else
192
7.97M
            *(ASN1_BOOLEAN *)pval = -1;
193
13.8M
        return;
194
195
9.89k
    case V_ASN1_NULL:
196
9.89k
        break;
197
198
25.7M
    case V_ASN1_ANY:
199
25.7M
        ossl_asn1_primitive_free(pval, NULL, 0);
200
25.7M
        OPENSSL_free(*pval);
201
25.7M
        break;
202
203
65.5M
    default:
204
65.5M
        ossl_asn1_string_embed_free((ASN1_STRING *)*pval, embed);
205
65.5M
        break;
206
144M
    }
207
131M
    *pval = NULL;
208
131M
}