PageRenderTime 56ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/SharpSSH3/SharpSSH/jsch/IdentityFile.cs

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