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

/SharpSSH/jsch/IdentityFile.cs

https://bitbucket.org/bluetoque/bluetoque-pub
C# | 964 lines | 738 code | 71 blank | 155 comment | 253 complexity | c91f0dcf1e7fdce7a7d5d4f0db7bee54 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. using System;
  2. using System.IO;
  3. using SharpSsh.Jsch;
  4. using SharpSsh;
  5. namespace SharpSsh.jsch
  6. {
  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 : IIdentity
  30. {
  31. string m_identity;
  32. byte[] m_key;
  33. byte[] m_iv;
  34. private JSch m_jsch;
  35. private HashAlgorithm m_hash;
  36. private byte[] m_encoded_data;
  37. private Cipher m_cipher;
  38. // DSA
  39. private byte[] m_P;
  40. private byte[] m_Q;
  41. private byte[] m_G_array;
  42. private byte[] m_pub;
  43. private byte[] m_prv;
  44. // RSA
  45. private byte[] m_n; // modulus
  46. private byte[] m_e; // public exponent
  47. private byte[] m_d; // private exponent
  48. private byte[] m_p;
  49. private byte[] m_q;
  50. private byte[] m_dmp1;
  51. private byte[] m_dmq1;
  52. private byte[] m_iqmp;
  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 m_type = ERROR;
  63. private int m_keytype = OPENSSH;
  64. private byte[] m_publickeyblob = null;
  65. private bool m_encrypted = true;
  66. internal IdentityFile(string identity, JSch jsch)
  67. {
  68. this.m_identity = identity;
  69. this.m_jsch = jsch;
  70. try
  71. {
  72. Type c = Type.GetType(jsch.GetConfig("3des-cbc"));
  73. m_cipher = (Cipher)Activator.CreateInstance(c);
  74. m_key = new byte[m_cipher.BlockSize]; // 24
  75. m_iv = new byte[m_cipher.IVSize]; // 8
  76. c = Type.GetType(jsch.GetConfig("md5"));
  77. m_hash = (HashAlgorithm)(Activator.CreateInstance(c));
  78. m_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') { m_type = DSS; }
  91. else if (buf[i] == 'R' && buf[i + 1] == 'S' && buf[i + 2] == 'A') { m_type = RSA; }
  92. else if (buf[i] == 'S' && buf[i + 1] == 'S' && buf[i + 2] == 'H')
  93. { // FSecure
  94. m_type = UNKNOWN;
  95. m_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 < m_iv.Length; ii++)
  109. {
  110. m_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. m_encrypted = false; // no passphrase
  140. break;
  141. }
  142. }
  143. i++;
  144. }
  145. if (m_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. m_encoded_data = Util.FromBase64(buf, start, i - start);
  168. if (m_encoded_data.Length > 4 && // FSecure
  169. m_encoded_data[0] == (byte)0x3f &&
  170. m_encoded_data[1] == (byte)0x6f &&
  171. m_encoded_data[2] == (byte)0xf9 &&
  172. m_encoded_data[3] == (byte)0xeb)
  173. {
  174. ByteBuffer _buf = new ByteBuffer(m_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[m_encoded_data.Length - _buf.Offset];
  186. _buf.GetByte(foo);
  187. m_encoded_data = foo;
  188. m_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. m_encrypted = false;
  196. byte[] foo = new byte[m_encoded_data.Length - _buf.Offset];
  197. _buf.GetByte(foo);
  198. m_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. m_publickeyblob = Util.FromBase64(buf, start, i - start);
  251. if (m_type == UNKNOWN)
  252. {
  253. if (m_publickeyblob[8] == 'd')
  254. {
  255. m_type = DSS;
  256. }
  257. else if (m_publickeyblob[8] == 'r')
  258. {
  259. m_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. m_publickeyblob = Util.FromBase64(buf, start, i - start);
  272. }
  273. }
  274. catch (Exception e)
  275. {
  276. Trace.TraceError("Error:\r\n{0}", e.ToString());
  277. if (e is JSchException)
  278. throw (JSchException)e;
  279. throw new JSchException(e.ToString());
  280. }
  281. }
  282. public string AlgName
  283. {
  284. get
  285. {
  286. if (m_type == RSA)
  287. return "ssh-rsa";
  288. return "ssh-dss";
  289. }
  290. }
  291. public bool SetPassphrase(string _passphrase)
  292. {
  293. /*
  294. hash is MD5
  295. h(0) <- hash(passphrase, iv);
  296. h(n) <- hash(h(n-1), passphrase, iv);
  297. key <- (h(0),...,h(n))[0,..,key.Length];
  298. */
  299. try
  300. {
  301. if (m_encrypted)
  302. {
  303. if (_passphrase == null) return false;
  304. byte[] passphrase = System.Text.Encoding.Default.GetBytes(_passphrase);
  305. int hsize = m_hash.BlockSize;
  306. byte[] hn = new byte[m_key.Length / hsize * hsize +
  307. (m_key.Length % hsize == 0 ? 0 : hsize)];
  308. byte[] tmp = null;
  309. if (m_keytype == OPENSSH)
  310. {
  311. for (int index = 0; index + hsize <= hn.Length; )
  312. {
  313. if (tmp != null) { m_hash.Update(tmp, 0, tmp.Length); }
  314. m_hash.Update(passphrase, 0, passphrase.Length);
  315. m_hash.Update(m_iv, 0, m_iv.Length);
  316. tmp = m_hash.Digest();
  317. Array.Copy(tmp, 0, hn, index, tmp.Length);
  318. index += tmp.Length;
  319. }
  320. Array.Copy(hn, 0, m_key, 0, m_key.Length);
  321. }
  322. else if (m_keytype == FSECURE)
  323. {
  324. for (int index = 0; index + hsize <= hn.Length; )
  325. {
  326. if (tmp != null) { m_hash.Update(tmp, 0, tmp.Length); }
  327. m_hash.Update(passphrase, 0, passphrase.Length);
  328. tmp = m_hash.Digest();
  329. Array.Copy(tmp, 0, hn, index, tmp.Length);
  330. index += tmp.Length;
  331. }
  332. Array.Copy(hn, 0, m_key, 0, m_key.Length);
  333. }
  334. }
  335. if (Decrypt())
  336. {
  337. m_encrypted = false;
  338. return true;
  339. }
  340. m_P = m_Q = m_G_array = m_pub = m_prv = null;
  341. return false;
  342. }
  343. catch (Exception e)
  344. {
  345. if (e is JSchException) throw (JSchException)e;
  346. throw new JSchException(e.ToString());
  347. }
  348. }
  349. public byte[] PublicKeyBlob
  350. {
  351. get
  352. {
  353. if (m_publickeyblob != null)
  354. return m_publickeyblob;
  355. if (m_type == RSA)
  356. return PublicKeyBlob_rsa();
  357. return PublicKeyBlob_dss();
  358. }
  359. }
  360. byte[] PublicKeyBlob_rsa()
  361. {
  362. if (m_e == null) return null;
  363. ByteBuffer buf = new ByteBuffer("ssh-rsa".Length + 4 +
  364. m_e.Length + 4 +
  365. m_n.Length + 4);
  366. buf.PutString(System.Text.Encoding.Default.GetBytes("ssh-rsa"));
  367. buf.PutString(m_e);
  368. buf.PutString(m_n);
  369. return buf.m_buffer;
  370. }
  371. byte[] PublicKeyBlob_dss()
  372. {
  373. if (m_P == null) return null;
  374. ByteBuffer buf = new ByteBuffer("ssh-dss".Length + 4 +
  375. m_P.Length + 4 +
  376. m_Q.Length + 4 +
  377. m_G_array.Length + 4 +
  378. m_pub.Length + 4);
  379. buf.PutString(System.Text.Encoding.Default.GetBytes("ssh-dss"));
  380. buf.PutString(m_P);
  381. buf.PutString(m_Q);
  382. buf.PutString(m_G_array);
  383. buf.PutString(m_pub);
  384. return buf.m_buffer;
  385. }
  386. public byte[] GetSignature(Session session, byte[] data)
  387. {
  388. if (m_type == RSA) return GetSignature_rsa(session, data);
  389. return GetSignature_dss(session, data);
  390. }
  391. byte[] GetSignature_rsa(Session session, byte[] data)
  392. {
  393. try
  394. {
  395. Type t = Type.GetType(m_jsch.GetConfig("signature.rsa"));
  396. ISignatureRSA rsa = (ISignatureRSA)Activator.CreateInstance(t);
  397. rsa.Init();
  398. rsa.SetPrvKey(m_e, m_n, m_d, m_p, m_q, m_dmp1, m_dmq1, m_iqmp);
  399. /*
  400. byte[] goo=new byte[4];
  401. goo[0]=(byte)(session.getSessionId().Length>>>24);
  402. goo[1]=(byte)(session.getSessionId().Length>>>16);
  403. goo[2]=(byte)(session.getSessionId().Length>>>8);
  404. goo[3]=(byte)(session.getSessionId().Length);
  405. rsa.update(goo);
  406. rsa.update(session.getSessionId());
  407. */
  408. rsa.Update(data);
  409. byte[] sig = rsa.Sign();
  410. ByteBuffer buf = new ByteBuffer("ssh-rsa".Length + 4 +
  411. sig.Length + 4);
  412. buf.PutString(System.Text.Encoding.Default.GetBytes("ssh-rsa"));
  413. buf.PutString(sig);
  414. return buf.m_buffer;
  415. }
  416. catch (Exception e)
  417. {
  418. Trace.TraceError("Error:\r\n{0}", e.ToString());
  419. }
  420. return null;
  421. }
  422. byte[] GetSignature_dss(Session session, byte[] data)
  423. {
  424. /*
  425. byte[] foo;
  426. int i;
  427. System.out.print("P ");
  428. foo=P_array;
  429. for(i=0; i<foo.Length; i++){
  430. System.out.print(Integer.toHexString(foo[i]&0xff)+":");
  431. }
  432. System.out.println("");
  433. System.out.print("Q ");
  434. foo=Q_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("G ");
  440. foo=G_array;
  441. for(i=0; i<foo.Length; i++){
  442. System.out.print(Integer.toHexString(foo[i]&0xff)+":");
  443. }
  444. System.out.println("");
  445. */
  446. try
  447. {
  448. Type t = Type.GetType(m_jsch.GetConfig("signature.dss"));
  449. ISignatureDSA dsa = (ISignatureDSA)(Activator.CreateInstance(t));
  450. dsa.Init();
  451. dsa.SetPrvKey(m_prv, m_P, m_Q, m_G_array);
  452. /*
  453. byte[] goo=new byte[4];
  454. goo[0]=(byte)(session.getSessionId().Length>>>24);
  455. goo[1]=(byte)(session.getSessionId().Length>>>16);
  456. goo[2]=(byte)(session.getSessionId().Length>>>8);
  457. goo[3]=(byte)(session.getSessionId().Length);
  458. dsa.update(goo);
  459. dsa.update(session.getSessionId());
  460. */
  461. dsa.Update(data);
  462. byte[] sig = dsa.Sign();
  463. ByteBuffer buf = new ByteBuffer("ssh-dss".Length + 4 +
  464. sig.Length + 4);
  465. buf.PutString(System.Text.Encoding.Default.GetBytes("ssh-dss"));
  466. buf.PutString(sig);
  467. return buf.m_buffer;
  468. }
  469. catch (Exception e)
  470. {
  471. Trace.TraceError("Error:\r\n{0}", e.ToString());
  472. }
  473. return null;
  474. }
  475. public bool Decrypt()
  476. {
  477. if (m_type == RSA) return Decrypt_rsa();
  478. return Decrypt_dss();
  479. }
  480. bool Decrypt_rsa()
  481. {
  482. // byte[] p_array;
  483. // byte[] q_array;
  484. // byte[] dmp1_array;
  485. // byte[] dmq1_array;
  486. // byte[] iqmp_array;
  487. try
  488. {
  489. byte[] plain;
  490. if (m_encrypted)
  491. {
  492. if (m_keytype == OPENSSH)
  493. {
  494. m_cipher.Init(Cipher.DECRYPT_MODE, m_key, m_iv);
  495. plain = new byte[m_encoded_data.Length];
  496. m_cipher.Update(m_encoded_data, 0, m_encoded_data.Length, plain, 0);
  497. }
  498. else if (m_keytype == FSECURE)
  499. {
  500. for (int i = 0; i < m_iv.Length; i++) m_iv[i] = 0;
  501. m_cipher.Init(Cipher.DECRYPT_MODE, m_key, m_iv);
  502. plain = new byte[m_encoded_data.Length];
  503. m_cipher.Update(m_encoded_data, 0, m_encoded_data.Length, plain, 0);
  504. }
  505. else
  506. {
  507. return false;
  508. }
  509. }
  510. else
  511. {
  512. if (m_n != null) return true;
  513. plain = m_encoded_data;
  514. }
  515. if (m_keytype == FSECURE)
  516. { // FSecure
  517. ByteBuffer buf = new ByteBuffer(plain);
  518. int foo = buf.GetInt();
  519. if (plain.Length != foo + 4)
  520. {
  521. return false;
  522. }
  523. m_e = buf.GetMPIntBits();
  524. m_d = buf.GetMPIntBits();
  525. m_n = buf.GetMPIntBits();
  526. byte[] u_array = buf.GetMPIntBits();
  527. m_p = buf.GetMPIntBits();
  528. m_q = buf.GetMPIntBits();
  529. return true;
  530. }
  531. int index = 0;
  532. int Length = 0;
  533. if (plain[index] != 0x30) return false;
  534. index++; // SEQUENCE
  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. if (plain[index] != 0x02) return false;
  542. index++; // INTEGER
  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. index += Length;
  550. //System.out.println("int: len="+Length);
  551. //System.out.print(Integer.toHexString(plain[index-1]&0xff)+":");
  552. //System.out.println("");
  553. index++;
  554. Length = plain[index++] & 0xff;
  555. if ((Length & 0x80) != 0)
  556. {
  557. int foo = Length & 0x7f; Length = 0;
  558. while (foo-- > 0) { Length = (Length << 8) + (plain[index++] & 0xff); }
  559. }
  560. m_n = new byte[Length];
  561. Array.Copy(plain, index, m_n, 0, Length);
  562. index += Length;
  563. /*
  564. System.out.println("int: N len="+Length);
  565. for(int i=0; i<n_array.Length; i++){
  566. System.out.print(Integer.toHexString(n_array[i]&0xff)+":");
  567. }
  568. System.out.println("");
  569. */
  570. index++;
  571. Length = plain[index++] & 0xff;
  572. if ((Length & 0x80) != 0)
  573. {
  574. int foo = Length & 0x7f; Length = 0;
  575. while (foo-- > 0) { Length = (Length << 8) + (plain[index++] & 0xff); }
  576. }
  577. m_e = new byte[Length];
  578. Array.Copy(plain, index, m_e, 0, Length);
  579. index += Length;
  580. /*
  581. System.out.println("int: E len="+Length);
  582. for(int i=0; i<e_array.Length; i++){
  583. System.out.print(Integer.toHexString(e_array[i]&0xff)+":");
  584. }
  585. System.out.println("");
  586. */
  587. index++;
  588. Length = plain[index++] & 0xff;
  589. if ((Length & 0x80) != 0)
  590. {
  591. int foo = Length & 0x7f; Length = 0;
  592. while (foo-- > 0) { Length = (Length << 8) + (plain[index++] & 0xff); }
  593. }
  594. m_d = new byte[Length];
  595. Array.Copy(plain, index, m_d, 0, Length);
  596. index += Length;
  597. /*
  598. System.out.println("int: D len="+Length);
  599. for(int i=0; i<d_array.Length; i++){
  600. System.out.print(Integer.toHexString(d_array[i]&0xff)+":");
  601. }
  602. System.out.println("");
  603. */
  604. index++;
  605. Length = plain[index++] & 0xff;
  606. if ((Length & 0x80) != 0)
  607. {
  608. int foo = Length & 0x7f; Length = 0;
  609. while (foo-- > 0) { Length = (Length << 8) + (plain[index++] & 0xff); }
  610. }
  611. m_p = new byte[Length];
  612. Array.Copy(plain, index, m_p, 0, Length);
  613. index += Length;
  614. /*
  615. System.out.println("int: P len="+Length);
  616. for(int i=0; i<p_array.Length; i++){
  617. System.out.print(Integer.toHexString(p_array[i]&0xff)+":");
  618. }
  619. System.out.println("");
  620. */
  621. index++;
  622. Length = plain[index++] & 0xff;
  623. if ((Length & 0x80) != 0)
  624. {
  625. int foo = Length & 0x7f; Length = 0;
  626. while (foo-- > 0) { Length = (Length << 8) + (plain[index++] & 0xff); }
  627. }
  628. m_q = new byte[Length];
  629. Array.Copy(plain, index, m_q, 0, Length);
  630. index += Length;
  631. /*
  632. System.out.println("int: q len="+Length);
  633. for(int i=0; i<q_array.Length; i++){
  634. System.out.print(Integer.toHexString(q_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; Length = 0;
  643. while (foo-- > 0) { Length = (Length << 8) + (plain[index++] & 0xff); }
  644. }
  645. m_dmp1 = new byte[Length];
  646. Array.Copy(plain, index, m_dmp1, 0, Length);
  647. index += Length;
  648. /*
  649. System.out.println("int: dmp1 len="+Length);
  650. for(int i=0; i<dmp1_array.Length; i++){
  651. System.out.print(Integer.toHexString(dmp1_array[i]&0xff)+":");
  652. }
  653. System.out.println("");
  654. */
  655. index++;
  656. Length = plain[index++] & 0xff;
  657. if ((Length & 0x80) != 0)
  658. {
  659. int foo = Length & 0x7f; Length = 0;
  660. while (foo-- > 0) { Length = (Length << 8) + (plain[index++] & 0xff); }
  661. }
  662. m_dmq1 = new byte[Length];
  663. Array.Copy(plain, index, m_dmq1, 0, Length);
  664. index += Length;
  665. /*
  666. System.out.println("int: dmq1 len="+Length);
  667. for(int i=0; i<dmq1_array.Length; i++){
  668. System.out.print(Integer.toHexString(dmq1_array[i]&0xff)+":");
  669. }
  670. System.out.println("");
  671. */
  672. index++;
  673. Length = plain[index++] & 0xff;
  674. if ((Length & 0x80) != 0)
  675. {
  676. int foo = Length & 0x7f; Length = 0;
  677. while (foo-- > 0) { Length = (Length << 8) + (plain[index++] & 0xff); }
  678. }
  679. m_iqmp = new byte[Length];
  680. Array.Copy(plain, index, m_iqmp, 0, Length);
  681. index += Length;
  682. /*
  683. System.out.println("int: iqmp len="+Length);
  684. for(int i=0; i<iqmp_array.Length; i++){
  685. System.out.print(Integer.toHexString(iqmp_array[i]&0xff)+":");
  686. }
  687. System.out.println("");
  688. */
  689. }
  690. catch
  691. {
  692. //System.out.println(e);
  693. return false;
  694. }
  695. return true;
  696. }
  697. bool Decrypt_dss()
  698. {
  699. try
  700. {
  701. byte[] plain;
  702. if (m_encrypted)
  703. {
  704. if (m_keytype == OPENSSH)
  705. {
  706. m_cipher.Init(Cipher.DECRYPT_MODE, m_key, m_iv);
  707. plain = new byte[m_encoded_data.Length];
  708. m_cipher.Update(m_encoded_data, 0, m_encoded_data.Length, plain, 0);
  709. /*
  710. for(int i=0; i<plain.Length; i++){
  711. System.out.print(Integer.toHexString(plain[i]&0xff)+":");
  712. }
  713. System.out.println("");
  714. */
  715. }
  716. else if (m_keytype == FSECURE)
  717. {
  718. for (int i = 0; i < m_iv.Length; i++) m_iv[i] = 0;
  719. m_cipher.Init(Cipher.DECRYPT_MODE, m_key, m_iv);
  720. plain = new byte[m_encoded_data.Length];
  721. m_cipher.Update(m_encoded_data, 0, m_encoded_data.Length, plain, 0);
  722. }
  723. else
  724. {
  725. return false;
  726. }
  727. }
  728. else
  729. {
  730. if (m_P != null) return true;
  731. plain = m_encoded_data;
  732. }
  733. if (m_keytype == FSECURE)
  734. { // FSecure
  735. ByteBuffer buf = new ByteBuffer(plain);
  736. int foo = buf.GetInt();
  737. if (plain.Length != foo + 4)
  738. {
  739. return false;
  740. }
  741. m_P = buf.GetMPIntBits();
  742. m_G_array = buf.GetMPIntBits();
  743. m_Q = buf.GetMPIntBits();
  744. m_pub = buf.GetMPIntBits();
  745. m_prv = buf.GetMPIntBits();
  746. return true;
  747. }
  748. int index = 0;
  749. int Length = 0;
  750. if (plain[index] != 0x30) return false;
  751. index++; // SEQUENCE
  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. if (plain[index] != 0x02) return false;
  759. index++; // INTEGER
  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. index += Length;
  767. index++;
  768. Length = plain[index++] & 0xff;
  769. if ((Length & 0x80) != 0)
  770. {
  771. int foo = Length & 0x7f; Length = 0;
  772. while (foo-- > 0) { Length = (Length << 8) + (plain[index++] & 0xff); }
  773. }
  774. m_P = new byte[Length];
  775. Array.Copy(plain, index, m_P, 0, Length);
  776. index += Length;
  777. index++;
  778. Length = plain[index++] & 0xff;
  779. if ((Length & 0x80) != 0)
  780. {
  781. int foo = Length & 0x7f; Length = 0;
  782. while (foo-- > 0) { Length = (Length << 8) + (plain[index++] & 0xff); }
  783. }
  784. m_Q = new byte[Length];
  785. Array.Copy(plain, index, m_Q, 0, Length);
  786. index += Length;
  787. index++;
  788. Length = plain[index++] & 0xff;
  789. if ((Length & 0x80) != 0)
  790. {
  791. int foo = Length & 0x7f; Length = 0;
  792. while (foo-- > 0) { Length = (Length << 8) + (plain[index++] & 0xff); }
  793. }
  794. m_G_array = new byte[Length];
  795. Array.Copy(plain, index, m_G_array, 0, Length);
  796. index += Length;
  797. index++;
  798. Length = plain[index++] & 0xff;
  799. if ((Length & 0x80) != 0)
  800. {
  801. int foo = Length & 0x7f; Length = 0;
  802. while (foo-- > 0) { Length = (Length << 8) + (plain[index++] & 0xff); }
  803. }
  804. m_pub = new byte[Length];
  805. Array.Copy(plain, index, m_pub, 0, Length);
  806. index += Length;
  807. index++;
  808. Length = plain[index++] & 0xff;
  809. if ((Length & 0x80) != 0)
  810. {
  811. int foo = Length & 0x7f; Length = 0;
  812. while (foo-- > 0) { Length = (Length << 8) + (plain[index++] & 0xff); }
  813. }
  814. m_prv = new byte[Length];
  815. Array.Copy(plain, index, m_prv, 0, Length);
  816. index += Length;
  817. }
  818. catch
  819. {
  820. //System.out.println(e);
  821. //e.printStackTrace();
  822. return false;
  823. }
  824. return true;
  825. }
  826. public bool isEncrypted { get { return m_encrypted; } }
  827. public string Name { get { return m_identity; } }
  828. private int WriteSEQUENCE(byte[] buf, int index, int len)
  829. {
  830. buf[index++] = 0x30;
  831. index = WriteLength(buf, index, len);
  832. return index;
  833. }
  834. private int WriteINTEGER(byte[] buf, int index, byte[] data)
  835. {
  836. buf[index++] = 0x02;
  837. index = WriteLength(buf, index, data.Length);
  838. Array.Copy(data, 0, buf, index, data.Length);
  839. index += data.Length;
  840. return index;
  841. }
  842. private int CountLength(int i_len)
  843. {
  844. uint len = (uint)i_len;
  845. int i = 1;
  846. if (len <= 0x7f) return i;
  847. while (len > 0)
  848. {
  849. len >>= 8;
  850. i++;
  851. }
  852. return i;
  853. }
  854. private int WriteLength(byte[] data, int index, int i_len)
  855. {
  856. int len = (int)i_len;
  857. int i = CountLength(len) - 1;
  858. if (i == 0)
  859. {
  860. data[index++] = (byte)len;
  861. return index;
  862. }
  863. data[index++] = (byte)(0x80 | i);
  864. int j = index + i;
  865. while (i > 0)
  866. {
  867. data[index + i - 1] = (byte)(len & 0xff);
  868. len >>= 8;
  869. i--;
  870. }
  871. return j;
  872. }
  873. private byte a2b(byte c)
  874. {
  875. if ('0' <= c && c <= '9')
  876. return (byte)(c - '0');
  877. if ('a' <= c && c <= 'z')
  878. return (byte)(c - 'a' + 10);
  879. return (byte)(c - 'A' + 10);
  880. }
  881. private byte b2a(byte c)
  882. {
  883. if (0 <= c && c <= 9)
  884. return (byte)(c + '0');
  885. return (byte)(c - 10 + 'A');
  886. }
  887. }
  888. }