obj.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.] */
  56. #if !defined(__STDC_FORMAT_MACROS)
  57. #define __STDC_FORMAT_MACROS
  58. #endif
  59. #include <openssl/obj.h>
  60. #include <inttypes.h>
  61. #include <limits.h>
  62. #include <string.h>
  63. #include <openssl/asn1.h>
  64. #include <openssl/buf.h>
  65. #include <openssl/bytestring.h>
  66. #include <openssl/err.h>
  67. #include <openssl/lhash.h>
  68. #include <openssl/mem.h>
  69. #include <openssl/thread.h>
  70. #include "obj_dat.h"
  71. #include "../internal.h"
  72. static struct CRYPTO_STATIC_MUTEX global_added_lock = CRYPTO_STATIC_MUTEX_INIT;
  73. // These globals are protected by |global_added_lock|.
  74. static LHASH_OF(ASN1_OBJECT) *global_added_by_data = NULL;
  75. static LHASH_OF(ASN1_OBJECT) *global_added_by_nid = NULL;
  76. static LHASH_OF(ASN1_OBJECT) *global_added_by_short_name = NULL;
  77. static LHASH_OF(ASN1_OBJECT) *global_added_by_long_name = NULL;
  78. static struct CRYPTO_STATIC_MUTEX global_next_nid_lock =
  79. CRYPTO_STATIC_MUTEX_INIT;
  80. static unsigned global_next_nid = NUM_NID;
  81. static int obj_next_nid(void) {
  82. int ret;
  83. CRYPTO_STATIC_MUTEX_lock_write(&global_next_nid_lock);
  84. ret = global_next_nid++;
  85. CRYPTO_STATIC_MUTEX_unlock_write(&global_next_nid_lock);
  86. return ret;
  87. }
  88. ASN1_OBJECT *OBJ_dup(const ASN1_OBJECT *o) {
  89. ASN1_OBJECT *r;
  90. unsigned char *data = NULL;
  91. char *sn = NULL, *ln = NULL;
  92. if (o == NULL) {
  93. return NULL;
  94. }
  95. if (!(o->flags & ASN1_OBJECT_FLAG_DYNAMIC)) {
  96. // TODO(fork): this is a little dangerous.
  97. return (ASN1_OBJECT *)o;
  98. }
  99. r = ASN1_OBJECT_new();
  100. if (r == NULL) {
  101. OPENSSL_PUT_ERROR(OBJ, ERR_R_ASN1_LIB);
  102. return NULL;
  103. }
  104. r->ln = r->sn = NULL;
  105. data = OPENSSL_malloc(o->length);
  106. if (data == NULL) {
  107. goto err;
  108. }
  109. if (o->data != NULL) {
  110. OPENSSL_memcpy(data, o->data, o->length);
  111. }
  112. // once data is attached to an object, it remains const
  113. r->data = data;
  114. r->length = o->length;
  115. r->nid = o->nid;
  116. if (o->ln != NULL) {
  117. ln = OPENSSL_strdup(o->ln);
  118. if (ln == NULL) {
  119. goto err;
  120. }
  121. }
  122. if (o->sn != NULL) {
  123. sn = OPENSSL_strdup(o->sn);
  124. if (sn == NULL) {
  125. goto err;
  126. }
  127. }
  128. r->sn = sn;
  129. r->ln = ln;
  130. r->flags =
  131. o->flags | (ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS |
  132. ASN1_OBJECT_FLAG_DYNAMIC_DATA);
  133. return r;
  134. err:
  135. OPENSSL_PUT_ERROR(OBJ, ERR_R_MALLOC_FAILURE);
  136. OPENSSL_free(ln);
  137. OPENSSL_free(sn);
  138. OPENSSL_free(data);
  139. OPENSSL_free(r);
  140. return NULL;
  141. }
  142. int OBJ_cmp(const ASN1_OBJECT *a, const ASN1_OBJECT *b) {
  143. int ret;
  144. ret = a->length - b->length;
  145. if (ret) {
  146. return ret;
  147. }
  148. return OPENSSL_memcmp(a->data, b->data, a->length);
  149. }
  150. const uint8_t *OBJ_get0_data(const ASN1_OBJECT *obj) {
  151. if (obj == NULL) {
  152. return NULL;
  153. }
  154. return obj->data;
  155. }
  156. size_t OBJ_length(const ASN1_OBJECT *obj) {
  157. if (obj == NULL || obj->length < 0) {
  158. return 0;
  159. }
  160. return (size_t)obj->length;
  161. }
  162. // obj_cmp is called to search the kNIDsInOIDOrder array. The |key| argument is
  163. // an |ASN1_OBJECT|* that we're looking for and |element| is a pointer to an
  164. // unsigned int in the array.
  165. static int obj_cmp(const void *key, const void *element) {
  166. unsigned nid = *((const unsigned*) element);
  167. const ASN1_OBJECT *a = key;
  168. const ASN1_OBJECT *b = &kObjects[nid];
  169. if (a->length < b->length) {
  170. return -1;
  171. } else if (a->length > b->length) {
  172. return 1;
  173. }
  174. return OPENSSL_memcmp(a->data, b->data, a->length);
  175. }
  176. int OBJ_obj2nid(const ASN1_OBJECT *obj) {
  177. const unsigned int *nid_ptr;
  178. if (obj == NULL) {
  179. return NID_undef;
  180. }
  181. if (obj->nid != 0) {
  182. return obj->nid;
  183. }
  184. CRYPTO_STATIC_MUTEX_lock_read(&global_added_lock);
  185. if (global_added_by_data != NULL) {
  186. ASN1_OBJECT *match;
  187. match = lh_ASN1_OBJECT_retrieve(global_added_by_data, obj);
  188. if (match != NULL) {
  189. CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock);
  190. return match->nid;
  191. }
  192. }
  193. CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock);
  194. nid_ptr = bsearch(obj, kNIDsInOIDOrder, OPENSSL_ARRAY_SIZE(kNIDsInOIDOrder),
  195. sizeof(kNIDsInOIDOrder[0]), obj_cmp);
  196. if (nid_ptr == NULL) {
  197. return NID_undef;
  198. }
  199. return kObjects[*nid_ptr].nid;
  200. }
  201. int OBJ_cbs2nid(const CBS *cbs) {
  202. if (CBS_len(cbs) > INT_MAX) {
  203. return NID_undef;
  204. }
  205. ASN1_OBJECT obj;
  206. OPENSSL_memset(&obj, 0, sizeof(obj));
  207. obj.data = CBS_data(cbs);
  208. obj.length = (int)CBS_len(cbs);
  209. return OBJ_obj2nid(&obj);
  210. }
  211. // short_name_cmp is called to search the kNIDsInShortNameOrder array. The
  212. // |key| argument is name that we're looking for and |element| is a pointer to
  213. // an unsigned int in the array.
  214. static int short_name_cmp(const void *key, const void *element) {
  215. const char *name = (const char *) key;
  216. unsigned nid = *((unsigned*) element);
  217. return strcmp(name, kObjects[nid].sn);
  218. }
  219. int OBJ_sn2nid(const char *short_name) {
  220. const unsigned int *nid_ptr;
  221. CRYPTO_STATIC_MUTEX_lock_read(&global_added_lock);
  222. if (global_added_by_short_name != NULL) {
  223. ASN1_OBJECT *match, template;
  224. template.sn = short_name;
  225. match = lh_ASN1_OBJECT_retrieve(global_added_by_short_name, &template);
  226. if (match != NULL) {
  227. CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock);
  228. return match->nid;
  229. }
  230. }
  231. CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock);
  232. nid_ptr = bsearch(short_name, kNIDsInShortNameOrder,
  233. OPENSSL_ARRAY_SIZE(kNIDsInShortNameOrder),
  234. sizeof(kNIDsInShortNameOrder[0]), short_name_cmp);
  235. if (nid_ptr == NULL) {
  236. return NID_undef;
  237. }
  238. return kObjects[*nid_ptr].nid;
  239. }
  240. // long_name_cmp is called to search the kNIDsInLongNameOrder array. The
  241. // |key| argument is name that we're looking for and |element| is a pointer to
  242. // an unsigned int in the array.
  243. static int long_name_cmp(const void *key, const void *element) {
  244. const char *name = (const char *) key;
  245. unsigned nid = *((unsigned*) element);
  246. return strcmp(name, kObjects[nid].ln);
  247. }
  248. int OBJ_ln2nid(const char *long_name) {
  249. const unsigned int *nid_ptr;
  250. CRYPTO_STATIC_MUTEX_lock_read(&global_added_lock);
  251. if (global_added_by_long_name != NULL) {
  252. ASN1_OBJECT *match, template;
  253. template.ln = long_name;
  254. match = lh_ASN1_OBJECT_retrieve(global_added_by_long_name, &template);
  255. if (match != NULL) {
  256. CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock);
  257. return match->nid;
  258. }
  259. }
  260. CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock);
  261. nid_ptr = bsearch(long_name, kNIDsInLongNameOrder,
  262. OPENSSL_ARRAY_SIZE(kNIDsInLongNameOrder),
  263. sizeof(kNIDsInLongNameOrder[0]), long_name_cmp);
  264. if (nid_ptr == NULL) {
  265. return NID_undef;
  266. }
  267. return kObjects[*nid_ptr].nid;
  268. }
  269. int OBJ_txt2nid(const char *s) {
  270. ASN1_OBJECT *obj;
  271. int nid;
  272. obj = OBJ_txt2obj(s, 0 /* search names */);
  273. nid = OBJ_obj2nid(obj);
  274. ASN1_OBJECT_free(obj);
  275. return nid;
  276. }
  277. OPENSSL_EXPORT int OBJ_nid2cbb(CBB *out, int nid) {
  278. const ASN1_OBJECT *obj = OBJ_nid2obj(nid);
  279. CBB oid;
  280. if (obj == NULL ||
  281. !CBB_add_asn1(out, &oid, CBS_ASN1_OBJECT) ||
  282. !CBB_add_bytes(&oid, obj->data, obj->length) ||
  283. !CBB_flush(out)) {
  284. return 0;
  285. }
  286. return 1;
  287. }
  288. const ASN1_OBJECT *OBJ_nid2obj(int nid) {
  289. if (nid >= 0 && nid < NUM_NID) {
  290. if (nid != NID_undef && kObjects[nid].nid == NID_undef) {
  291. goto err;
  292. }
  293. return &kObjects[nid];
  294. }
  295. CRYPTO_STATIC_MUTEX_lock_read(&global_added_lock);
  296. if (global_added_by_nid != NULL) {
  297. ASN1_OBJECT *match, template;
  298. template.nid = nid;
  299. match = lh_ASN1_OBJECT_retrieve(global_added_by_nid, &template);
  300. if (match != NULL) {
  301. CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock);
  302. return match;
  303. }
  304. }
  305. CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock);
  306. err:
  307. OPENSSL_PUT_ERROR(OBJ, OBJ_R_UNKNOWN_NID);
  308. return NULL;
  309. }
  310. const char *OBJ_nid2sn(int nid) {
  311. const ASN1_OBJECT *obj = OBJ_nid2obj(nid);
  312. if (obj == NULL) {
  313. return NULL;
  314. }
  315. return obj->sn;
  316. }
  317. const char *OBJ_nid2ln(int nid) {
  318. const ASN1_OBJECT *obj = OBJ_nid2obj(nid);
  319. if (obj == NULL) {
  320. return NULL;
  321. }
  322. return obj->ln;
  323. }
  324. static ASN1_OBJECT *create_object_with_text_oid(int (*get_nid)(void),
  325. const char *oid,
  326. const char *short_name,
  327. const char *long_name) {
  328. uint8_t *buf;
  329. size_t len;
  330. CBB cbb;
  331. if (!CBB_init(&cbb, 32) ||
  332. !CBB_add_asn1_oid_from_text(&cbb, oid, strlen(oid)) ||
  333. !CBB_finish(&cbb, &buf, &len)) {
  334. OPENSSL_PUT_ERROR(OBJ, OBJ_R_INVALID_OID_STRING);
  335. CBB_cleanup(&cbb);
  336. return NULL;
  337. }
  338. ASN1_OBJECT *ret = ASN1_OBJECT_create(get_nid ? get_nid() : NID_undef, buf,
  339. len, short_name, long_name);
  340. OPENSSL_free(buf);
  341. return ret;
  342. }
  343. ASN1_OBJECT *OBJ_txt2obj(const char *s, int dont_search_names) {
  344. if (!dont_search_names) {
  345. int nid = OBJ_sn2nid(s);
  346. if (nid == NID_undef) {
  347. nid = OBJ_ln2nid(s);
  348. }
  349. if (nid != NID_undef) {
  350. return (ASN1_OBJECT*) OBJ_nid2obj(nid);
  351. }
  352. }
  353. return create_object_with_text_oid(NULL, s, NULL, NULL);
  354. }
  355. static int strlcpy_int(char *dst, const char *src, int dst_size) {
  356. size_t ret = BUF_strlcpy(dst, src, dst_size < 0 ? 0 : (size_t)dst_size);
  357. if (ret > INT_MAX) {
  358. OPENSSL_PUT_ERROR(OBJ, ERR_R_OVERFLOW);
  359. return -1;
  360. }
  361. return (int)ret;
  362. }
  363. static int parse_oid_component(CBS *cbs, uint64_t *out) {
  364. uint64_t v = 0;
  365. uint8_t b;
  366. do {
  367. if (!CBS_get_u8(cbs, &b)) {
  368. return 0;
  369. }
  370. if ((v >> (64 - 7)) != 0) {
  371. // The component is too large.
  372. return 0;
  373. }
  374. if (v == 0 && b == 0x80) {
  375. // The component must be minimally encoded.
  376. return 0;
  377. }
  378. v = (v << 7) | (b & 0x7f);
  379. // Components end at an octet with the high bit cleared.
  380. } while (b & 0x80);
  381. *out = v;
  382. return 1;
  383. }
  384. static int add_decimal(CBB *out, uint64_t v) {
  385. char buf[DECIMAL_SIZE(uint64_t) + 1];
  386. BIO_snprintf(buf, sizeof(buf), "%" PRIu64, v);
  387. return CBB_add_bytes(out, (const uint8_t *)buf, strlen(buf));
  388. }
  389. int OBJ_obj2txt(char *out, int out_len, const ASN1_OBJECT *obj,
  390. int always_return_oid) {
  391. // Python depends on the empty OID successfully encoding as the empty
  392. // string.
  393. if (obj == NULL || obj->length == 0) {
  394. return strlcpy_int(out, "", out_len);
  395. }
  396. if (!always_return_oid) {
  397. int nid = OBJ_obj2nid(obj);
  398. if (nid != NID_undef) {
  399. const char *name = OBJ_nid2ln(nid);
  400. if (name == NULL) {
  401. name = OBJ_nid2sn(nid);
  402. }
  403. if (name != NULL) {
  404. return strlcpy_int(out, name, out_len);
  405. }
  406. }
  407. }
  408. CBB cbb;
  409. if (!CBB_init(&cbb, 32)) {
  410. goto err;
  411. }
  412. CBS cbs;
  413. CBS_init(&cbs, obj->data, obj->length);
  414. // The first component is 40 * value1 + value2, where value1 is 0, 1, or 2.
  415. uint64_t v;
  416. if (!parse_oid_component(&cbs, &v)) {
  417. goto err;
  418. }
  419. if (v >= 80) {
  420. if (!CBB_add_bytes(&cbb, (const uint8_t *)"2.", 2) ||
  421. !add_decimal(&cbb, v - 80)) {
  422. goto err;
  423. }
  424. } else if (!add_decimal(&cbb, v / 40) ||
  425. !CBB_add_u8(&cbb, '.') ||
  426. !add_decimal(&cbb, v % 40)) {
  427. goto err;
  428. }
  429. while (CBS_len(&cbs) != 0) {
  430. if (!parse_oid_component(&cbs, &v) ||
  431. !CBB_add_u8(&cbb, '.') ||
  432. !add_decimal(&cbb, v)) {
  433. goto err;
  434. }
  435. }
  436. uint8_t *txt;
  437. size_t txt_len;
  438. if (!CBB_add_u8(&cbb, '\0') ||
  439. !CBB_finish(&cbb, &txt, &txt_len)) {
  440. goto err;
  441. }
  442. int ret = strlcpy_int(out, (const char *)txt, out_len);
  443. OPENSSL_free(txt);
  444. return ret;
  445. err:
  446. CBB_cleanup(&cbb);
  447. if (out_len > 0) {
  448. out[0] = '\0';
  449. }
  450. return -1;
  451. }
  452. static uint32_t hash_nid(const ASN1_OBJECT *obj) {
  453. return obj->nid;
  454. }
  455. static int cmp_nid(const ASN1_OBJECT *a, const ASN1_OBJECT *b) {
  456. return a->nid - b->nid;
  457. }
  458. static uint32_t hash_data(const ASN1_OBJECT *obj) {
  459. return OPENSSL_hash32(obj->data, obj->length);
  460. }
  461. static int cmp_data(const ASN1_OBJECT *a, const ASN1_OBJECT *b) {
  462. int i = a->length - b->length;
  463. if (i) {
  464. return i;
  465. }
  466. return OPENSSL_memcmp(a->data, b->data, a->length);
  467. }
  468. static uint32_t hash_short_name(const ASN1_OBJECT *obj) {
  469. return lh_strhash(obj->sn);
  470. }
  471. static int cmp_short_name(const ASN1_OBJECT *a, const ASN1_OBJECT *b) {
  472. return strcmp(a->sn, b->sn);
  473. }
  474. static uint32_t hash_long_name(const ASN1_OBJECT *obj) {
  475. return lh_strhash(obj->ln);
  476. }
  477. static int cmp_long_name(const ASN1_OBJECT *a, const ASN1_OBJECT *b) {
  478. return strcmp(a->ln, b->ln);
  479. }
  480. // obj_add_object inserts |obj| into the various global hashes for run-time
  481. // added objects. It returns one on success or zero otherwise.
  482. static int obj_add_object(ASN1_OBJECT *obj) {
  483. int ok;
  484. ASN1_OBJECT *old_object;
  485. obj->flags &= ~(ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS |
  486. ASN1_OBJECT_FLAG_DYNAMIC_DATA);
  487. CRYPTO_STATIC_MUTEX_lock_write(&global_added_lock);
  488. if (global_added_by_nid == NULL) {
  489. global_added_by_nid = lh_ASN1_OBJECT_new(hash_nid, cmp_nid);
  490. global_added_by_data = lh_ASN1_OBJECT_new(hash_data, cmp_data);
  491. global_added_by_short_name = lh_ASN1_OBJECT_new(hash_short_name, cmp_short_name);
  492. global_added_by_long_name = lh_ASN1_OBJECT_new(hash_long_name, cmp_long_name);
  493. }
  494. // We don't pay attention to |old_object| (which contains any previous object
  495. // that was evicted from the hashes) because we don't have a reference count
  496. // on ASN1_OBJECT values. Also, we should never have duplicates nids and so
  497. // should always have objects in |global_added_by_nid|.
  498. ok = lh_ASN1_OBJECT_insert(global_added_by_nid, &old_object, obj);
  499. if (obj->length != 0 && obj->data != NULL) {
  500. ok &= lh_ASN1_OBJECT_insert(global_added_by_data, &old_object, obj);
  501. }
  502. if (obj->sn != NULL) {
  503. ok &= lh_ASN1_OBJECT_insert(global_added_by_short_name, &old_object, obj);
  504. }
  505. if (obj->ln != NULL) {
  506. ok &= lh_ASN1_OBJECT_insert(global_added_by_long_name, &old_object, obj);
  507. }
  508. CRYPTO_STATIC_MUTEX_unlock_write(&global_added_lock);
  509. return ok;
  510. }
  511. int OBJ_create(const char *oid, const char *short_name, const char *long_name) {
  512. ASN1_OBJECT *op =
  513. create_object_with_text_oid(obj_next_nid, oid, short_name, long_name);
  514. if (op == NULL ||
  515. !obj_add_object(op)) {
  516. return NID_undef;
  517. }
  518. return op->nid;
  519. }