Coverage Report

Created: 2026-07-19 06:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/x509/v3_lib.c
Line
Count
Source
1
/*
2
 * Copyright 1999-2026 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
#include "x509_local.h"
19
20
static STACK_OF(X509V3_EXT_METHOD) *ext_list = NULL;
21
22
static int ext_cmp(const X509V3_EXT_METHOD *const *a,
23
    const X509V3_EXT_METHOD *const *b);
24
static void ext_list_free(X509V3_EXT_METHOD *ext);
25
26
int X509V3_EXT_add(X509V3_EXT_METHOD *ext)
27
0
{
28
0
    if (ext_list == NULL
29
0
        && (ext_list = sk_X509V3_EXT_METHOD_new(ext_cmp)) == NULL) {
30
0
        ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB);
31
0
        return 0;
32
0
    }
33
0
    if (!sk_X509V3_EXT_METHOD_push(ext_list, ext)) {
34
0
        ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB);
35
0
        return 0;
36
0
    }
37
0
    return 1;
38
0
}
39
40
static int ext_cmp(const X509V3_EXT_METHOD *const *a,
41
    const X509V3_EXT_METHOD *const *b)
42
0
{
43
0
    return ((*a)->ext_nid - (*b)->ext_nid);
44
0
}
45
46
DECLARE_OBJ_BSEARCH_CMP_FN(const X509V3_EXT_METHOD *,
47
    const X509V3_EXT_METHOD *, ext);
48
IMPLEMENT_OBJ_BSEARCH_CMP_FN(const X509V3_EXT_METHOD *,
49
    const X509V3_EXT_METHOD *, ext);
50
51
#include "standard_exts.h"
52
53
const X509V3_EXT_METHOD *X509V3_EXT_get_nid(int nid)
54
0
{
55
0
    X509V3_EXT_METHOD tmp;
56
0
    const X509V3_EXT_METHOD *t = &tmp, *const * ret;
57
0
    int idx;
58
59
0
    if (nid < 0)
60
0
        return NULL;
61
0
    tmp.ext_nid = nid;
62
0
    ret = OBJ_bsearch_ext(&t, standard_exts, STANDARD_EXTENSION_COUNT);
63
0
    if (ret)
64
0
        return *ret;
65
0
    if (!ext_list)
66
0
        return NULL;
67
    /* Ideally, this would be done under a lock */
68
0
    sk_X509V3_EXT_METHOD_sort(ext_list);
69
0
    idx = sk_X509V3_EXT_METHOD_find(ext_list, &tmp);
70
    /* A failure to locate the item is handled by the value method */
71
0
    return sk_X509V3_EXT_METHOD_value(ext_list, idx);
72
0
}
73
74
const X509V3_EXT_METHOD *X509V3_EXT_get(const X509_EXTENSION *ext)
75
0
{
76
0
    int nid;
77
0
    if ((nid = OBJ_obj2nid(X509_EXTENSION_get_object(ext))) == NID_undef)
78
0
        return NULL;
79
0
    return X509V3_EXT_get_nid(nid);
80
0
}
81
82
int X509V3_EXT_add_list(X509V3_EXT_METHOD *extlist)
83
0
{
84
0
    for (; extlist->ext_nid != -1; extlist++)
85
0
        if (!X509V3_EXT_add(extlist))
86
0
            return 0;
87
0
    return 1;
88
0
}
89
90
int X509V3_EXT_add_alias(int nid_to, int nid_from)
91
0
{
92
0
    const X509V3_EXT_METHOD *ext;
93
0
    X509V3_EXT_METHOD *tmpext;
94
95
0
    if ((ext = X509V3_EXT_get_nid(nid_from)) == NULL) {
96
0
        ERR_raise(ERR_LIB_X509V3, X509V3_R_EXTENSION_NOT_FOUND);
97
0
        return 0;
98
0
    }
99
0
    if ((tmpext = OPENSSL_malloc(sizeof(*tmpext))) == NULL)
100
0
        return 0;
101
0
    *tmpext = *ext;
102
0
    tmpext->ext_nid = nid_to;
103
0
    tmpext->ext_flags |= X509V3_EXT_DYNAMIC;
104
0
    if (!X509V3_EXT_add(tmpext)) {
105
0
        OPENSSL_free(tmpext);
106
0
        return 0;
107
0
    }
108
0
    return 1;
109
0
}
110
111
void X509V3_EXT_cleanup(void)
112
0
{
113
0
    sk_X509V3_EXT_METHOD_pop_free(ext_list, ext_list_free);
114
0
    ext_list = NULL;
115
0
}
116
117
static void ext_list_free(X509V3_EXT_METHOD *ext)
118
0
{
119
0
    if (ext->ext_flags & X509V3_EXT_DYNAMIC)
120
0
        OPENSSL_free(ext);
121
0
}
122
123
/*
124
 * Legacy function: we don't need to add standard extensions any more because
125
 * they are now kept in ext_dat.h.
126
 */
127
128
int X509V3_add_standard_extensions(void)
129
0
{
130
0
    return 1;
131
0
}
132
133
int ossl_ignored_x509_extension(const X509_EXTENSION *ex, int flags)
134
0
{
135
    /*
136
     * Empty OCTET STRINGs and empty SEQUENCEs encode to just two bytes of tag
137
     * (0x04 or 0x30) and length (0x00).  We use this fact to suppress empty
138
     * AKID and SKID extensions that may be briefly generated when processing
139
     * the "= none" value or only ":nonss"-qualified AKIDs when the subject is
140
     * self-signed.
141
     *
142
     * The resulting extension is empty, and must not be retained, but does
143
     * serve to drop any previous value of the same extension, when called
144
     * via
145
     * - X509v3_add_extensions(), or
146
     * - either of X509V3_add1_i2d() or X509V3_EXT_add_nconf_sk(),
147
     *   with a flags (or ctx->flags) value that allows replacement.
148
     */
149
0
    if (ex->value.length == 2
150
0
        && (ex->value.data[0] == 0x30 || ex->value.data[0] == 0x04)) {
151
0
        ASN1_OBJECT *obj = ex->object;
152
0
        ASN1_OBJECT *skid = OBJ_nid2obj(NID_subject_key_identifier);
153
0
        ASN1_OBJECT *akid = OBJ_nid2obj(NID_authority_key_identifier);
154
155
0
        if (OBJ_cmp(obj, skid) == 0 || OBJ_cmp(obj, akid) == 0) {
156
0
            if ((flags & X509V3_ADD_SILENT) == 0)
157
0
                ERR_raise_data(ERR_LIB_X509, X509_R_INVALID_EXTENSION,
158
0
                    "Invalid empty X.509 %s extension", obj->sn);
159
0
            return 1;
160
0
        }
161
0
    }
162
0
    return 0;
163
0
}
164
165
/* Return an extension internal structure */
166
167
void *X509V3_EXT_d2i(const X509_EXTENSION *ext)
168
0
{
169
0
    const X509V3_EXT_METHOD *method;
170
0
    const unsigned char *p;
171
0
    const ASN1_STRING *extvalue;
172
0
    size_t extlen;
173
174
0
    if ((method = X509V3_EXT_get(ext)) == NULL)
175
0
        return NULL;
176
0
    extvalue = X509_EXTENSION_get_data(ext);
177
0
    p = ASN1_STRING_get0_data(extvalue);
178
0
    extlen = ASN1_STRING_length_ex(extvalue);
179
0
    if (extlen > INT_MAX)
180
0
        return NULL;
181
0
    if (method->it)
182
0
        return ASN1_item_d2i(NULL, &p, (int)extlen, ASN1_ITEM_ptr(method->it));
183
0
    return method->d2i(NULL, &p, (int)extlen);
184
0
}
185
186
/*-
187
 * Get critical flag and decoded version of extension from a NID.
188
 * The "idx" variable returns the last found extension and can
189
 * be used to retrieve multiple extensions of the same NID.
190
 * However multiple extensions with the same NID is usually
191
 * due to a badly encoded certificate so if idx is NULL we
192
 * choke if multiple extensions exist.
193
 * The "crit" variable is set to the critical value.
194
 * The return value is the decoded extension or NULL on
195
 * error. The actual error can have several different causes,
196
 * the value of *crit reflects the cause:
197
 * >= 0, extension found but not decoded (reflects critical value).
198
 * -1 extension not found.
199
 * -2 extension occurs more than once.
200
 */
201
202
void *X509V3_get_d2i(const STACK_OF(X509_EXTENSION) *x, int nid, int *crit,
203
    int *idx)
204
0
{
205
0
    int lastpos, i;
206
0
    X509_EXTENSION *ex, *found_ex = NULL;
207
208
0
    if (!x) {
209
0
        if (idx)
210
0
            *idx = -1;
211
0
        if (crit)
212
0
            *crit = -1;
213
0
        return NULL;
214
0
    }
215
0
    if (idx)
216
0
        lastpos = *idx + 1;
217
0
    else
218
0
        lastpos = 0;
219
0
    if (lastpos < 0)
220
0
        lastpos = 0;
221
0
    for (i = lastpos; i < sk_X509_EXTENSION_num(x); i++) {
222
0
        ex = sk_X509_EXTENSION_value(x, i);
223
0
        if (OBJ_obj2nid(X509_EXTENSION_get_object(ex)) == nid) {
224
0
            if (idx) {
225
0
                *idx = i;
226
0
                found_ex = ex;
227
0
                break;
228
0
            } else if (found_ex) {
229
                /* Found more than one */
230
0
                if (crit)
231
0
                    *crit = -2;
232
0
                return NULL;
233
0
            }
234
0
            found_ex = ex;
235
0
        }
236
0
    }
237
0
    if (found_ex) {
238
        /* Found it */
239
0
        if (crit)
240
0
            *crit = X509_EXTENSION_get_critical(found_ex);
241
0
        return X509V3_EXT_d2i(found_ex);
242
0
    }
243
244
    /* Extension not found */
245
0
    if (idx)
246
0
        *idx = -1;
247
0
    if (crit)
248
0
        *crit = -1;
249
0
    return NULL;
250
0
}
251
252
/*
253
 * This function is a general extension append, replace and delete utility.
254
 * The precise operation is governed by the 'flags' value. The 'crit' and
255
 * 'value' arguments (if relevant) are the extensions internal structure.
256
 */
257
258
int X509V3_add1_i2d(STACK_OF(X509_EXTENSION) **x, int nid, void *value,
259
    int crit, unsigned long flags)
260
0
{
261
0
    int errcode, extidx = -1;
262
0
    X509_EXTENSION *ext = NULL, *extmp;
263
0
    STACK_OF(X509_EXTENSION) *ret = NULL;
264
0
    unsigned long ext_op = flags & X509V3_ADD_OP_MASK;
265
266
    /*
267
     * If appending we don't care if it exists, otherwise look for existing
268
     * extension.
269
     */
270
0
    if (ext_op != X509V3_ADD_APPEND)
271
0
        extidx = X509v3_get_ext_by_NID(*x, nid, -1);
272
273
    /* See if extension exists */
274
0
    if (extidx >= 0) {
275
        /* If keep existing, nothing to do */
276
0
        if (ext_op == X509V3_ADD_KEEP_EXISTING)
277
0
            return 1;
278
        /* If default then its an error */
279
0
        if (ext_op == X509V3_ADD_DEFAULT) {
280
0
            errcode = X509V3_R_EXTENSION_EXISTS;
281
0
            goto err;
282
0
        }
283
        /* If delete, just delete it */
284
0
        if (ext_op == X509V3_ADD_DELETE) {
285
0
            extmp = sk_X509_EXTENSION_delete(*x, extidx);
286
0
            if (extmp == NULL)
287
0
                return -1;
288
0
            X509_EXTENSION_free(extmp);
289
0
            return 1;
290
0
        }
291
0
    } else {
292
        /*
293
         * If replace existing or delete, error since extension must exist
294
         */
295
0
        if ((ext_op == X509V3_ADD_REPLACE_EXISTING) || (ext_op == X509V3_ADD_DELETE)) {
296
0
            errcode = X509V3_R_EXTENSION_NOT_FOUND;
297
0
            goto err;
298
0
        }
299
0
    }
300
301
    /*
302
     * If we get this far then we have to create an extension: could have
303
     * some flags for alternative encoding schemes...
304
     */
305
306
0
    ext = X509V3_EXT_i2d(nid, crit, value);
307
308
0
    if (!ext) {
309
0
        ERR_raise(ERR_LIB_X509V3, X509V3_R_ERROR_CREATING_EXTENSION);
310
0
        return 0;
311
0
    }
312
313
    /* If extension exists replace it.. */
314
0
    if (extidx >= 0) {
315
0
        extmp = sk_X509_EXTENSION_value(*x, extidx);
316
0
        if (ossl_ignored_x509_extension(ext, X509V3_ADD_SILENT)) {
317
0
            if (!sk_X509_EXTENSION_delete(*x, extidx))
318
0
                return -1;
319
0
        } else if (!sk_X509_EXTENSION_set(*x, extidx, ext)) {
320
0
            return -1;
321
0
        }
322
0
        X509_EXTENSION_free(extmp);
323
0
        return 1;
324
0
    }
325
326
0
    ret = *x;
327
0
    if (*x == NULL
328
0
        && (ret = sk_X509_EXTENSION_new_null()) == NULL)
329
0
        goto m_fail;
330
0
    if (!sk_X509_EXTENSION_push(ret, ext))
331
0
        goto m_fail;
332
333
0
    *x = ret;
334
0
    return 1;
335
336
0
m_fail:
337
    /* ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB); */
338
0
    if (ret != *x)
339
0
        sk_X509_EXTENSION_free(ret);
340
0
    X509_EXTENSION_free(ext);
341
0
    return -1;
342
343
0
err:
344
0
    if (!(flags & X509V3_ADD_SILENT))
345
        ERR_raise(ERR_LIB_X509V3, errcode);
346
0
    return 0;
347
0
}