Coverage Report

Created: 2025-08-11 07:04

/src/openssl/crypto/asn1/tasn_utl.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2000-2023 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
668M
#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
6.55M
{
31
6.55M
    int *sel = offset2ptr(*pval, it->utype);
32
33
6.55M
    return *sel;
34
6.55M
}
35
36
int ossl_asn1_get_choice_selector_const(const ASN1_VALUE **pval,
37
                                        const ASN1_ITEM *it)
38
6.73M
{
39
6.73M
    int *sel = offset2ptr(*pval, it->utype);
40
41
6.73M
    return *sel;
42
6.73M
}
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
9.65M
{
51
9.65M
    int *sel, ret;
52
53
9.65M
    sel = offset2ptr(*pval, it->utype);
54
9.65M
    ret = *sel;
55
9.65M
    *sel = value;
56
9.65M
    return ret;
57
9.65M
}
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
93.9M
{
70
93.9M
    const ASN1_AUX *aux;
71
93.9M
    CRYPTO_RWLOCK **lock;
72
93.9M
    CRYPTO_REF_COUNT *refcnt;
73
93.9M
    int ret = -1;
74
75
93.9M
    if ((it->itype != ASN1_ITYPE_SEQUENCE)
76
93.9M
        && (it->itype != ASN1_ITYPE_NDEF_SEQUENCE))
77
0
        return 0;
78
93.9M
    aux = it->funcs;
79
93.9M
    if (aux == NULL || (aux->flags & ASN1_AFLG_REFCOUNT) == 0)
80
92.1M
        return 0;
81
1.82M
    lock = offset2ptr(*pval, aux->ref_lock);
82
1.82M
    refcnt = offset2ptr(*pval, aux->ref_offset);
83
84
1.82M
    switch (op) {
85
737k
    case 0:
86
737k
        if (!CRYPTO_NEW_REF(refcnt, 1))
87
0
            return -1;
88
737k
        *lock = CRYPTO_THREAD_lock_new();
89
737k
        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
737k
        ret = 1;
95
737k
        break;
96
0
    case 1:
97
0
        if (!CRYPTO_UP_REF(refcnt, &ret))
98
0
            return -1;
99
0
        break;
100
1.08M
    case -1:
101
1.08M
        if (!CRYPTO_DOWN_REF(refcnt, &ret))
102
0
            return -1;  /* failed */
103
1.08M
        REF_PRINT_EX(it->sname, ret, (void *)it);
104
1.08M
        REF_ASSERT_ISNT(ret < 0);
105
1.08M
        if (ret == 0) {
106
737k
            CRYPTO_THREAD_lock_free(*lock);
107
737k
            *lock = NULL;
108
737k
            CRYPTO_FREE_REF(refcnt);
109
737k
        }
110
1.08M
        break;
111
1.82M
    }
112
113
1.82M
    return ret;
114
1.82M
}
115
116
static ASN1_ENCODING *asn1_get_enc_ptr(ASN1_VALUE **pval, const ASN1_ITEM *it)
117
146M
{
118
146M
    const ASN1_AUX *aux;
119
120
146M
    if (pval == NULL || *pval == NULL)
121
0
        return NULL;
122
146M
    aux = it->funcs;
123
146M
    if (aux == NULL || (aux->flags & ASN1_AFLG_ENCODING) == 0)
124
143M
        return NULL;
125
2.96M
    return offset2ptr(*pval, aux->enc_offset);
126
146M
}
127
128
static const ASN1_ENCODING *asn1_get_const_enc_ptr(const ASN1_VALUE **pval,
129
                                                   const ASN1_ITEM *it)
130
73.9M
{
131
73.9M
    const ASN1_AUX *aux;
132
133
73.9M
    if (pval == NULL || *pval == NULL)
134
0
        return NULL;
135
73.9M
    aux = it->funcs;
136
73.9M
    if (aux == NULL || (aux->flags & ASN1_AFLG_ENCODING) == 0)
137
72.4M
        return NULL;
138
1.41M
    return offset2ptr(*pval, aux->enc_offset);
139
73.9M
}
140
141
void ossl_asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it)
142
57.4M
{
143
57.4M
    ASN1_ENCODING *enc = asn1_get_enc_ptr(pval, it);
144
145
57.4M
    if (enc != NULL) {
146
1.12M
        enc->enc = NULL;
147
1.12M
        enc->len = 0;
148
1.12M
        enc->modified = 1;
149
1.12M
    }
150
57.4M
}
151
152
void ossl_asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
153
58.3M
{
154
58.3M
    ASN1_ENCODING *enc = asn1_get_enc_ptr(pval, it);
155
156
58.3M
    if (enc != NULL) {
157
1.12M
        OPENSSL_free(enc->enc);
158
1.12M
        enc->enc = NULL;
159
1.12M
        enc->len = 0;
160
1.12M
        enc->modified = 1;
161
1.12M
    }
162
58.3M
}
163
164
int ossl_asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, long inlen,
165
                       const ASN1_ITEM *it)
166
30.1M
{
167
30.1M
    ASN1_ENCODING *enc = asn1_get_enc_ptr(pval, it);
168
169
30.1M
    if (enc == NULL)
170
29.4M
        return 1;
171
172
706k
    OPENSSL_free(enc->enc);
173
706k
    if (inlen <= 0) {
174
0
        enc->enc = NULL;
175
0
        return 0;
176
0
    }
177
706k
    if ((enc->enc = OPENSSL_malloc(inlen)) == NULL)
178
0
        return 0;
179
706k
    memcpy(enc->enc, in, inlen);
180
706k
    enc->len = inlen;
181
706k
    enc->modified = 0;
182
183
706k
    return 1;
184
706k
}
185
186
int ossl_asn1_enc_restore(int *len, unsigned char **out, const ASN1_VALUE **pval,
187
                          const ASN1_ITEM *it)
188
73.9M
{
189
73.9M
    const ASN1_ENCODING *enc = asn1_get_const_enc_ptr(pval, it);
190
191
73.9M
    if (enc == NULL || enc->modified)
192
72.4M
        return 0;
193
1.41M
    if (out) {
194
450k
        memcpy(*out, enc->enc, enc->len);
195
450k
        *out += enc->len;
196
450k
    }
197
1.41M
    if (len != NULL)
198
1.41M
        *len = enc->len;
199
1.41M
    return 1;
200
73.9M
}
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
413M
{
205
413M
    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
413M
    return pvaltmp;
212
413M
}
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
221M
{
218
221M
    return offset2ptr(*pval, tt->offset);
219
221M
}
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
460M
{
230
460M
    const ASN1_ADB *adb;
231
460M
    const ASN1_ADB_TABLE *atbl;
232
460M
    long selector;
233
460M
    const ASN1_VALUE **sfld;
234
460M
    int i;
235
236
460M
    if ((tt->flags & ASN1_TFLG_ADB_MASK) == 0)
237
457M
        return tt;
238
239
    /* Else ANY DEFINED BY ... get the table */
240
2.24M
    adb = ASN1_ADB_ptr(tt->item);
241
242
    /* Get the selector field */
243
2.24M
    sfld = offset2ptr(val, adb->offset);
244
245
    /* Check if NULL */
246
2.24M
    if (*sfld == NULL) {
247
917k
        if (adb->null_tt == NULL)
248
917k
            goto err;
249
0
        return adb->null_tt;
250
917k
    }
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
1.32M
    if ((tt->flags & ASN1_TFLG_ADB_OID) != 0)
257
1.32M
        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
1.32M
    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
9.76M
    for (atbl = adb->tbl, i = 0; i < adb->tblcount; i++, atbl++)
275
8.76M
        if (atbl->value == selector)
276
325k
            return &atbl->tt;
277
278
    /* FIXME: need to search application table too */
279
280
    /* No match, return default type */
281
1.00M
    if (!adb->default_tt)
282
0
        goto err;
283
1.00M
    return adb->default_tt;
284
285
917k
 err:
286
    /* FIXME: should log the value or OID of unsupported type */
287
917k
    if (nullerr)
288
917k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE);
289
917k
    return NULL;
290
1.00M
}