Example #1
0
            internal void Hash()
            {
                if (hashed)
                {
                    return;
                }
                MAC macsha1 = mykh.getHMACSHA1();

                if (salt == null)
                {
                    Random random = Session.random;
                    lock (random)
                    {
                        salt = new byte[macsha1.getBlockSize()];
                        random.fill(salt, 0, salt.Length);
                    }
                }
                try
                {
                    lock (macsha1)
                    {
                        macsha1.init(salt);
                        byte[] foo = host.getBytes();
                        macsha1.update(foo, 0, foo.Length);
                        hash = new byte[macsha1.getBlockSize()];
                        macsha1.doFinal(hash, 0);
                    }
                }
                catch //(Exception e)
                {
                }
                host = HASH_MAGIC + Encoding.UTF8.GetString(Util.toBase64(salt, 0, salt.Length)) +
                       HASH_DELIM + Encoding.UTF8.GetString(Util.toBase64(hash, 0, hash.Length));
                hashed = true;
            }
Example #2
0
            internal override bool isMatched(string _host)
            {
                if (!hashed)
                {
                    return(base.isMatched(_host));
                }
                MAC macsha1 = mykh.getHMACSHA1();

                try
                {
                    lock (macsha1)
                    {
                        macsha1.init(salt);
                        byte[] foo = _host.getBytes();
                        macsha1.update(foo, 0, foo.Length);
                        byte[] bar = new byte[macsha1.getBlockSize()];
                        macsha1.doFinal(bar, 0);
                        return(Util.array_equals(hash, bar));
                    }
                }
                catch (Exception e)
                {
                    Console.Out.WriteLine(e);
                }
                return(false);
            }
		private void updateKeys(KeyExchange kex) 
		{
			byte[] K=kex.getK();
			byte[] H=kex.getH();
			HASH hash=kex.getHash();

			String[] guess=kex._guess;

			if(session_id==null)
			{
				session_id=new byte[H.Length];
				Tamir.SharpSsh.java.System.arraycopy(H, 0, session_id, 0, H.Length);
			}

			/*
			  Initial IV client to server:     HASH (K || H || "A" || session_id)
			  Initial IV server to client:     HASH (K || H || "B" || session_id)
			  Encryption key client to server: HASH (K || H || "C" || session_id)
			  Encryption key server to client: HASH (K || H || "D" || session_id)
			  Integrity key client to server:  HASH (K || H || "E" || session_id)
			  Integrity key server to client:  HASH (K || H || "F" || session_id)
			*/

			buf.reset();
			buf.putMPInt(K);
			buf.putByte(H);
			buf.putByte((byte)0x41);
			buf.putByte(session_id);
			hash.update(buf.buffer, 0, buf.index);
			IVc2s=hash.digest();

			int j=buf.index-session_id.Length-1;

			buf.buffer[j]++;
			hash.update(buf.buffer, 0, buf.index);
			IVs2c=hash.digest();

			buf.buffer[j]++;
			hash.update(buf.buffer, 0, buf.index);
			Ec2s=hash.digest();

			buf.buffer[j]++;
			hash.update(buf.buffer, 0, buf.index);
			Es2c=hash.digest();

			buf.buffer[j]++;
			hash.update(buf.buffer, 0, buf.index);
			MACc2s=hash.digest();

			buf.buffer[j]++;
			hash.update(buf.buffer, 0, buf.index);
			MACs2c=hash.digest();

			try
			{
				Class c;

				c=Class.forName(getConfig(guess[KeyExchange.PROPOSAL_ENC_ALGS_STOC]));
				s2ccipher=(Cipher)(c.newInstance());
				while(s2ccipher.getBlockSize()>Es2c.Length)
				{
					buf.reset();
					buf.putMPInt(K);
					buf.putByte(H);
					buf.putByte(Es2c);
					hash.update(buf.buffer, 0, buf.index);
					byte[] foo=hash.digest();
					byte[] bar=new byte[Es2c.Length+foo.Length];
					Tamir.SharpSsh.java.System.arraycopy(Es2c, 0, bar, 0, Es2c.Length);
					Tamir.SharpSsh.java.System.arraycopy(foo, 0, bar, Es2c.Length, foo.Length);
					Es2c=bar;
				}
				s2ccipher.init(Cipher.DECRYPT_MODE, Es2c, IVs2c);
				cipher_size=s2ccipher.getIVSize();
				c=Class.forName(getConfig(guess[KeyExchange.PROPOSAL_MAC_ALGS_STOC]));
				s2cmac=(MAC)(c.newInstance());
				s2cmac.init(MACs2c);
				mac_buf=new byte[s2cmac.getBlockSize()];

				c=Class.forName(getConfig(guess[KeyExchange.PROPOSAL_ENC_ALGS_CTOS]));
				c2scipher=(Cipher)(c.newInstance());
				while(c2scipher.getBlockSize()>Ec2s.Length)
				{
					buf.reset();
					buf.putMPInt(K);
					buf.putByte(H);
					buf.putByte(Ec2s);
					hash.update(buf.buffer, 0, buf.index);
					byte[] foo=hash.digest();
					byte[] bar=new byte[Ec2s.Length+foo.Length];
					Tamir.SharpSsh.java.System.arraycopy(Ec2s, 0, bar, 0, Ec2s.Length);
					Tamir.SharpSsh.java.System.arraycopy(foo, 0, bar, Ec2s.Length, foo.Length);
					Ec2s=bar;
				}
				c2scipher.init(Cipher.ENCRYPT_MODE, Ec2s, IVc2s);

				c=Class.forName(getConfig(guess[KeyExchange.PROPOSAL_MAC_ALGS_CTOS]));
				c2smac=(MAC)(c.newInstance());
				c2smac.init(MACc2s);

				if(!guess[KeyExchange.PROPOSAL_COMP_ALGS_CTOS].equals("none"))
				{
					String foo=getConfig(guess[KeyExchange.PROPOSAL_COMP_ALGS_CTOS]);
					if(foo!=null)
					{
						try
						{
							c=Class.forName(foo);
							deflater=(Compression)(c.newInstance());
							int level=6;
							try{ level=Integer.parseInt(getConfig("compression_level"));}
							catch(Exception ee){ }
							deflater.init(Compression.DEFLATER, level);
						}
						catch(Exception ee)
						{
							System.Console.Error.WriteLine(foo+" isn't accessible.");
						}
					}
				}
				else
				{
					if(deflater!=null)
					{
						deflater=null;
					}
				}
				if(!guess[KeyExchange.PROPOSAL_COMP_ALGS_STOC].equals("none"))
				{
					String foo=getConfig(guess[KeyExchange.PROPOSAL_COMP_ALGS_STOC]);
					if(foo!=null)
					{
						try
						{
							c=Class.forName(foo);
							inflater=(Compression)(c.newInstance());
							inflater.init(Compression.INFLATER, 0);
						}
						catch(Exception ee)
						{
							System.Console.Error.WriteLine(foo+" isn't accessible.");
						}
					}
				}
				else
				{
					if(inflater!=null)
					{
						inflater=null;
					}
				}
			}
			catch(Exception e){ System.Console.Error.WriteLine("updatekeys: "+e); }
		}
Example #4
0
        private void updateKeys(KeyExchange kex)
        {
            byte[] K = kex.getK();
            byte[] H = kex.getH();
            HASH hash = kex.getHash();

            string[] guess = kex.m_guess;

            if (m_session_id == null)
            {
                m_session_id = new byte[H.Length];
                Array.Copy(H, 0, m_session_id, 0, H.Length);
            }

            /*
              Initial IV client to server:     HASH (K || H || "A" || session_id)
              Initial IV server to client:     HASH (K || H || "B" || session_id)
              Encryption key client to server: HASH (K || H || "C" || session_id)
              Encryption key server to client: HASH (K || H || "D" || session_id)
              Integrity key client to server:  HASH (K || H || "E" || session_id)
              Integrity key server to client:  HASH (K || H || "F" || session_id)
            */

            m_buf.reset();
            m_buf.putMPInt(K);
            m_buf.putByte(H);
            m_buf.putByte((byte)0x41);
            m_buf.putByte(m_session_id);
            hash.update(m_buf.m_buffer, 0, m_buf.m_index);
            m_IVc2s = hash.digest();

            int j = m_buf.m_index - m_session_id.Length - 1;

            m_buf.m_buffer[j]++;
            hash.update(m_buf.m_buffer, 0, m_buf.m_index);
            m_IVs2c = hash.digest();

            m_buf.m_buffer[j]++;
            hash.update(m_buf.m_buffer, 0, m_buf.m_index);
            m_Ec2s = hash.digest();

            m_buf.m_buffer[j]++;
            hash.update(m_buf.m_buffer, 0, m_buf.m_index);
            m_Es2c = hash.digest();

            m_buf.m_buffer[j]++;
            hash.update(m_buf.m_buffer, 0, m_buf.m_index);
            m_MACc2s = hash.digest();

            m_buf.m_buffer[j]++;
            hash.update(m_buf.m_buffer, 0, m_buf.m_index);
            m_MACs2c = hash.digest();

            try
            {
                Class c;

                c = Class.ForName(getConfig(guess[KeyExchange.PROPOSAL_ENC_ALGS_STOC]));
                m_s2ccipher = (Cipher)(c.Instance());
                while (m_s2ccipher.BlockSize > m_Es2c.Length)
                {
                    m_buf.reset();
                    m_buf.putMPInt(K);
                    m_buf.putByte(H);
                    m_buf.putByte(m_Es2c);
                    hash.update(m_buf.m_buffer, 0, m_buf.m_index);
                    byte[] foo = hash.digest();
                    byte[] bar = new byte[m_Es2c.Length + foo.Length];
                    Array.Copy(m_Es2c, 0, bar, 0, m_Es2c.Length);
                    Array.Copy(foo, 0, bar, m_Es2c.Length, foo.Length);
                    m_Es2c = bar;
                }
                m_s2ccipher.init(jsch.Cipher.DECRYPT_MODE, m_Es2c, m_IVs2c);
                m_cipher_size = m_s2ccipher.IVSize;
                c = Class.ForName(getConfig(guess[KeyExchange.PROPOSAL_MAC_ALGS_STOC]));
                m_s2cmac = (MAC)(c.Instance());
                m_s2cmac.init(m_MACs2c);
                m_mac_buf = new byte[m_s2cmac.BlockSize];

                c = Class.ForName(getConfig(guess[KeyExchange.PROPOSAL_ENC_ALGS_CTOS]));
                m_c2scipher = (Cipher)(c.Instance());
                while (m_c2scipher.BlockSize > m_Ec2s.Length)
                {
                    m_buf.reset();
                    m_buf.putMPInt(K);
                    m_buf.putByte(H);
                    m_buf.putByte(m_Ec2s);
                    hash.update(m_buf.m_buffer, 0, m_buf.m_index);
                    byte[] foo = hash.digest();
                    byte[] bar = new byte[m_Ec2s.Length + foo.Length];
                    Array.Copy(m_Ec2s, 0, bar, 0, m_Ec2s.Length);
                    Array.Copy(foo, 0, bar, m_Ec2s.Length, foo.Length);
                    m_Ec2s = bar;
                }
                m_c2scipher.init(jsch.Cipher.ENCRYPT_MODE, m_Ec2s, m_IVc2s);

                c = Class.ForName(getConfig(guess[KeyExchange.PROPOSAL_MAC_ALGS_CTOS]));
                m_c2smac = (MAC)(c.Instance());
                m_c2smac.init(m_MACc2s);

                if (!guess[KeyExchange.PROPOSAL_COMP_ALGS_CTOS].Equals("none"))
                {
                    string foo = getConfig(guess[KeyExchange.PROPOSAL_COMP_ALGS_CTOS]);
                    if (foo != null)
                    {
                        try
                        {
                            c = Class.ForName(foo);
                            m_deflater = (Compression)(c.Instance());
                            int level = 6;
                            try
                            {
                                level = int.Parse(getConfig("compression_level"));
                            }
                            catch (Exception)
                            { }
                            m_deflater.init(Compression.DEFLATER, level);
                        }
                        catch (Exception)
                        {
                            Console.Error.WriteLine(foo + " isn't accessible.");
                        }
                    }
                }
                else
                {
                    if (m_deflater != null)
                        m_deflater = null;
                }
                if (!guess[KeyExchange.PROPOSAL_COMP_ALGS_STOC].Equals("none"))
                {
                    string foo = getConfig(guess[KeyExchange.PROPOSAL_COMP_ALGS_STOC]);
                    if (foo != null)
                    {
                        try
                        {
                            c = Class.ForName(foo);
                            m_inflater = (Compression)(c.Instance());
                            m_inflater.init(Compression.INFLATER, 0);
                        }
                        catch (Exception)
                        {
                            Console.Error.WriteLine(foo + " isn't accessible.");
                        }
                    }
                }
                else
                {
                    if (m_inflater != null)
                        m_inflater = null;
                }
            }
            catch (Exception e) { Console.Error.WriteLine("updatekeys: " + e); }
        }
Example #5
0
        private void updateKeys(KeyExchange kex)
        {
            byte[] K = kex.getK();
            byte[] H = kex.getH();
            HASH hash = kex.getHash();

            //    string[] guess=kex.guess;

            if (session_id == null)
            {
                session_id = new byte[H.Length];
                Array.Copy(H, 0, session_id, 0, H.Length);
            }

            /*
              Initial IV client to server:     HASH (K || H || "A" || session_id)
              Initial IV server to client:     HASH (K || H || "B" || session_id)
              Encryption key client to server: HASH (K || H || "C" || session_id)
              Encryption key server to client: HASH (K || H || "D" || session_id)
              Integrity key client to server:  HASH (K || H || "E" || session_id)
              Integrity key server to client:  HASH (K || H || "F" || session_id)
            */

            buf.reset();
            buf.putMPInt(K);
            buf.putByte(H);
            buf.putByte((byte)0x41);
            buf.putByte(session_id);
            hash.update(buf.buffer, 0, buf.index);
            IVc2s = hash.digest();

            int j = buf.index - session_id.Length - 1;

            buf.buffer[j]++;
            hash.update(buf.buffer, 0, buf.index);
            IVs2c = hash.digest();

            buf.buffer[j]++;
            hash.update(buf.buffer, 0, buf.index);
            Ec2s = hash.digest();

            buf.buffer[j]++;
            hash.update(buf.buffer, 0, buf.index);
            Es2c = hash.digest();

            buf.buffer[j]++;
            hash.update(buf.buffer, 0, buf.index);
            MACc2s = hash.digest();

            buf.buffer[j]++;
            hash.update(buf.buffer, 0, buf.index);
            MACs2c = hash.digest();

            try
            {
                Type c;
                string method;

                method = guess[KeyExchange.PROPOSAL_ENC_ALGS_STOC];
                c = Type.GetType(getConfig(method));
                s2ccipher = (Cipher)(c.newInstance());
                while (s2ccipher.getBlockSize() > Es2c.Length)
                {
                    buf.reset();
                    buf.putMPInt(K);
                    buf.putByte(H);
                    buf.putByte(Es2c);
                    hash.update(buf.buffer, 0, buf.index);
                    byte[] foo = hash.digest();
                    byte[] bar = new byte[Es2c.Length + foo.Length];
                    Array.Copy(Es2c, 0, bar, 0, Es2c.Length);
                    Array.Copy(foo, 0, bar, Es2c.Length, foo.Length);
                    Es2c = bar;
                }
                s2ccipher.init(Cipher.DECRYPT_MODE, Es2c, IVs2c);
                s2ccipher_size = s2ccipher.getIVSize();

                method = guess[KeyExchange.PROPOSAL_MAC_ALGS_STOC];
                c = Type.GetType(getConfig(method));
                s2cmac = (MAC)(c.newInstance());
                s2cmac.init(MACs2c);
                //mac_buf=new byte[s2cmac.getBlockSize()];
                s2cmac_result1 = new byte[s2cmac.getBlockSize()];
                s2cmac_result2 = new byte[s2cmac.getBlockSize()];

                method = guess[KeyExchange.PROPOSAL_ENC_ALGS_CTOS];
                c = Type.GetType(getConfig(method));
                c2scipher = (Cipher)(c.newInstance());
                while (c2scipher.getBlockSize() > Ec2s.Length)
                {
                    buf.reset();
                    buf.putMPInt(K);
                    buf.putByte(H);
                    buf.putByte(Ec2s);
                    hash.update(buf.buffer, 0, buf.index);
                    byte[] foo = hash.digest();
                    byte[] bar = new byte[Ec2s.Length + foo.Length];
                    Array.Copy(Ec2s, 0, bar, 0, Ec2s.Length);
                    Array.Copy(foo, 0, bar, Ec2s.Length, foo.Length);
                    Ec2s = bar;
                }
                c2scipher.init(Cipher.ENCRYPT_MODE, Ec2s, IVc2s);
                c2scipher_size = c2scipher.getIVSize();

                method = guess[KeyExchange.PROPOSAL_MAC_ALGS_CTOS];
                c = Type.GetType(getConfig(method));
                c2smac = (MAC)(c.newInstance());
                c2smac.init(MACc2s);

                method = guess[KeyExchange.PROPOSAL_COMP_ALGS_CTOS];
                initDeflater(method);

                method = guess[KeyExchange.PROPOSAL_COMP_ALGS_STOC];
                initInflater(method);
            }
            catch (Exception e)
            {
                if (e is JSchException)
                    throw e;
                throw new JSchException(e.ToString(), e);
                //Console.Error.WriteLine("updatekeys: "+e);
            }
        }