I have troubles with the implementation of my proxy server over SSL with self signed Certificate:
HttpServer.cs:
static void Main(string[] args) { HttpsServer httpsServer = new HttpsServer(443, 1000, 1); httpsServer.Start(); } public HttpsServer(int port, int listeningInterval, int numOfThreads) { m_port = port; m_httpClient = new HttpClient(443, 1000, "127.0.0.1"); ThreadPool.SetMaxThreads(numOfThreads, 0); m_listeningInterval = listeningInterval; m_stop = false; } public void Start() { new Thread(() => { TcpListener listener = new TcpListener(m_port); listener.Start(); while (!m_stop) { if (listener.Pending()) { ThreadPool.QueueUserWorkItem((Object o) => { TcpClient client = listener.AcceptTcpClient(); m.WaitOne(); ProcessClient(client); m.ReleaseMutex(); }); } else { Thread.Sleep(m_listeningInterval); } } }).Start(); } static void ProcessClient(TcpClient client) { // A client has connected. Create the // SslStream using the client's network stream. SslStream sslStream = new SslStream(client.GetStream(), true);// here is the line // Authenticate the server but don't require the client to authenticate. try { serverCertificate = new X509Certificate2("CARoot.pfx", "pass"); sslStream.AuthenticateAsServer(serverCertificate, false, SslProtocols.Tls, true); // Set timeouts for the read and write to 5 seconds. sslStream.ReadTimeout = 5000; sslStream.WriteTimeout = 5000; // Read a message from the client. string messageData = ReadMessage(sslStream); // Write a message to the client. if (messageData != null && !messageData.Equals("")) { byte[] message = Encoding.UTF8.GetBytes(messageData); sslStream.Write(message); } } catch (AuthenticationException e) { Console.WriteLine("Exception: {0}", e.Message); if (e.InnerException != null) { Console.WriteLine("Inner exception: {0}", e.InnerException.Message); } Console.WriteLine("Authentication failed - closing the connection."); sslStream.Close(); client.Close(); return; } finally { // The client stream will be closed with the sslStream // because we specified this behavior when creating // the sslStream. sslStream.Close(); client.Close(); } } static string ReadMessage(SslStream sslStream) { // Read the message sent by the client. // The client signals the end of the message using the // "<EOF>" marker. byte[] buffer = new byte[65536]; StringBuilder message = new StringBuilder(); int b = -1; b = sslStream.Read(buffer, 0, buffer.Length); Decoder d = Encoding.UTF8.GetDecoder(); char[] data = new char[d.GetCharCount(buffer, 0, b)]; d.GetChars(buffer, 0, b, data, 0); message.Append(data); string ans = message.ToString(); return ans; }
and the HttpClient.cs:
public HttpClient(int port, int interval, string ip) { m_ip = ip; m_port = port; m_timeInterval = interval; CommunicateWithServer(); } private void CommunicateWithServer() { new Thread(() => { int port = m_port; String serverIP = m_ip; TcpClient client = new TcpClient(serverIP, port); SslStream sslStream = new SslStream( client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null ); // sslStream.AuthenticateAsClient("localhost"); // sslStream.AuthenticateAsClient(serverName); while (true) { if (sslStream.CanRead) { string serverMsg = ReadMessage(sslStream); writeToWeb(serverMsg); } else { Thread.Sleep(m_timeInterval); } } }).Start(); } static string ReadMessage(SslStream sslStream) { // Read the message sent by the server. // The end of the message is signaled using the // "<EOF>" marker. byte[] buffer = new byte[65536]; StringBuilder messageData = new StringBuilder(); int bytes = -1; bytes = sslStream.Read(buffer, 0, buffer.Length); // Use Decoder class to convert from bytes to UTF8 // in case a character spans two buffers. Decoder decoder = Encoding.UTF8.GetDecoder(); char[] chars = new char[decoder.GetCharCount(buffer, 0, bytes)]; decoder.GetChars(buffer, 0, bytes, chars, 0); messageData.Append(chars); return messageData.ToString(); }
I have created the CARoot.pfx already and the program just stay at the sslStream.AuthenticateAsServer(serverCertificate, false, SslProtocols.Tls, true); line in the HttpServer.cs
where am I wrong? help please.