protected void SendMsgUserAuthInfoRequest(string name, string instruction, IList <AuthenticationPrompt> prompts)
        {
            if (_isDisposed)
            {
                throw new ObjectDisposedException(this.GetType().FullName);
            }

            // Create message to send.
            using (var msgStream = new MemoryStream())
                using (var msgWriter = new SshStreamWriter(msgStream))
                {
                    msgWriter.Write((byte)SshAuthenticationMessage.InfoRequest);
                    msgWriter.WriteByteString(Encoding.UTF8.GetBytes(name));
                    msgWriter.WriteByteString(Encoding.UTF8.GetBytes(instruction));
                    msgWriter.Write(""); // language tag (deprecated)
                    msgWriter.Write(prompts.Count);

                    // Write info for each prompt.
                    foreach (var prompt in prompts)
                    {
                        msgWriter.WriteByteString(Encoding.UTF8.GetBytes(prompt.Prompt));
                        msgWriter.Write(prompt.Echo);
                    }

                    _client.SendPacket <SshAuthenticationMessage>(msgStream.ToArray());
                }
        }
        internal void SendMsgChannelExtendedData(SshChannel channel, SshExtendedDataType dataType,
                                                 byte[] data)
        {
            if (_isDisposed)
            {
                throw new ObjectDisposedException(this.GetType().FullName);
            }

            // Check if data is too large to send.
            if (data.Length > Math.Min(channel.MaxPacketSize, channel.WindowSize))
            {
                throw new InvalidOperationException("Data is too large to send in one packet.");
            }

            // Create message to send.
            using (var msgStream = new MemoryStream())
                using (var msgWriter = new SshStreamWriter(msgStream))
                {
                    msgWriter.Write((byte)SshConnectionMessage.ChannelExtendedData);
                    msgWriter.Write(channel.ClientChannel);
                    msgWriter.Write((uint)dataType);
                    msgWriter.WriteByteString(data);

                    _client.SendPacket <SshConnectionMessage>(msgStream.ToArray());
                }
        }
        protected bool VerifyPublicKeySignature(PublicKeyAlgorithm alg, byte[] payload, int payloadOffset,
                                                int payloadCount, byte[] signature)
        {
            using (var hashInputStream = new MemoryStream())
                using (var hashInputWriter = new SshStreamWriter(hashInputStream))
                {
                    // Write input data.
                    hashInputWriter.WriteByteString(_client.SessionId);
                    hashInputWriter.Write(payload, payloadOffset, payloadCount);

                    // Verify signature.
                    return(alg.VerifyData(hashInputStream.ToArray(), signature));
                }
        }
Beispiel #4
0
        public byte[] CreateSignatureData(byte[] data)
        {
            using (var dataStream = new MemoryStream())
            {
                using (var dataWriter = new SshStreamWriter(dataStream))
                {
                    // Create signature from hash data.
                    var signature = SignData(data);

                    // Write signature to stream.
                    dataWriter.Write(this.Name);
                    dataWriter.WriteByteString(signature);
                }

                return(dataStream.ToArray());
            }
        }
        public byte[] CreateSignatureData(byte[] data)
        {
            using (var dataStream = new MemoryStream())
            {
                using (var dataWriter = new SshStreamWriter(dataStream))
                {
                    // Create signature from hash data.
                    var signature = SignData(data);

                    // Write signature to stream.
                    dataWriter.Write(this.Name);
                    dataWriter.WriteByteString(signature);
                }

                return dataStream.ToArray();
            }
        }
        protected void SendMsgUserAuthPasswdChangeReq(string prompt, string language)
        {
            if (_isDisposed)
            {
                throw new ObjectDisposedException(this.GetType().FullName);
            }

            // Create message to send.
            using (var msgStream = new MemoryStream())
                using (var msgWriter = new SshStreamWriter(msgStream))
                {
                    msgWriter.Write((byte)SshAuthenticationMessage.PasswordChangeRequired);
                    msgWriter.WriteByteString(Encoding.UTF8.GetBytes(prompt));
                    msgWriter.Write(language);

                    _client.SendPacket <SshAuthenticationMessage>(msgStream.ToArray());
                }
        }
        protected void SendMsgUserAuthPkOk(string algName, byte[] keyBlob)
        {
            if (_isDisposed)
            {
                throw new ObjectDisposedException(this.GetType().FullName);
            }

            // Create message to send.
            using (var msgStream = new MemoryStream())
                using (var msgWriter = new SshStreamWriter(msgStream))
                {
                    msgWriter.Write((byte)SshAuthenticationMessage.PublicKeyOk);

                    // Write public key information.
                    msgWriter.Write(algName);
                    msgWriter.WriteByteString(keyBlob);

                    _client.SendPacket <SshAuthenticationMessage>(msgStream.ToArray());
                }
        }
        internal void SendMsgChannelOpenFailure(SshChannel channel, SshChannelOpenFailureReason reason,
                                                string description, string language)
        {
            if (_isDisposed)
            {
                throw new ObjectDisposedException(this.GetType().FullName);
            }

            // Create message to send.
            using (var msgStream = new MemoryStream())
                using (var msgWriter = new SshStreamWriter(msgStream))
                {
                    msgWriter.Write((byte)SshConnectionMessage.ChannelOpenFailure);
                    msgWriter.Write(channel.ClientChannel);
                    msgWriter.Write((uint)reason);
                    msgWriter.WriteByteString(Encoding.UTF8.GetBytes(description));
                    msgWriter.Write(language);

                    _client.SendPacket <SshConnectionMessage>(msgStream.ToArray());
                }
        }
        protected bool VerifyPublicKeySignature(PublicKeyAlgorithm alg, byte[] payload, int payloadOffset,
            int payloadCount, byte[] signature)
        {
            using (var hashInputStream = new MemoryStream())
            using (var hashInputWriter = new SshStreamWriter(hashInputStream))
            {
                // Write input data.
                hashInputWriter.WriteByteString(_client.SessionId);
                hashInputWriter.Write(payload, payloadOffset, payloadCount);

                // Verify signature.
                return alg.VerifyData(hashInputStream.ToArray(), signature);
            }
        }
        protected void SendMsgUserAuthPkOk(string algName, byte[] keyBlob)
        {
            if (_isDisposed) throw new ObjectDisposedException(this.GetType().FullName);

            // Create message to send.
            using (var msgStream = new MemoryStream())
            using (var msgWriter = new SshStreamWriter(msgStream))
            {
                msgWriter.Write((byte)SshAuthenticationMessage.PublicKeyOk);

                // Write public key information.
                msgWriter.Write(algName);
                msgWriter.WriteByteString(keyBlob);

                _client.SendPacket<SshAuthenticationMessage>(msgStream.ToArray());
            }
        }
        protected void SendMsgUserAuthPasswdChangeReq(string prompt, string language)
        {
            if (_isDisposed) throw new ObjectDisposedException(this.GetType().FullName);

            // Create message to send.
            using (var msgStream = new MemoryStream())
            using (var msgWriter = new SshStreamWriter(msgStream))
            {
                msgWriter.Write((byte)SshAuthenticationMessage.PasswordChangeRequired);
                msgWriter.WriteByteString(Encoding.UTF8.GetBytes(prompt));
                msgWriter.Write(language);

                _client.SendPacket<SshAuthenticationMessage>(msgStream.ToArray());
            }
        }
        protected void SendMsgUserAuthInfoRequest(string name, string instruction, IList<AuthenticationPrompt> prompts)
        {
            if (_isDisposed) throw new ObjectDisposedException(this.GetType().FullName);

            // Create message to send.
            using (var msgStream = new MemoryStream())
            using (var msgWriter = new SshStreamWriter(msgStream))
            {
                msgWriter.Write((byte)SshAuthenticationMessage.InfoRequest);
                msgWriter.WriteByteString(Encoding.UTF8.GetBytes(name));
                msgWriter.WriteByteString(Encoding.UTF8.GetBytes(instruction));
                msgWriter.Write(""); // language tag (deprecated)
                msgWriter.Write(prompts.Count);

                // Write info for each prompt.
                foreach (var prompt in prompts)
                {
                    msgWriter.WriteByteString(Encoding.UTF8.GetBytes(prompt.Prompt));
                    msgWriter.Write(prompt.Echo);
                }

                _client.SendPacket<SshAuthenticationMessage>(msgStream.ToArray());
            }
        }
        internal void SendMsgChannelOpenFailure(SshChannel channel, SshChannelOpenFailureReason reason,
            string description, string language)
        {
            if (_isDisposed) throw new ObjectDisposedException(this.GetType().FullName);

            // Create message to send.
            using (var msgStream = new MemoryStream())
            using (var msgWriter = new SshStreamWriter(msgStream))
            {
                msgWriter.Write((byte)SshConnectionMessage.ChannelOpenFailure);
                msgWriter.Write(channel.ClientChannel);
                msgWriter.Write((uint)reason);
                msgWriter.WriteByteString(Encoding.UTF8.GetBytes(description));
                msgWriter.Write(language);

                _client.SendPacket<SshConnectionMessage>(msgStream.ToArray());
            }
        }
        internal void SendMsgChannelExtendedData(SshChannel channel, SshExtendedDataType dataType,
            byte[] data)
        {
            if (_isDisposed) throw new ObjectDisposedException(this.GetType().FullName);

            // Check if data is too large to send.
            if (data.Length > Math.Min(channel.MaxPacketSize, channel.WindowSize))
                throw new InvalidOperationException("Data is too large to send in one packet.");

            // Create message to send.
            using (var msgStream = new MemoryStream())
            using (var msgWriter = new SshStreamWriter(msgStream))
            {
                msgWriter.Write((byte)SshConnectionMessage.ChannelExtendedData);
                msgWriter.Write(channel.ClientChannel);
                msgWriter.Write((uint)dataType);
                msgWriter.WriteByteString(data);

                _client.SendPacket<SshConnectionMessage>(msgStream.ToArray());
            }
        }