Coverage Report

Created: 2023-06-08 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
4.27M
{
20
4.27M
    ossl_asn1_item_embed_free(&val, it, 0);
21
4.27M
}
22
23
void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
24
2.48M
{
25
2.48M
    ossl_asn1_item_embed_free(pval, it, 0);
26
2.48M
}
27
28
void ossl_asn1_item_embed_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
29
20.8M
{
30
20.8M
    const ASN1_TEMPLATE *tt = NULL, *seqtt;
31
20.8M
    const ASN1_EXTERN_FUNCS *ef;
32
20.8M
    const ASN1_AUX *aux = it->funcs;
33
20.8M
    ASN1_aux_cb *asn1_cb;
34
20.8M
    int i;
35
36
20.8M
    if (pval == NULL)
37
0
        return;
38
20.8M
    if ((it->itype != ASN1_ITYPE_PRIMITIVE) && *pval == NULL)
39
2.20M
        return;
40
18.6M
    if (aux && aux->asn1_cb)
41
1.07M
        asn1_cb = aux->asn1_cb;
42
17.5M
    else
43
17.5M
        asn1_cb = 0;
44
45
18.6M
    switch (it->itype) {
46
47
11.1M
    case ASN1_ITYPE_PRIMITIVE:
48
11.1M
        if (it->templates)
49
1.55M
            ossl_asn1_template_free(pval, it->templates);
50
9.57M
        else
51
9.57M
            ossl_asn1_primitive_free(pval, it, embed);
52
11.1M
        break;
53
54
2.01M
    case ASN1_ITYPE_MSTRING:
55
2.01M
        ossl_asn1_primitive_free(pval, it, embed);
56
2.01M
        break;
57
58
341k
    case ASN1_ITYPE_CHOICE:
59
341k
        if (asn1_cb) {
60
31.3k
            i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
61
31.3k
            if (i == 2)
62
0
                return;
63
31.3k
        }
64
341k
        i = ossl_asn1_get_choice_selector(pval, it);
65
341k
        if ((i >= 0) && (i < it->tcount)) {
66
99.8k
            ASN1_VALUE **pchval;
67
68
99.8k
            tt = it->templates + i;
69
99.8k
            pchval = ossl_asn1_get_field_ptr(pval, tt);
70
99.8k
            ossl_asn1_template_free(pchval, tt);
71
99.8k
        }
72
341k
        if (asn1_cb)
73
31.3k
            asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
74
341k
        if (embed == 0) {
75
322k
            OPENSSL_free(*pval);
76
322k
            *pval = NULL;
77
322k
        }
78
341k
        break;
79
80
423k
    case ASN1_ITYPE_EXTERN:
81
423k
        ef = it->funcs;
82
423k
        if (ef && ef->asn1_ex_free)
83
423k
            ef->asn1_ex_free(pval, it);
84
423k
        break;
85
86
149k
    case ASN1_ITYPE_NDEF_SEQUENCE:
87
4.74M
    case ASN1_ITYPE_SEQUENCE:
88
4.74M
        if (ossl_asn1_do_lock(pval, -1, it) != 0) /* if error or ref-counter > 0 */
89
0
            return;
90
4.74M
        if (asn1_cb) {
91
627k
            i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
92
627k
            if (i == 2)
93
151k
                return;
94
627k
        }
95
4.59M
        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.59M
        tt = it->templates + it->tcount;
102
16.6M
        for (i = 0; i < it->tcount; i++) {
103
12.1M
            ASN1_VALUE **pseqval;
104
105
12.1M
            tt--;
106
12.1M
            seqtt = ossl_asn1_do_adb(*pval, tt, 0);
107
12.1M
            if (!seqtt)
108
69.4k
                continue;
109
12.0M
            pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
110
12.0M
            ossl_asn1_template_free(pseqval, seqtt);
111
12.0M
        }
112
4.59M
        if (asn1_cb)
113
475k
            asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
114
4.59M
        if (embed == 0) {
115
4.11M
            OPENSSL_free(*pval);
116
4.11M
            *pval = NULL;
117
4.11M
        }
118
4.59M
        break;
119
18.6M
    }
120
18.6M
}
121
122
void ossl_asn1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
123
14.7M
{
124
14.7M
    int embed = tt->flags & ASN1_TFLG_EMBED;
125
14.7M
    ASN1_VALUE *tval;
126
14.7M
    if (embed) {
127
1.04M
        tval = (ASN1_VALUE *)pval;
128
1.04M
        pval = &tval;
129
1.04M
    }
130
14.7M
    if (tt->flags & ASN1_TFLG_SK_MASK) {
131
2.61M
        STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
132
2.61M
        int i;
133
134
4.55M
        for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
135
1.93M
            ASN1_VALUE *vtmp = sk_ASN1_VALUE_value(sk, i);
136
137
1.93M
            ossl_asn1_item_embed_free(&vtmp, ASN1_ITEM_ptr(tt->item), embed);
138
1.93M
        }
139
2.61M
        sk_ASN1_VALUE_free(sk);
140
2.61M
        *pval = NULL;
141
12.1M
    } else {
142
12.1M
        ossl_asn1_item_embed_free(pval, ASN1_ITEM_ptr(tt->item), embed);
143
12.1M
    }
144
14.7M
}
145
146
void ossl_asn1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
147
12.9M
{
148
12.9M
    int utype;
149
150
    /* Special case: if 'it' is a primitive with a free_func, use that. */
151
12.9M
    if (it) {
152
11.5M
        const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
153
154
11.5M
        if (embed) {
155
551k
            if (pf && pf->prim_clear) {
156
209k
                pf->prim_clear(pval, it);
157
209k
                return;
158
209k
            }
159
11.0M
        } else if (pf && pf->prim_free) {
160
204k
            pf->prim_free(pval, it);
161
204k
            return;
162
204k
        }
163
11.5M
    }
164
165
    /* Special case: if 'it' is NULL, free contents of ASN1_TYPE */
166
12.5M
    if (!it) {
167
1.40M
        ASN1_TYPE *typ = (ASN1_TYPE *)*pval;
168
169
1.40M
        utype = typ->type;
170
1.40M
        pval = &typ->value.asn1_value;
171
1.40M
        if (*pval == NULL)
172
906k
            return;
173
11.1M
    } else if (it->itype == ASN1_ITYPE_MSTRING) {
174
2.01M
        utype = -1;
175
2.01M
        if (*pval == NULL)
176
0
            return;
177
9.16M
    } else {
178
9.16M
        utype = it->utype;
179
9.16M
        if ((utype != V_ASN1_BOOLEAN) && *pval == NULL)
180
2.68M
            return;
181
9.16M
    }
182
183
8.98M
    switch (utype) {
184
3.05M
    case V_ASN1_OBJECT:
185
3.05M
        ASN1_OBJECT_free((ASN1_OBJECT *)*pval);
186
3.05M
        break;
187
188
357k
    case V_ASN1_BOOLEAN:
189
357k
        if (it)
190
355k
            *(ASN1_BOOLEAN *)pval = it->size;
191
1.17k
        else
192
1.17k
            *(ASN1_BOOLEAN *)pval = -1;
193
357k
        return;
194
195
35
    case V_ASN1_NULL:
196
35
        break;
197
198
1.40M
    case V_ASN1_ANY:
199
1.40M
        ossl_asn1_primitive_free(pval, NULL, 0);
200
1.40M
        OPENSSL_free(*pval);
201
1.40M
        break;
202
203
4.17M
    default:
204
4.17M
        ossl_asn1_string_embed_free((ASN1_STRING *)*pval, embed);
205
4.17M
        break;
206
8.98M
    }
207
8.63M
    *pval = NULL;
208
8.63M
}