Coverage Report

Created: 2023-09-25 06:41

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