obj_dat.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. /*
  2. * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  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
  8. */
  9. #include <stdio.h>
  10. #include "internal/ctype.h"
  11. #include <limits.h>
  12. #include "internal/cryptlib.h"
  13. #include <openssl/lhash.h>
  14. #include <openssl/asn1.h>
  15. #include "internal/objects.h"
  16. #include <openssl/bn.h>
  17. #include "internal/asn1_int.h"
  18. #include "obj_lcl.h"
  19. /* obj_dat.h is generated from objects.h by obj_dat.pl */
  20. #include "obj_dat.h"
  21. DECLARE_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, sn);
  22. DECLARE_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, ln);
  23. DECLARE_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, obj);
  24. #define ADDED_DATA 0
  25. #define ADDED_SNAME 1
  26. #define ADDED_LNAME 2
  27. #define ADDED_NID 3
  28. struct added_obj_st {
  29. int type;
  30. ASN1_OBJECT *obj;
  31. };
  32. static int new_nid = NUM_NID;
  33. static LHASH_OF(ADDED_OBJ) *added = NULL;
  34. static int sn_cmp(const ASN1_OBJECT *const *a, const unsigned int *b)
  35. {
  36. return strcmp((*a)->sn, nid_objs[*b].sn);
  37. }
  38. IMPLEMENT_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, sn);
  39. static int ln_cmp(const ASN1_OBJECT *const *a, const unsigned int *b)
  40. {
  41. return strcmp((*a)->ln, nid_objs[*b].ln);
  42. }
  43. IMPLEMENT_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, ln);
  44. static unsigned long added_obj_hash(const ADDED_OBJ *ca)
  45. {
  46. const ASN1_OBJECT *a;
  47. int i;
  48. unsigned long ret = 0;
  49. unsigned char *p;
  50. a = ca->obj;
  51. switch (ca->type) {
  52. case ADDED_DATA:
  53. ret = a->length << 20L;
  54. p = (unsigned char *)a->data;
  55. for (i = 0; i < a->length; i++)
  56. ret ^= p[i] << ((i * 3) % 24);
  57. break;
  58. case ADDED_SNAME:
  59. ret = OPENSSL_LH_strhash(a->sn);
  60. break;
  61. case ADDED_LNAME:
  62. ret = OPENSSL_LH_strhash(a->ln);
  63. break;
  64. case ADDED_NID:
  65. ret = a->nid;
  66. break;
  67. default:
  68. /* abort(); */
  69. return 0;
  70. }
  71. ret &= 0x3fffffffL;
  72. ret |= ((unsigned long)ca->type) << 30L;
  73. return ret;
  74. }
  75. static int added_obj_cmp(const ADDED_OBJ *ca, const ADDED_OBJ *cb)
  76. {
  77. ASN1_OBJECT *a, *b;
  78. int i;
  79. i = ca->type - cb->type;
  80. if (i)
  81. return i;
  82. a = ca->obj;
  83. b = cb->obj;
  84. switch (ca->type) {
  85. case ADDED_DATA:
  86. i = (a->length - b->length);
  87. if (i)
  88. return i;
  89. return memcmp(a->data, b->data, (size_t)a->length);
  90. case ADDED_SNAME:
  91. if (a->sn == NULL)
  92. return -1;
  93. else if (b->sn == NULL)
  94. return 1;
  95. else
  96. return strcmp(a->sn, b->sn);
  97. case ADDED_LNAME:
  98. if (a->ln == NULL)
  99. return -1;
  100. else if (b->ln == NULL)
  101. return 1;
  102. else
  103. return strcmp(a->ln, b->ln);
  104. case ADDED_NID:
  105. return a->nid - b->nid;
  106. default:
  107. /* abort(); */
  108. return 0;
  109. }
  110. }
  111. static int init_added(void)
  112. {
  113. if (added != NULL)
  114. return 1;
  115. added = lh_ADDED_OBJ_new(added_obj_hash, added_obj_cmp);
  116. return added != NULL;
  117. }
  118. static void cleanup1_doall(ADDED_OBJ *a)
  119. {
  120. a->obj->nid = 0;
  121. a->obj->flags |= ASN1_OBJECT_FLAG_DYNAMIC |
  122. ASN1_OBJECT_FLAG_DYNAMIC_STRINGS | ASN1_OBJECT_FLAG_DYNAMIC_DATA;
  123. }
  124. static void cleanup2_doall(ADDED_OBJ *a)
  125. {
  126. a->obj->nid++;
  127. }
  128. static void cleanup3_doall(ADDED_OBJ *a)
  129. {
  130. if (--a->obj->nid == 0)
  131. ASN1_OBJECT_free(a->obj);
  132. OPENSSL_free(a);
  133. }
  134. void obj_cleanup_int(void)
  135. {
  136. if (added == NULL)
  137. return;
  138. lh_ADDED_OBJ_set_down_load(added, 0);
  139. lh_ADDED_OBJ_doall(added, cleanup1_doall); /* zero counters */
  140. lh_ADDED_OBJ_doall(added, cleanup2_doall); /* set counters */
  141. lh_ADDED_OBJ_doall(added, cleanup3_doall); /* free objects */
  142. lh_ADDED_OBJ_free(added);
  143. added = NULL;
  144. }
  145. int OBJ_new_nid(int num)
  146. {
  147. int i;
  148. i = new_nid;
  149. new_nid += num;
  150. return i;
  151. }
  152. int OBJ_add_object(const ASN1_OBJECT *obj)
  153. {
  154. ASN1_OBJECT *o;
  155. ADDED_OBJ *ao[4] = { NULL, NULL, NULL, NULL }, *aop;
  156. int i;
  157. if (added == NULL)
  158. if (!init_added())
  159. return 0;
  160. if ((o = OBJ_dup(obj)) == NULL)
  161. goto err;
  162. if ((ao[ADDED_NID] = OPENSSL_malloc(sizeof(*ao[0]))) == NULL)
  163. goto err2;
  164. if ((o->length != 0) && (obj->data != NULL))
  165. if ((ao[ADDED_DATA] = OPENSSL_malloc(sizeof(*ao[0]))) == NULL)
  166. goto err2;
  167. if (o->sn != NULL)
  168. if ((ao[ADDED_SNAME] = OPENSSL_malloc(sizeof(*ao[0]))) == NULL)
  169. goto err2;
  170. if (o->ln != NULL)
  171. if ((ao[ADDED_LNAME] = OPENSSL_malloc(sizeof(*ao[0]))) == NULL)
  172. goto err2;
  173. for (i = ADDED_DATA; i <= ADDED_NID; i++) {
  174. if (ao[i] != NULL) {
  175. ao[i]->type = i;
  176. ao[i]->obj = o;
  177. aop = lh_ADDED_OBJ_insert(added, ao[i]);
  178. /* memory leak, but should not normally matter */
  179. OPENSSL_free(aop);
  180. }
  181. }
  182. o->flags &=
  183. ~(ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS |
  184. ASN1_OBJECT_FLAG_DYNAMIC_DATA);
  185. return o->nid;
  186. err2:
  187. OBJerr(OBJ_F_OBJ_ADD_OBJECT, ERR_R_MALLOC_FAILURE);
  188. err:
  189. for (i = ADDED_DATA; i <= ADDED_NID; i++)
  190. OPENSSL_free(ao[i]);
  191. ASN1_OBJECT_free(o);
  192. return NID_undef;
  193. }
  194. ASN1_OBJECT *OBJ_nid2obj(int n)
  195. {
  196. ADDED_OBJ ad, *adp;
  197. ASN1_OBJECT ob;
  198. if ((n >= 0) && (n < NUM_NID)) {
  199. if ((n != NID_undef) && (nid_objs[n].nid == NID_undef)) {
  200. OBJerr(OBJ_F_OBJ_NID2OBJ, OBJ_R_UNKNOWN_NID);
  201. return NULL;
  202. }
  203. return (ASN1_OBJECT *)&(nid_objs[n]);
  204. } else if (added == NULL)
  205. return NULL;
  206. else {
  207. ad.type = ADDED_NID;
  208. ad.obj = &ob;
  209. ob.nid = n;
  210. adp = lh_ADDED_OBJ_retrieve(added, &ad);
  211. if (adp != NULL)
  212. return adp->obj;
  213. else {
  214. OBJerr(OBJ_F_OBJ_NID2OBJ, OBJ_R_UNKNOWN_NID);
  215. return NULL;
  216. }
  217. }
  218. }
  219. const char *OBJ_nid2sn(int n)
  220. {
  221. ADDED_OBJ ad, *adp;
  222. ASN1_OBJECT ob;
  223. if ((n >= 0) && (n < NUM_NID)) {
  224. if ((n != NID_undef) && (nid_objs[n].nid == NID_undef)) {
  225. OBJerr(OBJ_F_OBJ_NID2SN, OBJ_R_UNKNOWN_NID);
  226. return NULL;
  227. }
  228. return nid_objs[n].sn;
  229. } else if (added == NULL)
  230. return NULL;
  231. else {
  232. ad.type = ADDED_NID;
  233. ad.obj = &ob;
  234. ob.nid = n;
  235. adp = lh_ADDED_OBJ_retrieve(added, &ad);
  236. if (adp != NULL)
  237. return adp->obj->sn;
  238. else {
  239. OBJerr(OBJ_F_OBJ_NID2SN, OBJ_R_UNKNOWN_NID);
  240. return NULL;
  241. }
  242. }
  243. }
  244. const char *OBJ_nid2ln(int n)
  245. {
  246. ADDED_OBJ ad, *adp;
  247. ASN1_OBJECT ob;
  248. if ((n >= 0) && (n < NUM_NID)) {
  249. if ((n != NID_undef) && (nid_objs[n].nid == NID_undef)) {
  250. OBJerr(OBJ_F_OBJ_NID2LN, OBJ_R_UNKNOWN_NID);
  251. return NULL;
  252. }
  253. return nid_objs[n].ln;
  254. } else if (added == NULL)
  255. return NULL;
  256. else {
  257. ad.type = ADDED_NID;
  258. ad.obj = &ob;
  259. ob.nid = n;
  260. adp = lh_ADDED_OBJ_retrieve(added, &ad);
  261. if (adp != NULL)
  262. return adp->obj->ln;
  263. else {
  264. OBJerr(OBJ_F_OBJ_NID2LN, OBJ_R_UNKNOWN_NID);
  265. return NULL;
  266. }
  267. }
  268. }
  269. static int obj_cmp(const ASN1_OBJECT *const *ap, const unsigned int *bp)
  270. {
  271. int j;
  272. const ASN1_OBJECT *a = *ap;
  273. const ASN1_OBJECT *b = &nid_objs[*bp];
  274. j = (a->length - b->length);
  275. if (j)
  276. return j;
  277. if (a->length == 0)
  278. return 0;
  279. return memcmp(a->data, b->data, a->length);
  280. }
  281. IMPLEMENT_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, obj);
  282. int OBJ_obj2nid(const ASN1_OBJECT *a)
  283. {
  284. const unsigned int *op;
  285. ADDED_OBJ ad, *adp;
  286. if (a == NULL)
  287. return NID_undef;
  288. if (a->nid != 0)
  289. return a->nid;
  290. if (a->length == 0)
  291. return NID_undef;
  292. if (added != NULL) {
  293. ad.type = ADDED_DATA;
  294. ad.obj = (ASN1_OBJECT *)a; /* XXX: ugly but harmless */
  295. adp = lh_ADDED_OBJ_retrieve(added, &ad);
  296. if (adp != NULL)
  297. return adp->obj->nid;
  298. }
  299. op = OBJ_bsearch_obj(&a, obj_objs, NUM_OBJ);
  300. if (op == NULL)
  301. return NID_undef;
  302. return nid_objs[*op].nid;
  303. }
  304. /*
  305. * Convert an object name into an ASN1_OBJECT if "noname" is not set then
  306. * search for short and long names first. This will convert the "dotted" form
  307. * into an object: unlike OBJ_txt2nid it can be used with any objects, not
  308. * just registered ones.
  309. */
  310. ASN1_OBJECT *OBJ_txt2obj(const char *s, int no_name)
  311. {
  312. int nid = NID_undef;
  313. ASN1_OBJECT *op;
  314. unsigned char *buf;
  315. unsigned char *p;
  316. const unsigned char *cp;
  317. int i, j;
  318. if (!no_name) {
  319. if (((nid = OBJ_sn2nid(s)) != NID_undef) ||
  320. ((nid = OBJ_ln2nid(s)) != NID_undef))
  321. return OBJ_nid2obj(nid);
  322. }
  323. /* Work out size of content octets */
  324. i = a2d_ASN1_OBJECT(NULL, 0, s, -1);
  325. if (i <= 0) {
  326. /* Don't clear the error */
  327. /*
  328. * ERR_clear_error();
  329. */
  330. return NULL;
  331. }
  332. /* Work out total size */
  333. j = ASN1_object_size(0, i, V_ASN1_OBJECT);
  334. if (j < 0)
  335. return NULL;
  336. if ((buf = OPENSSL_malloc(j)) == NULL) {
  337. OBJerr(OBJ_F_OBJ_TXT2OBJ, ERR_R_MALLOC_FAILURE);
  338. return NULL;
  339. }
  340. p = buf;
  341. /* Write out tag+length */
  342. ASN1_put_object(&p, 0, i, V_ASN1_OBJECT, V_ASN1_UNIVERSAL);
  343. /* Write out contents */
  344. a2d_ASN1_OBJECT(p, i, s, -1);
  345. cp = buf;
  346. op = d2i_ASN1_OBJECT(NULL, &cp, j);
  347. OPENSSL_free(buf);
  348. return op;
  349. }
  350. int OBJ_obj2txt(char *buf, int buf_len, const ASN1_OBJECT *a, int no_name)
  351. {
  352. int i, n = 0, len, nid, first, use_bn;
  353. BIGNUM *bl;
  354. unsigned long l;
  355. const unsigned char *p;
  356. char tbuf[DECIMAL_SIZE(i) + DECIMAL_SIZE(l) + 2];
  357. /* Ensure that, at every state, |buf| is NUL-terminated. */
  358. if (buf && buf_len > 0)
  359. buf[0] = '\0';
  360. if ((a == NULL) || (a->data == NULL))
  361. return 0;
  362. if (!no_name && (nid = OBJ_obj2nid(a)) != NID_undef) {
  363. const char *s;
  364. s = OBJ_nid2ln(nid);
  365. if (s == NULL)
  366. s = OBJ_nid2sn(nid);
  367. if (s) {
  368. if (buf)
  369. OPENSSL_strlcpy(buf, s, buf_len);
  370. n = strlen(s);
  371. return n;
  372. }
  373. }
  374. len = a->length;
  375. p = a->data;
  376. first = 1;
  377. bl = NULL;
  378. while (len > 0) {
  379. l = 0;
  380. use_bn = 0;
  381. for (;;) {
  382. unsigned char c = *p++;
  383. len--;
  384. if ((len == 0) && (c & 0x80))
  385. goto err;
  386. if (use_bn) {
  387. if (!BN_add_word(bl, c & 0x7f))
  388. goto err;
  389. } else
  390. l |= c & 0x7f;
  391. if (!(c & 0x80))
  392. break;
  393. if (!use_bn && (l > (ULONG_MAX >> 7L))) {
  394. if (bl == NULL && (bl = BN_new()) == NULL)
  395. goto err;
  396. if (!BN_set_word(bl, l))
  397. goto err;
  398. use_bn = 1;
  399. }
  400. if (use_bn) {
  401. if (!BN_lshift(bl, bl, 7))
  402. goto err;
  403. } else
  404. l <<= 7L;
  405. }
  406. if (first) {
  407. first = 0;
  408. if (l >= 80) {
  409. i = 2;
  410. if (use_bn) {
  411. if (!BN_sub_word(bl, 80))
  412. goto err;
  413. } else
  414. l -= 80;
  415. } else {
  416. i = (int)(l / 40);
  417. l -= (long)(i * 40);
  418. }
  419. if (buf && (buf_len > 1)) {
  420. *buf++ = i + '0';
  421. *buf = '\0';
  422. buf_len--;
  423. }
  424. n++;
  425. }
  426. if (use_bn) {
  427. char *bndec;
  428. bndec = BN_bn2dec(bl);
  429. if (!bndec)
  430. goto err;
  431. i = strlen(bndec);
  432. if (buf) {
  433. if (buf_len > 1) {
  434. *buf++ = '.';
  435. *buf = '\0';
  436. buf_len--;
  437. }
  438. OPENSSL_strlcpy(buf, bndec, buf_len);
  439. if (i > buf_len) {
  440. buf += buf_len;
  441. buf_len = 0;
  442. } else {
  443. buf += i;
  444. buf_len -= i;
  445. }
  446. }
  447. n++;
  448. n += i;
  449. OPENSSL_free(bndec);
  450. } else {
  451. BIO_snprintf(tbuf, sizeof(tbuf), ".%lu", l);
  452. i = strlen(tbuf);
  453. if (buf && (buf_len > 0)) {
  454. OPENSSL_strlcpy(buf, tbuf, buf_len);
  455. if (i > buf_len) {
  456. buf += buf_len;
  457. buf_len = 0;
  458. } else {
  459. buf += i;
  460. buf_len -= i;
  461. }
  462. }
  463. n += i;
  464. l = 0;
  465. }
  466. }
  467. BN_free(bl);
  468. return n;
  469. err:
  470. BN_free(bl);
  471. return -1;
  472. }
  473. int OBJ_txt2nid(const char *s)
  474. {
  475. ASN1_OBJECT *obj;
  476. int nid;
  477. obj = OBJ_txt2obj(s, 0);
  478. nid = OBJ_obj2nid(obj);
  479. ASN1_OBJECT_free(obj);
  480. return nid;
  481. }
  482. int OBJ_ln2nid(const char *s)
  483. {
  484. ASN1_OBJECT o;
  485. const ASN1_OBJECT *oo = &o;
  486. ADDED_OBJ ad, *adp;
  487. const unsigned int *op;
  488. o.ln = s;
  489. if (added != NULL) {
  490. ad.type = ADDED_LNAME;
  491. ad.obj = &o;
  492. adp = lh_ADDED_OBJ_retrieve(added, &ad);
  493. if (adp != NULL)
  494. return adp->obj->nid;
  495. }
  496. op = OBJ_bsearch_ln(&oo, ln_objs, NUM_LN);
  497. if (op == NULL)
  498. return NID_undef;
  499. return nid_objs[*op].nid;
  500. }
  501. int OBJ_sn2nid(const char *s)
  502. {
  503. ASN1_OBJECT o;
  504. const ASN1_OBJECT *oo = &o;
  505. ADDED_OBJ ad, *adp;
  506. const unsigned int *op;
  507. o.sn = s;
  508. if (added != NULL) {
  509. ad.type = ADDED_SNAME;
  510. ad.obj = &o;
  511. adp = lh_ADDED_OBJ_retrieve(added, &ad);
  512. if (adp != NULL)
  513. return adp->obj->nid;
  514. }
  515. op = OBJ_bsearch_sn(&oo, sn_objs, NUM_SN);
  516. if (op == NULL)
  517. return NID_undef;
  518. return nid_objs[*op].nid;
  519. }
  520. const void *OBJ_bsearch_(const void *key, const void *base, int num, int size,
  521. int (*cmp) (const void *, const void *))
  522. {
  523. return OBJ_bsearch_ex_(key, base, num, size, cmp, 0);
  524. }
  525. const void *OBJ_bsearch_ex_(const void *key, const void *base, int num,
  526. int size,
  527. int (*cmp) (const void *, const void *),
  528. int flags)
  529. {
  530. const char *p = ossl_bsearch(key, base, num, size, cmp, flags);
  531. #ifdef CHARSET_EBCDIC
  532. /*
  533. * THIS IS A KLUDGE - Because the *_obj is sorted in ASCII order, and I
  534. * don't have perl (yet), we revert to a *LINEAR* search when the object
  535. * wasn't found in the binary search.
  536. */
  537. if (p == NULL) {
  538. const char *base_ = base;
  539. int l, h, i = 0, c = 0;
  540. for (i = 0; i < num; ++i) {
  541. p = &(base_[i * size]);
  542. c = (*cmp) (key, p);
  543. if (c == 0
  544. || (c < 0 && (flags & OBJ_BSEARCH_VALUE_ON_NOMATCH)))
  545. return p;
  546. }
  547. }
  548. #endif
  549. return p;
  550. }
  551. /*
  552. * Parse a BIO sink to create some extra oid's objects.
  553. * Line format:<OID:isdigit or '.']><isspace><SN><isspace><LN>
  554. */
  555. int OBJ_create_objects(BIO *in)
  556. {
  557. char buf[512];
  558. int i, num = 0;
  559. char *o, *s, *l = NULL;
  560. for (;;) {
  561. s = o = NULL;
  562. i = BIO_gets(in, buf, 512);
  563. if (i <= 0)
  564. return num;
  565. buf[i - 1] = '\0';
  566. if (!ossl_isalnum(buf[0]))
  567. return num;
  568. o = s = buf;
  569. while (ossl_isdigit(*s) || *s == '.')
  570. s++;
  571. if (*s != '\0') {
  572. *(s++) = '\0';
  573. while (ossl_isspace(*s))
  574. s++;
  575. if (*s == '\0') {
  576. s = NULL;
  577. } else {
  578. l = s;
  579. while (*l != '\0' && !ossl_isspace(*l))
  580. l++;
  581. if (*l != '\0') {
  582. *(l++) = '\0';
  583. while (ossl_isspace(*l))
  584. l++;
  585. if (*l == '\0') {
  586. l = NULL;
  587. }
  588. } else {
  589. l = NULL;
  590. }
  591. }
  592. } else {
  593. s = NULL;
  594. }
  595. if (*o == '\0')
  596. return num;
  597. if (!OBJ_create(o, s, l))
  598. return num;
  599. num++;
  600. }
  601. }
  602. int OBJ_create(const char *oid, const char *sn, const char *ln)
  603. {
  604. ASN1_OBJECT *tmpoid = NULL;
  605. int ok = 0;
  606. /* Check to see if short or long name already present */
  607. if ((sn != NULL && OBJ_sn2nid(sn) != NID_undef)
  608. || (ln != NULL && OBJ_ln2nid(ln) != NID_undef)) {
  609. OBJerr(OBJ_F_OBJ_CREATE, OBJ_R_OID_EXISTS);
  610. return 0;
  611. }
  612. /* Convert numerical OID string to an ASN1_OBJECT structure */
  613. tmpoid = OBJ_txt2obj(oid, 1);
  614. if (tmpoid == NULL)
  615. return 0;
  616. /* If NID is not NID_undef then object already exists */
  617. if (OBJ_obj2nid(tmpoid) != NID_undef) {
  618. OBJerr(OBJ_F_OBJ_CREATE, OBJ_R_OID_EXISTS);
  619. goto err;
  620. }
  621. tmpoid->nid = OBJ_new_nid(1);
  622. tmpoid->sn = (char *)sn;
  623. tmpoid->ln = (char *)ln;
  624. ok = OBJ_add_object(tmpoid);
  625. tmpoid->sn = NULL;
  626. tmpoid->ln = NULL;
  627. err:
  628. ASN1_OBJECT_free(tmpoid);
  629. return ok;
  630. }
  631. size_t OBJ_length(const ASN1_OBJECT *obj)
  632. {
  633. if (obj == NULL)
  634. return 0;
  635. return obj->length;
  636. }
  637. const unsigned char *OBJ_get0_data(const ASN1_OBJECT *obj)
  638. {
  639. if (obj == NULL)
  640. return NULL;
  641. return obj->data;
  642. }