Coverage Report

Created: 2025-08-26 06:04

/src/rauc/subprojects/openssl-3.0.8/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
44.6k
{
20
44.6k
    ossl_asn1_item_embed_free(&val, it, 0);
21
44.6k
}
22
23
void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
24
2.14k
{
25
2.14k
    ossl_asn1_item_embed_free(pval, it, 0);
26
2.14k
}
27
28
void ossl_asn1_item_embed_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
29
170k
{
30
170k
    const ASN1_TEMPLATE *tt = NULL, *seqtt;
31
170k
    const ASN1_EXTERN_FUNCS *ef;
32
170k
    const ASN1_AUX *aux = it->funcs;
33
170k
    ASN1_aux_cb *asn1_cb;
34
170k
    int i;
35
36
170k
    if (pval == NULL)
37
0
        return;
38
170k
    if ((it->itype != ASN1_ITYPE_PRIMITIVE) && *pval == NULL)
39
15.8k
        return;
40
155k
    if (aux && aux->asn1_cb)
41
14.2k
        asn1_cb = aux->asn1_cb;
42
140k
    else
43
140k
        asn1_cb = 0;
44
45
155k
    switch (it->itype) {
46
47
86.3k
    case ASN1_ITYPE_PRIMITIVE:
48
86.3k
        if (it->templates)
49
6.92k
            ossl_asn1_template_free(pval, it->templates);
50
79.4k
        else
51
79.4k
            ossl_asn1_primitive_free(pval, it, embed);
52
86.3k
        break;
53
54
16.0k
    case ASN1_ITYPE_MSTRING:
55
16.0k
        ossl_asn1_primitive_free(pval, it, embed);
56
16.0k
        break;
57
58
3.26k
    case ASN1_ITYPE_CHOICE:
59
3.26k
        if (asn1_cb) {
60
295
            i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
61
295
            if (i == 2)
62
0
                return;
63
295
        }
64
3.26k
        i = ossl_asn1_get_choice_selector(pval, it);
65
3.26k
        if ((i >= 0) && (i < it->tcount)) {
66
2.28k
            ASN1_VALUE **pchval;
67
68
2.28k
            tt = it->templates + i;
69
2.28k
            pchval = ossl_asn1_get_field_ptr(pval, tt);
70
2.28k
            ossl_asn1_template_free(pchval, tt);
71
2.28k
        }
72
3.26k
        if (asn1_cb)
73
295
            asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
74
3.26k
        if (embed == 0) {
75
3.26k
            OPENSSL_free(*pval);
76
3.26k
            *pval = NULL;
77
3.26k
        }
78
3.26k
        break;
79
80
4.72k
    case ASN1_ITYPE_EXTERN:
81
4.72k
        ef = it->funcs;
82
4.72k
        if (ef && ef->asn1_ex_free)
83
4.72k
            ef->asn1_ex_free(pval, it);
84
4.72k
        break;
85
86
12.2k
    case ASN1_ITYPE_NDEF_SEQUENCE:
87
44.7k
    case ASN1_ITYPE_SEQUENCE:
88
44.7k
        if (ossl_asn1_do_lock(pval, -1, it) != 0) /* if error or ref-counter > 0 */
89
0
            return;
90
44.7k
        if (asn1_cb) {
91
11.4k
            i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
92
11.4k
            if (i == 2)
93
109
                return;
94
11.4k
        }
95
44.6k
        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
44.6k
        tt = it->templates + it->tcount;
102
156k
        for (i = 0; i < it->tcount; i++) {
103
111k
            ASN1_VALUE **pseqval;
104
105
111k
            tt--;
106
111k
            seqtt = ossl_asn1_do_adb(*pval, tt, 0);
107
111k
            if (!seqtt)
108
226
                continue;
109
111k
            pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
110
111k
            ossl_asn1_template_free(pseqval, seqtt);
111
111k
        }
112
44.6k
        if (asn1_cb)
113
11.3k
            asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
114
44.6k
        if (embed == 0) {
115
39.6k
            OPENSSL_free(*pval);
116
39.6k
            *pval = NULL;
117
39.6k
        }
118
44.6k
        break;
119
155k
    }
120
155k
}
121
122
void ossl_asn1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
123
130k
{
124
130k
    int embed = tt->flags & ASN1_TFLG_EMBED;
125
130k
    ASN1_VALUE *tval;
126
130k
    if (embed) {
127
10.7k
        tval = (ASN1_VALUE *)pval;
128
10.7k
        pval = &tval;
129
10.7k
    }
130
130k
    if (tt->flags & ASN1_TFLG_SK_MASK) {
131
17.5k
        STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
132
17.5k
        int i;
133
134
29.3k
        for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
135
11.7k
            ASN1_VALUE *vtmp = sk_ASN1_VALUE_value(sk, i);
136
137
11.7k
            ossl_asn1_item_embed_free(&vtmp, ASN1_ITEM_ptr(tt->item), embed);
138
11.7k
        }
139
17.5k
        sk_ASN1_VALUE_free(sk);
140
17.5k
        *pval = NULL;
141
112k
    } else {
142
112k
        ossl_asn1_item_embed_free(pval, ASN1_ITEM_ptr(tt->item), embed);
143
112k
    }
144
130k
}
145
146
void ossl_asn1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
147
109k
{
148
109k
    int utype;
149
150
    /* Special case: if 'it' is a primitive with a free_func, use that. */
151
109k
    if (it) {
152
95.4k
        const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
153
154
95.4k
        if (embed) {
155
5.79k
            if (pf && pf->prim_clear) {
156
2.46k
                pf->prim_clear(pval, it);
157
2.46k
                return;
158
2.46k
            }
159
89.6k
        } else if (pf && pf->prim_free) {
160
24
            pf->prim_free(pval, it);
161
24
            return;
162
24
        }
163
95.4k
    }
164
165
    /* Special case: if 'it' is NULL, free contents of ASN1_TYPE */
166
106k
    if (!it) {
167
13.9k
        ASN1_TYPE *typ = (ASN1_TYPE *)*pval;
168
169
13.9k
        utype = typ->type;
170
13.9k
        pval = &typ->value.asn1_value;
171
13.9k
        if (*pval == NULL)
172
413
            return;
173
93.0k
    } else if (it->itype == ASN1_ITYPE_MSTRING) {
174
16.0k
        utype = -1;
175
16.0k
        if (*pval == NULL)
176
0
            return;
177
76.9k
    } else {
178
76.9k
        utype = it->utype;
179
76.9k
        if ((utype != V_ASN1_BOOLEAN) && *pval == NULL)
180
16.0k
            return;
181
76.9k
    }
182
183
90.5k
    switch (utype) {
184
36.7k
    case V_ASN1_OBJECT:
185
36.7k
        ASN1_OBJECT_free((ASN1_OBJECT *)*pval);
186
36.7k
        break;
187
188
1.88k
    case V_ASN1_BOOLEAN:
189
1.88k
        if (it)
190
1.65k
            *(ASN1_BOOLEAN *)pval = it->size;
191
225
        else
192
225
            *(ASN1_BOOLEAN *)pval = -1;
193
1.88k
        return;
194
195
0
    case V_ASN1_NULL:
196
0
        break;
197
198
13.9k
    case V_ASN1_ANY:
199
13.9k
        ossl_asn1_primitive_free(pval, NULL, 0);
200
13.9k
        OPENSSL_free(*pval);
201
13.9k
        break;
202
203
37.9k
    default:
204
37.9k
        ossl_asn1_string_embed_free((ASN1_STRING *)*pval, embed);
205
37.9k
        break;
206
90.5k
    }
207
88.6k
    *pval = NULL;
208
88.6k
}