Example #1
0
		/// <exception cref="System.Exception"></exception>
		internal override void DoRequest(Session session, Channel channel)
		{
			base.DoRequest(session, channel);
			Buffer buf = new Buffer();
			Packet packet = new Packet(buf);
			// byte      SSH_MSG_CHANNEL_REQUEST(98)
			// uint32 recipient channel
			// string request type        // "x11-req"
			// boolean want reply         // 0
			// boolean   single connection
			// string    x11 authentication protocol // "MIT-MAGIC-COOKIE-1".
			// string    x11 authentication cookie
			// uint32    x11 screen number
			packet.Reset();
			buf.PutByte(unchecked((byte)Session.SSH_MSG_CHANNEL_REQUEST));
			buf.PutInt(channel.GetRecipient());
			buf.PutString(Util.Str2byte("x11-req"));
			buf.PutByte(unchecked((byte)(WaitForReply() ? 1 : 0)));
			buf.PutByte(unchecked((byte)0));
			buf.PutString(Util.Str2byte("MIT-MAGIC-COOKIE-1"));
			buf.PutString(ChannelX11.GetFakedCookie(session));
			buf.PutInt(0);
			Write(packet);
			session.x11_forwarding = true;
		}
		/// <exception cref="System.Exception"></exception>
		internal override void DoRequest(Session session, Channel channel)
		{
			base.DoRequest(session, channel);
			Buffer buf = new Buffer();
			Packet packet = new Packet(buf);
			packet.Reset();
			buf.PutByte(unchecked((byte)Session.SSH_MSG_CHANNEL_REQUEST));
			buf.PutInt(channel.GetRecipient());
			buf.PutString(Util.Str2byte("subsystem"));
			buf.PutByte(unchecked((byte)(WaitForReply() ? 1 : 0)));
			buf.PutString(Util.Str2byte(subsystem));
			Write(packet);
		}
Example #3
0
		/// <exception cref="System.Exception"></exception>
		internal override void DoRequest(Session session, Channel channel)
		{
			base.DoRequest(session, channel);
			Buffer buf = new Buffer();
			Packet packet = new Packet(buf);
			// send
			// byte     SSH_MSG_CHANNEL_REQUEST(98)
			// uint32 recipient channel
			// string request type       // "shell"
			// boolean want reply        // 0
			packet.Reset();
			buf.PutByte(unchecked((byte)Session.SSH_MSG_CHANNEL_REQUEST));
			buf.PutInt(channel.GetRecipient());
			buf.PutString(Util.Str2byte("shell"));
			buf.PutByte(unchecked((byte)(WaitForReply() ? 1 : 0)));
			Write(packet);
		}
Example #4
0
		/// <exception cref="System.Exception"></exception>
		internal override void DoRequest(Session session, Channel channel)
		{
			base.DoRequest(session, channel);
			Buffer buf = new Buffer();
			Packet packet = new Packet(buf);
			packet.Reset();
			buf.PutByte(unchecked((byte)Session.SSH_MSG_CHANNEL_REQUEST));
			buf.PutInt(channel.GetRecipient());
			buf.PutString(Util.Str2byte("pty-req"));
			buf.PutByte(unchecked((byte)(WaitForReply() ? 1 : 0)));
			buf.PutString(Util.Str2byte(ttype));
			buf.PutInt(tcol);
			buf.PutInt(trow);
			buf.PutInt(twp);
			buf.PutInt(thp);
			buf.PutString(terminal_mode);
			Write(packet);
		}
		/// <exception cref="System.Exception"></exception>
		internal override void DoRequest(Session session, Channel channel)
		{
			base.DoRequest(session, channel);
			SetReply(false);
			Buffer buf = new Buffer();
			Packet packet = new Packet(buf);
			// byte      SSH_MSG_CHANNEL_REQUEST(98)
			// uint32 recipient channel
			// string request type        // "*****@*****.**"
			// boolean want reply         // 0
			packet.Reset();
			buf.PutByte(unchecked((byte)Session.SSH_MSG_CHANNEL_REQUEST));
			buf.PutInt(channel.GetRecipient());
			buf.PutString(Util.Str2byte("*****@*****.**"));
			buf.PutByte(unchecked((byte)(WaitForReply() ? 1 : 0)));
			Write(packet);
			session.agent_forwarding = true;
		}
 public override void Run()
 {
     try
     {
         SendChannelOpen();
         Buffer  buf      = new Buffer(rmpsize);
         Packet  packet   = new Packet(buf);
         Session _session = GetSession();
         int     i        = 0;
         while (IsConnected() && thread != null && io != null && io.@in != null)
         {
             i = [email protected](buf.buffer, 14, buf.buffer.Length - 14 - Session.buffer_margin);
             if (i <= 0)
             {
                 Eof();
                 break;
             }
             packet.Reset();
             buf.PutByte(unchecked ((byte)Session.SSH_MSG_CHANNEL_DATA));
             buf.PutInt(recipient);
             buf.PutInt(i);
             buf.Skip(i);
             lock (this)
             {
                 if (close)
                 {
                     break;
                 }
                 _session.Write(packet, this, i);
             }
         }
     }
     catch (Exception)
     {
     }
     Disconnect();
 }
Example #7
0
		/// <exception cref="System.Exception"></exception>
		internal override void DoRequest(Session session, Channel channel)
		{
			base.DoRequest(session, channel);
			Buffer buf = new Buffer();
			Packet packet = new Packet(buf);
			//byte      SSH_MSG_CHANNEL_REQUEST
			//uint32    recipient_channel
			//string    "window-change"
			//boolean   FALSE
			//uint32    terminal width, columns
			//uint32    terminal height, rows
			//uint32    terminal width, pixels
			//uint32    terminal height, pixels
			packet.Reset();
			buf.PutByte(unchecked((byte)Session.SSH_MSG_CHANNEL_REQUEST));
			buf.PutInt(channel.GetRecipient());
			buf.PutString(Util.Str2byte("window-change"));
			buf.PutByte(unchecked((byte)(WaitForReply() ? 1 : 0)));
			buf.PutInt(width_columns);
			buf.PutInt(height_rows);
			buf.PutInt(width_pixels);
			buf.PutInt(height_pixels);
			Write(packet);
		}
Example #8
0
 //System.err.println("Channel.eof");
 //e.printStackTrace();
 internal virtual void Close()
 {
     if (close)
     {
         return;
     }
     close     = true;
     eof_local = eof_remote = true;
     try
     {
         Buffer buf    = new Buffer(100);
         Packet packet = new Packet(buf);
         packet.Reset();
         buf.PutByte(unchecked ((byte)Session.SSH_MSG_CHANNEL_CLOSE));
         buf.PutInt(GetRecipient());
         lock (this)
         {
             GetSession().Write(packet);
         }
     }
     catch (Exception)
     {
     }
 }
Example #9
0
		/// <exception cref="NSch.JSchException"></exception>
		public virtual void Connect(int connectTimeout)
		{
			Session _session = GetSession();
			if (!_session.IsConnected())
			{
				throw new JSchException("session is down");
			}
			this.connectTimeout = connectTimeout;
			try
			{
				Buffer buf = new Buffer(100);
				Packet packet = new Packet(buf);
				// send
				// byte   SSH_MSG_CHANNEL_OPEN(90)
				// string channel type         //
				// uint32 sender channel       // 0
				// uint32 initial window size  // 0x100000(65536)
				// uint32 maxmum packet size   // 0x4000(16384)
				packet.Reset();
				buf.PutByte(unchecked((byte)90));
				buf.PutString(this.type);
				buf.PutInt(this.id);
				buf.PutInt(this.lwsize);
				buf.PutInt(this.lmpsize);
				_session.Write(packet);
				int retry = 1000;
				long start = Runtime.CurrentTimeMillis();
				long timeout = connectTimeout;
				while (this.GetRecipient() == -1 && _session.IsConnected() && retry > 0)
				{
					if (timeout > 0L)
					{
						if ((Runtime.CurrentTimeMillis() - start) > timeout)
						{
							retry = 0;
							continue;
						}
					}
					try
					{
						Sharpen.Thread.Sleep(50);
					}
					catch (Exception)
					{
					}
					retry--;
				}
				if (!_session.IsConnected())
				{
					throw new JSchException("session is down");
				}
				if (retry == 0)
				{
					throw new JSchException("channel is not opened.");
				}
				if (this.IsClosed())
				{
					throw new JSchException("channel is not opened.");
				}
				connected = true;
				Start();
			}
			catch (Exception e)
			{
				connected = false;
				Disconnect();
				if (e is JSchException)
				{
					throw (JSchException)e;
				}
				throw new JSchException(e.ToString(), e);
			}
		}
Example #10
0
		protected internal virtual Packet GenChannelOpenPacket()
		{
			Buffer buf = new Buffer(100);
			Packet packet = new Packet(buf);
			// byte   SSH_MSG_CHANNEL_OPEN(90)
			// string channel type         //
			// uint32 sender channel       // 0
			// uint32 initial window size  // 0x100000(65536)
			// uint32 maxmum packet size   // 0x4000(16384)
			packet.Reset();
			buf.PutByte(unchecked((byte)90));
			buf.PutString(this.type);
			buf.PutInt(this.id);
			buf.PutInt(this.lwsize);
			buf.PutInt(this.lmpsize);
			return packet;
		}
Example #11
0
		/// <exception cref="System.Exception"></exception>
		public virtual void SendKeepAliveMsg()
		{
			Buffer buf = new Buffer();
			Packet packet = new Packet(buf);
			packet.Reset();
			buf.PutByte(unchecked((byte)SSH_MSG_GLOBAL_REQUEST));
			buf.PutString(keepalivemsg);
			buf.PutByte(unchecked((byte)1));
			Write(packet);
		}
Example #12
0
		public override void Run()
		{
			try
			{
				SendChannelOpen();
				Buffer buf = new Buffer(rmpsize);
				Packet packet = new Packet(buf);
				Session _session = GetSession();
				int i = 0;
				while (IsConnected() && thread != null && io != null && io.@in != null)
				{
					i = [email protected](buf.buffer, 14, buf.buffer.Length - 14 - Session.buffer_margin);
					if (i <= 0)
					{
						Eof();
						break;
					}
					packet.Reset();
					buf.PutByte(unchecked((byte)Session.SSH_MSG_CHANNEL_DATA));
					buf.PutInt(recipient);
					buf.PutInt(i);
					buf.Skip(i);
					lock (this)
					{
						if (close)
						{
							break;
						}
						_session.Write(packet, this, i);
					}
				}
			}
			catch (Exception)
			{
			}
			Disconnect();
		}
Example #13
0
		/// <exception cref="NSch.JSchException"></exception>
		private void SetPortForwarding(string bind_address, int rport)
		{
			lock (grr)
			{
				Buffer buf = new Buffer(100);
				// ??
				Packet packet = new Packet(buf);
				string address_to_bind = ChannelForwardedTCPIP.Normalize(bind_address);
				try
				{
					// byte SSH_MSG_GLOBAL_REQUEST 80
					// string "tcpip-forward"
					// boolean want_reply
					// string  address_to_bind
					// uint32  port number to bind
					packet.Reset();
					buf.PutByte(unchecked((byte)SSH_MSG_GLOBAL_REQUEST));
					buf.PutString(Util.Str2byte("tcpip-forward"));
					//      buf.putByte((byte)0);
					buf.PutByte(unchecked((byte)1));
					buf.PutString(Util.Str2byte(address_to_bind));
					buf.PutInt(rport);
					Write(packet);
				}
				catch (Exception e)
				{
					if (e is Exception)
					{
						throw new JSchException(e.ToString(), (Exception)e);
					}
					throw new JSchException(e.ToString());
				}
				grr.SetThread(Sharpen.Thread.CurrentThread());
				try
				{
					Sharpen.Thread.Sleep(10000);
				}
				catch (Exception)
				{
				}
				int reply = grr.GetReply();
				grr.SetThread(null);
				if (reply == 0)
				{
					throw new JSchException("remote port forwarding failed for listen port " + rport);
				}
			}
		}
		public override void Run()
		{
			Buffer buf = new Buffer(rmpsize);
			Packet packet = new Packet(buf);
			int i = 0;
			try
			{
				Session _session = GetSession();
				while (IsConnected() && thread != null && io != null && io.@in != null)
				{
					i = [email protected](buf.buffer, 14, buf.buffer.Length - 14 - 32 - 20);
					// padding and mac
					if (i <= 0)
					{
						Eof();
						break;
					}
					if (close)
					{
						break;
					}
					packet.Reset();
					buf.PutByte(unchecked((byte)Session.SSH_MSG_CHANNEL_DATA));
					buf.PutInt(recipient);
					buf.PutInt(i);
					buf.Skip(i);
					_session.Write(packet, this, i);
				}
			}
			catch (Exception)
			{
			}
			Disconnect();
		}
        public override void Run()
        {
            try
            {
                if (lport == -1)
                {
                    Type c = Sharpen.Runtime.GetType(target);
                    daemon = (ForwardedTCPIPDaemon)System.Activator.CreateInstance(c);
                    PipedOutputStream @out = new PipedOutputStream();
                    io.SetInputStream(new Channel.PassiveInputStream(this, @out, 32 * 1024), false);
                    daemon.SetChannel(this, GetInputStream(), @out);
                    object[] foo = GetPort(GetSession(), rport);
                    daemon.SetArg((object[])foo[3]);
                    new Sharpen.Thread(daemon).Start();
                }
                else
                {
                    socket = (factory == null) ? Util.CreateSocket(target, lport, TIMEOUT) : factory.
                             CreateSocket(target, lport);
                    socket.NoDelay = true;
                    io.SetInputStream(socket.GetInputStream());
                    io.SetOutputStream(socket.GetOutputStream());
                }
                SendOpenConfirmation();
            }
            catch (Exception)
            {
                SendOpenFailure(SSH_OPEN_ADMINISTRATIVELY_PROHIBITED);
                close = true;
                Disconnect();
                return;
            }
            thread = Sharpen.Thread.CurrentThread();
            Buffer buf    = new Buffer(rmpsize);
            Packet packet = new Packet(buf);
            int    i      = 0;

            try
            {
                Session _session = GetSession();
                while (thread != null && io != null && io.@in != null)
                {
                    i = [email protected](buf.buffer, 14, buf.buffer.Length - 14 - Session.buffer_margin);
                    if (i <= 0)
                    {
                        Eof();
                        break;
                    }
                    packet.Reset();
                    buf.PutByte(unchecked ((byte)Session.SSH_MSG_CHANNEL_DATA));
                    buf.PutInt(recipient);
                    buf.PutInt(i);
                    buf.Skip(i);
                    lock (this)
                    {
                        if (close)
                        {
                            break;
                        }
                        _session.Write(packet, this, i);
                    }
                }
            }
            catch (Exception)
            {
            }
            //System.err.println(e);
            //thread=null;
            //eof();
            Disconnect();
        }
Example #16
0
		public override void Run()
		{
			//System.err.println(this+":run >");
			Buffer buf = new Buffer(rmpsize);
			Packet packet = new Packet(buf);
			int i = -1;
			try
			{
				while (IsConnected() && thread != null && io != null && io.@in != null)
				{
					i = [email protected](buf.buffer, 14, buf.buffer.Length - 14 - Session.buffer_margin);
					if (i == 0)
					{
						continue;
					}
					if (i == -1)
					{
						Eof();
						break;
					}
					if (close)
					{
						break;
					}
					//System.out.println("write: "+i);
					packet.Reset();
					buf.PutByte(unchecked((byte)Session.SSH_MSG_CHANNEL_DATA));
					buf.PutInt(recipient);
					buf.PutInt(i);
					buf.Skip(i);
					GetSession().Write(packet, this, i);
				}
			}
			catch (Exception)
			{
			}
			//System.err.println("# ChannelExec.run");
			//e.printStackTrace();
			Sharpen.Thread _thread = thread;
			if (_thread != null)
			{
				lock (_thread)
				{
					Sharpen.Runtime.NotifyAll(_thread);
				}
			}
			thread = null;
		}
Example #17
0
		/// <exception cref="System.Exception"></exception>
		public override void Init(Session session, byte[] V_S, byte[] V_C, byte[] I_S, byte
			[] I_C)
		{
			throw new NotSupportedException (); // The crypto for this method is unusably slow
			this.session = session;
			this.V_S = V_S;
			this.V_C = V_C;
			this.I_S = I_S;
			this.I_C = I_C;
			try
			{
				Type c = Sharpen.Runtime.GetType(session.GetConfig("sha-1"));
				sha = (HASH)(System.Activator.CreateInstance(c));
				sha.Init();
			}
			catch (Exception ex)
			{
				System.Console.Error.WriteLine(e);
			}
			buf = new Buffer();
			packet = new Packet(buf);
			try
			{
				Type c = Sharpen.Runtime.GetType(session.GetConfig("dh"));
				dh = (NSch.DH)(System.Activator.CreateInstance(c));
				dh.Init();
			}
			catch (Exception ex)
			{
				//System.err.println(e);
				throw;
			}
			dh.SetP(p);
			dh.SetG(g);
			// The client responds with:
			// byte  SSH_MSG_KEXDH_INIT(30)
			// mpint e <- g^x mod p
			//         x is a random number (1 < x < (p-1)/2)
			e = dh.GetE();
			packet.Reset();
			buf.PutByte(unchecked((byte)SSH_MSG_KEXDH_INIT));
			buf.PutMPInt(e);
			if (V_S == null)
			{
				// This is a really ugly hack for Session.checkKexes ;-(
				return;
			}
			session.Write(packet);
			if (JSch.GetLogger().IsEnabled(Logger.INFO))
			{
				JSch.GetLogger().Log(Logger.INFO, "SSH_MSG_KEXDH_INIT sent");
				JSch.GetLogger().Log(Logger.INFO, "expecting SSH_MSG_KEXDH_REPLY");
			}
			state = SSH_MSG_KEXDH_REPLY;
		}
Example #18
0
		public override void Run()
		{
			try
			{
				socket = Util.CreateSocket(host, port, TIMEOUT);
				socket.NoDelay = true;
				io = new IO();
				io.SetInputStream(socket.GetInputStream());
				io.SetOutputStream(socket.GetOutputStream());
				SendOpenConfirmation();
			}
			catch (Exception)
			{
				SendOpenFailure(SSH_OPEN_ADMINISTRATIVELY_PROHIBITED);
				close = true;
				Disconnect();
				return;
			}
			thread = Sharpen.Thread.CurrentThread();
			Buffer buf = new Buffer(rmpsize);
			Packet packet = new Packet(buf);
			int i = 0;
			try
			{
				while (thread != null && io != null && io.@in != null)
				{
					i = [email protected](buf.buffer, 14, buf.buffer.Length - 14 - 32 - 20);
					// padding and mac
					if (i <= 0)
					{
						Eof();
						break;
					}
					if (close)
					{
						break;
					}
					packet.Reset();
					buf.PutByte(unchecked((byte)Session.SSH_MSG_CHANNEL_DATA));
					buf.PutInt(recipient);
					buf.PutInt(i);
					buf.Skip(i);
					GetSession().Write(packet, this, i);
				}
			}
			catch (Exception)
			{
			}
			//System.err.println(e);
			Disconnect();
		}
		public override void Run()
		{
			try
			{
				if (lport == -1)
				{
					Type c = Sharpen.Runtime.GetType(target);
					daemon = (ForwardedTCPIPDaemon)System.Activator.CreateInstance(c);
					PipedOutputStream @out = new PipedOutputStream();
					io.SetInputStream(new Channel.PassiveInputStream(this, @out, 32 * 1024), false);
					daemon.SetChannel(this, GetInputStream(), @out);
					object[] foo = GetPort(GetSession(), rport);
					daemon.SetArg((object[])foo[3]);
					new Sharpen.Thread(daemon).Start();
				}
				else
				{
					socket = (factory == null) ? Util.CreateSocket(target, lport, TIMEOUT) : factory.
						CreateSocket(target, lport);
					socket.NoDelay = true;
					io.SetInputStream(socket.GetInputStream());
					io.SetOutputStream(socket.GetOutputStream());
				}
				SendOpenConfirmation();
			}
			catch (Exception)
			{
				SendOpenFailure(SSH_OPEN_ADMINISTRATIVELY_PROHIBITED);
				close = true;
				Disconnect();
				return;
			}
			thread = Sharpen.Thread.CurrentThread();
			Buffer buf = new Buffer(rmpsize);
			Packet packet = new Packet(buf);
			int i = 0;
			try
			{
				while (thread != null && io != null && io.@in != null)
				{
					i = [email protected](buf.buffer, 14, buf.buffer.Length - 14 - 32 - 20);
					// padding and mac
					if (i <= 0)
					{
						Eof();
						break;
					}
					packet.Reset();
					if (close)
					{
						break;
					}
					buf.PutByte(unchecked((byte)Session.SSH_MSG_CHANNEL_DATA));
					buf.PutInt(recipient);
					buf.PutInt(i);
					buf.Skip(i);
					GetSession().Write(packet, this, i);
				}
			}
			catch (Exception)
			{
			}
			//System.err.println(e);
			//thread=null;
			//eof();
			Disconnect();
		}
		internal static void DelPort(Session session, string address_to_bind, int rport)
		{
			lock (pool)
			{
				object[] foo = null;
				for (int i = 0; i < pool.Count; i++)
				{
					object[] bar = (object[])(pool[i]);
					if (bar[0] != session)
					{
						continue;
					}
					if (((int)bar[1]) != rport)
					{
						continue;
					}
					foo = bar;
					break;
				}
				if (foo == null)
				{
					return;
				}
				pool.RemoveElement(foo);
				if (address_to_bind == null)
				{
					address_to_bind = (string)foo[4];
				}
				if (address_to_bind == null)
				{
					address_to_bind = "0.0.0.0";
				}
			}
			Buffer buf = new Buffer(100);
			// ??
			Packet packet = new Packet(buf);
			try
			{
				// byte SSH_MSG_GLOBAL_REQUEST 80
				// string "cancel-tcpip-forward"
				// boolean want_reply
				// string  address_to_bind (e.g. "127.0.0.1")
				// uint32  port number to bind
				packet.Reset();
				buf.PutByte(unchecked((byte)80));
				buf.PutString(Util.Str2byte("cancel-tcpip-forward"));
				buf.PutByte(unchecked((byte)0));
				buf.PutString(Util.Str2byte(address_to_bind));
				buf.PutInt(rport);
				session.Write(packet);
			}
			catch (Exception)
			{
			}
		}
        /// <exception cref="System.Exception"></exception>
        public override bool Next(Buffer _buf)
        {
            int i;
            int j;

            switch (state)
            {
            case SSH_MSG_KEX_DH_GEX_GROUP:
            {
                // byte  SSH_MSG_KEX_DH_GEX_GROUP(31)
                // mpint p, safe prime
                // mpint g, generator for subgroup in GF (p)
                _buf.GetInt();
                _buf.GetByte();
                j = _buf.GetByte();
                if (j != SSH_MSG_KEX_DH_GEX_GROUP)
                {
                    System.Console.Error.WriteLine("type: must be SSH_MSG_KEX_DH_GEX_GROUP " + j);
                    return(false);
                }
                p = _buf.GetMPInt();
                g = _buf.GetMPInt();
                dh.SetP(p);
                dh.SetG(g);
                // The client responds with:
                // byte  SSH_MSG_KEX_DH_GEX_INIT(32)
                // mpint e <- g^x mod p
                //         x is a random number (1 < x < (p-1)/2)
                e = dh.GetE();
                packet.Reset();
                buf.PutByte(unchecked ((byte)SSH_MSG_KEX_DH_GEX_INIT));
                buf.PutMPInt(e);
                session.Write(packet);
                if (JSch.GetLogger().IsEnabled(Logger.INFO))
                {
                    JSch.GetLogger().Log(Logger.INFO, "SSH_MSG_KEX_DH_GEX_INIT sent");
                    JSch.GetLogger().Log(Logger.INFO, "expecting SSH_MSG_KEX_DH_GEX_REPLY");
                }
                state = SSH_MSG_KEX_DH_GEX_REPLY;
                return(true);
            }

            case SSH_MSG_KEX_DH_GEX_REPLY:
            {
                //break;
                // The server responds with:
                // byte      SSH_MSG_KEX_DH_GEX_REPLY(33)
                // string    server public host key and certificates (K_S)
                // mpint     f
                // string    signature of H
                j = _buf.GetInt();
                j = _buf.GetByte();
                j = _buf.GetByte();
                if (j != SSH_MSG_KEX_DH_GEX_REPLY)
                {
                    System.Console.Error.WriteLine("type: must be SSH_MSG_KEX_DH_GEX_REPLY " + j);
                    return(false);
                }
                K_S = _buf.GetString();
                // K_S is server_key_blob, which includes ....
                // string ssh-dss
                // impint p of dsa
                // impint q of dsa
                // impint g of dsa
                // impint pub_key of dsa
                //System.err.print("K_S: "); dump(K_S, 0, K_S.length);
                byte[] f        = _buf.GetMPInt();
                byte[] sig_of_H = _buf.GetString();
                dh.SetF(f);
                K = dh.GetK();
                //The hash H is computed as the HASH hash of the concatenation of the
                //following:
                // string    V_C, the client's version string (CR and NL excluded)
                // string    V_S, the server's version string (CR and NL excluded)
                // string    I_C, the payload of the client's SSH_MSG_KEXINIT
                // string    I_S, the payload of the server's SSH_MSG_KEXINIT
                // string    K_S, the host key
                // uint32    min, minimal size in bits of an acceptable group
                // uint32   n, preferred size in bits of the group the server should send
                // uint32    max, maximal size in bits of an acceptable group
                // mpint     p, safe prime
                // mpint     g, generator for subgroup
                // mpint     e, exchange value sent by the client
                // mpint     f, exchange value sent by the server
                // mpint     K, the shared secret
                // This value is called the exchange hash, and it is used to authenti-
                // cate the key exchange.
                buf.Reset();
                buf.PutString(V_C);
                buf.PutString(V_S);
                buf.PutString(I_C);
                buf.PutString(I_S);
                buf.PutString(K_S);
                buf.PutInt(min);
                buf.PutInt(preferred);
                buf.PutInt(max);
                buf.PutMPInt(p);
                buf.PutMPInt(g);
                buf.PutMPInt(e);
                buf.PutMPInt(f);
                buf.PutMPInt(K);
                byte[] foo = new byte[buf.GetLength()];
                buf.GetByte(foo);
                sha.Update(foo, 0, foo.Length);
                H = sha.Digest();
                // System.err.print("H -> "); dump(H, 0, H.length);
                i = 0;
                j = 0;
                j = ((K_S[i++] << 24) & unchecked ((int)(0xff000000))) | ((K_S[i++] << 16) & unchecked (
                                                                              (int)(0x00ff0000))) | ((K_S[i++] << 8) & unchecked ((int)(0x0000ff00))) | ((K_S[i
                                                                                                                                                              ++]) & unchecked ((int)(0x000000ff)));
                string alg = Util.Byte2str(K_S, i, j);
                i += j;
                bool result = false;
                if (alg.Equals("ssh-rsa"))
                {
                    byte[] tmp;
                    byte[] ee;
                    byte[] n;
                    type = RSA;
                    j    = ((K_S[i++] << 24) & unchecked ((int)(0xff000000))) | ((K_S[i++] << 16) & unchecked (
                                                                                     (int)(0x00ff0000))) | ((K_S[i++] << 8) & unchecked ((int)(0x0000ff00))) | ((K_S[i
                                                                                                                                                                     ++]) & unchecked ((int)(0x000000ff)));
                    tmp = new byte[j];
                    System.Array.Copy(K_S, i, tmp, 0, j);
                    i += j;
                    ee = tmp;
                    j  = ((K_S[i++] << 24) & unchecked ((int)(0xff000000))) | ((K_S[i++] << 16) & unchecked (
                                                                                   (int)(0x00ff0000))) | ((K_S[i++] << 8) & unchecked ((int)(0x0000ff00))) | ((K_S[i
                                                                                                                                                                   ++]) & unchecked ((int)(0x000000ff)));
                    tmp = new byte[j];
                    System.Array.Copy(K_S, i, tmp, 0, j);
                    i += j;
                    n  = tmp;
                    //	SignatureRSA sig=new SignatureRSA();
                    //	sig.init();
                    NSch.SignatureRSA sig = null;
                    try
                    {
                        Type c = Sharpen.Runtime.GetType(session.GetConfig("signature.rsa"));
                        sig = (NSch.SignatureRSA)(System.Activator.CreateInstance(c));
                        sig.Init();
                    }
                    catch (Exception ex)
                    {
                        System.Console.Error.WriteLine(ex);
                    }
                    sig.SetPubKey(ee, n);
                    sig.Update(H);
                    result = sig.Verify(sig_of_H);
                    if (JSch.GetLogger().IsEnabled(Logger.INFO))
                    {
                        JSch.GetLogger().Log(Logger.INFO, "ssh_rsa_verify: signature " + result);
                    }
                }
                else
                {
                    if (alg.Equals("ssh-dss"))
                    {
                        byte[] q = null;
                        byte[] tmp;
                        type = DSS;
                        j    = ((K_S[i++] << 24) & unchecked ((int)(0xff000000))) | ((K_S[i++] << 16) & unchecked (
                                                                                         (int)(0x00ff0000))) | ((K_S[i++] << 8) & unchecked ((int)(0x0000ff00))) | ((K_S[i
                                                                                                                                                                         ++]) & unchecked ((int)(0x000000ff)));
                        tmp = new byte[j];
                        System.Array.Copy(K_S, i, tmp, 0, j);
                        i += j;
                        p  = tmp;
                        j  = ((K_S[i++] << 24) & unchecked ((int)(0xff000000))) | ((K_S[i++] << 16) & unchecked (
                                                                                       (int)(0x00ff0000))) | ((K_S[i++] << 8) & unchecked ((int)(0x0000ff00))) | ((K_S[i
                                                                                                                                                                       ++]) & unchecked ((int)(0x000000ff)));
                        tmp = new byte[j];
                        System.Array.Copy(K_S, i, tmp, 0, j);
                        i += j;
                        q  = tmp;
                        j  = ((K_S[i++] << 24) & unchecked ((int)(0xff000000))) | ((K_S[i++] << 16) & unchecked (
                                                                                       (int)(0x00ff0000))) | ((K_S[i++] << 8) & unchecked ((int)(0x0000ff00))) | ((K_S[i
                                                                                                                                                                       ++]) & unchecked ((int)(0x000000ff)));
                        tmp = new byte[j];
                        System.Array.Copy(K_S, i, tmp, 0, j);
                        i += j;
                        g  = tmp;
                        j  = ((K_S[i++] << 24) & unchecked ((int)(0xff000000))) | ((K_S[i++] << 16) & unchecked (
                                                                                       (int)(0x00ff0000))) | ((K_S[i++] << 8) & unchecked ((int)(0x0000ff00))) | ((K_S[i
                                                                                                                                                                       ++]) & unchecked ((int)(0x000000ff)));
                        tmp = new byte[j];
                        System.Array.Copy(K_S, i, tmp, 0, j);
                        i += j;
                        f  = tmp;
                        //	SignatureDSA sig=new SignatureDSA();
                        //	sig.init();
                        NSch.SignatureDSA sig = null;
                        try
                        {
                            Type c = Sharpen.Runtime.GetType(session.GetConfig("signature.dss"));
                            sig = (NSch.SignatureDSA)(System.Activator.CreateInstance(c));
                            sig.Init();
                        }
                        catch (Exception ex)
                        {
                            System.Console.Error.WriteLine(ex);
                        }
                        sig.SetPubKey(f, p, q, g);
                        sig.Update(H);
                        result = sig.Verify(sig_of_H);
                        if (JSch.GetLogger().IsEnabled(Logger.INFO))
                        {
                            JSch.GetLogger().Log(Logger.INFO, "ssh_dss_verify: signature " + result);
                        }
                    }
                    else
                    {
                        System.Console.Error.WriteLine("unknown alg");
                    }
                }
                state = STATE_END;
                return(result);
            }
            }
            return(false);
        }
		/// <exception cref="NSch.JSchException"></exception>
		public override void Connect()
		{
			try
			{
				Session _session = GetSession();
				if (!_session.IsConnected())
				{
					throw new JSchException("session is down");
				}
				Buffer buf = new Buffer(150);
				Packet packet = new Packet(buf);
				// send
				// byte   SSH_MSG_CHANNEL_OPEN(90)
				// string channel type         //
				// uint32 sender channel       // 0
				// uint32 initial window size  // 0x100000(65536)
				// uint32 maxmum packet size   // 0x4000(16384)
				packet.Reset();
				buf.PutByte(unchecked((byte)90));
				buf.PutString(Util.Str2byte("direct-tcpip"));
				buf.PutInt(id);
				buf.PutInt(lwsize);
				buf.PutInt(lmpsize);
				buf.PutString(Util.Str2byte(host));
				buf.PutInt(port);
				buf.PutString(Util.Str2byte(originator_IP_address));
				buf.PutInt(originator_port);
				_session.Write(packet);
				int retry = 1000;
				try
				{
					while (this.GetRecipient() == -1 && _session.IsConnected() && retry > 0 && !eof_remote
						)
					{
						//Thread.sleep(500);
						Sharpen.Thread.Sleep(50);
						retry--;
					}
				}
				catch (Exception)
				{
				}
				if (!_session.IsConnected())
				{
					throw new JSchException("session is down");
				}
				if (retry == 0 || this.eof_remote)
				{
					throw new JSchException("channel is not opened.");
				}
				connected = true;
				if (io.@in != null)
				{
					thread = new Sharpen.Thread(this);
					thread.SetName("DirectTCPIP thread " + _session.GetHost());
					if (_session.daemon_thread)
					{
						thread.SetDaemon(_session.daemon_thread);
					}
					thread.Start();
				}
			}
			catch (Exception e)
			{
				io.Close();
				io = null;
				Channel.Del(this);
				if (e is JSchException)
				{
					throw (JSchException)e;
				}
			}
		}
Example #23
0
		//  static int min=512;
		//  static int preferred=1024;
		//  static int max=2000;
		//  com.jcraft.jsch.DH dh;
		//private byte[] f;
		/// <exception cref="System.Exception"></exception>
		public override void Init(Session session, byte[] V_S, byte[] V_C, byte[] I_S, byte
			[] I_C)
		{
			this.session = session;
			this.V_S = V_S;
			this.V_C = V_C;
			this.I_S = I_S;
			this.I_C = I_C;
			try
			{
				Type c = Sharpen.Runtime.GetType(session.GetConfig("sha-1"));
				sha = (HASH)(System.Activator.CreateInstance(c));
				sha.Init();
			}
			catch (Exception e)
			{
				System.Console.Error.WriteLine(e);
			}
			buf = new Buffer();
			packet = new Packet(buf);
			try
			{
				Type c = Sharpen.Runtime.GetType(session.GetConfig("dh"));
				dh = (NSch.DH)(System.Activator.CreateInstance(c));
				dh.Init();
			}
			catch (Exception e)
			{
				//      System.err.println(e);
				throw;
			}
			packet.Reset();
			buf.PutByte(unchecked((byte)SSH_MSG_KEX_DH_GEX_REQUEST));
			buf.PutInt(min);
			buf.PutInt(preferred);
			buf.PutInt(max);
			session.Write(packet);
			if (JSch.GetLogger().IsEnabled(Logger.INFO))
			{
				JSch.GetLogger().Log(Logger.INFO, "SSH_MSG_KEX_DH_GEX_REQUEST(" + min + "<" + preferred
					 + "<" + max + ") sent");
				JSch.GetLogger().Log(Logger.INFO, "expecting SSH_MSG_KEX_DH_GEX_GROUP");
			}
			state = SSH_MSG_KEX_DH_GEX_GROUP;
		}
Example #24
0
 /// <exception cref="NSch.JSchException"></exception>
 public override void Connect()
 {
     try
     {
         Session _session = GetSession();
         if (!_session.IsConnected())
         {
             throw new JSchException("session is down");
         }
         Buffer buf    = new Buffer(150);
         Packet packet = new Packet(buf);
         // send
         // byte   SSH_MSG_CHANNEL_OPEN(90)
         // string channel type         //
         // uint32 sender channel       // 0
         // uint32 initial window size  // 0x100000(65536)
         // uint32 maxmum packet size   // 0x4000(16384)
         packet.Reset();
         buf.PutByte(unchecked ((byte)90));
         buf.PutString(Util.Str2byte("direct-tcpip"));
         buf.PutInt(id);
         buf.PutInt(lwsize);
         buf.PutInt(lmpsize);
         buf.PutString(Util.Str2byte(host));
         buf.PutInt(port);
         buf.PutString(Util.Str2byte(originator_IP_address));
         buf.PutInt(originator_port);
         _session.Write(packet);
         int retry = 1000;
         try
         {
             while (this.GetRecipient() == -1 && _session.IsConnected() && retry > 0 && !eof_remote
                    )
             {
                 //Thread.sleep(500);
                 Sharpen.Thread.Sleep(50);
                 retry--;
             }
         }
         catch (Exception)
         {
         }
         if (!_session.IsConnected())
         {
             throw new JSchException("session is down");
         }
         if (retry == 0 || this.eof_remote)
         {
             throw new JSchException("channel is not opened.");
         }
         connected = true;
         if (io.@in != null)
         {
             thread = new Sharpen.Thread(this);
             thread.SetName("DirectTCPIP thread " + _session.GetHost());
             if (_session.daemon_thread)
             {
                 thread.SetDaemon(_session.daemon_thread);
             }
             thread.Start();
         }
     }
     catch (Exception e)
     {
         io.Close();
         io = null;
         Channel.Del(this);
         if (e is JSchException)
         {
             throw (JSchException)e;
         }
     }
 }
Example #25
0
		protected internal override Packet GenChannelOpenPacket()
		{
			Buffer buf = new Buffer(150);
			Packet packet = new Packet(buf);
			// byte   SSH_MSG_CHANNEL_OPEN(90)
			// string channel type         //
			// uint32 sender channel       // 0
			// uint32 initial window size  // 0x100000(65536)
			// uint32 maxmum packet size   // 0x4000(16384)
			packet.Reset();
			buf.PutByte(unchecked((byte)90));
			buf.PutString(this.type);
			buf.PutInt(id);
			buf.PutInt(lwsize);
			buf.PutInt(lmpsize);
			buf.PutString(Util.Str2byte(host));
			buf.PutInt(port);
			buf.PutString(Util.Str2byte(originator_IP_address));
			buf.PutInt(originator_port);
			return packet;
		}
Example #26
0
		//System.err.println("Channel.eof");
		//e.printStackTrace();
		internal virtual void Close()
		{
			if (close)
			{
				return;
			}
			close = true;
			eof_local = eof_remote = true;
			try
			{
				Buffer buf = new Buffer(100);
				Packet packet = new Packet(buf);
				packet.Reset();
				buf.PutByte(unchecked((byte)Session.SSH_MSG_CHANNEL_CLOSE));
				buf.PutInt(GetRecipient());
				lock (this)
				{
					GetSession().Write(packet);
				}
			}
			catch (Exception)
			{
			}
		}
Example #27
0
		public virtual void Run()
		{
			thread = this;
			byte[] foo;
			Buffer buf = new Buffer();
			Packet packet = new Packet(buf);
			int i = 0;
			Channel channel;
			int[] start = new int[1];
			int[] length = new int[1];
			KeyExchange kex = null;
			int stimeout = 0;
			try
			{
				while (isConnected && thread != null)
				{
					try
					{
						buf = Read(buf);
						stimeout = 0;
					}
					catch (ThreadInterruptedException ee)
					{
						if (!in_kex && stimeout < serverAliveCountMax)
						{
							SendKeepAliveMsg();
							stimeout++;
							continue;
						}
						throw;
					}
					int msgType = buf.GetCommand() & unchecked((int)(0xff));
					if (kex != null && kex.GetState() == msgType)
					{
						kex_start_time = Runtime.CurrentTimeMillis();
						bool result = kex.Next(buf);
						if (!result)
						{
							throw new JSchException("verify: " + result);
						}
						continue;
					}
					switch (msgType)
					{
						case SSH_MSG_KEXINIT:
						{
							//System.err.println("KEXINIT");
							kex = Receive_kexinit(buf);
							break;
						}

						case SSH_MSG_NEWKEYS:
						{
							//System.err.println("NEWKEYS");
							Send_newkeys();
							Receive_newkeys(buf, kex);
							kex = null;
							break;
						}

						case SSH_MSG_CHANNEL_DATA:
						{
							buf.GetInt();
							buf.GetByte();
							buf.GetByte();
							i = buf.GetInt();
							channel = Channel.GetChannel(i, this);
							foo = buf.GetString(start, length);
							if (channel == null)
							{
								break;
							}
							if (length[0] == 0)
							{
								break;
							}
							try
							{
								channel.Write(foo, start[0], length[0]);
							}
							catch (Exception)
							{
								//System.err.println(e);
								try
								{
									channel.Disconnect();
								}
								catch (Exception)
								{
								}
								break;
							}
							int len = length[0];
							channel.SetLocalWindowSize(channel.lwsize - len);
							if (channel.lwsize < channel.lwsize_max / 2)
							{
								packet.Reset();
								buf.PutByte(unchecked((byte)SSH_MSG_CHANNEL_WINDOW_ADJUST));
								buf.PutInt(channel.GetRecipient());
								buf.PutInt(channel.lwsize_max - channel.lwsize);
								Write(packet);
								channel.SetLocalWindowSize(channel.lwsize_max);
							}
							break;
						}

						case SSH_MSG_CHANNEL_EXTENDED_DATA:
						{
							buf.GetInt();
							buf.GetShort();
							i = buf.GetInt();
							channel = Channel.GetChannel(i, this);
							buf.GetInt();
							// data_type_code == 1
							foo = buf.GetString(start, length);
							//System.err.println("stderr: "+new String(foo,start[0],length[0]));
							if (channel == null)
							{
								break;
							}
							if (length[0] == 0)
							{
								break;
							}
							channel.Write_ext(foo, start[0], length[0]);
							int len = length[0];
							channel.SetLocalWindowSize(channel.lwsize - len);
							if (channel.lwsize < channel.lwsize_max / 2)
							{
								packet.Reset();
								buf.PutByte(unchecked((byte)SSH_MSG_CHANNEL_WINDOW_ADJUST));
								buf.PutInt(channel.GetRecipient());
								buf.PutInt(channel.lwsize_max - channel.lwsize);
								Write(packet);
								channel.SetLocalWindowSize(channel.lwsize_max);
							}
							break;
						}

						case SSH_MSG_CHANNEL_WINDOW_ADJUST:
						{
							buf.GetInt();
							buf.GetShort();
							i = buf.GetInt();
							channel = Channel.GetChannel(i, this);
							if (channel == null)
							{
								break;
							}
							channel.AddRemoteWindowSize(buf.GetInt());
							break;
						}

						case SSH_MSG_CHANNEL_EOF:
						{
							buf.GetInt();
							buf.GetShort();
							i = buf.GetInt();
							channel = Channel.GetChannel(i, this);
							if (channel != null)
							{
								//channel.eof_remote=true;
								//channel.eof();
								channel.Eof_remote();
							}
							break;
						}

						case SSH_MSG_CHANNEL_CLOSE:
						{
							buf.GetInt();
							buf.GetShort();
							i = buf.GetInt();
							channel = Channel.GetChannel(i, this);
							if (channel != null)
							{
								//	      channel.close();
								channel.Disconnect();
							}
							break;
						}

						case SSH_MSG_CHANNEL_OPEN_CONFIRMATION:
						{
							buf.GetInt();
							buf.GetShort();
							i = buf.GetInt();
							channel = Channel.GetChannel(i, this);
							if (channel == null)
							{
							}
							//break;
							int r = buf.GetInt();
							long rws = buf.GetUInt();
							int rps = buf.GetInt();
							channel.SetRemoteWindowSize(rws);
							channel.SetRemotePacketSize(rps);
							channel.SetRecipient(r);
							break;
						}

						case SSH_MSG_CHANNEL_OPEN_FAILURE:
						{
							buf.GetInt();
							buf.GetShort();
							i = buf.GetInt();
							channel = Channel.GetChannel(i, this);
							if (channel == null)
							{
							}
							//break;
							int reason_code = buf.GetInt();
							//foo=buf.getString();  // additional textual information
							//foo=buf.getString();  // language tag 
							channel.exitstatus = reason_code;
							channel.close = true;
							channel.eof_remote = true;
							channel.SetRecipient(0);
							break;
						}

						case SSH_MSG_CHANNEL_REQUEST:
						{
							buf.GetInt();
							buf.GetShort();
							i = buf.GetInt();
							foo = buf.GetString();
							bool reply = (buf.GetByte() != 0);
							channel = Channel.GetChannel(i, this);
							if (channel != null)
							{
								byte reply_type = unchecked((byte)SSH_MSG_CHANNEL_FAILURE);
								if ((Util.Byte2str(foo)).Equals("exit-status"))
								{
									i = buf.GetInt();
									// exit-status
									channel.SetExitStatus(i);
									reply_type = unchecked((byte)SSH_MSG_CHANNEL_SUCCESS);
								}
								if (reply)
								{
									packet.Reset();
									buf.PutByte(reply_type);
									buf.PutInt(channel.GetRecipient());
									Write(packet);
								}
							}
							break;
						}

						case SSH_MSG_CHANNEL_OPEN:
						{
							buf.GetInt();
							buf.GetShort();
							foo = buf.GetString();
							string ctyp = Util.Byte2str(foo);
							if (!"forwarded-tcpip".Equals(ctyp) && !("x11".Equals(ctyp) && x11_forwarding) &&
								 !("*****@*****.**".Equals(ctyp) && agent_forwarding))
							{
								//System.err.println("Session.run: CHANNEL OPEN "+ctyp); 
								//throw new IOException("Session.run: CHANNEL OPEN "+ctyp);
								packet.Reset();
								buf.PutByte(unchecked((byte)SSH_MSG_CHANNEL_OPEN_FAILURE));
								buf.PutInt(buf.GetInt());
								buf.PutInt(Channel.SSH_OPEN_ADMINISTRATIVELY_PROHIBITED);
								buf.PutString(Util.empty);
								buf.PutString(Util.empty);
								Write(packet);
							}
							else
							{
								channel = Channel.GetChannel(ctyp);
								AddChannel(channel);
								channel.GetData(buf);
								channel.Init();
								Sharpen.Thread tmp = new Sharpen.Thread(channel);
								tmp.SetName("Channel " + ctyp + " " + host);
								if (daemon_thread)
								{
									tmp.SetDaemon(daemon_thread);
								}
								tmp.Start();
								break;
							}
							goto case SSH_MSG_CHANNEL_SUCCESS;
						}

						case SSH_MSG_CHANNEL_SUCCESS:
						{
							buf.GetInt();
							buf.GetShort();
							i = buf.GetInt();
							channel = Channel.GetChannel(i, this);
							if (channel == null)
							{
								break;
							}
							channel.reply = 1;
							break;
						}

						case SSH_MSG_CHANNEL_FAILURE:
						{
							buf.GetInt();
							buf.GetShort();
							i = buf.GetInt();
							channel = Channel.GetChannel(i, this);
							if (channel == null)
							{
								break;
							}
							channel.reply = 0;
							break;
						}

						case SSH_MSG_GLOBAL_REQUEST:
						{
							buf.GetInt();
							buf.GetShort();
							foo = buf.GetString();
							// request name
							bool reply = (buf.GetByte() != 0);
							if (reply)
							{
								packet.Reset();
								buf.PutByte(unchecked((byte)SSH_MSG_REQUEST_FAILURE));
								Write(packet);
							}
							break;
						}

						case SSH_MSG_REQUEST_FAILURE:
						case SSH_MSG_REQUEST_SUCCESS:
						{
							Sharpen.Thread t = grr.GetThread();
							if (t != null)
							{
								grr.SetReply(msgType == SSH_MSG_REQUEST_SUCCESS ? 1 : 0);
								t.Interrupt();
							}
							break;
						}

						default:
						{
							//System.err.println("Session.run: unsupported type "+msgType); 
							throw new IOException("Unknown SSH message type " + msgType);
						}
					}
				}
			}
			catch (Exception e)
			{
				in_kex = false;
				if (JSch.GetLogger().IsEnabled(Logger.INFO))
				{
					JSch.GetLogger().Log(Logger.INFO, "Caught an exception, leaving main loop due to "
						 + e.Message);
				}
			}
			//System.err.println("# Session.run");
			//e.printStackTrace();
			try
			{
				Disconnect();
			}
			catch (ArgumentNullException)
			{
			}
			catch (Exception)
			{
			}
			//System.err.println("@1");
			//e.printStackTrace();
			//System.err.println("@2");
			//e.printStackTrace();
			isConnected = false;
		}
Example #28
0
		/// <exception cref="System.Exception"></exception>
		protected internal virtual void SendOpenConfirmation()
		{
			Buffer buf = new Buffer(100);
			Packet packet = new Packet(buf);
			packet.Reset();
			buf.PutByte(unchecked((byte)SSH_MSG_CHANNEL_OPEN_CONFIRMATION));
			buf.PutInt(GetRecipient());
			buf.PutInt(id);
			buf.PutInt(lwsize);
			buf.PutInt(lmpsize);
			GetSession().Write(packet);
		}
Example #29
0
		/// <exception cref="System.Exception"></exception>
		public virtual void SendIgnore()
		{
			Buffer buf = new Buffer();
			Packet packet = new Packet(buf);
			packet.Reset();
			buf.PutByte(unchecked((byte)SSH_MSG_IGNORE));
			Write(packet);
		}
Example #30
0
		protected internal virtual void SendOpenFailure(int reasoncode)
		{
			try
			{
				Buffer buf = new Buffer(100);
				Packet packet = new Packet(buf);
				packet.Reset();
				buf.PutByte(unchecked((byte)SSH_MSG_CHANNEL_OPEN_FAILURE));
				buf.PutInt(GetRecipient());
				buf.PutInt(reasoncode);
				buf.PutString(Util.Str2byte("open failed"));
				buf.PutString(Util.empty);
				GetSession().Write(packet);
			}
			catch (Exception)
			{
			}
		}
Example #31
0
		/// <exception cref="System.Exception"></exception>
		private void Send_kexinit()
		{
			if (in_kex)
			{
				return;
			}
			string cipherc2s = GetConfig("cipher.c2s");
			string ciphers2c = GetConfig("cipher.s2c");
			string[] not_available = CheckCiphers(GetConfig("CheckCiphers"));
			if (not_available != null && not_available.Length > 0)
			{
				cipherc2s = Util.DiffString(cipherc2s, not_available);
				ciphers2c = Util.DiffString(ciphers2c, not_available);
				if (cipherc2s == null || ciphers2c == null)
				{
					throw new JSchException("There are not any available ciphers.");
				}
			}
			in_kex = true;
			kex_start_time = Runtime.CurrentTimeMillis();
			// byte      SSH_MSG_KEXINIT(20)
			// byte[16]  cookie (random bytes)
			// string    kex_algorithms
			// string    server_host_key_algorithms
			// string    encryption_algorithms_client_to_server
			// string    encryption_algorithms_server_to_client
			// string    mac_algorithms_client_to_server
			// string    mac_algorithms_server_to_client
			// string    compression_algorithms_client_to_server
			// string    compression_algorithms_server_to_client
			// string    languages_client_to_server
			// string    languages_server_to_client
			Buffer buf = new Buffer();
			// send_kexinit may be invoked
			Packet packet = new Packet(buf);
			// by user thread.
			packet.Reset();
			buf.PutByte(unchecked((byte)SSH_MSG_KEXINIT));
			lock (random)
			{
				random.Fill(buf.buffer, buf.index, 16);
				buf.Skip(16);
			}
			buf.PutString(Util.Str2byte(GetConfig("kex")));
			buf.PutString(Util.Str2byte(GetConfig("server_host_key")));
			buf.PutString(Util.Str2byte(cipherc2s));
			buf.PutString(Util.Str2byte(ciphers2c));
			buf.PutString(Util.Str2byte(GetConfig("mac.c2s")));
			buf.PutString(Util.Str2byte(GetConfig("mac.s2c")));
			buf.PutString(Util.Str2byte(GetConfig("compression.c2s")));
			buf.PutString(Util.Str2byte(GetConfig("compression.s2c")));
			buf.PutString(Util.Str2byte(GetConfig("lang.c2s")));
			buf.PutString(Util.Str2byte(GetConfig("lang.s2c")));
			buf.PutByte(unchecked((byte)0));
			buf.PutInt(0);
			buf.SetOffSet(5);
			I_C = new byte[buf.GetLength()];
			buf.GetByte(I_C);
			Write(packet);
			if (JSch.GetLogger().IsEnabled(Logger.INFO))
			{
				JSch.GetLogger().Log(Logger.INFO, "SSH_MSG_KEXINIT sent");
			}
		}
Example #32
0
        /// <exception cref="NSch.JSchException"></exception>
        public virtual void Connect(int connectTimeout)
        {
            Session _session = GetSession();

            if (!_session.IsConnected())
            {
                throw new JSchException("session is down");
            }
            this.connectTimeout = connectTimeout;
            try
            {
                Buffer buf    = new Buffer(100);
                Packet packet = new Packet(buf);
                // send
                // byte   SSH_MSG_CHANNEL_OPEN(90)
                // string channel type         //
                // uint32 sender channel       // 0
                // uint32 initial window size  // 0x100000(65536)
                // uint32 maxmum packet size   // 0x4000(16384)
                packet.Reset();
                buf.PutByte(unchecked ((byte)90));
                buf.PutString(this.type);
                buf.PutInt(this.id);
                buf.PutInt(this.lwsize);
                buf.PutInt(this.lmpsize);
                _session.Write(packet);
                int  retry   = 1000;
                long start   = Runtime.CurrentTimeMillis();
                long timeout = connectTimeout;
                while (this.GetRecipient() == -1 && _session.IsConnected() && retry > 0)
                {
                    if (timeout > 0L)
                    {
                        if ((Runtime.CurrentTimeMillis() - start) > timeout)
                        {
                            retry = 0;
                            continue;
                        }
                    }
                    try
                    {
                        Sharpen.Thread.Sleep(50);
                    }
                    catch (Exception)
                    {
                    }
                    retry--;
                }
                if (!_session.IsConnected())
                {
                    throw new JSchException("session is down");
                }
                if (retry == 0)
                {
                    throw new JSchException("channel is not opened.");
                }
                if (this.IsClosed())
                {
                    throw new JSchException("channel is not opened.");
                }
                connected = true;
                Start();
            }
            catch (Exception e)
            {
                connected = false;
                Disconnect();
                if (e is JSchException)
                {
                    throw (JSchException)e;
                }
                throw new JSchException(e.ToString(), e);
            }
        }