1 /* $OpenBSD: x509_lib.c,v 1.4 2022/07/24 21:41:29 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 "ext_dat.h"
67 #include "x509_lcl.h"
68 
69 static STACK_OF(X509V3_EXT_METHOD) *ext_list = NULL;
70 
71 static int ext_cmp(const X509V3_EXT_METHOD * const *a,
72     const X509V3_EXT_METHOD * const *b);
73 static void ext_list_free(X509V3_EXT_METHOD *ext);
74 
75 int
X509V3_EXT_add(X509V3_EXT_METHOD * ext)76 X509V3_EXT_add(X509V3_EXT_METHOD *ext)
77 {
78 	if (!ext_list && !(ext_list = sk_X509V3_EXT_METHOD_new(ext_cmp))) {
79 		X509V3error(ERR_R_MALLOC_FAILURE);
80 		return 0;
81 	}
82 	if (!sk_X509V3_EXT_METHOD_push(ext_list, ext)) {
83 		X509V3error(ERR_R_MALLOC_FAILURE);
84 		return 0;
85 	}
86 	return 1;
87 }
88 
89 static int
ext_cmp(const X509V3_EXT_METHOD * const * a,const X509V3_EXT_METHOD * const * b)90 ext_cmp(const X509V3_EXT_METHOD * const *a, const X509V3_EXT_METHOD * const *b)
91 {
92 	return ((*a)->ext_nid - (*b)->ext_nid);
93 }
94 
95 static int ext_cmp_BSEARCH_CMP_FN(const void *, const void *);
96 static int ext_cmp(const X509V3_EXT_METHOD * const *, const X509V3_EXT_METHOD * const *);
97 static const X509V3_EXT_METHOD * *OBJ_bsearch_ext(const X509V3_EXT_METHOD * *key, const X509V3_EXT_METHOD * const *base, int num);
98 
99 static int
ext_cmp_BSEARCH_CMP_FN(const void * a_,const void * b_)100 ext_cmp_BSEARCH_CMP_FN(const void *a_, const void *b_)
101 {
102 	const X509V3_EXT_METHOD * const *a = a_;
103 	const X509V3_EXT_METHOD * const *b = b_;
104 	return ext_cmp(a, b);
105 }
106 
107 static const X509V3_EXT_METHOD **
OBJ_bsearch_ext(const X509V3_EXT_METHOD ** key,const X509V3_EXT_METHOD * const * base,int num)108 OBJ_bsearch_ext(const X509V3_EXT_METHOD **key,
109     const X509V3_EXT_METHOD *const *base, int num)
110 {
111 	return (const X509V3_EXT_METHOD **)OBJ_bsearch_(key, base, num,
112 	    sizeof(const X509V3_EXT_METHOD *), ext_cmp_BSEARCH_CMP_FN);
113 }
114 
115 const X509V3_EXT_METHOD *
X509V3_EXT_get_nid(int nid)116 X509V3_EXT_get_nid(int nid)
117 {
118 	X509V3_EXT_METHOD tmp;
119 	const X509V3_EXT_METHOD *t = &tmp, * const *ret;
120 	int idx;
121 
122 	if (nid < 0)
123 		return NULL;
124 	tmp.ext_nid = nid;
125 	ret = OBJ_bsearch_ext(&t, standard_exts, STANDARD_EXTENSION_COUNT);
126 	if (ret)
127 		return *ret;
128 	if (!ext_list)
129 		return NULL;
130 	idx = sk_X509V3_EXT_METHOD_find(ext_list, &tmp);
131 	if (idx == -1)
132 		return NULL;
133 	return sk_X509V3_EXT_METHOD_value(ext_list, idx);
134 }
135 
136 const X509V3_EXT_METHOD *
X509V3_EXT_get(X509_EXTENSION * ext)137 X509V3_EXT_get(X509_EXTENSION *ext)
138 {
139 	int nid;
140 
141 	if ((nid = OBJ_obj2nid(ext->object)) == NID_undef)
142 		return NULL;
143 	return X509V3_EXT_get_nid(nid);
144 }
145 
146 int
X509V3_EXT_add_list(X509V3_EXT_METHOD * extlist)147 X509V3_EXT_add_list(X509V3_EXT_METHOD *extlist)
148 {
149 	for (; extlist->ext_nid!=-1; extlist++)
150 		if (!X509V3_EXT_add(extlist))
151 			return 0;
152 	return 1;
153 }
154 
155 int
X509V3_EXT_add_alias(int nid_to,int nid_from)156 X509V3_EXT_add_alias(int nid_to, int nid_from)
157 {
158 	const X509V3_EXT_METHOD *ext;
159 	X509V3_EXT_METHOD *tmpext;
160 
161 	if (!(ext = X509V3_EXT_get_nid(nid_from))) {
162 		X509V3error(X509V3_R_EXTENSION_NOT_FOUND);
163 		return 0;
164 	}
165 	if (!(tmpext = malloc(sizeof(X509V3_EXT_METHOD)))) {
166 		X509V3error(ERR_R_MALLOC_FAILURE);
167 		return 0;
168 	}
169 	*tmpext = *ext;
170 	tmpext->ext_nid = nid_to;
171 	tmpext->ext_flags |= X509V3_EXT_DYNAMIC;
172 	if (!X509V3_EXT_add(tmpext)) {
173 		free(tmpext);
174 		return 0;
175 	}
176 	return 1;
177 }
178 
179 void
X509V3_EXT_cleanup(void)180 X509V3_EXT_cleanup(void)
181 {
182 	sk_X509V3_EXT_METHOD_pop_free(ext_list, ext_list_free);
183 	ext_list = NULL;
184 }
185 
186 static void
ext_list_free(X509V3_EXT_METHOD * ext)187 ext_list_free(X509V3_EXT_METHOD *ext)
188 {
189 	if (ext->ext_flags & X509V3_EXT_DYNAMIC)
190 		free(ext);
191 }
192 
193 /* Legacy function: we don't need to add standard extensions
194  * any more because they are now kept in ext_dat.h.
195  */
196 
197 int
X509V3_add_standard_extensions(void)198 X509V3_add_standard_extensions(void)
199 {
200 	return 1;
201 }
202 
203 /* Return an extension internal structure */
204 
205 void *
X509V3_EXT_d2i(X509_EXTENSION * ext)206 X509V3_EXT_d2i(X509_EXTENSION *ext)
207 {
208 	const X509V3_EXT_METHOD *method;
209 	const unsigned char *p;
210 
211 	if (!(method = X509V3_EXT_get(ext)))
212 		return NULL;
213 	p = ext->value->data;
214 	if (method->it)
215 		return ASN1_item_d2i(NULL, &p, ext->value->length,
216 		    method->it);
217 	return method->d2i(NULL, &p, ext->value->length);
218 }
219 
220 /* Get critical flag and decoded version of extension from a NID.
221  * The "idx" variable returns the last found extension and can
222  * be used to retrieve multiple extensions of the same NID.
223  * However multiple extensions with the same NID is usually
224  * due to a badly encoded certificate so if idx is NULL we
225  * choke if multiple extensions exist.
226  * The "crit" variable is set to the critical value.
227  * The return value is the decoded extension or NULL on
228  * error. The actual error can have several different causes,
229  * the value of *crit reflects the cause:
230  * >= 0, extension found but not decoded (reflects critical value).
231  * -1 extension not found.
232  * -2 extension occurs more than once.
233  */
234 
235 void *
X509V3_get_d2i(const STACK_OF (X509_EXTENSION)* x,int nid,int * crit,int * idx)236 X509V3_get_d2i(const STACK_OF(X509_EXTENSION) *x, int nid, int *crit, int *idx)
237 {
238 	int lastpos, i;
239 	X509_EXTENSION *ex, *found_ex = NULL;
240 
241 	if (!x) {
242 		if (idx)
243 			*idx = -1;
244 		if (crit)
245 			*crit = -1;
246 		return NULL;
247 	}
248 	if (idx)
249 		lastpos = *idx + 1;
250 	else
251 		lastpos = 0;
252 	if (lastpos < 0)
253 		lastpos = 0;
254 	for (i = lastpos; i < sk_X509_EXTENSION_num(x); i++) {
255 		ex = sk_X509_EXTENSION_value(x, i);
256 		if (OBJ_obj2nid(ex->object) == nid) {
257 			if (idx) {
258 				*idx = i;
259 				found_ex = ex;
260 				break;
261 			} else if (found_ex) {
262 				/* Found more than one */
263 				if (crit)
264 					*crit = -2;
265 				return NULL;
266 			}
267 			found_ex = ex;
268 		}
269 	}
270 	if (found_ex) {
271 		/* Found it */
272 		if (crit)
273 			*crit = X509_EXTENSION_get_critical(found_ex);
274 		return X509V3_EXT_d2i(found_ex);
275 	}
276 
277 	/* Extension not found */
278 	if (idx)
279 		*idx = -1;
280 	if (crit)
281 		*crit = -1;
282 	return NULL;
283 }
284 
285 /* This function is a general extension append, replace and delete utility.
286  * The precise operation is governed by the 'flags' value. The 'crit' and
287  * 'value' arguments (if relevant) are the extensions internal structure.
288  */
289 
290 int
X509V3_add1_i2d(STACK_OF (X509_EXTENSION)** x,int nid,void * value,int crit,unsigned long flags)291 X509V3_add1_i2d(STACK_OF(X509_EXTENSION) **x, int nid, void *value,
292     int crit, unsigned long flags)
293 {
294 	int extidx = -1;
295 	int errcode;
296 	X509_EXTENSION *ext, *extmp;
297 	unsigned long ext_op = flags & X509V3_ADD_OP_MASK;
298 
299 	/* If appending we don't care if it exists, otherwise
300 	 * look for existing extension.
301 	 */
302 	if (ext_op != X509V3_ADD_APPEND)
303 		extidx = X509v3_get_ext_by_NID(*x, nid, -1);
304 
305 	/* See if extension exists */
306 	if (extidx >= 0) {
307 		/* If keep existing, nothing to do */
308 		if (ext_op == X509V3_ADD_KEEP_EXISTING)
309 			return 1;
310 		/* If default then its an error */
311 		if (ext_op == X509V3_ADD_DEFAULT) {
312 			errcode = X509V3_R_EXTENSION_EXISTS;
313 			goto err;
314 		}
315 		/* If delete, just delete it */
316 		if (ext_op == X509V3_ADD_DELETE) {
317 			if ((extmp = sk_X509_EXTENSION_delete(*x, extidx)) == NULL)
318 				return -1;
319 			X509_EXTENSION_free(extmp);
320 			return 1;
321 		}
322 	} else {
323 		/* If replace existing or delete, error since
324 		 * extension must exist
325 		 */
326 		if ((ext_op == X509V3_ADD_REPLACE_EXISTING) ||
327 		    (ext_op == X509V3_ADD_DELETE)) {
328 			errcode = X509V3_R_EXTENSION_NOT_FOUND;
329 			goto err;
330 		}
331 	}
332 
333 	/* If we get this far then we have to create an extension:
334 	 * could have some flags for alternative encoding schemes...
335 	 */
336 
337 	ext = X509V3_EXT_i2d(nid, crit, value);
338 
339 	if (!ext) {
340 		X509V3error(X509V3_R_ERROR_CREATING_EXTENSION);
341 		return 0;
342 	}
343 
344 	/* If extension exists replace it.. */
345 	if (extidx >= 0) {
346 		extmp = sk_X509_EXTENSION_value(*x, extidx);
347 		X509_EXTENSION_free(extmp);
348 		if (!sk_X509_EXTENSION_set(*x, extidx, ext))
349 			return -1;
350 		return 1;
351 	}
352 
353 	if (!*x && !(*x = sk_X509_EXTENSION_new_null()))
354 		return -1;
355 	if (!sk_X509_EXTENSION_push(*x, ext))
356 		return -1;
357 
358 	return 1;
359 
360 err:
361 	if (!(flags & X509V3_ADD_SILENT))
362 		X509V3error(errcode);
363 	return 0;
364 }
365