Coverage Report

Created: 2023-09-25 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
3.93M
{
20
3.93M
    asn1_item_embed_free(&val, it, 0);
21
3.93M
}
22
23
void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
24
28.4k
{
25
28.4k
    asn1_item_embed_free(pval, it, 0);
26
28.4k
}
27
28
void asn1_item_embed_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
29
13.6M
{
30
13.6M
    const ASN1_TEMPLATE *tt = NULL, *seqtt;
31
13.6M
    const ASN1_EXTERN_FUNCS *ef;
32
13.6M
    const ASN1_AUX *aux = it->funcs;
33
13.6M
    ASN1_aux_cb *asn1_cb;
34
13.6M
    int i;
35
36
13.6M
    if (!pval)
37
0
        return;
38
13.6M
    if ((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval)
39
474k
        return;
40
13.2M
    if (aux && aux->asn1_cb)
41
173k
        asn1_cb = aux->asn1_cb;
42
13.0M
    else
43
13.0M
        asn1_cb = 0;
44
45
13.2M
    switch (it->itype) {
46
47
5.66M
    case ASN1_ITYPE_PRIMITIVE:
48
5.66M
        if (it->templates)
49
212k
            asn1_template_free(pval, it->templates);
50
5.45M
        else
51
5.45M
            asn1_primitive_free(pval, it, embed);
52
5.66M
        break;
53
54
3.48M
    case ASN1_ITYPE_MSTRING:
55
3.48M
        asn1_primitive_free(pval, it, embed);
56
3.48M
        break;
57
58
103k
    case ASN1_ITYPE_CHOICE:
59
103k
        if (asn1_cb) {
60
19.0k
            i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
61
19.0k
            if (i == 2)
62
0
                return;
63
19.0k
        }
64
103k
        i = asn1_get_choice_selector(pval, it);
65
103k
        if ((i >= 0) && (i < it->tcount)) {
66
89.3k
            ASN1_VALUE **pchval;
67
68
89.3k
            tt = it->templates + i;
69
89.3k
            pchval = asn1_get_field_ptr(pval, tt);
70
89.3k
            asn1_template_free(pchval, tt);
71
89.3k
        }
72
103k
        if (asn1_cb)
73
19.0k
            asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
74
103k
        if (embed == 0) {
75
103k
            OPENSSL_free(*pval);
76
103k
            *pval = NULL;
77
103k
        }
78
103k
        break;
79
80
105k
    case ASN1_ITYPE_EXTERN:
81
105k
        ef = it->funcs;
82
105k
        if (ef && ef->asn1_ex_free)
83
105k
            ef->asn1_ex_free(pval, it);
84
105k
        break;
85
86
19.9k
    case ASN1_ITYPE_NDEF_SEQUENCE:
87
3.85M
    case ASN1_ITYPE_SEQUENCE:
88
3.85M
        if (asn1_do_lock(pval, -1, it) != 0) /* if error or ref-counter > 0 */
89
0
            return;
90
3.85M
        if (asn1_cb) {
91
138k
            i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
92
138k
            if (i == 2)
93
1.18k
                return;
94
138k
        }
95
3.85M
        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.85M
        tt = it->templates + it->tcount;
102
12.1M
        for (i = 0; i < it->tcount; i++) {
103
8.34M
            ASN1_VALUE **pseqval;
104
105
8.34M
            tt--;
106
8.34M
            seqtt = asn1_do_adb(pval, tt, 0);
107
8.34M
            if (!seqtt)
108
1.26k
                continue;
109
8.34M
            pseqval = asn1_get_field_ptr(pval, seqtt);
110
8.34M
            asn1_template_free(pseqval, seqtt);
111
8.34M
        }
112
3.85M
        if (asn1_cb)
113
137k
            asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
114
3.85M
        if (embed == 0) {
115
3.63M
            OPENSSL_free(*pval);
116
3.63M
            *pval = NULL;
117
3.63M
        }
118
3.85M
        break;
119
13.2M
    }
120
13.2M
}
121
122
void asn1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
123
9.08M
{
124
9.08M
    int embed = tt->flags & ASN1_TFLG_EMBED;
125
9.08M
    ASN1_VALUE *tval;
126
9.08M
    if (embed) {
127
368k
        tval = (ASN1_VALUE *)pval;
128
368k
        pval = &tval;
129
368k
    }
130
9.08M
    if (tt->flags & ASN1_TFLG_SK_MASK) {
131
419k
        STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
132
419k
        int i;
133
134
1.48M
        for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
135
1.06M
            ASN1_VALUE *vtmp = sk_ASN1_VALUE_value(sk, i);
136
137
1.06M
            asn1_item_embed_free(&vtmp, ASN1_ITEM_ptr(tt->item), embed);
138
1.06M
        }
139
419k
        sk_ASN1_VALUE_free(sk);
140
419k
        *pval = NULL;
141
8.66M
    } else {
142
8.66M
        asn1_item_embed_free(pval, ASN1_ITEM_ptr(tt->item), embed);
143
8.66M
    }
144
9.08M
}
145
146
void asn1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
147
9.86M
{
148
9.86M
    int utype;
149
150
    /* Special case: if 'it' is a primitive with a free_func, use that. */
151
9.86M
    if (it) {
152
8.94M
        const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
153
154
8.94M
        if (embed) {
155
148k
            if (pf && pf->prim_clear) {
156
15.1k
                pf->prim_clear(pval, it);
157
15.1k
                return;
158
15.1k
            }
159
8.79M
        } else if (pf && pf->prim_free) {
160
584
            pf->prim_free(pval, it);
161
584
            return;
162
584
        }
163
8.94M
    }
164
165
    /* Special case: if 'it' is NULL, free contents of ASN1_TYPE */
166
9.85M
    if (!it) {
167
922k
        ASN1_TYPE *typ = (ASN1_TYPE *)*pval;
168
169
922k
        utype = typ->type;
170
922k
        pval = &typ->value.asn1_value;
171
922k
        if (!*pval)
172
10.0k
            return;
173
8.92M
    } else if (it->itype == ASN1_ITYPE_MSTRING) {
174
3.48M
        utype = -1;
175
3.48M
        if (!*pval)
176
0
            return;
177
5.43M
    } else {
178
5.43M
        utype = it->utype;
179
5.43M
        if ((utype != V_ASN1_BOOLEAN) && !*pval)
180
551k
            return;
181
5.43M
    }
182
183
9.28M
    switch (utype) {
184
3.64M
    case V_ASN1_OBJECT:
185
3.64M
        ASN1_OBJECT_free((ASN1_OBJECT *)*pval);
186
3.64M
        break;
187
188
160k
    case V_ASN1_BOOLEAN:
189
160k
        if (it)
190
157k
            *(ASN1_BOOLEAN *)pval = it->size;
191
3.23k
        else
192
3.23k
            *(ASN1_BOOLEAN *)pval = -1;
193
160k
        return;
194
195
0
    case V_ASN1_NULL:
196
0
        break;
197
198
922k
    case V_ASN1_ANY:
199
922k
        asn1_primitive_free(pval, NULL, 0);
200
922k
        OPENSSL_free(*pval);
201
922k
        break;
202
203
4.56M
    default:
204
4.56M
        asn1_string_embed_free((ASN1_STRING *)*pval, embed);
205
4.56M
        break;
206
9.28M
    }
207
9.12M
    *pval = NULL;
208
9.12M
}