Пример #1
0
        private static void SendMyVersion(AbstractSocket stream, SSHConnectionParameter param)
        {
            string cv = SSHUtil.ClientVersionString(param.Protocol);

            if (param.Protocol == SSHProtocol.SSH1)
            {
                cv += param.SSH1VersionEOL;
            }
            else
            {
                cv += "\r\n";
            }
            byte[] data = Encoding.ASCII.GetBytes(cv);
            stream.Write(data, 0, data.Length);
        }
Пример #2
0
        internal override AuthenticationResult Connect(AbstractSocket s)
        {
            _stream = s;

            KeyExchanger kex = new KeyExchanger(this, null);
            if(!kex.SynchronousKexExchange()) {
                _stream.Close();
                return AuthenticationResult.Failure;
            }
            //Step3 user authentication
            ServiceRequest("ssh-userauth");
            _authenticationResult = UserAuth();
            return _authenticationResult;
        }
Пример #3
0
 private static void SendMyVersion(AbstractSocket stream, SSHConnectionParameter param)
 {
     string cv = SSHUtil.ClientVersionString(param.Protocol);
     if(param.Protocol==SSHProtocol.SSH1)
         cv += param.SSH1VersionEOL;
     else
         cv += "\r\n";
     byte[] data = Encoding.ASCII.GetBytes(cv);
     stream.Write(data, 0, data.Length);
 }
Пример #4
0
        private static SSHConnection ConnectMain(SSHConnectionParameter param, ISSHConnectionEventReceiver receiver, ProtocolNegotiationHandler pnh, AbstractSocket s)
        {
            pnh.Wait();

            if(pnh.State!=ReceiverState.Ready) throw new SSHException(pnh.ErrorMessage);

            string sv = pnh.ServerVersion;

            SSHConnection con = null;
            if(param.Protocol==SSHProtocol.SSH1)
                con = new SSH1Connection(param, receiver, sv, SSHUtil.ClientVersionString(param.Protocol));
            else
                con = new SSH2Connection(param, receiver, sv, SSHUtil.ClientVersionString(param.Protocol));

            s.SetHandler(con.PacketBuilder);
            SendMyVersion(s, param);

            if(con.Connect(s)!=AuthenticationResult.Failure)
                return con;
            else {
                s.Close();
                return null;
            }
        }
Пример #5
0
 internal abstract AuthenticationResult Connect(AbstractSocket target);
Пример #6
0
        internal static SSHConnection Connect(SSHConnectionParameter param, ISSHConnectionEventReceiver receiver, ProtocolNegotiationHandler pnh, AbstractSocket s)
        {
            if(param.UserName==null) throw new InvalidOperationException("UserName property is not set");
            if(param.Password==null) throw new InvalidOperationException("Password property is not set");

            return ConnectMain(param, receiver, pnh, s);
        }
Пример #7
0
        internal static SSHConnection Connect(SSHConnectionParameter param, ISSHConnectionEventReceiver receiver, ProtocolNegotiationHandler pnh, AbstractSocket s)
        {
            if (param.UserName == null)
            {
                throw new InvalidOperationException("UserName property is not set");
            }
            if (param.Password == null)
            {
                throw new InvalidOperationException("Password property is not set");
            }

            return(ConnectMain(param, receiver, pnh, s));
        }
Пример #8
0
        private static SSHConnection ConnectMain(SSHConnectionParameter param, ISSHConnectionEventReceiver receiver, ProtocolNegotiationHandler pnh, AbstractSocket s)
        {
            pnh.Wait();

            if (pnh.State != ReceiverState.Ready)
            {
                throw new SSHException(pnh.ErrorMessage);
            }

            string sv = pnh.ServerVersion;

            SSHConnection con = null;

            if (param.Protocol == SSHProtocol.SSH1)
            {
                con = new SSH1Connection(param, receiver, sv, SSHUtil.ClientVersionString(param.Protocol));
            }
            else
            {
                con = new SSH2Connection(param, receiver, sv, SSHUtil.ClientVersionString(param.Protocol));
            }

            s.SetHandler(con.PacketBuilder);
            SendMyVersion(s, param);

            if (con.Connect(s) != AuthenticationResult.Failure)
            {
                return(con);
            }
            else
            {
                s.Close();
                return(null);
            }
        }
Пример #9
0
 internal abstract AuthenticationResult Connect(AbstractSocket target);
Пример #10
0
        public void WriteTo(AbstractSocket strm, Cipher cipher)
        {
            int bodylen = 4+_packetLength;
            byte[] buf = new byte[bodylen + (_mac==null? 0 : _mac.Length)];
            WriteTo(buf, 0, false);

            if(cipher!=null)
                cipher.Encrypt(buf, 0, bodylen, buf, 0);

            if(_mac!=null)
                Array.Copy(_mac, 0, buf, bodylen, _mac.Length);

            try
            {
                strm.Write(buf, 0, buf.Length);
                strm.Flush();
            }
            catch { }
        }
Пример #11
0
        internal override AuthenticationResult Connect(AbstractSocket s)
        {
            _stream = s;

            // Phase2 receives server keys
            ReceiveServerKeys();
            if(_param.KeyCheck!=null && !_param.KeyCheck(_cInfo)) {
                _stream.Close();
                return AuthenticationResult.Failure;
            }

            // Phase3 generates session key
            byte[] session_key = GenerateSessionKey();

            // Phase4 establishes the session key
            try {
                _packetBuilder.SetSignal(false);
                SendSessionKey(session_key);
                InitCipher(session_key);
            }
            finally {
                _packetBuilder.SetSignal(true);
            }
            ReceiveKeyConfirmation();

            // Phase5 user authentication
            SendUserName(_param.UserName);
            if(ReceiveAuthenticationRequirement()==AUTH_REQUIRED) {
                if(_param.AuthenticationType==AuthenticationType.Password) {
                    SendPlainPassword();
                } else if(_param.AuthenticationType==AuthenticationType.PublicKey) {
                    DoRSAChallengeResponse();
                }
                bool auth = ReceiveAuthenticationResult();
                if(!auth) throw new SSHException(Strings.GetString("AuthenticationFailed"));

            }

            _packetBuilder.Handler = new CallbackSSH1PacketHandler(this);
            return AuthenticationResult.Success;
        }
Пример #12
0
        /**
        * writes to encrypted stream
        */
        public void WriteTo(AbstractSocket output, Cipher cipher)
        {
            byte[] image = BuildImage();
            //dumpBA(image);
            byte[] encrypted = new byte[image.Length-4];
            cipher.Encrypt(image, 4, image.Length-4, encrypted, 0); //length field must not be encrypted

            Array.Copy(encrypted, 0, image, 4, encrypted.Length);
            output.Write(image, 0, image.Length);
        }
Пример #13
0
 /**
 * writes to plain stream
 */
 public void WriteTo(AbstractSocket output)
 {
     byte[] image = BuildImage();
     output.Write(image, 0, image.Length);
 }