Skip to content

Commit ec82ec1

Browse files
committed
Ssl validation callback fix
1 parent 1d2287a commit ec82ec1

File tree

6 files changed

+60
-27
lines changed

6 files changed

+60
-27
lines changed

Benchmarks/SslBenchmark/Program.cs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using System.Threading.Tasks;
1313
using NetworkLibrary.TCP.SSL.ByteMessage;
1414
using System.Security.Cryptography.X509Certificates;
15+
using System.Net.Security;
1516

1617
namespace SslBenchmark
1718
{
@@ -32,6 +33,9 @@ private static void Bench()
3233
int totMsgServer = 0;
3334
int lastTimeStamp = 1;
3435
int clientAmount = 100;
36+
const int numMsg = 10000;
37+
var message = new byte[3200];
38+
var response = new byte[3200];
3539

3640
var scert = new X509Certificate2("server.pfx", "greenpass");
3741
var ccert = new X509Certificate2("client.pfx", "greenpass");
@@ -42,31 +46,39 @@ private static void Bench()
4246
Stopwatch sw2 = new Stopwatch();
4347
AutoResetEvent testCompletionEvent = new AutoResetEvent(false);
4448

45-
var message = new byte[3200];
46-
var response = new byte[3200];
49+
4750

4851
server.MaxIndexedMemoryPerClient = 1280000000;
4952
server.DropOnBackPressure = false;
5053
server.OnBytesReceived += OnServerReceviedMessage;
54+
server.RemoteCertificateValidationCallback += ValidateCertAsServer;
5155
server.StartServer();
5256

5357
Task[] toWait = new Task[clientAmount];
5458
for (int i = 0; i < clientAmount; i++)
5559
{
5660
var client = new SslByteMessageClient(ccert);
61+
client.RemoteCertificateValidationCallback += ValidateCertAsClient;
5762
client.BufferProvider = server.BufferProvider;
5863
client.OnBytesReceived += (buffer, offset, count) => OnClientReceivedMessage(client, buffer, offset, count);
5964
client.MaxIndexedMemory = server.MaxIndexedMemoryPerClient;
6065
client.Connect("127.0.0.1", 2008);
6166
clients.Add(client);
6267
}
6368

69+
bool ValidateCertAsClient(object sender, X509Certificate? certificate, X509Chain? chain, SslPolicyErrors sslPolicyErrors)
70+
{
71+
return true;
72+
}
73+
bool ValidateCertAsServer(object sender, X509Certificate? certificate, X509Chain? chain, SslPolicyErrors sslPolicyErrors)
74+
{
75+
return true;
76+
}
6477
// ----------------------- Bechmark ---------------------------
6578
Console.WriteLine("Press enter to start");
66-
Console.Read();
79+
Console.ReadLine();
6780
sw2.Start();
6881

69-
const int numMsg = 10000;
7082
Parallel.ForEach(clients, client =>
7183
{
7284
for (int i = 0; i < numMsg; i++)
@@ -83,7 +95,7 @@ private static void Bench()
8395
cl.SendAsync(new byte[502]);
8496
}
8597

86-
98+
// ----------------- End of stress test ---------------------
8799
Console.WriteLine("All messages are dispatched in :" + sw2.ElapsedMilliseconds +
88100
"ms. Press enter to see status");
89101
Console.ReadLine();
@@ -146,9 +158,7 @@ void OnServerReceviedMessage(Guid id, byte[] arg2, int offset, int count)
146158

147159
}
148160

149-
150-
151-
161+
152162
}
153163
}
154164

Benchmarks/SslBenchmark2/Program.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ private static void CustomSslTest()
3434
int lastTimeStamp = 1;
3535
int clientAmount = 100;
3636
const int numMsg = 10000;
37+
var message = new byte[3200];
38+
var response = new byte[3200];
3739

3840

3941
var scert = new X509Certificate2("server.pfx", "greenpass");
@@ -45,11 +47,7 @@ private static void CustomSslTest()
4547
Stopwatch sw2 = new Stopwatch();
4648
AutoResetEvent testCompletionEvent = new AutoResetEvent(false);
4749

48-
var message = new byte[3200];
49-
var response = new byte[3200];
50-
5150
server.MaxIndexedMemoryPerClient = 1280000000;
52-
5351
server.ClientSendBufsize = 128000;
5452
server.ClientReceiveBufsize = 128000;
5553
server.DropOnBackPressure = false;

Benchmarks/TcpBenchmark/Program.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ private static void TcpTest()
3535
int lastTimeStamp = 1;
3636
int clientAmount = 100;
3737
const int numMsg = 1000000;
38+
var message = new byte[32];
39+
var response = new byte[32];
3840

3941

4042
ByteMessageTcpServer server = new ByteMessageTcpServer(2008, clientAmount*2);
@@ -43,11 +45,6 @@ private static void TcpTest()
4345
Stopwatch sw2 = new Stopwatch();
4446
AutoResetEvent testCompletionEvent = new AutoResetEvent(false);
4547

46-
47-
48-
var message = new byte[32];
49-
var response = new byte[32];
50-
5148
server.MaxIndexedMemoryPerClient = 1280000000;
5249
server.ClientSendBufsize = 128000;
5350
server.ClientReceiveBufsize = 128000;
@@ -87,7 +84,7 @@ private static void TcpTest()
8784

8885
});
8986

90-
// final msg to get the tıme elapsed.
87+
// final msg to get the time elapsed.
9188
foreach (var cl in clients)
9289
{
9390
cl.SendAsync(new byte[502]);

NetworkLibrary/TCP/SSL/SslClient.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class SslClient:TcpClientBase
1515
#region Fields & Props
1616

1717
public BytesRecieved OnBytesReceived;
18-
public RemoteCertificateValidationCallback remoteCertificateValidationCallback;
18+
public RemoteCertificateValidationCallback RemoteCertificateValidationCallback;
1919

2020
protected Socket clientSocket;
2121
protected SslStream sslStream;
@@ -38,7 +38,7 @@ public BufferProvider BufferProvider
3838
public SslClient(X509Certificate2 certificate)
3939
{
4040
this.certificate = certificate;
41-
remoteCertificateValidationCallback += DefaultValidationCallbackHandler;
41+
RemoteCertificateValidationCallback += DefaultValidationCallbackHandler;
4242
}
4343

4444
private Socket GetSocket()
@@ -123,14 +123,14 @@ private void CheckBufferProvider()
123123

124124
protected virtual bool ValidateCeriticate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
125125
{
126-
return remoteCertificateValidationCallback.Invoke(sender, certificate, chain, sslPolicyErrors);
126+
return RemoteCertificateValidationCallback.Invoke(sender, certificate, chain, sslPolicyErrors);
127127

128128
}
129129

130130
private bool DefaultValidationCallbackHandler(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
131131
{
132-
return true;
133-
if (sslPolicyErrors == SslPolicyErrors.None || sslPolicyErrors == SslPolicyErrors.RemoteCertificateChainErrors)
132+
//return true;
133+
if (sslPolicyErrors == SslPolicyErrors.None)
134134
return true;
135135
return false;
136136
}

NetworkLibrary/TCP/SSL/SslServer.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public class SslServer : TcpServerBase
1717
{
1818
public BytesRecieved OnBytesReceived;
1919
public ClientAccepted OnClientAccepted;
20+
public RemoteCertificateValidationCallback RemoteCertificateValidationCallback;
21+
2022
// this returns bool
2123
public ClientConnectionRequest OnClientRequestedConnection;
2224

@@ -34,6 +36,7 @@ public SslServer(int port, int maxClients, X509Certificate2 certificate)
3436
this.certificate = certificate;
3537
BufferProvider = new BufferProvider(MaxClients, ClientSendBufsize, MaxClients, ClientReceiveBufsize);
3638
OnClientRequestedConnection = (socket) => true;
39+
RemoteCertificateValidationCallback += DefaultValidationCallback;
3740
}
3841

3942
public override void StartServer()
@@ -80,7 +83,12 @@ protected virtual bool ValidateConnection(Socket clientsocket)
8083
}
8184
private bool ValidateCeriticate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
8285
{
83-
if (sslPolicyErrors == SslPolicyErrors.None || sslPolicyErrors == SslPolicyErrors.RemoteCertificateChainErrors)
86+
return RemoteCertificateValidationCallback.Invoke(sender, certificate, chain, sslPolicyErrors);
87+
88+
}
89+
private bool DefaultValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
90+
{
91+
if (sslPolicyErrors == SslPolicyErrors.None)
8492
return true;
8593
return false;
8694
}

Tests/UnitTests/ProtocolConsistencyTests.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using System.Security.Cryptography.X509Certificates;
1313
using NetworkLibrary.TCP.SSL.ByteMessage;
1414
using NetworkLibrary.TCP.SSL.Custom;
15+
using System.Net.Security;
1516

1617
namespace UnitTests
1718
{
@@ -449,13 +450,14 @@ public void SSlMessageRushConsistencyTest()
449450
server.MaxIndexedMemoryPerClient = 128000000;
450451
server.DropOnBackPressure = false;
451452
server.OnBytesReceived += OnServerReceviedMessage;
452-
453+
server.RemoteCertificateValidationCallback += ValidateCertAsServer;
453454
server.StartServer();
454455

455456
Task[] toWait = new Task[clAmount];
456457
for (int i = 0; i < clAmount; i++)
457458
{
458459
var client = new SslByteMessageClient(ccert);
460+
client.RemoteCertificateValidationCallback += ValidateCertAsClient;
459461
client.MaxIndexedMemory = server.MaxIndexedMemoryPerClient;
460462
client.DropOnCongestion = false;
461463
client.OnBytesReceived += (byte[] arg2, int offset, int count) => OnClientReceivedMessage(client, arg2, offset, count);
@@ -467,7 +469,14 @@ public void SSlMessageRushConsistencyTest()
467469
clients.TryAdd(client, -1);
468470
}
469471

470-
//Task.WaitAll(toWait);
472+
bool ValidateCertAsClient(object sender, X509Certificate? certificate, X509Chain? chain, SslPolicyErrors sslPolicyErrors)
473+
{
474+
return true;
475+
}
476+
bool ValidateCertAsServer(object sender, X509Certificate? certificate, X509Chain? chain, SslPolicyErrors sslPolicyErrors)
477+
{
478+
return true;
479+
}
471480

472481
Thread.Sleep(1000);
473482
sw2.Start();
@@ -537,6 +546,7 @@ void OnServerReceviedMessage(Guid id, byte[] arg2, int offset, int count)
537546
// Send messages between 505-1000000 bytes and validate their order.
538547
public void SSlRandomSizeConsistencyTest()
539548
{
549+
540550
const int numMsg = 1000;
541551
int clAmount = 10;
542552
int completionCount = clAmount;
@@ -558,13 +568,14 @@ public void SSlRandomSizeConsistencyTest()
558568
server.MaxIndexedMemoryPerClient = 128000000;
559569
server.DropOnBackPressure = false;
560570
server.OnBytesReceived += OnServerReceviedMessage;
561-
571+
server.RemoteCertificateValidationCallback += ValidateCertAsServer;
562572
server.StartServer();
563573

564574
Task[] toWait = new Task[clAmount];
565575
for (int i = 0; i < clAmount; i++)
566576
{
567577
var client = new SslByteMessageClient(ccert);
578+
client.RemoteCertificateValidationCallback += ValidateCertAsClient;
568579
client.MaxIndexedMemory = server.MaxIndexedMemoryPerClient;
569580
client.DropOnCongestion = false;
570581
client.OnBytesReceived += (byte[] arg2, int offset, int count) => OnClientReceivedMessage(client, arg2, offset, count);
@@ -575,6 +586,15 @@ public void SSlRandomSizeConsistencyTest()
575586
clients.TryAdd(client, -1);
576587
}
577588

589+
590+
bool ValidateCertAsClient(object sender, X509Certificate? certificate, X509Chain? chain, SslPolicyErrors sslPolicyErrors)
591+
{
592+
return true;
593+
}
594+
bool ValidateCertAsServer(object sender, X509Certificate? certificate, X509Chain? chain, SslPolicyErrors sslPolicyErrors)
595+
{
596+
return true;
597+
}
578598
Task.WaitAll(toWait);
579599

580600
// Messages starts here

0 commit comments

Comments
 (0)