xref: /openbsd-src/lib/libcrypto/objects/obj_dat.c (revision 010af94e0ab4b8686746f9a02a7385fd85fc188b)
1 /* $OpenBSD: obj_dat.c,v 1.74 2023/12/14 18:09:35 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 <ctype.h>
60 #include <limits.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64 
65 #include <openssl/opensslconf.h>
66 
67 #include <openssl/asn1.h>
68 #include <openssl/bn.h>
69 #include <openssl/err.h>
70 #include <openssl/lhash.h>
71 #include <openssl/objects.h>
72 
73 #include "asn1_local.h"
74 
75 /* obj_dat.h is generated from objects.h by obj_dat.pl */
76 #include "obj_dat.h"
77 
78 #define ADDED_DATA	0
79 #define ADDED_SNAME	1
80 #define ADDED_LNAME	2
81 #define ADDED_NID	3
82 
83 typedef struct added_obj_st {
84 	int type;
85 	ASN1_OBJECT *obj;
86 } ADDED_OBJ;
87 DECLARE_LHASH_OF(ADDED_OBJ);
88 
89 static int new_nid = NUM_NID;
90 static LHASH_OF(ADDED_OBJ) *added = NULL;
91 
92 static unsigned long
93 added_obj_hash(const ADDED_OBJ *ca)
94 {
95 	const ASN1_OBJECT *a;
96 	int i;
97 	unsigned long ret = 0;
98 	unsigned char *p;
99 
100 	a = ca->obj;
101 	switch (ca->type) {
102 	case ADDED_DATA:
103 		ret = a->length << 20L;
104 		p = (unsigned char *)a->data;
105 		for (i = 0; i < a->length; i++)
106 			ret ^= p[i] << ((i * 3) % 24);
107 		break;
108 	case ADDED_SNAME:
109 		ret = lh_strhash(a->sn);
110 		break;
111 	case ADDED_LNAME:
112 		ret = lh_strhash(a->ln);
113 		break;
114 	case ADDED_NID:
115 		ret = a->nid;
116 		break;
117 	default:
118 		return 0;
119 	}
120 	ret &= 0x3fffffffL;
121 	ret |= ca->type << 30L;
122 	return (ret);
123 }
124 static IMPLEMENT_LHASH_HASH_FN(added_obj, ADDED_OBJ)
125 
126 static int
127 added_obj_cmp(const ADDED_OBJ *ca, const ADDED_OBJ *cb)
128 {
129 	const ASN1_OBJECT *a, *b;
130 	int cmp;
131 
132 	if ((cmp = ca->type - cb->type) != 0)
133 		return cmp;
134 
135 	a = ca->obj;
136 	b = cb->obj;
137 	switch (ca->type) {
138 	case ADDED_DATA:
139 		return OBJ_cmp(a, b);
140 	case ADDED_SNAME:
141 		if (a->sn == NULL)
142 			return -1;
143 		if (b->sn == NULL)
144 			return 1;
145 		return strcmp(a->sn, b->sn);
146 	case ADDED_LNAME:
147 		if (a->ln == NULL)
148 			return -1;
149 		if (b->ln == NULL)
150 			return 1;
151 		return strcmp(a->ln, b->ln);
152 	case ADDED_NID:
153 		return a->nid - b->nid;
154 	default:
155 		return 0;
156 	}
157 }
158 static IMPLEMENT_LHASH_COMP_FN(added_obj, ADDED_OBJ)
159 
160 static void
161 cleanup1_doall(ADDED_OBJ *a)
162 {
163 	a->obj->nid = 0;
164 	a->obj->flags |= ASN1_OBJECT_FLAG_DYNAMIC |
165 	    ASN1_OBJECT_FLAG_DYNAMIC_STRINGS |
166 	    ASN1_OBJECT_FLAG_DYNAMIC_DATA;
167 }
168 
169 static void cleanup2_doall(ADDED_OBJ *a)
170 {
171 	a->obj->nid++;
172 }
173 
174 static void
175 cleanup3_doall(ADDED_OBJ *a)
176 {
177 	if (--a->obj->nid == 0)
178 		ASN1_OBJECT_free(a->obj);
179 	free(a);
180 }
181 
182 static IMPLEMENT_LHASH_DOALL_FN(cleanup1, ADDED_OBJ)
183 static IMPLEMENT_LHASH_DOALL_FN(cleanup2, ADDED_OBJ)
184 static IMPLEMENT_LHASH_DOALL_FN(cleanup3, ADDED_OBJ)
185 
186 /* The purpose of obj_cleanup_defer is to avoid EVP_cleanup() attempting
187  * to use freed up OIDs. If necessary the actual freeing up of OIDs is
188  * delayed.
189  */
190 
191 int obj_cleanup_defer = 0;
192 
193 void
194 check_defer(int nid)
195 {
196 	if (!obj_cleanup_defer && nid >= NUM_NID)
197 		obj_cleanup_defer = 1;
198 }
199 
200 void
201 OBJ_cleanup(void)
202 {
203 	if (obj_cleanup_defer) {
204 		obj_cleanup_defer = 2;
205 		return;
206 	}
207 	if (added == NULL)
208 		return;
209 	lh_ADDED_OBJ_down_load(added) = 0;
210 	lh_ADDED_OBJ_doall(added, LHASH_DOALL_FN(cleanup1)); /* zero counters */
211 	lh_ADDED_OBJ_doall(added, LHASH_DOALL_FN(cleanup2)); /* set counters */
212 	lh_ADDED_OBJ_doall(added, LHASH_DOALL_FN(cleanup3)); /* free objects */
213 	lh_ADDED_OBJ_free(added);
214 	added = NULL;
215 }
216 LCRYPTO_ALIAS(OBJ_cleanup);
217 
218 int
219 OBJ_new_nid(int num)
220 {
221 	int i;
222 
223 	i = new_nid;
224 	new_nid += num;
225 	return (i);
226 }
227 LCRYPTO_ALIAS(OBJ_new_nid);
228 
229 int
230 OBJ_add_object(const ASN1_OBJECT *obj)
231 {
232 	ASN1_OBJECT *o = NULL;
233 	ADDED_OBJ *ao[4] = {NULL, NULL, NULL, NULL}, *aop;
234 	int i;
235 
236 	if (added == NULL)
237 		added = lh_ADDED_OBJ_new();
238 	if (added == NULL)
239 		goto err;
240 	if (obj == NULL || obj->nid == NID_undef)
241 		goto err;
242 	if ((o = OBJ_dup(obj)) == NULL)
243 		goto err;
244 	if (!(ao[ADDED_NID] = malloc(sizeof(ADDED_OBJ))))
245 		goto err2;
246 	if ((o->length != 0) && (obj->data != NULL))
247 		if (!(ao[ADDED_DATA] = malloc(sizeof(ADDED_OBJ))))
248 			goto err2;
249 	if (o->sn != NULL)
250 		if (!(ao[ADDED_SNAME] = malloc(sizeof(ADDED_OBJ))))
251 			goto err2;
252 	if (o->ln != NULL)
253 		if (!(ao[ADDED_LNAME] = malloc(sizeof(ADDED_OBJ))))
254 			goto err2;
255 
256 	for (i = ADDED_DATA; i <= ADDED_NID; i++) {
257 		if (ao[i] != NULL) {
258 			ao[i]->type = i;
259 			ao[i]->obj = o;
260 			aop = lh_ADDED_OBJ_insert(added, ao[i]);
261 			/* memory leak, but should not normally matter */
262 			free(aop);
263 		}
264 	}
265 	o->flags &= ~(ASN1_OBJECT_FLAG_DYNAMIC |
266 	    ASN1_OBJECT_FLAG_DYNAMIC_STRINGS |
267 	    ASN1_OBJECT_FLAG_DYNAMIC_DATA);
268 
269 	return (o->nid);
270 
271  err2:
272 	OBJerror(ERR_R_MALLOC_FAILURE);
273  err:
274 	for (i = ADDED_DATA; i <= ADDED_NID; i++)
275 		free(ao[i]);
276 	ASN1_OBJECT_free(o);
277 	return (NID_undef);
278 }
279 LCRYPTO_ALIAS(OBJ_add_object);
280 
281 ASN1_OBJECT *
282 OBJ_nid2obj(int nid)
283 {
284 	if (nid >= 0 && nid < NUM_NID) {
285 		if (nid == NID_undef || nid_objs[nid].nid != NID_undef)
286 			return (ASN1_OBJECT *)&nid_objs[nid];
287 
288 		goto unknown;
289 	}
290 
291 	/* XXX - locking. */
292 	if (added != NULL) {
293 		ASN1_OBJECT aobj = {
294 			.nid = nid,
295 		};
296 		ADDED_OBJ needle = {
297 			.type = ADDED_NID,
298 			.obj = &aobj,
299 		};
300 		ADDED_OBJ *found;
301 
302 		if ((found = lh_ADDED_OBJ_retrieve(added, &needle)) != NULL)
303 			return found->obj;
304 	}
305 
306  unknown:
307 	OBJerror(OBJ_R_UNKNOWN_NID);
308 
309 	return NULL;
310 }
311 LCRYPTO_ALIAS(OBJ_nid2obj);
312 
313 const char *
314 OBJ_nid2sn(int nid)
315 {
316 	ASN1_OBJECT *aobj;
317 
318 	if ((aobj = OBJ_nid2obj(nid)) == NULL)
319 		return NULL;
320 
321 	return aobj->sn;
322 }
323 LCRYPTO_ALIAS(OBJ_nid2sn);
324 
325 const char *
326 OBJ_nid2ln(int nid)
327 {
328 	ASN1_OBJECT *aobj;
329 
330 	if ((aobj = OBJ_nid2obj(nid)) == NULL)
331 		return NULL;
332 
333 	return aobj->ln;
334 }
335 LCRYPTO_ALIAS(OBJ_nid2ln);
336 
337 static int
338 obj_objs_cmp(const void *aobj, const void *b)
339 {
340 	const unsigned int *nid = b;
341 
342 	OPENSSL_assert(*nid >= 0 && *nid < NUM_NID);
343 
344 	return OBJ_cmp(aobj, &nid_objs[*nid]);
345 }
346 
347 int
348 OBJ_obj2nid(const ASN1_OBJECT *aobj)
349 {
350 	const unsigned int *nid;
351 
352 	if (aobj == NULL || aobj->length == 0)
353 		return NID_undef;
354 
355 	if (aobj->nid != NID_undef)
356 		return aobj->nid;
357 
358 	/* XXX - locking. OpenSSL 3 moved this after built-in object lookup. */
359 	if (added != NULL) {
360 		ADDED_OBJ needle = {
361 			.type = ADDED_DATA,
362 			.obj = (ASN1_OBJECT *)aobj,
363 		};
364 		ADDED_OBJ *found;
365 
366 		if ((found = lh_ADDED_OBJ_retrieve(added, &needle)) != NULL)
367 			return found->obj->nid;
368 	}
369 
370 	/* obj_objs holds built-in obj NIDs in ascending OBJ_cmp() order. */
371 	nid = bsearch(aobj, obj_objs, NUM_OBJ, sizeof(unsigned int), obj_objs_cmp);
372 	if (nid != NULL)
373 		return *nid;
374 
375 	return NID_undef;
376 }
377 LCRYPTO_ALIAS(OBJ_obj2nid);
378 
379 static int
380 ln_objs_cmp(const void *ln, const void *b)
381 {
382 	const unsigned int *nid = b;
383 
384 	OPENSSL_assert(*nid >= 0 && *nid < NUM_NID);
385 
386 	return strcmp(ln, nid_objs[*nid].ln);
387 }
388 
389 int
390 OBJ_ln2nid(const char *ln)
391 {
392 	const unsigned int *nid;
393 
394 	/* XXX - locking. OpenSSL 3 moved this after built-in object lookup. */
395 	if (added != NULL) {
396 		ASN1_OBJECT aobj = {
397 			.ln = ln,
398 		};
399 		ADDED_OBJ needle = {
400 			.type = ADDED_LNAME,
401 			.obj = &aobj,
402 		};
403 		ADDED_OBJ *found;
404 
405 		if ((found = lh_ADDED_OBJ_retrieve(added, &needle)) != NULL)
406 			return found->obj->nid;
407 	}
408 
409 	/* ln_objs holds NIDs in ascending alphabetical order of LN. */
410 	nid = bsearch(ln, ln_objs, NUM_LN, sizeof(unsigned int), ln_objs_cmp);
411 	if (nid != NULL)
412 		return *nid;
413 
414 	return NID_undef;
415 }
416 LCRYPTO_ALIAS(OBJ_ln2nid);
417 
418 static int
419 sn_objs_cmp(const void *sn, const void *b)
420 {
421 	const unsigned int *nid = b;
422 
423 	OPENSSL_assert(*nid >= 0 && *nid < NUM_NID);
424 
425 	return strcmp(sn, nid_objs[*nid].sn);
426 }
427 
428 int
429 OBJ_sn2nid(const char *sn)
430 {
431 	const unsigned int *nid;
432 
433 	/* XXX - locking. OpenSSL 3 moved this after built-in object lookup. */
434 	if (added != NULL) {
435 		ASN1_OBJECT aobj = {
436 			.sn = sn,
437 		};
438 		ADDED_OBJ needle = {
439 			.type = ADDED_SNAME,
440 			.obj = &aobj,
441 		};
442 		ADDED_OBJ *found;
443 
444 		if ((found = lh_ADDED_OBJ_retrieve(added, &needle)) != NULL)
445 			return found->obj->nid;
446 	}
447 
448 	/* sn_objs holds NIDs in ascending alphabetical order of SN. */
449 	nid = bsearch(sn, sn_objs, NUM_SN, sizeof(unsigned int), sn_objs_cmp);
450 	if (nid != NULL)
451 		return *nid;
452 
453 	return NID_undef;
454 }
455 LCRYPTO_ALIAS(OBJ_sn2nid);
456 
457 const void *
458 OBJ_bsearch_(const void *key, const void *base, int num, int size,
459     int (*cmp)(const void *, const void *))
460 {
461 	return OBJ_bsearch_ex_(key, base, num, size, cmp, 0);
462 }
463 LCRYPTO_ALIAS(OBJ_bsearch_);
464 
465 const void *
466 OBJ_bsearch_ex_(const void *key, const void *base_, int num, int size,
467     int (*cmp)(const void *, const void *), int flags)
468 {
469 	const char *base = base_;
470 	int l, h, i = 0, c = 0;
471 	const char *p = NULL;
472 
473 	if (num == 0)
474 		return (NULL);
475 	l = 0;
476 	h = num;
477 	while (l < h) {
478 		i = (l + h) / 2;
479 		p = &(base[i * size]);
480 		c = (*cmp)(key, p);
481 		if (c < 0)
482 			h = i;
483 		else if (c > 0)
484 			l = i + 1;
485 		else
486 			break;
487 	}
488 	if (c != 0 && !(flags & OBJ_BSEARCH_VALUE_ON_NOMATCH))
489 		p = NULL;
490 	else if (c == 0 && (flags & OBJ_BSEARCH_FIRST_VALUE_ON_MATCH)) {
491 		while (i > 0 && (*cmp)(key, &(base[(i - 1) * size])) == 0)
492 			i--;
493 		p = &(base[i * size]);
494 	}
495 	return (p);
496 }
497 
498 /* Convert an object name into an ASN1_OBJECT
499  * if "noname" is not set then search for short and long names first.
500  * This will convert the "dotted" form into an object: unlike OBJ_txt2nid
501  * it can be used with any objects, not just registered ones.
502  */
503 
504 ASN1_OBJECT *
505 OBJ_txt2obj(const char *s, int no_name)
506 {
507 	int nid;
508 
509 	if (!no_name) {
510 		if ((nid = OBJ_sn2nid(s)) != NID_undef ||
511 		    (nid = OBJ_ln2nid(s)) != NID_undef)
512 			return OBJ_nid2obj(nid);
513 	}
514 
515 	return t2i_ASN1_OBJECT_internal(s);
516 }
517 LCRYPTO_ALIAS(OBJ_txt2obj);
518 
519 int
520 OBJ_obj2txt(char *buf, int buf_len, const ASN1_OBJECT *aobj, int no_name)
521 {
522 	return i2t_ASN1_OBJECT_internal(aobj, buf, buf_len, no_name);
523 }
524 LCRYPTO_ALIAS(OBJ_obj2txt);
525 
526 int
527 OBJ_txt2nid(const char *s)
528 {
529 	ASN1_OBJECT *obj;
530 	int nid;
531 
532 	obj = OBJ_txt2obj(s, 0);
533 	nid = OBJ_obj2nid(obj);
534 	ASN1_OBJECT_free(obj);
535 	return nid;
536 }
537 LCRYPTO_ALIAS(OBJ_txt2nid);
538 
539 int
540 OBJ_create_objects(BIO *in)
541 {
542 	char buf[512];
543 	int i, num = 0;
544 	char *o, *s, *l = NULL;
545 
546 	for (;;) {
547 		s = o = NULL;
548 		i = BIO_gets(in, buf, 512);
549 		if (i <= 0)
550 			return (num);
551 		buf[i - 1] = '\0';
552 		if (!isalnum((unsigned char)buf[0]))
553 			return (num);
554 		o = s=buf;
555 		while (isdigit((unsigned char)*s) || (*s == '.'))
556 			s++;
557 		if (*s != '\0') {
558 			*(s++) = '\0';
559 			while (isspace((unsigned char)*s))
560 				s++;
561 			if (*s == '\0')
562 				s = NULL;
563 			else {
564 				l = s;
565 				while ((*l != '\0') &&
566 				    !isspace((unsigned char)*l))
567 					l++;
568 				if (*l != '\0') {
569 					*(l++) = '\0';
570 					while (isspace((unsigned char)*l))
571 						l++;
572 					if (*l == '\0')
573 						l = NULL;
574 				} else
575 					l = NULL;
576 			}
577 		} else
578 			s = NULL;
579 		if ((o == NULL) || (*o == '\0'))
580 			return (num);
581 		if (!OBJ_create(o, s, l))
582 			return (num);
583 		num++;
584 	}
585 	/* return(num); */
586 }
587 LCRYPTO_ALIAS(OBJ_create_objects);
588 
589 int
590 OBJ_create(const char *oid, const char *sn, const char *ln)
591 {
592 	int ok = 0;
593 	ASN1_OBJECT *op = NULL;
594 	unsigned char *buf;
595 	int len;
596 
597 	len = a2d_ASN1_OBJECT(NULL, 0, oid, -1);
598 	if (len <= 0)
599 		return 0;
600 
601 	if ((buf = malloc(len)) == NULL) {
602 		OBJerror(ERR_R_MALLOC_FAILURE);
603 		return 0;
604 	}
605 	len = a2d_ASN1_OBJECT(buf, len, oid, -1);
606 	if (len == 0)
607 		goto err;
608 	op = ASN1_OBJECT_create(OBJ_new_nid(1), buf, len, sn, ln);
609 	if (op == NULL)
610 		goto err;
611 	ok = OBJ_add_object(op);
612 
613  err:
614 	ASN1_OBJECT_free(op);
615 	free(buf);
616 	return ok;
617 }
618 LCRYPTO_ALIAS(OBJ_create);
619 
620 size_t
621 OBJ_length(const ASN1_OBJECT *obj)
622 {
623 	if (obj == NULL)
624 		return 0;
625 
626 	if (obj->length < 0)
627 		return 0;
628 
629 	return obj->length;
630 }
631 LCRYPTO_ALIAS(OBJ_length);
632 
633 const unsigned char *
634 OBJ_get0_data(const ASN1_OBJECT *obj)
635 {
636 	if (obj == NULL)
637 		return NULL;
638 
639 	return obj->data;
640 }
641 LCRYPTO_ALIAS(OBJ_get0_data);
642