xref: /openbsd/lib/libcrypto/x509/x509_v3.c (revision b5e72661)
1 /* $OpenBSD: x509_v3.c,v 1.43 2024/07/12 09:57:04 tb Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  *
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  *
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  *
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  *
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58 
59 #include <stdio.h>
60 
61 #include <openssl/asn1.h>
62 #include <openssl/err.h>
63 #include <openssl/objects.h>
64 #include <openssl/stack.h>
65 #include <openssl/x509.h>
66 #include <openssl/x509v3.h>
67 
68 #include "x509_local.h"
69 
70 int
X509v3_get_ext_count(const STACK_OF (X509_EXTENSION)* exts)71 X509v3_get_ext_count(const STACK_OF(X509_EXTENSION) *exts)
72 {
73 	if (exts == NULL)
74 		return 0;
75 
76 	return sk_X509_EXTENSION_num(exts);
77 }
78 LCRYPTO_ALIAS(X509v3_get_ext_count);
79 
80 int
X509v3_get_ext_by_NID(const STACK_OF (X509_EXTENSION)* exts,int nid,int lastpos)81 X509v3_get_ext_by_NID(const STACK_OF(X509_EXTENSION) *exts, int nid, int lastpos)
82 {
83 	const ASN1_OBJECT *obj;
84 
85 	if ((obj = OBJ_nid2obj(nid)) == NULL)
86 		return -2;
87 
88 	return X509v3_get_ext_by_OBJ(exts, obj, lastpos);
89 }
90 LCRYPTO_ALIAS(X509v3_get_ext_by_NID);
91 
92 int
X509v3_get_ext_by_OBJ(const STACK_OF (X509_EXTENSION)* exts,const ASN1_OBJECT * obj,int lastpos)93 X509v3_get_ext_by_OBJ(const STACK_OF(X509_EXTENSION) *exts,
94     const ASN1_OBJECT *obj, int lastpos)
95 {
96 	if (++lastpos < 0)
97 		lastpos = 0;
98 
99 	for (; lastpos < X509v3_get_ext_count(exts); lastpos++) {
100 		const X509_EXTENSION *ext = X509v3_get_ext(exts, lastpos);
101 
102 		if (OBJ_cmp(ext->object, obj) == 0)
103 			return lastpos;
104 	}
105 
106 	return -1;
107 }
108 LCRYPTO_ALIAS(X509v3_get_ext_by_OBJ);
109 
110 int
X509v3_get_ext_by_critical(const STACK_OF (X509_EXTENSION)* exts,int critical,int lastpos)111 X509v3_get_ext_by_critical(const STACK_OF(X509_EXTENSION) *exts, int critical,
112     int lastpos)
113 {
114 	critical = (critical != 0);
115 
116 	if (++lastpos < 0)
117 		lastpos = 0;
118 
119 	for (; lastpos < X509v3_get_ext_count(exts); lastpos++) {
120 		const X509_EXTENSION *ext = X509v3_get_ext(exts, lastpos);
121 
122 		if (X509_EXTENSION_get_critical(ext) == critical)
123 			return lastpos;
124 	}
125 
126 	return -1;
127 }
128 LCRYPTO_ALIAS(X509v3_get_ext_by_critical);
129 
130 X509_EXTENSION *
X509v3_get_ext(const STACK_OF (X509_EXTENSION)* exts,int loc)131 X509v3_get_ext(const STACK_OF(X509_EXTENSION) *exts, int loc)
132 {
133 	return sk_X509_EXTENSION_value(exts, loc);
134 }
135 LCRYPTO_ALIAS(X509v3_get_ext);
136 
137 X509_EXTENSION *
X509v3_delete_ext(STACK_OF (X509_EXTENSION)* exts,int loc)138 X509v3_delete_ext(STACK_OF(X509_EXTENSION) *exts, int loc)
139 {
140 	return sk_X509_EXTENSION_delete(exts, loc);
141 }
142 LCRYPTO_ALIAS(X509v3_delete_ext);
143 
STACK_OF(X509_EXTENSION)144 STACK_OF(X509_EXTENSION) *
145 X509v3_add_ext(STACK_OF(X509_EXTENSION) **out_exts, X509_EXTENSION *ext, int loc)
146 {
147 	STACK_OF(X509_EXTENSION) *exts = NULL;
148 	X509_EXTENSION *new_ext = NULL;
149 
150 	/*
151 	 * XXX - Nonsense from the poorly reviewed OpenSSL c755c5fd8ba (2005).
152 	 * This check should have been joined with the next check, i.e., if no
153 	 * stack was passed in, a new one should be created and returned.
154 	 */
155 	if (out_exts == NULL) {
156 		X509error(ERR_R_PASSED_NULL_PARAMETER);
157 		goto err;
158 	}
159 
160 	if ((exts = *out_exts) == NULL)
161 		exts = sk_X509_EXTENSION_new_null();
162 	if (exts == NULL) {
163 		X509error(ERR_R_MALLOC_FAILURE);
164 		goto err;
165 	}
166 
167 	if ((new_ext = X509_EXTENSION_dup(ext)) == NULL)
168 		goto err;
169 	if (!sk_X509_EXTENSION_insert(exts, new_ext, loc))
170 		goto err;
171 	new_ext = NULL;
172 
173 	*out_exts = exts;
174 
175 	return exts;
176 
177  err:
178 	X509_EXTENSION_free(new_ext);
179 	if (out_exts != NULL && exts != *out_exts)
180 		sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
181 
182 	return NULL;
183 }
184 LCRYPTO_ALIAS(X509v3_add_ext);
185 
186 X509_EXTENSION *
X509_EXTENSION_create_by_NID(X509_EXTENSION ** out_ext,int nid,int critical,ASN1_OCTET_STRING * data)187 X509_EXTENSION_create_by_NID(X509_EXTENSION **out_ext, int nid, int critical,
188     ASN1_OCTET_STRING *data)
189 {
190 	const ASN1_OBJECT *obj;
191 
192 	if ((obj = OBJ_nid2obj(nid)) == NULL) {
193 		X509error(X509_R_UNKNOWN_NID);
194 		return NULL;
195 	}
196 
197 	return X509_EXTENSION_create_by_OBJ(out_ext, obj, critical, data);
198 }
199 LCRYPTO_ALIAS(X509_EXTENSION_create_by_NID);
200 
201 X509_EXTENSION *
X509_EXTENSION_create_by_OBJ(X509_EXTENSION ** out_ext,const ASN1_OBJECT * obj,int critical,ASN1_OCTET_STRING * data)202 X509_EXTENSION_create_by_OBJ(X509_EXTENSION **out_ext, const ASN1_OBJECT *obj,
203     int critical, ASN1_OCTET_STRING *data)
204 {
205 	X509_EXTENSION *ext;
206 
207 	if (out_ext == NULL || (ext = *out_ext) == NULL)
208 		ext = X509_EXTENSION_new();
209 	if (ext == NULL) {
210 		X509error(ERR_R_MALLOC_FAILURE);
211 		goto err;
212 	}
213 
214 	if (!X509_EXTENSION_set_object(ext, obj))
215 		goto err;
216 	if (!X509_EXTENSION_set_critical(ext, critical))
217 		goto err;
218 	if (!X509_EXTENSION_set_data(ext, data))
219 		goto err;
220 
221 	if (out_ext != NULL)
222 		*out_ext = ext;
223 
224 	return ext;
225 
226  err:
227 	if (out_ext == NULL || ext != *out_ext)
228 		X509_EXTENSION_free(ext);
229 
230 	return NULL;
231 }
232 LCRYPTO_ALIAS(X509_EXTENSION_create_by_OBJ);
233 
234 int
X509_EXTENSION_set_object(X509_EXTENSION * ext,const ASN1_OBJECT * obj)235 X509_EXTENSION_set_object(X509_EXTENSION *ext, const ASN1_OBJECT *obj)
236 {
237 	if (ext == NULL || obj == NULL)
238 		return 0;
239 
240 	ASN1_OBJECT_free(ext->object);
241 	return (ext->object = OBJ_dup(obj)) != NULL;
242 }
243 LCRYPTO_ALIAS(X509_EXTENSION_set_object);
244 
245 int
X509_EXTENSION_set_critical(X509_EXTENSION * ext,int critical)246 X509_EXTENSION_set_critical(X509_EXTENSION *ext, int critical)
247 {
248 	if (ext == NULL)
249 		return 0;
250 
251 	ext->critical = critical ? 0xFF : -1;
252 
253 	return 1;
254 }
255 LCRYPTO_ALIAS(X509_EXTENSION_set_critical);
256 
257 int
X509_EXTENSION_set_data(X509_EXTENSION * ext,ASN1_OCTET_STRING * data)258 X509_EXTENSION_set_data(X509_EXTENSION *ext, ASN1_OCTET_STRING *data)
259 {
260 	if (ext == NULL)
261 		return 0;
262 
263 	return ASN1_STRING_set(ext->value, data->data, data->length);
264 }
265 LCRYPTO_ALIAS(X509_EXTENSION_set_data);
266 
267 ASN1_OBJECT *
X509_EXTENSION_get_object(X509_EXTENSION * ext)268 X509_EXTENSION_get_object(X509_EXTENSION *ext)
269 {
270 	if (ext == NULL)
271 		return NULL;
272 
273 	return ext->object;
274 }
275 LCRYPTO_ALIAS(X509_EXTENSION_get_object);
276 
277 ASN1_OCTET_STRING *
X509_EXTENSION_get_data(X509_EXTENSION * ext)278 X509_EXTENSION_get_data(X509_EXTENSION *ext)
279 {
280 	if (ext == NULL)
281 		return NULL;
282 
283 	return ext->value;
284 }
285 LCRYPTO_ALIAS(X509_EXTENSION_get_data);
286 
287 int
X509_EXTENSION_get_critical(const X509_EXTENSION * ext)288 X509_EXTENSION_get_critical(const X509_EXTENSION *ext)
289 {
290 	if (ext == NULL)
291 		return 0;
292 
293 	return ext->critical > 0;
294 }
295 LCRYPTO_ALIAS(X509_EXTENSION_get_critical);
296