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