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