obj_dat.c 20 KB

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