obj_dat.c 21 KB

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