obj_dat.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. /* crypto/objects/obj_dat.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. #include <stdio.h>
  59. #include <ctype.h>
  60. #include <limits.h>
  61. #include "cryptlib.h"
  62. #include <openssl/lhash.h>
  63. #include <openssl/asn1.h>
  64. #include <openssl/objects.h>
  65. #include <openssl/bn.h>
  66. /* obj_dat.h is generated from objects.h by obj_dat.pl */
  67. #ifndef OPENSSL_NO_OBJECT
  68. # include "obj_dat.h"
  69. #else
  70. /* You will have to load all the objects needed manually in the application */
  71. # define NUM_NID 0
  72. # define NUM_SN 0
  73. # define NUM_LN 0
  74. # define NUM_OBJ 0
  75. static const unsigned char lvalues[1];
  76. static const ASN1_OBJECT nid_objs[1];
  77. static const unsigned int sn_objs[1];
  78. static const unsigned int ln_objs[1];
  79. static const unsigned int obj_objs[1];
  80. #endif
  81. DECLARE_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, sn);
  82. DECLARE_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, ln);
  83. DECLARE_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, obj);
  84. #define ADDED_DATA 0
  85. #define ADDED_SNAME 1
  86. #define ADDED_LNAME 2
  87. #define ADDED_NID 3
  88. typedef struct added_obj_st {
  89. int type;
  90. ASN1_OBJECT *obj;
  91. } ADDED_OBJ;
  92. DECLARE_LHASH_OF(ADDED_OBJ);
  93. static int new_nid = NUM_NID;
  94. static LHASH_OF(ADDED_OBJ) *added = NULL;
  95. static int sn_cmp(const ASN1_OBJECT *const *a, const unsigned int *b)
  96. {
  97. return (strcmp((*a)->sn, nid_objs[*b].sn));
  98. }
  99. IMPLEMENT_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, sn);
  100. static int ln_cmp(const ASN1_OBJECT *const *a, const unsigned int *b)
  101. {
  102. return (strcmp((*a)->ln, nid_objs[*b].ln));
  103. }
  104. IMPLEMENT_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, ln);
  105. static unsigned long added_obj_hash(const ADDED_OBJ *ca)
  106. {
  107. const ASN1_OBJECT *a;
  108. int i;
  109. unsigned long ret = 0;
  110. unsigned char *p;
  111. a = ca->obj;
  112. switch (ca->type) {
  113. case ADDED_DATA:
  114. ret = a->length << 20L;
  115. p = (unsigned char *)a->data;
  116. for (i = 0; i < a->length; i++)
  117. ret ^= p[i] << ((i * 3) % 24);
  118. break;
  119. case ADDED_SNAME:
  120. ret = lh_strhash(a->sn);
  121. break;
  122. case ADDED_LNAME:
  123. ret = lh_strhash(a->ln);
  124. break;
  125. case ADDED_NID:
  126. ret = a->nid;
  127. break;
  128. default:
  129. /* abort(); */
  130. return 0;
  131. }
  132. ret &= 0x3fffffffL;
  133. ret |= ((unsigned long)ca->type) << 30L;
  134. return (ret);
  135. }
  136. static IMPLEMENT_LHASH_HASH_FN(added_obj, ADDED_OBJ)
  137. static int added_obj_cmp(const ADDED_OBJ *ca, const ADDED_OBJ *cb)
  138. {
  139. ASN1_OBJECT *a, *b;
  140. int i;
  141. i = ca->type - cb->type;
  142. if (i)
  143. return (i);
  144. a = ca->obj;
  145. b = cb->obj;
  146. switch (ca->type) {
  147. case ADDED_DATA:
  148. i = (a->length - b->length);
  149. if (i)
  150. return (i);
  151. return (memcmp(a->data, b->data, (size_t)a->length));
  152. case ADDED_SNAME:
  153. if (a->sn == NULL)
  154. return (-1);
  155. else if (b->sn == NULL)
  156. return (1);
  157. else
  158. return (strcmp(a->sn, b->sn));
  159. case ADDED_LNAME:
  160. if (a->ln == NULL)
  161. return (-1);
  162. else if (b->ln == NULL)
  163. return (1);
  164. else
  165. return (strcmp(a->ln, b->ln));
  166. case ADDED_NID:
  167. return (a->nid - b->nid);
  168. default:
  169. /* abort(); */
  170. return 0;
  171. }
  172. }
  173. static IMPLEMENT_LHASH_COMP_FN(added_obj, ADDED_OBJ)
  174. static int init_added(void)
  175. {
  176. if (added != NULL)
  177. return (1);
  178. added = lh_ADDED_OBJ_new();
  179. return (added != NULL);
  180. }
  181. static void cleanup1_doall(ADDED_OBJ *a)
  182. {
  183. a->obj->nid = 0;
  184. a->obj->flags |= ASN1_OBJECT_FLAG_DYNAMIC |
  185. ASN1_OBJECT_FLAG_DYNAMIC_STRINGS | ASN1_OBJECT_FLAG_DYNAMIC_DATA;
  186. }
  187. static void cleanup2_doall(ADDED_OBJ *a)
  188. {
  189. a->obj->nid++;
  190. }
  191. static void cleanup3_doall(ADDED_OBJ *a)
  192. {
  193. if (--a->obj->nid == 0)
  194. ASN1_OBJECT_free(a->obj);
  195. OPENSSL_free(a);
  196. }
  197. static IMPLEMENT_LHASH_DOALL_FN(cleanup1, ADDED_OBJ)
  198. static IMPLEMENT_LHASH_DOALL_FN(cleanup2, ADDED_OBJ)
  199. static IMPLEMENT_LHASH_DOALL_FN(cleanup3, ADDED_OBJ)
  200. /*
  201. * The purpose of obj_cleanup_defer is to avoid EVP_cleanup() attempting to
  202. * use freed up OIDs. If neccessary the actual freeing up of OIDs is delayed.
  203. */
  204. int obj_cleanup_defer = 0;
  205. void check_defer(int nid)
  206. {
  207. if (!obj_cleanup_defer && nid >= NUM_NID)
  208. obj_cleanup_defer = 1;
  209. }
  210. void OBJ_cleanup(void)
  211. {
  212. if (obj_cleanup_defer) {
  213. obj_cleanup_defer = 2;
  214. return;
  215. }
  216. if (added == NULL)
  217. return;
  218. lh_ADDED_OBJ_down_load(added) = 0;
  219. lh_ADDED_OBJ_doall(added, LHASH_DOALL_FN(cleanup1)); /* zero counters */
  220. lh_ADDED_OBJ_doall(added, LHASH_DOALL_FN(cleanup2)); /* set counters */
  221. lh_ADDED_OBJ_doall(added, LHASH_DOALL_FN(cleanup3)); /* free objects */
  222. lh_ADDED_OBJ_free(added);
  223. added = NULL;
  224. }
  225. int OBJ_new_nid(int num)
  226. {
  227. int i;
  228. i = new_nid;
  229. new_nid += num;
  230. return (i);
  231. }
  232. int OBJ_add_object(const ASN1_OBJECT *obj)
  233. {
  234. ASN1_OBJECT *o;
  235. ADDED_OBJ *ao[4] = { NULL, NULL, NULL, NULL }, *aop;
  236. int i;
  237. if (added == NULL)
  238. if (!init_added())
  239. return (0);
  240. if ((o = OBJ_dup(obj)) == NULL)
  241. goto err;
  242. if (!(ao[ADDED_NID] = (ADDED_OBJ *)OPENSSL_malloc(sizeof(ADDED_OBJ))))
  243. goto err2;
  244. if ((o->length != 0) && (obj->data != NULL))
  245. if (!
  246. (ao[ADDED_DATA] = (ADDED_OBJ *)OPENSSL_malloc(sizeof(ADDED_OBJ))))
  247. goto err2;
  248. if (o->sn != NULL)
  249. if (!
  250. (ao[ADDED_SNAME] =
  251. (ADDED_OBJ *)OPENSSL_malloc(sizeof(ADDED_OBJ))))
  252. goto err2;
  253. if (o->ln != NULL)
  254. if (!
  255. (ao[ADDED_LNAME] =
  256. (ADDED_OBJ *)OPENSSL_malloc(sizeof(ADDED_OBJ))))
  257. goto err2;
  258. for (i = ADDED_DATA; i <= ADDED_NID; i++) {
  259. if (ao[i] != NULL) {
  260. ao[i]->type = i;
  261. ao[i]->obj = o;
  262. aop = lh_ADDED_OBJ_insert(added, ao[i]);
  263. /* memory leak, buit should not normally matter */
  264. if (aop != NULL)
  265. OPENSSL_free(aop);
  266. }
  267. }
  268. o->flags &=
  269. ~(ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS |
  270. ASN1_OBJECT_FLAG_DYNAMIC_DATA);
  271. return (o->nid);
  272. err2:
  273. OBJerr(OBJ_F_OBJ_ADD_OBJECT, ERR_R_MALLOC_FAILURE);
  274. err:
  275. for (i = ADDED_DATA; i <= ADDED_NID; i++)
  276. if (ao[i] != NULL)
  277. OPENSSL_free(ao[i]);
  278. if (o != NULL)
  279. OPENSSL_free(o);
  280. return (NID_undef);
  281. }
  282. ASN1_OBJECT *OBJ_nid2obj(int n)
  283. {
  284. ADDED_OBJ ad, *adp;
  285. ASN1_OBJECT ob;
  286. if ((n >= 0) && (n < NUM_NID)) {
  287. if ((n != NID_undef) && (nid_objs[n].nid == NID_undef)) {
  288. OBJerr(OBJ_F_OBJ_NID2OBJ, OBJ_R_UNKNOWN_NID);
  289. return (NULL);
  290. }
  291. return ((ASN1_OBJECT *)&(nid_objs[n]));
  292. } else if (added == NULL)
  293. return (NULL);
  294. else {
  295. ad.type = ADDED_NID;
  296. ad.obj = &ob;
  297. ob.nid = n;
  298. adp = lh_ADDED_OBJ_retrieve(added, &ad);
  299. if (adp != NULL)
  300. return (adp->obj);
  301. else {
  302. OBJerr(OBJ_F_OBJ_NID2OBJ, OBJ_R_UNKNOWN_NID);
  303. return (NULL);
  304. }
  305. }
  306. }
  307. const char *OBJ_nid2sn(int n)
  308. {
  309. ADDED_OBJ ad, *adp;
  310. ASN1_OBJECT ob;
  311. if ((n >= 0) && (n < NUM_NID)) {
  312. if ((n != NID_undef) && (nid_objs[n].nid == NID_undef)) {
  313. OBJerr(OBJ_F_OBJ_NID2SN, OBJ_R_UNKNOWN_NID);
  314. return (NULL);
  315. }
  316. return (nid_objs[n].sn);
  317. } else if (added == NULL)
  318. return (NULL);
  319. else {
  320. ad.type = ADDED_NID;
  321. ad.obj = &ob;
  322. ob.nid = n;
  323. adp = lh_ADDED_OBJ_retrieve(added, &ad);
  324. if (adp != NULL)
  325. return (adp->obj->sn);
  326. else {
  327. OBJerr(OBJ_F_OBJ_NID2SN, OBJ_R_UNKNOWN_NID);
  328. return (NULL);
  329. }
  330. }
  331. }
  332. const char *OBJ_nid2ln(int n)
  333. {
  334. ADDED_OBJ ad, *adp;
  335. ASN1_OBJECT ob;
  336. if ((n >= 0) && (n < NUM_NID)) {
  337. if ((n != NID_undef) && (nid_objs[n].nid == NID_undef)) {
  338. OBJerr(OBJ_F_OBJ_NID2LN, OBJ_R_UNKNOWN_NID);
  339. return (NULL);
  340. }
  341. return (nid_objs[n].ln);
  342. } else if (added == NULL)
  343. return (NULL);
  344. else {
  345. ad.type = ADDED_NID;
  346. ad.obj = &ob;
  347. ob.nid = n;
  348. adp = lh_ADDED_OBJ_retrieve(added, &ad);
  349. if (adp != NULL)
  350. return (adp->obj->ln);
  351. else {
  352. OBJerr(OBJ_F_OBJ_NID2LN, OBJ_R_UNKNOWN_NID);
  353. return (NULL);
  354. }
  355. }
  356. }
  357. static int obj_cmp(const ASN1_OBJECT *const *ap, const unsigned int *bp)
  358. {
  359. int j;
  360. const ASN1_OBJECT *a = *ap;
  361. const ASN1_OBJECT *b = &nid_objs[*bp];
  362. j = (a->length - b->length);
  363. if (j)
  364. return (j);
  365. if (a->length == 0)
  366. return 0;
  367. return (memcmp(a->data, b->data, a->length));
  368. }
  369. IMPLEMENT_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, obj);
  370. int OBJ_obj2nid(const ASN1_OBJECT *a)
  371. {
  372. const unsigned int *op;
  373. ADDED_OBJ ad, *adp;
  374. if (a == NULL)
  375. return (NID_undef);
  376. if (a->nid != 0)
  377. return (a->nid);
  378. if (a->length == 0)
  379. return NID_undef;
  380. if (added != NULL) {
  381. ad.type = ADDED_DATA;
  382. ad.obj = (ASN1_OBJECT *)a; /* XXX: ugly but harmless */
  383. adp = lh_ADDED_OBJ_retrieve(added, &ad);
  384. if (adp != NULL)
  385. return (adp->obj->nid);
  386. }
  387. op = OBJ_bsearch_obj(&a, obj_objs, NUM_OBJ);
  388. if (op == NULL)
  389. return (NID_undef);
  390. return (nid_objs[*op].nid);
  391. }
  392. /*
  393. * Convert an object name into an ASN1_OBJECT if "noname" is not set then
  394. * search for short and long names first. This will convert the "dotted" form
  395. * into an object: unlike OBJ_txt2nid it can be used with any objects, not
  396. * just registered ones.
  397. */
  398. ASN1_OBJECT *OBJ_txt2obj(const char *s, int no_name)
  399. {
  400. int nid = NID_undef;
  401. ASN1_OBJECT *op = NULL;
  402. unsigned char *buf;
  403. unsigned char *p;
  404. const unsigned char *cp;
  405. int i, j;
  406. if (!no_name) {
  407. if (((nid = OBJ_sn2nid(s)) != NID_undef) ||
  408. ((nid = OBJ_ln2nid(s)) != NID_undef))
  409. return OBJ_nid2obj(nid);
  410. }
  411. /* Work out size of content octets */
  412. i = a2d_ASN1_OBJECT(NULL, 0, s, -1);
  413. if (i <= 0) {
  414. /* Don't clear the error */
  415. /*
  416. * ERR_clear_error();
  417. */
  418. return NULL;
  419. }
  420. /* Work out total size */
  421. j = ASN1_object_size(0, i, V_ASN1_OBJECT);
  422. if ((buf = (unsigned char *)OPENSSL_malloc(j)) == NULL)
  423. return NULL;
  424. p = buf;
  425. /* Write out tag+length */
  426. ASN1_put_object(&p, 0, i, V_ASN1_OBJECT, V_ASN1_UNIVERSAL);
  427. /* Write out contents */
  428. a2d_ASN1_OBJECT(p, i, s, -1);
  429. cp = buf;
  430. op = d2i_ASN1_OBJECT(NULL, &cp, j);
  431. OPENSSL_free(buf);
  432. return op;
  433. }
  434. int OBJ_obj2txt(char *buf, int buf_len, const ASN1_OBJECT *a, int no_name)
  435. {
  436. int i, n = 0, len, nid, first, use_bn;
  437. BIGNUM *bl;
  438. unsigned long l;
  439. const unsigned char *p;
  440. char tbuf[DECIMAL_SIZE(i) + DECIMAL_SIZE(l) + 2];
  441. /* Ensure that, at every state, |buf| is NUL-terminated. */
  442. if (buf && buf_len > 0)
  443. buf[0] = '\0';
  444. if ((a == NULL) || (a->data == NULL))
  445. return (0);
  446. if (!no_name && (nid = OBJ_obj2nid(a)) != NID_undef) {
  447. const char *s;
  448. s = OBJ_nid2ln(nid);
  449. if (s == NULL)
  450. s = OBJ_nid2sn(nid);
  451. if (s) {
  452. if (buf)
  453. BUF_strlcpy(buf, s, buf_len);
  454. n = strlen(s);
  455. return n;
  456. }
  457. }
  458. len = a->length;
  459. p = a->data;
  460. first = 1;
  461. bl = NULL;
  462. while (len > 0) {
  463. l = 0;
  464. use_bn = 0;
  465. for (;;) {
  466. unsigned char c = *p++;
  467. len--;
  468. if ((len == 0) && (c & 0x80))
  469. goto err;
  470. if (use_bn) {
  471. if (!BN_add_word(bl, c & 0x7f))
  472. goto err;
  473. } else
  474. l |= c & 0x7f;
  475. if (!(c & 0x80))
  476. break;
  477. if (!use_bn && (l > (ULONG_MAX >> 7L))) {
  478. if (!bl && !(bl = BN_new()))
  479. goto err;
  480. if (!BN_set_word(bl, l))
  481. goto err;
  482. use_bn = 1;
  483. }
  484. if (use_bn) {
  485. if (!BN_lshift(bl, bl, 7))
  486. goto err;
  487. } else
  488. l <<= 7L;
  489. }
  490. if (first) {
  491. first = 0;
  492. if (l >= 80) {
  493. i = 2;
  494. if (use_bn) {
  495. if (!BN_sub_word(bl, 80))
  496. goto err;
  497. } else
  498. l -= 80;
  499. } else {
  500. i = (int)(l / 40);
  501. l -= (long)(i * 40);
  502. }
  503. if (buf && (buf_len > 1)) {
  504. *buf++ = i + '0';
  505. *buf = '\0';
  506. buf_len--;
  507. }
  508. n++;
  509. }
  510. if (use_bn) {
  511. char *bndec;
  512. bndec = BN_bn2dec(bl);
  513. if (!bndec)
  514. goto err;
  515. i = strlen(bndec);
  516. if (buf) {
  517. if (buf_len > 1) {
  518. *buf++ = '.';
  519. *buf = '\0';
  520. buf_len--;
  521. }
  522. BUF_strlcpy(buf, bndec, buf_len);
  523. if (i > buf_len) {
  524. buf += buf_len;
  525. buf_len = 0;
  526. } else {
  527. buf += i;
  528. buf_len -= i;
  529. }
  530. }
  531. n++;
  532. n += i;
  533. OPENSSL_free(bndec);
  534. } else {
  535. BIO_snprintf(tbuf, sizeof tbuf, ".%lu", l);
  536. i = strlen(tbuf);
  537. if (buf && (buf_len > 0)) {
  538. BUF_strlcpy(buf, tbuf, buf_len);
  539. if (i > buf_len) {
  540. buf += buf_len;
  541. buf_len = 0;
  542. } else {
  543. buf += i;
  544. buf_len -= i;
  545. }
  546. }
  547. n += i;
  548. l = 0;
  549. }
  550. }
  551. if (bl)
  552. BN_free(bl);
  553. return n;
  554. err:
  555. if (bl)
  556. BN_free(bl);
  557. return -1;
  558. }
  559. int OBJ_txt2nid(const char *s)
  560. {
  561. ASN1_OBJECT *obj;
  562. int nid;
  563. obj = OBJ_txt2obj(s, 0);
  564. nid = OBJ_obj2nid(obj);
  565. ASN1_OBJECT_free(obj);
  566. return nid;
  567. }
  568. int OBJ_ln2nid(const char *s)
  569. {
  570. ASN1_OBJECT o;
  571. const ASN1_OBJECT *oo = &o;
  572. ADDED_OBJ ad, *adp;
  573. const unsigned int *op;
  574. o.ln = s;
  575. if (added != NULL) {
  576. ad.type = ADDED_LNAME;
  577. ad.obj = &o;
  578. adp = lh_ADDED_OBJ_retrieve(added, &ad);
  579. if (adp != NULL)
  580. return (adp->obj->nid);
  581. }
  582. op = OBJ_bsearch_ln(&oo, ln_objs, NUM_LN);
  583. if (op == NULL)
  584. return (NID_undef);
  585. return (nid_objs[*op].nid);
  586. }
  587. int OBJ_sn2nid(const char *s)
  588. {
  589. ASN1_OBJECT o;
  590. const ASN1_OBJECT *oo = &o;
  591. ADDED_OBJ ad, *adp;
  592. const unsigned int *op;
  593. o.sn = s;
  594. if (added != NULL) {
  595. ad.type = ADDED_SNAME;
  596. ad.obj = &o;
  597. adp = lh_ADDED_OBJ_retrieve(added, &ad);
  598. if (adp != NULL)
  599. return (adp->obj->nid);
  600. }
  601. op = OBJ_bsearch_sn(&oo, sn_objs, NUM_SN);
  602. if (op == NULL)
  603. return (NID_undef);
  604. return (nid_objs[*op].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 *base = base_;
  617. int l, h, i = 0, c = 0;
  618. const char *p = NULL;
  619. if (num == 0)
  620. return (NULL);
  621. l = 0;
  622. h = num;
  623. while (l < h) {
  624. i = (l + h) / 2;
  625. p = &(base[i * size]);
  626. c = (*cmp) (key, p);
  627. if (c < 0)
  628. h = i;
  629. else if (c > 0)
  630. l = i + 1;
  631. else
  632. break;
  633. }
  634. #ifdef CHARSET_EBCDIC
  635. /*
  636. * THIS IS A KLUDGE - Because the *_obj is sorted in ASCII order, and I
  637. * don't have perl (yet), we revert to a *LINEAR* search when the object
  638. * wasn't found in the binary search.
  639. */
  640. if (c != 0) {
  641. for (i = 0; i < num; ++i) {
  642. p = &(base[i * size]);
  643. c = (*cmp) (key, p);
  644. if (c == 0 || (c < 0 && (flags & OBJ_BSEARCH_VALUE_ON_NOMATCH)))
  645. return p;
  646. }
  647. }
  648. #endif
  649. if (c != 0 && !(flags & OBJ_BSEARCH_VALUE_ON_NOMATCH))
  650. p = NULL;
  651. else if (c == 0 && (flags & OBJ_BSEARCH_FIRST_VALUE_ON_MATCH)) {
  652. while (i > 0 && (*cmp) (key, &(base[(i - 1) * size])) == 0)
  653. i--;
  654. p = &(base[i * size]);
  655. }
  656. return (p);
  657. }
  658. int OBJ_create_objects(BIO *in)
  659. {
  660. MS_STATIC char buf[512];
  661. int i, num = 0;
  662. char *o, *s, *l = NULL;
  663. for (;;) {
  664. s = o = NULL;
  665. i = BIO_gets(in, buf, 512);
  666. if (i <= 0)
  667. return (num);
  668. buf[i - 1] = '\0';
  669. if (!isalnum((unsigned char)buf[0]))
  670. return (num);
  671. o = s = buf;
  672. while (isdigit((unsigned char)*s) || (*s == '.'))
  673. s++;
  674. if (*s != '\0') {
  675. *(s++) = '\0';
  676. while (isspace((unsigned char)*s))
  677. s++;
  678. if (*s == '\0')
  679. s = NULL;
  680. else {
  681. l = s;
  682. while ((*l != '\0') && !isspace((unsigned char)*l))
  683. l++;
  684. if (*l != '\0') {
  685. *(l++) = '\0';
  686. while (isspace((unsigned char)*l))
  687. l++;
  688. if (*l == '\0')
  689. l = NULL;
  690. } else
  691. l = NULL;
  692. }
  693. } else
  694. s = NULL;
  695. if ((o == NULL) || (*o == '\0'))
  696. return (num);
  697. if (!OBJ_create(o, s, l))
  698. return (num);
  699. num++;
  700. }
  701. /* return(num); */
  702. }
  703. int OBJ_create(const char *oid, const char *sn, const char *ln)
  704. {
  705. int ok = 0;
  706. ASN1_OBJECT *op = NULL;
  707. unsigned char *buf;
  708. int i;
  709. i = a2d_ASN1_OBJECT(NULL, 0, oid, -1);
  710. if (i <= 0)
  711. return (0);
  712. if ((buf = (unsigned char *)OPENSSL_malloc(i)) == NULL) {
  713. OBJerr(OBJ_F_OBJ_CREATE, ERR_R_MALLOC_FAILURE);
  714. return (0);
  715. }
  716. i = a2d_ASN1_OBJECT(buf, i, oid, -1);
  717. if (i == 0)
  718. goto err;
  719. op = (ASN1_OBJECT *)ASN1_OBJECT_create(OBJ_new_nid(1), buf, i, sn, ln);
  720. if (op == NULL)
  721. goto err;
  722. ok = OBJ_add_object(op);
  723. err:
  724. ASN1_OBJECT_free(op);
  725. OPENSSL_free(buf);
  726. return (ok);
  727. }