Coverage Report

Created: 2024-07-27 06:35

/src/openssl/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
656k
{
20
656k
    ossl_asn1_item_embed_free(&val, it, 0);
21
656k
}
22
23
void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
24
278
{
25
278
    ossl_asn1_item_embed_free(pval, it, 0);
26
278
}
27
28
void ossl_asn1_item_embed_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
29
1.99M
{
30
1.99M
    const ASN1_TEMPLATE *tt = NULL, *seqtt;
31
1.99M
    const ASN1_EXTERN_FUNCS *ef;
32
1.99M
    const ASN1_AUX *aux = it->funcs;
33
1.99M
    ASN1_aux_cb *asn1_cb;
34
1.99M
    int i;
35
36
1.99M
    if (pval == NULL)
37
0
        return;
38
1.99M
    if ((it->itype != ASN1_ITYPE_PRIMITIVE) && *pval == NULL)
39
264k
        return;
40
1.73M
    if (aux && aux->asn1_cb)
41
98.9k
        asn1_cb = aux->asn1_cb;
42
1.63M
    else
43
1.63M
        asn1_cb = 0;
44
45
1.73M
    switch (it->itype) {
46
47
976k
    case ASN1_ITYPE_PRIMITIVE:
48
976k
        if (it->templates)
49
55.0k
            ossl_asn1_template_free(pval, it->templates);
50
920k
        else
51
920k
            ossl_asn1_primitive_free(pval, it, embed);
52
976k
        break;
53
54
192k
    case ASN1_ITYPE_MSTRING:
55
192k
        ossl_asn1_primitive_free(pval, it, embed);
56
192k
        break;
57
58
13.9k
    case ASN1_ITYPE_CHOICE:
59
13.9k
        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
13.9k
        i = ossl_asn1_get_choice_selector(pval, it);
65
13.9k
        if ((i >= 0) && (i < it->tcount)) {
66
13.7k
            ASN1_VALUE **pchval;
67
68
13.7k
            tt = it->templates + i;
69
13.7k
            pchval = ossl_asn1_get_field_ptr(pval, tt);
70
13.7k
            ossl_asn1_template_free(pchval, tt);
71
13.7k
        }
72
13.9k
        if (asn1_cb)
73
0
            asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
74
13.9k
        if (embed == 0) {
75
13.9k
            OPENSSL_free(*pval);
76
13.9k
            *pval = NULL;
77
13.9k
        }
78
13.9k
        break;
79
80
54.9k
    case ASN1_ITYPE_EXTERN:
81
54.9k
        ef = it->funcs;
82
54.9k
        if (ef && ef->asn1_ex_free)
83
54.9k
            ef->asn1_ex_free(pval, it);
84
54.9k
        break;
85
86
0
    case ASN1_ITYPE_NDEF_SEQUENCE:
87
492k
    case ASN1_ITYPE_SEQUENCE:
88
492k
        if (ossl_asn1_do_lock(pval, -1, it) != 0) /* if error or ref-counter > 0 */
89
30.1k
            return;
90
462k
        if (asn1_cb) {
91
64.1k
            i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
92
64.1k
            if (i == 2)
93
0
                return;
94
64.1k
        }
95
462k
        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
462k
        tt = it->templates + it->tcount;
102
1.67M
        for (i = 0; i < it->tcount; i++) {
103
1.21M
            ASN1_VALUE **pseqval;
104
105
1.21M
            tt--;
106
1.21M
            seqtt = ossl_asn1_do_adb(*pval, tt, 0);
107
1.21M
            if (!seqtt)
108
0
                continue;
109
1.21M
            pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
110
1.21M
            ossl_asn1_template_free(pseqval, seqtt);
111
1.21M
        }
112
462k
        if (asn1_cb)
113
64.1k
            asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
114
462k
        if (embed == 0) {
115
407k
            OPENSSL_free(*pval);
116
407k
            *pval = NULL;
117
407k
        }
118
462k
        break;
119
1.73M
    }
120
1.73M
}
121
122
void ossl_asn1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
123
1.46M
{
124
1.46M
    int embed = tt->flags & ASN1_TFLG_EMBED;
125
1.46M
    ASN1_VALUE *tval;
126
1.46M
    if (embed) {
127
137k
        tval = (ASN1_VALUE *)pval;
128
137k
        pval = &tval;
129
137k
    }
130
1.46M
    if (tt->flags & ASN1_TFLG_SK_MASK) {
131
183k
        STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
132
183k
        int i;
133
134
242k
        for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
135
59.5k
            ASN1_VALUE *vtmp = sk_ASN1_VALUE_value(sk, i);
136
137
59.5k
            ossl_asn1_item_embed_free(&vtmp, ASN1_ITEM_ptr(tt->item), embed);
138
59.5k
        }
139
183k
        sk_ASN1_VALUE_free(sk);
140
183k
        *pval = NULL;
141
1.27M
    } else {
142
1.27M
        ossl_asn1_item_embed_free(pval, ASN1_ITEM_ptr(tt->item), embed);
143
1.27M
    }
144
1.46M
}
145
146
void ossl_asn1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
147
1.21M
{
148
1.21M
    int utype;
149
150
    /* Special case: if 'it' is a primitive with a free_func, use that. */
151
1.21M
    if (it) {
152
1.11M
        const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
153
154
1.11M
        if (embed) {
155
82.5k
            if (pf && pf->prim_clear) {
156
4.58k
                pf->prim_clear(pval, it);
157
4.58k
                return;
158
4.58k
            }
159
1.03M
        } else if (pf && pf->prim_free) {
160
0
            pf->prim_free(pval, it);
161
0
            return;
162
0
        }
163
1.11M
    }
164
165
    /* Special case: if 'it' is NULL, free contents of ASN1_TYPE */
166
1.20M
    if (!it) {
167
100k
        ASN1_TYPE *typ = (ASN1_TYPE *)*pval;
168
169
100k
        utype = typ->type;
170
100k
        pval = &typ->value.asn1_value;
171
100k
        if (*pval == NULL)
172
73.2k
            return;
173
1.10M
    } else if (it->itype == ASN1_ITYPE_MSTRING) {
174
192k
        utype = -1;
175
192k
        if (*pval == NULL)
176
0
            return;
177
916k
    } else {
178
916k
        utype = it->utype;
179
916k
        if ((utype != V_ASN1_BOOLEAN) && *pval == NULL)
180
128k
            return;
181
916k
    }
182
183
1.00M
    switch (utype) {
184
352k
    case V_ASN1_OBJECT:
185
352k
        ASN1_OBJECT_free((ASN1_OBJECT *)*pval);
186
352k
        break;
187
188
119k
    case V_ASN1_BOOLEAN:
189
119k
        if (it)
190
119k
            *(ASN1_BOOLEAN *)pval = it->size;
191
0
        else
192
0
            *(ASN1_BOOLEAN *)pval = -1;
193
119k
        return;
194
195
0
    case V_ASN1_NULL:
196
0
        break;
197
198
100k
    case V_ASN1_ANY:
199
100k
        ossl_asn1_primitive_free(pval, NULL, 0);
200
100k
        OPENSSL_free(*pval);
201
100k
        break;
202
203
435k
    default:
204
435k
        ossl_asn1_string_embed_free((ASN1_STRING *)*pval, embed);
205
435k
        break;
206
1.00M
    }
207
888k
    *pval = NULL;
208
888k
}