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

/SharpSSH/jsch/IdentityFile.cs

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