PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/SSISSSHFTP/SSISSharpSSHFTP/SharpSSH/jsch/IdentityFile.cs

#
C# | 949 lines | 727 code | 67 blank | 155 comment | 112 complexity | 8cfe2186d635c12d69fdde5234b9c4be MD5 | raw file
  1. using System;
  2. using System.IO;
  3. namespace Tamir.SharpSsh.jsch
  4. {
  5. /* -*-mode:java; c-basic-offset:2; -*- */
  6. /*
  7. Copyright (c) 2002,2003,2004 ymnk, JCraft,Inc. All rights reserved.
  8. Redistribution and use in source and binary forms, with or without
  9. modification, are permitted provided that the following conditions are met:
  10. 1. Redistributions of source code must retain the above copyright notice,
  11. this list of conditions and the following disclaimer.
  12. 2. Redistributions in binary form must reproduce the above copyright
  13. notice, this list of conditions and the following disclaimer in
  14. the documentation and/or other materials provided with the distribution.
  15. 3. The names of the authors may not be used to endorse or promote products
  16. derived from this software without specific prior written permission.
  17. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  18. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  19. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
  20. INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  22. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  23. OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  24. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  25. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  26. EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. internal class IdentityFile : Identity
  29. {
  30. String identity;
  31. byte[] key;
  32. byte[] iv;
  33. private JSch jsch;
  34. private HASH hash;
  35. private byte[] encoded_data;
  36. private Cipher cipher;
  37. // DSA
  38. private byte[] P_array;
  39. private byte[] Q_array;
  40. private byte[] G_array;
  41. private byte[] pub_array;
  42. private byte[] prv_array;
  43. // RSA
  44. private byte[] n_array; // modulus
  45. private byte[] e_array; // public exponent
  46. private byte[] d_array; // private exponent
  47. private byte[] p_array;
  48. private byte[] q_array;
  49. private byte[] dmp1_array;
  50. private byte[] dmq1_array;
  51. private byte[] iqmp_array;
  52. // private String algname="ssh-dss";
  53. private String algname="ssh-rsa";
  54. private const int ERROR=0;
  55. private const int RSA=1;
  56. private const int DSS=2;
  57. internal const int UNKNOWN=3;
  58. private const int OPENSSH=0;
  59. private const int FSECURE=1;
  60. private const int PUTTY=2;
  61. private int type=ERROR;
  62. private int keytype=OPENSSH;
  63. private byte[] publickeyblob=null;
  64. private bool encrypted=true;
  65. internal IdentityFile(String identity, JSch jsch)
  66. {
  67. this.identity=identity;
  68. this.jsch=jsch;
  69. try
  70. {
  71. Type c=Type.GetType(jsch.getConfig("3des-cbc"));
  72. cipher=(Cipher)Activator.CreateInstance(c);
  73. key=new byte[cipher.getBlockSize()]; // 24
  74. iv=new byte[cipher.getIVSize()]; // 8
  75. c=Type.GetType(jsch.getConfig("md5"));
  76. hash=(HASH)(Activator.CreateInstance(c));
  77. hash.init();
  78. FileInfo file=new FileInfo(identity);
  79. FileStream fis = File.OpenRead(identity);
  80. byte[] buf=new byte[(int)(file.Length)];
  81. int len=fis.Read(buf, 0, buf.Length);
  82. fis.Close();
  83. int i=0;
  84. while(i<len)
  85. {
  86. if(buf[i]=='B'&& buf[i+1]=='E'&& buf[i+2]=='G'&& buf[i+3]=='I')
  87. {
  88. i+=6;
  89. if(buf[i]=='D'&& buf[i+1]=='S'&& buf[i+2]=='A'){ type=DSS; }
  90. else if(buf[i]=='R'&& buf[i+1]=='S'&& buf[i+2]=='A'){ type=RSA; }
  91. else if(buf[i]=='S'&& buf[i+1]=='S'&& buf[i+2]=='H')
  92. { // FSecure
  93. type=UNKNOWN;
  94. keytype=FSECURE;
  95. }
  96. else
  97. {
  98. //System.out.println("invalid format: "+identity);
  99. throw new JSchException("invaid privatekey: "+identity);
  100. }
  101. i+=3;
  102. continue;
  103. }
  104. if(buf[i]=='C'&& buf[i+1]=='B'&& buf[i+2]=='C'&& buf[i+3]==',')
  105. {
  106. i+=4;
  107. for(int ii=0; ii<iv.Length; ii++)
  108. {
  109. iv[ii]=(byte)(((a2b(buf[i++])<<4)&0xf0)+
  110. (a2b(buf[i++])&0xf));
  111. }
  112. continue;
  113. }
  114. if(buf[i]==0x0d &&
  115. i+1<buf.Length && buf[i+1]==0x0a)
  116. {
  117. i++;
  118. continue;
  119. }
  120. if(buf[i]==0x0a && i+1<buf.Length)
  121. {
  122. if(buf[i+1]==0x0a){ i+=2; break; }
  123. if(buf[i+1]==0x0d &&
  124. i+2<buf.Length && buf[i+2]==0x0a)
  125. {
  126. i+=3; break;
  127. }
  128. bool inheader=false;
  129. for(int j=i+1; j<buf.Length; j++)
  130. {
  131. if(buf[j]==0x0a) break;
  132. //if(buf[j]==0x0d) break;
  133. if(buf[j]==':'){inheader=true; break;}
  134. }
  135. if(!inheader)
  136. {
  137. i++;
  138. encrypted=false; // no passphrase
  139. break;
  140. }
  141. }
  142. i++;
  143. }
  144. if(type==ERROR)
  145. {
  146. throw new JSchException("invaid privatekey: "+identity);
  147. }
  148. int start=i;
  149. while(i<len)
  150. {
  151. if(buf[i]==0x0a)
  152. {
  153. bool xd=(buf[i-1]==0x0d);
  154. Array.Copy(buf, i+1,
  155. buf,
  156. i-(xd ? 1 : 0),
  157. len-i-1-(xd ? 1 : 0)
  158. );
  159. if(xd)len--;
  160. len--;
  161. continue;
  162. }
  163. if(buf[i]=='-'){ break; }
  164. i++;
  165. }
  166. encoded_data=Util.fromBase64(buf, start, i-start);
  167. if(encoded_data.Length>4 && // FSecure
  168. encoded_data[0]==(byte)0x3f &&
  169. encoded_data[1]==(byte)0x6f &&
  170. encoded_data[2]==(byte)0xf9 &&
  171. encoded_data[3]==(byte)0xeb)
  172. {
  173. Buffer _buf=new Buffer(encoded_data);
  174. _buf.getInt(); // 0x3f6ff9be
  175. _buf.getInt();
  176. byte[]_type=_buf.getString();
  177. //System.out.println("type: "+new String(_type));
  178. byte[] _cipher=_buf.getString();
  179. String s_cipher=System.Text.Encoding.Default.GetString(_cipher);
  180. //System.out.println("cipher: "+cipher);
  181. if(s_cipher.Equals("3des-cbc"))
  182. {
  183. _buf.getInt();
  184. byte[] foo=new byte[encoded_data.Length-_buf.getOffSet()];
  185. _buf.getByte(foo);
  186. encoded_data=foo;
  187. encrypted=true;
  188. throw new JSchException("unknown privatekey format: "+identity);
  189. }
  190. else if(s_cipher.Equals("none"))
  191. {
  192. _buf.getInt();
  193. //_buf.getInt();
  194. encrypted=false;
  195. byte[] foo=new byte[encoded_data.Length-_buf.getOffSet()];
  196. _buf.getByte(foo);
  197. encoded_data=foo;
  198. }
  199. }
  200. try
  201. {
  202. file=new FileInfo(identity+".pub");
  203. fis=File.OpenRead(identity+".pub");
  204. buf=new byte[(int)(file.Length)];
  205. len=fis.Read(buf, 0, buf.Length);
  206. fis.Close();
  207. }
  208. catch
  209. {
  210. return;
  211. }
  212. if(buf.Length>4 && // FSecure's public key
  213. buf[0]=='-' && buf[1]=='-' && buf[2]=='-' && buf[3]=='-')
  214. {
  215. i=0;
  216. do{i++;}while(buf.Length>i && buf[i]!=0x0a);
  217. if(buf.Length<=i) return;
  218. while(true)
  219. {
  220. if(buf[i]==0x0a)
  221. {
  222. bool inheader=false;
  223. for(int j=i+1; j<buf.Length; j++)
  224. {
  225. if(buf[j]==0x0a) break;
  226. if(buf[j]==':'){inheader=true; break;}
  227. }
  228. if(!inheader)
  229. {
  230. i++;
  231. break;
  232. }
  233. }
  234. i++;
  235. }
  236. if(buf.Length<=i) return;
  237. start=i;
  238. while(i<len)
  239. {
  240. if(buf[i]==0x0a)
  241. {
  242. Array.Copy(buf, i+1, buf, i, len-i-1);
  243. len--;
  244. continue;
  245. }
  246. if(buf[i]=='-'){ break; }
  247. i++;
  248. }
  249. publickeyblob=Util.fromBase64(buf, start, i-start);
  250. if(type==UNKNOWN)
  251. {
  252. if(publickeyblob[8]=='d')
  253. {
  254. type=DSS;
  255. }
  256. else if(publickeyblob[8]=='r')
  257. {
  258. type=RSA;
  259. }
  260. }
  261. }
  262. else
  263. {
  264. if(buf[0]!='s'|| buf[1]!='s'|| buf[2]!='h'|| buf[3]!='-') return;
  265. i=0;
  266. while(i<len){ if(buf[i]==' ')break; i++;} i++;
  267. if(i>=len) return;
  268. start=i;
  269. while(i<len){ if(buf[i]==' ')break; i++;}
  270. publickeyblob=Util.fromBase64(buf, start, i-start);
  271. }
  272. }
  273. catch(Exception e)
  274. {
  275. Console.WriteLine("Identity: "+e);
  276. if(e is JSchException) throw (JSchException)e;
  277. throw new JSchException(e.ToString());
  278. }
  279. }
  280. public String getAlgName()
  281. {
  282. if(type==RSA) return "ssh-rsa";
  283. return "ssh-dss";
  284. }
  285. public bool setPassphrase(String _passphrase)
  286. {
  287. /*
  288. hash is MD5
  289. h(0) <- hash(passphrase, iv);
  290. h(n) <- hash(h(n-1), passphrase, iv);
  291. key <- (h(0),...,h(n))[0,..,key.Length];
  292. */
  293. try
  294. {
  295. if(encrypted)
  296. {
  297. if(_passphrase==null) return false;
  298. byte[] passphrase= System.Text.Encoding.Default.GetBytes( _passphrase );
  299. int hsize=hash.getBlockSize();
  300. byte[] hn=new byte[key.Length/hsize*hsize+
  301. (key.Length%hsize==0?0:hsize)];
  302. byte[] tmp=null;
  303. if(keytype==OPENSSH)
  304. {
  305. for(int index=0; index+hsize<=hn.Length;)
  306. {
  307. if(tmp!=null){ hash.update(tmp, 0, tmp.Length); }
  308. hash.update(passphrase, 0, passphrase.Length);
  309. hash.update(iv, 0, iv.Length);
  310. tmp=hash.digest();
  311. Array.Copy(tmp, 0, hn, index, tmp.Length);
  312. index+=tmp.Length;
  313. }
  314. Array.Copy(hn, 0, key, 0, key.Length);
  315. }
  316. else if(keytype==FSECURE)
  317. {
  318. for(int index=0; index+hsize<=hn.Length;)
  319. {
  320. if(tmp!=null){ hash.update(tmp, 0, tmp.Length); }
  321. hash.update(passphrase, 0, passphrase.Length);
  322. tmp=hash.digest();
  323. Array.Copy(tmp, 0, hn, index, tmp.Length);
  324. index+=tmp.Length;
  325. }
  326. Array.Copy(hn, 0, key, 0, key.Length);
  327. }
  328. }
  329. if(decrypt())
  330. {
  331. encrypted=false;
  332. return true;
  333. }
  334. P_array=Q_array=G_array=pub_array=prv_array=null;
  335. return false;
  336. }
  337. catch(Exception e)
  338. {
  339. if(e is JSchException) throw (JSchException)e;
  340. throw new JSchException(e.ToString());
  341. }
  342. }
  343. public byte[] getPublicKeyBlob()
  344. {
  345. if(publickeyblob!=null) return publickeyblob;
  346. if(type==RSA) return getPublicKeyBlob_rsa();
  347. return getPublicKeyBlob_dss();
  348. }
  349. byte[] getPublicKeyBlob_rsa()
  350. {
  351. if(e_array==null) return null;
  352. Buffer buf=new Buffer("ssh-rsa".Length+4+
  353. e_array.Length+4+
  354. n_array.Length+4);
  355. buf.putString( System.Text.Encoding.Default.GetBytes( "ssh-rsa" ) );
  356. buf.putString(e_array);
  357. buf.putString(n_array);
  358. return buf.buffer;
  359. }
  360. byte[] getPublicKeyBlob_dss()
  361. {
  362. if(P_array==null) return null;
  363. Buffer buf=new Buffer("ssh-dss".Length+4+
  364. P_array.Length+4+
  365. Q_array.Length+4+
  366. G_array.Length+4+
  367. pub_array.Length+4);
  368. buf.putString(System.Text.Encoding.Default.GetBytes("ssh-dss"));
  369. buf.putString(P_array);
  370. buf.putString(Q_array);
  371. buf.putString(G_array);
  372. buf.putString(pub_array);
  373. return buf.buffer;
  374. }
  375. public byte[] getSignature(Session session, byte[] data)
  376. {
  377. if(type==RSA) return getSignature_rsa(session, data);
  378. return getSignature_dss(session, data);
  379. }
  380. byte[] getSignature_rsa(Session session, byte[] data)
  381. {
  382. try
  383. {
  384. Type t=Type.GetType(jsch.getConfig("signature.rsa"));
  385. SignatureRSA rsa=(SignatureRSA)Activator.CreateInstance(t);
  386. rsa.init();
  387. rsa.setPrvKey(e_array, n_array, d_array, p_array, q_array, dmp1_array, dmq1_array, iqmp_array);
  388. /*
  389. byte[] goo=new byte[4];
  390. goo[0]=(byte)(session.getSessionId().Length>>>24);
  391. goo[1]=(byte)(session.getSessionId().Length>>>16);
  392. goo[2]=(byte)(session.getSessionId().Length>>>8);
  393. goo[3]=(byte)(session.getSessionId().Length);
  394. rsa.update(goo);
  395. rsa.update(session.getSessionId());
  396. */
  397. rsa.update(data);
  398. byte[] sig = rsa.sign();
  399. Buffer buf=new Buffer("ssh-rsa".Length+4+
  400. sig.Length+4);
  401. buf.putString( System.Text.Encoding.Default.GetBytes( "ssh-rsa" ));
  402. buf.putString(sig);
  403. return buf.buffer;
  404. }
  405. catch(Exception e)
  406. {
  407. Console.WriteLine(e);
  408. }
  409. return null;
  410. }
  411. byte[] getSignature_dss(Session session, byte[] data)
  412. {
  413. /*
  414. byte[] foo;
  415. int i;
  416. System.out.print("P ");
  417. foo=P_array;
  418. for(i=0; i<foo.Length; i++){
  419. System.out.print(Integer.toHexString(foo[i]&0xff)+":");
  420. }
  421. System.out.println("");
  422. System.out.print("Q ");
  423. foo=Q_array;
  424. for(i=0; i<foo.Length; i++){
  425. System.out.print(Integer.toHexString(foo[i]&0xff)+":");
  426. }
  427. System.out.println("");
  428. System.out.print("G ");
  429. foo=G_array;
  430. for(i=0; i<foo.Length; i++){
  431. System.out.print(Integer.toHexString(foo[i]&0xff)+":");
  432. }
  433. System.out.println("");
  434. */
  435. try
  436. {
  437. Type t=Type.GetType(jsch.getConfig("signature.dss"));
  438. SignatureDSA dsa=(SignatureDSA)(Activator.CreateInstance(t));
  439. dsa.init();
  440. dsa.setPrvKey(prv_array, P_array, Q_array, G_array);
  441. /*
  442. byte[] goo=new byte[4];
  443. goo[0]=(byte)(session.getSessionId().Length>>>24);
  444. goo[1]=(byte)(session.getSessionId().Length>>>16);
  445. goo[2]=(byte)(session.getSessionId().Length>>>8);
  446. goo[3]=(byte)(session.getSessionId().Length);
  447. dsa.update(goo);
  448. dsa.update(session.getSessionId());
  449. */
  450. dsa.update(data);
  451. byte[] sig = dsa.sign();
  452. Buffer buf=new Buffer("ssh-dss".Length+4+
  453. sig.Length+4);
  454. buf.putString( System.Text.Encoding.Default.GetBytes( "ssh-dss" ) );
  455. buf.putString(sig);
  456. return buf.buffer;
  457. }
  458. catch(Exception e)
  459. {
  460. Console.WriteLine("e "+e);
  461. }
  462. return null;
  463. }
  464. public bool decrypt()
  465. {
  466. if(type==RSA) return decrypt_rsa();
  467. return decrypt_dss();
  468. }
  469. bool decrypt_rsa()
  470. {
  471. // byte[] p_array;
  472. // byte[] q_array;
  473. // byte[] dmp1_array;
  474. // byte[] dmq1_array;
  475. // byte[] iqmp_array;
  476. try
  477. {
  478. byte[] plain;
  479. if(encrypted)
  480. {
  481. if(keytype==OPENSSH)
  482. {
  483. cipher.init(Cipher.DECRYPT_MODE, key, iv);
  484. plain=new byte[encoded_data.Length];
  485. cipher.update(encoded_data, 0, encoded_data.Length, plain, 0);
  486. }
  487. else if(keytype==FSECURE)
  488. {
  489. for(int i=0; i<iv.Length; i++)iv[i]=0;
  490. cipher.init(Cipher.DECRYPT_MODE, key, iv);
  491. plain=new byte[encoded_data.Length];
  492. cipher.update(encoded_data, 0, encoded_data.Length, plain, 0);
  493. }
  494. else
  495. {
  496. return false;
  497. }
  498. }
  499. else
  500. {
  501. if(n_array!=null) return true;
  502. plain=encoded_data;
  503. }
  504. if(keytype==FSECURE)
  505. { // FSecure
  506. Buffer buf=new Buffer(plain);
  507. int foo=buf.getInt();
  508. if(plain.Length!=foo+4)
  509. {
  510. return false;
  511. }
  512. e_array=buf.getMPIntBits();
  513. d_array=buf.getMPIntBits();
  514. n_array=buf.getMPIntBits();
  515. byte[] u_array=buf.getMPIntBits();
  516. p_array=buf.getMPIntBits();
  517. q_array=buf.getMPIntBits();
  518. return true;
  519. }
  520. int index=0;
  521. int Length=0;
  522. if(plain[index]!=0x30)return false;
  523. index++; // SEQUENCE
  524. Length=plain[index++]&0xff;
  525. if((Length&0x80)!=0)
  526. {
  527. int foo=Length&0x7f; Length=0;
  528. while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
  529. }
  530. if(plain[index]!=0x02)return false;
  531. index++; // INTEGER
  532. Length=plain[index++]&0xff;
  533. if((Length&0x80)!=0)
  534. {
  535. int foo=Length&0x7f; Length=0;
  536. while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
  537. }
  538. index+=Length;
  539. //System.out.println("int: len="+Length);
  540. //System.out.print(Integer.toHexString(plain[index-1]&0xff)+":");
  541. //System.out.println("");
  542. index++;
  543. Length=plain[index++]&0xff;
  544. if((Length&0x80)!=0)
  545. {
  546. int foo=Length&0x7f; Length=0;
  547. while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
  548. }
  549. n_array=new byte[Length];
  550. Array.Copy(plain, index, n_array, 0, Length);
  551. index+=Length;
  552. /*
  553. System.out.println("int: N len="+Length);
  554. for(int i=0; i<n_array.Length; i++){
  555. System.out.print(Integer.toHexString(n_array[i]&0xff)+":");
  556. }
  557. System.out.println("");
  558. */
  559. index++;
  560. Length=plain[index++]&0xff;
  561. if((Length&0x80)!=0)
  562. {
  563. int foo=Length&0x7f; Length=0;
  564. while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
  565. }
  566. e_array=new byte[Length];
  567. Array.Copy(plain, index, e_array, 0, Length);
  568. index+=Length;
  569. /*
  570. System.out.println("int: E len="+Length);
  571. for(int i=0; i<e_array.Length; i++){
  572. System.out.print(Integer.toHexString(e_array[i]&0xff)+":");
  573. }
  574. System.out.println("");
  575. */
  576. index++;
  577. Length=plain[index++]&0xff;
  578. if((Length&0x80)!=0)
  579. {
  580. int foo=Length&0x7f; Length=0;
  581. while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
  582. }
  583. d_array=new byte[Length];
  584. Array.Copy(plain, index, d_array, 0, Length);
  585. index+=Length;
  586. /*
  587. System.out.println("int: D len="+Length);
  588. for(int i=0; i<d_array.Length; i++){
  589. System.out.print(Integer.toHexString(d_array[i]&0xff)+":");
  590. }
  591. System.out.println("");
  592. */
  593. index++;
  594. Length=plain[index++]&0xff;
  595. if((Length&0x80)!=0)
  596. {
  597. int foo=Length&0x7f; Length=0;
  598. while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
  599. }
  600. p_array=new byte[Length];
  601. Array.Copy(plain, index, p_array, 0, Length);
  602. index+=Length;
  603. /*
  604. System.out.println("int: P len="+Length);
  605. for(int i=0; i<p_array.Length; i++){
  606. System.out.print(Integer.toHexString(p_array[i]&0xff)+":");
  607. }
  608. System.out.println("");
  609. */
  610. index++;
  611. Length=plain[index++]&0xff;
  612. if((Length&0x80)!=0)
  613. {
  614. int foo=Length&0x7f; Length=0;
  615. while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
  616. }
  617. q_array=new byte[Length];
  618. Array.Copy(plain, index, q_array, 0, Length);
  619. index+=Length;
  620. /*
  621. System.out.println("int: q len="+Length);
  622. for(int i=0; i<q_array.Length; i++){
  623. System.out.print(Integer.toHexString(q_array[i]&0xff)+":");
  624. }
  625. System.out.println("");
  626. */
  627. index++;
  628. Length=plain[index++]&0xff;
  629. if((Length&0x80)!=0)
  630. {
  631. int foo=Length&0x7f; Length=0;
  632. while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
  633. }
  634. dmp1_array=new byte[Length];
  635. Array.Copy(plain, index, dmp1_array, 0, Length);
  636. index+=Length;
  637. /*
  638. System.out.println("int: dmp1 len="+Length);
  639. for(int i=0; i<dmp1_array.Length; i++){
  640. System.out.print(Integer.toHexString(dmp1_array[i]&0xff)+":");
  641. }
  642. System.out.println("");
  643. */
  644. index++;
  645. Length=plain[index++]&0xff;
  646. if((Length&0x80)!=0)
  647. {
  648. int foo=Length&0x7f; Length=0;
  649. while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
  650. }
  651. dmq1_array=new byte[Length];
  652. Array.Copy(plain, index, dmq1_array, 0, Length);
  653. index+=Length;
  654. /*
  655. System.out.println("int: dmq1 len="+Length);
  656. for(int i=0; i<dmq1_array.Length; i++){
  657. System.out.print(Integer.toHexString(dmq1_array[i]&0xff)+":");
  658. }
  659. System.out.println("");
  660. */
  661. index++;
  662. Length=plain[index++]&0xff;
  663. if((Length&0x80)!=0)
  664. {
  665. int foo=Length&0x7f; Length=0;
  666. while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
  667. }
  668. iqmp_array=new byte[Length];
  669. Array.Copy(plain, index, iqmp_array, 0, Length);
  670. index+=Length;
  671. /*
  672. System.out.println("int: iqmp len="+Length);
  673. for(int i=0; i<iqmp_array.Length; i++){
  674. System.out.print(Integer.toHexString(iqmp_array[i]&0xff)+":");
  675. }
  676. System.out.println("");
  677. */
  678. }
  679. catch
  680. {
  681. //System.out.println(e);
  682. return false;
  683. }
  684. return true;
  685. }
  686. bool decrypt_dss()
  687. {
  688. try
  689. {
  690. byte[] plain;
  691. if(encrypted)
  692. {
  693. if(keytype==OPENSSH)
  694. {
  695. cipher.init(Cipher.DECRYPT_MODE, key, iv);
  696. plain=new byte[encoded_data.Length];
  697. cipher.update(encoded_data, 0, encoded_data.Length, plain, 0);
  698. /*
  699. for(int i=0; i<plain.Length; i++){
  700. System.out.print(Integer.toHexString(plain[i]&0xff)+":");
  701. }
  702. System.out.println("");
  703. */
  704. }
  705. else if(keytype==FSECURE)
  706. {
  707. for(int i=0; i<iv.Length; i++)iv[i]=0;
  708. cipher.init(Cipher.DECRYPT_MODE, key, iv);
  709. plain=new byte[encoded_data.Length];
  710. cipher.update(encoded_data, 0, encoded_data.Length, plain, 0);
  711. }
  712. else
  713. {
  714. return false;
  715. }
  716. }
  717. else
  718. {
  719. if(P_array!=null) return true;
  720. plain=encoded_data;
  721. }
  722. if(keytype==FSECURE)
  723. { // FSecure
  724. Buffer buf=new Buffer(plain);
  725. int foo=buf.getInt();
  726. if(plain.Length!=foo+4)
  727. {
  728. return false;
  729. }
  730. P_array=buf.getMPIntBits();
  731. G_array=buf.getMPIntBits();
  732. Q_array=buf.getMPIntBits();
  733. pub_array=buf.getMPIntBits();
  734. prv_array=buf.getMPIntBits();
  735. return true;
  736. }
  737. int index=0;
  738. int Length=0;
  739. if(plain[index]!=0x30)return false;
  740. index++; // SEQUENCE
  741. Length=plain[index++]&0xff;
  742. if((Length&0x80)!=0)
  743. {
  744. int foo=Length&0x7f; Length=0;
  745. while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
  746. }
  747. if(plain[index]!=0x02)return false;
  748. index++; // INTEGER
  749. Length=plain[index++]&0xff;
  750. if((Length&0x80)!=0)
  751. {
  752. int foo=Length&0x7f; Length=0;
  753. while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
  754. }
  755. index+=Length;
  756. index++;
  757. Length=plain[index++]&0xff;
  758. if((Length&0x80)!=0)
  759. {
  760. int foo=Length&0x7f; Length=0;
  761. while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
  762. }
  763. P_array=new byte[Length];
  764. Array.Copy(plain, index, P_array, 0, Length);
  765. index+=Length;
  766. index++;
  767. Length=plain[index++]&0xff;
  768. if((Length&0x80)!=0)
  769. {
  770. int foo=Length&0x7f; Length=0;
  771. while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
  772. }
  773. Q_array=new byte[Length];
  774. Array.Copy(plain, index, Q_array, 0, Length);
  775. index+=Length;
  776. index++;
  777. Length=plain[index++]&0xff;
  778. if((Length&0x80)!=0)
  779. {
  780. int foo=Length&0x7f; Length=0;
  781. while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
  782. }
  783. G_array=new byte[Length];
  784. Array.Copy(plain, index, G_array, 0, Length);
  785. index+=Length;
  786. index++;
  787. Length=plain[index++]&0xff;
  788. if((Length&0x80)!=0)
  789. {
  790. int foo=Length&0x7f; Length=0;
  791. while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
  792. }
  793. pub_array=new byte[Length];
  794. Array.Copy(plain, index, pub_array, 0, Length);
  795. index+=Length;
  796. index++;
  797. Length=plain[index++]&0xff;
  798. if((Length&0x80)!=0)
  799. {
  800. int foo=Length&0x7f; Length=0;
  801. while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
  802. }
  803. prv_array=new byte[Length];
  804. Array.Copy(plain, index, prv_array, 0, Length);
  805. index+=Length;
  806. }
  807. catch
  808. {
  809. //System.out.println(e);
  810. //e.printStackTrace();
  811. return false;
  812. }
  813. return true;
  814. }
  815. public bool isEncrypted()
  816. {
  817. return encrypted;
  818. }
  819. public String getName(){return identity;}
  820. private int writeSEQUENCE(byte[] buf, int index, int len)
  821. {
  822. buf[index++]=0x30;
  823. index=writeLength(buf, index, len);
  824. return index;
  825. }
  826. private int writeINTEGER(byte[] buf, int index, byte[] data)
  827. {
  828. buf[index++]=0x02;
  829. index=writeLength(buf, index, data.Length);
  830. Array.Copy(data, 0, buf, index, data.Length);
  831. index+=data.Length;
  832. return index;
  833. }
  834. private int countLength(int i_len)
  835. {
  836. uint len = (uint)i_len;
  837. int i=1;
  838. if(len<=0x7f) return i;
  839. while(len>0)
  840. {
  841. len>>=8;
  842. i++;
  843. }
  844. return i;
  845. }
  846. private int writeLength(byte[] data, int index, int i_len)
  847. {
  848. int len = (int)i_len;
  849. int i=countLength(len)-1;
  850. if(i==0)
  851. {
  852. data[index++]=(byte)len;
  853. return index;
  854. }
  855. data[index++]=(byte)(0x80|i);
  856. int j=index+i;
  857. while(i>0)
  858. {
  859. data[index+i-1]=(byte)(len&0xff);
  860. len>>=8;
  861. i--;
  862. }
  863. return j;
  864. }
  865. private byte a2b(byte c)
  866. {
  867. if('0'<=c&&c<='9') return (byte)(c-'0');
  868. if('a'<=c&&c<='z') return (byte)(c-'a'+10);
  869. return (byte)(c-'A'+10);
  870. }
  871. private byte b2a(byte c)
  872. {
  873. if(0<=c&&c<=9) return (byte)(c+'0');
  874. return (byte)(c-10+'A');
  875. }
  876. }
  877. }