Ejemplo n.º 1
0
		/// <summary>
		/// Constructs a new SSH instance
		/// </summary>
		/// <param name="sftpHost">The remote SSH host</param>
		/// <param name="user">The login username</param>
		/// <param name="password">The login password</param>
		public SshBase(string sftpHost, string user, string password)
		{
			this.m_host = sftpHost;
			this.m_user = user;
			this.Password = password;
			m_jsch = new JSch();
		}
Ejemplo n.º 2
0
        public SshHelper(string host, string username, string password)
        {
            this.host = host;
            JSch jsch=new JSch();
            Session session=jsch.getSession(username, host, 22);
            session.setPassword( password );

            Hashtable config=new Hashtable();
            config.Add("StrictHostKeyChecking", "no");
            session.setConfig(config);

            session.connect();

            channel=(ChannelShell)session.openChannel("shell");

            writer_po = new PipedOutputStream();
            PipedInputStream writer_pi = new PipedInputStream( writer_po );

            PipedInputStream reader_pi = new PipedInputStream();
            PipedOutputStream reader_po = new PipedOutputStream( reader_pi );
            reader = new StreamReader (reader_pi,Encoding.UTF8);

            channel.setInputStream( writer_pi );
            channel.setOutputStream( reader_po );

            channel.connect();
            channel.setPtySize(132, 132, 1024, 768);
        }
Ejemplo n.º 3
0
        public String getFingerPrint(JSch jsch)
        {
            HASH hash = null;

            try{
                hash = (HASH)Activator.CreateInstance(Type.GetType(jsch.getConfig("md5")));
            }
            catch (Exception e) { Console.Error.WriteLine("getFingerPrint: " + e); }
            return(Util.getFingerPrint(hash, key));
        }
Ejemplo n.º 4
0
        public static KeyPair load(JSch jsch, String prvkey)
        {
            String pubkey = prvkey + ".pub";

//			if(!new File(pubkey).exists())
            if (!File.Exists(pubkey))
            {
                pubkey = null;
            }
            return(load(jsch, prvkey, pubkey));
        }
Ejemplo n.º 5
0
        public static KeyPair genKeyPair(JSch jsch, int type, int key_size)
        {
            KeyPair kpair = null;

            if (type == DSA)
            {
                kpair = new KeyPairDSA(jsch);
            }
            else if (type == RSA)
            {
                kpair = new KeyPairRSA(jsch);
            }
            if (kpair != null)
            {
                kpair.generate(key_size);
            }
            return(kpair);
        }
Ejemplo n.º 6
0
		/// <summary>
		/// Constructs a new SSH stream.
		/// </summary>
		/// <param name="host">The hostname or IP address of the remote SSH machine</param>
		/// <param name="username">The name of the user connecting to the remote machine</param>
		/// <param name="password">The password of the user connecting to the remote machine</param>
		public SshStream(string host, string username, string password)
		{
			this.m_host = host;
			JSch jsch=new JSch();
			m_session=jsch.getSession(username, host, 22);
			m_session.setPassword( password );
		
			Hashtable config=new Hashtable();
			config.Add("StrictHostKeyChecking", "no");
			m_session.setConfig(config);
		
			m_session.connect();
           
			m_channel=(ChannelShell)m_session.openChannel("shell");

			m_in	= m_channel.getInputStream();
			m_out	= m_channel.getOutputStream();

			m_channel.connect();
			m_channel.setPtySize(80, 132, 1024, 768);

			Prompt = "\n";
			m_escapeCharPattern = "\\[[0-9;?]*[^0-9;]";
		}
Ejemplo n.º 7
0
        public static KeyPair load(JSch jsch, String prvkey, String pubkey)
        {
            byte[] iv=new byte[8];       // 8
            bool encrypted=true;
            byte[] data=null;

            byte[] publickeyblob=null;

            int type=ERROR;
            int vendor=VENDOR_OPENSSH;

            try
            {
                //File file=new File(prvkey);
                FileStream fis=File.OpenRead(prvkey);
                byte[] buf=new byte[(int)(fis.Length)];
                int len=fis.Read(buf, 0, buf.Length);
                fis.Close();

                int i=0;

                while(i<len)
                {
                    if(buf[i]=='B'&& buf[i+1]=='E'&& buf[i+2]=='G'&& buf[i+3]=='I')
                    {
                        i+=6;
                        if(buf[i]=='D'&& buf[i+1]=='S'&& buf[i+2]=='A'){ type=DSA; }
                        else if(buf[i]=='R'&& buf[i+1]=='S'&& buf[i+2]=='A'){ type=RSA; }
                        else if(buf[i]=='S'&& buf[i+1]=='S'&& buf[i+2]=='H')
                        { // FSecure
                            type=UNKNOWN;
                            vendor=VENDOR_FSECURE;
                        }
                        else
                        {
                            //System.outs.println("invalid format: "+identity);
                            throw new JSchException("invaid privatekey: "+prvkey);
                        }
                        i+=3;
                        continue;
                    }
                    if(buf[i]=='C'&& buf[i+1]=='B'&& buf[i+2]=='C'&& buf[i+3]==',')
                    {
                        i+=4;
                        for(int ii=0; ii<iv.Length; ii++)
                        {
                            iv[ii]=(byte)(((a2b(buf[i++])<<4)&0xf0)+(a2b(buf[i++])&0xf));
                        }
                        continue;
                    }
                    if(buf[i]==0x0d &&
                        i+1<buf.Length && buf[i+1]==0x0a)
                    {
                        i++;
                        continue;
                    }
                    if(buf[i]==0x0a && i+1<buf.Length)
                    {
                        if(buf[i+1]==0x0a){ i+=2; break; }
                        if(buf[i+1]==0x0d &&
                            i+2<buf.Length && buf[i+2]==0x0a)
                        {
                            i+=3; break;
                        }
                        bool inheader=false;
                        for(int j=i+1; j<buf.Length; j++)
                        {
                            if(buf[j]==0x0a) break;
                            //if(buf[j]==0x0d) break;
                            if(buf[j]==':'){inheader=true; break;}
                        }
                        if(!inheader)
                        {
                            i++;
                            encrypted=false;    // no passphrase
                            break;
                        }
                    }
                    i++;
                }

                if(type==ERROR)
                {
                    throw new JSchException("invaid privatekey: "+prvkey);
                }

                int start=i;
                while(i<len)
                {
                    if(buf[i]==0x0a)
                    {
                        bool xd=(buf[i-1]==0x0d);
                        Array.Copy(buf, i+1,
                            buf,
                            i-(xd ? 1 : 0),
                            len-i-1-(xd ? 1 : 0)
                            );
                        if(xd)len--;
                        len--;
                        continue;
                    }
                    if(buf[i]=='-'){  break; }
                    i++;
                }
                data=Util.fromBase64(buf, start, i-start);

                if(data.Length>4 &&            // FSecure
                    data[0]==(byte)0x3f &&
                    data[1]==(byte)0x6f &&
                    data[2]==(byte)0xf9 &&
                    data[3]==(byte)0xeb)
                {

                    Buffer _buf=new Buffer(data);
                    _buf.getInt();  // 0x3f6ff9be
                    _buf.getInt();
                    byte[]_type=_buf.getString();
                    //System.outs.println("type: "+new String(_type));
                    byte[] _cipher=_buf.getString();
                    String cipher=Util.getString(_cipher);
                    //System.outs.println("cipher: "+cipher);
                    if(cipher.Equals("3des-cbc"))
                    {
                        _buf.getInt();
                        byte[] foo=new byte[data.Length-_buf.getOffSet()];
                        _buf.getByte(foo);
                        data=foo;
                        encrypted=true;
                        throw new JSchException("unknown privatekey format: "+prvkey);
                    }
                    else if(cipher.Equals("none"))
                    {
                        _buf.getInt();
                        _buf.getInt();

                        encrypted=false;

                        byte[] foo=new byte[data.Length-_buf.getOffSet()];
                        _buf.getByte(foo);
                        data=foo;
                    }
                }

                if(pubkey!=null)
                {
                    try
                    {
                        //file=new File(pubkey);
                        fis=File.OpenRead(pubkey);
                        buf=new byte[(int)(fis.Length)];
                        len=fis.Read(buf, 0, buf.Length);
                        fis.Close();

                        if(buf.Length>4 &&             // FSecure's public key
                            buf[0]=='-' && buf[1]=='-' && buf[2]=='-' && buf[3]=='-')
                        {

                            bool valid=true;
                            i=0;
                            do{i++;}while(buf.Length>i && buf[i]!=0x0a);
                            if(buf.Length<=i) {valid=false;}

                            while(valid)
                            {
                                if(buf[i]==0x0a)
                                {
                                    bool inheader=false;
                                    for(int j=i+1; j<buf.Length; j++)
                                    {
                                        if(buf[j]==0x0a) break;
                                        if(buf[j]==':'){inheader=true; break;}
                                    }
                                    if(!inheader)
                                    {
                                        i++;
                                        break;
                                    }
                                }
                                i++;
                            }
                            if(buf.Length<=i){valid=false;}

                            start=i;
                            while(valid && i<len)
                            {
                                if(buf[i]==0x0a)
                                {
                                    Array.Copy(buf, i+1, buf, i, len-i-1);
                                    len--;
                                    continue;
                                }
                                if(buf[i]=='-'){  break; }
                                i++;
                            }
                            if(valid)
                            {
                                publickeyblob=Util.fromBase64(buf, start, i-start);
                                if(type==UNKNOWN)
                                {
                                    if(publickeyblob[8]=='d'){ type=DSA; }
                                    else if(publickeyblob[8]=='r'){ type=RSA; }
                                }
                            }
                        }
                        else
                        {
                            if(buf[0]=='s'&& buf[1]=='s'&& buf[2]=='h' && buf[3]=='-')
                            {
                                i=0;
                                while(i<len){ if(buf[i]==' ')break; i++;} i++;
                                if(i<len)
                                {
                                    start=i;
                                    while(i<len){ if(buf[i]==' ')break; i++;}
                                    publickeyblob=Util.fromBase64(buf, start, i-start);
                                }
                            }
                        }
                    }
                    catch//(Exception ee)
                    {
                    }
                }
            }
            catch(Exception e)
            {
                if(e is JSchException) throw (JSchException)e;
                throw new JSchException(e.ToString());
            }

            KeyPair kpair=null;
            if(type==DSA){ kpair=new KeyPairDSA(jsch); }
            else if(type==RSA){ kpair=new KeyPairRSA(jsch); }

            if(kpair!=null)
            {
                kpair.encrypted=encrypted;
                kpair.publickeyblob=publickeyblob;
                kpair.vendor=vendor;

                if(encrypted)
                {
                    kpair.iv=iv;
                    kpair.data=data;
                }
                else
                {
                    if(kpair.parse(data))
                    {
                        return kpair;
                    }
                    else
                    {
                        throw new JSchException("invaid privatekey: "+prvkey);
                    }
                }
            }

            return kpair;
        }
Ejemplo n.º 8
0
 public static KeyPair load(JSch jsch, String prvkey)
 {
     String pubkey=prvkey+".pub";
     //			if(!new File(pubkey).exists())
     if(!File.Exists(pubkey))
     {
         pubkey=null;
     }
     return load(jsch, prvkey, pubkey);
 }
Ejemplo n.º 9
0
 public static KeyPair genKeyPair(JSch jsch, int type, int key_size)
 {
     KeyPair kpair=null;
     if(type==DSA){ kpair=new KeyPairDSA(jsch); }
     else if(type==RSA){ kpair=new KeyPairRSA(jsch); }
     if(kpair!=null)
     {
         kpair.generate(key_size);
     }
     return kpair;
 }
Ejemplo n.º 10
0
 public KeyPairRSA(JSch jsch) : base(jsch)
 {
 }
Ejemplo n.º 11
0
 internal Session(JSch jsch)
 {
     ;
     this.jsch=jsch;
     buf=new Buffer();
     packet=new Packet(buf);
 }
Ejemplo n.º 12
0
 public KeyPairDSA(JSch jsch)
     : base(jsch)
 {
 }
Ejemplo n.º 13
0
        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 (i < len)
                {
                    if (buf[i] == 'B' && buf[i + 1] == 'E' && buf[i + 2] == 'G' && buf[i + 3] == 'I')
                    {
                        i += 6;
                        if (buf[i] == 'D' && buf[i + 1] == 'S' && buf[i + 2] == 'A')
                        {
                            type = DSS;
                        }
                        else if (buf[i] == 'R' && buf[i + 1] == 'S' && buf[i + 2] == 'A')
                        {
                            type = RSA;
                        }
                        else if (buf[i] == 'S' && buf[i + 1] == 'S' && buf[i + 2] == 'H')
                        {                         // FSecure
                            type    = UNKNOWN;
                            keytype = FSECURE;
                        }
                        else
                        {
                            //System.out.println("invalid format: "+identity);
                            throw new JSchException("invaid privatekey: " + identity);
                        }
                        i += 3;
                        continue;
                    }
                    if (buf[i] == 'C' && buf[i + 1] == 'B' && buf[i + 2] == 'C' && buf[i + 3] == ',')
                    {
                        i += 4;
                        for (int ii = 0; ii < iv.Length; ii++)
                        {
                            iv[ii] = (byte)(((a2b(buf[i++]) << 4) & 0xf0) +
                                            (a2b(buf[i++]) & 0xf));
                        }
                        continue;
                    }
                    if (buf[i] == 0x0d &&
                        i + 1 < buf.Length && buf[i + 1] == 0x0a)
                    {
                        i++;
                        continue;
                    }
                    if (buf[i] == 0x0a && i + 1 < buf.Length)
                    {
                        if (buf[i + 1] == 0x0a)
                        {
                            i += 2; break;
                        }
                        if (buf[i + 1] == 0x0d &&
                            i + 2 < buf.Length && buf[i + 2] == 0x0a)
                        {
                            i += 3; break;
                        }
                        bool inheader = false;
                        for (int j = i + 1; j < buf.Length; j++)
                        {
                            if (buf[j] == 0x0a)
                            {
                                break;
                            }
                            //if(buf[j]==0x0d) break;
                            if (buf[j] == ':')
                            {
                                inheader = true; break;
                            }
                        }
                        if (!inheader)
                        {
                            i++;
                            encrypted = false;                              // no passphrase
                            break;
                        }
                    }
                    i++;
                }

                if (type == ERROR)
                {
                    throw new JSchException("invaid privatekey: " + identity);
                }

                int start = i;
                while (i < len)
                {
                    if (buf[i] == 0x0a)
                    {
                        bool xd = (buf[i - 1] == 0x0d);
                        Array.Copy(buf, i + 1,
                                   buf,
                                   i - (xd ? 1 : 0),
                                   len - i - 1 - (xd ? 1 : 0)
                                   );
                        if (xd)
                        {
                            len--;
                        }
                        len--;
                        continue;
                    }
                    if (buf[i] == '-')
                    {
                        break;
                    }
                    i++;
                }
                encoded_data = Util.fromBase64(buf, start, i - start);

                if (encoded_data.Length > 4 &&                         // 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 < buf.Length; j++)
                            {
                                if (buf[j] == 0x0a)
                                {
                                    break;
                                }
                                if (buf[j] == ':')
                                {
                                    inheader = true; break;
                                }
                            }
                            if (!inheader)
                            {
                                i++;
                                break;
                            }
                        }
                        i++;
                    }
                    if (buf.Length <= i)
                    {
                        return;
                    }

                    start = i;
                    while (i < len)
                    {
                        if (buf[i] == 0x0a)
                        {
                            Array.Copy(buf, i + 1, buf, i, len - i - 1);
                            len--;
                            continue;
                        }
                        if (buf[i] == '-')
                        {
                            break;
                        }
                        i++;
                    }
                    publickeyblob = Util.fromBase64(buf, start, i - start);

                    if (type == UNKNOWN)
                    {
                        if (publickeyblob[8] == 'd')
                        {
                            type = DSS;
                        }
                        else if (publickeyblob[8] == 'r')
                        {
                            type = RSA;
                        }
                    }
                }
                else
                {
                    if (buf[0] != 's' || buf[1] != 's' || buf[2] != 'h' || buf[3] != '-')
                    {
                        return;
                    }
                    i = 0;
                    while (i < len)
                    {
                        if (buf[i] == ' ')
                        {
                            break;
                        }
                        i++;
                    }
                    i++;
                    if (i >= len)
                    {
                        return;
                    }
                    start = i;
                    while (i < len)
                    {
                        if (buf[i] == ' ')
                        {
                            break;
                        }
                        i++;
                    }
                    publickeyblob = Util.fromBase64(buf, start, i - start);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Identity: " + e);
                if (e is JSchException)
                {
                    throw (JSchException)e;
                }
                throw new JSchException(e.ToString());
            }
        }
Ejemplo n.º 14
0
 public KeyPair(JSch jsch)
 {
     this.jsch = jsch;
 }
Ejemplo n.º 15
0
 internal KnownHosts(JSch jsch)
     : base()
 {
     this.jsch=jsch;
     pool=new System.Collections.ArrayList();
 }
Ejemplo n.º 16
0
 public KeyPair(JSch jsch)
 {
     this.jsch=jsch;
 }
Ejemplo n.º 17
0
 public static KeyPair genKeyPair(JSch jsch, int type)
 {
     return genKeyPair(jsch, type, 1024);
 }
Ejemplo n.º 18
0
        public static KeyPair load(JSch jsch, String prvkey, String pubkey)
        {
            byte[] iv        = new byte[8];          // 8
            bool   encrypted = true;

            byte[] data = null;

            byte[] publickeyblob = null;

            int type   = ERROR;
            int vendor = VENDOR_OPENSSH;

            try
            {
                //File file=new File(prvkey);
                FileStream fis = File.OpenRead(prvkey);
                byte[]     buf = new byte[(int)(fis.Length)];
                int        len = fis.Read(buf, 0, buf.Length);
                fis.Close();

                int i = 0;

                while (i < len)
                {
                    if (buf[i] == 'B' && buf[i + 1] == 'E' && buf[i + 2] == 'G' && buf[i + 3] == 'I')
                    {
                        i += 6;
                        if (buf[i] == 'D' && buf[i + 1] == 'S' && buf[i + 2] == 'A')
                        {
                            type = DSA;
                        }
                        else if (buf[i] == 'R' && buf[i + 1] == 'S' && buf[i + 2] == 'A')
                        {
                            type = RSA;
                        }
                        else if (buf[i] == 'S' && buf[i + 1] == 'S' && buf[i + 2] == 'H')
                        {                         // FSecure
                            type   = UNKNOWN;
                            vendor = VENDOR_FSECURE;
                        }
                        else
                        {
                            //System.outs.println("invalid format: "+identity);
                            throw new JSchException("invaid privatekey: " + prvkey);
                        }
                        i += 3;
                        continue;
                    }
                    if (buf[i] == 'C' && buf[i + 1] == 'B' && buf[i + 2] == 'C' && buf[i + 3] == ',')
                    {
                        i += 4;
                        for (int ii = 0; ii < iv.Length; ii++)
                        {
                            iv[ii] = (byte)(((a2b(buf[i++]) << 4) & 0xf0) + (a2b(buf[i++]) & 0xf));
                        }
                        continue;
                    }
                    if (buf[i] == 0x0d &&
                        i + 1 < buf.Length && buf[i + 1] == 0x0a)
                    {
                        i++;
                        continue;
                    }
                    if (buf[i] == 0x0a && i + 1 < buf.Length)
                    {
                        if (buf[i + 1] == 0x0a)
                        {
                            i += 2; break;
                        }
                        if (buf[i + 1] == 0x0d &&
                            i + 2 < buf.Length && buf[i + 2] == 0x0a)
                        {
                            i += 3; break;
                        }
                        bool inheader = false;
                        for (int j = i + 1; j < buf.Length; j++)
                        {
                            if (buf[j] == 0x0a)
                            {
                                break;
                            }
                            //if(buf[j]==0x0d) break;
                            if (buf[j] == ':')
                            {
                                inheader = true; break;
                            }
                        }
                        if (!inheader)
                        {
                            i++;
                            encrypted = false;                              // no passphrase
                            break;
                        }
                    }
                    i++;
                }

                if (type == ERROR)
                {
                    throw new JSchException("invaid privatekey: " + prvkey);
                }

                int start = i;
                while (i < len)
                {
                    if (buf[i] == 0x0a)
                    {
                        bool xd = (buf[i - 1] == 0x0d);
                        Array.Copy(buf, i + 1,
                                   buf,
                                   i - (xd ? 1 : 0),
                                   len - i - 1 - (xd ? 1 : 0)
                                   );
                        if (xd)
                        {
                            len--;
                        }
                        len--;
                        continue;
                    }
                    if (buf[i] == '-')
                    {
                        break;
                    }
                    i++;
                }
                data = Util.fromBase64(buf, start, i - start);

                if (data.Length > 4 &&                         // FSecure
                    data[0] == (byte)0x3f &&
                    data[1] == (byte)0x6f &&
                    data[2] == (byte)0xf9 &&
                    data[3] == (byte)0xeb)
                {
                    Buffer _buf = new Buffer(data);
                    _buf.getInt();                      // 0x3f6ff9be
                    _buf.getInt();
                    byte[] _type = _buf.getString();
                    //System.outs.println("type: "+new String(_type));
                    byte[] _cipher = _buf.getString();
                    String cipher  = Util.getString(_cipher);
                    //System.outs.println("cipher: "+cipher);
                    if (cipher.Equals("3des-cbc"))
                    {
                        _buf.getInt();
                        byte[] foo = new byte[data.Length - _buf.getOffSet()];
                        _buf.getByte(foo);
                        data      = foo;
                        encrypted = true;
                        throw new JSchException("unknown privatekey format: " + prvkey);
                    }
                    else if (cipher.Equals("none"))
                    {
                        _buf.getInt();
                        _buf.getInt();

                        encrypted = false;

                        byte[] foo = new byte[data.Length - _buf.getOffSet()];
                        _buf.getByte(foo);
                        data = foo;
                    }
                }

                if (pubkey != null)
                {
                    try
                    {
                        //file=new File(pubkey);
                        fis = File.OpenRead(pubkey);
                        buf = new byte[(int)(fis.Length)];
                        len = fis.Read(buf, 0, buf.Length);
                        fis.Close();

                        if (buf.Length > 4 &&                                  // FSecure's public key
                            buf[0] == '-' && buf[1] == '-' && buf[2] == '-' && buf[3] == '-')
                        {
                            bool valid = true;
                            i = 0;
                            do
                            {
                                i++;
                            }while(buf.Length > i && buf[i] != 0x0a);
                            if (buf.Length <= i)
                            {
                                valid = false;
                            }

                            while (valid)
                            {
                                if (buf[i] == 0x0a)
                                {
                                    bool inheader = false;
                                    for (int j = i + 1; j < buf.Length; j++)
                                    {
                                        if (buf[j] == 0x0a)
                                        {
                                            break;
                                        }
                                        if (buf[j] == ':')
                                        {
                                            inheader = true; break;
                                        }
                                    }
                                    if (!inheader)
                                    {
                                        i++;
                                        break;
                                    }
                                }
                                i++;
                            }
                            if (buf.Length <= i)
                            {
                                valid = false;
                            }

                            start = i;
                            while (valid && i < len)
                            {
                                if (buf[i] == 0x0a)
                                {
                                    Array.Copy(buf, i + 1, buf, i, len - i - 1);
                                    len--;
                                    continue;
                                }
                                if (buf[i] == '-')
                                {
                                    break;
                                }
                                i++;
                            }
                            if (valid)
                            {
                                publickeyblob = Util.fromBase64(buf, start, i - start);
                                if (type == UNKNOWN)
                                {
                                    if (publickeyblob[8] == 'd')
                                    {
                                        type = DSA;
                                    }
                                    else if (publickeyblob[8] == 'r')
                                    {
                                        type = RSA;
                                    }
                                }
                            }
                        }
                        else
                        {
                            if (buf[0] == 's' && buf[1] == 's' && buf[2] == 'h' && buf[3] == '-')
                            {
                                i = 0;
                                while (i < len)
                                {
                                    if (buf[i] == ' ')
                                    {
                                        break;
                                    }
                                    i++;
                                }
                                i++;
                                if (i < len)
                                {
                                    start = i;
                                    while (i < len)
                                    {
                                        if (buf[i] == ' ')
                                        {
                                            break;
                                        }
                                        i++;
                                    }
                                    publickeyblob = Util.fromBase64(buf, start, i - start);
                                }
                            }
                        }
                    }
                    catch                    //(Exception ee)
                    {
                    }
                }
            }
            catch (Exception e)
            {
                if (e is JSchException)
                {
                    throw (JSchException)e;
                }
                throw new JSchException(e.ToString());
            }

            KeyPair kpair = null;

            if (type == DSA)
            {
                kpair = new KeyPairDSA(jsch);
            }
            else if (type == RSA)
            {
                kpair = new KeyPairRSA(jsch);
            }

            if (kpair != null)
            {
                kpair.encrypted     = encrypted;
                kpair.publickeyblob = publickeyblob;
                kpair.vendor        = vendor;

                if (encrypted)
                {
                    kpair.iv   = iv;
                    kpair.data = data;
                }
                else
                {
                    if (kpair.parse(data))
                    {
                        return(kpair);
                    }
                    else
                    {
                        throw new JSchException("invaid privatekey: " + prvkey);
                    }
                }
            }

            return(kpair);
        }
Ejemplo n.º 19
0
 internal KnownHosts(JSch jsch) : base()
 {
     this.jsch = jsch;
     pool      = new System.Collections.ArrayList();
 }
Ejemplo n.º 20
0
 public static KeyPair genKeyPair(JSch jsch, int type)
 {
     return(genKeyPair(jsch, type, 1024));
 }
Ejemplo n.º 21
0
        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(i<len)
                {
                    if(buf[i]=='B'&& buf[i+1]=='E'&& buf[i+2]=='G'&& buf[i+3]=='I')
                    {
                        i+=6;
                        if(buf[i]=='D'&& buf[i+1]=='S'&& buf[i+2]=='A'){ type=DSS; }
                        else if(buf[i]=='R'&& buf[i+1]=='S'&& buf[i+2]=='A'){ type=RSA; }
                        else if(buf[i]=='S'&& buf[i+1]=='S'&& buf[i+2]=='H')
                        { // FSecure
                            type=UNKNOWN;
                            keytype=FSECURE;
                        }
                        else
                        {
                            //System.out.println("invalid format: "+identity);
                            throw new JSchException("invaid privatekey: "+identity);
                        }
                        i+=3;
                        continue;
                    }
                    if(buf[i]=='C'&& buf[i+1]=='B'&& buf[i+2]=='C'&& buf[i+3]==',')
                    {
                        i+=4;
                        for(int ii=0; ii<iv.Length; ii++)
                        {
                            iv[ii]=(byte)(((a2b(buf[i++])<<4)&0xf0)+
                                (a2b(buf[i++])&0xf));
                        }
                        continue;
                    }
                    if(buf[i]==0x0d &&
                        i+1<buf.Length && buf[i+1]==0x0a)
                    {
                        i++;
                        continue;
                    }
                    if(buf[i]==0x0a && i+1<buf.Length)
                    {
                        if(buf[i+1]==0x0a){ i+=2; break; }
                        if(buf[i+1]==0x0d &&
                            i+2<buf.Length && buf[i+2]==0x0a)
                        {
                            i+=3; break;
                        }
                        bool inheader=false;
                        for(int j=i+1; j<buf.Length; j++)
                        {
                            if(buf[j]==0x0a) break;
                            //if(buf[j]==0x0d) break;
                            if(buf[j]==':'){inheader=true; break;}
                        }
                        if(!inheader)
                        {
                            i++;
                            encrypted=false;    // no passphrase
                            break;
                        }
                    }
                    i++;
                }

                if(type==ERROR)
                {
                    throw new JSchException("invaid privatekey: "+identity);
                }

                int start=i;
                while(i<len)
                {
                    if(buf[i]==0x0a)
                    {
                        bool xd=(buf[i-1]==0x0d);
                        Array.Copy(buf, i+1,
                            buf,
                            i-(xd ? 1 : 0),
                            len-i-1-(xd ? 1 : 0)
                            );
                        if(xd)len--;
                        len--;
                        continue;
                    }
                    if(buf[i]=='-'){  break; }
                    i++;
                }
                encoded_data=Util.fromBase64(buf, start, i-start);

                if(encoded_data.Length>4 &&            // 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<buf.Length; j++)
                            {
                                if(buf[j]==0x0a) break;
                                if(buf[j]==':'){inheader=true; break;}
                            }
                            if(!inheader)
                            {
                                i++;
                                break;
                            }
                        }
                        i++;
                    }
                    if(buf.Length<=i) return;

                    start=i;
                    while(i<len)
                    {
                        if(buf[i]==0x0a)
                        {
                            Array.Copy(buf, i+1, buf, i, len-i-1);
                            len--;
                            continue;
                        }
                        if(buf[i]=='-'){  break; }
                        i++;
                    }
                    publickeyblob=Util.fromBase64(buf, start, i-start);

                    if(type==UNKNOWN)
                    {
                        if(publickeyblob[8]=='d')
                        {
                            type=DSS;
                        }
                        else if(publickeyblob[8]=='r')
                        {
                            type=RSA;
                        }
                    }
                }
                else
                {
                    if(buf[0]!='s'|| buf[1]!='s'|| buf[2]!='h'|| buf[3]!='-') return;
                    i=0;
                    while(i<len){ if(buf[i]==' ')break; i++;} i++;
                    if(i>=len) return;
                    start=i;
                    while(i<len){ if(buf[i]==' ')break; i++;}
                    publickeyblob=Util.fromBase64(buf, start, i-start);
                }

            }
            catch(Exception e)
            {
                Console.WriteLine("Identity: "+e);
                if(e is JSchException) throw (JSchException)e;
                throw new JSchException(e.ToString());
            }
        }
Ejemplo n.º 22
0
 public String getFingerPrint(JSch jsch)
 {
     HASH hash=null;
     try{
     hash=(HASH)Activator.CreateInstance(Type.GetType(jsch.getConfig("md5")));
     }
     catch(Exception e){ Console.Error.WriteLine("getFingerPrint: "+e); }
     return Util.getFingerPrint(hash, key);
 }