Coverage Report

Created: 2023-06-08 06:43

/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
199k
{
42
199k
    return ((*a)->ext_nid - (*b)->ext_nid);
43
199k
}
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
38.8k
{
54
38.8k
    X509V3_EXT_METHOD tmp;
55
38.8k
    const X509V3_EXT_METHOD *t = &tmp, *const *ret;
56
38.8k
    int idx;
57
58
38.8k
    if (nid < 0)
59
0
        return NULL;
60
38.8k
    tmp.ext_nid = nid;
61
38.8k
    ret = OBJ_bsearch_ext(&t, standard_exts, STANDARD_EXTENSION_COUNT);
62
38.8k
    if (ret)
63
35.2k
        return *ret;
64
3.60k
    if (!ext_list)
65
3.60k
        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
3.60k
}
69
70
const X509V3_EXT_METHOD *X509V3_EXT_get(X509_EXTENSION *ext)
71
40.5k
{
72
40.5k
    int nid;
73
40.5k
    if ((nid = OBJ_obj2nid(X509_EXTENSION_get_object(ext))) == NID_undef)
74
1.70k
        return NULL;
75
38.8k
    return X509V3_EXT_get_nid(nid);
76
40.5k
}
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
    return X509V3_EXT_add(tmpext);
103
0
}
104
105
void X509V3_EXT_cleanup(void)
106
0
{
107
0
    sk_X509V3_EXT_METHOD_pop_free(ext_list, ext_list_free);
108
0
    ext_list = NULL;
109
0
}
110
111
static void ext_list_free(X509V3_EXT_METHOD *ext)
112
0
{
113
0
    if (ext->ext_flags & X509V3_EXT_DYNAMIC)
114
0
        OPENSSL_free(ext);
115
0
}
116
117
/*
118
 * Legacy function: we don't need to add standard extensions any more because
119
 * they are now kept in ext_dat.h.
120
 */
121
122
int X509V3_add_standard_extensions(void)
123
0
{
124
0
    return 1;
125
0
}
126
127
/* Return an extension internal structure */
128
129
void *X509V3_EXT_d2i(X509_EXTENSION *ext)
130
17.2k
{
131
17.2k
    const X509V3_EXT_METHOD *method;
132
17.2k
    const unsigned char *p;
133
17.2k
    ASN1_STRING *extvalue;
134
17.2k
    int extlen;
135
136
17.2k
    if ((method = X509V3_EXT_get(ext)) == NULL)
137
0
        return NULL;
138
17.2k
    extvalue = X509_EXTENSION_get_data(ext);
139
17.2k
    p = ASN1_STRING_get0_data(extvalue);
140
17.2k
    extlen = ASN1_STRING_length(extvalue);
141
17.2k
    if (method->it)
142
17.2k
        return ASN1_item_d2i(NULL, &p, extlen, ASN1_ITEM_ptr(method->it));
143
0
    return method->d2i(NULL, &p, extlen);
144
17.2k
}
145
146
/*-
147
 * Get critical flag and decoded version of extension from a NID.
148
 * The "idx" variable returns the last found extension and can
149
 * be used to retrieve multiple extensions of the same NID.
150
 * However multiple extensions with the same NID is usually
151
 * due to a badly encoded certificate so if idx is NULL we
152
 * choke if multiple extensions exist.
153
 * The "crit" variable is set to the critical value.
154
 * The return value is the decoded extension or NULL on
155
 * error. The actual error can have several different causes,
156
 * the value of *crit reflects the cause:
157
 * >= 0, extension found but not decoded (reflects critical value).
158
 * -1 extension not found.
159
 * -2 extension occurs more than once.
160
 */
161
162
void *X509V3_get_d2i(const STACK_OF(X509_EXTENSION) *x, int nid, int *crit,
163
                     int *idx)
164
76.4k
{
165
76.4k
    int lastpos, i;
166
76.4k
    X509_EXTENSION *ex, *found_ex = NULL;
167
168
76.4k
    if (!x) {
169
21.6k
        if (idx)
170
0
            *idx = -1;
171
21.6k
        if (crit)
172
21.6k
            *crit = -1;
173
21.6k
        return NULL;
174
21.6k
    }
175
54.7k
    if (idx)
176
0
        lastpos = *idx + 1;
177
54.7k
    else
178
54.7k
        lastpos = 0;
179
54.7k
    if (lastpos < 0)
180
0
        lastpos = 0;
181
133k
    for (i = lastpos; i < sk_X509_EXTENSION_num(x); i++) {
182
79.6k
        ex = sk_X509_EXTENSION_value(x, i);
183
79.6k
        if (OBJ_obj2nid(X509_EXTENSION_get_object(ex)) == nid) {
184
18.2k
            if (idx) {
185
0
                *idx = i;
186
0
                found_ex = ex;
187
0
                break;
188
18.2k
            } else if (found_ex) {
189
                /* Found more than one */
190
463
                if (crit)
191
463
                    *crit = -2;
192
463
                return NULL;
193
463
            }
194
17.7k
            found_ex = ex;
195
17.7k
        }
196
79.6k
    }
197
54.3k
    if (found_ex) {
198
        /* Found it */
199
17.2k
        if (crit)
200
17.2k
            *crit = X509_EXTENSION_get_critical(found_ex);
201
17.2k
        return X509V3_EXT_d2i(found_ex);
202
17.2k
    }
203
204
    /* Extension not found */
205
37.0k
    if (idx)
206
0
        *idx = -1;
207
37.0k
    if (crit)
208
37.0k
        *crit = -1;
209
37.0k
    return NULL;
210
54.3k
}
211
212
/*
213
 * This function is a general extension append, replace and delete utility.
214
 * The precise operation is governed by the 'flags' value. The 'crit' and
215
 * 'value' arguments (if relevant) are the extensions internal structure.
216
 */
217
218
int X509V3_add1_i2d(STACK_OF(X509_EXTENSION) **x, int nid, void *value,
219
                    int crit, unsigned long flags)
220
0
{
221
0
    int errcode, extidx = -1;
222
0
    X509_EXTENSION *ext = NULL, *extmp;
223
0
    STACK_OF(X509_EXTENSION) *ret = NULL;
224
0
    unsigned long ext_op = flags & X509V3_ADD_OP_MASK;
225
226
    /*
227
     * If appending we don't care if it exists, otherwise look for existing
228
     * extension.
229
     */
230
0
    if (ext_op != X509V3_ADD_APPEND)
231
0
        extidx = X509v3_get_ext_by_NID(*x, nid, -1);
232
233
    /* See if extension exists */
234
0
    if (extidx >= 0) {
235
        /* If keep existing, nothing to do */
236
0
        if (ext_op == X509V3_ADD_KEEP_EXISTING)
237
0
            return 1;
238
        /* If default then its an error */
239
0
        if (ext_op == X509V3_ADD_DEFAULT) {
240
0
            errcode = X509V3_R_EXTENSION_EXISTS;
241
0
            goto err;
242
0
        }
243
        /* If delete, just delete it */
244
0
        if (ext_op == X509V3_ADD_DELETE) {
245
0
            extmp = sk_X509_EXTENSION_delete(*x, extidx);
246
0
            if (extmp == NULL)
247
0
                return -1;
248
0
            X509_EXTENSION_free(extmp);
249
0
            return 1;
250
0
        }
251
0
    } else {
252
        /*
253
         * If replace existing or delete, error since extension must exist
254
         */
255
0
        if ((ext_op == X509V3_ADD_REPLACE_EXISTING) ||
256
0
            (ext_op == X509V3_ADD_DELETE)) {
257
0
            errcode = X509V3_R_EXTENSION_NOT_FOUND;
258
0
            goto err;
259
0
        }
260
0
    }
261
262
    /*
263
     * If we get this far then we have to create an extension: could have
264
     * some flags for alternative encoding schemes...
265
     */
266
267
0
    ext = X509V3_EXT_i2d(nid, crit, value);
268
269
0
    if (!ext) {
270
0
        ERR_raise(ERR_LIB_X509V3, X509V3_R_ERROR_CREATING_EXTENSION);
271
0
        return 0;
272
0
    }
273
274
    /* If extension exists replace it.. */
275
0
    if (extidx >= 0) {
276
0
        extmp = sk_X509_EXTENSION_value(*x, extidx);
277
0
        X509_EXTENSION_free(extmp);
278
0
        if (!sk_X509_EXTENSION_set(*x, extidx, ext))
279
0
            return -1;
280
0
        return 1;
281
0
    }
282
283
0
    ret = *x;
284
0
    if (*x == NULL
285
0
        && (ret = sk_X509_EXTENSION_new_null()) == NULL)
286
0
        goto m_fail;
287
0
    if (!sk_X509_EXTENSION_push(ret, ext))
288
0
        goto m_fail;
289
290
0
    *x = ret;
291
0
    return 1;
292
293
0
 m_fail:
294
    /* ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE); */
295
0
    if (ret != *x)
296
0
        sk_X509_EXTENSION_free(ret);
297
0
    X509_EXTENSION_free(ext);
298
0
    return -1;
299
300
0
 err:
301
0
    if (!(flags & X509V3_ADD_SILENT))
302
0
        ERR_raise(ERR_LIB_X509V3, errcode);
303
0
    return 0;
304
0
}