private void ConnectCallback(IAsyncResult ar) { var client = (Socket)ar.AsyncState; client.EndConnect(ar); if (_useSsl) { var stream = new NetworkStream(_client, FileAccess.ReadWrite, true); _sslWrapper = new SslStream(stream, false, ValidateRemoteCert, clientCertificateSelectionCallback); _sslWrapper.AuthenticateAsClient(_server, _xList, _xChain, SslProtocols.Default, SslStrength.All, false); SecureReceive(_sslWrapper); } else { Receive(client); } if (ConnectComplete != null) ConnectComplete(this, EventArgs.Empty); }
public void IntermediateClientTest() { try { testName = "IntermediateClientTest"; client = new TcpClient("localhost", 9000); sslStream = new SslStream(client.GetStream(), false); sslStream.AuthenticateAsClient("localhost", null, null, SslProtocols.Tls, SslStrength.Medium | SslStrength.High, false); if (sslStream.SslProtocol != SslProtocols.Tls) { Console.WriteLine("{0} failed - negotiated a non Tls connection", testName); Shutdown(false); return; } if (sslStream.CipherStrength < 256) { Console.WriteLine("{0} failed - negotiated less that 256bit cipher", testName); Console.WriteLine("Cipher={0}\nCipherStrength = {1}", sslStream.CipherAlgorithm.ToString(), sslStream.CipherStrength); Shutdown(false); return; } if (sslStream.CipherAlgorithm != CipherAlgorithmType.Aes256) { Console.WriteLine("{0} failed - negotiatied cipher wasn't Aes256", testName); Console.WriteLine("Cipher was {0}, expected {0}", sslStream.CipherAlgorithm.ToString(), CipherAlgorithmType.Aes256.ToString()); Shutdown(false); return; } if (DoClientReadWrite()) { Shutdown(true); } else { Shutdown(false); } } catch (Exception ex) { Shutdown(false); Console.WriteLine(ex); } }
public void AdvancedClientTest() { //Initialize delegates for certificate callbacks clientRemoteCertificateValidationCallback = new RemoteCertificateValidationHandler(ValidateRemoteCert); clientLocalCertificateSelectionCallback = new LocalCertificateSelectionHandler(clientCertificateSelectionCallback); try { testName = "AdvancedClientTest"; client = new TcpClient("localhost", 9000); // Create the SslStream object with the certificate callbacks sslStream = new SslStream(client.GetStream(), false, clientRemoteCertificateValidationCallback, clientLocalCertificateSelectionCallback); // Initialize with client certificate list, and client CA chain sslStream.AuthenticateAsClient("localhost", testServer.clientCertificateList, testServer.clientCAChain, SslProtocols.Tls, SslStrength.Medium | SslStrength.High, true); // Verify mutual authentication if (!sslStream.IsMutuallyAuthenticated) { Console.WriteLine("{0} failed - Stream is not mutally authenticated", testName); Shutdown(false); } // Verify protocol if (sslStream.SslProtocol != SslProtocols.Tls) { Console.WriteLine("{0} failed - negotiated a non Tls connection", testName); Shutdown(false); } // Verify cipher strength if (sslStream.CipherStrength < 256) { Console.WriteLine("{0} failed - negotiated less that 256bit cipher", testName); Console.WriteLine("Cipher={0}\nCipherStrength = {1}", sslStream.CipherAlgorithm.ToString(), sslStream.CipherStrength); Shutdown(false); } // Verify cipher if (sslStream.CipherAlgorithm != CipherAlgorithmType.Aes256) { Console.WriteLine("{0} failed - negotiatied cipher wasn't Aes256", testName); Console.WriteLine("Cipher was {0}, expected {0}", sslStream.CipherAlgorithm.ToString(), CipherAlgorithmType.Aes256.ToString()); Shutdown(false); } if (DoClientReadWrite()) { Shutdown(true); } else { Shutdown(false); } } catch (Exception ex) { Shutdown(false); Console.WriteLine(ex); } }
public void BasicClientTest() { try { testName = "BasicClientTest"; client = new TcpClient("localhost", 9000); sslStream = new SslStream(client.GetStream(), false); sslStream.AuthenticateAsClient("localhost"); if (DoClientReadWrite()) { Shutdown(true); } else { Shutdown(false); } } catch (Exception) { Shutdown(false); } }