Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl40/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
507k
{
24
507k
    int ret;
25
26
507k
    if (x == NULL)
27
2.43k
        return 0;
28
505k
    ret = sk_X509_EXTENSION_num(x);
29
505k
    return ret > 0 ? ret : 0;
30
507k
}
31
32
int X509v3_get_ext_by_NID(const STACK_OF(X509_EXTENSION) *x, int nid,
33
    int lastpos)
34
1.20k
{
35
1.20k
    ASN1_OBJECT *obj;
36
37
1.20k
    obj = OBJ_nid2obj(nid);
38
1.20k
    if (obj == NULL)
39
0
        return -2;
40
1.20k
    return X509v3_get_ext_by_OBJ(x, obj, lastpos);
41
1.20k
}
42
43
int X509v3_get_ext_by_OBJ(const STACK_OF(X509_EXTENSION) *sk,
44
    const ASN1_OBJECT *obj, int lastpos)
45
1.20k
{
46
1.20k
    int n;
47
1.20k
    X509_EXTENSION *ex;
48
49
1.20k
    if (sk == NULL)
50
138
        return -1;
51
1.06k
    lastpos++;
52
1.06k
    if (lastpos < 0)
53
0
        lastpos = 0;
54
1.06k
    n = sk_X509_EXTENSION_num(sk);
55
5.95k
    for (; lastpos < n; lastpos++) {
56
5.16k
        ex = sk_X509_EXTENSION_value(sk, lastpos);
57
5.16k
        if (OBJ_cmp(ex->object, obj) == 0)
58
271
            return lastpos;
59
5.16k
    }
60
798
    return -1;
61
1.06k
}
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
551k
{
87
551k
    if (x == NULL || sk_X509_EXTENSION_num(x) <= loc || loc < 0)
88
0
        return NULL;
89
551k
    else
90
551k
        return sk_X509_EXTENSION_value(x, loc);
91
551k
}
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
10.0k
{
117
10.0k
    X509_EXTENSION *new_ex = NULL;
118
10.0k
    int n;
119
10.0k
    STACK_OF(X509_EXTENSION) *sk = NULL;
120
121
10.0k
    if (x == NULL) {
122
0
        ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
123
0
        goto err;
124
0
    }
125
126
10.0k
    if (*x == NULL) {
127
6.57k
        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
6.57k
    } else
132
3.45k
        sk = *x;
133
134
10.0k
    if (ossl_ignored_x509_extension(ex, X509V3_ADD_SILENT))
135
54
        goto done;
136
137
9.97k
    n = sk_X509_EXTENSION_num(sk);
138
9.97k
    if (loc > n)
139
0
        loc = n;
140
9.97k
    else if (loc < 0)
141
9.97k
        loc = n;
142
143
9.97k
    if ((new_ex = X509_EXTENSION_dup(ex)) == NULL) {
144
13
        ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
145
13
        goto err;
146
13
    }
147
9.96k
    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
10.0k
done:
152
10.0k
    if (*x == NULL)
153
6.57k
        *x = sk;
154
10.0k
    return sk;
155
13
err:
156
13
    X509_EXTENSION_free(new_ex);
157
13
    if (x != NULL && *x == NULL)
158
8
        sk_X509_EXTENSION_free(sk);
159
13
    return NULL;
160
9.96k
}
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
6.35k
{
196
6.35k
    ASN1_OBJECT *obj;
197
6.35k
    X509_EXTENSION *ret;
198
199
6.35k
    obj = OBJ_nid2obj(nid);
200
6.35k
    if (obj == NULL) {
201
0
        ERR_raise(ERR_LIB_X509, X509_R_UNKNOWN_NID);
202
0
        return NULL;
203
0
    }
204
6.35k
    ret = X509_EXTENSION_create_by_OBJ(ex, obj, crit, data);
205
6.35k
    if (ret == NULL)
206
0
        ASN1_OBJECT_free(obj);
207
6.35k
    return ret;
208
6.35k
}
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
10.0k
{
214
10.0k
    X509_EXTENSION *ret;
215
216
10.0k
    if ((ex == NULL) || (*ex == NULL)) {
217
10.0k
        if ((ret = X509_EXTENSION_new()) == NULL) {
218
0
            ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
219
0
            return NULL;
220
0
        }
221
10.0k
    } else
222
0
        ret = *ex;
223
224
10.0k
    if (!X509_EXTENSION_set_object(ret, obj))
225
0
        goto err;
226
10.0k
    if (!X509_EXTENSION_set_critical(ret, crit))
227
0
        goto err;
228
10.0k
    if (!X509_EXTENSION_set_data(ret, data))
229
0
        goto err;
230
231
10.0k
    if ((ex != NULL) && (*ex == NULL))
232
0
        *ex = ret;
233
10.0k
    return ret;
234
0
err:
235
0
    if ((ex == NULL) || (ret != *ex))
236
0
        X509_EXTENSION_free(ret);
237
0
    return NULL;
238
10.0k
}
239
240
int X509_EXTENSION_set_object(X509_EXTENSION *ex, const ASN1_OBJECT *obj)
241
10.0k
{
242
10.0k
    if ((ex == NULL) || (obj == NULL))
243
0
        return 0;
244
10.0k
    ASN1_OBJECT_free(ex->object);
245
10.0k
    ex->object = OBJ_dup(obj);
246
10.0k
    return ex->object != NULL;
247
10.0k
}
248
249
int X509_EXTENSION_set_critical(X509_EXTENSION *ex, int crit)
250
10.0k
{
251
10.0k
    if (ex == NULL)
252
0
        return 0;
253
10.0k
    ex->critical = (crit) ? 0xFF : 0;
254
10.0k
    return 1;
255
10.0k
}
256
257
int X509_EXTENSION_set_data(X509_EXTENSION *ex, const ASN1_OCTET_STRING *data)
258
10.0k
{
259
10.0k
    int i;
260
261
10.0k
    if (ex == NULL)
262
0
        return 0;
263
10.0k
    i = ASN1_OCTET_STRING_set(&ex->value, data->data, data->length);
264
10.0k
    if (!i)
265
0
        return 0;
266
10.0k
    return 1;
267
10.0k
}
268
269
const ASN1_OBJECT *X509_EXTENSION_get_object(const X509_EXTENSION *ex)
270
14.6M
{
271
14.6M
    if (ex == NULL)
272
0
        return NULL;
273
14.6M
    return ex->object;
274
14.6M
}
275
276
const ASN1_OCTET_STRING *X509_EXTENSION_get_data(const X509_EXTENSION *ex)
277
3.78M
{
278
3.78M
    if (ex == NULL)
279
0
        return NULL;
280
3.78M
    return &ex->value;
281
3.78M
}
282
283
int X509_EXTENSION_get_critical(const X509_EXTENSION *ex)
284
3.93M
{
285
3.93M
    if (ex == NULL)
286
0
        return 0;
287
3.93M
    if (ex->critical > 0)
288
271k
        return 1;
289
3.66M
    return 0;
290
3.93M
}