internal override AuthenticationResult Connect() { //key exchange KeyExchanger kex = new KeyExchanger(this, null); if (!kex.SynchronizedKexExchange()) { Close(); return AuthenticationResult.Failure; } //user authentication ServiceRequest("ssh-userauth"); _authenticationResult = UserAuth(); return _authenticationResult; }
//Start key refresh public void ReexchangeKeys() { _asyncKeyExchanger = new KeyExchanger(this, _sessionID); _asyncKeyExchanger.AsyncStartReexchange(); }
internal void RefreshKeys(byte[] sessionID, Cipher tc, Cipher rc, MAC tm, MAC rm) { lock (this) { //these must change synchronously _sessionID = sessionID; _tCipher = tc; _tMAC = tm; _packetBuilder.SetCipher(rc, rm, _param.CheckMACError); _asyncKeyExchanger = null; } }
private bool ProcessPacket(DataFragment packet) { if (_readerForProcessPacket == null) _readerForProcessPacket = new SSH2DataReader(packet); else _readerForProcessPacket.Recycle(packet); //avoid 'new' SSH2DataReader r = _readerForProcessPacket; //rename for frequently use PacketType pt = r.ReadPacketType(); if (pt == PacketType.SSH_MSG_DISCONNECT) { int errorcode = r.ReadInt32(); _eventReceiver.OnConnectionClosed(); return false; } else if (_waitingForPortForwardingResponse) { if (pt != PacketType.SSH_MSG_REQUEST_SUCCESS) _eventReceiver.OnUnknownMessage((byte)pt, r.Image); _waitingForPortForwardingResponse = false; return true; } else if (pt == PacketType.SSH_MSG_CHANNEL_OPEN) { string method = Encoding.ASCII.GetString(r.ReadString()); if (method == "forwarded-tcpip") ProcessPortforwardingRequest(_eventReceiver, r); else if (method.StartsWith("auth-agent")) //in most cases, method is "*****@*****.**" ProcessAgentForwardRequest(_eventReceiver, r); else { SSH2DataWriter wr = new SSH2DataWriter(); wr.WritePacketType(PacketType.SSH_MSG_CHANNEL_OPEN_FAILURE); wr.WriteInt32(r.ReadInt32()); wr.WriteInt32(0); wr.WriteString("unknown method"); wr.WriteString(""); //lang tag TraceReceptionEvent("SSH_MSG_CHANNEL_OPEN rejected", "method={0}", method); } return true; } else if (pt >= PacketType.SSH_MSG_CHANNEL_OPEN_CONFIRMATION && pt <= PacketType.SSH_MSG_CHANNEL_FAILURE) { int local_channel = r.ReadInt32(); ChannelCollection.Entry e = this.ChannelCollection.FindChannelEntry(local_channel); if (e != null) ((SSH2Channel)e.Channel).ProcessPacket(e.Receiver, pt, 5 + r.Rest, r); else ;//Debug.WriteLine("unexpected channel pt=" + pt + " local_channel=" + local_channel.ToString()); return true; } else if (pt == PacketType.SSH_MSG_IGNORE) { _eventReceiver.OnIgnoreMessage(r.ReadString()); return true; } else if (_asyncKeyExchanger != null) { _asyncKeyExchanger.AsyncProcessPacket(packet); return true; } else if (pt == PacketType.SSH_MSG_KEXINIT) { //Debug.WriteLine("Host sent KEXINIT"); _asyncKeyExchanger = new KeyExchanger(this, _sessionID); _asyncKeyExchanger.AsyncProcessPacket(packet); return true; } else if (pt == PacketType.SSH_MSG_GLOBAL_REQUEST) { SSH2DataWriter wr = new SSH2DataWriter(); wr.WritePacketType(PacketType.SSH_MSG_REQUEST_SUCCESS); wr.WriteBool(true); TransmitRawPayload(wr.ToByteArray()); return true; } else { _eventReceiver.OnUnknownMessage((byte)pt, r.Image); return false; } }