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