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