obj.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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. /* obj_cmp is called to search the kNIDsInOIDOrder array. The |key| argument is
  151. * an |ASN1_OBJECT|* that we're looking for and |element| is a pointer to an
  152. * unsigned int in the array. */
  153. static int obj_cmp(const void *key, const void *element) {
  154. unsigned nid = *((const unsigned*) element);
  155. const ASN1_OBJECT *a = key;
  156. const ASN1_OBJECT *b = &kObjects[nid];
  157. if (a->length < b->length) {
  158. return -1;
  159. } else if (a->length > b->length) {
  160. return 1;
  161. }
  162. return OPENSSL_memcmp(a->data, b->data, a->length);
  163. }
  164. int OBJ_obj2nid(const ASN1_OBJECT *obj) {
  165. const unsigned int *nid_ptr;
  166. if (obj == NULL) {
  167. return NID_undef;
  168. }
  169. if (obj->nid != 0) {
  170. return obj->nid;
  171. }
  172. CRYPTO_STATIC_MUTEX_lock_read(&global_added_lock);
  173. if (global_added_by_data != NULL) {
  174. ASN1_OBJECT *match;
  175. match = lh_ASN1_OBJECT_retrieve(global_added_by_data, obj);
  176. if (match != NULL) {
  177. CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock);
  178. return match->nid;
  179. }
  180. }
  181. CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock);
  182. nid_ptr = bsearch(obj, kNIDsInOIDOrder, OPENSSL_ARRAY_SIZE(kNIDsInOIDOrder),
  183. sizeof(kNIDsInOIDOrder[0]), obj_cmp);
  184. if (nid_ptr == NULL) {
  185. return NID_undef;
  186. }
  187. return kObjects[*nid_ptr].nid;
  188. }
  189. int OBJ_cbs2nid(const CBS *cbs) {
  190. if (CBS_len(cbs) > INT_MAX) {
  191. return NID_undef;
  192. }
  193. ASN1_OBJECT obj;
  194. OPENSSL_memset(&obj, 0, sizeof(obj));
  195. obj.data = CBS_data(cbs);
  196. obj.length = (int)CBS_len(cbs);
  197. return OBJ_obj2nid(&obj);
  198. }
  199. /* short_name_cmp is called to search the kNIDsInShortNameOrder array. The
  200. * |key| argument is name that we're looking for and |element| is a pointer to
  201. * an unsigned int in the array. */
  202. static int short_name_cmp(const void *key, const void *element) {
  203. const char *name = (const char *) key;
  204. unsigned nid = *((unsigned*) element);
  205. return strcmp(name, kObjects[nid].sn);
  206. }
  207. int OBJ_sn2nid(const char *short_name) {
  208. const unsigned int *nid_ptr;
  209. CRYPTO_STATIC_MUTEX_lock_read(&global_added_lock);
  210. if (global_added_by_short_name != NULL) {
  211. ASN1_OBJECT *match, template;
  212. template.sn = short_name;
  213. match = lh_ASN1_OBJECT_retrieve(global_added_by_short_name, &template);
  214. if (match != NULL) {
  215. CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock);
  216. return match->nid;
  217. }
  218. }
  219. CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock);
  220. nid_ptr = bsearch(short_name, kNIDsInShortNameOrder,
  221. OPENSSL_ARRAY_SIZE(kNIDsInShortNameOrder),
  222. sizeof(kNIDsInShortNameOrder[0]), short_name_cmp);
  223. if (nid_ptr == NULL) {
  224. return NID_undef;
  225. }
  226. return kObjects[*nid_ptr].nid;
  227. }
  228. /* long_name_cmp is called to search the kNIDsInLongNameOrder array. The
  229. * |key| argument is name that we're looking for and |element| is a pointer to
  230. * an unsigned int in the array. */
  231. static int long_name_cmp(const void *key, const void *element) {
  232. const char *name = (const char *) key;
  233. unsigned nid = *((unsigned*) element);
  234. return strcmp(name, kObjects[nid].ln);
  235. }
  236. int OBJ_ln2nid(const char *long_name) {
  237. const unsigned int *nid_ptr;
  238. CRYPTO_STATIC_MUTEX_lock_read(&global_added_lock);
  239. if (global_added_by_long_name != NULL) {
  240. ASN1_OBJECT *match, template;
  241. template.ln = long_name;
  242. match = lh_ASN1_OBJECT_retrieve(global_added_by_long_name, &template);
  243. if (match != NULL) {
  244. CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock);
  245. return match->nid;
  246. }
  247. }
  248. CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock);
  249. nid_ptr = bsearch(long_name, kNIDsInLongNameOrder,
  250. OPENSSL_ARRAY_SIZE(kNIDsInLongNameOrder),
  251. sizeof(kNIDsInLongNameOrder[0]), long_name_cmp);
  252. if (nid_ptr == NULL) {
  253. return NID_undef;
  254. }
  255. return kObjects[*nid_ptr].nid;
  256. }
  257. int OBJ_txt2nid(const char *s) {
  258. ASN1_OBJECT *obj;
  259. int nid;
  260. obj = OBJ_txt2obj(s, 0 /* search names */);
  261. nid = OBJ_obj2nid(obj);
  262. ASN1_OBJECT_free(obj);
  263. return nid;
  264. }
  265. OPENSSL_EXPORT int OBJ_nid2cbb(CBB *out, int nid) {
  266. const ASN1_OBJECT *obj = OBJ_nid2obj(nid);
  267. CBB oid;
  268. if (obj == NULL ||
  269. !CBB_add_asn1(out, &oid, CBS_ASN1_OBJECT) ||
  270. !CBB_add_bytes(&oid, obj->data, obj->length) ||
  271. !CBB_flush(out)) {
  272. return 0;
  273. }
  274. return 1;
  275. }
  276. const ASN1_OBJECT *OBJ_nid2obj(int nid) {
  277. if (nid >= 0 && nid < NUM_NID) {
  278. if (nid != NID_undef && kObjects[nid].nid == NID_undef) {
  279. goto err;
  280. }
  281. return &kObjects[nid];
  282. }
  283. CRYPTO_STATIC_MUTEX_lock_read(&global_added_lock);
  284. if (global_added_by_nid != NULL) {
  285. ASN1_OBJECT *match, template;
  286. template.nid = nid;
  287. match = lh_ASN1_OBJECT_retrieve(global_added_by_nid, &template);
  288. if (match != NULL) {
  289. CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock);
  290. return match;
  291. }
  292. }
  293. CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock);
  294. err:
  295. OPENSSL_PUT_ERROR(OBJ, OBJ_R_UNKNOWN_NID);
  296. return NULL;
  297. }
  298. const char *OBJ_nid2sn(int nid) {
  299. const ASN1_OBJECT *obj = OBJ_nid2obj(nid);
  300. if (obj == NULL) {
  301. return NULL;
  302. }
  303. return obj->sn;
  304. }
  305. const char *OBJ_nid2ln(int nid) {
  306. const ASN1_OBJECT *obj = OBJ_nid2obj(nid);
  307. if (obj == NULL) {
  308. return NULL;
  309. }
  310. return obj->ln;
  311. }
  312. ASN1_OBJECT *OBJ_txt2obj(const char *s, int dont_search_names) {
  313. int nid = NID_undef;
  314. ASN1_OBJECT *op = NULL;
  315. unsigned char *buf;
  316. unsigned char *p;
  317. const unsigned char *bufp;
  318. int contents_len, total_len;
  319. if (!dont_search_names) {
  320. nid = OBJ_sn2nid(s);
  321. if (nid == NID_undef) {
  322. nid = OBJ_ln2nid(s);
  323. }
  324. if (nid != NID_undef) {
  325. return (ASN1_OBJECT*) OBJ_nid2obj(nid);
  326. }
  327. }
  328. /* Work out size of content octets */
  329. contents_len = a2d_ASN1_OBJECT(NULL, 0, s, -1);
  330. if (contents_len <= 0) {
  331. return NULL;
  332. }
  333. /* Work out total size */
  334. total_len = ASN1_object_size(0, contents_len, V_ASN1_OBJECT);
  335. buf = OPENSSL_malloc(total_len);
  336. if (buf == NULL) {
  337. OPENSSL_PUT_ERROR(OBJ, ERR_R_MALLOC_FAILURE);
  338. return NULL;
  339. }
  340. p = buf;
  341. /* Write out tag+length */
  342. ASN1_put_object(&p, 0, contents_len, V_ASN1_OBJECT, V_ASN1_UNIVERSAL);
  343. /* Write out contents */
  344. a2d_ASN1_OBJECT(p, contents_len, s, -1);
  345. bufp = buf;
  346. op = d2i_ASN1_OBJECT(NULL, &bufp, total_len);
  347. OPENSSL_free(buf);
  348. return op;
  349. }
  350. static int strlcpy_int(char *dst, const char *src, int dst_size) {
  351. size_t ret = BUF_strlcpy(dst, src, dst_size < 0 ? 0 : (size_t)dst_size);
  352. if (ret > INT_MAX) {
  353. OPENSSL_PUT_ERROR(OBJ, ERR_R_OVERFLOW);
  354. return -1;
  355. }
  356. return (int)ret;
  357. }
  358. static int parse_oid_component(CBS *cbs, uint64_t *out) {
  359. uint64_t v = 0;
  360. uint8_t b;
  361. do {
  362. if (!CBS_get_u8(cbs, &b)) {
  363. return 0;
  364. }
  365. if ((v >> (64 - 7)) != 0) {
  366. /* The component is too large. */
  367. return 0;
  368. }
  369. if (v == 0 && b == 0x80) {
  370. /* The component must be minimally encoded. */
  371. return 0;
  372. }
  373. v = (v << 7) | (b & 0x7f);
  374. /* Components end at an octet with the high bit cleared. */
  375. } while (b & 0x80);
  376. *out = v;
  377. return 1;
  378. }
  379. static int add_decimal(CBB *out, uint64_t v) {
  380. char buf[DECIMAL_SIZE(uint64_t) + 1];
  381. BIO_snprintf(buf, sizeof(buf), "%" PRIu64, v);
  382. return CBB_add_bytes(out, (const uint8_t *)buf, strlen(buf));
  383. }
  384. int OBJ_obj2txt(char *out, int out_len, const ASN1_OBJECT *obj,
  385. int always_return_oid) {
  386. /* Python depends on the empty OID successfully encoding as the empty
  387. * string. */
  388. if (obj == NULL || obj->length == 0) {
  389. return strlcpy_int(out, "", out_len);
  390. }
  391. if (!always_return_oid) {
  392. int nid = OBJ_obj2nid(obj);
  393. if (nid != NID_undef) {
  394. const char *name = OBJ_nid2ln(nid);
  395. if (name == NULL) {
  396. name = OBJ_nid2sn(nid);
  397. }
  398. if (name != NULL) {
  399. return strlcpy_int(out, name, out_len);
  400. }
  401. }
  402. }
  403. CBB cbb;
  404. if (!CBB_init(&cbb, 32)) {
  405. goto err;
  406. }
  407. CBS cbs;
  408. CBS_init(&cbs, obj->data, obj->length);
  409. /* The first component is 40 * value1 + value2, where value1 is 0, 1, or 2. */
  410. uint64_t v;
  411. if (!parse_oid_component(&cbs, &v)) {
  412. goto err;
  413. }
  414. if (v >= 80) {
  415. if (!CBB_add_bytes(&cbb, (const uint8_t *)"2.", 2) ||
  416. !add_decimal(&cbb, v - 80)) {
  417. goto err;
  418. }
  419. } else if (!add_decimal(&cbb, v / 40) ||
  420. !CBB_add_u8(&cbb, '.') ||
  421. !add_decimal(&cbb, v % 40)) {
  422. goto err;
  423. }
  424. while (CBS_len(&cbs) != 0) {
  425. if (!parse_oid_component(&cbs, &v) ||
  426. !CBB_add_u8(&cbb, '.') ||
  427. !add_decimal(&cbb, v)) {
  428. goto err;
  429. }
  430. }
  431. uint8_t *txt;
  432. size_t txt_len;
  433. if (!CBB_add_u8(&cbb, '\0') ||
  434. !CBB_finish(&cbb, &txt, &txt_len)) {
  435. goto err;
  436. }
  437. int ret = strlcpy_int(out, (const char *)txt, out_len);
  438. OPENSSL_free(txt);
  439. return ret;
  440. err:
  441. CBB_cleanup(&cbb);
  442. if (out_len > 0) {
  443. out[0] = '\0';
  444. }
  445. return -1;
  446. }
  447. static uint32_t hash_nid(const ASN1_OBJECT *obj) {
  448. return obj->nid;
  449. }
  450. static int cmp_nid(const ASN1_OBJECT *a, const ASN1_OBJECT *b) {
  451. return a->nid - b->nid;
  452. }
  453. static uint32_t hash_data(const ASN1_OBJECT *obj) {
  454. return OPENSSL_hash32(obj->data, obj->length);
  455. }
  456. static int cmp_data(const ASN1_OBJECT *a, const ASN1_OBJECT *b) {
  457. int i = a->length - b->length;
  458. if (i) {
  459. return i;
  460. }
  461. return OPENSSL_memcmp(a->data, b->data, a->length);
  462. }
  463. static uint32_t hash_short_name(const ASN1_OBJECT *obj) {
  464. return lh_strhash(obj->sn);
  465. }
  466. static int cmp_short_name(const ASN1_OBJECT *a, const ASN1_OBJECT *b) {
  467. return strcmp(a->sn, b->sn);
  468. }
  469. static uint32_t hash_long_name(const ASN1_OBJECT *obj) {
  470. return lh_strhash(obj->ln);
  471. }
  472. static int cmp_long_name(const ASN1_OBJECT *a, const ASN1_OBJECT *b) {
  473. return strcmp(a->ln, b->ln);
  474. }
  475. /* obj_add_object inserts |obj| into the various global hashes for run-time
  476. * added objects. It returns one on success or zero otherwise. */
  477. static int obj_add_object(ASN1_OBJECT *obj) {
  478. int ok;
  479. ASN1_OBJECT *old_object;
  480. obj->flags &= ~(ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS |
  481. ASN1_OBJECT_FLAG_DYNAMIC_DATA);
  482. CRYPTO_STATIC_MUTEX_lock_write(&global_added_lock);
  483. if (global_added_by_nid == NULL) {
  484. global_added_by_nid = lh_ASN1_OBJECT_new(hash_nid, cmp_nid);
  485. global_added_by_data = lh_ASN1_OBJECT_new(hash_data, cmp_data);
  486. global_added_by_short_name = lh_ASN1_OBJECT_new(hash_short_name, cmp_short_name);
  487. global_added_by_long_name = lh_ASN1_OBJECT_new(hash_long_name, cmp_long_name);
  488. }
  489. /* We don't pay attention to |old_object| (which contains any previous object
  490. * that was evicted from the hashes) because we don't have a reference count
  491. * on ASN1_OBJECT values. Also, we should never have duplicates nids and so
  492. * should always have objects in |global_added_by_nid|. */
  493. ok = lh_ASN1_OBJECT_insert(global_added_by_nid, &old_object, obj);
  494. if (obj->length != 0 && obj->data != NULL) {
  495. ok &= lh_ASN1_OBJECT_insert(global_added_by_data, &old_object, obj);
  496. }
  497. if (obj->sn != NULL) {
  498. ok &= lh_ASN1_OBJECT_insert(global_added_by_short_name, &old_object, obj);
  499. }
  500. if (obj->ln != NULL) {
  501. ok &= lh_ASN1_OBJECT_insert(global_added_by_long_name, &old_object, obj);
  502. }
  503. CRYPTO_STATIC_MUTEX_unlock_write(&global_added_lock);
  504. return ok;
  505. }
  506. int OBJ_create(const char *oid, const char *short_name, const char *long_name) {
  507. int ret = NID_undef;
  508. ASN1_OBJECT *op = NULL;
  509. unsigned char *buf = NULL;
  510. int len;
  511. len = a2d_ASN1_OBJECT(NULL, 0, oid, -1);
  512. if (len <= 0) {
  513. goto err;
  514. }
  515. buf = OPENSSL_malloc(len);
  516. if (buf == NULL) {
  517. OPENSSL_PUT_ERROR(OBJ, ERR_R_MALLOC_FAILURE);
  518. goto err;
  519. }
  520. len = a2d_ASN1_OBJECT(buf, len, oid, -1);
  521. if (len == 0) {
  522. goto err;
  523. }
  524. op = (ASN1_OBJECT *)ASN1_OBJECT_create(obj_next_nid(), buf, len, short_name,
  525. long_name);
  526. if (op == NULL) {
  527. goto err;
  528. }
  529. if (obj_add_object(op)) {
  530. ret = op->nid;
  531. }
  532. op = NULL;
  533. err:
  534. ASN1_OBJECT_free(op);
  535. OPENSSL_free(buf);
  536. return ret;
  537. }