obj_dat.c 20 KB

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