Skip to content

Commit 18a7f21

Browse files
committed
Automatic certificate generation & bug fixes
1 parent 201e14d commit 18a7f21

37 files changed

+820
-322
lines changed

Benchmarks/RelayBenchmark/Program.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,11 @@ private static void RelayTest()
189189

190190
var cert = new X509Certificate2("client.pfx", "greenpass");
191191
var scert = new X509Certificate2("server.pfx", "greenpass");
192-
var server = new SecureProtoRelayServer(20020, scert);
193-
server.StartServer();
192+
//var server = new SecureProtoRelayServer(20020, scert);
193+
//server.StartServer();
194194
//Task.Run(async () => { while (true) { await Task.Delay(10000); server.GetTcpStatistics(out var generalStats, out _); Console.WriteLine(generalStats.ToString()); } });
195195
var clients = new List<RelayClient>();
196-
int numclients = 2;
196+
int numclients = 30;
197197
var pending = new Task[numclients];
198198
Task.Run(async () => { while (true) { await Task.Delay(1000); Console.WriteLine(Interlocked.Exchange(ref sumsum, 0).ToString("N0")); } });
199199

@@ -208,7 +208,7 @@ private static void RelayTest()
208208
try
209209
{
210210
pending[i] = client.ConnectAsync(ip, 20020);
211-
//client.StartPingService();
211+
client.StartPingService();
212212
// client.Connect(ip, 20011);
213213
clients.Add(client);
214214
//client.StartPingService();
@@ -235,10 +235,10 @@ private static void RelayTest()
235235
if (peer.Key == Guid.Empty)
236236
throw new Exception();
237237

238-
// var a = client.RequestTcpHolePunchAsync(peer.Key);
239-
// pndg.Add(a);
240-
var aa = client.RequestHolePunchAsync(peer.Key, 10000, false);
241-
pndg.Add(aa);
238+
var a = client.RequestTcpHolePunchAsync(peer.Key);
239+
pndg.Add(a);
240+
// var aa = client.RequestHolePunchAsync(peer.Key, 10000, false);
241+
//pndg.Add(aa);
242242
//client.TestHP(peer.Key, 10000, false);
243243
// Console.WriteLine(peer.Key+" cnt=> "+ ++cc);
244244
}
@@ -258,7 +258,7 @@ private static void RelayTest()
258258
var testMessage = new MessageEnvelope()
259259
{
260260
Header = "Test",
261-
Payload = new byte[320000]
261+
Payload = new byte[320]
262262
};
263263
for (int i = 0; i < testMessage.PayloadCount; i++)
264264
{
@@ -270,8 +270,8 @@ private static void RelayTest()
270270
foreach (var peer in client.Peers.Keys)
271271
{
272272
//await client.SendRequestAndWaitResponse(peer, testMessage,1000);
273-
// client.SendAsyncMessage(peer, testMessage);
274-
client.SendUdpMessage(peer, testMessage);
273+
client.SendAsyncMessage(peer, testMessage);
274+
//client.SendUdpMessage(peer, testMessage);
275275
// client.SendRudpMessage(peer, testMessage);
276276
// client.BroadcastMessage(testMessage);
277277
//client.BroadcastUdpMessage(testMessage);

Benchmarks/SslBenchmark/Program.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using NetworkLibrary.Components.Statistics;
1+
using NetworkLibrary.Components.Crypto.Certificate;
2+
using NetworkLibrary.Components.Statistics;
23
using NetworkLibrary.TCP.SSL.ByteMessage;
34
using NetworkLibrary.Utils;
45
using System.Diagnostics;
@@ -88,7 +89,7 @@ private static void InitializeClients()
8889
{
8990
clientMessage = new byte[messageSize];
9091
clients = new List<SslByteMessageClient>();
91-
var ccert = new X509Certificate2("client.pfx", "greenpass");
92+
var ccert = CertificateGenerator.GenerateSelfSignedCertificate();//new X509Certificate2("client.pfx", "greenpass");
9293

9394
for (int i = 0; i < numClients; i++)
9495
{
@@ -133,7 +134,7 @@ private static void InitializeServer()
133134
fixedMessage = isFixedMessage ? new byte[fixedMessageSize] : new byte[0];
134135
var scert = new X509Certificate2("server.pfx", "greenpass");
135136

136-
server = new SslByteMessageServer(port, scert);
137+
server = new SslByteMessageServer(port);
137138
server.RemoteCertificateValidationCallback
138139
+= ValidateCertAsClient;
139140

NetworkLibrary/Components/ConcurrentAesAlgorithm.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ private byte[] GenerateIV(byte[] key)
3434
{
3535
SHA256 sHA = SHA256.Create();
3636
var hash = sHA.ComputeHash(key);
37+
sHA.Dispose();
3738
return ByteCopy.ToArray(hash, 0, 16);
3839
}
3940

NetworkLibrary/Components/Crypto/AesManager.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Concurrent;
33
using System.Collections.Generic;
44
using System.Drawing;
5+
using System.Security.Cryptography.X509Certificates;
56
using System.Text;
67
using NetworkLibrary.Components.Crypto.Algorithms;
78
using NetworkLibrary.Utils;
@@ -19,6 +20,7 @@ internal class AesManager:IAesEngine
1920

2021
public AesManager(byte[] Key,byte[] IV, AesMode mode)
2122
{
23+
2224
this.Key = Key;
2325
this.IV = IV;
2426
this.AesMode = mode;
@@ -46,10 +48,7 @@ private IAesAlgorithm Create()
4648
return new AesCbcRandIVAlgorithm(Key, IV);
4749
case AesMode.GCM:
4850
//return new AesGcmManagedAlgorithm(Key, IV);
49-
#if NETSTANDARD2_1_OR_GREATER
50-
return new AesGcmAlgorithm(Key,IV);
51-
52-
#elif NET5_0_OR_GREATER
51+
#if NETSTANDARD2_1_OR_GREATER || NET5_0_OR_GREATER
5352
return new AesGcmAlgorithm(Key,IV);
5453
#endif
5554
return new AesGcmManagedAlgorithm(Key,IV);

NetworkLibrary/Components/Crypto/Algorithms/AesGcmAlgorithm.cs

Lines changed: 1 addition & 210 deletions
Original file line numberDiff line numberDiff line change
@@ -9,217 +9,8 @@
99

1010
namespace NetworkLibrary.Components.Crypto.Algorithms
1111
{
12-
#if NETSTANDARD2_1_OR_GREATER
13-
internal class AesGcmAlgorithm : IAesAlgorithm
14-
{
15-
16-
public int DecryptorInputBlockSize => 16;
17-
18-
public int DecryptorOutputBlockSize => 16;
19-
20-
public int EncryptorInputBlockSize => 16;
21-
22-
public int EncryptorOutputBlockSize => 16;
23-
byte[] iv;
24-
25-
private AesGcm aes;
26-
private AesGcm aes2;
27-
const int nonceSize = 12;
28-
const int tagSize = 14;
29-
public AesGcmAlgorithm(byte[] Key, byte[] IV)
30-
{
31-
aes = new AesGcm(Key);
32-
aes2 = new AesGcm(Key);
33-
iv = IV;
34-
}
35-
36-
public byte[] Decrypt(byte[] message)
37-
{
38-
return Decrypt(message, 0, message.Length);
39-
}
40-
41-
public byte[] Decrypt(byte[] buffer, int offset, int count)
42-
{
43-
var buff = BufferPool.RentBuffer(count + 256);
44-
int am = DecryptInto(buffer, offset, count, buff, 0);
45-
var res = ByteCopy.ToArray(buff, 0, am);
46-
BufferPool.ReturnBuffer(buff);
47-
return res;
48-
}
49-
50-
public void Dispose()
51-
{
52-
aes?.Dispose();
53-
aes2?.Dispose();
54-
}
55-
56-
public byte[] Encrypt(byte[] message)
57-
{
58-
return Encrypt(message, 0, message.Length);
59-
}
60-
61-
public byte[] Encrypt(byte[] buffer, int offset, int count)
62-
{
63-
var buff = BufferPool.RentBuffer(count + 256);
64-
int am = EncryptInto(buffer, offset, count, buff, 0);
65-
var res = ByteCopy.ToArray(buff, 0, am);
66-
BufferPool.ReturnBuffer(buff);
67-
return res;
68-
}
69-
70-
public int GetEncriptorOutputSize(int inputSize)
71-
{
72-
return nonceSize + tagSize + inputSize;
73-
}
74-
75-
public int EncryptInto(byte[] data, int offset, int count, byte[] output, int outputOffset)
76-
{
77-
int cipherSize = count;
78-
var non = GenerateNextNonce(out int nonceSize);
79-
80-
int encryptedDataLength = nonceSize + tagSize + cipherSize;
81-
Span<byte> encryptedData;
82-
unsafe
83-
{
84-
fixed (byte* buffer = &output[outputOffset])
85-
{
86-
encryptedData = new Span<byte>(buffer, encryptedDataLength);
87-
}
88-
}
89-
Buffer.BlockCopy(non, 4, output, outputOffset, nonceSize);
90-
91-
var nonce = encryptedData.Slice(0, nonceSize);
92-
var tag = encryptedData.Slice(nonceSize + cipherSize, tagSize);
93-
var cipherBytes = encryptedData.Slice(nonceSize, cipherSize);
94-
95-
// RandomNumberGenerator.Fill(nonce);
96-
97-
unsafe
98-
{
99-
fixed (byte* buffer = &data[offset])
100-
aes.Encrypt(non, new ReadOnlySpan<byte>(buffer, count), cipherBytes, tag);
101-
}
102-
103-
return encryptedDataLength;
104-
}
105-
public int EncryptInto(byte[] data, int offset, int count,
106-
byte[] data1, int offset1, int count1, byte[] output, int outputOffset)
107-
{
108-
{
109-
var non = GenerateNextNonce(out int nonceSize);
110-
111-
int cipherSize = count + count1;
112-
113-
int encryptedDataLength = tagSize + nonceSize + cipherSize;
114-
Span<byte> encryptedData = new Span<byte>(output);
115-
unsafe
116-
{
117-
fixed (byte* buffer = &output[outputOffset])
118-
{
119-
encryptedData = new Span<byte>(buffer, encryptedDataLength);
120-
}
121-
}
122-
Buffer.BlockCopy(non, 4, output, outputOffset, nonceSize);
123-
124-
var nonce = encryptedData.Slice(0, nonceSize);
125-
var tag = encryptedData.Slice(nonceSize + cipherSize, tagSize);
126-
var cipherBytes = encryptedData.Slice(nonceSize, cipherSize);
127-
128-
byte[] b = BufferPool.RentBuffer(cipherSize);
129-
Buffer.BlockCopy(data, offset, b, 0, count);
130-
Buffer.BlockCopy(data1, offset1, b, count, count1);
131-
// Generate secure nonce
132-
133-
unsafe
134-
{
135-
fixed (byte* buffer = &b[0])
136-
aes.Encrypt(non, new ReadOnlySpan<byte>(buffer, cipherSize), cipherBytes, tag);
137-
138-
}
139-
BufferPool.ReturnBuffer(b);
140-
return encryptedDataLength;
141-
}
142-
}
143-
public int DecryptInto(byte[] data, int offset, int count, byte[] output, int outputOffset)
144-
{
145-
Span<byte> encryptedData;
146-
unsafe
147-
{
148-
fixed (byte* buffer = &data[offset])
149-
{
150-
encryptedData = new Span<byte>(buffer, count);
151-
}
152-
}
153-
154-
int oldOff = offset;
155-
var nonce = ParseNonce(data, ref offset, ref count).AsSpan();
156-
int nonceSize = offset - oldOff;
157-
158-
int cipherSize = encryptedData.Length - nonceSize - tagSize;
159-
160-
// var nonce = encryptedData.Slice(0, nonceSize);
161-
var tag = encryptedData.Slice(nonceSize + cipherSize, tagSize);
162-
var cipherBytes = encryptedData.Slice(nonceSize, cipherSize);
163-
164-
unsafe
165-
{
166-
fixed (byte* buffer = &output[outputOffset])
167-
{
168-
aes2.Decrypt(nonce, cipherBytes, tag, new Span<byte>(buffer, cipherSize));
169-
}
170-
}
171-
172-
return cipherSize;
173-
}
174-
175-
176-
[ThreadStatic]
177-
static byte[] nonce;
178-
long ctr = 0;
179-
180-
private byte[] GetEmptyNonceArray()
181-
{
182-
if (true || nonce == null)
183-
{
184-
nonce = new byte[12];
185-
nonce[0] = iv[0];
186-
nonce[1] = iv[1];
187-
nonce[2] = iv[2];
188-
nonce[3] = iv[3];
189-
return nonce;
190-
191-
}
192-
int off = 4;
193-
PrimitiveEncoder.WriteFixedInt64(nonce, ref off, 0);
194-
return nonce;
195-
}
196-
private byte[] GenerateNextNonce(out int encodedCount)
197-
{
198-
long n = Interlocked.Increment(ref ctr);
199-
var nonce = GetEmptyNonceArray();// fill random 4 in begining and 8 0s after
200-
int off = 4;
201-
202-
PrimitiveEncoder.WriteInt64(nonce, ref off, n);// encode counter
203-
encodedCount = off - 4;
204-
return nonce;
205-
}
206-
private byte[] ParseNonce(byte[] source, ref int sourceOffset, ref int sourceCount)
207-
{
208-
var nonce = GetEmptyNonceArray();
209-
int oldOff = sourceOffset;
210-
PrimitiveEncoder.ReadInt64(source, ref sourceOffset);
211-
212-
int n = sourceOffset - oldOff;
213-
Buffer.BlockCopy(source, oldOff, nonce, 4, n);
214-
sourceCount -= n;
21512

216-
return nonce;
217-
}
218-
219-
}
220-
221-
#endif
222-
#if NET5_0_OR_GREATER
13+
#if NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
22314
internal class AesGcmAlgorithm : IAesAlgorithm
22415
{
22516

0 commit comments

Comments
 (0)