obj_dat.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. /*
  2. * Copyright 1995-2021 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 ossl_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. ERR_raise(ERR_LIB_OBJ, 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. ERR_raise(ERR_LIB_OBJ, 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. ERR_raise(ERR_LIB_OBJ, 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. ERR_raise(ERR_LIB_OBJ, 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. ERR_raise(ERR_LIB_OBJ, 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. ERR_raise(ERR_LIB_OBJ, 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. ERR_raise(ERR_LIB_OBJ, 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. if (!ossl_isdigit(*s)) {
  322. ERR_raise(ERR_LIB_OBJ, OBJ_R_UNKNOWN_OBJECT_NAME);
  323. return NULL;
  324. }
  325. }
  326. /* Work out size of content octets */
  327. i = a2d_ASN1_OBJECT(NULL, 0, s, -1);
  328. if (i <= 0) {
  329. /* Don't clear the error */
  330. /*
  331. * ERR_clear_error();
  332. */
  333. return NULL;
  334. }
  335. /* Work out total size */
  336. j = ASN1_object_size(0, i, V_ASN1_OBJECT);
  337. if (j < 0)
  338. return NULL;
  339. if ((buf = OPENSSL_malloc(j)) == NULL) {
  340. ERR_raise(ERR_LIB_OBJ, ERR_R_MALLOC_FAILURE);
  341. return NULL;
  342. }
  343. p = buf;
  344. /* Write out tag+length */
  345. ASN1_put_object(&p, 0, i, V_ASN1_OBJECT, V_ASN1_UNIVERSAL);
  346. /* Write out contents */
  347. a2d_ASN1_OBJECT(p, i, s, -1);
  348. cp = buf;
  349. op = d2i_ASN1_OBJECT(NULL, &cp, j);
  350. OPENSSL_free(buf);
  351. return op;
  352. }
  353. int OBJ_obj2txt(char *buf, int buf_len, const ASN1_OBJECT *a, int no_name)
  354. {
  355. int i, n = 0, len, nid, first, use_bn;
  356. BIGNUM *bl;
  357. unsigned long l;
  358. const unsigned char *p;
  359. char tbuf[DECIMAL_SIZE(i) + DECIMAL_SIZE(l) + 2];
  360. /* Ensure that, at every state, |buf| is NUL-terminated. */
  361. if (buf && buf_len > 0)
  362. buf[0] = '\0';
  363. if ((a == NULL) || (a->data == NULL))
  364. return 0;
  365. if (!no_name && (nid = OBJ_obj2nid(a)) != NID_undef) {
  366. const char *s;
  367. s = OBJ_nid2ln(nid);
  368. if (s == NULL)
  369. s = OBJ_nid2sn(nid);
  370. if (s) {
  371. if (buf)
  372. OPENSSL_strlcpy(buf, s, buf_len);
  373. n = strlen(s);
  374. return n;
  375. }
  376. }
  377. len = a->length;
  378. p = a->data;
  379. first = 1;
  380. bl = NULL;
  381. while (len > 0) {
  382. l = 0;
  383. use_bn = 0;
  384. for (;;) {
  385. unsigned char c = *p++;
  386. len--;
  387. if ((len == 0) && (c & 0x80))
  388. goto err;
  389. if (use_bn) {
  390. if (!BN_add_word(bl, c & 0x7f))
  391. goto err;
  392. } else
  393. l |= c & 0x7f;
  394. if (!(c & 0x80))
  395. break;
  396. if (!use_bn && (l > (ULONG_MAX >> 7L))) {
  397. if (bl == NULL && (bl = BN_new()) == NULL)
  398. goto err;
  399. if (!BN_set_word(bl, l))
  400. goto err;
  401. use_bn = 1;
  402. }
  403. if (use_bn) {
  404. if (!BN_lshift(bl, bl, 7))
  405. goto err;
  406. } else
  407. l <<= 7L;
  408. }
  409. if (first) {
  410. first = 0;
  411. if (l >= 80) {
  412. i = 2;
  413. if (use_bn) {
  414. if (!BN_sub_word(bl, 80))
  415. goto err;
  416. } else
  417. l -= 80;
  418. } else {
  419. i = (int)(l / 40);
  420. l -= (long)(i * 40);
  421. }
  422. if (buf && (buf_len > 1)) {
  423. *buf++ = i + '0';
  424. *buf = '\0';
  425. buf_len--;
  426. }
  427. n++;
  428. }
  429. if (use_bn) {
  430. char *bndec;
  431. bndec = BN_bn2dec(bl);
  432. if (!bndec)
  433. goto err;
  434. i = strlen(bndec);
  435. if (buf) {
  436. if (buf_len > 1) {
  437. *buf++ = '.';
  438. *buf = '\0';
  439. buf_len--;
  440. }
  441. OPENSSL_strlcpy(buf, bndec, buf_len);
  442. if (i > buf_len) {
  443. buf += buf_len;
  444. buf_len = 0;
  445. } else {
  446. buf += i;
  447. buf_len -= i;
  448. }
  449. }
  450. n++;
  451. n += i;
  452. OPENSSL_free(bndec);
  453. } else {
  454. BIO_snprintf(tbuf, sizeof(tbuf), ".%lu", l);
  455. i = strlen(tbuf);
  456. if (buf && (buf_len > 0)) {
  457. OPENSSL_strlcpy(buf, tbuf, buf_len);
  458. if (i > buf_len) {
  459. buf += buf_len;
  460. buf_len = 0;
  461. } else {
  462. buf += i;
  463. buf_len -= i;
  464. }
  465. }
  466. n += i;
  467. l = 0;
  468. }
  469. }
  470. BN_free(bl);
  471. return n;
  472. err:
  473. BN_free(bl);
  474. return -1;
  475. }
  476. int OBJ_txt2nid(const char *s)
  477. {
  478. ASN1_OBJECT *obj;
  479. int nid;
  480. obj = OBJ_txt2obj(s, 0);
  481. nid = OBJ_obj2nid(obj);
  482. ASN1_OBJECT_free(obj);
  483. return nid;
  484. }
  485. int OBJ_ln2nid(const char *s)
  486. {
  487. ASN1_OBJECT o;
  488. const ASN1_OBJECT *oo = &o;
  489. ADDED_OBJ ad, *adp;
  490. const unsigned int *op;
  491. /* Make sure we've loaded config before checking for any "added" objects */
  492. OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
  493. o.ln = s;
  494. if (added != NULL) {
  495. ad.type = ADDED_LNAME;
  496. ad.obj = &o;
  497. adp = lh_ADDED_OBJ_retrieve(added, &ad);
  498. if (adp != NULL)
  499. return adp->obj->nid;
  500. }
  501. op = OBJ_bsearch_ln(&oo, ln_objs, NUM_LN);
  502. if (op == NULL)
  503. return NID_undef;
  504. return nid_objs[*op].nid;
  505. }
  506. int OBJ_sn2nid(const char *s)
  507. {
  508. ASN1_OBJECT o;
  509. const ASN1_OBJECT *oo = &o;
  510. ADDED_OBJ ad, *adp;
  511. const unsigned int *op;
  512. /* Make sure we've loaded config before checking for any "added" objects */
  513. OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
  514. o.sn = s;
  515. if (added != NULL) {
  516. ad.type = ADDED_SNAME;
  517. ad.obj = &o;
  518. adp = lh_ADDED_OBJ_retrieve(added, &ad);
  519. if (adp != NULL)
  520. return adp->obj->nid;
  521. }
  522. op = OBJ_bsearch_sn(&oo, sn_objs, NUM_SN);
  523. if (op == NULL)
  524. return NID_undef;
  525. return nid_objs[*op].nid;
  526. }
  527. const void *OBJ_bsearch_(const void *key, const void *base, int num, int size,
  528. int (*cmp) (const void *, const void *))
  529. {
  530. return OBJ_bsearch_ex_(key, base, num, size, cmp, 0);
  531. }
  532. const void *OBJ_bsearch_ex_(const void *key, const void *base, int num,
  533. int size,
  534. int (*cmp) (const void *, const void *),
  535. int flags)
  536. {
  537. const char *p = ossl_bsearch(key, base, num, size, cmp, flags);
  538. #ifdef CHARSET_EBCDIC
  539. /*
  540. * THIS IS A KLUDGE - Because the *_obj is sorted in ASCII order, and I
  541. * don't have perl (yet), we revert to a *LINEAR* search when the object
  542. * wasn't found in the binary search.
  543. */
  544. if (p == NULL) {
  545. const char *base_ = base;
  546. int l, h, i = 0, c = 0;
  547. for (i = 0; i < num; ++i) {
  548. p = &(base_[i * size]);
  549. c = (*cmp) (key, p);
  550. if (c == 0
  551. || (c < 0 && (flags & OBJ_BSEARCH_VALUE_ON_NOMATCH)))
  552. return p;
  553. }
  554. }
  555. #endif
  556. return p;
  557. }
  558. /*
  559. * Parse a BIO sink to create some extra oid's objects.
  560. * Line format:<OID:isdigit or '.']><isspace><SN><isspace><LN>
  561. */
  562. int OBJ_create_objects(BIO *in)
  563. {
  564. char buf[512];
  565. int i, num = 0;
  566. char *o, *s, *l = NULL;
  567. for (;;) {
  568. s = o = NULL;
  569. i = BIO_gets(in, buf, 512);
  570. if (i <= 0)
  571. return num;
  572. buf[i - 1] = '\0';
  573. if (!ossl_isalnum(buf[0]))
  574. return num;
  575. o = s = buf;
  576. while (ossl_isdigit(*s) || *s == '.')
  577. s++;
  578. if (*s != '\0') {
  579. *(s++) = '\0';
  580. while (ossl_isspace(*s))
  581. s++;
  582. if (*s == '\0') {
  583. s = NULL;
  584. } else {
  585. l = s;
  586. while (*l != '\0' && !ossl_isspace(*l))
  587. l++;
  588. if (*l != '\0') {
  589. *(l++) = '\0';
  590. while (ossl_isspace(*l))
  591. l++;
  592. if (*l == '\0') {
  593. l = NULL;
  594. }
  595. } else {
  596. l = NULL;
  597. }
  598. }
  599. } else {
  600. s = NULL;
  601. }
  602. if (*o == '\0')
  603. return num;
  604. if (!OBJ_create(o, s, l))
  605. return num;
  606. num++;
  607. }
  608. }
  609. int OBJ_create(const char *oid, const char *sn, const char *ln)
  610. {
  611. ASN1_OBJECT *tmpoid = NULL;
  612. int ok = 0;
  613. /* Check to see if short or long name already present */
  614. if ((sn != NULL && OBJ_sn2nid(sn) != NID_undef)
  615. || (ln != NULL && OBJ_ln2nid(ln) != NID_undef)) {
  616. ERR_raise(ERR_LIB_OBJ, OBJ_R_OID_EXISTS);
  617. return 0;
  618. }
  619. /* Convert numerical OID string to an ASN1_OBJECT structure */
  620. tmpoid = OBJ_txt2obj(oid, 1);
  621. if (tmpoid == NULL)
  622. return 0;
  623. /* If NID is not NID_undef then object already exists */
  624. if (OBJ_obj2nid(tmpoid) != NID_undef) {
  625. ERR_raise(ERR_LIB_OBJ, OBJ_R_OID_EXISTS);
  626. goto err;
  627. }
  628. tmpoid->nid = OBJ_new_nid(1);
  629. tmpoid->sn = (char *)sn;
  630. tmpoid->ln = (char *)ln;
  631. ok = OBJ_add_object(tmpoid);
  632. tmpoid->sn = NULL;
  633. tmpoid->ln = NULL;
  634. err:
  635. ASN1_OBJECT_free(tmpoid);
  636. return ok;
  637. }
  638. size_t OBJ_length(const ASN1_OBJECT *obj)
  639. {
  640. if (obj == NULL)
  641. return 0;
  642. return obj->length;
  643. }
  644. const unsigned char *OBJ_get0_data(const ASN1_OBJECT *obj)
  645. {
  646. if (obj == NULL)
  647. return NULL;
  648. return obj->data;
  649. }