2
0

obj_dat.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  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. {
  90. int type;
  91. ASN1_OBJECT *obj;
  92. } ADDED_OBJ;
  93. DECLARE_LHASH_OF(ADDED_OBJ);
  94. static int new_nid=NUM_NID;
  95. static LHASH_OF(ADDED_OBJ) *added=NULL;
  96. static int sn_cmp(const ASN1_OBJECT * const *a, const unsigned int *b)
  97. { return(strcmp((*a)->sn,nid_objs[*b].sn)); }
  98. IMPLEMENT_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, sn);
  99. static int ln_cmp(const ASN1_OBJECT * const *a, const unsigned int *b)
  100. { return(strcmp((*a)->ln,nid_objs[*b].ln)); }
  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. {
  111. case ADDED_DATA:
  112. ret=a->length<<20L;
  113. p=(unsigned char *)a->data;
  114. for (i=0; i<a->length; i++)
  115. ret^=p[i]<<((i*3)%24);
  116. break;
  117. case ADDED_SNAME:
  118. ret=lh_strhash(a->sn);
  119. break;
  120. case ADDED_LNAME:
  121. ret=lh_strhash(a->ln);
  122. break;
  123. case ADDED_NID:
  124. ret=a->nid;
  125. break;
  126. default:
  127. /* abort(); */
  128. return 0;
  129. }
  130. ret&=0x3fffffffL;
  131. ret|=ca->type<<30L;
  132. return(ret);
  133. }
  134. static IMPLEMENT_LHASH_HASH_FN(added_obj, ADDED_OBJ)
  135. static int added_obj_cmp(const ADDED_OBJ *ca, const ADDED_OBJ *cb)
  136. {
  137. ASN1_OBJECT *a,*b;
  138. int i;
  139. i=ca->type-cb->type;
  140. if (i) return(i);
  141. a=ca->obj;
  142. b=cb->obj;
  143. switch (ca->type)
  144. {
  145. case ADDED_DATA:
  146. i=(a->length - b->length);
  147. if (i) return(i);
  148. return(memcmp(a->data,b->data,(size_t)a->length));
  149. case ADDED_SNAME:
  150. if (a->sn == NULL) return(-1);
  151. else if (b->sn == NULL) return(1);
  152. else return(strcmp(a->sn,b->sn));
  153. case ADDED_LNAME:
  154. if (a->ln == NULL) return(-1);
  155. else if (b->ln == NULL) return(1);
  156. else return(strcmp(a->ln,b->ln));
  157. case ADDED_NID:
  158. return(a->nid-b->nid);
  159. default:
  160. /* abort(); */
  161. return 0;
  162. }
  163. }
  164. static IMPLEMENT_LHASH_COMP_FN(added_obj, ADDED_OBJ)
  165. static int init_added(void)
  166. {
  167. if (added != NULL) return(1);
  168. added=lh_ADDED_OBJ_new();
  169. return(added != NULL);
  170. }
  171. static void cleanup1_doall(ADDED_OBJ *a)
  172. {
  173. a->obj->nid=0;
  174. a->obj->flags|=ASN1_OBJECT_FLAG_DYNAMIC|
  175. ASN1_OBJECT_FLAG_DYNAMIC_STRINGS|
  176. ASN1_OBJECT_FLAG_DYNAMIC_DATA;
  177. }
  178. static void cleanup2_doall(ADDED_OBJ *a)
  179. { a->obj->nid++; }
  180. static void cleanup3_doall(ADDED_OBJ *a)
  181. {
  182. if (--a->obj->nid == 0)
  183. ASN1_OBJECT_free(a->obj);
  184. OPENSSL_free(a);
  185. }
  186. static IMPLEMENT_LHASH_DOALL_FN(cleanup1, ADDED_OBJ)
  187. static IMPLEMENT_LHASH_DOALL_FN(cleanup2, ADDED_OBJ)
  188. static IMPLEMENT_LHASH_DOALL_FN(cleanup3, ADDED_OBJ)
  189. /* The purpose of obj_cleanup_defer is to avoid EVP_cleanup() attempting
  190. * to use freed up OIDs. If neccessary the actual freeing up of OIDs is
  191. * delayed.
  192. */
  193. int obj_cleanup_defer = 0;
  194. void check_defer(int nid)
  195. {
  196. if (!obj_cleanup_defer && nid >= NUM_NID)
  197. obj_cleanup_defer = 1;
  198. }
  199. void OBJ_cleanup(void)
  200. {
  201. if (obj_cleanup_defer)
  202. {
  203. obj_cleanup_defer = 2;
  204. return ;
  205. }
  206. if (added == NULL) return;
  207. lh_ADDED_OBJ_down_load(added) = 0;
  208. lh_ADDED_OBJ_doall(added,LHASH_DOALL_FN(cleanup1)); /* zero counters */
  209. lh_ADDED_OBJ_doall(added,LHASH_DOALL_FN(cleanup2)); /* set counters */
  210. lh_ADDED_OBJ_doall(added,LHASH_DOALL_FN(cleanup3)); /* free objects */
  211. lh_ADDED_OBJ_free(added);
  212. added=NULL;
  213. }
  214. int OBJ_new_nid(int num)
  215. {
  216. int i;
  217. i=new_nid;
  218. new_nid+=num;
  219. return(i);
  220. }
  221. int OBJ_add_object(const ASN1_OBJECT *obj)
  222. {
  223. ASN1_OBJECT *o;
  224. ADDED_OBJ *ao[4]={NULL,NULL,NULL,NULL},*aop;
  225. int i;
  226. if (added == NULL)
  227. if (!init_added()) return(0);
  228. if ((o=OBJ_dup(obj)) == NULL) goto err;
  229. if (!(ao[ADDED_NID]=(ADDED_OBJ *)OPENSSL_malloc(sizeof(ADDED_OBJ)))) goto err2;
  230. if ((o->length != 0) && (obj->data != NULL))
  231. if (!(ao[ADDED_DATA]=(ADDED_OBJ *)OPENSSL_malloc(sizeof(ADDED_OBJ)))) goto err2;
  232. if (o->sn != NULL)
  233. if (!(ao[ADDED_SNAME]=(ADDED_OBJ *)OPENSSL_malloc(sizeof(ADDED_OBJ)))) goto err2;
  234. if (o->ln != NULL)
  235. if (!(ao[ADDED_LNAME]=(ADDED_OBJ *)OPENSSL_malloc(sizeof(ADDED_OBJ)))) goto err2;
  236. for (i=ADDED_DATA; i<=ADDED_NID; i++)
  237. {
  238. if (ao[i] != NULL)
  239. {
  240. ao[i]->type=i;
  241. ao[i]->obj=o;
  242. aop=lh_ADDED_OBJ_insert(added,ao[i]);
  243. /* memory leak, buit should not normally matter */
  244. if (aop != NULL)
  245. OPENSSL_free(aop);
  246. }
  247. }
  248. o->flags&= ~(ASN1_OBJECT_FLAG_DYNAMIC|ASN1_OBJECT_FLAG_DYNAMIC_STRINGS|
  249. ASN1_OBJECT_FLAG_DYNAMIC_DATA);
  250. return(o->nid);
  251. err2:
  252. OBJerr(OBJ_F_OBJ_ADD_OBJECT,ERR_R_MALLOC_FAILURE);
  253. err:
  254. for (i=ADDED_DATA; i<=ADDED_NID; i++)
  255. if (ao[i] != NULL) OPENSSL_free(ao[i]);
  256. if (o != NULL) OPENSSL_free(o);
  257. return(NID_undef);
  258. }
  259. ASN1_OBJECT *OBJ_nid2obj(int n)
  260. {
  261. ADDED_OBJ ad,*adp;
  262. ASN1_OBJECT ob;
  263. if ((n >= 0) && (n < NUM_NID))
  264. {
  265. if ((n != NID_undef) && (nid_objs[n].nid == NID_undef))
  266. {
  267. OBJerr(OBJ_F_OBJ_NID2OBJ,OBJ_R_UNKNOWN_NID);
  268. return(NULL);
  269. }
  270. return((ASN1_OBJECT *)&(nid_objs[n]));
  271. }
  272. else if (added == NULL)
  273. return(NULL);
  274. else
  275. {
  276. ad.type=ADDED_NID;
  277. ad.obj= &ob;
  278. ob.nid=n;
  279. adp=lh_ADDED_OBJ_retrieve(added,&ad);
  280. if (adp != NULL)
  281. return(adp->obj);
  282. else
  283. {
  284. OBJerr(OBJ_F_OBJ_NID2OBJ,OBJ_R_UNKNOWN_NID);
  285. return(NULL);
  286. }
  287. }
  288. }
  289. const char *OBJ_nid2sn(int n)
  290. {
  291. ADDED_OBJ ad,*adp;
  292. ASN1_OBJECT ob;
  293. if ((n >= 0) && (n < NUM_NID))
  294. {
  295. if ((n != NID_undef) && (nid_objs[n].nid == NID_undef))
  296. {
  297. OBJerr(OBJ_F_OBJ_NID2SN,OBJ_R_UNKNOWN_NID);
  298. return(NULL);
  299. }
  300. return(nid_objs[n].sn);
  301. }
  302. else if (added == NULL)
  303. return(NULL);
  304. else
  305. {
  306. ad.type=ADDED_NID;
  307. ad.obj= &ob;
  308. ob.nid=n;
  309. adp=lh_ADDED_OBJ_retrieve(added,&ad);
  310. if (adp != NULL)
  311. return(adp->obj->sn);
  312. else
  313. {
  314. OBJerr(OBJ_F_OBJ_NID2SN,OBJ_R_UNKNOWN_NID);
  315. return(NULL);
  316. }
  317. }
  318. }
  319. const char *OBJ_nid2ln(int n)
  320. {
  321. ADDED_OBJ ad,*adp;
  322. ASN1_OBJECT ob;
  323. if ((n >= 0) && (n < NUM_NID))
  324. {
  325. if ((n != NID_undef) && (nid_objs[n].nid == NID_undef))
  326. {
  327. OBJerr(OBJ_F_OBJ_NID2LN,OBJ_R_UNKNOWN_NID);
  328. return(NULL);
  329. }
  330. return(nid_objs[n].ln);
  331. }
  332. else if (added == NULL)
  333. return(NULL);
  334. else
  335. {
  336. ad.type=ADDED_NID;
  337. ad.obj= &ob;
  338. ob.nid=n;
  339. adp=lh_ADDED_OBJ_retrieve(added,&ad);
  340. if (adp != NULL)
  341. return(adp->obj->ln);
  342. else
  343. {
  344. OBJerr(OBJ_F_OBJ_NID2LN,OBJ_R_UNKNOWN_NID);
  345. return(NULL);
  346. }
  347. }
  348. }
  349. static int obj_cmp(const ASN1_OBJECT * const *ap, const unsigned int *bp)
  350. {
  351. int j;
  352. const ASN1_OBJECT *a= *ap;
  353. const ASN1_OBJECT *b= &nid_objs[*bp];
  354. j=(a->length - b->length);
  355. if (j) return(j);
  356. return(memcmp(a->data,b->data,a->length));
  357. }
  358. IMPLEMENT_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, obj);
  359. int OBJ_obj2nid(const ASN1_OBJECT *a)
  360. {
  361. const unsigned int *op;
  362. ADDED_OBJ ad,*adp;
  363. if (a == NULL)
  364. return(NID_undef);
  365. if (a->nid != 0)
  366. return(a->nid);
  367. if (added != NULL)
  368. {
  369. ad.type=ADDED_DATA;
  370. ad.obj=(ASN1_OBJECT *)a; /* XXX: ugly but harmless */
  371. adp=lh_ADDED_OBJ_retrieve(added,&ad);
  372. if (adp != NULL) return (adp->obj->nid);
  373. }
  374. op=OBJ_bsearch_obj(&a, obj_objs, NUM_OBJ);
  375. if (op == NULL)
  376. return(NID_undef);
  377. return(nid_objs[*op].nid);
  378. }
  379. /* Convert an object name into an ASN1_OBJECT
  380. * if "noname" is not set then search for short and long names first.
  381. * This will convert the "dotted" form into an object: unlike OBJ_txt2nid
  382. * it can be used with any objects, not just registered ones.
  383. */
  384. ASN1_OBJECT *OBJ_txt2obj(const char *s, int no_name)
  385. {
  386. int nid = NID_undef;
  387. ASN1_OBJECT *op=NULL;
  388. unsigned char *buf;
  389. unsigned char *p;
  390. const unsigned char *cp;
  391. int i, j;
  392. if(!no_name) {
  393. if( ((nid = OBJ_sn2nid(s)) != NID_undef) ||
  394. ((nid = OBJ_ln2nid(s)) != NID_undef) )
  395. return OBJ_nid2obj(nid);
  396. }
  397. /* Work out size of content octets */
  398. i=a2d_ASN1_OBJECT(NULL,0,s,-1);
  399. if (i <= 0) {
  400. /* Don't clear the error */
  401. /*ERR_clear_error();*/
  402. return NULL;
  403. }
  404. /* Work out total size */
  405. j = ASN1_object_size(0,i,V_ASN1_OBJECT);
  406. if((buf=(unsigned char *)OPENSSL_malloc(j)) == NULL) return NULL;
  407. p = buf;
  408. /* Write out tag+length */
  409. ASN1_put_object(&p,0,i,V_ASN1_OBJECT,V_ASN1_UNIVERSAL);
  410. /* Write out contents */
  411. a2d_ASN1_OBJECT(p,i,s,-1);
  412. cp=buf;
  413. op=d2i_ASN1_OBJECT(NULL,&cp,j);
  414. OPENSSL_free(buf);
  415. return op;
  416. }
  417. int OBJ_obj2txt(char *buf, int buf_len, const ASN1_OBJECT *a, int no_name)
  418. {
  419. int i,n=0,len,nid, first, use_bn;
  420. BIGNUM *bl;
  421. unsigned long l;
  422. const unsigned char *p;
  423. char tbuf[DECIMAL_SIZE(i)+DECIMAL_SIZE(l)+2];
  424. if ((a == NULL) || (a->data == NULL)) {
  425. buf[0]='\0';
  426. return(0);
  427. }
  428. if (!no_name && (nid=OBJ_obj2nid(a)) != NID_undef)
  429. {
  430. const char *s;
  431. s=OBJ_nid2ln(nid);
  432. if (s == NULL)
  433. s=OBJ_nid2sn(nid);
  434. if (s)
  435. {
  436. if (buf)
  437. BUF_strlcpy(buf,s,buf_len);
  438. n=strlen(s);
  439. return n;
  440. }
  441. }
  442. len=a->length;
  443. p=a->data;
  444. first = 1;
  445. bl = NULL;
  446. while (len > 0)
  447. {
  448. l=0;
  449. use_bn = 0;
  450. for (;;)
  451. {
  452. unsigned char c = *p++;
  453. len--;
  454. if ((len == 0) && (c & 0x80))
  455. goto err;
  456. if (use_bn)
  457. {
  458. if (!BN_add_word(bl, c & 0x7f))
  459. goto err;
  460. }
  461. else
  462. l |= c & 0x7f;
  463. if (!(c & 0x80))
  464. break;
  465. if (!use_bn && (l > (ULONG_MAX >> 7L)))
  466. {
  467. if (!bl && !(bl = BN_new()))
  468. goto err;
  469. if (!BN_set_word(bl, l))
  470. goto err;
  471. use_bn = 1;
  472. }
  473. if (use_bn)
  474. {
  475. if (!BN_lshift(bl, bl, 7))
  476. goto err;
  477. }
  478. else
  479. l<<=7L;
  480. }
  481. if (first)
  482. {
  483. first = 0;
  484. if (l >= 80)
  485. {
  486. i = 2;
  487. if (use_bn)
  488. {
  489. if (!BN_sub_word(bl, 80))
  490. goto err;
  491. }
  492. else
  493. l -= 80;
  494. }
  495. else
  496. {
  497. i=(int)(l/40);
  498. l-=(long)(i*40);
  499. }
  500. if (buf && (buf_len > 0))
  501. {
  502. *buf++ = i + '0';
  503. buf_len--;
  504. }
  505. n++;
  506. }
  507. if (use_bn)
  508. {
  509. char *bndec;
  510. bndec = BN_bn2dec(bl);
  511. if (!bndec)
  512. goto err;
  513. i = strlen(bndec);
  514. if (buf)
  515. {
  516. if (buf_len > 0)
  517. {
  518. *buf++ = '.';
  519. buf_len--;
  520. }
  521. BUF_strlcpy(buf,bndec,buf_len);
  522. if (i > buf_len)
  523. {
  524. buf += buf_len;
  525. buf_len = 0;
  526. }
  527. else
  528. {
  529. buf+=i;
  530. buf_len-=i;
  531. }
  532. }
  533. n++;
  534. n += i;
  535. OPENSSL_free(bndec);
  536. }
  537. else
  538. {
  539. BIO_snprintf(tbuf,sizeof tbuf,".%lu",l);
  540. i=strlen(tbuf);
  541. if (buf && (buf_len > 0))
  542. {
  543. BUF_strlcpy(buf,tbuf,buf_len);
  544. if (i > buf_len)
  545. {
  546. buf += buf_len;
  547. buf_len = 0;
  548. }
  549. else
  550. {
  551. buf+=i;
  552. buf_len-=i;
  553. }
  554. }
  555. n+=i;
  556. l=0;
  557. }
  558. }
  559. if (bl)
  560. BN_free(bl);
  561. return n;
  562. err:
  563. if (bl)
  564. BN_free(bl);
  565. return -1;
  566. }
  567. int OBJ_txt2nid(const char *s)
  568. {
  569. ASN1_OBJECT *obj;
  570. int nid;
  571. obj = OBJ_txt2obj(s, 0);
  572. nid = OBJ_obj2nid(obj);
  573. ASN1_OBJECT_free(obj);
  574. return nid;
  575. }
  576. int OBJ_ln2nid(const char *s)
  577. {
  578. ASN1_OBJECT o;
  579. const ASN1_OBJECT *oo= &o;
  580. ADDED_OBJ ad,*adp;
  581. const unsigned int *op;
  582. o.ln=s;
  583. if (added != NULL)
  584. {
  585. ad.type=ADDED_LNAME;
  586. ad.obj= &o;
  587. adp=lh_ADDED_OBJ_retrieve(added,&ad);
  588. if (adp != NULL) return (adp->obj->nid);
  589. }
  590. op=OBJ_bsearch_ln(&oo, ln_objs, NUM_LN);
  591. if (op == NULL) return(NID_undef);
  592. return(nid_objs[*op].nid);
  593. }
  594. int OBJ_sn2nid(const char *s)
  595. {
  596. ASN1_OBJECT o;
  597. const ASN1_OBJECT *oo= &o;
  598. ADDED_OBJ ad,*adp;
  599. const unsigned int *op;
  600. o.sn=s;
  601. if (added != NULL)
  602. {
  603. ad.type=ADDED_SNAME;
  604. ad.obj= &o;
  605. adp=lh_ADDED_OBJ_retrieve(added,&ad);
  606. if (adp != NULL) return (adp->obj->nid);
  607. }
  608. op=OBJ_bsearch_sn(&oo, sn_objs, NUM_SN);
  609. if (op == NULL) return(NID_undef);
  610. return(nid_objs[*op].nid);
  611. }
  612. const void *OBJ_bsearch_(const void *key, const void *base, int num, int size,
  613. int (*cmp)(const void *, const void *))
  614. {
  615. return OBJ_bsearch_ex_(key, base, num, size, cmp, 0);
  616. }
  617. const void *OBJ_bsearch_ex_(const void *key, const void *base_, int num,
  618. int size,
  619. int (*cmp)(const void *, const void *),
  620. int flags)
  621. {
  622. const char *base=base_;
  623. int l,h,i=0,c=0;
  624. const char *p = NULL;
  625. if (num == 0) return(NULL);
  626. l=0;
  627. h=num;
  628. while (l < h)
  629. {
  630. i=(l+h)/2;
  631. p= &(base[i*size]);
  632. c=(*cmp)(key,p);
  633. if (c < 0)
  634. h=i;
  635. else if (c > 0)
  636. l=i+1;
  637. else
  638. break;
  639. }
  640. #ifdef CHARSET_EBCDIC
  641. /* THIS IS A KLUDGE - Because the *_obj is sorted in ASCII order, and
  642. * I don't have perl (yet), we revert to a *LINEAR* search
  643. * when the object wasn't found in the binary search.
  644. */
  645. if (c != 0)
  646. {
  647. for (i=0; i<num; ++i)
  648. {
  649. p= &(base[i*size]);
  650. c = (*cmp)(key,p);
  651. if (c == 0 || (c < 0 && (flags & OBJ_BSEARCH_VALUE_ON_NOMATCH)))
  652. return p;
  653. }
  654. }
  655. #endif
  656. if (c != 0 && !(flags & OBJ_BSEARCH_VALUE_ON_NOMATCH))
  657. p = NULL;
  658. else if (c == 0 && (flags & OBJ_BSEARCH_FIRST_VALUE_ON_MATCH))
  659. {
  660. while(i > 0 && (*cmp)(key,&(base[(i-1)*size])) == 0)
  661. i--;
  662. p = &(base[i*size]);
  663. }
  664. return(p);
  665. }
  666. int OBJ_create_objects(BIO *in)
  667. {
  668. MS_STATIC char buf[512];
  669. int i,num=0;
  670. char *o,*s,*l=NULL;
  671. for (;;)
  672. {
  673. s=o=NULL;
  674. i=BIO_gets(in,buf,512);
  675. if (i <= 0) return(num);
  676. buf[i-1]='\0';
  677. if (!isalnum((unsigned char)buf[0])) return(num);
  678. o=s=buf;
  679. while (isdigit((unsigned char)*s) || (*s == '.'))
  680. s++;
  681. if (*s != '\0')
  682. {
  683. *(s++)='\0';
  684. while (isspace((unsigned char)*s))
  685. s++;
  686. if (*s == '\0')
  687. s=NULL;
  688. else
  689. {
  690. l=s;
  691. while ((*l != '\0') && !isspace((unsigned char)*l))
  692. l++;
  693. if (*l != '\0')
  694. {
  695. *(l++)='\0';
  696. while (isspace((unsigned char)*l))
  697. l++;
  698. if (*l == '\0') l=NULL;
  699. }
  700. else
  701. l=NULL;
  702. }
  703. }
  704. else
  705. s=NULL;
  706. if ((o == NULL) || (*o == '\0')) return(num);
  707. if (!OBJ_create(o,s,l)) return(num);
  708. num++;
  709. }
  710. /* return(num); */
  711. }
  712. int OBJ_create(const char *oid, const char *sn, const char *ln)
  713. {
  714. int ok=0;
  715. ASN1_OBJECT *op=NULL;
  716. unsigned char *buf;
  717. int i;
  718. i=a2d_ASN1_OBJECT(NULL,0,oid,-1);
  719. if (i <= 0) return(0);
  720. if ((buf=(unsigned char *)OPENSSL_malloc(i)) == NULL)
  721. {
  722. OBJerr(OBJ_F_OBJ_CREATE,ERR_R_MALLOC_FAILURE);
  723. return(0);
  724. }
  725. i=a2d_ASN1_OBJECT(buf,i,oid,-1);
  726. if (i == 0)
  727. goto err;
  728. op=(ASN1_OBJECT *)ASN1_OBJECT_create(OBJ_new_nid(1),buf,i,sn,ln);
  729. if (op == NULL)
  730. goto err;
  731. ok=OBJ_add_object(op);
  732. err:
  733. ASN1_OBJECT_free(op);
  734. OPENSSL_free(buf);
  735. return(ok);
  736. }