obj_dat.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. /*
  2. * Copyright 1995-2023 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 "internal/thread_once.h"
  14. #include "internal/tsan_assist.h"
  15. #include <openssl/lhash.h>
  16. #include <openssl/asn1.h>
  17. #include "crypto/objects.h"
  18. #include <openssl/bn.h>
  19. #include "crypto/asn1.h"
  20. #include "obj_local.h"
  21. /* obj_dat.h is generated from objects.h by obj_dat.pl */
  22. #include "obj_dat.h"
  23. /*
  24. * If we don't have suitable TSAN support, we'll use a lock for generation of
  25. * new NIDs. This will be slower of course.
  26. */
  27. #ifndef tsan_ld_acq
  28. # define OBJ_USE_LOCK_FOR_NEW_NID
  29. #endif
  30. DECLARE_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, sn);
  31. DECLARE_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, ln);
  32. DECLARE_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, obj);
  33. #define ADDED_DATA 0
  34. #define ADDED_SNAME 1
  35. #define ADDED_LNAME 2
  36. #define ADDED_NID 3
  37. struct added_obj_st {
  38. int type;
  39. ASN1_OBJECT *obj;
  40. };
  41. static LHASH_OF(ADDED_OBJ) *added = NULL;
  42. static CRYPTO_RWLOCK *ossl_obj_lock = NULL;
  43. #ifdef OBJ_USE_LOCK_FOR_NEW_NID
  44. static CRYPTO_RWLOCK *ossl_obj_nid_lock = NULL;
  45. #endif
  46. static CRYPTO_ONCE ossl_obj_lock_init = CRYPTO_ONCE_STATIC_INIT;
  47. static ossl_inline void objs_free_locks(void)
  48. {
  49. CRYPTO_THREAD_lock_free(ossl_obj_lock);
  50. ossl_obj_lock = NULL;
  51. #ifdef OBJ_USE_LOCK_FOR_NEW_NID
  52. CRYPTO_THREAD_lock_free(ossl_obj_nid_lock);
  53. ossl_obj_nid_lock = NULL;
  54. #endif
  55. }
  56. DEFINE_RUN_ONCE_STATIC(obj_lock_initialise)
  57. {
  58. ossl_obj_lock = CRYPTO_THREAD_lock_new();
  59. if (ossl_obj_lock == NULL)
  60. return 0;
  61. #ifdef OBJ_USE_LOCK_FOR_NEW_NID
  62. ossl_obj_nid_lock = CRYPTO_THREAD_lock_new();
  63. if (ossl_obj_nid_lock == NULL) {
  64. objs_free_locks();
  65. return 0;
  66. }
  67. #endif
  68. return 1;
  69. }
  70. static ossl_inline int ossl_init_added_lock(void)
  71. {
  72. #ifndef OPENSSL_NO_AUTOLOAD_CONFIG
  73. /* Make sure we've loaded config before checking for any "added" objects */
  74. OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
  75. #endif
  76. return RUN_ONCE(&ossl_obj_lock_init, obj_lock_initialise);
  77. }
  78. static ossl_inline int ossl_obj_write_lock(int lock)
  79. {
  80. if (!lock)
  81. return 1;
  82. if (!ossl_init_added_lock())
  83. return 0;
  84. return CRYPTO_THREAD_write_lock(ossl_obj_lock);
  85. }
  86. static ossl_inline int ossl_obj_read_lock(int lock)
  87. {
  88. if (!lock)
  89. return 1;
  90. if (!ossl_init_added_lock())
  91. return 0;
  92. return CRYPTO_THREAD_read_lock(ossl_obj_lock);
  93. }
  94. static ossl_inline void ossl_obj_unlock(int lock)
  95. {
  96. if (lock)
  97. CRYPTO_THREAD_unlock(ossl_obj_lock);
  98. }
  99. static int sn_cmp(const ASN1_OBJECT *const *a, const unsigned int *b)
  100. {
  101. return strcmp((*a)->sn, nid_objs[*b].sn);
  102. }
  103. IMPLEMENT_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, sn);
  104. static int ln_cmp(const ASN1_OBJECT *const *a, const unsigned int *b)
  105. {
  106. return strcmp((*a)->ln, nid_objs[*b].ln);
  107. }
  108. IMPLEMENT_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, ln);
  109. static unsigned long added_obj_hash(const ADDED_OBJ *ca)
  110. {
  111. const ASN1_OBJECT *a;
  112. int i;
  113. unsigned long ret = 0;
  114. unsigned char *p;
  115. a = ca->obj;
  116. switch (ca->type) {
  117. case ADDED_DATA:
  118. ret = a->length << 20L;
  119. p = (unsigned char *)a->data;
  120. for (i = 0; i < a->length; i++)
  121. ret ^= p[i] << ((i * 3) % 24);
  122. break;
  123. case ADDED_SNAME:
  124. ret = OPENSSL_LH_strhash(a->sn);
  125. break;
  126. case ADDED_LNAME:
  127. ret = OPENSSL_LH_strhash(a->ln);
  128. break;
  129. case ADDED_NID:
  130. ret = a->nid;
  131. break;
  132. default:
  133. /* abort(); */
  134. return 0;
  135. }
  136. ret &= 0x3fffffffL;
  137. ret |= ((unsigned long)ca->type) << 30L;
  138. return ret;
  139. }
  140. static int added_obj_cmp(const ADDED_OBJ *ca, const ADDED_OBJ *cb)
  141. {
  142. ASN1_OBJECT *a, *b;
  143. int i;
  144. i = ca->type - cb->type;
  145. if (i)
  146. return i;
  147. a = ca->obj;
  148. b = cb->obj;
  149. switch (ca->type) {
  150. case ADDED_DATA:
  151. i = (a->length - b->length);
  152. if (i)
  153. return i;
  154. return memcmp(a->data, b->data, (size_t)a->length);
  155. case ADDED_SNAME:
  156. if (a->sn == NULL)
  157. return -1;
  158. else if (b->sn == NULL)
  159. return 1;
  160. else
  161. return strcmp(a->sn, b->sn);
  162. case ADDED_LNAME:
  163. if (a->ln == NULL)
  164. return -1;
  165. else if (b->ln == NULL)
  166. return 1;
  167. else
  168. return strcmp(a->ln, b->ln);
  169. case ADDED_NID:
  170. return a->nid - b->nid;
  171. default:
  172. /* abort(); */
  173. return 0;
  174. }
  175. }
  176. static void cleanup1_doall(ADDED_OBJ *a)
  177. {
  178. a->obj->nid = 0;
  179. a->obj->flags |= ASN1_OBJECT_FLAG_DYNAMIC |
  180. ASN1_OBJECT_FLAG_DYNAMIC_STRINGS | ASN1_OBJECT_FLAG_DYNAMIC_DATA;
  181. }
  182. static void cleanup2_doall(ADDED_OBJ *a)
  183. {
  184. a->obj->nid++;
  185. }
  186. static void cleanup3_doall(ADDED_OBJ *a)
  187. {
  188. if (--a->obj->nid == 0)
  189. ASN1_OBJECT_free(a->obj);
  190. OPENSSL_free(a);
  191. }
  192. void ossl_obj_cleanup_int(void)
  193. {
  194. if (added != NULL) {
  195. lh_ADDED_OBJ_set_down_load(added, 0);
  196. lh_ADDED_OBJ_doall(added, cleanup1_doall); /* zero counters */
  197. lh_ADDED_OBJ_doall(added, cleanup2_doall); /* set counters */
  198. lh_ADDED_OBJ_doall(added, cleanup3_doall); /* free objects */
  199. lh_ADDED_OBJ_free(added);
  200. added = NULL;
  201. }
  202. objs_free_locks();
  203. }
  204. /*
  205. * Requires that the ossl_obj_lock be held
  206. * if TSAN_REQUIRES_LOCKING defined
  207. */
  208. static int obj_new_nid_unlocked(int num)
  209. {
  210. #ifdef OBJ_USE_LOCK_FOR_NEW_NID
  211. static int new_nid = NUM_NID;
  212. int i;
  213. i = new_nid;
  214. new_nid += num;
  215. return i;
  216. #else
  217. static TSAN_QUALIFIER int new_nid = NUM_NID;
  218. return tsan_add(&new_nid, num);
  219. #endif
  220. }
  221. int OBJ_new_nid(int num)
  222. {
  223. #ifdef TSAN_REQUIRES_LOCKING
  224. int i;
  225. if (!ossl_obj_write_lock(1)) {
  226. ERR_raise(ERR_LIB_OBJ, ERR_R_UNABLE_TO_GET_WRITE_LOCK);
  227. return NID_undef;
  228. }
  229. i = obj_new_nid_unlocked(num);
  230. ossl_obj_unlock(1);
  231. return i;
  232. #else
  233. return obj_new_nid_unlocked(num);
  234. #endif
  235. }
  236. static int ossl_obj_add_object(const ASN1_OBJECT *obj, int lock)
  237. {
  238. ASN1_OBJECT *o = NULL;
  239. ADDED_OBJ *ao[4] = { NULL, NULL, NULL, NULL }, *aop;
  240. int i;
  241. if ((o = OBJ_dup(obj)) == NULL)
  242. return NID_undef;
  243. if ((ao[ADDED_NID] = OPENSSL_malloc(sizeof(*ao[0]))) == NULL
  244. || (o->length != 0
  245. && obj->data != NULL
  246. && (ao[ADDED_DATA] = OPENSSL_malloc(sizeof(*ao[0]))) == NULL)
  247. || (o->sn != NULL
  248. && (ao[ADDED_SNAME] = OPENSSL_malloc(sizeof(*ao[0]))) == NULL)
  249. || (o->ln != NULL
  250. && (ao[ADDED_LNAME] = OPENSSL_malloc(sizeof(*ao[0]))) == NULL)) {
  251. ERR_raise(ERR_LIB_OBJ, ERR_R_MALLOC_FAILURE);
  252. goto err2;
  253. }
  254. if (!ossl_obj_write_lock(lock)) {
  255. ERR_raise(ERR_LIB_OBJ, ERR_R_UNABLE_TO_GET_WRITE_LOCK);
  256. goto err2;
  257. }
  258. if (added == NULL) {
  259. added = lh_ADDED_OBJ_new(added_obj_hash, added_obj_cmp);
  260. if (added == NULL) {
  261. ERR_raise(ERR_LIB_OBJ, ERR_R_MALLOC_FAILURE);
  262. goto err;
  263. }
  264. }
  265. for (i = ADDED_DATA; i <= ADDED_NID; i++) {
  266. if (ao[i] != NULL) {
  267. ao[i]->type = i;
  268. ao[i]->obj = o;
  269. aop = lh_ADDED_OBJ_insert(added, ao[i]);
  270. /* memory leak, but should not normally matter */
  271. OPENSSL_free(aop);
  272. }
  273. }
  274. o->flags &=
  275. ~(ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS |
  276. ASN1_OBJECT_FLAG_DYNAMIC_DATA);
  277. ossl_obj_unlock(lock);
  278. return o->nid;
  279. err:
  280. ossl_obj_unlock(lock);
  281. err2:
  282. for (i = ADDED_DATA; i <= ADDED_NID; i++)
  283. OPENSSL_free(ao[i]);
  284. ASN1_OBJECT_free(o);
  285. return NID_undef;
  286. }
  287. ASN1_OBJECT *OBJ_nid2obj(int n)
  288. {
  289. ADDED_OBJ ad, *adp = NULL;
  290. ASN1_OBJECT ob;
  291. if (n == NID_undef
  292. || (n > 0 && n < NUM_NID && nid_objs[n].nid != NID_undef))
  293. return (ASN1_OBJECT *)&(nid_objs[n]);
  294. ad.type = ADDED_NID;
  295. ad.obj = &ob;
  296. ob.nid = n;
  297. if (!ossl_obj_read_lock(1)) {
  298. ERR_raise(ERR_LIB_OBJ, ERR_R_UNABLE_TO_GET_READ_LOCK);
  299. return NULL;
  300. }
  301. if (added != NULL)
  302. adp = lh_ADDED_OBJ_retrieve(added, &ad);
  303. ossl_obj_unlock(1);
  304. if (adp != NULL)
  305. return adp->obj;
  306. ERR_raise(ERR_LIB_OBJ, OBJ_R_UNKNOWN_NID);
  307. return NULL;
  308. }
  309. const char *OBJ_nid2sn(int n)
  310. {
  311. ASN1_OBJECT *ob = OBJ_nid2obj(n);
  312. return ob == NULL ? NULL : ob->sn;
  313. }
  314. const char *OBJ_nid2ln(int n)
  315. {
  316. ASN1_OBJECT *ob = OBJ_nid2obj(n);
  317. return ob == NULL ? NULL : ob->ln;
  318. }
  319. static int obj_cmp(const ASN1_OBJECT *const *ap, const unsigned int *bp)
  320. {
  321. int j;
  322. const ASN1_OBJECT *a = *ap;
  323. const ASN1_OBJECT *b = &nid_objs[*bp];
  324. j = (a->length - b->length);
  325. if (j)
  326. return j;
  327. if (a->length == 0)
  328. return 0;
  329. return memcmp(a->data, b->data, a->length);
  330. }
  331. IMPLEMENT_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, obj);
  332. static int ossl_obj_obj2nid(const ASN1_OBJECT *a, const int lock)
  333. {
  334. int nid = NID_undef;
  335. const unsigned int *op;
  336. ADDED_OBJ ad, *adp;
  337. if (a == NULL)
  338. return NID_undef;
  339. if (a->nid != NID_undef)
  340. return a->nid;
  341. if (a->length == 0)
  342. return NID_undef;
  343. op = OBJ_bsearch_obj(&a, obj_objs, NUM_OBJ);
  344. if (op != NULL)
  345. return nid_objs[*op].nid;
  346. if (!ossl_obj_read_lock(lock)) {
  347. ERR_raise(ERR_LIB_OBJ, ERR_R_UNABLE_TO_GET_READ_LOCK);
  348. return NID_undef;
  349. }
  350. if (added != NULL) {
  351. ad.type = ADDED_DATA;
  352. ad.obj = (ASN1_OBJECT *)a; /* casting away const is harmless here */
  353. adp = lh_ADDED_OBJ_retrieve(added, &ad);
  354. if (adp != NULL)
  355. nid = adp->obj->nid;
  356. }
  357. ossl_obj_unlock(lock);
  358. return nid;
  359. }
  360. /*
  361. * Convert an object name into an ASN1_OBJECT if "noname" is not set then
  362. * search for short and long names first. This will convert the "dotted" form
  363. * into an object: unlike OBJ_txt2nid it can be used with any objects, not
  364. * just registered ones.
  365. */
  366. ASN1_OBJECT *OBJ_txt2obj(const char *s, int no_name)
  367. {
  368. int nid = NID_undef;
  369. ASN1_OBJECT *op = NULL;
  370. unsigned char *buf;
  371. unsigned char *p;
  372. const unsigned char *cp;
  373. int i, j;
  374. if (!no_name) {
  375. if ((nid = OBJ_sn2nid(s)) != NID_undef ||
  376. (nid = OBJ_ln2nid(s)) != NID_undef) {
  377. return OBJ_nid2obj(nid);
  378. }
  379. if (!ossl_isdigit(*s)) {
  380. ERR_raise(ERR_LIB_OBJ, OBJ_R_UNKNOWN_OBJECT_NAME);
  381. return NULL;
  382. }
  383. }
  384. /* Work out size of content octets */
  385. i = a2d_ASN1_OBJECT(NULL, 0, s, -1);
  386. if (i <= 0)
  387. return NULL;
  388. /* Work out total size */
  389. j = ASN1_object_size(0, i, V_ASN1_OBJECT);
  390. if (j < 0)
  391. return NULL;
  392. if ((buf = OPENSSL_malloc(j)) == NULL) {
  393. ERR_raise(ERR_LIB_OBJ, ERR_R_MALLOC_FAILURE);
  394. return NULL;
  395. }
  396. p = buf;
  397. /* Write out tag+length */
  398. ASN1_put_object(&p, 0, i, V_ASN1_OBJECT, V_ASN1_UNIVERSAL);
  399. /* Write out contents */
  400. a2d_ASN1_OBJECT(p, i, s, -1);
  401. cp = buf;
  402. op = d2i_ASN1_OBJECT(NULL, &cp, j);
  403. OPENSSL_free(buf);
  404. return op;
  405. }
  406. int OBJ_obj2txt(char *buf, int buf_len, const ASN1_OBJECT *a, int no_name)
  407. {
  408. int i, n = 0, len, nid, first, use_bn;
  409. BIGNUM *bl;
  410. unsigned long l;
  411. const unsigned char *p;
  412. char tbuf[DECIMAL_SIZE(i) + DECIMAL_SIZE(l) + 2];
  413. const char *s;
  414. /* Ensure that, at every state, |buf| is NUL-terminated. */
  415. if (buf != NULL && buf_len > 0)
  416. buf[0] = '\0';
  417. if (a == NULL || a->data == NULL)
  418. return 0;
  419. if (!no_name && (nid = OBJ_obj2nid(a)) != NID_undef) {
  420. s = OBJ_nid2ln(nid);
  421. if (s == NULL)
  422. s = OBJ_nid2sn(nid);
  423. if (s != NULL) {
  424. if (buf != NULL)
  425. OPENSSL_strlcpy(buf, s, buf_len);
  426. return (int)strlen(s);
  427. }
  428. }
  429. len = a->length;
  430. p = a->data;
  431. first = 1;
  432. bl = NULL;
  433. /*
  434. * RFC 2578 (STD 58) says this about OBJECT IDENTIFIERs:
  435. *
  436. * > 3.5. OBJECT IDENTIFIER values
  437. * >
  438. * > An OBJECT IDENTIFIER value is an ordered list of non-negative
  439. * > numbers. For the SMIv2, each number in the list is referred to as a
  440. * > sub-identifier, there are at most 128 sub-identifiers in a value,
  441. * > and each sub-identifier has a maximum value of 2^32-1 (4294967295
  442. * > decimal).
  443. *
  444. * So a legitimate OID according to this RFC is at most (32 * 128 / 7),
  445. * i.e. 586 bytes long.
  446. *
  447. * Ref: https://datatracker.ietf.org/doc/html/rfc2578#section-3.5
  448. */
  449. if (len > 586)
  450. goto err;
  451. while (len > 0) {
  452. l = 0;
  453. use_bn = 0;
  454. for (;;) {
  455. unsigned char c = *p++;
  456. len--;
  457. if ((len == 0) && (c & 0x80))
  458. goto err;
  459. if (use_bn) {
  460. if (!BN_add_word(bl, c & 0x7f))
  461. goto err;
  462. } else
  463. l |= c & 0x7f;
  464. if (!(c & 0x80))
  465. break;
  466. if (!use_bn && (l > (ULONG_MAX >> 7L))) {
  467. if (bl == NULL && (bl = BN_new()) == NULL)
  468. goto err;
  469. if (!BN_set_word(bl, l))
  470. goto err;
  471. use_bn = 1;
  472. }
  473. if (use_bn) {
  474. if (!BN_lshift(bl, bl, 7))
  475. goto err;
  476. } else
  477. l <<= 7L;
  478. }
  479. if (first) {
  480. first = 0;
  481. if (l >= 80) {
  482. i = 2;
  483. if (use_bn) {
  484. if (!BN_sub_word(bl, 80))
  485. goto err;
  486. } else
  487. l -= 80;
  488. } else {
  489. i = (int)(l / 40);
  490. l -= (long)(i * 40);
  491. }
  492. if (buf && (buf_len > 1)) {
  493. *buf++ = i + '0';
  494. *buf = '\0';
  495. buf_len--;
  496. }
  497. n++;
  498. }
  499. if (use_bn) {
  500. char *bndec;
  501. bndec = BN_bn2dec(bl);
  502. if (!bndec)
  503. goto err;
  504. i = strlen(bndec);
  505. if (buf) {
  506. if (buf_len > 1) {
  507. *buf++ = '.';
  508. *buf = '\0';
  509. buf_len--;
  510. }
  511. OPENSSL_strlcpy(buf, bndec, buf_len);
  512. if (i > buf_len) {
  513. buf += buf_len;
  514. buf_len = 0;
  515. } else {
  516. buf += i;
  517. buf_len -= i;
  518. }
  519. }
  520. n++;
  521. n += i;
  522. OPENSSL_free(bndec);
  523. } else {
  524. BIO_snprintf(tbuf, sizeof(tbuf), ".%lu", l);
  525. i = strlen(tbuf);
  526. if (buf && (buf_len > 0)) {
  527. OPENSSL_strlcpy(buf, tbuf, buf_len);
  528. if (i > buf_len) {
  529. buf += buf_len;
  530. buf_len = 0;
  531. } else {
  532. buf += i;
  533. buf_len -= i;
  534. }
  535. }
  536. n += i;
  537. l = 0;
  538. }
  539. }
  540. BN_free(bl);
  541. return n;
  542. err:
  543. BN_free(bl);
  544. return -1;
  545. }
  546. int OBJ_txt2nid(const char *s)
  547. {
  548. ASN1_OBJECT *obj = OBJ_txt2obj(s, 0);
  549. int nid = NID_undef;
  550. if (obj != NULL) {
  551. nid = OBJ_obj2nid(obj);
  552. ASN1_OBJECT_free(obj);
  553. }
  554. return nid;
  555. }
  556. int OBJ_ln2nid(const char *s)
  557. {
  558. ASN1_OBJECT o;
  559. const ASN1_OBJECT *oo = &o;
  560. ADDED_OBJ ad, *adp;
  561. const unsigned int *op;
  562. int nid = NID_undef;
  563. o.ln = s;
  564. op = OBJ_bsearch_ln(&oo, ln_objs, NUM_LN);
  565. if (op != NULL)
  566. return nid_objs[*op].nid;
  567. if (!ossl_obj_read_lock(1)) {
  568. ERR_raise(ERR_LIB_OBJ, ERR_R_UNABLE_TO_GET_READ_LOCK);
  569. return NID_undef;
  570. }
  571. if (added != NULL) {
  572. ad.type = ADDED_LNAME;
  573. ad.obj = &o;
  574. adp = lh_ADDED_OBJ_retrieve(added, &ad);
  575. if (adp != NULL)
  576. nid = adp->obj->nid;
  577. }
  578. ossl_obj_unlock(1);
  579. return nid;
  580. }
  581. int OBJ_sn2nid(const char *s)
  582. {
  583. ASN1_OBJECT o;
  584. const ASN1_OBJECT *oo = &o;
  585. ADDED_OBJ ad, *adp;
  586. const unsigned int *op;
  587. int nid = NID_undef;
  588. o.sn = s;
  589. op = OBJ_bsearch_sn(&oo, sn_objs, NUM_SN);
  590. if (op != NULL)
  591. return nid_objs[*op].nid;
  592. if (!ossl_obj_read_lock(1)) {
  593. ERR_raise(ERR_LIB_OBJ, ERR_R_UNABLE_TO_GET_READ_LOCK);
  594. return NID_undef;
  595. }
  596. if (added != NULL) {
  597. ad.type = ADDED_SNAME;
  598. ad.obj = &o;
  599. adp = lh_ADDED_OBJ_retrieve(added, &ad);
  600. if (adp != NULL)
  601. nid = adp->obj->nid;
  602. }
  603. ossl_obj_unlock(1);
  604. return nid;
  605. }
  606. const void *OBJ_bsearch_(const void *key, const void *base, int num, int size,
  607. int (*cmp) (const void *, const void *))
  608. {
  609. return OBJ_bsearch_ex_(key, base, num, size, cmp, 0);
  610. }
  611. const void *OBJ_bsearch_ex_(const void *key, const void *base, int num,
  612. int size,
  613. int (*cmp) (const void *, const void *),
  614. int flags)
  615. {
  616. const char *p = ossl_bsearch(key, base, num, size, cmp, flags);
  617. #ifdef CHARSET_EBCDIC
  618. /*
  619. * THIS IS A KLUDGE - Because the *_obj is sorted in ASCII order, and I
  620. * don't have perl (yet), we revert to a *LINEAR* search when the object
  621. * wasn't found in the binary search.
  622. */
  623. if (p == NULL) {
  624. const char *base_ = base;
  625. int l, h, i = 0, c = 0;
  626. char *p1;
  627. for (i = 0; i < num; ++i) {
  628. p1 = &(base_[i * size]);
  629. c = (*cmp) (key, p1);
  630. if (c == 0
  631. || (c < 0 && (flags & OBJ_BSEARCH_VALUE_ON_NOMATCH)))
  632. return p1;
  633. }
  634. }
  635. #endif
  636. return p;
  637. }
  638. /*
  639. * Parse a BIO sink to create some extra oid's objects.
  640. * Line format:<OID:isdigit or '.']><isspace><SN><isspace><LN>
  641. */
  642. int OBJ_create_objects(BIO *in)
  643. {
  644. char buf[512];
  645. int i, num = 0;
  646. char *o, *s, *l = NULL;
  647. for (;;) {
  648. s = o = NULL;
  649. i = BIO_gets(in, buf, 512);
  650. if (i <= 0)
  651. return num;
  652. buf[i - 1] = '\0';
  653. if (!ossl_isalnum(buf[0]))
  654. return num;
  655. o = s = buf;
  656. while (ossl_isdigit(*s) || *s == '.')
  657. s++;
  658. if (*s != '\0') {
  659. *(s++) = '\0';
  660. while (ossl_isspace(*s))
  661. s++;
  662. if (*s == '\0') {
  663. s = NULL;
  664. } else {
  665. l = s;
  666. while (*l != '\0' && !ossl_isspace(*l))
  667. l++;
  668. if (*l != '\0') {
  669. *(l++) = '\0';
  670. while (ossl_isspace(*l))
  671. l++;
  672. if (*l == '\0') {
  673. l = NULL;
  674. }
  675. } else {
  676. l = NULL;
  677. }
  678. }
  679. } else {
  680. s = NULL;
  681. }
  682. if (*o == '\0')
  683. return num;
  684. if (!OBJ_create(o, s, l))
  685. return num;
  686. num++;
  687. }
  688. }
  689. int OBJ_create(const char *oid, const char *sn, const char *ln)
  690. {
  691. ASN1_OBJECT *tmpoid = NULL;
  692. int ok = 0;
  693. /* Check to see if short or long name already present */
  694. if ((sn != NULL && OBJ_sn2nid(sn) != NID_undef)
  695. || (ln != NULL && OBJ_ln2nid(ln) != NID_undef)) {
  696. ERR_raise(ERR_LIB_OBJ, OBJ_R_OID_EXISTS);
  697. return 0;
  698. }
  699. /* Convert numerical OID string to an ASN1_OBJECT structure */
  700. tmpoid = OBJ_txt2obj(oid, 1);
  701. if (tmpoid == NULL)
  702. return 0;
  703. if (!ossl_obj_write_lock(1)) {
  704. ERR_raise(ERR_LIB_OBJ, ERR_R_UNABLE_TO_GET_WRITE_LOCK);
  705. ASN1_OBJECT_free(tmpoid);
  706. return 0;
  707. }
  708. /* If NID is not NID_undef then object already exists */
  709. if (ossl_obj_obj2nid(tmpoid, 0) != NID_undef) {
  710. ERR_raise(ERR_LIB_OBJ, OBJ_R_OID_EXISTS);
  711. goto err;
  712. }
  713. tmpoid->nid = obj_new_nid_unlocked(1);
  714. if (tmpoid->nid == NID_undef)
  715. goto err;
  716. tmpoid->sn = (char *)sn;
  717. tmpoid->ln = (char *)ln;
  718. ok = ossl_obj_add_object(tmpoid, 0);
  719. tmpoid->sn = NULL;
  720. tmpoid->ln = NULL;
  721. err:
  722. ossl_obj_unlock(1);
  723. ASN1_OBJECT_free(tmpoid);
  724. return ok;
  725. }
  726. size_t OBJ_length(const ASN1_OBJECT *obj)
  727. {
  728. if (obj == NULL)
  729. return 0;
  730. return obj->length;
  731. }
  732. const unsigned char *OBJ_get0_data(const ASN1_OBJECT *obj)
  733. {
  734. if (obj == NULL)
  735. return NULL;
  736. return obj->data;
  737. }
  738. int OBJ_add_object(const ASN1_OBJECT *obj)
  739. {
  740. return ossl_obj_add_object(obj, 1);
  741. }
  742. int OBJ_obj2nid(const ASN1_OBJECT *a)
  743. {
  744. return ossl_obj_obj2nid(a, 1);
  745. }