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