obj_dat.c 18 KB

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