2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include "internal/ctype.h"
13 #include "internal/cryptlib.h"
14 #include <openssl/lhash.h>
15 #include <openssl/asn1.h>
16 #include "internal/objects.h"
17 #include <openssl/bn.h>
18 #include "internal/asn1_int.h"
21 /* obj_dat.h is generated from objects.h by obj_dat.pl */
24 DECLARE_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT
*, unsigned int, sn
);
25 DECLARE_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT
*, unsigned int, ln
);
26 DECLARE_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT
*, unsigned int, obj
);
38 static int new_nid
= NUM_NID
;
39 static LHASH_OF(ADDED_OBJ
) *added
= NULL
;
41 static int sn_cmp(const ASN1_OBJECT
*const *a
, const unsigned int *b
)
43 return strcmp((*a
)->sn
, nid_objs
[*b
].sn
);
46 IMPLEMENT_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT
*, unsigned int, sn
);
48 static int ln_cmp(const ASN1_OBJECT
*const *a
, const unsigned int *b
)
50 return strcmp((*a
)->ln
, nid_objs
[*b
].ln
);
53 IMPLEMENT_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT
*, unsigned int, ln
);
55 static unsigned long added_obj_hash(const ADDED_OBJ
*ca
)
59 unsigned long ret
= 0;
65 ret
= a
->length
<< 20L;
66 p
= (unsigned char *)a
->data
;
67 for (i
= 0; i
< a
->length
; i
++)
68 ret
^= p
[i
] << ((i
* 3) % 24);
71 ret
= OPENSSL_LH_strhash(a
->sn
);
74 ret
= OPENSSL_LH_strhash(a
->ln
);
84 ret
|= ((unsigned long)ca
->type
) << 30L;
88 static int added_obj_cmp(const ADDED_OBJ
*ca
, const ADDED_OBJ
*cb
)
93 i
= ca
->type
- cb
->type
;
100 i
= (a
->length
- b
->length
);
103 return memcmp(a
->data
, b
->data
, (size_t)a
->length
);
107 else if (b
->sn
== NULL
)
110 return strcmp(a
->sn
, b
->sn
);
114 else if (b
->ln
== NULL
)
117 return strcmp(a
->ln
, b
->ln
);
119 return a
->nid
- b
->nid
;
126 static int init_added(void)
130 added
= lh_ADDED_OBJ_new(added_obj_hash
, added_obj_cmp
);
131 return added
!= NULL
;
134 static void cleanup1_doall(ADDED_OBJ
*a
)
137 a
->obj
->flags
|= ASN1_OBJECT_FLAG_DYNAMIC
|
138 ASN1_OBJECT_FLAG_DYNAMIC_STRINGS
| ASN1_OBJECT_FLAG_DYNAMIC_DATA
;
141 static void cleanup2_doall(ADDED_OBJ
*a
)
146 static void cleanup3_doall(ADDED_OBJ
*a
)
148 if (--a
->obj
->nid
== 0)
149 ASN1_OBJECT_free(a
->obj
);
153 void obj_cleanup_int(void)
157 lh_ADDED_OBJ_set_down_load(added
, 0);
158 lh_ADDED_OBJ_doall(added
, cleanup1_doall
); /* zero counters */
159 lh_ADDED_OBJ_doall(added
, cleanup2_doall
); /* set counters */
160 lh_ADDED_OBJ_doall(added
, cleanup3_doall
); /* free objects */
161 lh_ADDED_OBJ_free(added
);
165 int OBJ_new_nid(int num
)
174 int OBJ_add_object(const ASN1_OBJECT
*obj
)
177 ADDED_OBJ
*ao
[4] = { NULL
, NULL
, NULL
, NULL
}, *aop
;
183 if ((o
= OBJ_dup(obj
)) == NULL
)
185 if ((ao
[ADDED_NID
] = OPENSSL_malloc(sizeof(*ao
[0]))) == NULL
)
187 if ((o
->length
!= 0) && (obj
->data
!= NULL
))
188 if ((ao
[ADDED_DATA
] = OPENSSL_malloc(sizeof(*ao
[0]))) == NULL
)
191 if ((ao
[ADDED_SNAME
] = OPENSSL_malloc(sizeof(*ao
[0]))) == NULL
)
194 if ((ao
[ADDED_LNAME
] = OPENSSL_malloc(sizeof(*ao
[0]))) == NULL
)
197 for (i
= ADDED_DATA
; i
<= ADDED_NID
; i
++) {
201 aop
= lh_ADDED_OBJ_insert(added
, ao
[i
]);
202 /* memory leak, but should not normally matter */
207 ~(ASN1_OBJECT_FLAG_DYNAMIC
| ASN1_OBJECT_FLAG_DYNAMIC_STRINGS
|
208 ASN1_OBJECT_FLAG_DYNAMIC_DATA
);
212 OBJerr(OBJ_F_OBJ_ADD_OBJECT
, ERR_R_MALLOC_FAILURE
);
214 for (i
= ADDED_DATA
; i
<= ADDED_NID
; i
++)
220 ASN1_OBJECT
*OBJ_nid2obj(int n
)
225 if ((n
>= 0) && (n
< NUM_NID
)) {
226 if ((n
!= NID_undef
) && (nid_objs
[n
].nid
== NID_undef
)) {
227 OBJerr(OBJ_F_OBJ_NID2OBJ
, OBJ_R_UNKNOWN_NID
);
230 return (ASN1_OBJECT
*)&(nid_objs
[n
]);
231 } else if (added
== NULL
)
237 adp
= lh_ADDED_OBJ_retrieve(added
, &ad
);
241 OBJerr(OBJ_F_OBJ_NID2OBJ
, OBJ_R_UNKNOWN_NID
);
247 const char *OBJ_nid2sn(int n
)
252 if ((n
>= 0) && (n
< NUM_NID
)) {
253 if ((n
!= NID_undef
) && (nid_objs
[n
].nid
== NID_undef
)) {
254 OBJerr(OBJ_F_OBJ_NID2SN
, OBJ_R_UNKNOWN_NID
);
257 return nid_objs
[n
].sn
;
258 } else if (added
== NULL
)
264 adp
= lh_ADDED_OBJ_retrieve(added
, &ad
);
268 OBJerr(OBJ_F_OBJ_NID2SN
, OBJ_R_UNKNOWN_NID
);
274 const char *OBJ_nid2ln(int n
)
279 if ((n
>= 0) && (n
< NUM_NID
)) {
280 if ((n
!= NID_undef
) && (nid_objs
[n
].nid
== NID_undef
)) {
281 OBJerr(OBJ_F_OBJ_NID2LN
, OBJ_R_UNKNOWN_NID
);
284 return nid_objs
[n
].ln
;
285 } else if (added
== NULL
)
291 adp
= lh_ADDED_OBJ_retrieve(added
, &ad
);
295 OBJerr(OBJ_F_OBJ_NID2LN
, OBJ_R_UNKNOWN_NID
);
301 static int obj_cmp(const ASN1_OBJECT
*const *ap
, const unsigned int *bp
)
304 const ASN1_OBJECT
*a
= *ap
;
305 const ASN1_OBJECT
*b
= &nid_objs
[*bp
];
307 j
= (a
->length
- b
->length
);
312 return memcmp(a
->data
, b
->data
, a
->length
);
315 IMPLEMENT_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT
*, unsigned int, obj
);
317 int OBJ_obj2nid(const ASN1_OBJECT
*a
)
319 const unsigned int *op
;
331 ad
.type
= ADDED_DATA
;
332 ad
.obj
= (ASN1_OBJECT
*)a
; /* XXX: ugly but harmless */
333 adp
= lh_ADDED_OBJ_retrieve(added
, &ad
);
335 return adp
->obj
->nid
;
337 op
= OBJ_bsearch_obj(&a
, obj_objs
, NUM_OBJ
);
340 return nid_objs
[*op
].nid
;
344 * Convert an object name into an ASN1_OBJECT if "noname" is not set then
345 * search for short and long names first. This will convert the "dotted" form
346 * into an object: unlike OBJ_txt2nid it can be used with any objects, not
347 * just registered ones.
350 ASN1_OBJECT
*OBJ_txt2obj(const char *s
, int no_name
)
356 const unsigned char *cp
;
360 if (((nid
= OBJ_sn2nid(s
)) != NID_undef
) ||
361 ((nid
= OBJ_ln2nid(s
)) != NID_undef
))
362 return OBJ_nid2obj(nid
);
365 /* Work out size of content octets */
366 i
= a2d_ASN1_OBJECT(NULL
, 0, s
, -1);
368 /* Don't clear the error */
374 /* Work out total size */
375 j
= ASN1_object_size(0, i
, V_ASN1_OBJECT
);
379 if ((buf
= OPENSSL_malloc(j
)) == NULL
) {
380 OBJerr(OBJ_F_OBJ_TXT2OBJ
, ERR_R_MALLOC_FAILURE
);
385 /* Write out tag+length */
386 ASN1_put_object(&p
, 0, i
, V_ASN1_OBJECT
, V_ASN1_UNIVERSAL
);
387 /* Write out contents */
388 a2d_ASN1_OBJECT(p
, i
, s
, -1);
391 op
= d2i_ASN1_OBJECT(NULL
, &cp
, j
);
396 int OBJ_obj2txt(char *buf
, int buf_len
, const ASN1_OBJECT
*a
, int no_name
)
398 int i
, n
= 0, len
, nid
, first
, use_bn
;
401 const unsigned char *p
;
402 char tbuf
[DECIMAL_SIZE(i
) + DECIMAL_SIZE(l
) + 2];
404 /* Ensure that, at every state, |buf| is NUL-terminated. */
405 if (buf
&& buf_len
> 0)
408 if ((a
== NULL
) || (a
->data
== NULL
))
411 if (!no_name
&& (nid
= OBJ_obj2nid(a
)) != NID_undef
) {
418 OPENSSL_strlcpy(buf
, s
, buf_len
);
434 unsigned char c
= *p
++;
436 if ((len
== 0) && (c
& 0x80))
439 if (!BN_add_word(bl
, c
& 0x7f))
445 if (!use_bn
&& (l
> (ULONG_MAX
>> 7L))) {
446 if (bl
== NULL
&& (bl
= BN_new()) == NULL
)
448 if (!BN_set_word(bl
, l
))
453 if (!BN_lshift(bl
, bl
, 7))
464 if (!BN_sub_word(bl
, 80))
472 if (buf
&& (buf_len
> 1)) {
482 bndec
= BN_bn2dec(bl
);
492 OPENSSL_strlcpy(buf
, bndec
, buf_len
);
505 BIO_snprintf(tbuf
, sizeof(tbuf
), ".%lu", l
);
507 if (buf
&& (buf_len
> 0)) {
508 OPENSSL_strlcpy(buf
, tbuf
, buf_len
);
530 int OBJ_txt2nid(const char *s
)
534 obj
= OBJ_txt2obj(s
, 0);
535 nid
= OBJ_obj2nid(obj
);
536 ASN1_OBJECT_free(obj
);
540 int OBJ_ln2nid(const char *s
)
543 const ASN1_OBJECT
*oo
= &o
;
545 const unsigned int *op
;
549 ad
.type
= ADDED_LNAME
;
551 adp
= lh_ADDED_OBJ_retrieve(added
, &ad
);
553 return adp
->obj
->nid
;
555 op
= OBJ_bsearch_ln(&oo
, ln_objs
, NUM_LN
);
558 return nid_objs
[*op
].nid
;
561 int OBJ_sn2nid(const char *s
)
564 const ASN1_OBJECT
*oo
= &o
;
566 const unsigned int *op
;
570 ad
.type
= ADDED_SNAME
;
572 adp
= lh_ADDED_OBJ_retrieve(added
, &ad
);
574 return adp
->obj
->nid
;
576 op
= OBJ_bsearch_sn(&oo
, sn_objs
, NUM_SN
);
579 return nid_objs
[*op
].nid
;
582 const void *OBJ_bsearch_(const void *key
, const void *base
, int num
, int size
,
583 int (*cmp
) (const void *, const void *))
585 return OBJ_bsearch_ex_(key
, base
, num
, size
, cmp
, 0);
588 const void *OBJ_bsearch_ex_(const void *key
, const void *base
, int num
,
590 int (*cmp
) (const void *, const void *),
593 const char *p
= ossl_bsearch(key
, base
, num
, size
, cmp
, flags
);
595 #ifdef CHARSET_EBCDIC
597 * THIS IS A KLUDGE - Because the *_obj is sorted in ASCII order, and I
598 * don't have perl (yet), we revert to a *LINEAR* search when the object
599 * wasn't found in the binary search.
602 const char *base_
= base
;
603 int l
, h
, i
= 0, c
= 0;
605 for (i
= 0; i
< num
; ++i
) {
606 p
= &(base_
[i
* size
]);
609 || (c
< 0 && (flags
& OBJ_BSEARCH_VALUE_ON_NOMATCH
)))
618 * Parse a BIO sink to create some extra oid's objects.
619 * Line format:<OID:isdigit or '.']><isspace><SN><isspace><LN>
621 int OBJ_create_objects(BIO
*in
)
625 char *o
, *s
, *l
= NULL
;
629 i
= BIO_gets(in
, buf
, 512);
633 if (!ossl_isalnum(buf
[0]))
636 while (ossl_isdigit(*s
) || *s
== '.')
640 while (ossl_isspace(*s
))
646 while (*l
!= '\0' && !ossl_isspace(*l
))
650 while (ossl_isspace(*l
))
664 if (!OBJ_create(o
, s
, l
))
670 int OBJ_create(const char *oid
, const char *sn
, const char *ln
)
672 ASN1_OBJECT
*tmpoid
= NULL
;
675 /* Check to see if short or long name already present */
676 if ((sn
!= NULL
&& OBJ_sn2nid(sn
) != NID_undef
)
677 || (ln
!= NULL
&& OBJ_ln2nid(ln
) != NID_undef
)) {
678 OBJerr(OBJ_F_OBJ_CREATE
, OBJ_R_OID_EXISTS
);
682 /* Convert numerical OID string to an ASN1_OBJECT structure */
683 tmpoid
= OBJ_txt2obj(oid
, 1);
687 /* If NID is not NID_undef then object already exists */
688 if (OBJ_obj2nid(tmpoid
) != NID_undef
) {
689 OBJerr(OBJ_F_OBJ_CREATE
, OBJ_R_OID_EXISTS
);
693 tmpoid
->nid
= OBJ_new_nid(1);
694 tmpoid
->sn
= (char *)sn
;
695 tmpoid
->ln
= (char *)ln
;
697 ok
= OBJ_add_object(tmpoid
);
703 ASN1_OBJECT_free(tmpoid
);
707 size_t OBJ_length(const ASN1_OBJECT
*obj
)
714 const unsigned char *OBJ_get0_data(const ASN1_OBJECT
*obj
)