Coverage Report

Created: 2023-06-08 06:40

/src/openssl30/crypto/asn1/tasn_fre.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2000-2021 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (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
317k
{
20
317k
    ossl_asn1_item_embed_free(&val, it, 0);
21
317k
}
22
23
void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
24
49.3k
{
25
49.3k
    ossl_asn1_item_embed_free(pval, it, 0);
26
49.3k
}
27
28
void ossl_asn1_item_embed_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
29
2.76M
{
30
2.76M
    const ASN1_TEMPLATE *tt = NULL, *seqtt;
31
2.76M
    const ASN1_EXTERN_FUNCS *ef;
32
2.76M
    const ASN1_AUX *aux = it->funcs;
33
2.76M
    ASN1_aux_cb *asn1_cb;
34
2.76M
    int i;
35
36
2.76M
    if (pval == NULL)
37
0
        return;
38
2.76M
    if ((it->itype != ASN1_ITYPE_PRIMITIVE) && *pval == NULL)
39
120k
        return;
40
2.64M
    if (aux && aux->asn1_cb)
41
12.8k
        asn1_cb = aux->asn1_cb;
42
2.63M
    else
43
2.63M
        asn1_cb = 0;
44
45
2.64M
    switch (it->itype) {
46
47
2.09M
    case ASN1_ITYPE_PRIMITIVE:
48
2.09M
        if (it->templates)
49
1.26M
            ossl_asn1_template_free(pval, it->templates);
50
836k
        else
51
836k
            ossl_asn1_primitive_free(pval, it, embed);
52
2.09M
        break;
53
54
130k
    case ASN1_ITYPE_MSTRING:
55
130k
        ossl_asn1_primitive_free(pval, it, embed);
56
130k
        break;
57
58
32.8k
    case ASN1_ITYPE_CHOICE:
59
32.8k
        if (asn1_cb) {
60
1.41k
            i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
61
1.41k
            if (i == 2)
62
0
                return;
63
1.41k
        }
64
32.8k
        i = ossl_asn1_get_choice_selector(pval, it);
65
32.8k
        if ((i >= 0) && (i < it->tcount)) {
66
20.4k
            ASN1_VALUE **pchval;
67
68
20.4k
            tt = it->templates + i;
69
20.4k
            pchval = ossl_asn1_get_field_ptr(pval, tt);
70
20.4k
            ossl_asn1_template_free(pchval, tt);
71
20.4k
        }
72
32.8k
        if (asn1_cb)
73
1.41k
            asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
74
32.8k
        if (embed == 0) {
75
32.8k
            OPENSSL_free(*pval);
76
32.8k
            *pval = NULL;
77
32.8k
        }
78
32.8k
        break;
79
80
40.9k
    case ASN1_ITYPE_EXTERN:
81
40.9k
        ef = it->funcs;
82
40.9k
        if (ef && ef->asn1_ex_free)
83
40.9k
            ef->asn1_ex_free(pval, it);
84
40.9k
        break;
85
86
0
    case ASN1_ITYPE_NDEF_SEQUENCE:
87
347k
    case ASN1_ITYPE_SEQUENCE:
88
347k
        if (ossl_asn1_do_lock(pval, -1, it) != 0) /* if error or ref-counter > 0 */
89
0
            return;
90
347k
        if (asn1_cb) {
91
10.1k
            i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
92
10.1k
            if (i == 2)
93
208
                return;
94
10.1k
        }
95
347k
        ossl_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
347k
        tt = it->templates + it->tcount;
102
1.26M
        for (i = 0; i < it->tcount; i++) {
103
914k
            ASN1_VALUE **pseqval;
104
105
914k
            tt--;
106
914k
            seqtt = ossl_asn1_do_adb(*pval, tt, 0);
107
914k
            if (!seqtt)
108
586
                continue;
109
913k
            pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
110
913k
            ossl_asn1_template_free(pseqval, seqtt);
111
913k
        }
112
347k
        if (asn1_cb)
113
9.89k
            asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
114
347k
        if (embed == 0) {
115
307k
            OPENSSL_free(*pval);
116
307k
            *pval = NULL;
117
307k
        }
118
347k
        break;
119
2.64M
    }
120
2.64M
}
121
122
void ossl_asn1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
123
2.37M
{
124
2.37M
    int embed = tt->flags & ASN1_TFLG_EMBED;
125
2.37M
    ASN1_VALUE *tval;
126
2.37M
    if (embed) {
127
166k
        tval = (ASN1_VALUE *)pval;
128
166k
        pval = &tval;
129
166k
    }
130
2.37M
    if (tt->flags & ASN1_TFLG_SK_MASK) {
131
1.32M
        STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
132
1.32M
        int i;
133
134
2.66M
        for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
135
1.34M
            ASN1_VALUE *vtmp = sk_ASN1_VALUE_value(sk, i);
136
137
1.34M
            ossl_asn1_item_embed_free(&vtmp, ASN1_ITEM_ptr(tt->item), embed);
138
1.34M
        }
139
1.32M
        sk_ASN1_VALUE_free(sk);
140
1.32M
        *pval = NULL;
141
1.32M
    } else {
142
1.05M
        ossl_asn1_item_embed_free(pval, ASN1_ITEM_ptr(tt->item), embed);
143
1.05M
    }
144
2.37M
}
145
146
void ossl_asn1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
147
979k
{
148
979k
    int utype;
149
150
    /* Special case: if 'it' is a primitive with a free_func, use that. */
151
979k
    if (it) {
152
967k
        const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
153
154
967k
        if (embed) {
155
127k
            if (pf && pf->prim_clear) {
156
1.21k
                pf->prim_clear(pval, it);
157
1.21k
                return;
158
1.21k
            }
159
839k
        } else if (pf && pf->prim_free) {
160
66
            pf->prim_free(pval, it);
161
66
            return;
162
66
        }
163
967k
    }
164
165
    /* Special case: if 'it' is NULL, free contents of ASN1_TYPE */
166
977k
    if (!it) {
167
12.2k
        ASN1_TYPE *typ = (ASN1_TYPE *)*pval;
168
169
12.2k
        utype = typ->type;
170
12.2k
        pval = &typ->value.asn1_value;
171
12.2k
        if (*pval == NULL)
172
2.49k
            return;
173
965k
    } else if (it->itype == ASN1_ITYPE_MSTRING) {
174
130k
        utype = -1;
175
130k
        if (*pval == NULL)
176
0
            return;
177
835k
    } else {
178
835k
        utype = it->utype;
179
835k
        if ((utype != V_ASN1_BOOLEAN) && *pval == NULL)
180
130k
            return;
181
835k
    }
182
183
845k
    switch (utype) {
184
262k
    case V_ASN1_OBJECT:
185
262k
        ASN1_OBJECT_free((ASN1_OBJECT *)*pval);
186
262k
        break;
187
188
233k
    case V_ASN1_BOOLEAN:
189
233k
        if (it)
190
233k
            *(ASN1_BOOLEAN *)pval = it->size;
191
13
        else
192
13
            *(ASN1_BOOLEAN *)pval = -1;
193
233k
        return;
194
195
891
    case V_ASN1_NULL:
196
891
        break;
197
198
12.2k
    case V_ASN1_ANY:
199
12.2k
        ossl_asn1_primitive_free(pval, NULL, 0);
200
12.2k
        OPENSSL_free(*pval);
201
12.2k
        break;
202
203
336k
    default:
204
336k
        ossl_asn1_string_embed_free((ASN1_STRING *)*pval, embed);
205
336k
        break;
206
845k
    }
207
611k
    *pval = NULL;
208
611k
}