xref: /openbsd-src/lib/libcrypto/x509/x509_lib.c (revision b1d78d1adba4ade8cc49ae15a997f2256193d690)
1 /* $OpenBSD: x509_lib.c,v 1.9 2023/04/16 11:59:50 tb Exp $ */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3  * project 1999.
4  */
5 /* ====================================================================
6  * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    licensing@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This product includes cryptographic software written by Eric Young
54  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com).
56  *
57  */
58 /* X509 v3 extension utilities */
59 
60 #include <stdio.h>
61 
62 #include <openssl/conf.h>
63 #include <openssl/err.h>
64 #include <openssl/x509v3.h>
65 
66 #include "x509_local.h"
67 
68 static STACK_OF(X509V3_EXT_METHOD) *ext_list = NULL;
69 
70 static int ext_cmp(const X509V3_EXT_METHOD * const *a,
71     const X509V3_EXT_METHOD * const *b);
72 static void ext_list_free(X509V3_EXT_METHOD *ext);
73 
74 extern X509V3_EXT_METHOD v3_bcons, v3_nscert, v3_key_usage, v3_ext_ku;
75 extern X509V3_EXT_METHOD v3_pkey_usage_period, v3_sxnet, v3_info, v3_sinfo;
76 extern X509V3_EXT_METHOD v3_ns_ia5_list[], v3_alt[], v3_skey_id, v3_akey_id;
77 extern X509V3_EXT_METHOD v3_crl_num, v3_crl_reason, v3_crl_invdate;
78 extern X509V3_EXT_METHOD v3_delta_crl, v3_cpols, v3_crld, v3_freshest_crl;
79 extern X509V3_EXT_METHOD v3_ocsp_nonce, v3_ocsp_accresp, v3_ocsp_acutoff;
80 extern X509V3_EXT_METHOD v3_ocsp_crlid, v3_ocsp_nocheck, v3_ocsp_serviceloc;
81 extern X509V3_EXT_METHOD v3_crl_hold;
82 extern X509V3_EXT_METHOD v3_policy_mappings, v3_policy_constraints;
83 extern X509V3_EXT_METHOD v3_name_constraints, v3_inhibit_anyp, v3_idp;
84 extern const X509V3_EXT_METHOD v3_addr, v3_asid;
85 extern const X509V3_EXT_METHOD v3_ct_scts[3];
86 
87 /*
88  * This table needs to be sorted by increasing ext_nid values for OBJ_bsearch_.
89  */
90 
91 static const X509V3_EXT_METHOD *standard_exts[] = {
92 	&v3_nscert,
93 	&v3_ns_ia5_list[0],
94 	&v3_ns_ia5_list[1],
95 	&v3_ns_ia5_list[2],
96 	&v3_ns_ia5_list[3],
97 	&v3_ns_ia5_list[4],
98 	&v3_ns_ia5_list[5],
99 	&v3_ns_ia5_list[6],
100 	&v3_skey_id,
101 	&v3_key_usage,
102 	&v3_pkey_usage_period,
103 	&v3_alt[0],
104 	&v3_alt[1],
105 	&v3_bcons,
106 	&v3_crl_num,
107 	&v3_cpols,
108 	&v3_akey_id,
109 	&v3_crld,
110 	&v3_ext_ku,
111 	&v3_delta_crl,
112 	&v3_crl_reason,
113 #ifndef OPENSSL_NO_OCSP
114 	&v3_crl_invdate,
115 #endif
116 	&v3_sxnet,
117 	&v3_info,
118 #ifndef OPENSSL_NO_RFC3779
119 	&v3_addr,
120 	&v3_asid,
121 #endif
122 #ifndef OPENSSL_NO_OCSP
123 	&v3_ocsp_nonce,
124 	&v3_ocsp_crlid,
125 	&v3_ocsp_accresp,
126 	&v3_ocsp_nocheck,
127 	&v3_ocsp_acutoff,
128 	&v3_ocsp_serviceloc,
129 #endif
130 	&v3_sinfo,
131 	&v3_policy_constraints,
132 #ifndef OPENSSL_NO_OCSP
133 	&v3_crl_hold,
134 #endif
135 	&v3_name_constraints,
136 	&v3_policy_mappings,
137 	&v3_inhibit_anyp,
138 	&v3_idp,
139 	&v3_alt[2],
140 	&v3_freshest_crl,
141 #ifndef OPENSSL_NO_CT
142 	&v3_ct_scts[0],
143 	&v3_ct_scts[1],
144 	&v3_ct_scts[2],
145 #endif
146 };
147 
148 #define STANDARD_EXTENSION_COUNT (sizeof(standard_exts)/sizeof(X509V3_EXT_METHOD *))
149 
150 int
151 X509V3_EXT_add(X509V3_EXT_METHOD *ext)
152 {
153 	if (!ext_list && !(ext_list = sk_X509V3_EXT_METHOD_new(ext_cmp))) {
154 		X509V3error(ERR_R_MALLOC_FAILURE);
155 		return 0;
156 	}
157 	if (!sk_X509V3_EXT_METHOD_push(ext_list, ext)) {
158 		X509V3error(ERR_R_MALLOC_FAILURE);
159 		return 0;
160 	}
161 	return 1;
162 }
163 LCRYPTO_ALIAS(X509V3_EXT_add);
164 
165 static int
166 ext_cmp(const X509V3_EXT_METHOD * const *a, const X509V3_EXT_METHOD * const *b)
167 {
168 	return ((*a)->ext_nid - (*b)->ext_nid);
169 }
170 
171 static int ext_cmp_BSEARCH_CMP_FN(const void *, const void *);
172 static int ext_cmp(const X509V3_EXT_METHOD * const *, const X509V3_EXT_METHOD * const *);
173 static const X509V3_EXT_METHOD * *OBJ_bsearch_ext(const X509V3_EXT_METHOD * *key, const X509V3_EXT_METHOD * const *base, int num);
174 
175 static int
176 ext_cmp_BSEARCH_CMP_FN(const void *a_, const void *b_)
177 {
178 	const X509V3_EXT_METHOD * const *a = a_;
179 	const X509V3_EXT_METHOD * const *b = b_;
180 	return ext_cmp(a, b);
181 }
182 
183 static const X509V3_EXT_METHOD **
184 OBJ_bsearch_ext(const X509V3_EXT_METHOD **key,
185     const X509V3_EXT_METHOD *const *base, int num)
186 {
187 	return (const X509V3_EXT_METHOD **)OBJ_bsearch_(key, base, num,
188 	    sizeof(const X509V3_EXT_METHOD *), ext_cmp_BSEARCH_CMP_FN);
189 }
190 
191 const X509V3_EXT_METHOD *
192 X509V3_EXT_get_nid(int nid)
193 {
194 	X509V3_EXT_METHOD tmp;
195 	const X509V3_EXT_METHOD *t = &tmp, * const *ret;
196 	int idx;
197 
198 	if (nid < 0)
199 		return NULL;
200 	tmp.ext_nid = nid;
201 	ret = OBJ_bsearch_ext(&t, standard_exts, STANDARD_EXTENSION_COUNT);
202 	if (ret)
203 		return *ret;
204 	if (!ext_list)
205 		return NULL;
206 	idx = sk_X509V3_EXT_METHOD_find(ext_list, &tmp);
207 	if (idx == -1)
208 		return NULL;
209 	return sk_X509V3_EXT_METHOD_value(ext_list, idx);
210 }
211 LCRYPTO_ALIAS(X509V3_EXT_get_nid);
212 
213 const X509V3_EXT_METHOD *
214 X509V3_EXT_get(X509_EXTENSION *ext)
215 {
216 	int nid;
217 
218 	if ((nid = OBJ_obj2nid(ext->object)) == NID_undef)
219 		return NULL;
220 	return X509V3_EXT_get_nid(nid);
221 }
222 LCRYPTO_ALIAS(X509V3_EXT_get);
223 
224 int
225 X509V3_EXT_add_list(X509V3_EXT_METHOD *extlist)
226 {
227 	for (; extlist->ext_nid!=-1; extlist++)
228 		if (!X509V3_EXT_add(extlist))
229 			return 0;
230 	return 1;
231 }
232 LCRYPTO_ALIAS(X509V3_EXT_add_list);
233 
234 int
235 X509V3_EXT_add_alias(int nid_to, int nid_from)
236 {
237 	const X509V3_EXT_METHOD *ext;
238 	X509V3_EXT_METHOD *tmpext;
239 
240 	if (!(ext = X509V3_EXT_get_nid(nid_from))) {
241 		X509V3error(X509V3_R_EXTENSION_NOT_FOUND);
242 		return 0;
243 	}
244 	if (!(tmpext = malloc(sizeof(X509V3_EXT_METHOD)))) {
245 		X509V3error(ERR_R_MALLOC_FAILURE);
246 		return 0;
247 	}
248 	*tmpext = *ext;
249 	tmpext->ext_nid = nid_to;
250 	tmpext->ext_flags |= X509V3_EXT_DYNAMIC;
251 	if (!X509V3_EXT_add(tmpext)) {
252 		free(tmpext);
253 		return 0;
254 	}
255 	return 1;
256 }
257 LCRYPTO_ALIAS(X509V3_EXT_add_alias);
258 
259 void
260 X509V3_EXT_cleanup(void)
261 {
262 	sk_X509V3_EXT_METHOD_pop_free(ext_list, ext_list_free);
263 	ext_list = NULL;
264 }
265 LCRYPTO_ALIAS(X509V3_EXT_cleanup);
266 
267 static void
268 ext_list_free(X509V3_EXT_METHOD *ext)
269 {
270 	if (ext->ext_flags & X509V3_EXT_DYNAMIC)
271 		free(ext);
272 }
273 
274 int
275 X509V3_add_standard_extensions(void)
276 {
277 	return 1;
278 }
279 LCRYPTO_ALIAS(X509V3_add_standard_extensions);
280 
281 /* Return an extension internal structure */
282 
283 void *
284 X509V3_EXT_d2i(X509_EXTENSION *ext)
285 {
286 	const X509V3_EXT_METHOD *method;
287 	const unsigned char *p;
288 
289 	if (!(method = X509V3_EXT_get(ext)))
290 		return NULL;
291 	p = ext->value->data;
292 	if (method->it)
293 		return ASN1_item_d2i(NULL, &p, ext->value->length,
294 		    method->it);
295 	return method->d2i(NULL, &p, ext->value->length);
296 }
297 LCRYPTO_ALIAS(X509V3_EXT_d2i);
298 
299 /* Get critical flag and decoded version of extension from a NID.
300  * The "idx" variable returns the last found extension and can
301  * be used to retrieve multiple extensions of the same NID.
302  * However multiple extensions with the same NID is usually
303  * due to a badly encoded certificate so if idx is NULL we
304  * choke if multiple extensions exist.
305  * The "crit" variable is set to the critical value.
306  * The return value is the decoded extension or NULL on
307  * error. The actual error can have several different causes,
308  * the value of *crit reflects the cause:
309  * >= 0, extension found but not decoded (reflects critical value).
310  * -1 extension not found.
311  * -2 extension occurs more than once.
312  */
313 
314 void *
315 X509V3_get_d2i(const STACK_OF(X509_EXTENSION) *x, int nid, int *crit, int *idx)
316 {
317 	int lastpos, i;
318 	X509_EXTENSION *ex, *found_ex = NULL;
319 
320 	if (!x) {
321 		if (idx)
322 			*idx = -1;
323 		if (crit)
324 			*crit = -1;
325 		return NULL;
326 	}
327 	if (idx)
328 		lastpos = *idx + 1;
329 	else
330 		lastpos = 0;
331 	if (lastpos < 0)
332 		lastpos = 0;
333 	for (i = lastpos; i < sk_X509_EXTENSION_num(x); i++) {
334 		ex = sk_X509_EXTENSION_value(x, i);
335 		if (OBJ_obj2nid(ex->object) == nid) {
336 			if (idx) {
337 				*idx = i;
338 				found_ex = ex;
339 				break;
340 			} else if (found_ex) {
341 				/* Found more than one */
342 				if (crit)
343 					*crit = -2;
344 				return NULL;
345 			}
346 			found_ex = ex;
347 		}
348 	}
349 	if (found_ex) {
350 		/* Found it */
351 		if (crit)
352 			*crit = X509_EXTENSION_get_critical(found_ex);
353 		return X509V3_EXT_d2i(found_ex);
354 	}
355 
356 	/* Extension not found */
357 	if (idx)
358 		*idx = -1;
359 	if (crit)
360 		*crit = -1;
361 	return NULL;
362 }
363 LCRYPTO_ALIAS(X509V3_get_d2i);
364 
365 /* This function is a general extension append, replace and delete utility.
366  * The precise operation is governed by the 'flags' value. The 'crit' and
367  * 'value' arguments (if relevant) are the extensions internal structure.
368  */
369 
370 int
371 X509V3_add1_i2d(STACK_OF(X509_EXTENSION) **x, int nid, void *value,
372     int crit, unsigned long flags)
373 {
374 	int extidx = -1;
375 	int errcode;
376 	X509_EXTENSION *ext, *extmp;
377 	unsigned long ext_op = flags & X509V3_ADD_OP_MASK;
378 
379 	/* If appending we don't care if it exists, otherwise
380 	 * look for existing extension.
381 	 */
382 	if (ext_op != X509V3_ADD_APPEND)
383 		extidx = X509v3_get_ext_by_NID(*x, nid, -1);
384 
385 	/* See if extension exists */
386 	if (extidx >= 0) {
387 		/* If keep existing, nothing to do */
388 		if (ext_op == X509V3_ADD_KEEP_EXISTING)
389 			return 1;
390 		/* If default then its an error */
391 		if (ext_op == X509V3_ADD_DEFAULT) {
392 			errcode = X509V3_R_EXTENSION_EXISTS;
393 			goto err;
394 		}
395 		/* If delete, just delete it */
396 		if (ext_op == X509V3_ADD_DELETE) {
397 			if ((extmp = sk_X509_EXTENSION_delete(*x, extidx)) == NULL)
398 				return -1;
399 			X509_EXTENSION_free(extmp);
400 			return 1;
401 		}
402 	} else {
403 		/* If replace existing or delete, error since
404 		 * extension must exist
405 		 */
406 		if ((ext_op == X509V3_ADD_REPLACE_EXISTING) ||
407 		    (ext_op == X509V3_ADD_DELETE)) {
408 			errcode = X509V3_R_EXTENSION_NOT_FOUND;
409 			goto err;
410 		}
411 	}
412 
413 	/* If we get this far then we have to create an extension:
414 	 * could have some flags for alternative encoding schemes...
415 	 */
416 
417 	ext = X509V3_EXT_i2d(nid, crit, value);
418 
419 	if (!ext) {
420 		X509V3error(X509V3_R_ERROR_CREATING_EXTENSION);
421 		return 0;
422 	}
423 
424 	/* If extension exists replace it.. */
425 	if (extidx >= 0) {
426 		extmp = sk_X509_EXTENSION_value(*x, extidx);
427 		X509_EXTENSION_free(extmp);
428 		if (!sk_X509_EXTENSION_set(*x, extidx, ext))
429 			return -1;
430 		return 1;
431 	}
432 
433 	if (!*x && !(*x = sk_X509_EXTENSION_new_null()))
434 		return -1;
435 	if (!sk_X509_EXTENSION_push(*x, ext))
436 		return -1;
437 
438 	return 1;
439 
440 err:
441 	if (!(flags & X509V3_ADD_SILENT))
442 		X509V3error(errcode);
443 	return 0;
444 }
445 LCRYPTO_ALIAS(X509V3_add1_i2d);
446