obj_dat.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  1. /* crypto/objects/obj_dat.c */
  2. /* Copyright (C) 1995-1998 Eric Young ([email protected])
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young ([email protected]).
  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 ([email protected]).
  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 ([email protected])"
  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 ([email protected])"
  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. ASN1_OBJECT_free(o);
  279. return NID_undef;
  280. }
  281. ASN1_OBJECT *OBJ_nid2obj(int n)
  282. {
  283. ADDED_OBJ ad, *adp;
  284. ASN1_OBJECT ob;
  285. if ((n >= 0) && (n < NUM_NID)) {
  286. if ((n != NID_undef) && (nid_objs[n].nid == NID_undef)) {
  287. OBJerr(OBJ_F_OBJ_NID2OBJ, OBJ_R_UNKNOWN_NID);
  288. return (NULL);
  289. }
  290. return ((ASN1_OBJECT *)&(nid_objs[n]));
  291. } else if (added == NULL)
  292. return (NULL);
  293. else {
  294. ad.type = ADDED_NID;
  295. ad.obj = &ob;
  296. ob.nid = n;
  297. adp = lh_ADDED_OBJ_retrieve(added, &ad);
  298. if (adp != NULL)
  299. return (adp->obj);
  300. else {
  301. OBJerr(OBJ_F_OBJ_NID2OBJ, OBJ_R_UNKNOWN_NID);
  302. return (NULL);
  303. }
  304. }
  305. }
  306. const char *OBJ_nid2sn(int n)
  307. {
  308. ADDED_OBJ ad, *adp;
  309. ASN1_OBJECT ob;
  310. if ((n >= 0) && (n < NUM_NID)) {
  311. if ((n != NID_undef) && (nid_objs[n].nid == NID_undef)) {
  312. OBJerr(OBJ_F_OBJ_NID2SN, OBJ_R_UNKNOWN_NID);
  313. return (NULL);
  314. }
  315. return (nid_objs[n].sn);
  316. } else if (added == NULL)
  317. return (NULL);
  318. else {
  319. ad.type = ADDED_NID;
  320. ad.obj = &ob;
  321. ob.nid = n;
  322. adp = lh_ADDED_OBJ_retrieve(added, &ad);
  323. if (adp != NULL)
  324. return (adp->obj->sn);
  325. else {
  326. OBJerr(OBJ_F_OBJ_NID2SN, OBJ_R_UNKNOWN_NID);
  327. return (NULL);
  328. }
  329. }
  330. }
  331. const char *OBJ_nid2ln(int n)
  332. {
  333. ADDED_OBJ ad, *adp;
  334. ASN1_OBJECT ob;
  335. if ((n >= 0) && (n < NUM_NID)) {
  336. if ((n != NID_undef) && (nid_objs[n].nid == NID_undef)) {
  337. OBJerr(OBJ_F_OBJ_NID2LN, OBJ_R_UNKNOWN_NID);
  338. return (NULL);
  339. }
  340. return (nid_objs[n].ln);
  341. } else if (added == NULL)
  342. return (NULL);
  343. else {
  344. ad.type = ADDED_NID;
  345. ad.obj = &ob;
  346. ob.nid = n;
  347. adp = lh_ADDED_OBJ_retrieve(added, &ad);
  348. if (adp != NULL)
  349. return (adp->obj->ln);
  350. else {
  351. OBJerr(OBJ_F_OBJ_NID2LN, OBJ_R_UNKNOWN_NID);
  352. return (NULL);
  353. }
  354. }
  355. }
  356. static int obj_cmp(const ASN1_OBJECT *const *ap, const unsigned int *bp)
  357. {
  358. int j;
  359. const ASN1_OBJECT *a = *ap;
  360. const ASN1_OBJECT *b = &nid_objs[*bp];
  361. j = (a->length - b->length);
  362. if (j)
  363. return (j);
  364. if (a->length == 0)
  365. return 0;
  366. return (memcmp(a->data, b->data, a->length));
  367. }
  368. IMPLEMENT_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, obj);
  369. int OBJ_obj2nid(const ASN1_OBJECT *a)
  370. {
  371. const unsigned int *op;
  372. ADDED_OBJ ad, *adp;
  373. if (a == NULL)
  374. return (NID_undef);
  375. if (a->nid != 0)
  376. return (a->nid);
  377. if (a->length == 0)
  378. return NID_undef;
  379. if (added != NULL) {
  380. ad.type = ADDED_DATA;
  381. ad.obj = (ASN1_OBJECT *)a; /* XXX: ugly but harmless */
  382. adp = lh_ADDED_OBJ_retrieve(added, &ad);
  383. if (adp != NULL)
  384. return (adp->obj->nid);
  385. }
  386. op = OBJ_bsearch_obj(&a, obj_objs, NUM_OBJ);
  387. if (op == NULL)
  388. return (NID_undef);
  389. return (nid_objs[*op].nid);
  390. }
  391. /*
  392. * Convert an object name into an ASN1_OBJECT if "noname" is not set then
  393. * search for short and long names first. This will convert the "dotted" form
  394. * into an object: unlike OBJ_txt2nid it can be used with any objects, not
  395. * just registered ones.
  396. */
  397. ASN1_OBJECT *OBJ_txt2obj(const char *s, int no_name)
  398. {
  399. int nid = NID_undef;
  400. ASN1_OBJECT *op = NULL;
  401. unsigned char *buf;
  402. unsigned char *p;
  403. const unsigned char *cp;
  404. int i, j;
  405. if (!no_name) {
  406. if (((nid = OBJ_sn2nid(s)) != NID_undef) ||
  407. ((nid = OBJ_ln2nid(s)) != NID_undef))
  408. return OBJ_nid2obj(nid);
  409. }
  410. /* Work out size of content octets */
  411. i = a2d_ASN1_OBJECT(NULL, 0, s, -1);
  412. if (i <= 0) {
  413. /* Don't clear the error */
  414. /*
  415. * ERR_clear_error();
  416. */
  417. return NULL;
  418. }
  419. /* Work out total size */
  420. j = ASN1_object_size(0, i, V_ASN1_OBJECT);
  421. if ((buf = (unsigned char *)OPENSSL_malloc(j)) == NULL)
  422. return NULL;
  423. p = buf;
  424. /* Write out tag+length */
  425. ASN1_put_object(&p, 0, i, V_ASN1_OBJECT, V_ASN1_UNIVERSAL);
  426. /* Write out contents */
  427. a2d_ASN1_OBJECT(p, i, s, -1);
  428. cp = buf;
  429. op = d2i_ASN1_OBJECT(NULL, &cp, j);
  430. OPENSSL_free(buf);
  431. return op;
  432. }
  433. int OBJ_obj2txt(char *buf, int buf_len, const ASN1_OBJECT *a, int no_name)
  434. {
  435. int i, n = 0, len, nid, first, use_bn;
  436. BIGNUM *bl;
  437. unsigned long l;
  438. const unsigned char *p;
  439. char tbuf[DECIMAL_SIZE(i) + DECIMAL_SIZE(l) + 2];
  440. /* Ensure that, at every state, |buf| is NUL-terminated. */
  441. if (buf && buf_len > 0)
  442. buf[0] = '\0';
  443. if ((a == NULL) || (a->data == NULL))
  444. return (0);
  445. if (!no_name && (nid = OBJ_obj2nid(a)) != NID_undef) {
  446. const char *s;
  447. s = OBJ_nid2ln(nid);
  448. if (s == NULL)
  449. s = OBJ_nid2sn(nid);
  450. if (s) {
  451. if (buf)
  452. BUF_strlcpy(buf, s, buf_len);
  453. n = strlen(s);
  454. return n;
  455. }
  456. }
  457. len = a->length;
  458. p = a->data;
  459. first = 1;
  460. bl = NULL;
  461. while (len > 0) {
  462. l = 0;
  463. use_bn = 0;
  464. for (;;) {
  465. unsigned char c = *p++;
  466. len--;
  467. if ((len == 0) && (c & 0x80))
  468. goto err;
  469. if (use_bn) {
  470. if (!BN_add_word(bl, c & 0x7f))
  471. goto err;
  472. } else
  473. l |= c & 0x7f;
  474. if (!(c & 0x80))
  475. break;
  476. if (!use_bn && (l > (ULONG_MAX >> 7L))) {
  477. if (!bl && !(bl = BN_new()))
  478. goto err;
  479. if (!BN_set_word(bl, l))
  480. goto err;
  481. use_bn = 1;
  482. }
  483. if (use_bn) {
  484. if (!BN_lshift(bl, bl, 7))
  485. goto err;
  486. } else
  487. l <<= 7L;
  488. }
  489. if (first) {
  490. first = 0;
  491. if (l >= 80) {
  492. i = 2;
  493. if (use_bn) {
  494. if (!BN_sub_word(bl, 80))
  495. goto err;
  496. } else
  497. l -= 80;
  498. } else {
  499. i = (int)(l / 40);
  500. l -= (long)(i * 40);
  501. }
  502. if (buf && (buf_len > 1)) {
  503. *buf++ = i + '0';
  504. *buf = '\0';
  505. buf_len--;
  506. }
  507. n++;
  508. }
  509. if (use_bn) {
  510. char *bndec;
  511. bndec = BN_bn2dec(bl);
  512. if (!bndec)
  513. goto err;
  514. i = strlen(bndec);
  515. if (buf) {
  516. if (buf_len > 1) {
  517. *buf++ = '.';
  518. *buf = '\0';
  519. buf_len--;
  520. }
  521. BUF_strlcpy(buf, bndec, buf_len);
  522. if (i > buf_len) {
  523. buf += buf_len;
  524. buf_len = 0;
  525. } else {
  526. buf += i;
  527. buf_len -= i;
  528. }
  529. }
  530. n++;
  531. n += i;
  532. OPENSSL_free(bndec);
  533. } else {
  534. BIO_snprintf(tbuf, sizeof(tbuf), ".%lu", l);
  535. i = strlen(tbuf);
  536. if (buf && (buf_len > 0)) {
  537. BUF_strlcpy(buf, tbuf, buf_len);
  538. if (i > buf_len) {
  539. buf += buf_len;
  540. buf_len = 0;
  541. } else {
  542. buf += i;
  543. buf_len -= i;
  544. }
  545. }
  546. n += i;
  547. l = 0;
  548. }
  549. }
  550. if (bl)
  551. BN_free(bl);
  552. return n;
  553. err:
  554. if (bl)
  555. BN_free(bl);
  556. return -1;
  557. }
  558. int OBJ_txt2nid(const char *s)
  559. {
  560. ASN1_OBJECT *obj;
  561. int nid;
  562. obj = OBJ_txt2obj(s, 0);
  563. nid = OBJ_obj2nid(obj);
  564. ASN1_OBJECT_free(obj);
  565. return nid;
  566. }
  567. int OBJ_ln2nid(const char *s)
  568. {
  569. ASN1_OBJECT o;
  570. const ASN1_OBJECT *oo = &o;
  571. ADDED_OBJ ad, *adp;
  572. const unsigned int *op;
  573. o.ln = s;
  574. if (added != NULL) {
  575. ad.type = ADDED_LNAME;
  576. ad.obj = &o;
  577. adp = lh_ADDED_OBJ_retrieve(added, &ad);
  578. if (adp != NULL)
  579. return (adp->obj->nid);
  580. }
  581. op = OBJ_bsearch_ln(&oo, ln_objs, NUM_LN);
  582. if (op == NULL)
  583. return (NID_undef);
  584. return (nid_objs[*op].nid);
  585. }
  586. int OBJ_sn2nid(const char *s)
  587. {
  588. ASN1_OBJECT o;
  589. const ASN1_OBJECT *oo = &o;
  590. ADDED_OBJ ad, *adp;
  591. const unsigned int *op;
  592. o.sn = s;
  593. if (added != NULL) {
  594. ad.type = ADDED_SNAME;
  595. ad.obj = &o;
  596. adp = lh_ADDED_OBJ_retrieve(added, &ad);
  597. if (adp != NULL)
  598. return (adp->obj->nid);
  599. }
  600. op = OBJ_bsearch_sn(&oo, sn_objs, NUM_SN);
  601. if (op == NULL)
  602. return (NID_undef);
  603. return (nid_objs[*op].nid);
  604. }
  605. const void *OBJ_bsearch_(const void *key, const void *base, int num, int size,
  606. int (*cmp) (const void *, const void *))
  607. {
  608. return OBJ_bsearch_ex_(key, base, num, size, cmp, 0);
  609. }
  610. const void *OBJ_bsearch_ex_(const void *key, const void *base_, int num,
  611. int size,
  612. int (*cmp) (const void *, const void *),
  613. int flags)
  614. {
  615. const char *base = base_;
  616. int l, h, i = 0, c = 0;
  617. const char *p = NULL;
  618. if (num == 0)
  619. return (NULL);
  620. l = 0;
  621. h = num;
  622. while (l < h) {
  623. i = (l + h) / 2;
  624. p = &(base[i * size]);
  625. c = (*cmp) (key, p);
  626. if (c < 0)
  627. h = i;
  628. else if (c > 0)
  629. l = i + 1;
  630. else
  631. break;
  632. }
  633. #ifdef CHARSET_EBCDIC
  634. /*
  635. * THIS IS A KLUDGE - Because the *_obj is sorted in ASCII order, and I
  636. * don't have perl (yet), we revert to a *LINEAR* search when the object
  637. * wasn't found in the binary search.
  638. */
  639. if (c != 0) {
  640. for (i = 0; i < num; ++i) {
  641. p = &(base[i * size]);
  642. c = (*cmp) (key, p);
  643. if (c == 0 || (c < 0 && (flags & OBJ_BSEARCH_VALUE_ON_NOMATCH)))
  644. return p;
  645. }
  646. }
  647. #endif
  648. if (c != 0 && !(flags & OBJ_BSEARCH_VALUE_ON_NOMATCH))
  649. p = NULL;
  650. else if (c == 0 && (flags & OBJ_BSEARCH_FIRST_VALUE_ON_MATCH)) {
  651. while (i > 0 && (*cmp) (key, &(base[(i - 1) * size])) == 0)
  652. i--;
  653. p = &(base[i * size]);
  654. }
  655. return (p);
  656. }
  657. /*
  658. * Parse a BIO sink to create some extra oid's objects.
  659. * Line format:<OID:isdigit or '.']><isspace><SN><isspace><LN>
  660. */
  661. int OBJ_create_objects(BIO *in)
  662. {
  663. MS_STATIC char buf[512];
  664. int i, num = 0;
  665. char *o, *s, *l = NULL;
  666. for (;;) {
  667. s = o = NULL;
  668. i = BIO_gets(in, buf, 512);
  669. if (i <= 0)
  670. return (num);
  671. buf[i - 1] = '\0';
  672. if (!isalnum((unsigned char)buf[0]))
  673. return (num);
  674. o = s = buf;
  675. while (isdigit((unsigned char)*s) || (*s == '.'))
  676. s++;
  677. if (*s != '\0') {
  678. *(s++) = '\0';
  679. while (isspace((unsigned char)*s))
  680. s++;
  681. if (*s == '\0') {
  682. s = NULL;
  683. } else {
  684. l = s;
  685. while ((*l != '\0') && !isspace((unsigned char)*l))
  686. l++;
  687. if (*l != '\0') {
  688. *(l++) = '\0';
  689. while (isspace((unsigned char)*l))
  690. l++;
  691. if (*l == '\0') {
  692. l = NULL;
  693. }
  694. } else {
  695. l = NULL;
  696. }
  697. }
  698. } else {
  699. s = NULL;
  700. }
  701. if (*o == '\0')
  702. return num;
  703. if (!OBJ_create(o, s, l))
  704. return (num);
  705. num++;
  706. }
  707. /* return(num); */
  708. }
  709. int OBJ_create(const char *oid, const char *sn, const char *ln)
  710. {
  711. int ok = 0;
  712. ASN1_OBJECT *op = NULL;
  713. unsigned char *buf;
  714. int i;
  715. i = a2d_ASN1_OBJECT(NULL, 0, oid, -1);
  716. if (i <= 0)
  717. return (0);
  718. if ((buf = (unsigned char *)OPENSSL_malloc(i)) == NULL) {
  719. OBJerr(OBJ_F_OBJ_CREATE, ERR_R_MALLOC_FAILURE);
  720. return (0);
  721. }
  722. i = a2d_ASN1_OBJECT(buf, i, oid, -1);
  723. if (i == 0)
  724. goto err;
  725. op = (ASN1_OBJECT *)ASN1_OBJECT_create(OBJ_new_nid(1), buf, i, sn, ln);
  726. if (op == NULL)
  727. goto err;
  728. ok = OBJ_add_object(op);
  729. err:
  730. ASN1_OBJECT_free(op);
  731. OPENSSL_free(buf);
  732. return (ok);
  733. }