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