Coverage Report

Created: 2023-06-08 06:41

/src/openssl111/crypto/asn1/tasn_fre.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the OpenSSL license (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
2.84M
{
20
2.84M
    asn1_item_embed_free(&val, it, 0);
21
2.84M
}
22
23
void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
24
2.17M
{
25
2.17M
    asn1_item_embed_free(pval, it, 0);
26
2.17M
}
27
28
void asn1_item_embed_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
29
18.0M
{
30
18.0M
    const ASN1_TEMPLATE *tt = NULL, *seqtt;
31
18.0M
    const ASN1_EXTERN_FUNCS *ef;
32
18.0M
    const ASN1_AUX *aux = it->funcs;
33
18.0M
    ASN1_aux_cb *asn1_cb;
34
18.0M
    int i;
35
36
18.0M
    if (!pval)
37
0
        return;
38
18.0M
    if ((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval)
39
1.51M
        return;
40
16.5M
    if (aux && aux->asn1_cb)
41
921k
        asn1_cb = aux->asn1_cb;
42
15.6M
    else
43
15.6M
        asn1_cb = 0;
44
45
16.5M
    switch (it->itype) {
46
47
9.80M
    case ASN1_ITYPE_PRIMITIVE:
48
9.80M
        if (it->templates)
49
460k
            asn1_template_free(pval, it->templates);
50
9.34M
        else
51
9.34M
            asn1_primitive_free(pval, it, embed);
52
9.80M
        break;
53
54
1.41M
    case ASN1_ITYPE_MSTRING:
55
1.41M
        asn1_primitive_free(pval, it, embed);
56
1.41M
        break;
57
58
1.64M
    case ASN1_ITYPE_CHOICE:
59
1.64M
        if (asn1_cb) {
60
29.8k
            i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
61
29.8k
            if (i == 2)
62
0
                return;
63
29.8k
        }
64
1.64M
        i = asn1_get_choice_selector(pval, it);
65
1.64M
        if ((i >= 0) && (i < it->tcount)) {
66
1.39M
            ASN1_VALUE **pchval;
67
68
1.39M
            tt = it->templates + i;
69
1.39M
            pchval = asn1_get_field_ptr(pval, tt);
70
1.39M
            asn1_template_free(pchval, tt);
71
1.39M
        }
72
1.64M
        if (asn1_cb)
73
29.8k
            asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
74
1.64M
        if (embed == 0) {
75
1.62M
            OPENSSL_free(*pval);
76
1.62M
            *pval = NULL;
77
1.62M
        }
78
1.64M
        break;
79
80
219k
    case ASN1_ITYPE_EXTERN:
81
219k
        ef = it->funcs;
82
219k
        if (ef && ef->asn1_ex_free)
83
219k
            ef->asn1_ex_free(pval, it);
84
219k
        break;
85
86
150k
    case ASN1_ITYPE_NDEF_SEQUENCE:
87
3.50M
    case ASN1_ITYPE_SEQUENCE:
88
3.50M
        if (asn1_do_lock(pval, -1, it) != 0) /* if error or ref-counter > 0 */
89
0
            return;
90
3.50M
        if (asn1_cb) {
91
479k
            i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
92
479k
            if (i == 2)
93
90.5k
                return;
94
479k
        }
95
3.41M
        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
3.41M
        tt = it->templates + it->tcount;
102
12.5M
        for (i = 0; i < it->tcount; i++) {
103
9.14M
            ASN1_VALUE **pseqval;
104
105
9.14M
            tt--;
106
9.14M
            seqtt = asn1_do_adb(pval, tt, 0);
107
9.14M
            if (!seqtt)
108
70.4k
                continue;
109
9.06M
            pseqval = asn1_get_field_ptr(pval, seqtt);
110
9.06M
            asn1_template_free(pseqval, seqtt);
111
9.06M
        }
112
3.41M
        if (asn1_cb)
113
389k
            asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
114
3.41M
        if (embed == 0) {
115
3.01M
            OPENSSL_free(*pval);
116
3.01M
            *pval = NULL;
117
3.01M
        }
118
3.41M
        break;
119
16.5M
    }
120
16.5M
}
121
122
void asn1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
123
11.8M
{
124
11.8M
    int embed = tt->flags & ASN1_TFLG_EMBED;
125
11.8M
    ASN1_VALUE *tval;
126
11.8M
    if (embed) {
127
886k
        tval = (ASN1_VALUE *)pval;
128
886k
        pval = &tval;
129
886k
    }
130
11.8M
    if (tt->flags & ASN1_TFLG_SK_MASK) {
131
1.30M
        STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
132
1.30M
        int i;
133
134
3.81M
        for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
135
2.51M
            ASN1_VALUE *vtmp = sk_ASN1_VALUE_value(sk, i);
136
137
2.51M
            asn1_item_embed_free(&vtmp, ASN1_ITEM_ptr(tt->item), embed);
138
2.51M
        }
139
1.30M
        sk_ASN1_VALUE_free(sk);
140
1.30M
        *pval = NULL;
141
10.5M
    } else {
142
10.5M
        asn1_item_embed_free(pval, ASN1_ITEM_ptr(tt->item), embed);
143
10.5M
    }
144
11.8M
}
145
146
void asn1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
147
12.4M
{
148
12.4M
    int utype;
149
150
    /* Special case: if 'it' is a primitive with a free_func, use that. */
151
12.4M
    if (it) {
152
10.7M
        const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
153
154
10.7M
        if (embed) {
155
467k
            if (pf && pf->prim_clear) {
156
167k
                pf->prim_clear(pval, it);
157
167k
                return;
158
167k
            }
159
10.2M
        } else if (pf && pf->prim_free) {
160
243k
            pf->prim_free(pval, it);
161
243k
            return;
162
243k
        }
163
10.7M
    }
164
165
    /* Special case: if 'it' is NULL, free contents of ASN1_TYPE */
166
12.0M
    if (!it) {
167
1.66M
        ASN1_TYPE *typ = (ASN1_TYPE *)*pval;
168
169
1.66M
        utype = typ->type;
170
1.66M
        pval = &typ->value.asn1_value;
171
1.66M
        if (!*pval)
172
301k
            return;
173
10.3M
    } else if (it->itype == ASN1_ITYPE_MSTRING) {
174
1.41M
        utype = -1;
175
1.41M
        if (!*pval)
176
0
            return;
177
8.93M
    } else {
178
8.93M
        utype = it->utype;
179
8.93M
        if ((utype != V_ASN1_BOOLEAN) && !*pval)
180
2.19M
            return;
181
8.93M
    }
182
183
9.51M
    switch (utype) {
184
2.07M
    case V_ASN1_OBJECT:
185
2.07M
        ASN1_OBJECT_free((ASN1_OBJECT *)*pval);
186
2.07M
        break;
187
188
357k
    case V_ASN1_BOOLEAN:
189
357k
        if (it)
190
356k
            *(ASN1_BOOLEAN *)pval = it->size;
191
1.05k
        else
192
1.05k
            *(ASN1_BOOLEAN *)pval = -1;
193
357k
        return;
194
195
13
    case V_ASN1_NULL:
196
13
        break;
197
198
1.66M
    case V_ASN1_ANY:
199
1.66M
        asn1_primitive_free(pval, NULL, 0);
200
1.66M
        OPENSSL_free(*pval);
201
1.66M
        break;
202
203
5.41M
    default:
204
5.41M
        asn1_string_embed_free((ASN1_STRING *)*pval, embed);
205
5.41M
        break;
206
9.51M
    }
207
9.15M
    *pval = NULL;
208
9.15M
}