-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathEndpointState.cs
More file actions
177 lines (164 loc) · 5.26 KB
/
EndpointState.cs
File metadata and controls
177 lines (164 loc) · 5.26 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Threading.Tasks;
namespace Waher.Security.DTLS
{
/// <summary>
/// Maintains the communication state for a remote endpoint.
/// </summary>
public class EndpointState : IDisposable
{
internal object remoteEndpoint;
internal ICipher pendingCipher = null;
internal ICipher currentCipher = null;
internal ICipher previousCipher = null;
internal MemoryStream buffer = null;
internal DtlsState state = DtlsState.Created;
internal DtlsEndpoint localEndpoint;
internal IDtlsCredentials credentials;
internal byte[] handshake_client_hello = null;
internal byte[] handshake_server_hello = null;
internal byte[] handshake_server_certificate = null;
internal byte[] handshake_server_key_exchange = null;
internal byte[] handshake_certificate_request = null;
internal byte[] handshake_server_hello_done = null;
internal byte[] handshake_client_certificate = null;
internal byte[] handshake_client_key_exchange = null;
internal byte[] handshake_certificate_verify = null;
internal byte[] handshake_client_finished = null;
internal byte[] psk_identity_hint = null;
internal byte[] cookie = new byte[1] { 0 };
internal byte[] sessionId = new byte[1] { 0 };
internal byte[] cookieRandom = new byte[32];
internal byte[] clientRandom = new byte[32];
internal byte[] serverRandom = Array.Empty<byte>();
internal byte[] clientHandshakeHash = null;
internal byte[] serverHandshakeHash = null;
internal byte[] masterSecret = null;
internal byte[] client_write_MAC_key = null;
internal byte[] server_write_MAC_key = null;
internal byte[] client_write_key = null;
internal byte[] server_write_key = null;
internal byte[] client_write_IV = null;
internal byte[] server_write_IV = null;
internal ulong nonceCount = 0;
internal ulong receivedPacketsWindow = 0;
internal ulong leftEdgeSeqNr = 0;
internal ulong currentSeqNr = 0;
internal ulong previousReceivedPacketsWindow = 0;
internal ulong previousLeftEdgeSeqNr = 0;
internal ulong previousSeqNr = 0;
internal ushort previousFlightTxSeq = 0;
internal ushort previousFlightRxSeq = 0;
internal ushort currentEpoch = 0;
internal ushort message_seq = 0;
internal ushort next_receive_seq = 0;
internal ushort flightTxSeq = 0;
internal ushort flightRxSeq = 0;
internal bool acceptRollbackPrevEpoch = false;
internal bool isClient = true;
internal bool clientFinished = false;
internal bool serverFinished = false;
internal int timeoutSeconds = 1;
internal long flightNr = 0;
internal LinkedList<ResendableRecord> lastFlight = new LinkedList<ResendableRecord>();
internal DateTime timeout = DateTime.MinValue;
/// <summary>
/// Maintains the communication state for a remote endpoint.
/// </summary>
/// <param name="LocalEndpoint">Local endpoint.</param>
/// <param name="RemoteEndpoint">Remote endpoint.</param>
public EndpointState(DtlsEndpoint LocalEndpoint, object RemoteEndpoint)
{
this.localEndpoint = LocalEndpoint;
this.remoteEndpoint = RemoteEndpoint;
}
/// <summary>
/// If pre-shared keys (PSK) are used.
/// </summary>
internal bool UsesPsk
{
get
{
if (this.credentials is PresharedKey Psk)
return !(Psk.Identity is null) && !(Psk.Key is null);
else
return false;
}
}
/// <summary>
/// <see cref="IDisposable.Dispose"/>
/// </summary>
public void Dispose()
{
if (!(this.buffer is null))
{
this.buffer.Dispose();
this.buffer = null;
}
this.clientHandshakeHash = null;
this.serverHandshakeHash = null;
}
/// <summary>
/// Remote endpoint.
/// </summary>
public object RemoteEndpoint => this.remoteEndpoint;
/// <summary>
/// Current DTLS state.
/// </summary>
public DtlsState State => this.state;
/// <summary>
/// Sets the current DTLS state.
/// </summary>
internal async Task SetState(DtlsState NewState)
{
if (this.state != NewState)
{
this.state = NewState;
await this.localEndpoint.StateChanged(this, NewState);
}
}
internal void CalcClientHandshakeHash()
{
if (this.clientHandshakeHash is null)
{
using (IncrementalHash H = IncrementalHash.CreateHash(HashAlgorithmName.SHA256))
{
this.AppendClient(H);
this.clientHandshakeHash = H.GetHashAndReset();
}
}
}
private void AppendClient(IncrementalHash H)
{
this.Append(this.handshake_client_hello, H);
this.Append(this.handshake_server_hello, H);
this.Append(this.handshake_server_certificate, H);
this.Append(this.handshake_server_key_exchange, H);
this.Append(this.handshake_certificate_request, H);
this.Append(this.handshake_server_hello_done, H);
this.Append(this.handshake_client_certificate, H);
this.Append(this.handshake_client_key_exchange, H);
this.Append(this.handshake_certificate_verify, H);
}
internal void CalcServerHandshakeHash()
{
if (this.serverHandshakeHash is null)
{
using (IncrementalHash H = IncrementalHash.CreateHash(HashAlgorithmName.SHA256))
{
this.AppendClient(H);
this.Append(this.handshake_client_finished, H);
this.serverHandshakeHash = H.GetHashAndReset();
}
}
}
private void Append(byte[] Msg, IncrementalHash H)
{
if (!(Msg is null))
H.AppendData(Msg);
}
}
}