Coverage Report

Created: 2026-10-28 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl41/crypto/x509/x509_v3.c
Line
Count
Source
1
/*
2
 * Copyright 1995-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
#include <stdio.h>
11
#include "internal/cryptlib.h"
12
#include <openssl/safestack.h>
13
#include <openssl/asn1.h>
14
#include <openssl/objects.h>
15
#include <openssl/evp.h>
16
#include <openssl/x509.h>
17
#include <openssl/x509v3.h>
18
#include "x509_local.h"
19
20
#include <crypto/asn1.h>
21
22
int X509v3_get_ext_count(const STACK_OF(X509_EXTENSION) *x)
23
482k
{
24
482k
    int ret;
25
26
482k
    if (x == NULL)
27
3.06k
        return 0;
28
478k
    ret = sk_X509_EXTENSION_num(x);
29
478k
    return ret > 0 ? ret : 0;
30
482k
}
31
32
int X509v3_get_ext_by_NID(const STACK_OF(X509_EXTENSION) *x, int nid,
33
    int lastpos)
34
20.5k
{
35
20.5k
    ASN1_OBJECT *obj;
36
37
20.5k
    obj = OBJ_nid2obj(nid);
38
20.5k
    if (obj == NULL)
39
0
        return -2;
40
20.5k
    return X509v3_get_ext_by_OBJ(x, obj, lastpos);
41
20.5k
}
42
43
int X509v3_get_ext_by_OBJ(const STACK_OF(X509_EXTENSION) *sk,
44
    const ASN1_OBJECT *obj, int lastpos)
45
20.5k
{
46
20.5k
    int n;
47
20.5k
    X509_EXTENSION *ex;
48
49
20.5k
    if (sk == NULL)
50
2.63k
        return -1;
51
17.8k
    lastpos++;
52
17.8k
    if (lastpos < 0)
53
0
        lastpos = 0;
54
17.8k
    n = sk_X509_EXTENSION_num(sk);
55
24.6k
    for (; lastpos < n; lastpos++) {
56
22.9k
        ex = sk_X509_EXTENSION_value(sk, lastpos);
57
22.9k
        if (OBJ_cmp(ex->object, obj) == 0)
58
16.1k
            return lastpos;
59
22.9k
    }
60
1.68k
    return -1;
61
17.8k
}
62
63
int X509v3_get_ext_by_critical(const STACK_OF(X509_EXTENSION) *sk, int crit,
64
    int lastpos)
65
0
{
66
0
    int n, c;
67
0
    X509_EXTENSION *ex;
68
69
0
    if (sk == NULL)
70
0
        return -1;
71
0
    lastpos++;
72
0
    if (lastpos < 0)
73
0
        lastpos = 0;
74
0
    n = sk_X509_EXTENSION_num(sk);
75
0
    for (; lastpos < n; lastpos++) {
76
0
        ex = sk_X509_EXTENSION_value(sk, lastpos);
77
0
        c = X509_EXTENSION_get_critical(ex);
78
0
        crit = crit != 0;
79
0
        if (c == crit)
80
0
            return lastpos;
81
0
    }
82
0
    return -1;
83
0
}
84
85
const X509_EXTENSION *X509v3_get_ext(const STACK_OF(X509_EXTENSION) *x, int loc)
86
694k
{
87
694k
    if (x == NULL || sk_X509_EXTENSION_num(x) <= loc || loc < 0)
88
0
        return NULL;
89
694k
    else
90
694k
        return sk_X509_EXTENSION_value(x, loc);
91
694k
}
92
93
X509_EXTENSION *X509v3_delete_ext(STACK_OF(X509_EXTENSION) *x, int loc)
94
0
{
95
0
    return sk_X509_EXTENSION_delete(x, loc);
96
0
}
97
98
X509_EXTENSION *X509v3_delete_extension(STACK_OF(X509_EXTENSION) **x, int loc)
99
0
{
100
0
    X509_EXTENSION *ext;
101
102
0
    if (x == NULL)
103
0
        return NULL;
104
105
    /* Set extensions to NULL when last element dropped */
106
0
    if ((ext = X509v3_delete_ext(*x, loc)) != NULL
107
0
        && sk_X509_EXTENSION_num(*x) == 0) {
108
0
        sk_X509_EXTENSION_free(*x);
109
0
        *x = NULL;
110
0
    }
111
0
    return ext;
112
0
}
113
114
STACK_OF(X509_EXTENSION) *X509v3_add_ext(STACK_OF(X509_EXTENSION) **x,
115
    const X509_EXTENSION *ex, int loc)
116
19.4k
{
117
19.4k
    X509_EXTENSION *new_ex = NULL;
118
19.4k
    int n;
119
19.4k
    STACK_OF(X509_EXTENSION) *sk = NULL;
120
121
19.4k
    if (x == NULL) {
122
0
        ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
123
0
        goto err;
124
0
    }
125
126
19.4k
    if (*x == NULL) {
127
13.6k
        if ((sk = sk_X509_EXTENSION_new_null()) == NULL) {
128
0
            ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB);
129
0
            goto err;
130
0
        }
131
13.6k
    } else
132
5.77k
        sk = *x;
133
134
19.4k
    if (ossl_ignored_x509_extension(ex, X509V3_ADD_SILENT))
135
197
        goto done;
136
137
19.2k
    n = sk_X509_EXTENSION_num(sk);
138
19.2k
    if (loc > n)
139
0
        loc = n;
140
19.2k
    else if (loc < 0)
141
19.2k
        loc = n;
142
143
19.2k
    if ((new_ex = X509_EXTENSION_dup(ex)) == NULL) {
144
23
        ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
145
23
        goto err;
146
23
    }
147
19.2k
    if (!sk_X509_EXTENSION_insert(sk, new_ex, loc)) {
148
0
        ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB);
149
0
        goto err;
150
0
    }
151
19.4k
done:
152
19.4k
    if (*x == NULL)
153
13.6k
        *x = sk;
154
19.4k
    return sk;
155
23
err:
156
23
    X509_EXTENSION_free(new_ex);
157
23
    if (x != NULL && *x == NULL)
158
16
        sk_X509_EXTENSION_free(sk);
159
23
    return NULL;
160
19.2k
}
161
162
/* This returns NULL also in non-error case *target == NULL && sk_X509_EXTENSION_num(exts) <= 0 */
163
STACK_OF(X509_EXTENSION) *X509v3_add_extensions(STACK_OF(X509_EXTENSION) **target,
164
    const STACK_OF(X509_EXTENSION) *exts)
165
0
{
166
0
    int i;
167
168
0
    if (target == NULL) {
169
0
        ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
170
0
        return NULL;
171
0
    }
172
173
0
    for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
174
0
        const X509_EXTENSION *ext = sk_X509_EXTENSION_value(exts, i);
175
0
        const ASN1_OBJECT *obj = X509_EXTENSION_get_object(ext);
176
0
        int idx = X509v3_get_ext_by_OBJ(*target, obj, -1);
177
178
        /* Does extension exist in target? */
179
0
        if (idx != -1) {
180
            /* Delete all extensions of same type */
181
0
            do {
182
0
                X509_EXTENSION_free(sk_X509_EXTENSION_delete(*target, idx));
183
0
                idx = X509v3_get_ext_by_OBJ(*target, obj, -1);
184
0
            } while (idx != -1);
185
0
        }
186
0
        if (!X509v3_add_ext(target, ext, -1))
187
0
            return NULL;
188
0
    }
189
0
    return *target;
190
0
}
191
192
X509_EXTENSION *X509_EXTENSION_create_by_NID(X509_EXTENSION **ex, int nid,
193
    int crit,
194
    ASN1_OCTET_STRING *data)
195
11.1k
{
196
11.1k
    ASN1_OBJECT *obj;
197
11.1k
    X509_EXTENSION *ret;
198
199
11.1k
    obj = OBJ_nid2obj(nid);
200
11.1k
    if (obj == NULL) {
201
0
        ERR_raise(ERR_LIB_X509, X509_R_UNKNOWN_NID);
202
0
        return NULL;
203
0
    }
204
11.1k
    ret = X509_EXTENSION_create_by_OBJ(ex, obj, crit, data);
205
11.1k
    if (ret == NULL)
206
0
        ASN1_OBJECT_free(obj);
207
11.1k
    return ret;
208
11.1k
}
209
210
X509_EXTENSION *X509_EXTENSION_create_by_OBJ(X509_EXTENSION **ex,
211
    const ASN1_OBJECT *obj, int crit,
212
    ASN1_OCTET_STRING *data)
213
19.4k
{
214
19.4k
    X509_EXTENSION *ret;
215
216
19.4k
    if ((ex == NULL) || (*ex == NULL)) {
217
19.4k
        if ((ret = X509_EXTENSION_new()) == NULL) {
218
0
            ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
219
0
            return NULL;
220
0
        }
221
19.4k
    } else
222
0
        ret = *ex;
223
224
19.4k
    if (!X509_EXTENSION_set_object(ret, obj))
225
0
        goto err;
226
19.4k
    if (!X509_EXTENSION_set_critical(ret, crit))
227
0
        goto err;
228
19.4k
    if (!X509_EXTENSION_set_data(ret, data))
229
0
        goto err;
230
231
19.4k
    if ((ex != NULL) && (*ex == NULL))
232
0
        *ex = ret;
233
19.4k
    return ret;
234
0
err:
235
0
    if ((ex == NULL) || (ret != *ex))
236
0
        X509_EXTENSION_free(ret);
237
0
    return NULL;
238
19.4k
}
239
240
int X509_EXTENSION_set_object(X509_EXTENSION *ex, const ASN1_OBJECT *obj)
241
19.4k
{
242
19.4k
    if ((ex == NULL) || (obj == NULL))
243
0
        return 0;
244
19.4k
    ASN1_OBJECT_free(ex->object);
245
19.4k
    ex->object = OBJ_dup(obj);
246
19.4k
    return ex->object != NULL;
247
19.4k
}
248
249
int X509_EXTENSION_set_critical(X509_EXTENSION *ex, int crit)
250
19.4k
{
251
19.4k
    if (ex == NULL)
252
0
        return 0;
253
19.4k
    ex->critical = (crit) ? 0xFF : 0;
254
19.4k
    return 1;
255
19.4k
}
256
257
int X509_EXTENSION_set_data(X509_EXTENSION *ex, const ASN1_OCTET_STRING *data)
258
19.4k
{
259
19.4k
    int i;
260
261
19.4k
    if (ex == NULL)
262
0
        return 0;
263
19.4k
    i = ASN1_OCTET_STRING_set(&ex->value, data->data, data->length);
264
19.4k
    if (!i)
265
0
        return 0;
266
19.4k
    return 1;
267
19.4k
}
268
269
const ASN1_OBJECT *X509_EXTENSION_get_object(const X509_EXTENSION *ex)
270
18.2M
{
271
18.2M
    if (ex == NULL)
272
0
        return NULL;
273
18.2M
    return ex->object;
274
18.2M
}
275
276
const ASN1_OCTET_STRING *X509_EXTENSION_get_data(const X509_EXTENSION *ex)
277
4.72M
{
278
4.72M
    if (ex == NULL)
279
0
        return NULL;
280
4.72M
    return &ex->value;
281
4.72M
}
282
283
int X509_EXTENSION_get_critical(const X509_EXTENSION *ex)
284
4.87M
{
285
4.87M
    if (ex == NULL)
286
0
        return 0;
287
4.87M
    if (ex->critical > 0)
288
326k
        return 1;
289
4.54M
    return 0;
290
4.87M
}