Esempio n. 1
0
 private void receive_newkeys(Buffer buf, KeyExchange kex)
 {
     updateKeys(kex);
     in_kex = false;
 }
Esempio n. 2
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);
            }
        }
Esempio n. 3
0
        private void checkHost(string chost, int port, KeyExchange kex)
        {
            string shkc = getConfig("StrictHostKeyChecking");

            if (hostKeyAlias != null)
            {
                chost = hostKeyAlias;
            }

            //Console.Error.WriteLine("shkc: "+shkc);

            byte[] K_S = kex.getHostKey();
            string key_type = kex.getKeyType();
            string key_fprint = kex.getFingerPrint();

            if (hostKeyAlias == null && port != 22)
            {
                chost = ("[" + chost + "]:" + port);
            }

            //    hostkey=new HostKey(chost, K_S);

            HostKeyRepository hkr = jsch.getHostKeyRepository();
            int i = 0;
            lock (hkr)
            {
                i = hkr.check(chost, K_S);
            }

            bool insert = false;

            if ((shkc.Equals("ask") || shkc.Equals("yes")) &&
               i == HostKeyRepository.CHANGED)
            {
                string file = null;
                lock (hkr)
                {
                    file = hkr.getKnownHostsRepositoryID();
                }
                if (file == null) { file = "known_hosts"; }

                bool b = false;

                if (userinfo != null)
                {
                    string message =
            "WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!\n" +
            "IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!\n" +
            "Someone could be eavesdropping on you right now (man-in-the-middle attack)!\n" +
            "It is also possible that the " + key_type + " host key has just been changed.\n" +
            "The fingerprint for the " + key_type + " key sent by the remote host is\n" +
            key_fprint + ".\n" +
            "Please contact your system administrator.\n" +
            "Add correct host key in " + file + " to get rid of this message.";

                    if (shkc.Equals("ask"))
                    {
                        b = userinfo.promptYesNo(message +
                                               "\nDo you want to delete the old key and insert the new key?");
                    }
                    else
                    {  // shkc.Equals("yes")
                        userinfo.showMessage(message);
                    }
                }

                if (!b)
                {
                    throw new JSchException("HostKey has been changed: " + chost);
                }

                lock (hkr)
                {
                    hkr.remove(chost,
                               (key_type.Equals("DSA") ? "ssh-dss" : "ssh-rsa"),
                               null);
                    insert = true;
                }
            }

            if ((shkc.Equals("ask") || shkc.Equals("yes")) &&
               (i != HostKeyRepository.OK) && !insert)
            {
                if (shkc.Equals("yes"))
                {
                    throw new JSchException("reject HostKey: " + host);
                }
                //Console.Error.WriteLine("finger-print: "+key_fprint);
                if (userinfo != null)
                {
                    bool foo = userinfo.promptYesNo(
                "The authenticity of host '" + host + "' can't be established.\n" +
                key_type + " key fingerprint is " + key_fprint + ".\n" +
                "Are you sure you want to continue connecting?"
                                     );
                    if (!foo)
                    {
                        throw new JSchException("reject HostKey: " + host);
                    }
                    insert = true;
                }
                else
                {
                    if (i == HostKeyRepository.NOT_INCLUDED)
                        throw new JSchException("UnknownHostKey: " + host + ". " + key_type + " key fingerprint is " + key_fprint);
                    else
                        throw new JSchException("HostKey has been changed: " + host);
                }
            }

            if (shkc.Equals("no") &&
               HostKeyRepository.NOT_INCLUDED == i)
            {
                insert = true;
            }

            if (i == HostKeyRepository.OK &&
               JSch.getLogger().isEnabled(Logger.INFO))
            {
                JSch.getLogger().log(Logger.INFO,
                                     "Host '" + host + "' is known and mathces the " + key_type + " host key");
            }

            if (insert &&
               JSch.getLogger().isEnabled(Logger.WARN))
            {
                JSch.getLogger().log(Logger.WARN,
                                     "Permanently added '" + host + "' (" + key_type + ") to the list of known hosts.");
            }

            string hkh = getConfig("HashKnownHosts");
            if (hkh.Equals("yes") && (hkr is KnownHosts))
            {
                hostkey = ((KnownHosts)hkr).createHashedHostKey(chost, K_S);
            }
            else
            {
                hostkey = new HostKey(chost, K_S);
            }

            if (insert)
            {
                lock (hkr)
                {
                    hkr.add(hostkey, userinfo);
                }

            }
        }