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