Coverage Report

Created: 2023-09-25 06:45

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