Coverage Report

Created: 2023-06-08 06:41

/src/openssl111/crypto/asn1/tasn_utl.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2000-2018 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the OpenSSL license (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
9.79M
#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 asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it)
30
88.8k
{
31
88.8k
    int *sel = offset2ptr(*pval, it->utype);
32
88.8k
    return *sel;
33
88.8k
}
34
35
/*
36
 * Given an ASN1_ITEM CHOICE type set the selector value, return old value.
37
 */
38
39
int asn1_set_choice_selector(ASN1_VALUE **pval, int value,
40
                             const ASN1_ITEM *it)
41
129k
{
42
129k
    int *sel, ret;
43
129k
    sel = offset2ptr(*pval, it->utype);
44
129k
    ret = *sel;
45
129k
    *sel = value;
46
129k
    return ret;
47
129k
}
48
49
/*
50
 * Do atomic reference counting. The value 'op' decides what to do.
51
 * If it is +1 then the count is incremented.
52
 * If |op| is 0, lock is initialised and count is set to 1.
53
 * If |op| is -1, count is decremented and the return value is the current
54
 * reference count or 0 if no reference count is active.
55
 * It returns -1 on initialisation error.
56
 * Used by ASN1_SEQUENCE construct of X509, X509_REQ, X509_CRL objects
57
 */
58
int asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it)
59
2.02M
{
60
2.02M
    const ASN1_AUX *aux;
61
2.02M
    CRYPTO_REF_COUNT *lck;
62
2.02M
    CRYPTO_RWLOCK **lock;
63
2.02M
    int ret = -1;
64
65
2.02M
    if ((it->itype != ASN1_ITYPE_SEQUENCE)
66
2.02M
        && (it->itype != ASN1_ITYPE_NDEF_SEQUENCE))
67
0
        return 0;
68
2.02M
    aux = it->funcs;
69
2.02M
    if (!aux || !(aux->flags & ASN1_AFLG_REFCOUNT))
70
2.00M
        return 0;
71
19.5k
    lck = offset2ptr(*pval, aux->ref_offset);
72
19.5k
    lock = offset2ptr(*pval, aux->ref_lock);
73
74
19.5k
    switch (op) {
75
9.76k
    case 0:
76
9.76k
        *lck = ret = 1;
77
9.76k
        *lock = CRYPTO_THREAD_lock_new();
78
9.76k
        if (*lock == NULL) {
79
0
            ASN1err(ASN1_F_ASN1_DO_LOCK, ERR_R_MALLOC_FAILURE);
80
0
            return -1;
81
0
        }
82
9.76k
        break;
83
9.76k
    case 1:
84
0
        if (!CRYPTO_UP_REF(lck, &ret, *lock))
85
0
            return -1;
86
0
        break;
87
9.76k
    case -1:
88
9.76k
        if (!CRYPTO_DOWN_REF(lck, &ret, *lock))
89
0
            return -1;  /* failed */
90
#ifdef REF_PRINT
91
        fprintf(stderr, "%p:%4d:%s\n", it, ret, it->sname);
92
#endif
93
9.76k
        REF_ASSERT_ISNT(ret < 0);
94
9.76k
        if (ret == 0) {
95
9.76k
            CRYPTO_THREAD_lock_free(*lock);
96
9.76k
            *lock = NULL;
97
9.76k
        }
98
9.76k
        break;
99
19.5k
    }
100
101
19.5k
    return ret;
102
19.5k
}
103
104
static ASN1_ENCODING *asn1_get_enc_ptr(ASN1_VALUE **pval, const ASN1_ITEM *it)
105
3.74M
{
106
3.74M
    const ASN1_AUX *aux;
107
3.74M
    if (!pval || !*pval)
108
0
        return NULL;
109
3.74M
    aux = it->funcs;
110
3.74M
    if (!aux || !(aux->flags & ASN1_AFLG_ENCODING))
111
3.70M
        return NULL;
112
40.0k
    return offset2ptr(*pval, aux->enc_offset);
113
3.74M
}
114
115
void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it)
116
1.01M
{
117
1.01M
    ASN1_ENCODING *enc;
118
1.01M
    enc = asn1_get_enc_ptr(pval, it);
119
1.01M
    if (enc) {
120
9.76k
        enc->enc = NULL;
121
9.76k
        enc->len = 0;
122
9.76k
        enc->modified = 1;
123
9.76k
    }
124
1.01M
}
125
126
void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
127
1.01M
{
128
1.01M
    ASN1_ENCODING *enc;
129
1.01M
    enc = asn1_get_enc_ptr(pval, it);
130
1.01M
    if (enc) {
131
9.76k
        OPENSSL_free(enc->enc);
132
9.76k
        enc->enc = NULL;
133
9.76k
        enc->len = 0;
134
9.76k
        enc->modified = 1;
135
9.76k
    }
136
1.01M
}
137
138
int asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen,
139
                  const ASN1_ITEM *it)
140
595k
{
141
595k
    ASN1_ENCODING *enc;
142
595k
    enc = asn1_get_enc_ptr(pval, it);
143
595k
    if (!enc)
144
590k
        return 1;
145
146
5.17k
    OPENSSL_free(enc->enc);
147
5.17k
    if ((enc->enc = OPENSSL_malloc(inlen)) == NULL) {
148
0
        ASN1err(ASN1_F_ASN1_ENC_SAVE, ERR_R_MALLOC_FAILURE);
149
0
        return 0;
150
0
    }
151
5.17k
    memcpy(enc->enc, in, inlen);
152
5.17k
    enc->len = inlen;
153
5.17k
    enc->modified = 0;
154
155
5.17k
    return 1;
156
5.17k
}
157
158
int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval,
159
                     const ASN1_ITEM *it)
160
1.12M
{
161
1.12M
    ASN1_ENCODING *enc;
162
1.12M
    enc = asn1_get_enc_ptr(pval, it);
163
1.12M
    if (!enc || enc->modified)
164
1.11M
        return 0;
165
15.3k
    if (out) {
166
5.11k
        memcpy(*out, enc->enc, enc->len);
167
5.11k
        *out += enc->len;
168
5.11k
    }
169
15.3k
    if (len)
170
15.3k
        *len = enc->len;
171
15.3k
    return 1;
172
1.12M
}
173
174
/* Given an ASN1_TEMPLATE get a pointer to a field */
175
ASN1_VALUE **asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
176
9.46M
{
177
9.46M
    ASN1_VALUE **pvaltmp;
178
9.46M
    pvaltmp = offset2ptr(*pval, tt->offset);
179
    /*
180
     * NOTE for BOOLEAN types the field is just a plain int so we can't
181
     * return int **, so settle for (int *).
182
     */
183
9.46M
    return pvaltmp;
184
9.46M
}
185
186
/*
187
 * Handle ANY DEFINED BY template, find the selector, look up the relevant
188
 * ASN1_TEMPLATE in the table and return it.
189
 */
190
191
const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt,
192
                                 int nullerr)
193
6.86M
{
194
6.86M
    const ASN1_ADB *adb;
195
6.86M
    const ASN1_ADB_TABLE *atbl;
196
6.86M
    long selector;
197
6.86M
    ASN1_VALUE **sfld;
198
6.86M
    int i;
199
6.86M
    if (!(tt->flags & ASN1_TFLG_ADB_MASK))
200
6.82M
        return tt;
201
202
    /* Else ANY DEFINED BY ... get the table */
203
33.1k
    adb = ASN1_ADB_ptr(tt->item);
204
205
    /* Get the selector field */
206
33.1k
    sfld = offset2ptr(*pval, adb->offset);
207
208
    /* Check if NULL */
209
33.1k
    if (*sfld == NULL) {
210
12.1k
        if (!adb->null_tt)
211
12.1k
            goto err;
212
0
        return adb->null_tt;
213
12.1k
    }
214
215
    /*
216
     * Convert type to a long: NB: don't check for NID_undef here because it
217
     * might be a legitimate value in the table
218
     */
219
20.9k
    if (tt->flags & ASN1_TFLG_ADB_OID)
220
20.9k
        selector = OBJ_obj2nid((ASN1_OBJECT *)*sfld);
221
0
    else
222
0
        selector = ASN1_INTEGER_get((ASN1_INTEGER *)*sfld);
223
224
    /* Let application callback translate value */
225
20.9k
    if (adb->adb_cb != NULL && adb->adb_cb(&selector) == 0) {
226
0
        ASN1err(ASN1_F_ASN1_DO_ADB, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE);
227
0
        return NULL;
228
0
    }
229
230
    /*
231
     * Try to find matching entry in table Maybe should check application
232
     * types first to allow application override? Might also be useful to
233
     * have a flag which indicates table is sorted and we can do a binary
234
     * search. For now stick to a linear search.
235
     */
236
237
50.5k
    for (atbl = adb->tbl, i = 0; i < adb->tblcount; i++, atbl++)
238
36.3k
        if (atbl->value == selector)
239
6.79k
            return &atbl->tt;
240
241
    /* FIXME: need to search application table too */
242
243
    /* No match, return default type */
244
14.1k
    if (!adb->default_tt)
245
0
        goto err;
246
14.1k
    return adb->default_tt;
247
248
12.1k
 err:
249
    /* FIXME: should log the value or OID of unsupported type */
250
12.1k
    if (nullerr)
251
12.1k
        ASN1err(ASN1_F_ASN1_DO_ADB, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE);
252
12.1k
    return NULL;
253
14.1k
}