using System; using System.IO; namespace Tamir.SharpSsh.jsch { /* -*-mode:java; c-basic-offset:2; -*- */ /* Copyright (c) 2002,2003,2004 ymnk, JCraft,Inc. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT, INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ internal class IdentityFile : Identity { String identity; byte[] key; byte[] iv; private JSch jsch; private HASH hash; private byte[] encoded_data; private Cipher cipher; // DSA private byte[] P_array; private byte[] Q_array; private byte[] G_array; private byte[] pub_array; private byte[] prv_array; // RSA private byte[] n_array; // modulus private byte[] e_array; // public exponent private byte[] d_array; // private exponent private byte[] p_array; private byte[] q_array; private byte[] dmp1_array; private byte[] dmq1_array; private byte[] iqmp_array; // private String algname="ssh-dss"; private String algname="ssh-rsa"; private const int ERROR=0; private const int RSA=1; private const int DSS=2; internal const int UNKNOWN=3; private const int OPENSSH=0; private const int FSECURE=1; private const int PUTTY=2; private int type=ERROR; private int keytype=OPENSSH; private byte[] publickeyblob=null; private bool encrypted=true; internal IdentityFile(String identity, JSch jsch) { this.identity=identity; this.jsch=jsch; try { Type c=Type.GetType(jsch.getConfig("3des-cbc")); cipher=(Cipher)Activator.CreateInstance(c); key=new byte[cipher.getBlockSize()]; // 24 iv=new byte[cipher.getIVSize()]; // 8 c=Type.GetType(jsch.getConfig("md5")); hash=(HASH)(Activator.CreateInstance(c)); hash.init(); FileInfo file=new FileInfo(identity); FileStream fis = File.OpenRead(identity); byte[] buf=new byte[(int)(file.Length)]; int len=fis.Read(buf, 0, buf.Length); fis.Close(); int i=0; while(i4 && // FSecure encoded_data[0]==(byte)0x3f && encoded_data[1]==(byte)0x6f && encoded_data[2]==(byte)0xf9 && encoded_data[3]==(byte)0xeb) { Buffer _buf=new Buffer(encoded_data); _buf.getInt(); // 0x3f6ff9be _buf.getInt(); byte[]_type=_buf.getString(); //System.out.println("type: "+new String(_type)); byte[] _cipher=_buf.getString(); String s_cipher=System.Text.Encoding.Default.GetString(_cipher); //System.out.println("cipher: "+cipher); if(s_cipher.Equals("3des-cbc")) { _buf.getInt(); byte[] foo=new byte[encoded_data.Length-_buf.getOffSet()]; _buf.getByte(foo); encoded_data=foo; encrypted=true; throw new JSchException("unknown privatekey format: "+identity); } else if(s_cipher.Equals("none")) { _buf.getInt(); //_buf.getInt(); encrypted=false; byte[] foo=new byte[encoded_data.Length-_buf.getOffSet()]; _buf.getByte(foo); encoded_data=foo; } } try { file=new FileInfo(identity+".pub"); fis=File.OpenRead(identity+".pub"); buf=new byte[(int)(file.Length)]; len=fis.Read(buf, 0, buf.Length); fis.Close(); } catch { return; } if(buf.Length>4 && // FSecure's public key buf[0]=='-' && buf[1]=='-' && buf[2]=='-' && buf[3]=='-') { i=0; do{i++;}while(buf.Length>i && buf[i]!=0x0a); if(buf.Length<=i) return; while(true) { if(buf[i]==0x0a) { bool inheader=false; for(int j=i+1; j=len) return; start=i; while(i>>24); goo[1]=(byte)(session.getSessionId().Length>>>16); goo[2]=(byte)(session.getSessionId().Length>>>8); goo[3]=(byte)(session.getSessionId().Length); rsa.update(goo); rsa.update(session.getSessionId()); */ rsa.update(data); byte[] sig = rsa.sign(); Buffer buf=new Buffer("ssh-rsa".Length+4+ sig.Length+4); buf.putString( System.Text.Encoding.Default.GetBytes( "ssh-rsa" )); buf.putString(sig); return buf.buffer; } catch(Exception e) { Console.WriteLine(e); } return null; } byte[] getSignature_dss(Session session, byte[] data) { /* byte[] foo; int i; System.out.print("P "); foo=P_array; for(i=0; i>>24); goo[1]=(byte)(session.getSessionId().Length>>>16); goo[2]=(byte)(session.getSessionId().Length>>>8); goo[3]=(byte)(session.getSessionId().Length); dsa.update(goo); dsa.update(session.getSessionId()); */ dsa.update(data); byte[] sig = dsa.sign(); Buffer buf=new Buffer("ssh-dss".Length+4+ sig.Length+4); buf.putString( System.Text.Encoding.Default.GetBytes( "ssh-dss" ) ); buf.putString(sig); return buf.buffer; } catch(Exception e) { Console.WriteLine("e "+e); } return null; } public bool decrypt() { if(type==RSA) return decrypt_rsa(); return decrypt_dss(); } bool decrypt_rsa() { // byte[] p_array; // byte[] q_array; // byte[] dmp1_array; // byte[] dmq1_array; // byte[] iqmp_array; try { byte[] plain; if(encrypted) { if(keytype==OPENSSH) { cipher.init(Cipher.DECRYPT_MODE, key, iv); plain=new byte[encoded_data.Length]; cipher.update(encoded_data, 0, encoded_data.Length, plain, 0); } else if(keytype==FSECURE) { for(int i=0; i0){ Length=(Length<<8)+(plain[index++]&0xff); } } if(plain[index]!=0x02)return false; index++; // INTEGER Length=plain[index++]&0xff; if((Length&0x80)!=0) { int foo=Length&0x7f; Length=0; while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); } } index+=Length; //System.out.println("int: len="+Length); //System.out.print(Integer.toHexString(plain[index-1]&0xff)+":"); //System.out.println(""); index++; Length=plain[index++]&0xff; if((Length&0x80)!=0) { int foo=Length&0x7f; Length=0; while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); } } n_array=new byte[Length]; Array.Copy(plain, index, n_array, 0, Length); index+=Length; /* System.out.println("int: N len="+Length); for(int i=0; i0){ Length=(Length<<8)+(plain[index++]&0xff); } } e_array=new byte[Length]; Array.Copy(plain, index, e_array, 0, Length); index+=Length; /* System.out.println("int: E len="+Length); for(int i=0; i0){ Length=(Length<<8)+(plain[index++]&0xff); } } d_array=new byte[Length]; Array.Copy(plain, index, d_array, 0, Length); index+=Length; /* System.out.println("int: D len="+Length); for(int i=0; i0){ Length=(Length<<8)+(plain[index++]&0xff); } } p_array=new byte[Length]; Array.Copy(plain, index, p_array, 0, Length); index+=Length; /* System.out.println("int: P len="+Length); for(int i=0; i0){ Length=(Length<<8)+(plain[index++]&0xff); } } q_array=new byte[Length]; Array.Copy(plain, index, q_array, 0, Length); index+=Length; /* System.out.println("int: q len="+Length); for(int i=0; i0){ Length=(Length<<8)+(plain[index++]&0xff); } } dmp1_array=new byte[Length]; Array.Copy(plain, index, dmp1_array, 0, Length); index+=Length; /* System.out.println("int: dmp1 len="+Length); for(int i=0; i0){ Length=(Length<<8)+(plain[index++]&0xff); } } dmq1_array=new byte[Length]; Array.Copy(plain, index, dmq1_array, 0, Length); index+=Length; /* System.out.println("int: dmq1 len="+Length); for(int i=0; i0){ Length=(Length<<8)+(plain[index++]&0xff); } } iqmp_array=new byte[Length]; Array.Copy(plain, index, iqmp_array, 0, Length); index+=Length; /* System.out.println("int: iqmp len="+Length); for(int i=0; i0){ Length=(Length<<8)+(plain[index++]&0xff); } } if(plain[index]!=0x02)return false; index++; // INTEGER Length=plain[index++]&0xff; if((Length&0x80)!=0) { int foo=Length&0x7f; Length=0; while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); } } index+=Length; index++; Length=plain[index++]&0xff; if((Length&0x80)!=0) { int foo=Length&0x7f; Length=0; while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); } } P_array=new byte[Length]; Array.Copy(plain, index, P_array, 0, Length); index+=Length; index++; Length=plain[index++]&0xff; if((Length&0x80)!=0) { int foo=Length&0x7f; Length=0; while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); } } Q_array=new byte[Length]; Array.Copy(plain, index, Q_array, 0, Length); index+=Length; index++; Length=plain[index++]&0xff; if((Length&0x80)!=0) { int foo=Length&0x7f; Length=0; while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); } } G_array=new byte[Length]; Array.Copy(plain, index, G_array, 0, Length); index+=Length; index++; Length=plain[index++]&0xff; if((Length&0x80)!=0) { int foo=Length&0x7f; Length=0; while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); } } pub_array=new byte[Length]; Array.Copy(plain, index, pub_array, 0, Length); index+=Length; index++; Length=plain[index++]&0xff; if((Length&0x80)!=0) { int foo=Length&0x7f; Length=0; while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); } } prv_array=new byte[Length]; Array.Copy(plain, index, prv_array, 0, Length); index+=Length; } catch { //System.out.println(e); //e.printStackTrace(); return false; } return true; } public bool isEncrypted() { return encrypted; } public String getName(){return identity;} private int writeSEQUENCE(byte[] buf, int index, int len) { buf[index++]=0x30; index=writeLength(buf, index, len); return index; } private int writeINTEGER(byte[] buf, int index, byte[] data) { buf[index++]=0x02; index=writeLength(buf, index, data.Length); Array.Copy(data, 0, buf, index, data.Length); index+=data.Length; return index; } private int countLength(int i_len) { uint len = (uint)i_len; int i=1; if(len<=0x7f) return i; while(len>0) { len>>=8; i++; } return i; } private int writeLength(byte[] data, int index, int i_len) { int len = (int)i_len; int i=countLength(len)-1; if(i==0) { data[index++]=(byte)len; return index; } data[index++]=(byte)(0x80|i); int j=index+i; while(i>0) { data[index+i-1]=(byte)(len&0xff); len>>=8; i--; } return j; } private byte a2b(byte c) { if('0'<=c&&c<='9') return (byte)(c-'0'); if('a'<=c&&c<='z') return (byte)(c-'a'+10); return (byte)(c-'A'+10); } private byte b2a(byte c) { if(0<=c&&c<=9) return (byte)(c+'0'); return (byte)(c-10+'A'); } } }