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