Coverage Report

Created: 2026-04-08 06:20

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