Coverage Report

Created: 2024-05-21 06:33

/src/openssl/crypto/x509/x509_v3.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2020 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
int X509v3_get_ext_count(const STACK_OF(X509_EXTENSION) *x)
21
0
{
22
0
    int ret;
23
24
0
    if (x == NULL)
25
0
        return 0;
26
0
    ret = sk_X509_EXTENSION_num(x);
27
0
    return ret > 0 ? ret : 0;
28
0
}
29
30
int X509v3_get_ext_by_NID(const STACK_OF(X509_EXTENSION) *x, int nid,
31
                          int lastpos)
32
0
{
33
0
    ASN1_OBJECT *obj;
34
35
0
    obj = OBJ_nid2obj(nid);
36
0
    if (obj == NULL)
37
0
        return -2;
38
0
    return X509v3_get_ext_by_OBJ(x, obj, lastpos);
39
0
}
40
41
int X509v3_get_ext_by_OBJ(const STACK_OF(X509_EXTENSION) *sk,
42
                          const ASN1_OBJECT *obj, int lastpos)
43
0
{
44
0
    int n;
45
0
    X509_EXTENSION *ex;
46
47
0
    if (sk == NULL)
48
0
        return -1;
49
0
    lastpos++;
50
0
    if (lastpos < 0)
51
0
        lastpos = 0;
52
0
    n = sk_X509_EXTENSION_num(sk);
53
0
    for (; lastpos < n; lastpos++) {
54
0
        ex = sk_X509_EXTENSION_value(sk, lastpos);
55
0
        if (OBJ_cmp(ex->object, obj) == 0)
56
0
            return lastpos;
57
0
    }
58
0
    return -1;
59
0
}
60
61
int X509v3_get_ext_by_critical(const STACK_OF(X509_EXTENSION) *sk, int crit,
62
                               int lastpos)
63
0
{
64
0
    int n, c;
65
0
    X509_EXTENSION *ex;
66
67
0
    if (sk == NULL)
68
0
        return -1;
69
0
    lastpos++;
70
0
    if (lastpos < 0)
71
0
        lastpos = 0;
72
0
    n = sk_X509_EXTENSION_num(sk);
73
0
    for (; lastpos < n; lastpos++) {
74
0
        ex = sk_X509_EXTENSION_value(sk, lastpos);
75
0
        c = X509_EXTENSION_get_critical(ex);
76
0
        crit = crit != 0;
77
0
        if (c == crit)
78
0
            return lastpos;
79
0
    }
80
0
    return -1;
81
0
}
82
83
X509_EXTENSION *X509v3_get_ext(const STACK_OF(X509_EXTENSION) *x, int loc)
84
0
{
85
0
    if (x == NULL || sk_X509_EXTENSION_num(x) <= loc || loc < 0)
86
0
        return NULL;
87
0
    else
88
0
        return sk_X509_EXTENSION_value(x, loc);
89
0
}
90
91
X509_EXTENSION *X509v3_delete_ext(STACK_OF(X509_EXTENSION) *x, int loc)
92
0
{
93
0
    X509_EXTENSION *ret;
94
95
0
    if (x == NULL || sk_X509_EXTENSION_num(x) <= loc || loc < 0)
96
0
        return NULL;
97
0
    ret = sk_X509_EXTENSION_delete(x, loc);
98
0
    return ret;
99
0
}
100
101
STACK_OF(X509_EXTENSION) *X509v3_add_ext(STACK_OF(X509_EXTENSION) **x,
102
                                         X509_EXTENSION *ex, int loc)
103
0
{
104
0
    X509_EXTENSION *new_ex = NULL;
105
0
    int n;
106
0
    STACK_OF(X509_EXTENSION) *sk = NULL;
107
108
0
    if (x == NULL) {
109
0
        ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
110
0
        goto err;
111
0
    }
112
113
0
    if (*x == NULL) {
114
0
        if ((sk = sk_X509_EXTENSION_new_null()) == NULL) {
115
0
            ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB);
116
0
            goto err;
117
0
        }
118
0
    } else
119
0
        sk = *x;
120
121
0
    n = sk_X509_EXTENSION_num(sk);
122
0
    if (loc > n)
123
0
        loc = n;
124
0
    else if (loc < 0)
125
0
        loc = n;
126
127
0
    if ((new_ex = X509_EXTENSION_dup(ex)) == NULL) {
128
0
        ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
129
0
        goto err;
130
0
    }
131
0
    if (!sk_X509_EXTENSION_insert(sk, new_ex, loc)) {
132
0
        ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB);
133
0
        goto err;
134
0
    }
135
0
    if (*x == NULL)
136
0
        *x = sk;
137
0
    return sk;
138
0
 err:
139
0
    X509_EXTENSION_free(new_ex);
140
0
    if (x != NULL && *x == NULL)
141
0
        sk_X509_EXTENSION_free(sk);
142
0
    return NULL;
143
0
}
144
145
X509_EXTENSION *X509_EXTENSION_create_by_NID(X509_EXTENSION **ex, int nid,
146
                                             int crit,
147
                                             ASN1_OCTET_STRING *data)
148
0
{
149
0
    ASN1_OBJECT *obj;
150
0
    X509_EXTENSION *ret;
151
152
0
    obj = OBJ_nid2obj(nid);
153
0
    if (obj == NULL) {
154
0
        ERR_raise(ERR_LIB_X509, X509_R_UNKNOWN_NID);
155
0
        return NULL;
156
0
    }
157
0
    ret = X509_EXTENSION_create_by_OBJ(ex, obj, crit, data);
158
0
    if (ret == NULL)
159
0
        ASN1_OBJECT_free(obj);
160
0
    return ret;
161
0
}
162
163
X509_EXTENSION *X509_EXTENSION_create_by_OBJ(X509_EXTENSION **ex,
164
                                             const ASN1_OBJECT *obj, int crit,
165
                                             ASN1_OCTET_STRING *data)
166
0
{
167
0
    X509_EXTENSION *ret;
168
169
0
    if ((ex == NULL) || (*ex == NULL)) {
170
0
        if ((ret = X509_EXTENSION_new()) == NULL) {
171
0
            ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
172
0
            return NULL;
173
0
        }
174
0
    } else
175
0
        ret = *ex;
176
177
0
    if (!X509_EXTENSION_set_object(ret, obj))
178
0
        goto err;
179
0
    if (!X509_EXTENSION_set_critical(ret, crit))
180
0
        goto err;
181
0
    if (!X509_EXTENSION_set_data(ret, data))
182
0
        goto err;
183
184
0
    if ((ex != NULL) && (*ex == NULL))
185
0
        *ex = ret;
186
0
    return ret;
187
0
 err:
188
0
    if ((ex == NULL) || (ret != *ex))
189
0
        X509_EXTENSION_free(ret);
190
0
    return NULL;
191
0
}
192
193
int X509_EXTENSION_set_object(X509_EXTENSION *ex, const ASN1_OBJECT *obj)
194
0
{
195
0
    if ((ex == NULL) || (obj == NULL))
196
0
        return 0;
197
0
    ASN1_OBJECT_free(ex->object);
198
0
    ex->object = OBJ_dup(obj);
199
0
    return ex->object != NULL;
200
0
}
201
202
int X509_EXTENSION_set_critical(X509_EXTENSION *ex, int crit)
203
0
{
204
0
    if (ex == NULL)
205
0
        return 0;
206
0
    ex->critical = (crit) ? 0xFF : 0;
207
0
    return 1;
208
0
}
209
210
int X509_EXTENSION_set_data(X509_EXTENSION *ex, ASN1_OCTET_STRING *data)
211
0
{
212
0
    int i;
213
214
0
    if (ex == NULL)
215
0
        return 0;
216
0
    i = ASN1_OCTET_STRING_set(&ex->value, data->data, data->length);
217
0
    if (!i)
218
0
        return 0;
219
0
    return 1;
220
0
}
221
222
ASN1_OBJECT *X509_EXTENSION_get_object(X509_EXTENSION *ex)
223
0
{
224
0
    if (ex == NULL)
225
0
        return NULL;
226
0
    return ex->object;
227
0
}
228
229
ASN1_OCTET_STRING *X509_EXTENSION_get_data(X509_EXTENSION *ex)
230
0
{
231
0
    if (ex == NULL)
232
0
        return NULL;
233
0
    return &ex->value;
234
0
}
235
236
int X509_EXTENSION_get_critical(const X509_EXTENSION *ex)
237
0
{
238
0
    if (ex == NULL)
239
0
        return 0;
240
0
    if (ex->critical > 0)
241
0
        return 1;
242
0
    return 0;
243
0
}