本文整理汇总了C#中SslStream.AuthenticateAsServer方法的典型用法代码示例。如果您正苦于以下问题:C# SslStream.AuthenticateAsServer方法的具体用法?C# SslStream.AuthenticateAsServer怎么用?C# SslStream.AuthenticateAsServer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SslStream
的用法示例。
在下文中一共展示了SslStream.AuthenticateAsServer方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: WebSocketClient
//Encryption
internal WebSocketClient(Socket s,X509Certificate cert)
{
this.Socket = s;
var ns = new NetworkStream(s,false);
var ss = new SslStream(ns, false);
ss.AuthenticateAsServer(cert,false,SslProtocols.Tls12,false);
this.stream = ss;
}
示例2: BasicServerTest
public void BasicServerTest() {
try {
testName = "BasicServerTest";
AcceptConnection(); // sets the client member
sslStream = new SslStream(client.GetStream(), false);
sslStream.AuthenticateAsServer(testServer.serverCertificate);
// Do the server read, and write of the messages
if (DoServerReadWrite()) {
Shutdown(true);
}
else {
Shutdown(false);
}
}
catch (Exception) {
Shutdown(false);
}
}
示例3: AdvancedServerTest
public void AdvancedServerTest() {
serverRemoteCertificateValidationCallback = new RemoteCertificateValidationHandler(ValidateRemoteCert);
try {
testName = "AdvancedServerTest";
AcceptConnection(); // sets the client member
sslStream = new SslStream(client.GetStream(), false, serverRemoteCertificateValidationCallback);
sslStream.AuthenticateAsServer(testServer.serverCertificate, true, testServer.serverCAChain, SslProtocols.Tls, SslStrength.All, true);
// Verify mutual authentication
if (!sslStream.IsMutuallyAuthenticated) {
Console.WriteLine("{0} failed - stream is not mutually authenticated", testName);
Shutdown(false);
return;
}
// Verify protocol
if (sslStream.SslProtocol != SslProtocols.Tls) {
Console.WriteLine("{0} failed - negotiated non Tls connection", testName);
Shutdown(false);
return;
}
// Verify cipher strength
if (sslStream.CipherStrength < 256) {
Console.WriteLine("{0} failed - negotiated less than 256bit cipher", testName);
Shutdown(false);
return;
}
// Do the server read, and write of the messages
if (DoServerReadWrite()) {
Shutdown(true);
}
else {
Shutdown(false);
}
}
catch (Exception) {
Shutdown(false);
}
}
示例4: IntermediateServerTest
public void IntermediateServerTest() {
try {
testName = "IntermediateServerTest";
AcceptConnection(); // sets the client member
sslStream = new SslStream(client.GetStream(), false);
sslStream.AuthenticateAsServer(testServer.serverCertificate, false, null, SslProtocols.Tls, SslStrength.All, false);
// Verify protocol
if (sslStream.SslProtocol != SslProtocols.Tls) {
Console.WriteLine("{0} failed - negotiated non Tls connection", testName);
Shutdown(false);
return;
}
// Verify cipher strength
if (sslStream.CipherStrength < 256) {
Console.WriteLine("{0} failed - negotiated less than 256bit cipher", testName);
Shutdown(false);
return;
}
//Verify cipher
if (sslStream.CipherAlgorithm != CipherAlgorithmType.Aes256) {
Console.WriteLine("{0} failed - negotiated cipher was not AES256", testName);
Shutdown(false);
return;
}
// Do the server read, and write of the messages
if (DoServerReadWrite()) {
Shutdown(true);
}
else {
Shutdown(false);
}
}
catch (Exception) {
Shutdown(false);
}
}
示例5: Run
public void Run()
{
Trace.TraceInformation("Server started on port: " + _server.Port);
while (!IsStopped())
{
try
{
TcpClient connection = null;
Stream stream = null;
try
{
connection = _socket.AcceptTcpClient();
stream = connection.GetStream();
if (_server.UseSSL)
{
SslStream sslStream = new SslStream(stream, false);
stream = sslStream;
sslStream.AuthenticateAsServer(_server.ServerCertificate,
false,
SslProtocols.Tls,
false);
}
ConnectionProcessor processor =
new ConnectionProcessor(_server,
new RemoteFrameworkConnection(connection, stream));
Thread thread = new Thread(processor.Run);
thread.IsBackground = false;
thread.Start();
}
catch (Exception)
{
if (stream != null)
{
try { stream.Close(); }
catch (Exception) { }
}
if (connection != null)
{
try { connection.Close(); }
catch (Exception) { }
}
throw;
}
}
catch (Exception e)
{
//log the error unless it's because we've stopped
if (!IsStopped() || !(e is SocketException))
{
TraceUtil.TraceException("Error processing request", e);
}
//wait a second before trying again
if (!IsStopped())
{
Thread.Sleep(1000);
}
}
}
}
示例6: DoHandshake
protected override bool DoHandshake(SslStream clientSslStream, SslStream serverSslStream)
{
using (X509Certificate2 certificate = Configuration.Certificates.GetServerCertificate())
{
Task t1 = Task.Run(() => clientSslStream.AuthenticateAsClient(certificate.GetNameInfo(X509NameType.SimpleName, false)));
Task t2 = Task.Run(() => serverSslStream.AuthenticateAsServer(certificate));
return Task.WaitAll(new[] { t1, t2 }, TestConfiguration.PassingTestTimeoutMilliseconds);
}
}
示例7: TestSyncAdvanced
public void TestSyncAdvanced()
{
IPEndPoint ep = null;
var evtReady = new AutoResetEvent(false);
var serverTask = Task.Factory.StartNew(() =>
{
var listener = new TcpListener(IPAddress.Loopback, 0);
listener.Start(5);
ep = (IPEndPoint)listener.LocalEndpoint;
evtReady.Set();
Console.WriteLine("Server> waiting for accept");
using (var tcp = listener.AcceptTcpClient())
using (var sslStream = new SslStream(tcp.GetStream(), false, ValidateRemoteCert))
{
Console.WriteLine("Server> authenticate");
sslStream.AuthenticateAsServer(
_ctx.ServerCertificate,
true,
_ctx.CAChain,
SslProtocols.Tls,
SslStrength.All,
true
);
Console.WriteLine("Server> CurrentCipher: {0}", sslStream.Ssl.CurrentCipher.Name);
Assert.AreEqual("AES256-GCM-SHA384", sslStream.Ssl.CurrentCipher.Name);
Assert.IsTrue(sslStream.IsMutuallyAuthenticated);
Console.WriteLine("Server> rx msg");
var buf = new byte[256];
sslStream.Read(buf, 0, buf.Length);
Assert.AreEqual(clientMessage.ToString(), buf.ToString());
Console.WriteLine("Server> tx msg");
sslStream.Write(serverMessage, 0, serverMessage.Length);
Console.WriteLine("Server> done");
}
listener.Stop();
});
var clientTask = Task.Factory.StartNew(() =>
{
evtReady.WaitOne();
Console.WriteLine("Client> Connecting to: {0}:{1}", ep.Address, ep.Port);
using (var tcp = new TcpClient(ep.Address.ToString(), ep.Port))
using (var sslStream = new SslStream(
tcp.GetStream(),
false,
ValidateRemoteCert,
SelectClientCertificate))
{
Console.WriteLine("Client> authenticate");
sslStream.AuthenticateAsClient(
"localhost",
_ctx.ClientCertificateList,
_ctx.CAChain,
SslProtocols.Tls,
SslStrength.All,
true
);
Console.WriteLine("Client> CurrentCipher: {0}", sslStream.Ssl.CurrentCipher.Name);
Assert.AreEqual("AES256-GCM-SHA384", sslStream.Ssl.CurrentCipher.Name);
Assert.IsTrue(sslStream.IsMutuallyAuthenticated);
Console.WriteLine("Client> tx msg");
sslStream.Write(clientMessage, 0, clientMessage.Length);
Console.WriteLine("Client> rx msg");
var buf = new byte[256];
sslStream.Read(buf, 0, buf.Length);
Assert.AreEqual(serverMessage.ToString(), buf.ToString());
Console.WriteLine("Client> done");
}
});
Task.WaitAll(clientTask, serverTask);
}
示例8: TestSyncIntermediate
public void TestSyncIntermediate()
{
IPEndPoint ep = null;
var evtReady = new AutoResetEvent(false);
var serverTask = Task.Factory.StartNew(() =>
{
var listener = new TcpListener(IPAddress.Loopback, 0);
listener.Start(5);
ep = (IPEndPoint)listener.LocalEndpoint;
evtReady.Set();
Console.WriteLine("Server> waiting for accept");
using (var tcp = listener.AcceptTcpClient())
using (var sslStream = new SslStream(tcp.GetStream()))
{
Console.WriteLine("Server> authenticate");
sslStream.AuthenticateAsServer(
_ctx.ServerCertificate,
false,
null,
SslProtocols.Default,
SslStrength.Low,
false
);
Console.WriteLine("Server> CurrentCipher: {0}", sslStream.Ssl.CurrentCipher.Name);
Assert.AreEqual("DES-CBC-SHA", sslStream.Ssl.CurrentCipher.Name);
Console.WriteLine("Server> rx msg");
var buf = new byte[256];
sslStream.Read(buf, 0, buf.Length);
Assert.AreEqual(clientMessage.ToString(), buf.ToString());
Console.WriteLine("Server> tx msg");
sslStream.Write(serverMessage, 0, serverMessage.Length);
Console.WriteLine("Server> done");
}
listener.Stop();
});
var clientTask = Task.Factory.StartNew(() =>
{
evtReady.WaitOne();
Console.WriteLine("Client> Connecting to: {0}:{1}", ep.Address, ep.Port);
using (var tcp = new TcpClient(ep.Address.ToString(), ep.Port))
using (var sslStream = new SslStream(tcp.GetStream()))
{
Console.WriteLine("Client> authenticate");
sslStream.AuthenticateAsClient(
"localhost",
null,
null,
SslProtocols.Default,
SslStrength.Low,
false
);
Console.WriteLine("Client> CurrentCipher: {0}", sslStream.Ssl.CurrentCipher.Name);
Assert.AreEqual("DES-CBC-SHA", sslStream.Ssl.CurrentCipher.Name);
Console.WriteLine("Client> tx msg");
sslStream.Write(clientMessage, 0, clientMessage.Length);
Console.WriteLine("Client> rx msg");
var buf = new byte[256];
sslStream.Read(buf, 0, buf.Length);
Assert.AreEqual(serverMessage.ToString(), buf.ToString());
Console.WriteLine("Client> done");
}
});
serverTask.Wait();
clientTask.Wait();
}
示例9: ProcessRequest
internal void ProcessRequest()
{
#if(LOG && !MF && !WindowsCE)
Console.WriteLine((_client.RemoteEndPoint as IPEndPoint).ToString());
#endif
using (_client)
{
while (true)
{
#region Wait for first byte (used for keep-alive, too)
int avail = 0;
DateTime maxWait = DateTime.Now.AddMilliseconds(2000);
do
{
try
{
avail = _client.Available;
if (avail == 0)
Thread.Sleep(10);
}
catch
{
break;
}
}
while (avail == 0 && DateTime.Now <= maxWait);
#endregion
if (avail == 0)
break;
DateTime begin = DateTime.Now;
HttpRequest httpRequest = new HttpRequest();
HttpResponse httpResponse = null;
Stream stream;
#if(SSL)
if (_server.IsSecure && _server.Certificate != null)
{
SslStream ssl = null;
try
{
#if(!MF)
ssl = new SslStream(new NetworkStream(_client));
ssl.AuthenticateAsServer(_server.Certificate, false, SslProtocols.Default, false);
#else
ssl = new SslStream(_client);
ssl.AuthenticateAsServer(_server.Certificate, SslVerification.NoVerification, SslProtocols.Default);
#endif
stream = ssl;
}
catch (Exception)
{
Close();
return;
}
}
else
#endif
{
stream = new NetworkStream(_client);
}
stream.ReadTimeout = 200;
stream.WriteTimeout = 1000;
try
{
if (!httpRequest.Read(stream, (_client.RemoteEndPoint as IPEndPoint)))
{
httpResponse = new HttpResponse();
httpResponse.RaiseError(HttpStatusCode.ServiceUnavailable);
httpResponse.AddHeader("Connection", "close");
}
}
catch (HttpException ex)
{
httpResponse = new HttpResponse();
httpResponse.RaiseError(ex.Message, ex.Code);
httpResponse.AddHeader("Connection", "close");
}
catch (Exception)
{
httpResponse = new HttpResponse();
httpResponse.RaiseError();
httpResponse.AddHeader("Connection", "close");
}
if (httpResponse == null)
{
httpResponse = new HttpResponse();
//.........这里部分代码省略.........
示例10: CreateSecureContext
private void CreateSecureContext(Stream stream, IPEndPoint remoteEndPoint)
{
SslStream sslStream = new SslStream(stream, false);
try
{
sslStream.AuthenticateAsServer(_certificate, false, _sslProtocol, false); //todo: this may fail
new HttpClientContextImp(true, remoteEndPoint, _requestHandler, _disconnectHandler, sslStream,
LogWriter);
}
catch (IOException err)
{
if (UseTraceLogs)
_logWriter.Write(this, LogPrio.Trace, err.Message);
}
catch (ObjectDisposedException err)
{
if (UseTraceLogs)
_logWriter.Write(this, LogPrio.Trace, err.Message);
}
}