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

/sshxcute/src/com/jcraft/jsch/IdentityFile.java

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