Coverage Report

Created: 2023-09-25 06:42

/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.69M
{
20
3.69M
    asn1_item_embed_free(&val, it, 0);
21
3.69M
}
22
23
void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
24
2.14M
{
25
2.14M
    asn1_item_embed_free(pval, it, 0);
26
2.14M
}
27
28
void asn1_item_embed_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
29
19.3M
{
30
19.3M
    const ASN1_TEMPLATE *tt = NULL, *seqtt;
31
19.3M
    const ASN1_EXTERN_FUNCS *ef;
32
19.3M
    const ASN1_AUX *aux = it->funcs;
33
19.3M
    ASN1_aux_cb *asn1_cb;
34
19.3M
    int i;
35
36
19.3M
    if (!pval)
37
0
        return;
38
19.3M
    if ((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval)
39
1.48M
        return;
40
17.9M
    if (aux && aux->asn1_cb)
41
911k
        asn1_cb = aux->asn1_cb;
42
16.9M
    else
43
16.9M
        asn1_cb = 0;
44
45
17.9M
    switch (it->itype) {
46
47
11.3M
    case ASN1_ITYPE_PRIMITIVE:
48
11.3M
        if (it->templates)
49
475k
            asn1_template_free(pval, it->templates);
50
10.8M
        else
51
10.8M
            asn1_primitive_free(pval, it, embed);
52
11.3M
        break;
53
54
1.53M
    case ASN1_ITYPE_MSTRING:
55
1.53M
        asn1_primitive_free(pval, it, embed);
56
1.53M
        break;
57
58
1.24M
    case ASN1_ITYPE_CHOICE:
59
1.24M
        if (asn1_cb) {
60
29.6k
            i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
61
29.6k
            if (i == 2)
62
0
                return;
63
29.6k
        }
64
1.24M
        i = asn1_get_choice_selector(pval, it);
65
1.24M
        if ((i >= 0) && (i < it->tcount)) {
66
999k
            ASN1_VALUE **pchval;
67
68
999k
            tt = it->templates + i;
69
999k
            pchval = asn1_get_field_ptr(pval, tt);
70
999k
            asn1_template_free(pchval, tt);
71
999k
        }
72
1.24M
        if (asn1_cb)
73
29.6k
            asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
74
1.24M
        if (embed == 0) {
75
1.22M
            OPENSSL_free(*pval);
76
1.22M
            *pval = NULL;
77
1.22M
        }
78
1.24M
        break;
79
80
217k
    case ASN1_ITYPE_EXTERN:
81
217k
        ef = it->funcs;
82
217k
        if (ef && ef->asn1_ex_free)
83
217k
            ef->asn1_ex_free(pval, it);
84
217k
        break;
85
86
149k
    case ASN1_ITYPE_NDEF_SEQUENCE:
87
3.60M
    case ASN1_ITYPE_SEQUENCE:
88
3.60M
        if (asn1_do_lock(pval, -1, it) != 0) /* if error or ref-counter > 0 */
89
0
            return;
90
3.60M
        if (asn1_cb) {
91
475k
            i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
92
475k
            if (i == 2)
93
89.7k
                return;
94
475k
        }
95
3.51M
        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.51M
        tt = it->templates + it->tcount;
102
12.8M
        for (i = 0; i < it->tcount; i++) {
103
9.30M
            ASN1_VALUE **pseqval;
104
105
9.30M
            tt--;
106
9.30M
            seqtt = asn1_do_adb(pval, tt, 0);
107
9.30M
            if (!seqtt)
108
69.0k
                continue;
109
9.23M
            pseqval = asn1_get_field_ptr(pval, seqtt);
110
9.23M
            asn1_template_free(pseqval, seqtt);
111
9.23M
        }
112
3.51M
        if (asn1_cb)
113
385k
            asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
114
3.51M
        if (embed == 0) {
115
3.11M
            OPENSSL_free(*pval);
116
3.11M
            *pval = NULL;
117
3.11M
        }
118
3.51M
        break;
119
17.9M
    }
120
17.9M
}
121
122
void asn1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
123
11.6M
{
124
11.6M
    int embed = tt->flags & ASN1_TFLG_EMBED;
125
11.6M
    ASN1_VALUE *tval;
126
11.6M
    if (embed) {
127
874k
        tval = (ASN1_VALUE *)pval;
128
874k
        pval = &tval;
129
874k
    }
130
11.6M
    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
4.53M
        for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
135
3.22M
            ASN1_VALUE *vtmp = sk_ASN1_VALUE_value(sk, i);
136
137
3.22M
            asn1_item_embed_free(&vtmp, ASN1_ITEM_ptr(tt->item), embed);
138
3.22M
        }
139
1.30M
        sk_ASN1_VALUE_free(sk);
140
1.30M
        *pval = NULL;
141
10.3M
    } else {
142
10.3M
        asn1_item_embed_free(pval, ASN1_ITEM_ptr(tt->item), embed);
143
10.3M
    }
144
11.6M
}
145
146
void asn1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
147
15.8M
{
148
15.8M
    int utype;
149
150
    /* Special case: if 'it' is a primitive with a free_func, use that. */
151
15.8M
    if (it) {
152
12.3M
        const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
153
154
12.3M
        if (embed) {
155
459k
            if (pf && pf->prim_clear) {
156
166k
                pf->prim_clear(pval, it);
157
166k
                return;
158
166k
            }
159
11.9M
        } else if (pf && pf->prim_free) {
160
240k
            pf->prim_free(pval, it);
161
240k
            return;
162
240k
        }
163
12.3M
    }
164
165
    /* Special case: if 'it' is NULL, free contents of ASN1_TYPE */
166
15.4M
    if (!it) {
167
3.49M
        ASN1_TYPE *typ = (ASN1_TYPE *)*pval;
168
169
3.49M
        utype = typ->type;
170
3.49M
        pval = &typ->value.asn1_value;
171
3.49M
        if (!*pval)
172
955k
            return;
173
11.9M
    } else if (it->itype == ASN1_ITYPE_MSTRING) {
174
1.53M
        utype = -1;
175
1.53M
        if (!*pval)
176
0
            return;
177
10.4M
    } else {
178
10.4M
        utype = it->utype;
179
10.4M
        if ((utype != V_ASN1_BOOLEAN) && !*pval)
180
2.17M
            return;
181
10.4M
    }
182
183
12.3M
    switch (utype) {
184
2.18M
    case V_ASN1_OBJECT:
185
2.18M
        ASN1_OBJECT_free((ASN1_OBJECT *)*pval);
186
2.18M
        break;
187
188
351k
    case V_ASN1_BOOLEAN:
189
351k
        if (it)
190
350k
            *(ASN1_BOOLEAN *)pval = it->size;
191
969
        else
192
969
            *(ASN1_BOOLEAN *)pval = -1;
193
351k
        return;
194
195
13
    case V_ASN1_NULL:
196
13
        break;
197
198
3.49M
    case V_ASN1_ANY:
199
3.49M
        asn1_primitive_free(pval, NULL, 0);
200
3.49M
        OPENSSL_free(*pval);
201
3.49M
        break;
202
203
6.29M
    default:
204
6.29M
        asn1_string_embed_free((ASN1_STRING *)*pval, embed);
205
6.29M
        break;
206
12.3M
    }
207
11.9M
    *pval = NULL;
208
11.9M
}