Coverage Report

Created: 2024-09-08 06:32

/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
10.6M
{
20
10.6M
    ossl_asn1_item_embed_free(&val, it, 0);
21
10.6M
}
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
31.5M
{
30
31.5M
    const ASN1_TEMPLATE *tt = NULL, *seqtt;
31
31.5M
    const ASN1_EXTERN_FUNCS *ef;
32
31.5M
    const ASN1_AUX *aux = it->funcs;
33
31.5M
    ASN1_aux_cb *asn1_cb;
34
31.5M
    int i;
35
36
31.5M
    if (pval == NULL)
37
0
        return;
38
31.5M
    if ((it->itype != ASN1_ITYPE_PRIMITIVE) && *pval == NULL)
39
3.69M
        return;
40
27.8M
    if (aux && aux->asn1_cb)
41
568k
        asn1_cb = aux->asn1_cb;
42
27.3M
    else
43
27.3M
        asn1_cb = 0;
44
45
27.8M
    switch (it->itype) {
46
47
14.2M
    case ASN1_ITYPE_PRIMITIVE:
48
14.2M
        if (it->templates)
49
1.07M
            ossl_asn1_template_free(pval, it->templates);
50
13.2M
        else
51
13.2M
            ossl_asn1_primitive_free(pval, it, embed);
52
14.2M
        break;
53
54
4.50M
    case ASN1_ITYPE_MSTRING:
55
4.50M
        ossl_asn1_primitive_free(pval, it, embed);
56
4.50M
        break;
57
58
84.0k
    case ASN1_ITYPE_CHOICE:
59
84.0k
        if (asn1_cb) {
60
33.2k
            i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
61
33.2k
            if (i == 2)
62
0
                return;
63
33.2k
        }
64
84.0k
        i = ossl_asn1_get_choice_selector(pval, it);
65
84.0k
        if ((i >= 0) && (i < it->tcount)) {
66
84.0k
            ASN1_VALUE **pchval;
67
68
84.0k
            tt = it->templates + i;
69
84.0k
            pchval = ossl_asn1_get_field_ptr(pval, tt);
70
84.0k
            ossl_asn1_template_free(pchval, tt);
71
84.0k
        }
72
84.0k
        if (asn1_cb)
73
33.2k
            asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
74
84.0k
        if (embed == 0) {
75
84.0k
            OPENSSL_free(*pval);
76
84.0k
            *pval = NULL;
77
84.0k
        }
78
84.0k
        break;
79
80
1.08M
    case ASN1_ITYPE_EXTERN:
81
1.08M
        ef = it->funcs;
82
1.08M
        if (ef && ef->asn1_ex_free)
83
1.08M
            ef->asn1_ex_free(pval, it);
84
1.08M
        break;
85
86
0
    case ASN1_ITYPE_NDEF_SEQUENCE:
87
7.94M
    case ASN1_ITYPE_SEQUENCE:
88
7.94M
        if (ossl_asn1_do_lock(pval, -1, it) != 0) /* if error or ref-counter > 0 */
89
267k
            return;
90
7.67M
        if (asn1_cb) {
91
267k
            i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
92
267k
            if (i == 2)
93
0
                return;
94
267k
        }
95
7.67M
        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
7.67M
        tt = it->templates + it->tcount;
102
26.4M
        for (i = 0; i < it->tcount; i++) {
103
18.7M
            ASN1_VALUE **pseqval;
104
105
18.7M
            tt--;
106
18.7M
            seqtt = ossl_asn1_do_adb(*pval, tt, 0);
107
18.7M
            if (!seqtt)
108
0
                continue;
109
18.7M
            pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
110
18.7M
            ossl_asn1_template_free(pseqval, seqtt);
111
18.7M
        }
112
7.67M
        if (asn1_cb)
113
267k
            asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
114
7.67M
        if (embed == 0) {
115
6.60M
            OPENSSL_free(*pval);
116
6.60M
            *pval = NULL;
117
6.60M
        }
118
7.67M
        break;
119
27.8M
    }
120
27.8M
}
121
122
void ossl_asn1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
123
21.4M
{
124
21.4M
    int embed = tt->flags & ASN1_TFLG_EMBED;
125
21.4M
    ASN1_VALUE *tval;
126
21.4M
    if (embed) {
127
2.53M
        tval = (ASN1_VALUE *)pval;
128
2.53M
        pval = &tval;
129
2.53M
    }
130
21.4M
    if (tt->flags & ASN1_TFLG_SK_MASK) {
131
1.54M
        STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
132
1.54M
        int i;
133
134
2.56M
        for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
135
1.01M
            ASN1_VALUE *vtmp = sk_ASN1_VALUE_value(sk, i);
136
137
1.01M
            ossl_asn1_item_embed_free(&vtmp, ASN1_ITEM_ptr(tt->item), embed);
138
1.01M
        }
139
1.54M
        sk_ASN1_VALUE_free(sk);
140
1.54M
        *pval = NULL;
141
19.8M
    } else {
142
19.8M
        ossl_asn1_item_embed_free(pval, ASN1_ITEM_ptr(tt->item), embed);
143
19.8M
    }
144
21.4M
}
145
146
void ossl_asn1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
147
18.9M
{
148
18.9M
    int utype;
149
150
    /* Special case: if 'it' is a primitive with a free_func, use that. */
151
18.9M
    if (it) {
152
17.7M
        const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
153
154
17.7M
        if (embed) {
155
1.46M
            if (pf && pf->prim_clear) {
156
0
                pf->prim_clear(pval, it);
157
0
                return;
158
0
            }
159
16.2M
        } else if (pf && pf->prim_free) {
160
0
            pf->prim_free(pval, it);
161
0
            return;
162
0
        }
163
17.7M
    }
164
165
    /* Special case: if 'it' is NULL, free contents of ASN1_TYPE */
166
18.9M
    if (!it) {
167
1.20M
        ASN1_TYPE *typ = (ASN1_TYPE *)*pval;
168
169
1.20M
        utype = typ->type;
170
1.20M
        pval = &typ->value.asn1_value;
171
1.20M
        if (*pval == NULL)
172
1.00M
            return;
173
17.7M
    } else if (it->itype == ASN1_ITYPE_MSTRING) {
174
4.50M
        utype = -1;
175
4.50M
        if (*pval == NULL)
176
0
            return;
177
13.2M
    } else {
178
13.2M
        utype = it->utype;
179
13.2M
        if ((utype != V_ASN1_BOOLEAN) && *pval == NULL)
180
2.02M
            return;
181
13.2M
    }
182
183
15.8M
    switch (utype) {
184
6.44M
    case V_ASN1_OBJECT:
185
6.44M
        ASN1_OBJECT_free((ASN1_OBJECT *)*pval);
186
6.44M
        break;
187
188
1.61M
    case V_ASN1_BOOLEAN:
189
1.61M
        if (it)
190
1.61M
            *(ASN1_BOOLEAN *)pval = it->size;
191
0
        else
192
0
            *(ASN1_BOOLEAN *)pval = -1;
193
1.61M
        return;
194
195
0
    case V_ASN1_NULL:
196
0
        break;
197
198
1.20M
    case V_ASN1_ANY:
199
1.20M
        ossl_asn1_primitive_free(pval, NULL, 0);
200
1.20M
        OPENSSL_free(*pval);
201
1.20M
        break;
202
203
6.62M
    default:
204
6.62M
        ossl_asn1_string_embed_free((ASN1_STRING *)*pval, embed);
205
6.62M
        break;
206
15.8M
    }
207
14.2M
    *pval = NULL;
208
14.2M
}