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