IdentityFile.java

Go to the documentation of this file.
00001 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
00002 /*
00003 Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
00004 
00005 Redistribution and use in source and binary forms, with or without
00006 modification, are permitted provided that the following conditions are met:
00007 
00008   1. Redistributions of source code must retain the above copyright notice,
00009      this list of conditions and the following disclaimer.
00010 
00011   2. Redistributions in binary form must reproduce the above copyright 
00012      notice, this list of conditions and the following disclaimer in 
00013      the documentation and/or other materials provided with the distribution.
00014 
00015   3. The names of the authors may not be used to endorse or promote products
00016      derived from this software without specific prior written permission.
00017 
00018 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
00019 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
00020 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
00021 INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
00022 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00023 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00024 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00025 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00026 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
00027 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028 */
00029 
00030 package com.jcraft.jsch;
00031 
00032 import java.io.*;
00033 
00034 
00039 class IdentityFile implements Identity{
00040   String identity;
00041   byte[] key;
00042   byte[] iv;
00043   private JSch jsch;
00044   private HASH hash;
00045   private byte[] encoded_data;
00046 
00047   private Cipher cipher;
00048 
00049   // DSA
00050   private byte[] P_array;    
00051   private byte[] Q_array;    
00052   private byte[] G_array;    
00053   private byte[] pub_array;    
00054   private byte[] prv_array;    
00055  
00056   // RSA
00057   private  byte[] n_array;   // modulus
00058   private  byte[] e_array;   // public exponent
00059   private  byte[] d_array;   // private exponent
00060  
00061 //  private String algname="ssh-dss";
00062   private String algname="ssh-rsa";
00063 
00064   private static final int ERROR=0;
00065   private static final int RSA=1;
00066   private static final int DSS=2;
00067   private static final int UNKNOWN=3;
00068 
00069   private static final int OPENSSH=0;
00070   private static final int FSECURE=1;
00071   private static final int PUTTY=2;
00072 
00073   private int type=ERROR;
00074   private int keytype=OPENSSH;
00075 
00076   private byte[] publickeyblob=null;
00077 
00078   private boolean encrypted=true;
00079 
00080 
00085   static IdentityFile newInstance(String prvfile, String pubfile, JSch jsch) throws JSchException{
00086     byte[] prvkey=null;
00087     byte[] pubkey=null;
00088 
00089     File file=null;
00090     FileInputStream fis=null;
00091     try{
00092       file=new File(prvfile);
00093       fis=new FileInputStream(prvfile);
00094       prvkey=new byte[(int)(file.length())];
00095       int len=0;
00096       while(true){
00097         int i=fis.read(prvkey, len, prvkey.length-len);
00098         if(i<=0)
00099           break;
00100         len+=i;
00101       }
00102       fis.close();
00103     }
00104     catch(Exception e){
00105       try{ if(fis!=null) fis.close();}
00106       catch(Exception ee){}
00107       if(e instanceof Throwable)
00108         throw new JSchException(e.toString(), (Throwable)e);
00109       throw new JSchException(e.toString());
00110     }
00111 
00112     String _pubfile=pubfile;
00113     if(pubfile==null){
00114       _pubfile=prvfile+".pub";
00115     }
00116 
00117     try{
00118       file=new File(_pubfile);
00119       fis = new FileInputStream(_pubfile);
00120       pubkey=new byte[(int)(file.length())];
00121       int len=0;
00122       while(true){
00123         int i=fis.read(pubkey, len, pubkey.length-len);
00124         if(i<=0)
00125           break;
00126         len+=i;
00127       }
00128       fis.close();
00129     }
00130     catch(Exception e){
00131       try{ if(fis!=null) fis.close();}
00132       catch(Exception ee){}
00133       if(pubfile!=null){  
00134         // The pubfile is explicitry given, but not accessible.
00135         if(e instanceof Throwable)
00136           throw new JSchException(e.toString(), (Throwable)e);
00137         throw new JSchException(e.toString());
00138       }
00139     }
00140     return newInstance(prvfile, prvkey, pubkey, jsch);
00141   }
00142 
00148   static IdentityFile newInstance(String name, byte[] prvkey, byte[] pubkey, JSch jsch) throws JSchException{
00149     try{
00150       return new IdentityFile(name, prvkey, pubkey, jsch);
00151     }
00152     finally{
00153       Util.bzero(prvkey);
00154     }
00155   }
00156 
00157   private IdentityFile(String name, byte[] prvkey, byte[] pubkey, JSch jsch) throws JSchException{
00158     this.identity=name;
00159     this.jsch=jsch;
00160 
00161     /* TODO: IdentityFile should use KeyPair.
00162      * The following logic exists also in KeyPair. It is redundant.
00163      */
00164     try{
00165       Class c;
00166       c=Class.forName((String)jsch.getConfig("3des-cbc"));
00167       cipher=(Cipher)(c.newInstance());
00168       key=new byte[cipher.getBlockSize()];   // 24
00169       iv=new byte[cipher.getIVSize()];       // 8
00170       c=Class.forName((String)jsch.getConfig("md5"));
00171       hash=(HASH)(c.newInstance());
00172       hash.init();
00173 
00174       byte[] buf=prvkey;
00175       int len=buf.length;
00176 
00177       int i=0;
00178 
00179       while(i<len){
00180         if(buf[i] == '-' && i+4<len && 
00181            buf[i+1] == '-' && buf[i+2] == '-' && 
00182            buf[i+3] == '-' && buf[i+4] == '-'){
00183           break;
00184         }
00185         i++;
00186       }
00187 
00188       while(i<len){
00189         if(buf[i]=='B'&& i+3<len && buf[i+1]=='E'&& buf[i+2]=='G'&& buf[i+3]=='I'){
00190           i+=6;     
00191           if(buf[i]=='D'&& buf[i+1]=='S'&& buf[i+2]=='A'){ type=DSS; }
00192       else if(buf[i]=='R'&& buf[i+1]=='S'&& buf[i+2]=='A'){ type=RSA; }
00193       else if(buf[i]=='S'&& buf[i+1]=='S'&& buf[i+2]=='H'){ // FSecure
00194         type=UNKNOWN;
00195         keytype=FSECURE;
00196       }
00197       else{
00198             //System.err.println("invalid format: "+identity);
00199         throw new JSchException("invalid privatekey: "+identity);
00200       }
00201           i+=3;
00202       continue;
00203     }
00204         if(buf[i]=='A'&& i+7<len && buf[i+1]=='E'&& buf[i+2]=='S'&& buf[i+3]=='-' && 
00205            buf[i+4]=='2'&& buf[i+5]=='5'&& buf[i+6]=='6'&& buf[i+7]=='-'){
00206           i+=8;
00207           if(Session.checkCipher((String)jsch.getConfig("aes256-cbc"))){
00208             c=Class.forName((String)jsch.getConfig("aes256-cbc"));
00209             cipher=(Cipher)(c.newInstance());
00210             key=new byte[cipher.getBlockSize()];
00211             iv=new byte[cipher.getIVSize()];
00212           }
00213           else{
00214             throw new JSchException("privatekey: aes256-cbc is not available "+identity);
00215           }
00216           continue;
00217         }
00218         if(buf[i]=='A'&& i+7<len && buf[i+1]=='E'&& buf[i+2]=='S'&& buf[i+3]=='-' && 
00219            buf[i+4]=='1'&& buf[i+5]=='9'&& buf[i+6]=='2'&& buf[i+7]=='-'){
00220           i+=8;
00221           if(Session.checkCipher((String)jsch.getConfig("aes192-cbc"))){
00222             c=Class.forName((String)jsch.getConfig("aes192-cbc"));
00223             cipher=(Cipher)(c.newInstance());
00224             key=new byte[cipher.getBlockSize()];
00225             iv=new byte[cipher.getIVSize()];
00226           }
00227           else{
00228             throw new JSchException("privatekey: aes192-cbc is not available "+identity);
00229           }
00230           continue;
00231         }
00232         if(buf[i]=='A'&& i+7<len && buf[i+1]=='E'&& buf[i+2]=='S'&& buf[i+3]=='-' && 
00233            buf[i+4]=='1'&& buf[i+5]=='2'&& buf[i+6]=='8'&& buf[i+7]=='-'){
00234           i+=8;
00235           if(Session.checkCipher((String)jsch.getConfig("aes128-cbc"))){
00236             c=Class.forName((String)jsch.getConfig("aes128-cbc"));
00237             cipher=(Cipher)(c.newInstance());
00238             key=new byte[cipher.getBlockSize()];
00239             iv=new byte[cipher.getIVSize()];
00240           }
00241           else{
00242             throw new JSchException("privatekey: aes128-cbc is not available "+identity);
00243           }
00244           continue;
00245         }
00246         if(buf[i]=='C'&& i+3<len && buf[i+1]=='B'&& buf[i+2]=='C'&& buf[i+3]==','){
00247           i+=4;
00248       for(int ii=0; ii<iv.length; ii++){
00249             iv[ii]=(byte)(((a2b(buf[i++])<<4)&0xf0)+
00250               (a2b(buf[i++])&0xf));
00251       }
00252       continue;
00253     }
00254     if(buf[i]==0x0d && i+1<len && buf[i+1]==0x0a){
00255       i++;
00256       continue;
00257     }
00258     if(buf[i]==0x0a && i+1<len){
00259       if(buf[i+1]==0x0a){ i+=2; break; }
00260       if(buf[i+1]==0x0d &&
00261          i+2<len && buf[i+2]==0x0a){
00262          i+=3; break;
00263       }
00264       boolean inheader=false;
00265       for(int j=i+1; j<len; j++){
00266         if(buf[j]==0x0a) break;
00267         //if(buf[j]==0x0d) break;
00268         if(buf[j]==':'){inheader=true; break;}
00269       }
00270       if(!inheader){
00271         i++; 
00272         encrypted=false;    // no passphrase
00273         break;
00274       }
00275     }
00276     i++;
00277       }
00278 
00279       if(type==ERROR){
00280     throw new JSchException("invalid privatekey: "+identity);
00281       }
00282 
00283       int start=i;
00284       while(i<len){
00285         if(buf[i]==0x0a){
00286       boolean xd=(buf[i-1]==0x0d);
00287           System.arraycopy(buf, i+1, 
00288                buf, 
00289                i-(xd ? 1 : 0), 
00290                len-i-1-(xd ? 1 : 0)
00291                );
00292       if(xd)len--;
00293           len--;
00294           continue;
00295         }
00296         if(buf[i]=='-'){  break; }
00297         i++;
00298       }
00299       encoded_data=Util.fromBase64(buf, start, i-start);
00300 
00301       if(encoded_data.length>4 &&            // FSecure
00302      encoded_data[0]==(byte)0x3f &&
00303      encoded_data[1]==(byte)0x6f &&
00304      encoded_data[2]==(byte)0xf9 &&
00305      encoded_data[3]==(byte)0xeb){
00306 
00307     Buffer _buf=new Buffer(encoded_data);
00308     _buf.getInt();  // 0x3f6ff9be
00309     _buf.getInt();
00310     byte[]_type=_buf.getString();
00311     //System.err.println("type: "+new String(_type)); 
00312     byte[] _cipher=_buf.getString();
00313     String cipher=Util.byte2str(_cipher);
00314     //System.err.println("cipher: "+cipher); 
00315     if(cipher.equals("3des-cbc")){
00316        _buf.getInt();
00317        byte[] foo=new byte[encoded_data.length-_buf.getOffSet()];
00318        _buf.getByte(foo);
00319        encoded_data=foo;
00320        encrypted=true;
00321        throw new JSchException("unknown privatekey format: "+identity);
00322     }
00323     else if(cipher.equals("none")){
00324        _buf.getInt();
00325        //_buf.getInt();
00326 
00327            encrypted=false;
00328 
00329        byte[] foo=new byte[encoded_data.length-_buf.getOffSet()];
00330        _buf.getByte(foo);
00331        encoded_data=foo;
00332     }
00333 
00334       }
00335 
00336       if(pubkey==null){
00337         return;
00338       }
00339       
00340       buf=pubkey;
00341       len=buf.length;
00342 
00343       if(buf.length>4 &&             // FSecure's public key
00344      buf[0]=='-' && buf[1]=='-' && buf[2]=='-' && buf[3]=='-'){
00345     i=0;
00346     do{i++;}while(len>i && buf[i]!=0x0a);
00347     if(len<=i) return;
00348     while(i<len){
00349       if(buf[i]==0x0a){
00350         boolean inheader=false;
00351         for(int j=i+1; j<len; j++){
00352           if(buf[j]==0x0a) break;
00353           if(buf[j]==':'){inheader=true; break;}
00354         }
00355         if(!inheader){
00356           i++; 
00357           break;
00358         }
00359       }
00360       i++;
00361     }
00362     if(len<=i) return;
00363 
00364     start=i;
00365     while(i<len){
00366       if(buf[i]==0x0a){
00367         System.arraycopy(buf, i+1, buf, i, len-i-1);
00368         len--;
00369         continue;
00370       }
00371       if(buf[i]=='-'){  break; }
00372       i++;
00373     }
00374     publickeyblob=Util.fromBase64(buf, start, i-start);
00375 
00376     if(type==UNKNOWN && publickeyblob.length>8){
00377       if(publickeyblob[8]=='d'){
00378         type=DSS;
00379       }
00380       else if(publickeyblob[8]=='r'){
00381         type=RSA;
00382       }
00383     }
00384       }
00385       else{
00386     if(buf[0]!='s'|| buf[1]!='s'|| buf[2]!='h'|| buf[3]!='-') return;
00387     i=0;
00388     while(i<len){ if(buf[i]==' ')break; i++;} i++;
00389     if(i>=len) return;
00390     start=i;
00391     while(i<len){ if(buf[i]==' ' || buf[i]=='\n')break; i++;}
00392     publickeyblob=Util.fromBase64(buf, start, i-start);
00393         if(publickeyblob.length<4+7){  // It must start with "ssh-XXX".
00394           if(JSch.getLogger().isEnabled(Logger.WARN)){
00395             JSch.getLogger().log(Logger.WARN, 
00396                                  "failed to parse the public key");
00397           }
00398           publickeyblob=null;
00399         }
00400       }
00401     }
00402     catch(Exception e){
00403       //System.err.println("IdentityFile: "+e);
00404       if(e instanceof JSchException) throw (JSchException)e;
00405       if(e instanceof Throwable)
00406         throw new JSchException(e.toString(), (Throwable)e);
00407       throw new JSchException(e.toString());
00408     }
00409   }
00410 
00411   public String getAlgName(){
00412     if(type==RSA) return "ssh-rsa";
00413     return "ssh-dss"; 
00414   }
00415 
00416   public boolean setPassphrase(byte[] _passphrase) throws JSchException{
00417     /*
00418       hash is MD5
00419       h(0) <- hash(passphrase, iv);
00420       h(n) <- hash(h(n-1), passphrase, iv);
00421       key <- (h(0),...,h(n))[0,..,key.length];
00422     */
00423     try{
00424       if(encrypted){
00425     if(_passphrase==null) return false;
00426     byte[] passphrase=_passphrase;
00427     int hsize=hash.getBlockSize();
00428     byte[] hn=new byte[key.length/hsize*hsize+
00429                (key.length%hsize==0?0:hsize)];
00430     byte[] tmp=null;
00431     if(keytype==OPENSSH){
00432       for(int index=0; index+hsize<=hn.length;){
00433         if(tmp!=null){ hash.update(tmp, 0, tmp.length); }
00434         hash.update(passphrase, 0, passphrase.length);
00435         hash.update(iv, 0, iv.length > 8 ? 8: iv.length);
00436         tmp=hash.digest();
00437         System.arraycopy(tmp, 0, hn, index, tmp.length);
00438         index+=tmp.length;
00439       }
00440       System.arraycopy(hn, 0, key, 0, key.length); 
00441     }
00442     else if(keytype==FSECURE){
00443       for(int index=0; index+hsize<=hn.length;){
00444         if(tmp!=null){ hash.update(tmp, 0, tmp.length); }
00445         hash.update(passphrase, 0, passphrase.length);
00446         tmp=hash.digest();
00447         System.arraycopy(tmp, 0, hn, index, tmp.length);
00448         index+=tmp.length;
00449       }
00450       System.arraycopy(hn, 0, key, 0, key.length); 
00451     }
00452         Util.bzero(passphrase);
00453       }
00454       if(decrypt()){
00455     encrypted=false;
00456     return true;
00457       }
00458       P_array=Q_array=G_array=pub_array=prv_array=null;
00459       return false;
00460     }
00461     catch(Exception e){
00462       if(e instanceof JSchException) throw (JSchException)e;
00463       if(e instanceof Throwable)
00464         throw new JSchException(e.toString(), (Throwable)e);
00465       throw new JSchException(e.toString());
00466     }
00467   }
00468 
00469   public byte[] getPublicKeyBlob(){
00470     if(publickeyblob!=null) return publickeyblob;
00471     if(type==RSA) return getPublicKeyBlob_rsa();
00472     return getPublicKeyBlob_dss();
00473   }
00474 
00475   byte[] getPublicKeyBlob_rsa(){
00476     if(e_array==null) return null;
00477     Buffer buf=new Buffer("ssh-rsa".length()+4+
00478                e_array.length+4+ 
00479                n_array.length+4);
00480     buf.putString(Util.str2byte("ssh-rsa"));
00481     buf.putString(e_array);
00482     buf.putString(n_array);
00483     return buf.buffer;
00484   }
00485 
00486   byte[] getPublicKeyBlob_dss(){
00487     if(P_array==null) return null;
00488     Buffer buf=new Buffer("ssh-dss".length()+4+
00489                P_array.length+4+ 
00490                Q_array.length+4+ 
00491                G_array.length+4+ 
00492                pub_array.length+4);
00493     buf.putString(Util.str2byte("ssh-dss"));
00494     buf.putString(P_array);
00495     buf.putString(Q_array);
00496     buf.putString(G_array);
00497     buf.putString(pub_array);
00498     return buf.buffer;
00499   }
00500 
00501   public byte[] getSignature(byte[] data){
00502     if(type==RSA) return getSignature_rsa(data);
00503     return getSignature_dss(data);
00504   }
00505 
00506   byte[] getSignature_rsa(byte[] data){
00507     try{      
00508       Class c=Class.forName((String)jsch.getConfig("signature.rsa"));
00509       SignatureRSA rsa=(SignatureRSA)(c.newInstance());
00510 
00511       rsa.init();
00512       rsa.setPrvKey(d_array, n_array);
00513 
00514       rsa.update(data);
00515       byte[] sig = rsa.sign();
00516       Buffer buf=new Buffer("ssh-rsa".length()+4+
00517                 sig.length+4);
00518       buf.putString(Util.str2byte("ssh-rsa"));
00519       buf.putString(sig);
00520       return buf.buffer;
00521     }
00522     catch(Exception e){
00523       JSch.getLogger().log(Logger.WARN, ""+e);
00524       e.printStackTrace();
00525     }
00526     return null;
00527   }
00528 
00529   byte[] getSignature_dss(byte[] data){
00530 /*
00531     byte[] foo;
00532     int i;
00533     System.err.print("P ");
00534     foo=P_array;
00535     for(i=0;  i<foo.length; i++){
00536       System.err.print(Integer.toHexString(foo[i]&0xff)+":");
00537     }
00538     System.err.println("");
00539     System.err.print("Q ");
00540     foo=Q_array;
00541     for(i=0;  i<foo.length; i++){
00542       System.err.print(Integer.toHexString(foo[i]&0xff)+":");
00543     }
00544     System.err.println("");
00545     System.err.print("G ");
00546     foo=G_array;
00547     for(i=0;  i<foo.length; i++){
00548       System.err.print(Integer.toHexString(foo[i]&0xff)+":");
00549     }
00550     System.err.println("");
00551 */
00552 
00553     try{      
00554       Class c=Class.forName((String)jsch.getConfig("signature.dss"));
00555       SignatureDSA dsa=(SignatureDSA)(c.newInstance());
00556       dsa.init();
00557       dsa.setPrvKey(prv_array, P_array, Q_array, G_array);
00558 
00559       dsa.update(data);
00560       byte[] sig = dsa.sign();
00561       Buffer buf=new Buffer("ssh-dss".length()+4+
00562                 sig.length+4);
00563       buf.putString(Util.str2byte("ssh-dss"));
00564       buf.putString(sig);
00565       return buf.buffer;
00566     }
00567     catch(Exception e){
00568       //System.err.println("e "+e);
00569     }
00570     return null;
00571   }
00572 
00573   public boolean decrypt(){
00574     if(type==RSA) return decrypt_rsa();
00575     return decrypt_dss();
00576   }
00577 
00578   boolean decrypt_rsa(){
00579     byte[] p_array;
00580     byte[] q_array;
00581     byte[] dmp1_array;
00582     byte[] dmq1_array;
00583     byte[] iqmp_array;
00584 
00585     try{
00586       byte[] plain;
00587       if(encrypted){
00588     if(keytype==OPENSSH){
00589       cipher.init(Cipher.DECRYPT_MODE, key, iv);
00590       plain=new byte[encoded_data.length];
00591       cipher.update(encoded_data, 0, encoded_data.length, plain, 0);
00592     }
00593     else if(keytype==FSECURE){
00594       for(int i=0; i<iv.length; i++)iv[i]=0;
00595       cipher.init(Cipher.DECRYPT_MODE, key, iv);
00596       plain=new byte[encoded_data.length];
00597       cipher.update(encoded_data, 0, encoded_data.length, plain, 0);
00598     }
00599     else{
00600       return false;
00601     }
00602       }
00603       else{
00604     if(n_array!=null) return true;
00605     plain=encoded_data;
00606       }
00607 
00608       if(keytype==FSECURE){              // FSecure   
00609     Buffer buf=new Buffer(plain);
00610         int foo=buf.getInt();
00611         if(plain.length!=foo+4){
00612           return false;
00613         }
00614     e_array=buf.getMPIntBits();
00615         d_array=buf.getMPIntBits();
00616     n_array=buf.getMPIntBits();
00617     byte[] u_array=buf.getMPIntBits();
00618     p_array=buf.getMPIntBits();
00619     q_array=buf.getMPIntBits();
00620         return true;
00621       }
00622 
00623       int index=0;
00624       int length=0;
00625 
00626       if(plain[index]!=0x30)return false;
00627       index++; // SEQUENCE
00628       length=plain[index++]&0xff;
00629       if((length&0x80)!=0){
00630         int foo=length&0x7f; length=0;
00631         while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
00632       }
00633 
00634       if(plain[index]!=0x02)return false;
00635       index++; // INTEGER
00636       length=plain[index++]&0xff;
00637       if((length&0x80)!=0){
00638         int foo=length&0x7f; length=0;
00639         while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
00640       }
00641       index+=length;
00642 
00643 //System.err.println("int: len="+length);
00644 //System.err.print(Integer.toHexString(plain[index-1]&0xff)+":");
00645 //System.err.println("");
00646 
00647       index++;
00648       length=plain[index++]&0xff;
00649       if((length&0x80)!=0){
00650         int foo=length&0x7f; length=0;
00651         while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
00652       }
00653       n_array=new byte[length];
00654       System.arraycopy(plain, index, n_array, 0, length);
00655       index+=length;
00656 /*
00657 System.err.println("int: N len="+length);
00658 for(int i=0; i<n_array.length; i++){
00659 System.err.print(Integer.toHexString(n_array[i]&0xff)+":");
00660 }
00661 System.err.println("");
00662 */
00663       index++;
00664       length=plain[index++]&0xff;
00665       if((length&0x80)!=0){
00666         int foo=length&0x7f; length=0;
00667         while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
00668       }
00669       e_array=new byte[length];
00670       System.arraycopy(plain, index, e_array, 0, length);
00671       index+=length;
00672 /*
00673 System.err.println("int: E len="+length);
00674 for(int i=0; i<e_array.length; i++){
00675 System.err.print(Integer.toHexString(e_array[i]&0xff)+":");
00676 }
00677 System.err.println("");
00678 */
00679       index++;
00680       length=plain[index++]&0xff;
00681       if((length&0x80)!=0){
00682         int foo=length&0x7f; length=0;
00683         while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
00684       }
00685       d_array=new byte[length];
00686       System.arraycopy(plain, index, d_array, 0, length);
00687       index+=length;
00688 /*
00689 System.err.println("int: D len="+length);
00690 for(int i=0; i<d_array.length; i++){
00691 System.err.print(Integer.toHexString(d_array[i]&0xff)+":");
00692 }
00693 System.err.println("");
00694 */
00695 
00696       index++;
00697       length=plain[index++]&0xff;
00698       if((length&0x80)!=0){
00699         int foo=length&0x7f; length=0;
00700         while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
00701       }
00702       p_array=new byte[length];
00703       System.arraycopy(plain, index, p_array, 0, length);
00704       index+=length;
00705 /*
00706 System.err.println("int: P len="+length);
00707 for(int i=0; i<p_array.length; i++){
00708 System.err.print(Integer.toHexString(p_array[i]&0xff)+":");
00709 }
00710 System.err.println("");
00711 */
00712       index++;
00713       length=plain[index++]&0xff;
00714       if((length&0x80)!=0){
00715         int foo=length&0x7f; length=0;
00716         while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
00717       }
00718       q_array=new byte[length];
00719       System.arraycopy(plain, index, q_array, 0, length);
00720       index+=length;
00721 /*
00722 System.err.println("int: q len="+length);
00723 for(int i=0; i<q_array.length; i++){
00724 System.err.print(Integer.toHexString(q_array[i]&0xff)+":");
00725 }
00726 System.err.println("");
00727 */
00728       index++;
00729       length=plain[index++]&0xff;
00730       if((length&0x80)!=0){
00731         int foo=length&0x7f; length=0;
00732         while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
00733       }
00734       dmp1_array=new byte[length];
00735       System.arraycopy(plain, index, dmp1_array, 0, length);
00736       index+=length;
00737 /*
00738 System.err.println("int: dmp1 len="+length);
00739 for(int i=0; i<dmp1_array.length; i++){
00740 System.err.print(Integer.toHexString(dmp1_array[i]&0xff)+":");
00741 }
00742 System.err.println("");
00743 */
00744       index++;
00745       length=plain[index++]&0xff;
00746       if((length&0x80)!=0){
00747         int foo=length&0x7f; length=0;
00748         while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
00749       }
00750       dmq1_array=new byte[length];
00751       System.arraycopy(plain, index, dmq1_array, 0, length);
00752       index+=length;
00753 /*
00754 System.err.println("int: dmq1 len="+length);
00755 for(int i=0; i<dmq1_array.length; i++){
00756 System.err.print(Integer.toHexString(dmq1_array[i]&0xff)+":");
00757 }
00758 System.err.println("");
00759 */
00760       index++;
00761       length=plain[index++]&0xff;
00762       if((length&0x80)!=0){
00763         int foo=length&0x7f; length=0;
00764         while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
00765       }
00766       iqmp_array=new byte[length];
00767       System.arraycopy(plain, index, iqmp_array, 0, length);
00768       index+=length;
00769 /*
00770 System.err.println("int: iqmp len="+length);
00771 for(int i=0; i<iqmp_array.length; i++){
00772 System.err.print(Integer.toHexString(iqmp_array[i]&0xff)+":");
00773 }
00774 System.err.println("");
00775 */
00776     }
00777     catch(Exception e){
00778       //System.err.println(e);
00779       return false;
00780     }
00781     return true;
00782   }
00783 
00784   boolean decrypt_dss(){
00785     try{
00786       byte[] plain;
00787       if(encrypted){
00788     if(keytype==OPENSSH){
00789       cipher.init(Cipher.DECRYPT_MODE, key, iv);
00790       plain=new byte[encoded_data.length];
00791       cipher.update(encoded_data, 0, encoded_data.length, plain, 0);
00792 /*
00793 for(int i=0; i<plain.length; i++){
00794 System.err.print(Integer.toHexString(plain[i]&0xff)+":");
00795 }
00796 System.err.println("");
00797 */
00798     }
00799     else if(keytype==FSECURE){
00800       for(int i=0; i<iv.length; i++)iv[i]=0;
00801       cipher.init(Cipher.DECRYPT_MODE, key, iv);
00802       plain=new byte[encoded_data.length];
00803       cipher.update(encoded_data, 0, encoded_data.length, plain, 0);
00804     }
00805     else{
00806       return false;
00807     }
00808       }
00809       else{
00810     if(P_array!=null) return true;
00811     plain=encoded_data;
00812       }
00813 
00814       if(keytype==FSECURE){              // FSecure   
00815     Buffer buf=new Buffer(plain);
00816         int foo=buf.getInt();
00817         if(plain.length!=foo+4){
00818           return false;
00819         }
00820     P_array=buf.getMPIntBits();
00821         G_array=buf.getMPIntBits();
00822     Q_array=buf.getMPIntBits();
00823     pub_array=buf.getMPIntBits();
00824     prv_array=buf.getMPIntBits();
00825         return true;
00826       }
00827 
00828       int index=0;
00829       int length=0;
00830       if(plain[index]!=0x30)return false;
00831       index++; // SEQUENCE
00832       length=plain[index++]&0xff;
00833       if((length&0x80)!=0){
00834         int foo=length&0x7f; length=0;
00835         while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
00836       }
00837       if(plain[index]!=0x02)return false;
00838       index++; // INTEGER
00839       length=plain[index++]&0xff;
00840       if((length&0x80)!=0){
00841         int foo=length&0x7f; length=0;
00842         while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
00843       }
00844       index+=length;
00845 
00846       index++;
00847       length=plain[index++]&0xff;
00848       if((length&0x80)!=0){
00849         int foo=length&0x7f; length=0;
00850         while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
00851       }
00852       P_array=new byte[length];
00853       System.arraycopy(plain, index, P_array, 0, length);
00854       index+=length;
00855 
00856       index++;
00857       length=plain[index++]&0xff;
00858       if((length&0x80)!=0){
00859         int foo=length&0x7f; length=0;
00860         while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
00861       }
00862       Q_array=new byte[length];
00863       System.arraycopy(plain, index, Q_array, 0, length);
00864       index+=length;
00865 
00866       index++;
00867       length=plain[index++]&0xff;
00868       if((length&0x80)!=0){
00869         int foo=length&0x7f; length=0;
00870         while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
00871       }
00872       G_array=new byte[length];
00873       System.arraycopy(plain, index, G_array, 0, length);
00874       index+=length;
00875 
00876       index++;
00877       length=plain[index++]&0xff;
00878       if((length&0x80)!=0){
00879         int foo=length&0x7f; length=0;
00880         while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
00881       }
00882       pub_array=new byte[length];
00883       System.arraycopy(plain, index, pub_array, 0, length);
00884       index+=length;
00885 
00886       index++;
00887       length=plain[index++]&0xff;
00888       if((length&0x80)!=0){
00889         int foo=length&0x7f; length=0;
00890         while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
00891       }
00892       prv_array=new byte[length];
00893       System.arraycopy(plain, index, prv_array, 0, length);
00894       index+=length;
00895     }
00896     catch(Exception e){
00897       //System.err.println(e);
00898       //e.printStackTrace();
00899       return false;
00900     }
00901     return true;
00902   }
00903 
00904   public boolean isEncrypted(){
00905     return encrypted;
00906   }
00907 
00908   public String getName(){
00909     return identity;
00910   }
00911 
00912   private byte a2b(byte c){
00913     if('0'<=c&&c<='9') return (byte)(c-'0');
00914     if('a'<=c&&c<='z') return (byte)(c-'a'+10);
00915     return (byte)(c-'A'+10);
00916   }
00917 
00918   // we should have a hashCode implementation, too.
00919   public boolean equals(Object o){
00920     if(!(o instanceof IdentityFile)) return super.equals(o); // return false would be more clear here.
00921     IdentityFile foo=(IdentityFile)o;
00922     return getName().equals(foo.getName());
00923   }
00924 
00925   public void clear(){
00926     Util.bzero(encoded_data);
00927     Util.bzero(prv_array);
00928     Util.bzero(d_array);
00929     Util.bzero(key);
00930     Util.bzero(iv);
00931   }
00932 
00933   public void finalize (){
00934     clear();
00935   }
00936 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1