Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl36/crypto/asn1/tasn_utl.c
Line
Count
Source
1
/*
2
 * Copyright 2000-2025 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 <string.h>
12
#include "internal/cryptlib.h"
13
#include "internal/refcount.h"
14
#include <openssl/asn1.h>
15
#include <openssl/asn1t.h>
16
#include <openssl/objects.h>
17
#include <openssl/err.h>
18
#include "asn1_local.h"
19
20
/* Utility functions for manipulating fields and offsets */
21
22
/* Add 'offset' to 'addr' */
23
809M
#define offset2ptr(addr, offset) (void *)(((char *)addr) + offset)
24
25
/*
26
 * Given an ASN1_ITEM CHOICE type return the selector value
27
 */
28
29
int ossl_asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it)
30
7.66M
{
31
7.66M
    int *sel = offset2ptr(*pval, it->utype);
32
33
7.66M
    return *sel;
34
7.66M
}
35
36
int ossl_asn1_get_choice_selector_const(const ASN1_VALUE **pval,
37
    const ASN1_ITEM *it)
38
6.78M
{
39
6.78M
    int *sel = offset2ptr(*pval, it->utype);
40
41
6.78M
    return *sel;
42
6.78M
}
43
44
/*
45
 * Given an ASN1_ITEM CHOICE type set the selector value, return old value.
46
 */
47
48
int ossl_asn1_set_choice_selector(ASN1_VALUE **pval, int value,
49
    const ASN1_ITEM *it)
50
11.4M
{
51
11.4M
    int *sel, ret;
52
53
11.4M
    sel = offset2ptr(*pval, it->utype);
54
11.4M
    ret = *sel;
55
11.4M
    *sel = value;
56
11.4M
    return ret;
57
11.4M
}
58
59
/*
60
 * Do atomic reference counting. The value 'op' decides what to do.
61
 * If it is +1 then the count is incremented.
62
 * If |op| is 0, count is initialised and set to 1.
63
 * If |op| is -1, count is decremented and the return value is the current
64
 * reference count or 0 if no reference count is active.
65
 * It returns -1 on initialisation error.
66
 * Used by ASN1_SEQUENCE construct of X509, X509_REQ, X509_CRL objects
67
 */
68
int ossl_asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it)
69
118M
{
70
118M
    const ASN1_AUX *aux;
71
118M
    CRYPTO_RWLOCK **lock;
72
118M
    CRYPTO_REF_COUNT *refcnt;
73
118M
    int ret = -1;
74
75
118M
    if ((it->itype != ASN1_ITYPE_SEQUENCE)
76
1.63M
        && (it->itype != ASN1_ITYPE_NDEF_SEQUENCE))
77
0
        return 0;
78
118M
    aux = it->funcs;
79
118M
    if (aux == NULL || (aux->flags & ASN1_AFLG_REFCOUNT) == 0)
80
116M
        return 0;
81
2.41M
    lock = offset2ptr(*pval, aux->ref_lock);
82
2.41M
    refcnt = offset2ptr(*pval, aux->ref_offset);
83
84
2.41M
    switch (op) {
85
985k
    case 0:
86
985k
        if (!CRYPTO_NEW_REF(refcnt, 1))
87
0
            return -1;
88
985k
        *lock = CRYPTO_THREAD_lock_new();
89
985k
        if (*lock == NULL) {
90
0
            CRYPTO_FREE_REF(refcnt);
91
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_CRYPTO_LIB);
92
0
            return -1;
93
0
        }
94
985k
        ret = 1;
95
985k
        break;
96
0
    case 1:
97
0
        if (!CRYPTO_UP_REF(refcnt, &ret))
98
0
            return -1;
99
0
        break;
100
1.42M
    case -1:
101
1.42M
        if (!CRYPTO_DOWN_REF(refcnt, &ret))
102
0
            return -1; /* failed */
103
1.42M
        REF_PRINT_EX(it->sname, ret, (void *)it);
104
1.42M
        REF_ASSERT_ISNT(ret < 0);
105
1.42M
        if (ret == 0) {
106
985k
            CRYPTO_THREAD_lock_free(*lock);
107
985k
            *lock = NULL;
108
985k
            CRYPTO_FREE_REF(refcnt);
109
985k
        }
110
1.42M
        break;
111
2.41M
    }
112
113
2.41M
    return ret;
114
2.41M
}
115
116
static ASN1_ENCODING *asn1_get_enc_ptr(ASN1_VALUE **pval, const ASN1_ITEM *it)
117
180M
{
118
180M
    const ASN1_AUX *aux;
119
120
180M
    if (pval == NULL || *pval == NULL)
121
0
        return NULL;
122
180M
    aux = it->funcs;
123
180M
    if (aux == NULL || (aux->flags & ASN1_AFLG_ENCODING) == 0)
124
176M
        return NULL;
125
3.75M
    return offset2ptr(*pval, aux->enc_offset);
126
180M
}
127
128
static const ASN1_ENCODING *asn1_get_const_enc_ptr(const ASN1_VALUE **pval,
129
    const ASN1_ITEM *it)
130
89.4M
{
131
89.4M
    const ASN1_AUX *aux;
132
133
89.4M
    if (pval == NULL || *pval == NULL)
134
0
        return NULL;
135
89.4M
    aux = it->funcs;
136
89.4M
    if (aux == NULL || (aux->flags & ASN1_AFLG_ENCODING) == 0)
137
87.7M
        return NULL;
138
1.66M
    return offset2ptr(*pval, aux->enc_offset);
139
89.4M
}
140
141
void ossl_asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it)
142
70.6M
{
143
70.6M
    ASN1_ENCODING *enc = asn1_get_enc_ptr(pval, it);
144
145
70.6M
    if (enc != NULL) {
146
1.40M
        enc->enc = NULL;
147
1.40M
        enc->len = 0;
148
1.40M
        enc->modified = 1;
149
1.40M
    }
150
70.6M
}
151
152
void ossl_asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
153
71.5M
{
154
71.5M
    ASN1_ENCODING *enc = asn1_get_enc_ptr(pval, it);
155
156
71.5M
    if (enc != NULL) {
157
1.40M
        OPENSSL_free(enc->enc);
158
1.40M
        enc->enc = NULL;
159
1.40M
        enc->len = 0;
160
1.40M
        enc->modified = 1;
161
1.40M
    }
162
71.5M
}
163
164
int ossl_asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, long inlen,
165
    const ASN1_ITEM *it)
166
38.5M
{
167
38.5M
    ASN1_ENCODING *enc = asn1_get_enc_ptr(pval, it);
168
169
38.5M
    if (enc == NULL)
170
37.5M
        return 1;
171
172
944k
    OPENSSL_free(enc->enc);
173
944k
    if (inlen <= 0) {
174
0
        enc->enc = NULL;
175
0
        return 0;
176
0
    }
177
944k
    if ((enc->enc = OPENSSL_malloc(inlen)) == NULL)
178
0
        return 0;
179
944k
    memcpy(enc->enc, in, inlen);
180
944k
    enc->len = inlen;
181
944k
    enc->modified = 0;
182
183
944k
    return 1;
184
944k
}
185
186
int ossl_asn1_enc_restore(int *len, unsigned char **out, const ASN1_VALUE **pval,
187
    const ASN1_ITEM *it)
188
89.4M
{
189
89.4M
    const ASN1_ENCODING *enc = asn1_get_const_enc_ptr(pval, it);
190
191
89.4M
    if (enc == NULL || enc->modified)
192
87.7M
        return 0;
193
1.66M
    if (out) {
194
536k
        memcpy(*out, enc->enc, enc->len);
195
536k
        *out += enc->len;
196
536k
    }
197
1.66M
    if (len != NULL)
198
1.66M
        *len = enc->len;
199
1.66M
    return 1;
200
89.4M
}
201
202
/* Given an ASN1_TEMPLATE get a pointer to a field */
203
ASN1_VALUE **ossl_asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
204
506M
{
205
506M
    ASN1_VALUE **pvaltmp = offset2ptr(*pval, tt->offset);
206
207
    /*
208
     * NOTE for BOOLEAN types the field is just a plain int so we can't
209
     * return int **, so settle for (int *).
210
     */
211
506M
    return pvaltmp;
212
506M
}
213
214
/* Given an ASN1_TEMPLATE get a const pointer to a field */
215
const ASN1_VALUE **ossl_asn1_get_const_field_ptr(const ASN1_VALUE **pval,
216
    const ASN1_TEMPLATE *tt)
217
262M
{
218
262M
    return offset2ptr(*pval, tt->offset);
219
262M
}
220
221
/*
222
 * Handle ANY DEFINED BY template, find the selector, look up the relevant
223
 * ASN1_TEMPLATE in the table and return it.
224
 */
225
226
const ASN1_TEMPLATE *ossl_asn1_do_adb(const ASN1_VALUE *val,
227
    const ASN1_TEMPLATE *tt,
228
    int nullerr)
229
559M
{
230
559M
    const ASN1_ADB *adb;
231
559M
    const ASN1_ADB_TABLE *atbl;
232
559M
    long selector;
233
559M
    const ASN1_VALUE **sfld;
234
559M
    int i;
235
236
559M
    if ((tt->flags & ASN1_TFLG_ADB_MASK) == 0)
237
556M
        return tt;
238
239
    /* Else ANY DEFINED BY ... get the table */
240
3.12M
    adb = ASN1_ADB_ptr(tt->item);
241
242
    /* Get the selector field */
243
3.12M
    sfld = offset2ptr(val, adb->offset);
244
245
    /* Check if NULL */
246
3.12M
    if (*sfld == NULL) {
247
1.11M
        if (adb->null_tt == NULL)
248
1.11M
            goto err;
249
0
        return adb->null_tt;
250
1.11M
    }
251
252
    /*
253
     * Convert type to a long: NB: don't check for NID_undef here because it
254
     * might be a legitimate value in the table
255
     */
256
2.00M
    if ((tt->flags & ASN1_TFLG_ADB_OID) != 0)
257
2.00M
        selector = OBJ_obj2nid((ASN1_OBJECT *)*sfld);
258
0
    else
259
0
        selector = ASN1_INTEGER_get((ASN1_INTEGER *)*sfld);
260
261
    /* Let application callback translate value */
262
2.00M
    if (adb->adb_cb != NULL && adb->adb_cb(&selector) == 0) {
263
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE);
264
0
        return NULL;
265
0
    }
266
267
    /*
268
     * Try to find matching entry in table Maybe should check application
269
     * types first to allow application override? Might also be useful to
270
     * have a flag which indicates table is sorted and we can do a binary
271
     * search. For now stick to a linear search.
272
     */
273
274
20.5M
    for (atbl = adb->tbl, i = 0; i < adb->tblcount; i++, atbl++)
275
18.9M
        if (atbl->value == selector)
276
354k
            return &atbl->tt;
277
278
    /* FIXME: need to search application table too */
279
280
    /* No match, return default type */
281
1.65M
    if (!adb->default_tt)
282
0
        goto err;
283
1.65M
    return adb->default_tt;
284
285
1.11M
err:
286
    /* FIXME: should log the value or OID of unsupported type */
287
1.11M
    if (nullerr)
288
1.11M
        ERR_raise(ERR_LIB_ASN1, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE);
289
    return NULL;
290
1.65M
}