obj_dat.c 21 KB

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