obj_dat.c 20 KB

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