Coverage Report

Created: 2025-06-13 06:58

/src/openssl30/crypto/x509/v3_lib.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1999-2022 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
/* X509 v3 extension utilities */
11
12
#include <stdio.h>
13
#include "internal/cryptlib.h"
14
#include <openssl/conf.h>
15
#include <openssl/x509v3.h>
16
17
#include "ext_dat.h"
18
19
static STACK_OF(X509V3_EXT_METHOD) *ext_list = NULL;
20
21
static int ext_cmp(const X509V3_EXT_METHOD *const *a,
22
                   const X509V3_EXT_METHOD *const *b);
23
static void ext_list_free(X509V3_EXT_METHOD *ext);
24
25
int X509V3_EXT_add(X509V3_EXT_METHOD *ext)
26
0
{
27
0
    if (ext_list == NULL
28
0
        && (ext_list = sk_X509V3_EXT_METHOD_new(ext_cmp)) == NULL) {
29
0
        ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
30
0
        return 0;
31
0
    }
32
0
    if (!sk_X509V3_EXT_METHOD_push(ext_list, ext)) {
33
0
        ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
34
0
        return 0;
35
0
    }
36
0
    return 1;
37
0
}
38
39
static int ext_cmp(const X509V3_EXT_METHOD *const *a,
40
                   const X509V3_EXT_METHOD *const *b)
41
8.24M
{
42
8.24M
    return ((*a)->ext_nid - (*b)->ext_nid);
43
8.24M
}
44
45
DECLARE_OBJ_BSEARCH_CMP_FN(const X509V3_EXT_METHOD *,
46
                           const X509V3_EXT_METHOD *, ext);
47
IMPLEMENT_OBJ_BSEARCH_CMP_FN(const X509V3_EXT_METHOD *,
48
                             const X509V3_EXT_METHOD *, ext);
49
50
#include "standard_exts.h"
51
52
const X509V3_EXT_METHOD *X509V3_EXT_get_nid(int nid)
53
1.63M
{
54
1.63M
    X509V3_EXT_METHOD tmp;
55
1.63M
    const X509V3_EXT_METHOD *t = &tmp, *const *ret;
56
1.63M
    int idx;
57
58
1.63M
    if (nid < 0)
59
0
        return NULL;
60
1.63M
    tmp.ext_nid = nid;
61
1.63M
    ret = OBJ_bsearch_ext(&t, standard_exts, STANDARD_EXTENSION_COUNT);
62
1.63M
    if (ret)
63
1.60M
        return *ret;
64
32.7k
    if (!ext_list)
65
32.7k
        return NULL;
66
0
    idx = sk_X509V3_EXT_METHOD_find(ext_list, &tmp);
67
0
    return sk_X509V3_EXT_METHOD_value(ext_list, idx);
68
32.7k
}
69
70
const X509V3_EXT_METHOD *X509V3_EXT_get(X509_EXTENSION *ext)
71
1.73M
{
72
1.73M
    int nid;
73
1.73M
    if ((nid = OBJ_obj2nid(X509_EXTENSION_get_object(ext))) == NID_undef)
74
94.8k
        return NULL;
75
1.63M
    return X509V3_EXT_get_nid(nid);
76
1.73M
}
77
78
int X509V3_EXT_add_list(X509V3_EXT_METHOD *extlist)
79
0
{
80
0
    for (; extlist->ext_nid != -1; extlist++)
81
0
        if (!X509V3_EXT_add(extlist))
82
0
            return 0;
83
0
    return 1;
84
0
}
85
86
int X509V3_EXT_add_alias(int nid_to, int nid_from)
87
0
{
88
0
    const X509V3_EXT_METHOD *ext;
89
0
    X509V3_EXT_METHOD *tmpext;
90
91
0
    if ((ext = X509V3_EXT_get_nid(nid_from)) == NULL) {
92
0
        ERR_raise(ERR_LIB_X509V3, X509V3_R_EXTENSION_NOT_FOUND);
93
0
        return 0;
94
0
    }
95
0
    if ((tmpext = OPENSSL_malloc(sizeof(*tmpext))) == NULL) {
96
0
        ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
97
0
        return 0;
98
0
    }
99
0
    *tmpext = *ext;
100
0
    tmpext->ext_nid = nid_to;
101
0
    tmpext->ext_flags |= X509V3_EXT_DYNAMIC;
102
0
    if (!X509V3_EXT_add(tmpext)) {
103
0
        OPENSSL_free(tmpext);
104
0
        return 0;
105
0
    }
106
0
    return 1;
107
0
}
108
109
void X509V3_EXT_cleanup(void)
110
0
{
111
0
    sk_X509V3_EXT_METHOD_pop_free(ext_list, ext_list_free);
112
0
    ext_list = NULL;
113
0
}
114
115
static void ext_list_free(X509V3_EXT_METHOD *ext)
116
0
{
117
0
    if (ext->ext_flags & X509V3_EXT_DYNAMIC)
118
0
        OPENSSL_free(ext);
119
0
}
120
121
/*
122
 * Legacy function: we don't need to add standard extensions any more because
123
 * they are now kept in ext_dat.h.
124
 */
125
126
int X509V3_add_standard_extensions(void)
127
0
{
128
0
    return 1;
129
0
}
130
131
/* Return an extension internal structure */
132
133
void *X509V3_EXT_d2i(X509_EXTENSION *ext)
134
592k
{
135
592k
    const X509V3_EXT_METHOD *method;
136
592k
    const unsigned char *p;
137
592k
    ASN1_STRING *extvalue;
138
592k
    int extlen;
139
140
592k
    if ((method = X509V3_EXT_get(ext)) == NULL)
141
0
        return NULL;
142
592k
    extvalue = X509_EXTENSION_get_data(ext);
143
592k
    p = ASN1_STRING_get0_data(extvalue);
144
592k
    extlen = ASN1_STRING_length(extvalue);
145
592k
    if (method->it)
146
592k
        return ASN1_item_d2i(NULL, &p, extlen, ASN1_ITEM_ptr(method->it));
147
0
    return method->d2i(NULL, &p, extlen);
148
592k
}
149
150
/*-
151
 * Get critical flag and decoded version of extension from a NID.
152
 * The "idx" variable returns the last found extension and can
153
 * be used to retrieve multiple extensions of the same NID.
154
 * However multiple extensions with the same NID is usually
155
 * due to a badly encoded certificate so if idx is NULL we
156
 * choke if multiple extensions exist.
157
 * The "crit" variable is set to the critical value.
158
 * The return value is the decoded extension or NULL on
159
 * error. The actual error can have several different causes,
160
 * the value of *crit reflects the cause:
161
 * >= 0, extension found but not decoded (reflects critical value).
162
 * -1 extension not found.
163
 * -2 extension occurs more than once.
164
 */
165
166
void *X509V3_get_d2i(const STACK_OF(X509_EXTENSION) *x, int nid, int *crit,
167
                     int *idx)
168
2.31M
{
169
2.31M
    int lastpos, i;
170
2.31M
    X509_EXTENSION *ex, *found_ex = NULL;
171
172
2.31M
    if (!x) {
173
307k
        if (idx)
174
0
            *idx = -1;
175
307k
        if (crit)
176
307k
            *crit = -1;
177
307k
        return NULL;
178
307k
    }
179
2.01M
    if (idx)
180
0
        lastpos = *idx + 1;
181
2.01M
    else
182
2.01M
        lastpos = 0;
183
2.01M
    if (lastpos < 0)
184
0
        lastpos = 0;
185
9.55M
    for (i = lastpos; i < sk_X509_EXTENSION_num(x); i++) {
186
7.54M
        ex = sk_X509_EXTENSION_value(x, i);
187
7.54M
        if (OBJ_obj2nid(X509_EXTENSION_get_object(ex)) == nid) {
188
604k
            if (idx) {
189
0
                *idx = i;
190
0
                found_ex = ex;
191
0
                break;
192
604k
            } else if (found_ex) {
193
                /* Found more than one */
194
6.03k
                if (crit)
195
6.03k
                    *crit = -2;
196
6.03k
                return NULL;
197
6.03k
            }
198
598k
            found_ex = ex;
199
598k
        }
200
7.54M
    }
201
2.00M
    if (found_ex) {
202
        /* Found it */
203
592k
        if (crit)
204
592k
            *crit = X509_EXTENSION_get_critical(found_ex);
205
592k
        return X509V3_EXT_d2i(found_ex);
206
592k
    }
207
208
    /* Extension not found */
209
1.41M
    if (idx)
210
0
        *idx = -1;
211
1.41M
    if (crit)
212
1.41M
        *crit = -1;
213
1.41M
    return NULL;
214
2.00M
}
215
216
/*
217
 * This function is a general extension append, replace and delete utility.
218
 * The precise operation is governed by the 'flags' value. The 'crit' and
219
 * 'value' arguments (if relevant) are the extensions internal structure.
220
 */
221
222
int X509V3_add1_i2d(STACK_OF(X509_EXTENSION) **x, int nid, void *value,
223
                    int crit, unsigned long flags)
224
0
{
225
0
    int errcode, extidx = -1;
226
0
    X509_EXTENSION *ext = NULL, *extmp;
227
0
    STACK_OF(X509_EXTENSION) *ret = NULL;
228
0
    unsigned long ext_op = flags & X509V3_ADD_OP_MASK;
229
230
    /*
231
     * If appending we don't care if it exists, otherwise look for existing
232
     * extension.
233
     */
234
0
    if (ext_op != X509V3_ADD_APPEND)
235
0
        extidx = X509v3_get_ext_by_NID(*x, nid, -1);
236
237
    /* See if extension exists */
238
0
    if (extidx >= 0) {
239
        /* If keep existing, nothing to do */
240
0
        if (ext_op == X509V3_ADD_KEEP_EXISTING)
241
0
            return 1;
242
        /* If default then its an error */
243
0
        if (ext_op == X509V3_ADD_DEFAULT) {
244
0
            errcode = X509V3_R_EXTENSION_EXISTS;
245
0
            goto err;
246
0
        }
247
        /* If delete, just delete it */
248
0
        if (ext_op == X509V3_ADD_DELETE) {
249
0
            extmp = sk_X509_EXTENSION_delete(*x, extidx);
250
0
            if (extmp == NULL)
251
0
                return -1;
252
0
            X509_EXTENSION_free(extmp);
253
0
            return 1;
254
0
        }
255
0
    } else {
256
        /*
257
         * If replace existing or delete, error since extension must exist
258
         */
259
0
        if ((ext_op == X509V3_ADD_REPLACE_EXISTING) ||
260
0
            (ext_op == X509V3_ADD_DELETE)) {
261
0
            errcode = X509V3_R_EXTENSION_NOT_FOUND;
262
0
            goto err;
263
0
        }
264
0
    }
265
266
    /*
267
     * If we get this far then we have to create an extension: could have
268
     * some flags for alternative encoding schemes...
269
     */
270
271
0
    ext = X509V3_EXT_i2d(nid, crit, value);
272
273
0
    if (!ext) {
274
0
        ERR_raise(ERR_LIB_X509V3, X509V3_R_ERROR_CREATING_EXTENSION);
275
0
        return 0;
276
0
    }
277
278
    /* If extension exists replace it.. */
279
0
    if (extidx >= 0) {
280
0
        extmp = sk_X509_EXTENSION_value(*x, extidx);
281
0
        X509_EXTENSION_free(extmp);
282
0
        if (!sk_X509_EXTENSION_set(*x, extidx, ext))
283
0
            return -1;
284
0
        return 1;
285
0
    }
286
287
0
    ret = *x;
288
0
    if (*x == NULL
289
0
        && (ret = sk_X509_EXTENSION_new_null()) == NULL)
290
0
        goto m_fail;
291
0
    if (!sk_X509_EXTENSION_push(ret, ext))
292
0
        goto m_fail;
293
294
0
    *x = ret;
295
0
    return 1;
296
297
0
 m_fail:
298
    /* ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE); */
299
0
    if (ret != *x)
300
0
        sk_X509_EXTENSION_free(ret);
301
0
    X509_EXTENSION_free(ext);
302
0
    return -1;
303
304
0
 err:
305
0
    if (!(flags & X509V3_ADD_SILENT))
306
0
        ERR_raise(ERR_LIB_X509V3, errcode);
307
0
    return 0;
308
0
}