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
2.33M
{
20
2.33M
    asn1_item_embed_free(&val, it, 0);
21
2.33M
}
22
23
void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
24
60.1k
{
25
60.1k
    asn1_item_embed_free(pval, it, 0);
26
60.1k
}
27
28
void asn1_item_embed_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
29
8.58M
{
30
8.58M
    const ASN1_TEMPLATE *tt = NULL, *seqtt;
31
8.58M
    const ASN1_EXTERN_FUNCS *ef;
32
8.58M
    const ASN1_AUX *aux = it->funcs;
33
8.58M
    ASN1_aux_cb *asn1_cb;
34
8.58M
    int i;
35
36
8.58M
    if (!pval)
37
0
        return;
38
8.58M
    if ((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval)
39
150k
        return;
40
8.43M
    if (aux && aux->asn1_cb)
41
44.3k
        asn1_cb = aux->asn1_cb;
42
8.38M
    else
43
8.38M
        asn1_cb = 0;
44
45
8.43M
    switch (it->itype) {
46
47
3.68M
    case ASN1_ITYPE_PRIMITIVE:
48
3.68M
        if (it->templates)
49
673k
            asn1_template_free(pval, it->templates);
50
3.00M
        else
51
3.00M
            asn1_primitive_free(pval, it, embed);
52
3.68M
        break;
53
54
2.20M
    case ASN1_ITYPE_MSTRING:
55
2.20M
        asn1_primitive_free(pval, it, embed);
56
2.20M
        break;
57
58
86.5k
    case ASN1_ITYPE_CHOICE:
59
86.5k
        if (asn1_cb) {
60
26.5k
            i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
61
26.5k
            if (i == 2)
62
0
                return;
63
26.5k
        }
64
86.5k
        i = asn1_get_choice_selector(pval, it);
65
86.5k
        if ((i >= 0) && (i < it->tcount)) {
66
46.7k
            ASN1_VALUE **pchval;
67
68
46.7k
            tt = it->templates + i;
69
46.7k
            pchval = asn1_get_field_ptr(pval, tt);
70
46.7k
            asn1_template_free(pchval, tt);
71
46.7k
        }
72
86.5k
        if (asn1_cb)
73
26.5k
            asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
74
86.5k
        if (embed == 0) {
75
86.5k
            OPENSSL_free(*pval);
76
86.5k
            *pval = NULL;
77
86.5k
        }
78
86.5k
        break;
79
80
27.2k
    case ASN1_ITYPE_EXTERN:
81
27.2k
        ef = it->funcs;
82
27.2k
        if (ef && ef->asn1_ex_free)
83
27.2k
            ef->asn1_ex_free(pval, it);
84
27.2k
        break;
85
86
0
    case ASN1_ITYPE_NDEF_SEQUENCE:
87
2.42M
    case ASN1_ITYPE_SEQUENCE:
88
2.42M
        if (asn1_do_lock(pval, -1, it) != 0) /* if error or ref-counter > 0 */
89
0
            return;
90
2.42M
        if (asn1_cb) {
91
17.6k
            i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
92
17.6k
            if (i == 2)
93
0
                return;
94
17.6k
        }
95
2.42M
        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
2.42M
        tt = it->templates + it->tcount;
102
7.60M
        for (i = 0; i < it->tcount; i++) {
103
5.17M
            ASN1_VALUE **pseqval;
104
105
5.17M
            tt--;
106
5.17M
            seqtt = asn1_do_adb(pval, tt, 0);
107
5.17M
            if (!seqtt)
108
372
                continue;
109
5.17M
            pseqval = asn1_get_field_ptr(pval, seqtt);
110
5.17M
            asn1_template_free(pseqval, seqtt);
111
5.17M
        }
112
2.42M
        if (asn1_cb)
113
17.6k
            asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
114
2.42M
        if (embed == 0) {
115
2.40M
            OPENSSL_free(*pval);
116
2.40M
            *pval = NULL;
117
2.40M
        }
118
2.42M
        break;
119
8.43M
    }
120
8.43M
}
121
122
void asn1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
123
6.16M
{
124
6.16M
    int embed = tt->flags & ASN1_TFLG_EMBED;
125
6.16M
    ASN1_VALUE *tval;
126
6.16M
    if (embed) {
127
159k
        tval = (ASN1_VALUE *)pval;
128
159k
        pval = &tval;
129
159k
    }
130
6.16M
    if (tt->flags & ASN1_TFLG_SK_MASK) {
131
805k
        STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
132
805k
        int i;
133
134
1.64M
        for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
135
835k
            ASN1_VALUE *vtmp = sk_ASN1_VALUE_value(sk, i);
136
137
835k
            asn1_item_embed_free(&vtmp, ASN1_ITEM_ptr(tt->item), embed);
138
835k
        }
139
805k
        sk_ASN1_VALUE_free(sk);
140
805k
        *pval = NULL;
141
5.35M
    } else {
142
5.35M
        asn1_item_embed_free(pval, ASN1_ITEM_ptr(tt->item), embed);
143
5.35M
    }
144
6.16M
}
145
146
void asn1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
147
5.22M
{
148
5.22M
    int utype;
149
150
    /* Special case: if 'it' is a primitive with a free_func, use that. */
151
5.22M
    if (it) {
152
5.21M
        const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
153
154
5.21M
        if (embed) {
155
133k
            if (pf && pf->prim_clear) {
156
0
                pf->prim_clear(pval, it);
157
0
                return;
158
0
            }
159
5.08M
        } else if (pf && pf->prim_free) {
160
112
            pf->prim_free(pval, it);
161
112
            return;
162
112
        }
163
5.21M
    }
164
165
    /* Special case: if 'it' is NULL, free contents of ASN1_TYPE */
166
5.22M
    if (!it) {
167
9.18k
        ASN1_TYPE *typ = (ASN1_TYPE *)*pval;
168
169
9.18k
        utype = typ->type;
170
9.18k
        pval = &typ->value.asn1_value;
171
9.18k
        if (!*pval)
172
2.82k
            return;
173
5.21M
    } else if (it->itype == ASN1_ITYPE_MSTRING) {
174
2.20M
        utype = -1;
175
2.20M
        if (!*pval)
176
0
            return;
177
3.00M
    } else {
178
3.00M
        utype = it->utype;
179
3.00M
        if ((utype != V_ASN1_BOOLEAN) && !*pval)
180
145k
            return;
181
3.00M
    }
182
183
5.07M
    switch (utype) {
184
2.32M
    case V_ASN1_OBJECT:
185
2.32M
        ASN1_OBJECT_free((ASN1_OBJECT *)*pval);
186
2.32M
        break;
187
188
345k
    case V_ASN1_BOOLEAN:
189
345k
        if (it)
190
345k
            *(ASN1_BOOLEAN *)pval = it->size;
191
74
        else
192
74
            *(ASN1_BOOLEAN *)pval = -1;
193
345k
        return;
194
195
907
    case V_ASN1_NULL:
196
907
        break;
197
198
9.18k
    case V_ASN1_ANY:
199
9.18k
        asn1_primitive_free(pval, NULL, 0);
200
9.18k
        OPENSSL_free(*pval);
201
9.18k
        break;
202
203
2.39M
    default:
204
2.39M
        asn1_string_embed_free((ASN1_STRING *)*pval, embed);
205
2.39M
        break;
206
5.07M
    }
207
4.73M
    *pval = NULL;
208
4.73M
}