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