Coverage Report

Created: 2023-06-08 06:41

/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
540k
{
20
540k
    ossl_asn1_item_embed_free(&val, it, 0);
21
540k
}
22
23
void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
24
766
{
25
766
    ossl_asn1_item_embed_free(pval, it, 0);
26
766
}
27
28
void ossl_asn1_item_embed_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
29
1.51M
{
30
1.51M
    const ASN1_TEMPLATE *tt = NULL, *seqtt;
31
1.51M
    const ASN1_EXTERN_FUNCS *ef;
32
1.51M
    const ASN1_AUX *aux = it->funcs;
33
1.51M
    ASN1_aux_cb *asn1_cb;
34
1.51M
    int i;
35
36
1.51M
    if (pval == NULL)
37
0
        return;
38
1.51M
    if ((it->itype != ASN1_ITYPE_PRIMITIVE) && *pval == NULL)
39
291k
        return;
40
1.22M
    if (aux && aux->asn1_cb)
41
54.8k
        asn1_cb = aux->asn1_cb;
42
1.17M
    else
43
1.17M
        asn1_cb = 0;
44
45
1.22M
    switch (it->itype) {
46
47
744k
    case ASN1_ITYPE_PRIMITIVE:
48
744k
        if (it->templates)
49
60.6k
            ossl_asn1_template_free(pval, it->templates);
50
683k
        else
51
683k
            ossl_asn1_primitive_free(pval, it, embed);
52
744k
        break;
53
54
90.9k
    case ASN1_ITYPE_MSTRING:
55
90.9k
        ossl_asn1_primitive_free(pval, it, embed);
56
90.9k
        break;
57
58
20.5k
    case ASN1_ITYPE_CHOICE:
59
20.5k
        if (asn1_cb) {
60
0
            i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
61
0
            if (i == 2)
62
0
                return;
63
0
        }
64
20.5k
        i = ossl_asn1_get_choice_selector(pval, it);
65
20.5k
        if ((i >= 0) && (i < it->tcount)) {
66
20.1k
            ASN1_VALUE **pchval;
67
68
20.1k
            tt = it->templates + i;
69
20.1k
            pchval = ossl_asn1_get_field_ptr(pval, tt);
70
20.1k
            ossl_asn1_template_free(pchval, tt);
71
20.1k
        }
72
20.5k
        if (asn1_cb)
73
0
            asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
74
20.5k
        if (embed == 0) {
75
20.5k
            OPENSSL_free(*pval);
76
20.5k
            *pval = NULL;
77
20.5k
        }
78
20.5k
        break;
79
80
60.4k
    case ASN1_ITYPE_EXTERN:
81
60.4k
        ef = it->funcs;
82
60.4k
        if (ef && ef->asn1_ex_free)
83
60.4k
            ef->asn1_ex_free(pval, it);
84
60.4k
        break;
85
86
0
    case ASN1_ITYPE_NDEF_SEQUENCE:
87
309k
    case ASN1_ITYPE_SEQUENCE:
88
309k
        if (ossl_asn1_do_lock(pval, -1, it) != 0) /* if error or ref-counter > 0 */
89
32.4k
            return;
90
276k
        if (asn1_cb) {
91
15.0k
            i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
92
15.0k
            if (i == 2)
93
0
                return;
94
15.0k
        }
95
276k
        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
276k
        tt = it->templates + it->tcount;
102
1.03M
        for (i = 0; i < it->tcount; i++) {
103
762k
            ASN1_VALUE **pseqval;
104
105
762k
            tt--;
106
762k
            seqtt = ossl_asn1_do_adb(*pval, tt, 0);
107
762k
            if (!seqtt)
108
0
                continue;
109
762k
            pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
110
762k
            ossl_asn1_template_free(pseqval, seqtt);
111
762k
        }
112
276k
        if (asn1_cb)
113
15.0k
            asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
114
276k
        if (embed == 0) {
115
216k
            OPENSSL_free(*pval);
116
216k
            *pval = NULL;
117
216k
        }
118
276k
        break;
119
1.22M
    }
120
1.22M
}
121
122
void ossl_asn1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
123
981k
{
124
981k
    int embed = tt->flags & ASN1_TFLG_EMBED;
125
981k
    ASN1_VALUE *tval;
126
981k
    if (embed) {
127
157k
        tval = (ASN1_VALUE *)pval;
128
157k
        pval = &tval;
129
157k
    }
130
981k
    if (tt->flags & ASN1_TFLG_SK_MASK) {
131
80.7k
        STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
132
80.7k
        int i;
133
134
156k
        for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
135
75.3k
            ASN1_VALUE *vtmp = sk_ASN1_VALUE_value(sk, i);
136
137
75.3k
            ossl_asn1_item_embed_free(&vtmp, ASN1_ITEM_ptr(tt->item), embed);
138
75.3k
        }
139
80.7k
        sk_ASN1_VALUE_free(sk);
140
80.7k
        *pval = NULL;
141
900k
    } else {
142
900k
        ossl_asn1_item_embed_free(pval, ASN1_ITEM_ptr(tt->item), embed);
143
900k
    }
144
981k
}
145
146
void ossl_asn1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
147
829k
{
148
829k
    int utype;
149
150
    /* Special case: if 'it' is a primitive with a free_func, use that. */
151
829k
    if (it) {
152
774k
        const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
153
154
774k
        if (embed) {
155
97.6k
            if (pf && pf->prim_clear) {
156
7.34k
                pf->prim_clear(pval, it);
157
7.34k
                return;
158
7.34k
            }
159
676k
        } else if (pf && pf->prim_free) {
160
0
            pf->prim_free(pval, it);
161
0
            return;
162
0
        }
163
774k
    }
164
165
    /* Special case: if 'it' is NULL, free contents of ASN1_TYPE */
166
822k
    if (!it) {
167
55.2k
        ASN1_TYPE *typ = (ASN1_TYPE *)*pval;
168
169
55.2k
        utype = typ->type;
170
55.2k
        pval = &typ->value.asn1_value;
171
55.2k
        if (*pval == NULL)
172
25.0k
            return;
173
767k
    } else if (it->itype == ASN1_ITYPE_MSTRING) {
174
90.9k
        utype = -1;
175
90.9k
        if (*pval == NULL)
176
0
            return;
177
676k
    } else {
178
676k
        utype = it->utype;
179
676k
        if ((utype != V_ASN1_BOOLEAN) && *pval == NULL)
180
132k
            return;
181
676k
    }
182
183
664k
    switch (utype) {
184
216k
    case V_ASN1_OBJECT:
185
216k
        ASN1_OBJECT_free((ASN1_OBJECT *)*pval);
186
216k
        break;
187
188
140k
    case V_ASN1_BOOLEAN:
189
140k
        if (it)
190
140k
            *(ASN1_BOOLEAN *)pval = it->size;
191
0
        else
192
0
            *(ASN1_BOOLEAN *)pval = -1;
193
140k
        return;
194
195
0
    case V_ASN1_NULL:
196
0
        break;
197
198
55.2k
    case V_ASN1_ANY:
199
55.2k
        ossl_asn1_primitive_free(pval, NULL, 0);
200
55.2k
        OPENSSL_free(*pval);
201
55.2k
        break;
202
203
252k
    default:
204
252k
        ossl_asn1_string_embed_free((ASN1_STRING *)*pval, embed);
205
252k
        break;
206
664k
    }
207
523k
    *pval = NULL;
208
523k
}