obj_dat.c 21 KB

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