Coverage Report

Created: 2023-09-25 06:43

/src/openssl30/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
3.65M
{
20
3.65M
    ossl_asn1_item_embed_free(&val, it, 0);
21
3.65M
}
22
23
void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
24
2.51M
{
25
2.51M
    ossl_asn1_item_embed_free(pval, it, 0);
26
2.51M
}
27
28
void ossl_asn1_item_embed_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
29
19.1M
{
30
19.1M
    const ASN1_TEMPLATE *tt = NULL, *seqtt;
31
19.1M
    const ASN1_EXTERN_FUNCS *ef;
32
19.1M
    const ASN1_AUX *aux = it->funcs;
33
19.1M
    ASN1_aux_cb *asn1_cb;
34
19.1M
    int i;
35
36
19.1M
    if (pval == NULL)
37
0
        return;
38
19.1M
    if ((it->itype != ASN1_ITYPE_PRIMITIVE) && *pval == NULL)
39
2.23M
        return;
40
16.9M
    if (aux && aux->asn1_cb)
41
1.08M
        asn1_cb = aux->asn1_cb;
42
15.8M
    else
43
15.8M
        asn1_cb = 0;
44
45
16.9M
    switch (it->itype) {
46
47
9.75M
    case ASN1_ITYPE_PRIMITIVE:
48
9.75M
        if (it->templates)
49
815k
            ossl_asn1_template_free(pval, it->templates);
50
8.93M
        else
51
8.93M
            ossl_asn1_primitive_free(pval, it, embed);
52
9.75M
        break;
53
54
1.80M
    case ASN1_ITYPE_MSTRING:
55
1.80M
        ossl_asn1_primitive_free(pval, it, embed);
56
1.80M
        break;
57
58
346k
    case ASN1_ITYPE_CHOICE:
59
346k
        if (asn1_cb) {
60
31.0k
            i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
61
31.0k
            if (i == 2)
62
0
                return;
63
31.0k
        }
64
346k
        i = ossl_asn1_get_choice_selector(pval, it);
65
346k
        if ((i >= 0) && (i < it->tcount)) {
66
102k
            ASN1_VALUE **pchval;
67
68
102k
            tt = it->templates + i;
69
102k
            pchval = ossl_asn1_get_field_ptr(pval, tt);
70
102k
            ossl_asn1_template_free(pchval, tt);
71
102k
        }
72
346k
        if (asn1_cb)
73
31.0k
            asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
74
346k
        if (embed == 0) {
75
326k
            OPENSSL_free(*pval);
76
326k
            *pval = NULL;
77
326k
        }
78
346k
        break;
79
80
427k
    case ASN1_ITYPE_EXTERN:
81
427k
        ef = it->funcs;
82
427k
        if (ef && ef->asn1_ex_free)
83
427k
            ef->asn1_ex_free(pval, it);
84
427k
        break;
85
86
150k
    case ASN1_ITYPE_NDEF_SEQUENCE:
87
4.57M
    case ASN1_ITYPE_SEQUENCE:
88
4.57M
        if (ossl_asn1_do_lock(pval, -1, it) != 0) /* if error or ref-counter > 0 */
89
0
            return;
90
4.57M
        if (asn1_cb) {
91
637k
            i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
92
637k
            if (i == 2)
93
154k
                return;
94
637k
        }
95
4.41M
        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
4.41M
        tt = it->templates + it->tcount;
102
16.1M
        for (i = 0; i < it->tcount; i++) {
103
11.7M
            ASN1_VALUE **pseqval;
104
105
11.7M
            tt--;
106
11.7M
            seqtt = ossl_asn1_do_adb(*pval, tt, 0);
107
11.7M
            if (!seqtt)
108
69.5k
                continue;
109
11.7M
            pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
110
11.7M
            ossl_asn1_template_free(pseqval, seqtt);
111
11.7M
        }
112
4.41M
        if (asn1_cb)
113
483k
            asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
114
4.41M
        if (embed == 0) {
115
3.93M
            OPENSSL_free(*pval);
116
3.93M
            *pval = NULL;
117
3.93M
        }
118
4.41M
        break;
119
16.9M
    }
120
16.9M
}
121
122
void ossl_asn1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
123
13.7M
{
124
13.7M
    int embed = tt->flags & ASN1_TFLG_EMBED;
125
13.7M
    ASN1_VALUE *tval;
126
13.7M
    if (embed) {
127
1.05M
        tval = (ASN1_VALUE *)pval;
128
1.05M
        pval = &tval;
129
1.05M
    }
130
13.7M
    if (tt->flags & ASN1_TFLG_SK_MASK) {
131
1.88M
        STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
132
1.88M
        int i;
133
134
2.99M
        for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
135
1.11M
            ASN1_VALUE *vtmp = sk_ASN1_VALUE_value(sk, i);
136
137
1.11M
            ossl_asn1_item_embed_free(&vtmp, ASN1_ITEM_ptr(tt->item), embed);
138
1.11M
        }
139
1.88M
        sk_ASN1_VALUE_free(sk);
140
1.88M
        *pval = NULL;
141
11.8M
    } else {
142
11.8M
        ossl_asn1_item_embed_free(pval, ASN1_ITEM_ptr(tt->item), embed);
143
11.8M
    }
144
13.7M
}
145
146
void ossl_asn1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
147
11.6M
{
148
11.6M
    int utype;
149
150
    /* Special case: if 'it' is a primitive with a free_func, use that. */
151
11.6M
    if (it) {
152
10.7M
        const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
153
154
10.7M
        if (embed) {
155
555k
            if (pf && pf->prim_clear) {
156
211k
                pf->prim_clear(pval, it);
157
211k
                return;
158
211k
            }
159
10.1M
        } else if (pf && pf->prim_free) {
160
202k
            pf->prim_free(pval, it);
161
202k
            return;
162
202k
        }
163
10.7M
    }
164
165
    /* Special case: if 'it' is NULL, free contents of ASN1_TYPE */
166
11.2M
    if (!it) {
167
891k
        ASN1_TYPE *typ = (ASN1_TYPE *)*pval;
168
169
891k
        utype = typ->type;
170
891k
        pval = &typ->value.asn1_value;
171
891k
        if (*pval == NULL)
172
415k
            return;
173
10.3M
    } else if (it->itype == ASN1_ITYPE_MSTRING) {
174
1.80M
        utype = -1;
175
1.80M
        if (*pval == NULL)
176
0
            return;
177
8.52M
    } else {
178
8.52M
        utype = it->utype;
179
8.52M
        if ((utype != V_ASN1_BOOLEAN) && *pval == NULL)
180
2.73M
            return;
181
8.52M
    }
182
183
8.07M
    switch (utype) {
184
2.85M
    case V_ASN1_OBJECT:
185
2.85M
        ASN1_OBJECT_free((ASN1_OBJECT *)*pval);
186
2.85M
        break;
187
188
353k
    case V_ASN1_BOOLEAN:
189
353k
        if (it)
190
352k
            *(ASN1_BOOLEAN *)pval = it->size;
191
1.09k
        else
192
1.09k
            *(ASN1_BOOLEAN *)pval = -1;
193
353k
        return;
194
195
26
    case V_ASN1_NULL:
196
26
        break;
197
198
891k
    case V_ASN1_ANY:
199
891k
        ossl_asn1_primitive_free(pval, NULL, 0);
200
891k
        OPENSSL_free(*pval);
201
891k
        break;
202
203
3.96M
    default:
204
3.96M
        ossl_asn1_string_embed_free((ASN1_STRING *)*pval, embed);
205
3.96M
        break;
206
8.07M
    }
207
7.71M
    *pval = NULL;
208
7.71M
}