2
0

obj_dat.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  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 "crypto/ctype.h"
  11. #include <limits.h>
  12. #include "internal/cryptlib.h"
  13. #include <openssl/lhash.h>
  14. #include <openssl/asn1.h>
  15. #include "crypto/objects.h"
  16. #include <openssl/bn.h>
  17. #include "crypto/asn1.h"
  18. #include "obj_local.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. }
  205. /* Make sure we've loaded config before checking for any "added" objects */
  206. OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
  207. if (added == NULL)
  208. return NULL;
  209. ad.type = ADDED_NID;
  210. ad.obj = &ob;
  211. ob.nid = n;
  212. adp = lh_ADDED_OBJ_retrieve(added, &ad);
  213. if (adp != NULL)
  214. return adp->obj;
  215. OBJerr(OBJ_F_OBJ_NID2OBJ, OBJ_R_UNKNOWN_NID);
  216. return NULL;
  217. }
  218. const char *OBJ_nid2sn(int n)
  219. {
  220. ADDED_OBJ ad, *adp;
  221. ASN1_OBJECT ob;
  222. if ((n >= 0) && (n < NUM_NID)) {
  223. if ((n != NID_undef) && (nid_objs[n].nid == NID_undef)) {
  224. OBJerr(OBJ_F_OBJ_NID2SN, OBJ_R_UNKNOWN_NID);
  225. return NULL;
  226. }
  227. return nid_objs[n].sn;
  228. }
  229. /* Make sure we've loaded config before checking for any "added" objects */
  230. OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
  231. if (added == NULL)
  232. return NULL;
  233. ad.type = ADDED_NID;
  234. ad.obj = &ob;
  235. ob.nid = n;
  236. adp = lh_ADDED_OBJ_retrieve(added, &ad);
  237. if (adp != NULL)
  238. return adp->obj->sn;
  239. OBJerr(OBJ_F_OBJ_NID2SN, OBJ_R_UNKNOWN_NID);
  240. return NULL;
  241. }
  242. const char *OBJ_nid2ln(int n)
  243. {
  244. ADDED_OBJ ad, *adp;
  245. ASN1_OBJECT ob;
  246. if ((n >= 0) && (n < NUM_NID)) {
  247. if ((n != NID_undef) && (nid_objs[n].nid == NID_undef)) {
  248. OBJerr(OBJ_F_OBJ_NID2LN, OBJ_R_UNKNOWN_NID);
  249. return NULL;
  250. }
  251. return nid_objs[n].ln;
  252. }
  253. /* Make sure we've loaded config before checking for any "added" objects */
  254. OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
  255. if (added == NULL)
  256. return NULL;
  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. OBJerr(OBJ_F_OBJ_NID2LN, OBJ_R_UNKNOWN_NID);
  264. return NULL;
  265. }
  266. static int obj_cmp(const ASN1_OBJECT *const *ap, const unsigned int *bp)
  267. {
  268. int j;
  269. const ASN1_OBJECT *a = *ap;
  270. const ASN1_OBJECT *b = &nid_objs[*bp];
  271. j = (a->length - b->length);
  272. if (j)
  273. return j;
  274. if (a->length == 0)
  275. return 0;
  276. return memcmp(a->data, b->data, a->length);
  277. }
  278. IMPLEMENT_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, obj);
  279. int OBJ_obj2nid(const ASN1_OBJECT *a)
  280. {
  281. const unsigned int *op;
  282. ADDED_OBJ ad, *adp;
  283. if (a == NULL)
  284. return NID_undef;
  285. if (a->nid != 0)
  286. return a->nid;
  287. if (a->length == 0)
  288. return NID_undef;
  289. /* Make sure we've loaded config before checking for any "added" objects */
  290. OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
  291. if (added != NULL) {
  292. ad.type = ADDED_DATA;
  293. ad.obj = (ASN1_OBJECT *)a; /* XXX: ugly but harmless */
  294. adp = lh_ADDED_OBJ_retrieve(added, &ad);
  295. if (adp != NULL)
  296. return adp->obj->nid;
  297. }
  298. op = OBJ_bsearch_obj(&a, obj_objs, NUM_OBJ);
  299. if (op == NULL)
  300. return NID_undef;
  301. return nid_objs[*op].nid;
  302. }
  303. /*
  304. * Convert an object name into an ASN1_OBJECT if "noname" is not set then
  305. * search for short and long names first. This will convert the "dotted" form
  306. * into an object: unlike OBJ_txt2nid it can be used with any objects, not
  307. * just registered ones.
  308. */
  309. ASN1_OBJECT *OBJ_txt2obj(const char *s, int no_name)
  310. {
  311. int nid = NID_undef;
  312. ASN1_OBJECT *op;
  313. unsigned char *buf;
  314. unsigned char *p;
  315. const unsigned char *cp;
  316. int i, j;
  317. if (!no_name) {
  318. if (((nid = OBJ_sn2nid(s)) != NID_undef) ||
  319. ((nid = OBJ_ln2nid(s)) != NID_undef))
  320. return OBJ_nid2obj(nid);
  321. }
  322. /* Work out size of content octets */
  323. i = a2d_ASN1_OBJECT(NULL, 0, s, -1);
  324. if (i <= 0) {
  325. /* Don't clear the error */
  326. /*
  327. * ERR_clear_error();
  328. */
  329. return NULL;
  330. }
  331. /* Work out total size */
  332. j = ASN1_object_size(0, i, V_ASN1_OBJECT);
  333. if (j < 0)
  334. return NULL;
  335. if ((buf = OPENSSL_malloc(j)) == NULL) {
  336. OBJerr(OBJ_F_OBJ_TXT2OBJ, ERR_R_MALLOC_FAILURE);
  337. return NULL;
  338. }
  339. p = buf;
  340. /* Write out tag+length */
  341. ASN1_put_object(&p, 0, i, V_ASN1_OBJECT, V_ASN1_UNIVERSAL);
  342. /* Write out contents */
  343. a2d_ASN1_OBJECT(p, i, s, -1);
  344. cp = buf;
  345. op = d2i_ASN1_OBJECT(NULL, &cp, j);
  346. OPENSSL_free(buf);
  347. return op;
  348. }
  349. int OBJ_obj2txt(char *buf, int buf_len, const ASN1_OBJECT *a, int no_name)
  350. {
  351. int i, n = 0, len, nid, first, use_bn;
  352. BIGNUM *bl;
  353. unsigned long l;
  354. const unsigned char *p;
  355. char tbuf[DECIMAL_SIZE(i) + DECIMAL_SIZE(l) + 2];
  356. /* Ensure that, at every state, |buf| is NUL-terminated. */
  357. if (buf && buf_len > 0)
  358. buf[0] = '\0';
  359. if ((a == NULL) || (a->data == NULL))
  360. return 0;
  361. if (!no_name && (nid = OBJ_obj2nid(a)) != NID_undef) {
  362. const char *s;
  363. s = OBJ_nid2ln(nid);
  364. if (s == NULL)
  365. s = OBJ_nid2sn(nid);
  366. if (s) {
  367. if (buf)
  368. OPENSSL_strlcpy(buf, s, buf_len);
  369. n = strlen(s);
  370. return n;
  371. }
  372. }
  373. len = a->length;
  374. p = a->data;
  375. first = 1;
  376. bl = NULL;
  377. while (len > 0) {
  378. l = 0;
  379. use_bn = 0;
  380. for (;;) {
  381. unsigned char c = *p++;
  382. len--;
  383. if ((len == 0) && (c & 0x80))
  384. goto err;
  385. if (use_bn) {
  386. if (!BN_add_word(bl, c & 0x7f))
  387. goto err;
  388. } else
  389. l |= c & 0x7f;
  390. if (!(c & 0x80))
  391. break;
  392. if (!use_bn && (l > (ULONG_MAX >> 7L))) {
  393. if (bl == NULL && (bl = BN_new()) == NULL)
  394. goto err;
  395. if (!BN_set_word(bl, l))
  396. goto err;
  397. use_bn = 1;
  398. }
  399. if (use_bn) {
  400. if (!BN_lshift(bl, bl, 7))
  401. goto err;
  402. } else
  403. l <<= 7L;
  404. }
  405. if (first) {
  406. first = 0;
  407. if (l >= 80) {
  408. i = 2;
  409. if (use_bn) {
  410. if (!BN_sub_word(bl, 80))
  411. goto err;
  412. } else
  413. l -= 80;
  414. } else {
  415. i = (int)(l / 40);
  416. l -= (long)(i * 40);
  417. }
  418. if (buf && (buf_len > 1)) {
  419. *buf++ = i + '0';
  420. *buf = '\0';
  421. buf_len--;
  422. }
  423. n++;
  424. }
  425. if (use_bn) {
  426. char *bndec;
  427. bndec = BN_bn2dec(bl);
  428. if (!bndec)
  429. goto err;
  430. i = strlen(bndec);
  431. if (buf) {
  432. if (buf_len > 1) {
  433. *buf++ = '.';
  434. *buf = '\0';
  435. buf_len--;
  436. }
  437. OPENSSL_strlcpy(buf, bndec, buf_len);
  438. if (i > buf_len) {
  439. buf += buf_len;
  440. buf_len = 0;
  441. } else {
  442. buf += i;
  443. buf_len -= i;
  444. }
  445. }
  446. n++;
  447. n += i;
  448. OPENSSL_free(bndec);
  449. } else {
  450. BIO_snprintf(tbuf, sizeof(tbuf), ".%lu", l);
  451. i = strlen(tbuf);
  452. if (buf && (buf_len > 0)) {
  453. OPENSSL_strlcpy(buf, tbuf, buf_len);
  454. if (i > buf_len) {
  455. buf += buf_len;
  456. buf_len = 0;
  457. } else {
  458. buf += i;
  459. buf_len -= i;
  460. }
  461. }
  462. n += i;
  463. l = 0;
  464. }
  465. }
  466. BN_free(bl);
  467. return n;
  468. err:
  469. BN_free(bl);
  470. return -1;
  471. }
  472. int OBJ_txt2nid(const char *s)
  473. {
  474. ASN1_OBJECT *obj;
  475. int nid;
  476. obj = OBJ_txt2obj(s, 0);
  477. nid = OBJ_obj2nid(obj);
  478. ASN1_OBJECT_free(obj);
  479. return nid;
  480. }
  481. int OBJ_ln2nid(const char *s)
  482. {
  483. ASN1_OBJECT o;
  484. const ASN1_OBJECT *oo = &o;
  485. ADDED_OBJ ad, *adp;
  486. const unsigned int *op;
  487. /* Make sure we've loaded config before checking for any "added" objects */
  488. OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
  489. o.ln = s;
  490. if (added != NULL) {
  491. ad.type = ADDED_LNAME;
  492. ad.obj = &o;
  493. adp = lh_ADDED_OBJ_retrieve(added, &ad);
  494. if (adp != NULL)
  495. return adp->obj->nid;
  496. }
  497. op = OBJ_bsearch_ln(&oo, ln_objs, NUM_LN);
  498. if (op == NULL)
  499. return NID_undef;
  500. return nid_objs[*op].nid;
  501. }
  502. int OBJ_sn2nid(const char *s)
  503. {
  504. ASN1_OBJECT o;
  505. const ASN1_OBJECT *oo = &o;
  506. ADDED_OBJ ad, *adp;
  507. const unsigned int *op;
  508. /* Make sure we've loaded config before checking for any "added" objects */
  509. OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
  510. o.sn = s;
  511. if (added != NULL) {
  512. ad.type = ADDED_SNAME;
  513. ad.obj = &o;
  514. adp = lh_ADDED_OBJ_retrieve(added, &ad);
  515. if (adp != NULL)
  516. return adp->obj->nid;
  517. }
  518. op = OBJ_bsearch_sn(&oo, sn_objs, NUM_SN);
  519. if (op == NULL)
  520. return NID_undef;
  521. return nid_objs[*op].nid;
  522. }
  523. const void *OBJ_bsearch_(const void *key, const void *base, int num, int size,
  524. int (*cmp) (const void *, const void *))
  525. {
  526. return OBJ_bsearch_ex_(key, base, num, size, cmp, 0);
  527. }
  528. const void *OBJ_bsearch_ex_(const void *key, const void *base, int num,
  529. int size,
  530. int (*cmp) (const void *, const void *),
  531. int flags)
  532. {
  533. const char *p = ossl_bsearch(key, base, num, size, cmp, flags);
  534. #ifdef CHARSET_EBCDIC
  535. /*
  536. * THIS IS A KLUDGE - Because the *_obj is sorted in ASCII order, and I
  537. * don't have perl (yet), we revert to a *LINEAR* search when the object
  538. * wasn't found in the binary search.
  539. */
  540. if (p == NULL) {
  541. const char *base_ = base;
  542. int l, h, i = 0, c = 0;
  543. for (i = 0; i < num; ++i) {
  544. p = &(base_[i * size]);
  545. c = (*cmp) (key, p);
  546. if (c == 0
  547. || (c < 0 && (flags & OBJ_BSEARCH_VALUE_ON_NOMATCH)))
  548. return p;
  549. }
  550. }
  551. #endif
  552. return p;
  553. }
  554. /*
  555. * Parse a BIO sink to create some extra oid's objects.
  556. * Line format:<OID:isdigit or '.']><isspace><SN><isspace><LN>
  557. */
  558. int OBJ_create_objects(BIO *in)
  559. {
  560. char buf[512];
  561. int i, num = 0;
  562. char *o, *s, *l = NULL;
  563. for (;;) {
  564. s = o = NULL;
  565. i = BIO_gets(in, buf, 512);
  566. if (i <= 0)
  567. return num;
  568. buf[i - 1] = '\0';
  569. if (!ossl_isalnum(buf[0]))
  570. return num;
  571. o = s = buf;
  572. while (ossl_isdigit(*s) || *s == '.')
  573. s++;
  574. if (*s != '\0') {
  575. *(s++) = '\0';
  576. while (ossl_isspace(*s))
  577. s++;
  578. if (*s == '\0') {
  579. s = NULL;
  580. } else {
  581. l = s;
  582. while (*l != '\0' && !ossl_isspace(*l))
  583. l++;
  584. if (*l != '\0') {
  585. *(l++) = '\0';
  586. while (ossl_isspace(*l))
  587. l++;
  588. if (*l == '\0') {
  589. l = NULL;
  590. }
  591. } else {
  592. l = NULL;
  593. }
  594. }
  595. } else {
  596. s = NULL;
  597. }
  598. if (*o == '\0')
  599. return num;
  600. if (!OBJ_create(o, s, l))
  601. return num;
  602. num++;
  603. }
  604. }
  605. int OBJ_create(const char *oid, const char *sn, const char *ln)
  606. {
  607. ASN1_OBJECT *tmpoid = NULL;
  608. int ok = 0;
  609. /* Check to see if short or long name already present */
  610. if ((sn != NULL && OBJ_sn2nid(sn) != NID_undef)
  611. || (ln != NULL && OBJ_ln2nid(ln) != NID_undef)) {
  612. OBJerr(OBJ_F_OBJ_CREATE, OBJ_R_OID_EXISTS);
  613. return 0;
  614. }
  615. /* Convert numerical OID string to an ASN1_OBJECT structure */
  616. tmpoid = OBJ_txt2obj(oid, 1);
  617. if (tmpoid == NULL)
  618. return 0;
  619. /* If NID is not NID_undef then object already exists */
  620. if (OBJ_obj2nid(tmpoid) != NID_undef) {
  621. OBJerr(OBJ_F_OBJ_CREATE, OBJ_R_OID_EXISTS);
  622. goto err;
  623. }
  624. tmpoid->nid = OBJ_new_nid(1);
  625. tmpoid->sn = (char *)sn;
  626. tmpoid->ln = (char *)ln;
  627. ok = OBJ_add_object(tmpoid);
  628. tmpoid->sn = NULL;
  629. tmpoid->ln = NULL;
  630. err:
  631. ASN1_OBJECT_free(tmpoid);
  632. return ok;
  633. }
  634. size_t OBJ_length(const ASN1_OBJECT *obj)
  635. {
  636. if (obj == NULL)
  637. return 0;
  638. return obj->length;
  639. }
  640. const unsigned char *OBJ_get0_data(const ASN1_OBJECT *obj)
  641. {
  642. if (obj == NULL)
  643. return NULL;
  644. return obj->data;
  645. }