1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
|
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using Hazel.Udp.FewerThreads;
using Hazel.Crypto;
namespace Hazel.Dtls
{
/// <summary>
/// Listens for new UDP-DTLS connections and creates UdpConnections for them.
/// </summary>
/// <inheritdoc />
public class DtlsConnectionListener : ThreadLimitedUdpConnectionListener
{
private const int MaxCertFragmentSizeV0 = 1200;
// Min MTU - UDP+IP header - 1 (for good measure. :))
private const int MaxCertFragmentSizeV1 = 576 - 32 - 1;
/// <summary>
/// Current state of handshake sequence
/// </summary>
enum HandshakeState
{
ExpectingHello,
ExpectingClientKeyExchange,
ExpectingChangeCipherSpec,
ExpectingFinish
}
/// <summary>
/// State to manage the current epoch `N`
/// </summary>
struct CurrentEpoch
{
public ulong NextOutgoingSequence;
public ulong NextExpectedSequence;
public ulong PreviousSequenceWindowBitmask;
public IRecordProtection RecordProtection;
public IRecordProtection PreviousRecordProtection;
// Need to keep these around so we can re-transmit our
// last handshake record flight
public ByteSpan ExpectedClientFinishedVerification;
public ByteSpan ServerFinishedVerification;
public ulong NextOutgoingSequenceForPreviousEpoch;
}
/// <summary>
/// State to manage the transition from the current
/// epoch `N` to epoch `N+1`
/// </summary>
struct NextEpoch
{
public ushort Epoch;
public HandshakeState State;
public CipherSuite SelectedCipherSuite;
public ulong NextOutgoingSequence;
public IHandshakeCipherSuite Handshake;
public IRecordProtection RecordProtection;
public ByteSpan ClientRandom;
public ByteSpan ServerRandom;
public Sha256Stream VerificationStream;
public ByteSpan ClientVerification;
public ByteSpan ServerVerification;
}
/// <summary>
/// Per-peer state
/// </summary>
sealed class PeerData : IDisposable
{
public ushort Epoch;
public bool CanHandleApplicationData;
public HazelDtlsSessionInfo Session;
public CurrentEpoch CurrentEpoch;
public NextEpoch NextEpoch;
public ConnectionId ConnectionId;
public readonly List<ByteSpan> QueuedApplicationDataMessage = new List<ByteSpan>();
public readonly ConcurrentBag<MessageReader> ApplicationData = new ConcurrentBag<MessageReader>();
public readonly ProtocolVersion ProtocolVersion;
public DateTime StartOfNegotiation;
public PeerData(ConnectionId connectionId, ulong nextExpectedSequenceNumber, ProtocolVersion protocolVersion)
{
ByteSpan block = new byte[2 * Finished.Size];
this.CurrentEpoch.ServerFinishedVerification = block.Slice(0, Finished.Size);
this.CurrentEpoch.ExpectedClientFinishedVerification = block.Slice(Finished.Size, Finished.Size);
this.ProtocolVersion = protocolVersion;
ResetPeer(connectionId, nextExpectedSequenceNumber);
}
public void ResetPeer(ConnectionId connectionId, ulong nextExpectedSequenceNumber)
{
Dispose();
this.Epoch = 0;
this.CanHandleApplicationData = false;
this.QueuedApplicationDataMessage.Clear();
this.CurrentEpoch.NextOutgoingSequence = 2; // Account for our ClientHelloVerify
this.CurrentEpoch.NextExpectedSequence = nextExpectedSequenceNumber;
this.CurrentEpoch.PreviousSequenceWindowBitmask = 0;
this.CurrentEpoch.RecordProtection = NullRecordProtection.Instance;
this.CurrentEpoch.PreviousRecordProtection = null;
this.CurrentEpoch.ServerFinishedVerification.SecureClear();
this.CurrentEpoch.ExpectedClientFinishedVerification.SecureClear();
this.NextEpoch.State = HandshakeState.ExpectingHello;
this.NextEpoch.RecordProtection = null;
this.NextEpoch.Handshake = null;
this.NextEpoch.ClientRandom = new byte[Random.Size];
this.NextEpoch.ServerRandom = new byte[Random.Size];
this.NextEpoch.VerificationStream = new Sha256Stream();
this.NextEpoch.ClientVerification = new byte[Finished.Size];
this.NextEpoch.ServerVerification = new byte[Finished.Size];
this.ConnectionId = connectionId;
this.StartOfNegotiation = DateTime.UtcNow;
}
public void Dispose()
{
this.CurrentEpoch.RecordProtection?.Dispose();
this.CurrentEpoch.PreviousRecordProtection?.Dispose();
this.NextEpoch.RecordProtection?.Dispose();
this.NextEpoch.Handshake?.Dispose();
this.NextEpoch.VerificationStream?.Dispose();
while (this.ApplicationData.TryTake(out var msg))
{
try
{
msg.Recycle();
}
catch { }
}
}
}
private RandomNumberGenerator random;
// Private key component of certificate's public key
private ByteSpan encodedCertificate;
private RSA certificatePrivateKey;
// HMAC key to validate ClientHello cookie
private ThreadedHmacHelper hmacHelper;
private HMAC CurrentCookieHmac {
get
{
return hmacHelper.GetCurrentCookieHmacsForThread();
}
}
private HMAC PreviousCookieHmac
{
get
{
return hmacHelper.GetPreviousCookieHmacsForThread();
}
}
private ConcurrentStack<ConnectionId> staleConnections = new ConcurrentStack<ConnectionId>();
private readonly ConcurrentDictionary<IPEndPoint, PeerData> existingPeers = new ConcurrentDictionary<IPEndPoint, PeerData>();
public int PeerCount => this.existingPeers.Count;
// TODO: Move these into an DtlsErrorStatistics class
public int NonPeerNonHelloPacketsDropped;
public int NonVerifiedFinishedHandshake;
public int NonPeerVerifyHelloRequests;
public int PeerVerifyHelloRequests;
private int connectionSerial_unsafe = 0;
private Timer staleConnectionUpkeep;
/// <summary>
/// Create a new instance of the DTLS listener
/// </summary>
/// <param name="numWorkers"></param>
/// <param name="endPoint"></param>
/// <param name="logger"></param>
/// <param name="ipMode"></param>
public DtlsConnectionListener(int numWorkers, IPEndPoint endPoint, ILogger logger, IPMode ipMode = IPMode.IPv4)
: base(numWorkers, endPoint, logger, ipMode)
{
this.random = RandomNumberGenerator.Create();
this.staleConnectionUpkeep = new Timer(this.HandleStaleConnections, null, 2500, 1000);
this.hmacHelper = new ThreadedHmacHelper(logger);
}
/// <inheritdoc />
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
this.staleConnectionUpkeep.Dispose();
this.random?.Dispose();
this.random = null;
this.hmacHelper?.Dispose();
this.hmacHelper = null;
foreach (var pair in this.existingPeers)
{
pair.Value.Dispose();
}
this.existingPeers.Clear();
}
/// <summary>
/// Set the certificate key pair for the listener
/// </summary>
/// <param name="certificate">Certificate for the server</param>
public void SetCertificate(X509Certificate2 certificate)
{
if (!certificate.HasPrivateKey)
{
throw new ArgumentException("Certificate must have a private key attached", nameof(certificate));
}
RSA privateKey = certificate.GetRSAPrivateKey();
if (privateKey == null)
{
throw new ArgumentException("Certificate must be signed by an RSA key", nameof(certificate));
}
this.certificatePrivateKey?.Dispose();
this.certificatePrivateKey = privateKey;
this.encodedCertificate = Certificate.Encode(certificate);
}
/// <summary>
/// Handle an incoming datagram from the network.
///
/// This is primarily a wrapper around ProcessIncomingMessage
/// to ensure `reader.Recycle()` is always called
/// </summary>
protected override void ReadCallback(MessageReader reader, IPEndPoint peerAddress, ConnectionId connectionId)
{
try
{
ByteSpan message = new ByteSpan(reader.Buffer, reader.Offset + reader.Position, reader.BytesRemaining);
this.ProcessIncomingMessage(message, peerAddress);
}
finally
{
reader.Recycle();
}
}
/// <summary>
/// Handle an incoming datagram from the network
/// </summary>
private void ProcessIncomingMessage(ByteSpan message, IPEndPoint peerAddress)
{
PeerData peer = null;
if (!this.existingPeers.TryGetValue(peerAddress, out peer))
{
lock (this.existingPeers)
{
if (!this.existingPeers.TryGetValue(peerAddress, out peer))
{
HandleNonPeerRecord(message, peerAddress);
return;
}
}
}
ConnectionId peerConnectionId;
lock (peer)
{
peerConnectionId = peer.ConnectionId;
// Each incoming packet may contain multiple DTLS
// records
while (message.Length > 0)
{
Record record;
if (!Record.Parse(out record, peer.ProtocolVersion, message))
{
this.Logger.WriteError($"Dropping malformed record from `{peerAddress}`");
return;
}
message = message.Slice(Record.Size);
if (message.Length < record.Length)
{
this.Logger.WriteError($"Dropping malformed record from `{peerAddress}` Length({record.Length}) AvailableBytes({message.Length})");
return;
}
ByteSpan recordPayload = message.Slice(0, record.Length);
message = message.Slice(record.Length);
// Early-out and drop ApplicationData records
if (record.ContentType == ContentType.ApplicationData && !peer.CanHandleApplicationData)
{
this.Logger.WriteInfo($"Dropping ApplicationData record from `{peerAddress}` Cannot process yet");
continue;
}
// Drop records from a different epoch
if (record.Epoch != peer.Epoch)
{
// Handle existing client negotiating a new connection
if (record.Epoch == 0 && record.ContentType == ContentType.Handshake)
{
ByteSpan handshakePayload = recordPayload;
Handshake handshake;
if (!Handshake.Parse(out handshake, recordPayload))
{
this.Logger.WriteError($"Dropping malformed re-negotiation Handshake from `{peerAddress}`");
continue;
}
handshakePayload = handshakePayload.Slice(Handshake.Size);
if (handshake.FragmentOffset != 0 || handshake.Length != handshake.FragmentLength)
{
this.Logger.WriteError($"Dropping fragmented re-negotiation Handshake from `{peerAddress}`");
continue;
}
else if (handshake.MessageType != HandshakeType.ClientHello)
{
this.Logger.WriteVerbose($"Dropping non-ClientHello re-negotiation Handshake from `{peerAddress}`");
continue;
}
else if (handshakePayload.Length < handshake.Length)
{
this.Logger.WriteError($"Dropping malformed re-negotiation Handshake from `{peerAddress}`: Length({handshake.Length}) AvailableBytes({handshakePayload.Length})");
}
if (!this.HandleClientHello(peer, peerAddress, ref record, ref handshake, recordPayload, handshakePayload))
{
return;
}
continue;
}
this.Logger.WriteVerbose($"Dropping bad-epoch record from `{peerAddress}` RecordEpoch({record.Epoch}) CurrentEpoch({peer.Epoch})");
continue;
}
// Prevent replay attacks by dropping records
// we've already processed
int windowIndex = (int)(peer.CurrentEpoch.NextExpectedSequence - record.SequenceNumber - 1);
ulong windowMask = 1ul << windowIndex;
if (record.SequenceNumber < peer.CurrentEpoch.NextExpectedSequence)
{
if (windowIndex >= 64)
{
this.Logger.WriteInfo($"Dropping too-old record from `{peerAddress}` Sequence({record.SequenceNumber}) Expected({peer.CurrentEpoch.NextExpectedSequence})");
continue;
}
if ((peer.CurrentEpoch.PreviousSequenceWindowBitmask & windowMask) != 0)
{
this.Logger.WriteInfo($"Dropping duplicate record from `{peerAddress}`");
continue;
}
}
// Validate record authenticity
int decryptedSize = peer.CurrentEpoch.RecordProtection.GetDecryptedSize(recordPayload.Length);
if (decryptedSize < 0)
{
this.Logger.WriteInfo($"Dropping malformed record: Length {recordPayload.Length} Decrypted length: {decryptedSize}");
continue;
}
ByteSpan decryptedPayload = recordPayload.ReuseSpanIfPossible(decryptedSize);
ProtocolVersion protocolVersion = peer.ProtocolVersion;
if (!peer.CurrentEpoch.RecordProtection.DecryptCiphertextFromClient(decryptedPayload, recordPayload, ref record))
{
this.Logger.WriteVerbose($"Dropping non-authentic {record.ContentType} record from `{peerAddress}`");
return;
}
recordPayload = decryptedPayload;
// Update our squence number bookeeping
if (record.SequenceNumber >= peer.CurrentEpoch.NextExpectedSequence)
{
int windowShift = (int)(record.SequenceNumber + 1 - peer.CurrentEpoch.NextExpectedSequence);
peer.CurrentEpoch.PreviousSequenceWindowBitmask <<= windowShift;
peer.CurrentEpoch.NextExpectedSequence = record.SequenceNumber + 1;
}
else
{
peer.CurrentEpoch.PreviousSequenceWindowBitmask |= windowMask;
}
// This is handy for debugging, but too verbose even for verbose.
// this.Logger.WriteVerbose($"Record type {record.ContentType} ({peer.NextEpoch.State})");
switch (record.ContentType)
{
case ContentType.ChangeCipherSpec:
if (peer.NextEpoch.State != HandshakeState.ExpectingChangeCipherSpec)
{
this.Logger.WriteError($"Dropping unexpected ChangeChiperSpec record from `{peerAddress}` State({peer.NextEpoch.State})");
break;
}
else if (peer.NextEpoch.RecordProtection == null)
{
///NOTE(mendsley): This _should_ not
/// happen on a well-formed server.
Debug.Assert(false, "How did we receive a ChangeCipherSpec message without a pending record protection instance?");
this.Logger.WriteError($"Dropping ChangeCipherSpec message from `{peerAddress}`: No pending record protection");
break;
}
if (!ChangeCipherSpec.Parse(recordPayload))
{
this.Logger.WriteError($"Dropping malformed ChangeCipherSpec message from `{peerAddress}`");
break;
}
// Migrate to the next epoch
peer.Epoch = peer.NextEpoch.Epoch;
peer.CanHandleApplicationData = false; // Need a Finished message
peer.CurrentEpoch.NextOutgoingSequenceForPreviousEpoch = peer.CurrentEpoch.NextOutgoingSequence;
peer.CurrentEpoch.PreviousRecordProtection?.Dispose();
peer.CurrentEpoch.PreviousRecordProtection = peer.CurrentEpoch.RecordProtection;
peer.CurrentEpoch.RecordProtection = peer.NextEpoch.RecordProtection;
peer.CurrentEpoch.NextOutgoingSequence = 1;
peer.CurrentEpoch.NextExpectedSequence = 1;
peer.CurrentEpoch.PreviousSequenceWindowBitmask = 0;
peer.NextEpoch.ClientVerification.CopyTo(peer.CurrentEpoch.ExpectedClientFinishedVerification);
peer.NextEpoch.ServerVerification.CopyTo(peer.CurrentEpoch.ServerFinishedVerification);
peer.NextEpoch.State = HandshakeState.ExpectingHello;
peer.NextEpoch.Handshake?.Dispose();
peer.NextEpoch.Handshake = null;
peer.NextEpoch.NextOutgoingSequence = 1;
peer.NextEpoch.RecordProtection = null;
peer.NextEpoch.VerificationStream.Reset();
peer.NextEpoch.ClientVerification.SecureClear();
peer.NextEpoch.ServerVerification.SecureClear();
break;
case ContentType.Alert:
this.Logger.WriteError($"Dropping unsupported Alert record from `{peerAddress}`");
break;
case ContentType.Handshake:
if (!ProcessHandshake(peer, peerAddress, ref record, recordPayload))
{
return;
}
break;
case ContentType.ApplicationData:
// Forward data to the application
MessageReader reader = MessageReader.GetSized(recordPayload.Length);
reader.Length = recordPayload.Length;
recordPayload.CopyTo(reader.Buffer);
peer.ApplicationData.Add(reader);
break;
}
}
}
// The peer lock must be exited before leaving the DtlsConnectionListener context to prevent deadlocks
// because ApplicationData processing may reenter this context
while (peer.ApplicationData.TryTake(out var appMsg))
{
base.ReadCallback(appMsg, peerAddress, peerConnectionId);
}
}
/// <summary>
/// Process an incoming Handshake protocol message
/// </summary>
/// <param name="peer">Originating peer</param>
/// <param name="peerAddress">Peer's network address</param>
/// <param name="record">Parent record</param>
/// <param name="message">Record payload</param>
/// <returns>
/// True if further processing of the underlying datagram
/// should be continues. Otherwise, false.
/// </returns>
private bool ProcessHandshake(PeerData peer, IPEndPoint peerAddress, ref Record record, ByteSpan message)
{
// Each record may have multiple handshake payloads
while (message.Length > 0)
{
ByteSpan originalMessage = message;
Handshake handshake;
if (!Handshake.Parse(out handshake, message))
{
this.Logger.WriteError($"Dropping malformed Handshake message from `{peerAddress}`");
return false;
}
message = message.Slice(Handshake.Size);
if (message.Length < handshake.Length)
{
this.Logger.WriteError($"Dropping malformed Handshake message from `{peerAddress}`");
return false;
}
ByteSpan payload = message.Slice(0, (int)message.Length);
message = message.Slice((int)handshake.Length);
originalMessage = originalMessage.Slice(0, Handshake.Size + (int)handshake.Length);
// We do not support fragmented handshake messages
// from the client
if (handshake.FragmentOffset != 0 || handshake.FragmentLength != handshake.Length)
{
this.Logger.WriteError($"Dropping fragmented Handshake message from `{peerAddress}` Offset({handshake.FragmentOffset}) FragmentLength({handshake.FragmentLength}) Length({handshake.Length})");
continue;
}
ByteSpan packet;
ByteSpan writer;
#if DEBUG
this.Logger.WriteVerbose($"Received handshake {handshake.MessageType} ({peer.NextEpoch.State})");
#endif
switch (handshake.MessageType)
{
case HandshakeType.ClientHello:
if (!this.HandleClientHello(peer, peerAddress, ref record, ref handshake, originalMessage, payload))
{
return false;
}
break;
case HandshakeType.ClientKeyExchange:
if (peer.NextEpoch.State != HandshakeState.ExpectingClientKeyExchange)
{
this.Logger.WriteError($"Dropping unexpected ClientKeyExchange message form `{peerAddress}` State({peer.NextEpoch.State})");
continue;
}
else if (handshake.MessageSequence != 5)
{
this.Logger.WriteError($"Dropping bad-sequence ClientKeyExchange message from `{peerAddress}` MessageSequence({handshake.MessageSequence})");
continue;
}
ByteSpan sharedSecret = new byte[peer.NextEpoch.Handshake.SharedKeySize()];
if (!peer.NextEpoch.Handshake.VerifyClientMessageAndGenerateSharedKey(sharedSecret, payload))
{
this.Logger.WriteError($"Dropping malformed ClientKeyExchange message from `{peerAddress}`");
return false;
}
// Record incoming ClientKeyExchange message
// to verification stream
peer.NextEpoch.VerificationStream.AddData(originalMessage);
ByteSpan randomSeed = new byte[2 * Random.Size];
peer.NextEpoch.ClientRandom.CopyTo(randomSeed);
peer.NextEpoch.ServerRandom.CopyTo(randomSeed.Slice(Random.Size));
const int MasterSecretSize = 48;
ByteSpan masterSecret = new byte[MasterSecretSize];
PrfSha256.ExpandSecret(
masterSecret
, sharedSecret
, PrfLabel.MASTER_SECRET
, randomSeed
);
// Create the record protection for the upcoming epoch
switch (peer.NextEpoch.SelectedCipherSuite)
{
case CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256:
peer.NextEpoch.RecordProtection = new Aes128GcmRecordProtection(
masterSecret
, peer.NextEpoch.ServerRandom
, peer.NextEpoch.ClientRandom);
break;
default:
Debug.Assert(false, $"How did we agree to a cipher suite {peer.NextEpoch.SelectedCipherSuite} we can't create?");
this.Logger.WriteError($"Dropping ClientKeyExchange message from `{peerAddress}` Unsuppored cipher suite");
return false;
}
// Generate verification signatures
ByteSpan handshakeStreamHash = new byte[Sha256Stream.DigestSize];
peer.NextEpoch.VerificationStream.CopyOrCalculateFinalHash(handshakeStreamHash);
PrfSha256.ExpandSecret(
peer.NextEpoch.ClientVerification
, masterSecret
, PrfLabel.CLIENT_FINISHED
, handshakeStreamHash
);
PrfSha256.ExpandSecret(
peer.NextEpoch.ServerVerification
, masterSecret
, PrfLabel.SERVER_FINISHED
, handshakeStreamHash
);
// Update handshake state
masterSecret.SecureClear();
peer.NextEpoch.State = HandshakeState.ExpectingChangeCipherSpec;
break;
case HandshakeType.Finished:
// Unlike other handshake messages, this is
// for the current epoch - not the next epoch
// Cannot process a Finished message for
// epoch 0
if (peer.Epoch == 0)
{
this.Logger.WriteError($"Dropping Finished message for 0-epoch from `{peerAddress}`");
continue;
}
// Cannot process a Finished message when we
// are negotiating the next epoch
else if (peer.NextEpoch.State != HandshakeState.ExpectingHello)
{
this.Logger.WriteError($"Dropping Finished message while negotiating new epoch from `{peerAddress}`");
continue;
}
// Cannot process a Finished message without
// verify data
else if (peer.CurrentEpoch.ExpectedClientFinishedVerification.Length != Finished.Size || peer.CurrentEpoch.ServerFinishedVerification.Length != Finished.Size)
{
///NOTE(mendsley): This _should_ not
/// happen on a well-formed server.
Debug.Assert(false, "How do we have an established non-zero epoch without verify data?");
this.Logger.WriteError($"Dropping Finished message (no verify data) from `{peerAddress}`");
return false;
}
// Cannot process a Finished message without
// record protection for the previous epoch
else if (peer.CurrentEpoch.PreviousRecordProtection == null)
{
///NOTE(mendsley): This _should_ not
/// happen on a well-formed server.
Debug.Assert(false, "How do we have an established non-zero epoch with record protection for the previous epoch?");
this.Logger.WriteError($"Dropping Finished message from `{peerAddress}`: No previous epoch record protection");
return false;
}
// Verify message sequence
if (handshake.MessageSequence != 6)
{
this.Logger.WriteError($"Dropping bad-sequence Finished message from `{peerAddress}` MessageSequence({handshake.MessageSequence})");
continue;
}
// Verify the client has the correct
// handshake sequence
if (payload.Length != Finished.Size)
{
this.Logger.WriteError($"Dropping malformed Finished message from `{peerAddress}`");
return false;
}
else if (1 != Crypto.Const.ConstantCompareSpans(payload, peer.CurrentEpoch.ExpectedClientFinishedVerification))
{
#if DEBUG
this.Logger.WriteError($"Dropping non-verified Finished Handshake from `{peerAddress}`");
#else
Interlocked.Increment(ref this.NonVerifiedFinishedHandshake);
#endif
// Abort the connection here
//
// The client is either broken, or
// doen not agree on our epoch settings.
//
// Either way, there is not a feasible
// way to progress the connection.
MarkConnectionAsStale(peer.ConnectionId);
this.existingPeers.TryRemove(peerAddress, out _);
return false;
}
ProtocolVersion protocolVersion = peer.ProtocolVersion;
// Describe our ChangeCipherSpec+Finished
Handshake outgoingHandshake = new Handshake();
outgoingHandshake.MessageType = HandshakeType.Finished;
outgoingHandshake.Length = Finished.Size;
outgoingHandshake.MessageSequence = 7;
outgoingHandshake.FragmentOffset = 0;
outgoingHandshake.FragmentLength = outgoingHandshake.Length;
Record changeCipherSpecRecord = new Record();
changeCipherSpecRecord.ContentType = ContentType.ChangeCipherSpec;
changeCipherSpecRecord.ProtocolVersion = protocolVersion;
changeCipherSpecRecord.Epoch = (ushort)(peer.Epoch - 1);
changeCipherSpecRecord.SequenceNumber = peer.CurrentEpoch.NextOutgoingSequenceForPreviousEpoch;
changeCipherSpecRecord.Length = (ushort)peer.CurrentEpoch.PreviousRecordProtection.GetEncryptedSize(ChangeCipherSpec.Size);
++peer.CurrentEpoch.NextOutgoingSequenceForPreviousEpoch;
int plaintextFinishedPayloadSize = Handshake.Size + (int)outgoingHandshake.Length;
Record finishedRecord = new Record();
finishedRecord.ContentType = ContentType.Handshake;
finishedRecord.ProtocolVersion = protocolVersion;
finishedRecord.Epoch = peer.Epoch;
finishedRecord.SequenceNumber = peer.CurrentEpoch.NextOutgoingSequence;
finishedRecord.Length = (ushort)peer.CurrentEpoch.RecordProtection.GetEncryptedSize(plaintextFinishedPayloadSize);
++peer.CurrentEpoch.NextOutgoingSequence;
// Encode the flight into wire format
packet = new byte[Record.Size + changeCipherSpecRecord.Length + Record.Size + finishedRecord.Length];
writer = packet;
changeCipherSpecRecord.Encode(writer);
writer = writer.Slice(Record.Size);
ChangeCipherSpec.Encode(writer);
ByteSpan startOfFinishedRecord = packet.Slice(Record.Size + changeCipherSpecRecord.Length);
writer = startOfFinishedRecord;
finishedRecord.Encode(writer);
writer = writer.Slice(Record.Size);
outgoingHandshake.Encode(writer);
writer = writer.Slice(Handshake.Size);
peer.CurrentEpoch.ServerFinishedVerification.CopyTo(writer);
// Protect the ChangeChipherSpec record
peer.CurrentEpoch.PreviousRecordProtection.EncryptServerPlaintext(
packet.Slice(Record.Size, changeCipherSpecRecord.Length),
packet.Slice(Record.Size, ChangeCipherSpec.Size),
ref changeCipherSpecRecord
);
// Protect the Finished Handshake record
peer.CurrentEpoch.RecordProtection.EncryptServerPlaintext(
startOfFinishedRecord.Slice(Record.Size, finishedRecord.Length),
startOfFinishedRecord.Slice(Record.Size, plaintextFinishedPayloadSize),
ref finishedRecord
);
// Current epoch can now handle application data
peer.CanHandleApplicationData = true;
base.QueueRawData(packet, peerAddress);
break;
// Drop messages that we do not support
case HandshakeType.CertificateVerify:
this.Logger.WriteError($"Dropping unsupported Handshake message from `{peerAddress}` MessageType({handshake.MessageType})");
continue;
// Drop messages that originate from the server
case HandshakeType.HelloRequest:
case HandshakeType.ServerHello:
case HandshakeType.HelloVerifyRequest:
case HandshakeType.Certificate:
case HandshakeType.ServerKeyExchange:
case HandshakeType.CertificateRequest:
case HandshakeType.ServerHelloDone:
this.Logger.WriteError($"Dropping server Handshake message from `{peerAddress}` MessageType({handshake.MessageType})");
continue;
}
}
return true;
}
/// <summary>
/// Handle a ClientHello message for a peer
/// </summary>
/// <param name="peer">Originating peer</param>
/// <param name="peerAddress">Peer address</param>
/// <param name="record">Parent record</param>
/// <param name="handshake">Parent Handshake header</param>
/// <param name="payload">Handshake payload</param>
private bool HandleClientHello(PeerData peer, IPEndPoint peerAddress, ref Record record, ref Handshake handshake, ByteSpan originalMessage, ByteSpan payload)
{
// Verify message sequence
if (handshake.MessageSequence != 0)
{
this.Logger.WriteError($"Dropping bad-sequence ClientHello from `{peerAddress}` MessageSequence({handshake.MessageSequence})`");
return true;
}
// Make sure we can handle a ClientHello message
if (peer.NextEpoch.State != HandshakeState.ExpectingHello && peer.NextEpoch.State != HandshakeState.ExpectingClientKeyExchange)
{
// Always handle ClientHello for epoch 0
if (record.Epoch != 0)
{
this.Logger.WriteError($"Dropping ClientHello from `{peer}` Not expecting ClientHello");
return true;
}
}
ProtocolVersion protocolVersion = peer.ProtocolVersion;
if (!ClientHello.Parse(out ClientHello clientHello, protocolVersion, payload))
{
this.Logger.WriteError($"Dropping malformed ClientHello Handshake message from `{peerAddress}`");
return false;
}
// Find an acceptable cipher suite we can use
CipherSuite selectedCipherSuite = CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256;
if (!clientHello.ContainsCipherSuite(CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) || !clientHello.ContainsCurve(NamedCurve.x25519))
{
this.Logger.WriteError($"Dropping ClientHello from `{peerAddress}` No compatible cipher suite");
return false;
}
// If this message was not signed by us,
// request a signed message before doing anything else
if (!HelloVerifyRequest.VerifyCookie(clientHello.Cookie, peerAddress, this.CurrentCookieHmac))
{
if (!HelloVerifyRequest.VerifyCookie(clientHello.Cookie, peerAddress, this.PreviousCookieHmac))
{
ulong outgoingSequence = 1;
IRecordProtection recordProtection = NullRecordProtection.Instance;
if (record.Epoch != 0)
{
outgoingSequence = peer.CurrentEpoch.NextExpectedSequence;
++peer.CurrentEpoch.NextOutgoingSequenceForPreviousEpoch;
recordProtection = peer.CurrentEpoch.RecordProtection;
}
#if DEBUG
this.Logger.WriteError($"Sending HelloVerifyRequest to peer `{peerAddress}`");
#else
Interlocked.Increment(ref this.PeerVerifyHelloRequests);
#endif
this.SendHelloVerifyRequest(peerAddress, outgoingSequence, record.Epoch, recordProtection, protocolVersion);
return true;
}
}
// Client is initiating a brand new connection. We need
// to destroy the existing connection and establish a
// new session.
if (record.Epoch == 0 && peer.Epoch != 0)
{
ConnectionId oldConnectionId = peer.ConnectionId;
peer.ResetPeer(this.AllocateConnectionId(peerAddress), record.SequenceNumber + 1);
// Inform the parent layer that the existing
// connection should be abandoned.
MarkConnectionAsStale(oldConnectionId);
}
// Determine if this is an original message, or a retransmission
bool recordMessagesForVerifyData = false;
if (peer.NextEpoch.State == HandshakeState.ExpectingHello)
{
// Create our handhake cipher suite
IHandshakeCipherSuite handshakeCipherSuite = null;
switch (selectedCipherSuite)
{
case CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256:
if (clientHello.ContainsCurve(NamedCurve.x25519))
{
handshakeCipherSuite = new X25519EcdheRsaSha256(this.random);
}
else
{
this.Logger.WriteError($"Dropping ClientHello from `{peerAddress}` Could not create TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 cipher suite");
return false;
}
break;
default:
this.Logger.WriteError($"Dropping ClientHello from `{peerAddress}` Could not create handshake cipher suite");
return false;
}
peer.Session = clientHello.Session;
// Update the state of our epoch transition
peer.NextEpoch.Epoch = (ushort)(record.Epoch + 1);
peer.NextEpoch.State = HandshakeState.ExpectingClientKeyExchange;
peer.NextEpoch.SelectedCipherSuite = selectedCipherSuite;
peer.NextEpoch.Handshake = handshakeCipherSuite;
clientHello.Random.CopyTo(peer.NextEpoch.ClientRandom);
peer.NextEpoch.ServerRandom.FillWithRandom(this.random);
recordMessagesForVerifyData = true;
#if DEBUG
this.Logger.WriteVerbose($"ClientRandom: {peer.NextEpoch.ClientRandom} ServerRandom: {peer.NextEpoch.ServerRandom}");
#endif
// Copy the original ClientHello
// handshake to our verification stream
peer.NextEpoch.VerificationStream.AddData(
originalMessage.Slice(
0
, Handshake.Size + (int)handshake.Length
)
);
}
// The initial record flight from the server
// contains the following Handshake messages:
// * ServerHello
// * Certificate
// * ServerKeyExchange
// * ServerHelloDone
//
// The Certificate message is almost always
// too large to fit into a single datagram,
// so it is pre-fragmented
// (see `SetCertificates`). Therefore, we
// need to send multiple record packets for
// this flight.
//
// The first record contains the ServerHello
// handshake message, as well as the first
// portion of the Certificate message.
//
// We then send a record packet until the
// entire Certificate message has been sent
// to the client.
//
// The final record packet contains the
// ServerKeyExchange and the ServerHelloDone
// messages.
// Describe first record of the flight
ServerHello serverHello = new ServerHello();
serverHello.ServerProtocolVersion = protocolVersion;
serverHello.Random = peer.NextEpoch.ServerRandom;
serverHello.CipherSuite = selectedCipherSuite;
Handshake serverHelloHandshake = new Handshake();
serverHelloHandshake.MessageType = HandshakeType.ServerHello;
serverHelloHandshake.Length = ServerHello.MinSize;
serverHelloHandshake.MessageSequence = 1;
serverHelloHandshake.FragmentOffset = 0;
serverHelloHandshake.FragmentLength = serverHelloHandshake.Length;
int maxCertFragmentSize = peer.Session.Version == 0 ? MaxCertFragmentSizeV0 : MaxCertFragmentSizeV1;
// The first certificate data needs to leave room for
// * Record header
// * ServerHello header
// * ServerHello payload
// * Certificate header
var certificateData = this.encodedCertificate;
int initialCertPadding = Record.Size + Handshake.Size + serverHello.Size + Handshake.Size;
int certInitialFragmentSize = Math.Min(certificateData.Length, maxCertFragmentSize - initialCertPadding);
Handshake certificateHandshake = new Handshake();
certificateHandshake.MessageType = HandshakeType.Certificate;
certificateHandshake.Length = (uint)certificateData.Length;
certificateHandshake.MessageSequence = 2;
certificateHandshake.FragmentOffset = 0;
certificateHandshake.FragmentLength = (uint)certInitialFragmentSize;
int initialRecordPayloadSize = 0
+ Handshake.Size + serverHello.Size
+ Handshake.Size + (int)certificateHandshake.FragmentLength
;
Record initialRecord = new Record();
initialRecord.ContentType = ContentType.Handshake;
initialRecord.ProtocolVersion = protocolVersion;
initialRecord.Epoch = peer.Epoch;
initialRecord.SequenceNumber = peer.CurrentEpoch.NextOutgoingSequence;
initialRecord.Length = (ushort)peer.CurrentEpoch.RecordProtection.GetEncryptedSize(initialRecordPayloadSize);
++peer.CurrentEpoch.NextOutgoingSequence;
// Convert initial record of the flight to
// wire format
ByteSpan packet = new byte[Record.Size + initialRecord.Length];
ByteSpan writer = packet;
initialRecord.Encode(writer);
writer = writer.Slice(Record.Size);
serverHelloHandshake.Encode(writer);
writer = writer.Slice(Handshake.Size);
serverHello.Encode(writer);
writer = writer.Slice(ServerHello.MinSize);
certificateHandshake.Encode(writer);
writer = writer.Slice(Handshake.Size);
certificateData.Slice(0, certInitialFragmentSize).CopyTo(writer);
certificateData = certificateData.Slice(certInitialFragmentSize);
// Protect initial record of the flight
peer.CurrentEpoch.RecordProtection.EncryptServerPlaintext(
packet.Slice(Record.Size, initialRecord.Length),
packet.Slice(Record.Size, initialRecordPayloadSize),
ref initialRecord
);
base.QueueRawData(packet, peerAddress);
// Record record payload for verification
if (recordMessagesForVerifyData)
{
Handshake fullCeritficateHandshake = certificateHandshake;
fullCeritficateHandshake.FragmentLength = fullCeritficateHandshake.Length;
packet = new byte[Handshake.Size + ServerHello.MinSize + Handshake.Size];
writer = packet;
serverHelloHandshake.Encode(writer);
writer = writer.Slice(Handshake.Size);
serverHello.Encode(writer);
writer = writer.Slice(ServerHello.MinSize);
fullCeritficateHandshake.Encode(writer);
writer = writer.Slice(Handshake.Size);
peer.NextEpoch.VerificationStream.AddData(packet);
peer.NextEpoch.VerificationStream.AddData(this.encodedCertificate);
}
// Process additional certificate records
// Subsequent certificate data needs to leave room for
// * Record header
// * Certificate header
const int CertPadding = Record.Size + Handshake.Size;
while (certificateData.Length > 0)
{
int certFragmentSize = Math.Min(certificateData.Length, maxCertFragmentSize - CertPadding);
certificateHandshake.FragmentOffset += certificateHandshake.FragmentLength;
certificateHandshake.FragmentLength = (uint)certFragmentSize;
int additionalRecordPayloadSize = Handshake.Size + (int)certificateHandshake.FragmentLength;
Record additionalRecord = new Record();
additionalRecord.ContentType = ContentType.Handshake;
additionalRecord.ProtocolVersion = protocolVersion;
additionalRecord.Epoch = peer.Epoch;
additionalRecord.SequenceNumber = peer.CurrentEpoch.NextOutgoingSequence;
additionalRecord.Length = (ushort)peer.CurrentEpoch.RecordProtection.GetEncryptedSize(additionalRecordPayloadSize);
++peer.CurrentEpoch.NextOutgoingSequence;
// Convert record to wire format
packet = new byte[Record.Size + additionalRecord.Length];
writer = packet;
additionalRecord.Encode(writer);
writer = writer.Slice(Record.Size);
certificateHandshake.Encode(writer);
writer = writer.Slice(Handshake.Size);
certificateData.Slice(0, certFragmentSize).CopyTo(writer);
certificateData = certificateData.Slice(certFragmentSize);
// Protect record
peer.CurrentEpoch.RecordProtection.EncryptServerPlaintext(
packet.Slice(Record.Size, additionalRecord.Length),
packet.Slice(Record.Size, additionalRecordPayloadSize),
ref additionalRecord
);
base.QueueRawData(packet, peerAddress);
}
// Describe final record of the flight
Handshake serverKeyExchangeHandshake = new Handshake();
serverKeyExchangeHandshake.MessageType = HandshakeType.ServerKeyExchange;
serverKeyExchangeHandshake.Length = (uint)peer.NextEpoch.Handshake.CalculateServerMessageSize(this.certificatePrivateKey);
serverKeyExchangeHandshake.MessageSequence = 3;
serverKeyExchangeHandshake.FragmentOffset = 0;
serverKeyExchangeHandshake.FragmentLength = serverKeyExchangeHandshake.Length;
Handshake serverHelloDoneHandshake = new Handshake();
serverHelloDoneHandshake.MessageType = HandshakeType.ServerHelloDone;
serverHelloDoneHandshake.Length = 0;
serverHelloDoneHandshake.MessageSequence = 4;
serverHelloDoneHandshake.FragmentOffset = 0;
serverHelloDoneHandshake.FragmentLength = 0;
int finalRecordPayloadSize = 0
+ Handshake.Size + (int)serverKeyExchangeHandshake.Length
+ Handshake.Size + (int)serverHelloDoneHandshake.Length
;
Record finalRecord = new Record();
finalRecord.ContentType = ContentType.Handshake;
finalRecord.ProtocolVersion = protocolVersion;
finalRecord.Epoch = peer.Epoch;
finalRecord.SequenceNumber = peer.CurrentEpoch.NextOutgoingSequence;
finalRecord.Length = (ushort)peer.CurrentEpoch.RecordProtection.GetEncryptedSize(finalRecordPayloadSize);
++peer.CurrentEpoch.NextOutgoingSequence;
// Convert final record of the flight to wire
// format
packet = new byte[Record.Size + finalRecord.Length];
writer = packet;
finalRecord.Encode(writer);
writer = writer.Slice(Record.Size);
serverKeyExchangeHandshake.Encode(writer);
writer = writer.Slice(Handshake.Size);
peer.NextEpoch.Handshake.EncodeServerKeyExchangeMessage(writer, this.certificatePrivateKey);
writer = writer.Slice((int)serverKeyExchangeHandshake.Length);
serverHelloDoneHandshake.Encode(writer);
// Record record payload for verification
if (recordMessagesForVerifyData)
{
peer.NextEpoch.VerificationStream.AddData(
packet.Slice(
packet.Offset + Record.Size
, finalRecordPayloadSize
)
);
}
// Protect final record of the flight
peer.CurrentEpoch.RecordProtection.EncryptServerPlaintext(
packet.Slice(Record.Size, finalRecord.Length),
packet.Slice(Record.Size, finalRecordPayloadSize),
ref finalRecord
);
base.QueueRawData(packet, peerAddress);
return true;
}
/// <summary>
/// Handle an incoming packet that is not tied to an existing peer
/// </summary>
/// <param name="message">Incoming datagram</param>
/// <param name="peerAddress">Originating address</param>
private void HandleNonPeerRecord(ByteSpan message, IPEndPoint peerAddress)
{
Record record;
if (!Record.Parse(out record, expectedProtocolVersion: null, message))
{
this.Logger.WriteError($"Dropping malformed record from non-peer `{peerAddress}`");
return;
}
message = message.Slice(Record.Size);
// The protocol only supports receiving a single record
// from a non-peer.
if (record.Length != message.Length)
{
// NOTE(mendsley): This isn't always fatal.
// However, this is an indication that something
// fishy is going on. In the best case, there's a
// bug on the client or in the UDP stack (some
// stacks don't both to verify the checksum). In the
// worst case we're dealing with a malicious actor.
// In the malicious case, we'll end up dropping the
// connection later in the process.
if (message.Length < record.Length)
{
this.Logger.WriteInfo($"Dropping bad record from non-peer `{peerAddress}`. Msg length {message.Length} < {record.Length}");
return;
}
}
// We only accept zero-epoch records from non-peers
if (record.Epoch != 0)
{
///NOTE(mendsley): Not logging anything here, as
/// this could easily be latent data arriving from a
/// recently disconnected peer.
return;
}
// We only accept Handshake protocol messages from non-peers
if (record.ContentType != ContentType.Handshake)
{
this.Logger.WriteError($"Dropping non-handhsake message from non-peer `{peerAddress}`");
return;
}
ByteSpan originalMessage = message;
Handshake handshake;
if (!Handshake.Parse(out handshake, message))
{
this.Logger.WriteError($"Dropping malformed handshake message from non-peer `{peerAddress}`");
return;
}
// We only accept ClientHello messages from non-peers
if (handshake.MessageType != HandshakeType.ClientHello)
{
#if DEBUG
this.Logger.WriteError($"Dropping non-ClientHello ({handshake.MessageType}) message from non-peer `{peerAddress}`");
#else
Interlocked.Increment(ref this.NonPeerNonHelloPacketsDropped);
#endif
return;
}
message = message.Slice(Handshake.Size);
if (!ClientHello.Parse(out ClientHello clientHello, expectedProtocolVersion: null, message))
{
this.Logger.WriteError($"Dropping malformed ClientHello message from non-peer `{peerAddress}`");
return;
}
// If this ClientHello is not signed by us, request the
// client send us a signed message
if (!HelloVerifyRequest.VerifyCookie(clientHello.Cookie, peerAddress, this.CurrentCookieHmac))
{
if (!HelloVerifyRequest.VerifyCookie(clientHello.Cookie, peerAddress, this.PreviousCookieHmac))
{
#if DEBUG
this.Logger.WriteVerbose($"Sending HelloVerifyRequest to non-peer `{peerAddress}`");
#else
Interlocked.Increment(ref this.NonPeerVerifyHelloRequests);
#endif
this.SendHelloVerifyRequest(peerAddress, 1, 0, NullRecordProtection.Instance, clientHello.ClientProtocolVersion);
return;
}
}
// Allocate state for the new peer and register it
PeerData peer = new PeerData(this.AllocateConnectionId(peerAddress), record.SequenceNumber + 1, clientHello.ClientProtocolVersion);
this.ProcessHandshake(peer, peerAddress, ref record, originalMessage);
this.existingPeers[peerAddress] = peer;
}
//Send a HelloVerifyRequest handshake message to a peer
private void SendHelloVerifyRequest(IPEndPoint peerAddress, ulong recordSequence, ushort epoch, IRecordProtection recordProtection, ProtocolVersion protocolVersion)
{
Handshake handshake = new Handshake();
handshake.MessageType = HandshakeType.HelloVerifyRequest;
handshake.Length = HelloVerifyRequest.Size;
handshake.MessageSequence = 0;
handshake.FragmentOffset = 0;
handshake.FragmentLength = handshake.Length;
int plaintextPayloadSize = Handshake.Size + (int)handshake.Length;
Record record = new Record();
record.ContentType = ContentType.Handshake;
record.ProtocolVersion = protocolVersion;
record.Epoch = epoch;
record.SequenceNumber = recordSequence;
record.Length = (ushort)recordProtection.GetEncryptedSize(plaintextPayloadSize);
// Encode record to wire format
ByteSpan packet = new byte[Record.Size + record.Length];
ByteSpan writer = packet;
record.Encode(writer);
writer = writer.Slice(Record.Size);
handshake.Encode(writer);
writer = writer.Slice(Handshake.Size);
HelloVerifyRequest.Encode(writer, peerAddress, this.CurrentCookieHmac, protocolVersion);
// Protect record payload
recordProtection.EncryptServerPlaintext(
packet.Slice(Record.Size, record.Length),
packet.Slice(Record.Size, plaintextPayloadSize),
ref record
);
base.QueueRawData(packet, peerAddress);
}
/// <summary>
/// Handle a requrest to send a datagram to the network
/// </summary>
protected override void QueueRawData(ByteSpan span, IPEndPoint remoteEndPoint)
{
PeerData peer;
if (!this.existingPeers.TryGetValue(remoteEndPoint, out peer))
{
// Drop messages if we don't know how to send them
return;
}
lock (peer)
{
// If we're negotiating a new epoch, queue data
if (peer.Epoch == 0 || peer.NextEpoch.State != HandshakeState.ExpectingHello)
{
ByteSpan copyOfSpan = new byte[span.Length];
span.CopyTo(copyOfSpan);
peer.QueuedApplicationDataMessage.Add(copyOfSpan);
return;
}
ProtocolVersion protocolVersion = peer.ProtocolVersion;
// Send any queued application data now
for (int ii = 0, nn = peer.QueuedApplicationDataMessage.Count; ii != nn; ++ii)
{
ByteSpan queuedSpan = peer.QueuedApplicationDataMessage[ii];
Record outgoingRecord = new Record();
outgoingRecord.ContentType = ContentType.ApplicationData;
outgoingRecord.ProtocolVersion = protocolVersion;
outgoingRecord.Epoch = peer.Epoch;
outgoingRecord.SequenceNumber = peer.CurrentEpoch.NextOutgoingSequence;
outgoingRecord.Length = (ushort)peer.CurrentEpoch.RecordProtection.GetEncryptedSize(queuedSpan.Length);
++peer.CurrentEpoch.NextOutgoingSequence;
// Encode the record to wire format
ByteSpan packet = new byte[Record.Size + outgoingRecord.Length];
ByteSpan writer = packet;
outgoingRecord.Encode(writer);
writer = writer.Slice(Record.Size);
queuedSpan.CopyTo(writer);
// Protect the record
peer.CurrentEpoch.RecordProtection.EncryptServerPlaintext(
packet.Slice(Record.Size, outgoingRecord.Length),
packet.Slice(Record.Size, queuedSpan.Length),
ref outgoingRecord
);
base.QueueRawData(packet, remoteEndPoint);
}
peer.QueuedApplicationDataMessage.Clear();
{
Record outgoingRecord = new Record();
outgoingRecord.ContentType = ContentType.ApplicationData;
outgoingRecord.ProtocolVersion = protocolVersion;
outgoingRecord.Epoch = peer.Epoch;
outgoingRecord.SequenceNumber = peer.CurrentEpoch.NextOutgoingSequence;
outgoingRecord.Length = (ushort)peer.CurrentEpoch.RecordProtection.GetEncryptedSize(span.Length);
++peer.CurrentEpoch.NextOutgoingSequence;
// Encode the record to wire format
ByteSpan packet = new byte[Record.Size + outgoingRecord.Length];
ByteSpan writer = packet;
outgoingRecord.Encode(writer);
writer = writer.Slice(Record.Size);
span.CopyTo(writer);
// Protect the record
peer.CurrentEpoch.RecordProtection.EncryptServerPlaintext(
packet.Slice(Record.Size, outgoingRecord.Length),
packet.Slice(Record.Size, span.Length),
ref outgoingRecord
);
base.QueueRawData(packet, remoteEndPoint);
}
}
}
private void HandleStaleConnections(object _)
{
TimeSpan maxAge = TimeSpan.FromSeconds(2.5f);
DateTime now = DateTime.UtcNow;
foreach (KeyValuePair<IPEndPoint, PeerData> kvp in this.existingPeers)
{
PeerData peer = kvp.Value;
lock (peer)
{
if (peer.Epoch == 0 || peer.NextEpoch.State != HandshakeState.ExpectingHello)
{
TimeSpan negotiationAge = now - peer.StartOfNegotiation;
if (negotiationAge > maxAge)
{
MarkConnectionAsStale(peer.ConnectionId);
}
}
}
}
ConnectionId connectionId;
while (this.staleConnections.TryPop(out connectionId))
{
ThreadLimitedUdpServerConnection connection;
if (this.allConnections.TryGetValue(connectionId, out connection))
{
connection.Disconnect("Stale Connection", null);
}
}
}
protected void MarkConnectionAsStale(ConnectionId connectionId)
{
if (this.allConnections.ContainsKey(connectionId))
{
this.staleConnections.Push(connectionId);
}
}
/// <inheritdoc />
internal override void RemovePeerRecord(ConnectionId connectionId)
{
if (this.existingPeers.TryRemove(connectionId.EndPoint, out var peer))
{
peer.Dispose();
}
}
/// <summary>
/// Allocate a new connection id
/// </summary>
private ConnectionId AllocateConnectionId(IPEndPoint endPoint)
{
int rawSerialId = Interlocked.Increment(ref this.connectionSerial_unsafe);
return ConnectionId.Create(endPoint, rawSerialId);
}
}
}
|