PageRenderTime 52ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/FireballFX/Fireball.Ssh/Fireball.Ssh/jsch/IdentityFile.cs

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