Coverage Report

Created: 2023-09-25 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
1.86M
{
20
1.86M
    ossl_asn1_item_embed_free(&val, it, 0);
21
1.86M
}
22
23
void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
24
36.7k
{
25
36.7k
    ossl_asn1_item_embed_free(pval, it, 0);
26
36.7k
}
27
28
void ossl_asn1_item_embed_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
29
7.25M
{
30
7.25M
    const ASN1_TEMPLATE *tt = NULL, *seqtt;
31
7.25M
    const ASN1_EXTERN_FUNCS *ef;
32
7.25M
    const ASN1_AUX *aux = it->funcs;
33
7.25M
    ASN1_aux_cb *asn1_cb;
34
7.25M
    int i;
35
36
7.25M
    if (pval == NULL)
37
0
        return;
38
7.25M
    if ((it->itype != ASN1_ITYPE_PRIMITIVE) && *pval == NULL)
39
747k
        return;
40
6.50M
    if (aux && aux->asn1_cb)
41
163k
        asn1_cb = aux->asn1_cb;
42
6.34M
    else
43
6.34M
        asn1_cb = 0;
44
45
6.50M
    switch (it->itype) {
46
47
3.76M
    case ASN1_ITYPE_PRIMITIVE:
48
3.76M
        if (it->templates)
49
399k
            ossl_asn1_template_free(pval, it->templates);
50
3.36M
        else
51
3.36M
            ossl_asn1_primitive_free(pval, it, embed);
52
3.76M
        break;
53
54
865k
    case ASN1_ITYPE_MSTRING:
55
865k
        ossl_asn1_primitive_free(pval, it, embed);
56
865k
        break;
57
58
171k
    case ASN1_ITYPE_CHOICE:
59
171k
        if (asn1_cb) {
60
14.7k
            i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
61
14.7k
            if (i == 2)
62
0
                return;
63
14.7k
        }
64
171k
        i = ossl_asn1_get_choice_selector(pval, it);
65
171k
        if ((i >= 0) && (i < it->tcount)) {
66
151k
            ASN1_VALUE **pchval;
67
68
151k
            tt = it->templates + i;
69
151k
            pchval = ossl_asn1_get_field_ptr(pval, tt);
70
151k
            ossl_asn1_template_free(pchval, tt);
71
151k
        }
72
171k
        if (asn1_cb)
73
14.7k
            asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
74
171k
        if (embed == 0) {
75
171k
            OPENSSL_free(*pval);
76
171k
            *pval = NULL;
77
171k
        }
78
171k
        break;
79
80
252k
    case ASN1_ITYPE_EXTERN:
81
252k
        ef = it->funcs;
82
252k
        if (ef && ef->asn1_ex_free)
83
252k
            ef->asn1_ex_free(pval, it);
84
252k
        break;
85
86
23.5k
    case ASN1_ITYPE_NDEF_SEQUENCE:
87
1.45M
    case ASN1_ITYPE_SEQUENCE:
88
1.45M
        if (ossl_asn1_do_lock(pval, -1, it) != 0) /* if error or ref-counter > 0 */
89
0
            return;
90
1.45M
        if (asn1_cb) {
91
116k
            i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
92
116k
            if (i == 2)
93
1.59k
                return;
94
116k
        }
95
1.45M
        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
1.45M
        tt = it->templates + it->tcount;
102
5.18M
        for (i = 0; i < it->tcount; i++) {
103
3.73M
            ASN1_VALUE **pseqval;
104
105
3.73M
            tt--;
106
3.73M
            seqtt = ossl_asn1_do_adb(*pval, tt, 0);
107
3.73M
            if (!seqtt)
108
1.63k
                continue;
109
3.73M
            pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
110
3.73M
            ossl_asn1_template_free(pseqval, seqtt);
111
3.73M
        }
112
1.45M
        if (asn1_cb)
113
114k
            asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
114
1.45M
        if (embed == 0) {
115
1.15M
            OPENSSL_free(*pval);
116
1.15M
            *pval = NULL;
117
1.15M
        }
118
1.45M
        break;
119
6.50M
    }
120
6.50M
}
121
122
void ossl_asn1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
123
4.85M
{
124
4.85M
    int embed = tt->flags & ASN1_TFLG_EMBED;
125
4.85M
    ASN1_VALUE *tval;
126
4.85M
    if (embed) {
127
486k
        tval = (ASN1_VALUE *)pval;
128
486k
        pval = &tval;
129
486k
    }
130
4.85M
    if (tt->flags & ASN1_TFLG_SK_MASK) {
131
638k
        STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
132
638k
        int i;
133
134
1.76M
        for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
135
1.12M
            ASN1_VALUE *vtmp = sk_ASN1_VALUE_value(sk, i);
136
137
1.12M
            ossl_asn1_item_embed_free(&vtmp, ASN1_ITEM_ptr(tt->item), embed);
138
1.12M
        }
139
638k
        sk_ASN1_VALUE_free(sk);
140
638k
        *pval = NULL;
141
4.21M
    } else {
142
4.21M
        ossl_asn1_item_embed_free(pval, ASN1_ITEM_ptr(tt->item), embed);
143
4.21M
    }
144
4.85M
}
145
146
void ossl_asn1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
147
5.16M
{
148
5.16M
    int utype;
149
150
    /* Special case: if 'it' is a primitive with a free_func, use that. */
151
5.16M
    if (it) {
152
4.22M
        const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
153
154
4.22M
        if (embed) {
155
191k
            if (pf && pf->prim_clear) {
156
22.9k
                pf->prim_clear(pval, it);
157
22.9k
                return;
158
22.9k
            }
159
4.03M
        } else if (pf && pf->prim_free) {
160
9.26k
            pf->prim_free(pval, it);
161
9.26k
            return;
162
9.26k
        }
163
4.22M
    }
164
165
    /* Special case: if 'it' is NULL, free contents of ASN1_TYPE */
166
5.13M
    if (!it) {
167
937k
        ASN1_TYPE *typ = (ASN1_TYPE *)*pval;
168
169
937k
        utype = typ->type;
170
937k
        pval = &typ->value.asn1_value;
171
937k
        if (*pval == NULL)
172
9.13k
            return;
173
4.19M
    } else if (it->itype == ASN1_ITYPE_MSTRING) {
174
865k
        utype = -1;
175
865k
        if (*pval == NULL)
176
0
            return;
177
3.32M
    } else {
178
3.32M
        utype = it->utype;
179
3.32M
        if ((utype != V_ASN1_BOOLEAN) && *pval == NULL)
180
834k
            return;
181
3.32M
    }
182
183
4.28M
    switch (utype) {
184
1.20M
    case V_ASN1_OBJECT:
185
1.20M
        ASN1_OBJECT_free((ASN1_OBJECT *)*pval);
186
1.20M
        break;
187
188
117k
    case V_ASN1_BOOLEAN:
189
117k
        if (it)
190
113k
            *(ASN1_BOOLEAN *)pval = it->size;
191
3.86k
        else
192
3.86k
            *(ASN1_BOOLEAN *)pval = -1;
193
117k
        return;
194
195
0
    case V_ASN1_NULL:
196
0
        break;
197
198
937k
    case V_ASN1_ANY:
199
937k
        ossl_asn1_primitive_free(pval, NULL, 0);
200
937k
        OPENSSL_free(*pval);
201
937k
        break;
202
203
2.03M
    default:
204
2.03M
        ossl_asn1_string_embed_free((ASN1_STRING *)*pval, embed);
205
2.03M
        break;
206
4.28M
    }
207
4.17M
    *pval = NULL;
208
4.17M
}