Coverage Report

Created: 2025-06-13 06:58

/src/openssl32/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
702M
#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.48M
{
31
6.48M
    int *sel = offset2ptr(*pval, it->utype);
32
33
6.48M
    return *sel;
34
6.48M
}
35
36
int ossl_asn1_get_choice_selector_const(const ASN1_VALUE **pval,
37
                                        const ASN1_ITEM *it)
38
16.9M
{
39
16.9M
    int *sel = offset2ptr(*pval, it->utype);
40
41
16.9M
    return *sel;
42
16.9M
}
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
10.1M
{
51
10.1M
    int *sel, ret;
52
53
10.1M
    sel = offset2ptr(*pval, it->utype);
54
10.1M
    ret = *sel;
55
10.1M
    *sel = value;
56
10.1M
    return ret;
57
10.1M
}
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
59.0M
{
70
59.0M
    const ASN1_AUX *aux;
71
59.0M
    CRYPTO_RWLOCK **lock;
72
59.0M
    CRYPTO_REF_COUNT *refcnt;
73
59.0M
    int ret = -1;
74
75
59.0M
    if ((it->itype != ASN1_ITYPE_SEQUENCE)
76
59.0M
        && (it->itype != ASN1_ITYPE_NDEF_SEQUENCE))
77
0
        return 0;
78
59.0M
    aux = it->funcs;
79
59.0M
    if (aux == NULL || (aux->flags & ASN1_AFLG_REFCOUNT) == 0)
80
57.9M
        return 0;
81
1.08M
    lock = offset2ptr(*pval, aux->ref_lock);
82
1.08M
    refcnt = offset2ptr(*pval, aux->ref_offset);
83
84
1.08M
    switch (op) {
85
467k
    case 0:
86
467k
        if (!CRYPTO_NEW_REF(refcnt, 1))
87
0
            return -1;
88
467k
        *lock = CRYPTO_THREAD_lock_new();
89
467k
        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
467k
        ret = 1;
95
467k
        break;
96
0
    case 1:
97
0
        if (!CRYPTO_UP_REF(refcnt, &ret))
98
0
            return -1;
99
0
        break;
100
617k
    case -1:
101
617k
        if (!CRYPTO_DOWN_REF(refcnt, &ret))
102
0
            return -1;  /* failed */
103
617k
        REF_PRINT_EX(it->sname, ret, (void *)it);
104
617k
        REF_ASSERT_ISNT(ret < 0);
105
617k
        if (ret == 0) {
106
467k
            CRYPTO_THREAD_lock_free(*lock);
107
467k
            *lock = NULL;
108
467k
            CRYPTO_FREE_REF(refcnt);
109
467k
        }
110
617k
        break;
111
1.08M
    }
112
113
1.08M
    return ret;
114
1.08M
}
115
116
static ASN1_ENCODING *asn1_get_enc_ptr(ASN1_VALUE **pval, const ASN1_ITEM *it)
117
147M
{
118
147M
    const ASN1_AUX *aux;
119
120
147M
    if (pval == NULL || *pval == NULL)
121
0
        return NULL;
122
147M
    aux = it->funcs;
123
147M
    if (aux == NULL || (aux->flags & ASN1_AFLG_ENCODING) == 0)
124
144M
        return NULL;
125
2.98M
    return offset2ptr(*pval, aux->enc_offset);
126
147M
}
127
128
static const ASN1_ENCODING *asn1_get_const_enc_ptr(const ASN1_VALUE **pval,
129
                                                   const ASN1_ITEM *it)
130
87.5M
{
131
87.5M
    const ASN1_AUX *aux;
132
133
87.5M
    if (pval == NULL || *pval == NULL)
134
0
        return NULL;
135
87.5M
    aux = it->funcs;
136
87.5M
    if (aux == NULL || (aux->flags & ASN1_AFLG_ENCODING) == 0)
137
86.3M
        return NULL;
138
1.20M
    return offset2ptr(*pval, aux->enc_offset);
139
87.5M
}
140
141
void ossl_asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it)
142
58.0M
{
143
58.0M
    ASN1_ENCODING *enc = asn1_get_enc_ptr(pval, it);
144
145
58.0M
    if (enc != NULL) {
146
1.10M
        enc->enc = NULL;
147
1.10M
        enc->len = 0;
148
1.10M
        enc->modified = 1;
149
1.10M
    }
150
58.0M
}
151
152
void ossl_asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
153
58.8M
{
154
58.8M
    ASN1_ENCODING *enc = asn1_get_enc_ptr(pval, it);
155
156
58.8M
    if (enc != NULL) {
157
1.10M
        OPENSSL_free(enc->enc);
158
1.10M
        enc->enc = NULL;
159
1.10M
        enc->len = 0;
160
1.10M
        enc->modified = 1;
161
1.10M
    }
162
58.8M
}
163
164
int ossl_asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int 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
768k
    OPENSSL_free(enc->enc);
173
768k
    if (inlen <= 0)
174
0
        return 0;
175
768k
    if ((enc->enc = OPENSSL_malloc(inlen)) == NULL)
176
0
        return 0;
177
768k
    memcpy(enc->enc, in, inlen);
178
768k
    enc->len = inlen;
179
768k
    enc->modified = 0;
180
181
768k
    return 1;
182
768k
}
183
184
int ossl_asn1_enc_restore(int *len, unsigned char **out, const ASN1_VALUE **pval,
185
                          const ASN1_ITEM *it)
186
87.5M
{
187
87.5M
    const ASN1_ENCODING *enc = asn1_get_const_enc_ptr(pval, it);
188
189
87.5M
    if (enc == NULL || enc->modified)
190
86.3M
        return 0;
191
1.20M
    if (out) {
192
378k
        memcpy(*out, enc->enc, enc->len);
193
378k
        *out += enc->len;
194
378k
    }
195
1.20M
    if (len != NULL)
196
1.20M
        *len = enc->len;
197
1.20M
    return 1;
198
87.5M
}
199
200
/* Given an ASN1_TEMPLATE get a pointer to a field */
201
ASN1_VALUE **ossl_asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
202
397M
{
203
397M
    ASN1_VALUE **pvaltmp = offset2ptr(*pval, tt->offset);
204
205
    /*
206
     * NOTE for BOOLEAN types the field is just a plain int so we can't
207
     * return int **, so settle for (int *).
208
     */
209
397M
    return pvaltmp;
210
397M
}
211
212
/* Given an ASN1_TEMPLATE get a const pointer to a field */
213
const ASN1_VALUE **ossl_asn1_get_const_field_ptr(const ASN1_VALUE **pval,
214
                                                 const ASN1_TEMPLATE *tt)
215
263M
{
216
263M
    return offset2ptr(*pval, tt->offset);
217
263M
}
218
219
/*
220
 * Handle ANY DEFINED BY template, find the selector, look up the relevant
221
 * ASN1_TEMPLATE in the table and return it.
222
 */
223
224
const ASN1_TEMPLATE *ossl_asn1_do_adb(const ASN1_VALUE *val,
225
                                      const ASN1_TEMPLATE *tt,
226
                                      int nullerr)
227
478M
{
228
478M
    const ASN1_ADB *adb;
229
478M
    const ASN1_ADB_TABLE *atbl;
230
478M
    long selector;
231
478M
    const ASN1_VALUE **sfld;
232
478M
    int i;
233
234
478M
    if ((tt->flags & ASN1_TFLG_ADB_MASK) == 0)
235
476M
        return tt;
236
237
    /* Else ANY DEFINED BY ... get the table */
238
2.02M
    adb = ASN1_ADB_ptr(tt->item);
239
240
    /* Get the selector field */
241
2.02M
    sfld = offset2ptr(val, adb->offset);
242
243
    /* Check if NULL */
244
2.02M
    if (*sfld == NULL) {
245
777k
        if (adb->null_tt == NULL)
246
777k
            goto err;
247
0
        return adb->null_tt;
248
777k
    }
249
250
    /*
251
     * Convert type to a long: NB: don't check for NID_undef here because it
252
     * might be a legitimate value in the table
253
     */
254
1.25M
    if ((tt->flags & ASN1_TFLG_ADB_OID) != 0)
255
1.25M
        selector = OBJ_obj2nid((ASN1_OBJECT *)*sfld);
256
0
    else
257
0
        selector = ASN1_INTEGER_get((ASN1_INTEGER *)*sfld);
258
259
    /* Let application callback translate value */
260
1.25M
    if (adb->adb_cb != NULL && adb->adb_cb(&selector) == 0) {
261
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE);
262
0
        return NULL;
263
0
    }
264
265
    /*
266
     * Try to find matching entry in table Maybe should check application
267
     * types first to allow application override? Might also be useful to
268
     * have a flag which indicates table is sorted and we can do a binary
269
     * search. For now stick to a linear search.
270
     */
271
272
8.36M
    for (atbl = adb->tbl, i = 0; i < adb->tblcount; i++, atbl++)
273
7.42M
        if (atbl->value == selector)
274
309k
            return &atbl->tt;
275
276
    /* FIXME: need to search application table too */
277
278
    /* No match, return default type */
279
942k
    if (!adb->default_tt)
280
0
        goto err;
281
942k
    return adb->default_tt;
282
283
777k
 err:
284
    /* FIXME: should log the value or OID of unsupported type */
285
777k
    if (nullerr)
286
777k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE);
287
777k
    return NULL;
288
942k
}