Coverage Report

Created: 2023-09-25 06:45

/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
31.4M
{
20
31.4M
    asn1_item_embed_free(&val, it, 0);
21
31.4M
}
22
23
void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
24
7.63M
{
25
7.63M
    asn1_item_embed_free(pval, it, 0);
26
7.63M
}
27
28
void asn1_item_embed_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
29
48.6M
{
30
48.6M
    const ASN1_TEMPLATE *tt = NULL, *seqtt;
31
48.6M
    const ASN1_EXTERN_FUNCS *ef;
32
48.6M
    const ASN1_AUX *aux = it->funcs;
33
48.6M
    ASN1_aux_cb *asn1_cb;
34
48.6M
    int i;
35
36
48.6M
    if (!pval)
37
0
        return;
38
48.6M
    if ((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval)
39
2.75M
        return;
40
45.8M
    if (aux && aux->asn1_cb)
41
1.25M
        asn1_cb = aux->asn1_cb;
42
44.6M
    else
43
44.6M
        asn1_cb = 0;
44
45
45.8M
    switch (it->itype) {
46
47
24.9M
    case ASN1_ITYPE_PRIMITIVE:
48
24.9M
        if (it->templates)
49
3.33M
            asn1_template_free(pval, it->templates);
50
21.5M
        else
51
21.5M
            asn1_primitive_free(pval, it, embed);
52
24.9M
        break;
53
54
7.92M
    case ASN1_ITYPE_MSTRING:
55
7.92M
        asn1_primitive_free(pval, it, embed);
56
7.92M
        break;
57
58
1.54M
    case ASN1_ITYPE_CHOICE:
59
1.54M
        if (asn1_cb) {
60
84.0k
            i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
61
84.0k
            if (i == 2)
62
0
                return;
63
84.0k
        }
64
1.54M
        i = asn1_get_choice_selector(pval, it);
65
1.54M
        if ((i >= 0) && (i < it->tcount)) {
66
1.22M
            ASN1_VALUE **pchval;
67
68
1.22M
            tt = it->templates + i;
69
1.22M
            pchval = asn1_get_field_ptr(pval, tt);
70
1.22M
            asn1_template_free(pchval, tt);
71
1.22M
        }
72
1.54M
        if (asn1_cb)
73
84.0k
            asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
74
1.54M
        if (embed == 0) {
75
1.52M
            OPENSSL_free(*pval);
76
1.52M
            *pval = NULL;
77
1.52M
        }
78
1.54M
        break;
79
80
418k
    case ASN1_ITYPE_EXTERN:
81
418k
        ef = it->funcs;
82
418k
        if (ef && ef->asn1_ex_free)
83
418k
            ef->asn1_ex_free(pval, it);
84
418k
        break;
85
86
169k
    case ASN1_ITYPE_NDEF_SEQUENCE:
87
11.0M
    case ASN1_ITYPE_SEQUENCE:
88
11.0M
        if (asn1_do_lock(pval, -1, it) != 0) /* if error or ref-counter > 0 */
89
46.9k
            return;
90
11.0M
        if (asn1_cb) {
91
696k
            i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
92
696k
            if (i == 2)
93
91.5k
                return;
94
696k
        }
95
10.9M
        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
10.9M
        tt = it->templates + it->tcount;
102
36.6M
        for (i = 0; i < it->tcount; i++) {
103
25.6M
            ASN1_VALUE **pseqval;
104
105
25.6M
            tt--;
106
25.6M
            seqtt = asn1_do_adb(pval, tt, 0);
107
25.6M
            if (!seqtt)
108
71.6k
                continue;
109
25.6M
            pseqval = asn1_get_field_ptr(pval, seqtt);
110
25.6M
            asn1_template_free(pseqval, seqtt);
111
25.6M
        }
112
10.9M
        if (asn1_cb)
113
604k
            asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
114
10.9M
        if (embed == 0) {
115
10.1M
            OPENSSL_free(*pval);
116
10.1M
            *pval = NULL;
117
10.1M
        }
118
10.9M
        break;
119
45.8M
    }
120
45.8M
}
121
122
void asn1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
123
32.2M
{
124
32.2M
    int embed = tt->flags & ASN1_TFLG_EMBED;
125
32.2M
    ASN1_VALUE *tval;
126
32.2M
    if (embed) {
127
1.79M
        tval = (ASN1_VALUE *)pval;
128
1.79M
        pval = &tval;
129
1.79M
    }
130
32.2M
    if (tt->flags & ASN1_TFLG_SK_MASK) {
131
4.60M
        STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
132
4.60M
        int i;
133
134
11.8M
        for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
135
7.25M
            ASN1_VALUE *vtmp = sk_ASN1_VALUE_value(sk, i);
136
137
7.25M
            asn1_item_embed_free(&vtmp, ASN1_ITEM_ptr(tt->item), embed);
138
7.25M
        }
139
4.60M
        sk_ASN1_VALUE_free(sk);
140
4.60M
        *pval = NULL;
141
27.6M
    } else {
142
27.6M
        asn1_item_embed_free(pval, ASN1_ITEM_ptr(tt->item), embed);
143
27.6M
    }
144
32.2M
}
145
146
void asn1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
147
33.9M
{
148
33.9M
    int utype;
149
150
    /* Special case: if 'it' is a primitive with a free_func, use that. */
151
33.9M
    if (it) {
152
29.5M
        const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
153
154
29.5M
        if (embed) {
155
1.00M
            if (pf && pf->prim_clear) {
156
191k
                pf->prim_clear(pval, it);
157
191k
                return;
158
191k
            }
159
28.4M
        } else if (pf && pf->prim_free) {
160
241k
            pf->prim_free(pval, it);
161
241k
            return;
162
241k
        }
163
29.5M
    }
164
165
    /* Special case: if 'it' is NULL, free contents of ASN1_TYPE */
166
33.5M
    if (!it) {
167
4.47M
        ASN1_TYPE *typ = (ASN1_TYPE *)*pval;
168
169
4.47M
        utype = typ->type;
170
4.47M
        pval = &typ->value.asn1_value;
171
4.47M
        if (!*pval)
172
993k
            return;
173
29.0M
    } else if (it->itype == ASN1_ITYPE_MSTRING) {
174
7.92M
        utype = -1;
175
7.92M
        if (!*pval)
176
0
            return;
177
21.1M
    } else {
178
21.1M
        utype = it->utype;
179
21.1M
        if ((utype != V_ASN1_BOOLEAN) && !*pval)
180
3.22M
            return;
181
21.1M
    }
182
183
29.3M
    switch (utype) {
184
9.10M
    case V_ASN1_OBJECT:
185
9.10M
        ASN1_OBJECT_free((ASN1_OBJECT *)*pval);
186
9.10M
        break;
187
188
1.34M
    case V_ASN1_BOOLEAN:
189
1.34M
        if (it)
190
1.34M
            *(ASN1_BOOLEAN *)pval = it->size;
191
4.32k
        else
192
4.32k
            *(ASN1_BOOLEAN *)pval = -1;
193
1.34M
        return;
194
195
1.50k
    case V_ASN1_NULL:
196
1.50k
        break;
197
198
4.47M
    case V_ASN1_ANY:
199
4.47M
        asn1_primitive_free(pval, NULL, 0);
200
4.47M
        OPENSSL_free(*pval);
201
4.47M
        break;
202
203
14.3M
    default:
204
14.3M
        asn1_string_embed_free((ASN1_STRING *)*pval, embed);
205
14.3M
        break;
206
29.3M
    }
207
27.9M
    *pval = NULL;
208
27.9M
}