-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUdp.cs
More file actions
307 lines (272 loc) · 9.47 KB
/
Udp.cs
File metadata and controls
307 lines (272 loc) · 9.47 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
// SPDX-License-Identifier: AGPL-3.0-only
/**
* Digital Voice Modem - Fixed Network Equipment Core Library
* AGPLv3 Open Source. Use is subject to license terms.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* @package DVM / Fixed Network Equipment Core Library
* @license AGPLv3 License (https://opensource.org/licenses/AGPL-3.0)
*
* Copyright (C) 2022,2024 Bryan Biedenkapp, N2PLL
*
*/
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Runtime.Intrinsics.X86;
using System.Security.Cryptography;
using System.Threading.Tasks;
namespace fnecore
{
/// <summary>
/// Structure representing a raw UDP packet frame.
/// </summary>
/// <remarks>"Frame" is used loosely here...</remarks>
public struct UdpFrame
{
/// <summary>
///
/// </summary>
public IPEndPoint Endpoint;
/// <summary>
///
/// </summary>
public byte[] Message;
} // public struct UDPFrame
/// <summary>
/// Base class from which all UDP classes are derived.
/// </summary>
public abstract class UdpBase
{
protected const ushort AES_WRAPPED_PCKT_MAGIC = 0xC0FE;
protected const int AES_BLOCK_SIZE = 128;
protected UdpClient client;
protected bool isCryptoWrapped;
protected byte[] presharedKey;
/*
** Methods
*/
/// <summary>
/// Initializes a new instance of the <see cref="UdpBase"/> class.
/// </summary>
protected UdpBase()
{
client = new UdpClient();
isCryptoWrapped = false;
presharedKey = null;
}
/// <summary>
/// Sets the preshared encryption key.
/// </summary>
/// <param name="presharedKey"></param>
public void SetPresharedKey(byte[] presharedKey)
{
if (presharedKey != null) {
this.presharedKey = presharedKey;
isCryptoWrapped = true;
} else {
this.presharedKey = null;
isCryptoWrapped = false;
}
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public async Task<UdpFrame> Receive()
{
UdpReceiveResult res = await client.ReceiveAsync();
byte[] buffer = res.Buffer;
// are we crypto wrapped?
if (isCryptoWrapped)
{
if (presharedKey == null)
throw new InvalidOperationException("tried to read datagram encrypted with no key? this shouldn't happen BUGBUG");
// does the network packet contain the appropriate magic leader?
ushort magic = FneUtils.ToUInt16(res.Buffer, 0);
if (magic == AES_WRAPPED_PCKT_MAGIC)
{
int cryptedLen = res.Buffer.Length - 2;
byte[] cryptoBuffer = new byte[cryptedLen];
Buffer.BlockCopy(res.Buffer, 2, cryptoBuffer, 0, cryptedLen);
// decrypt
byte[] decrypted = Decrypt(cryptoBuffer, presharedKey);
// finalize, cleanup buffers, and replace with new
if (decrypted != null)
{
buffer = decrypted;
}
else
{
buffer = new byte[0];
}
}
else
{
buffer = new byte[0]; // this will effectively discard packets without the packet magic
}
}
return new UdpFrame()
{
Message = buffer,
Endpoint = res.RemoteEndPoint
};
}
/// <summary>
///
/// </summary>
/// <param name="buffer"></param>
/// <param name="key"></param>
/// <returns></returns>
protected static byte[] Encrypt(byte[] buffer, byte[] key)
{
byte[] encrypted = null;
using (AesManaged aes = new AesManaged()
{
KeySize = 256,
Key = key,
BlockSize = AES_BLOCK_SIZE,
Mode = CipherMode.ECB,
Padding = PaddingMode.Zeros,
IV = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
})
{
ICryptoTransform encryptor = aes.CreateEncryptor();
using (MemoryStream ms = new MemoryStream())
using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
{
cs.Write(buffer, 0, buffer.Length);
encrypted = ms.ToArray();
}
}
return encrypted;
}
/// <summary>
///
/// </summary>
/// <param name="buffer"></param>
/// <param name="key"></param>
/// <returns></returns>
protected static byte[] Decrypt(byte[] buffer, byte[] key)
{
using (AesManaged aes = new AesManaged()
{
KeySize = 256,
Key = key,
BlockSize = 128,
Mode = CipherMode.ECB,
Padding = PaddingMode.Zeros,
IV = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
})
{
ICryptoTransform decryptor = aes.CreateDecryptor();
using (MemoryStream ms = new MemoryStream(buffer))
using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
using (MemoryStream resultStream = new MemoryStream())
{
cs.CopyTo(resultStream);
return resultStream.ToArray();
}
}
}
} // public abstract class UDPBase
/// <summary>
///
/// </summary>
public class UdpReceiver : UdpBase
{
private IPEndPoint endpoint;
/*
** Properties
*/
/// <summary>
/// Gets the <see cref="IPEndPoint"/> for this <see cref="UdpReceiver"/>.
/// </summary>
public IPEndPoint EndPoint => endpoint;
/*
** Methods
*/
/// <summary>
/// Initializes a new instance of the <see cref="UdpListener"/> class.
/// </summary>
public UdpReceiver()
{
/* stub */
}
/// <summary>
///
/// </summary>
/// <param name="hostName"></param>
/// <param name="port"></param>
/// <returns></returns>
public void Connect(string hostName, int port)
{
try
{
try
{
endpoint = new IPEndPoint(IPAddress.Parse(hostName), port);
}
catch
{
IPHostEntry entry = Dns.GetHostEntry(hostName);
if (entry.AddressList.Length > 0)
{
IPAddress address = entry.AddressList[0];
endpoint = new IPEndPoint(address, port);
}
}
}
catch
{
return;
}
client.Connect(endpoint.Address.ToString(), endpoint.Port);
}
/// <summary>
///
/// </summary>
/// <param name="endpoint"></param>
/// <returns></returns>
public void Connect(IPEndPoint endpoint)
{
UdpReceiver recv = new UdpReceiver();
this.endpoint = endpoint;
client.Connect(endpoint.Address.ToString(), endpoint.Port);
}
/// <summary>
///
/// </summary>
/// <param name="frame"></param>
public void Send(UdpFrame frame)
{
byte[] buffer = frame.Message;
// are we crypto wrapped?
if (isCryptoWrapped)
{
if (presharedKey == null)
throw new InvalidOperationException("tried to read datagram encrypted with no key? this shouldn't happen BUGBUG");
// calculate the length of the encrypted data
int cryptedLen = buffer.Length;
// calculate the padding needed to make the buffer a multiple of AES_BLOCK_SIZE
int paddingLen = AES_BLOCK_SIZE - (cryptedLen % AES_BLOCK_SIZE);
if (paddingLen == AES_BLOCK_SIZE)
{
paddingLen = 0; // no padding needed if already a multiple of AES_BLOCK_SIZE
}
byte[] cryptoBuffer = new byte[cryptedLen + paddingLen];
Buffer.BlockCopy(buffer, 0, cryptoBuffer, 0, buffer.Length);
// encrypt the buffer
byte[] crypted = Encrypt(cryptoBuffer, presharedKey);
// create the final buffer with the magic number and encrypted data
buffer = new byte[crypted.Length + 2];
Buffer.BlockCopy(crypted, 0, buffer, 2, crypted.Length);
FneUtils.WriteBytes(AES_WRAPPED_PCKT_MAGIC, ref buffer, 0);
// set the length to the actual length of the buffer to be sent
frame.Message = buffer;
}
client.Send(frame.Message, frame.Message.Length);
}
} // public class UdpReceiver : UdpBase
} // namespace fnecore