Coverage Report

Created: 2026-03-09 06:55

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-2024 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
    X509_EXTENSION *ret;
96
97
0
    if (x == NULL || sk_X509_EXTENSION_num(x) <= loc || loc < 0)
98
0
        return NULL;
99
0
    ret = sk_X509_EXTENSION_delete(x, loc);
100
0
    return ret;
101
0
}
102
103
X509_EXTENSION *X509v3_delete_extension(STACK_OF(X509_EXTENSION) **x, int loc)
104
0
{
105
0
    X509_EXTENSION *ext;
106
107
0
    if (x == NULL)
108
0
        return NULL;
109
110
    /* Set extensions to NULL when last element dropped */
111
0
    if ((ext = X509v3_delete_ext(*x, loc)) != NULL
112
0
        && sk_X509_EXTENSION_num(*x) == 0) {
113
0
        sk_X509_EXTENSION_free(*x);
114
0
        *x = NULL;
115
0
    }
116
0
    return ext;
117
0
}
118
119
STACK_OF(X509_EXTENSION) *X509v3_add_ext(STACK_OF(X509_EXTENSION) **x,
120
    const X509_EXTENSION *ex, int loc)
121
0
{
122
0
    X509_EXTENSION *new_ex = NULL;
123
0
    int n;
124
0
    STACK_OF(X509_EXTENSION) *sk = NULL;
125
126
0
    if (x == NULL) {
127
0
        ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
128
0
        goto err;
129
0
    }
130
131
0
    if (*x == NULL) {
132
0
        if ((sk = sk_X509_EXTENSION_new_null()) == NULL) {
133
0
            ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB);
134
0
            goto err;
135
0
        }
136
0
    } else
137
0
        sk = *x;
138
139
0
    if (ossl_ignored_x509_extension(ex, X509V3_ADD_SILENT))
140
0
        goto done;
141
142
0
    n = sk_X509_EXTENSION_num(sk);
143
0
    if (loc > n)
144
0
        loc = n;
145
0
    else if (loc < 0)
146
0
        loc = n;
147
148
0
    if ((new_ex = X509_EXTENSION_dup(ex)) == NULL) {
149
0
        ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
150
0
        goto err;
151
0
    }
152
0
    if (!sk_X509_EXTENSION_insert(sk, new_ex, loc)) {
153
0
        ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB);
154
0
        goto err;
155
0
    }
156
0
done:
157
0
    if (*x == NULL)
158
0
        *x = sk;
159
0
    return sk;
160
0
err:
161
0
    X509_EXTENSION_free(new_ex);
162
0
    if (x != NULL && *x == NULL)
163
0
        sk_X509_EXTENSION_free(sk);
164
0
    return NULL;
165
0
}
166
167
/* This returns NULL also in non-error case *target == NULL && sk_X509_EXTENSION_num(exts) <= 0 */
168
STACK_OF(X509_EXTENSION) *X509v3_add_extensions(STACK_OF(X509_EXTENSION) **target,
169
    const STACK_OF(X509_EXTENSION) *exts)
170
0
{
171
0
    int i;
172
173
0
    if (target == NULL) {
174
0
        ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
175
0
        return NULL;
176
0
    }
177
178
0
    for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
179
0
        const X509_EXTENSION *ext = sk_X509_EXTENSION_value(exts, i);
180
0
        const ASN1_OBJECT *obj = X509_EXTENSION_get_object(ext);
181
0
        int idx = X509v3_get_ext_by_OBJ(*target, obj, -1);
182
183
        /* Does extension exist in target? */
184
0
        if (idx != -1) {
185
            /* Delete all extensions of same type */
186
0
            do {
187
0
                X509_EXTENSION_free(sk_X509_EXTENSION_delete(*target, idx));
188
0
                idx = X509v3_get_ext_by_OBJ(*target, obj, -1);
189
0
            } while (idx != -1);
190
0
        }
191
0
        if (!X509v3_add_ext(target, ext, -1))
192
0
            return NULL;
193
0
    }
194
0
    return *target;
195
0
}
196
197
X509_EXTENSION *X509_EXTENSION_create_by_NID(X509_EXTENSION **ex, int nid,
198
    int crit,
199
    ASN1_OCTET_STRING *data)
200
0
{
201
0
    ASN1_OBJECT *obj;
202
0
    X509_EXTENSION *ret;
203
204
0
    obj = OBJ_nid2obj(nid);
205
0
    if (obj == NULL) {
206
0
        ERR_raise(ERR_LIB_X509, X509_R_UNKNOWN_NID);
207
0
        return NULL;
208
0
    }
209
0
    ret = X509_EXTENSION_create_by_OBJ(ex, obj, crit, data);
210
0
    if (ret == NULL)
211
0
        ASN1_OBJECT_free(obj);
212
0
    return ret;
213
0
}
214
215
X509_EXTENSION *X509_EXTENSION_create_by_OBJ(X509_EXTENSION **ex,
216
    const ASN1_OBJECT *obj, int crit,
217
    ASN1_OCTET_STRING *data)
218
0
{
219
0
    X509_EXTENSION *ret;
220
221
0
    if ((ex == NULL) || (*ex == NULL)) {
222
0
        if ((ret = X509_EXTENSION_new()) == NULL) {
223
0
            ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
224
0
            return NULL;
225
0
        }
226
0
    } else
227
0
        ret = *ex;
228
229
0
    if (!X509_EXTENSION_set_object(ret, obj))
230
0
        goto err;
231
0
    if (!X509_EXTENSION_set_critical(ret, crit))
232
0
        goto err;
233
0
    if (!X509_EXTENSION_set_data(ret, data))
234
0
        goto err;
235
236
0
    if ((ex != NULL) && (*ex == NULL))
237
0
        *ex = ret;
238
0
    return ret;
239
0
err:
240
0
    if ((ex == NULL) || (ret != *ex))
241
0
        X509_EXTENSION_free(ret);
242
0
    return NULL;
243
0
}
244
245
int X509_EXTENSION_set_object(X509_EXTENSION *ex, const ASN1_OBJECT *obj)
246
0
{
247
0
    if ((ex == NULL) || (obj == NULL))
248
0
        return 0;
249
0
    ASN1_OBJECT_free(ex->object);
250
0
    ex->object = OBJ_dup(obj);
251
0
    return ex->object != NULL;
252
0
}
253
254
int X509_EXTENSION_set_critical(X509_EXTENSION *ex, int crit)
255
0
{
256
0
    if (ex == NULL)
257
0
        return 0;
258
0
    ex->critical = (crit) ? 0xFF : 0;
259
0
    return 1;
260
0
}
261
262
int X509_EXTENSION_set_data(X509_EXTENSION *ex, const ASN1_OCTET_STRING *data)
263
0
{
264
0
    int i;
265
266
0
    if (ex == NULL)
267
0
        return 0;
268
0
    i = ASN1_OCTET_STRING_set(&ex->value, data->data, data->length);
269
0
    if (!i)
270
0
        return 0;
271
0
    return 1;
272
0
}
273
274
const ASN1_OBJECT *X509_EXTENSION_get_object(const X509_EXTENSION *ex)
275
0
{
276
0
    if (ex == NULL)
277
0
        return NULL;
278
0
    return ex->object;
279
0
}
280
281
const ASN1_OCTET_STRING *X509_EXTENSION_get_data(const X509_EXTENSION *ex)
282
0
{
283
0
    if (ex == NULL)
284
0
        return NULL;
285
0
    return &ex->value;
286
0
}
287
288
int X509_EXTENSION_get_critical(const X509_EXTENSION *ex)
289
0
{
290
0
    if (ex == NULL)
291
0
        return 0;
292
0
    if (ex->critical > 0)
293
0
        return 1;
294
0
    return 0;
295
0
}