Repository navigation
Expand file tree
/
Copy pathNCCPClient.cs
More file actions
326 lines (309 loc) · 14.7 KB
/
Copy pathNCCPClient.cs
File metadata and controls
326 lines (309 loc) · 14.7 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
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
using System.Collections.Concurrent;
using System.Globalization;
using System.Security.Cryptography;
using System.Text;
namespace ModemAPI
{
/// <summary>
/// Sends commands to an NCCP (Network Computers Control Protocol) server and waits for their results.
/// Thread-safe: several commands can be executed at the same time, also to different servers.
/// </summary>
/// <remarks>
/// The delivery isn't guaranteed: a request or its response can be lost, and then <see cref="NetworkExecuteCommand"/> throws
/// <see cref="TimeoutException"/>, though the command could have been executed. Repeating a command after a timeout can
/// execute it twice, so commands, that are repeated, have to tolerate that (be idempotent): that's up to the user.
/// </remarks>
public class NCCPClient : IDisposable
{
private readonly IModem Modem;
private readonly ConcurrentDictionary<string, PendingRequest> Pending = new();
private readonly Action<DataBlock, Action> ResponseListener;
private readonly Lock AttachLock = new();
private bool Attached = false;
private bool Disposed = false;
private sealed class PendingRequest
{
public required byte[]? Key;
public required string ClientAddress;
public required string ServerAddress;
public readonly TaskCompletionSource<string> Result = new(TaskCreationOptions.RunContinuationsAsynchronously);
}
/// <summary>
/// Most bytes of one packet, when a request is fragmented. 1 to 65535, default 1024.
/// </summary>
public ushort PacketSize { get; set; } = 1024;
/// <summary>
/// Gets invoked for responses, that are dropped: their tag didn't match (a wrong key, or they were damaged or forged),
/// or they break the format. The command waiting for them keeps waiting for a valid response till its timeout.
/// </summary>
public event Action<Exception> ErrorOccured = delegate { };
/// <summary>
/// Creates a new NCCP client.
/// </summary>
/// <param name="modem">Modem, through which the commands are sent; this client doesn't dial or drop it</param>
public NCCPClient(IModem modem)
{
ArgumentNullException.ThrowIfNull(modem);
Modem = modem;
ResponseListener = OnDataBlock;
}
/// <summary>
/// Executes a command on an NCCP server and returns its result.
/// </summary>
/// <param name="server">Address of the NCCP server</param>
/// <param name="syntax">The command, for example <c>Command;Arg1;(OtherCommand;Arg2)</c>. Special characters of the arguments
/// have to be escaped, see <see cref="Escape"/></param>
/// <param name="timeoutMs">How long to wait for the response, in milliseconds (more than 0)</param>
/// <param name="aes256gcmEncryptionKey"><see langword="null"/> or empty - the command and its result are sent without encryption;
/// 32 bytes - AES-256-GCM key, the same as the server has for the address of this modem</param>
/// <returns>The result of the command</returns>
/// <exception cref="ArgumentNullException">server or syntax is null</exception>
/// <exception cref="ArgumentException">server isn't a valid tetronet address</exception>
/// <exception cref="ArgumentOutOfRangeException">timeoutMs is not more than 0</exception>
/// <exception cref="InvalidDataException">The key is not empty and not 32 bytes long</exception>
/// <exception cref="ModemNotConnectedException">The modem isn't connected to the tetronet</exception>
/// <exception cref="TimeoutException">No valid response came in timeoutMs</exception>
/// <exception cref="ObjectDisposedException">This client is disposed</exception>
public string NetworkExecuteCommand(Address server, string syntax, int timeoutMs, byte[]? aes256gcmEncryptionKey)
{
ArgumentNullException.ThrowIfNull(server);
ArgumentNullException.ThrowIfNull(syntax);
if (!Address.Validate(server))
{
throw new ArgumentException($"'{server}' isn't a valid tetronet address", nameof(server));
}
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(timeoutMs);
byte[]? key = null;
if (aes256gcmEncryptionKey != null && aes256gcmEncryptionKey.Length != 0)
{
if (aes256gcmEncryptionKey.Length != NCCPProtocol.KeyLength)
{
throw new InvalidDataException($"AES-256-GCM key has to be exactly {NCCPProtocol.KeyLength} bytes long");
}
// copied, so changing the caller's array while waiting doesn't change the key used for the response
key = (byte[])aes256gcmEncryptionKey.Clone();
}
ObjectDisposedException.ThrowIf(Disposed, this);
Address? local = Modem.LocalModemAddress;
if (!Modem.IsModemConnected || local == null || local.IsEmpty)
{
throw new ModemNotConnectedException("the modem of the NCCP client isn't connected to the tetronet");
}
EnsureAttached();
byte[] plaintext = NCCPProtocol.Utf8.GetBytes(syntax);
byte[] nonce = RandomNumberGenerator.GetBytes(NCCPProtocol.NonceLength);
string id = Convert.ToHexStringLower(nonce);
PendingRequest request = new() { Key = key, ClientAddress = local.ToString(), ServerAddress = server.ToString() };
byte[] data;
string metadata;
if (key == null)
{
data = plaintext;
metadata = NCCPProtocol.PlainRequestPrefix + id;
}
else
{
string time = NCCPProtocol.FormatTime(DateTimeOffset.UtcNow.ToUnixTimeSeconds());
byte[] aad = NCCPProtocol.RequestAad(request.ClientAddress, request.ServerAddress, time);
data = NCCPProtocol.Encrypt(key, nonce, plaintext, aad, out byte[] tag);
metadata = NCCPProtocol.EncryptedRequestPrefix + id + ":" + time + ":" + Convert.ToHexStringLower(tag);
}
// the nonce is 12 random bytes, so two requests never share it
Pending[id] = request;
try
{
NCCPProtocol.Send(Modem, data, server, metadata, PacketSize);
if (!request.Result.Task.Wait(timeoutMs))
{
throw new TimeoutException($"NCCP server {server} didn't answer in {timeoutMs} ms");
}
return request.Result.Task.Result;
}
finally
{
Pending.TryRemove(id, out _);
}
}
/// <summary>
/// Escapes the special characters of an argument (<c>\</c>, <c>;</c>, <c>(</c>, <c>)</c>), so it's passed to the command as is.
/// </summary>
public static string Escape(string argument)
{
ArgumentNullException.ThrowIfNull(argument);
StringBuilder sb = new(argument.Length);
foreach (char c in argument)
{
if (c is '\\' or ';' or '(' or ')')
{
sb.Append('\\');
}
sb.Append(c);
}
return sb.ToString();
}
private void EnsureAttached()
{
lock (AttachLock)
{
ObjectDisposedException.ThrowIf(Disposed, this);
if (!Attached)
{
Modem.AttachReceiveEvent(ResponseListener);
Attached = true;
}
}
}
private void OnDataBlock(DataBlock block, Action k)
{
try
{
if (block.ConnectionID != NCCPProtocol.ConnectionID || block.QueryType != NCCPProtocol.QueryType || block.Metadata == null)
{
return;
}
string metadata = block.Metadata;
bool encrypted;
string id;
byte[]? nonce = null;
byte[]? tag = null;
if (metadata.StartsWith(NCCPProtocol.PlainResponsePrefix, StringComparison.Ordinal))
{
encrypted = false;
id = metadata[NCCPProtocol.PlainResponsePrefix.Length..];
}
else if (metadata.StartsWith(NCCPProtocol.EncryptedResponsePrefix, StringComparison.Ordinal))
{
encrypted = true;
string[] fields = metadata[NCCPProtocol.EncryptedResponsePrefix.Length..].Split(':');
if (fields.Length != 3)
{
throw new InvalidDataException("an encrypted NCCP response must have 3 fields in its metadata");
}
id = fields[0];
nonce = NCCPProtocol.ParseHex(fields[1], NCCPProtocol.NonceLength, "nonce");
tag = NCCPProtocol.ParseHex(fields[2], NCCPProtocol.TagLength, "tag");
}
else
{
// a request to a server on this modem, or something else
return;
}
if (!Pending.TryGetValue(id, out PendingRequest? request))
{
// a response to a request, that timed out already, or a repeated one
return;
}
byte[] data = [.. block.DataBytes];
string result;
if (request.Key == null)
{
if (encrypted)
{
throw new InvalidDataException($"NCCP server {block.Transmitter} sent an encrypted response to a request without encryption");
}
result = NCCPProtocol.Utf8.GetString(data);
}
else
{
if (!encrypted)
{
// never accepted: anyone could send it
throw new InvalidDataException($"NCCP response from {block.Transmitter} to an encrypted request isn't encrypted");
}
byte[] aad = NCCPProtocol.ResponseAad(request.ClientAddress, request.ServerAddress, id);
// throws on a wrong tag, and then nothing of the data is used
byte[] plaintext = NCCPProtocol.Decrypt(request.Key, nonce!, data, tag!, aad);
result = NCCPProtocol.Utf8.GetString(plaintext);
}
request.Result.TrySetResult(result);
}
catch (Exception ex)
{
ErrorOccured(ex);
}
}
/// <summary>
/// Stops listening to responses; commands in progress time out.
/// </summary>
public void Dispose()
{
lock (AttachLock)
{
if (Disposed)
{
return;
}
Disposed = true;
if (Attached)
{
Modem.DetachReceiveEvent(ResponseListener);
Attached = false;
}
}
GC.SuppressFinalize(this);
}
}
/// <summary>
/// The parts of NCCP, that the client and the server share (see PROTOCOLS.md).
/// </summary>
internal static class NCCPProtocol
{
public const uint ConnectionID = ConnectionIDDefaults.COMMAND; // 4998512
public const string QueryType = "command";
public const string PlainRequestPrefix = "q:";
public const string EncryptedRequestPrefix = "qe:";
public const string PlainResponsePrefix = "r:";
public const string EncryptedResponsePrefix = "re:";
public const int KeyLength = 32;
public const int NonceLength = 12;
public const int TagLength = 16;
public static readonly UTF8Encoding Utf8 = new(false, false);
public static readonly UTF8Encoding StrictUtf8 = new(false, true);
public static string FormatTime(long unixSeconds) => unixSeconds.ToString("x16", CultureInfo.InvariantCulture);
public static byte[] RequestAad(string client, string server, string timeHex) => Encoding.ASCII.GetBytes($"nccp-q|{client}|{server}|{timeHex}");
public static byte[] ResponseAad(string client, string server, string requestNonceHex) => Encoding.ASCII.GetBytes($"nccp-r|{client}|{server}|{requestNonceHex}");
public static byte[] Encrypt(byte[] key, byte[] nonce, byte[] plaintext, byte[] aad, out byte[] tag)
{
byte[] ciphertext = new byte[plaintext.Length];
tag = new byte[TagLength];
using AesGcm aes = new(key, TagLength);
aes.Encrypt(nonce, plaintext, ciphertext, tag, aad);
return ciphertext;
}
/// <exception cref="AuthenticationTagMismatchException">The tag doesn't match: the key, the data, the AAD or the tag is wrong</exception>
public static byte[] Decrypt(byte[] key, byte[] nonce, byte[] ciphertext, byte[] tag, byte[] aad)
{
byte[] plaintext = new byte[ciphertext.Length];
using AesGcm aes = new(key, TagLength);
aes.Decrypt(nonce, ciphertext, tag, plaintext, aad);
return plaintext;
}
/// <summary>
/// Reads exactly <paramref name="length"/> bytes written as hex digits (any case).
/// </summary>
public static byte[] ParseHex(string hex, int length, string what)
{
if (hex.Length != length * 2 || !hex.All(Uri.IsHexDigit))
{
throw new InvalidDataException($"NCCP {what} must be {length * 2} hex digits");
}
return Convert.FromHexString(hex);
}
/// <summary>
/// Sends the data of a request or a response with Transmit (fragmented). The only exception is empty data (an empty command
/// or result): Transmit sends no packets at all for it, so the other side would wait till its timeout; it is sent as
/// the one packet Transmit would have made, the last one with PacketNo 0 and no data.
/// </summary>
public static void Send(IModem modem, byte[] data, Address to, string metadata, ushort packetSize)
{
if (data.Length == 0)
{
modem.LowLevelTransmit(data, to, QueryType, ConnectionID, metadata);
}
else
{
modem.Transmit(data, to, QueryType, ConnectionID, metadata, packetSize == 0 ? (ushort)1024 : packetSize);
}
}
}
}