2024-12-14 15:17:58 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Net.Sockets;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
2024-12-14 17:10:09 +00:00
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
using System.Linq;
|
2024-12-17 18:41:30 +00:00
|
|
|
|
using System.IO;
|
|
|
|
|
using lib;
|
2024-12-14 15:17:58 +00:00
|
|
|
|
|
|
|
|
|
namespace Client;
|
|
|
|
|
|
|
|
|
|
public class Program
|
|
|
|
|
{
|
2024-12-17 18:41:30 +00:00
|
|
|
|
static async Task Main(string[] args)
|
2024-12-14 15:17:58 +00:00
|
|
|
|
{
|
2024-12-17 18:41:30 +00:00
|
|
|
|
string user = args
|
|
|
|
|
.SkipWhile(a => !new[] { "-p", "--phone" }.Contains(a)) // search for the option
|
|
|
|
|
.Skip(1) // skip the `-u/--user` itself to get the value
|
|
|
|
|
.FirstOrDefault() ?? "0000"; // get the value or deafult if it doesnt exist
|
|
|
|
|
|
|
|
|
|
// On boot, check if a key is available
|
2024-12-20 09:23:49 +00:00
|
|
|
|
RSA? serverKey = LoadRSAFromFile("server_key.pem");
|
|
|
|
|
if (serverKey == null) { Console.WriteLine("Could not find server key, please run server before clients!"); return; }
|
2024-12-17 18:41:30 +00:00
|
|
|
|
RSA pubKey = LoadRSAFromFile($"pubkey_{user}.pem") ?? RSA.Create(2048);
|
|
|
|
|
RSA privKey = LoadRSAFromFile($"privkey_{user}.pem") ?? pubKey;
|
|
|
|
|
|
|
|
|
|
SaveRSAKeys(user, pubKey, privKey);
|
2024-12-14 15:17:58 +00:00
|
|
|
|
using TcpClient client = new("127.0.0.1", 12345);
|
|
|
|
|
|
|
|
|
|
var stream = client.GetStream();
|
|
|
|
|
|
2024-12-17 18:41:30 +00:00
|
|
|
|
// First contact init
|
|
|
|
|
bool needsRegister = pubKey == privKey || args.Any(a => new[] { "-fr", "--force-register" }.Contains(a));
|
|
|
|
|
Aes sk = Aes.Create(); // creates an AES-256 key
|
|
|
|
|
if (needsRegister)
|
|
|
|
|
{
|
2024-12-20 09:23:49 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await RegisterClient(user, pubKey, privKey, serverKey, sk, stream);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Failed registration process");
|
|
|
|
|
Console.WriteLine("Exception: " + ex.Message);
|
|
|
|
|
Console.WriteLine("Stack: " + ex.StackTrace);
|
|
|
|
|
return;
|
2024-12-17 18:41:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-14 15:17:58 +00:00
|
|
|
|
var inputTask = Task.Run(async () => await HandleUserInput(client, stream));
|
|
|
|
|
var serverInput = Task.Run(async () => await HandleServerInput(client, stream));
|
|
|
|
|
|
|
|
|
|
_ = Task.WaitAny(inputTask, serverInput);
|
2024-12-17 18:41:30 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async static Task RegisterClient(string user, RSA pub, RSA priv, RSA server, Aes sk, NetworkStream stream)
|
|
|
|
|
{
|
|
|
|
|
byte counter = 0;
|
2024-12-20 09:23:49 +00:00
|
|
|
|
// Generate aes key and send it forward
|
|
|
|
|
byte[] skEnc = server.Encrypt([.. sk.Key, .. sk.IV], RSAEncryptionPadding.OaepSHA256);
|
|
|
|
|
await stream.WriteAsync(skEnc);
|
|
|
|
|
|
|
|
|
|
// Generate the Register msg
|
2024-12-17 18:41:30 +00:00
|
|
|
|
byte[] pubBytes = pub.ExportRSAPublicKey();
|
2024-12-20 09:23:49 +00:00
|
|
|
|
byte[] data = new byte[12];
|
2024-12-17 18:41:30 +00:00
|
|
|
|
Array.Copy(Utils.NumberToBytes(user), data, 8);
|
|
|
|
|
Array.Copy(BitConverter.GetBytes(pubBytes.Length), 0, data, 8, 4);
|
|
|
|
|
byte[] msg = Request.CreateRequest(RequestType.Register, ref counter, data);
|
2024-12-20 09:23:49 +00:00
|
|
|
|
// Encrypt msg and send it
|
|
|
|
|
byte[] enc = sk.EncryptCfb(msg, sk.IV, PaddingMode.PKCS7);
|
|
|
|
|
byte[] payload = [.. enc, .. pubBytes];
|
|
|
|
|
await stream.WriteAsync(payload);
|
|
|
|
|
|
|
|
|
|
// get the 6 digit code (from "secure channel", actually an OK message is expected here but the 6 digit code kinda replaces it)
|
2024-12-17 18:41:30 +00:00
|
|
|
|
byte[] digits = new byte[6];
|
2024-12-20 09:23:49 +00:00
|
|
|
|
int len = 0;
|
|
|
|
|
while (len != 6)
|
|
|
|
|
{
|
|
|
|
|
len = await stream.ReadAsync(digits);
|
|
|
|
|
}
|
2024-12-17 18:41:30 +00:00
|
|
|
|
// print the 6 digit code
|
2024-12-20 09:23:49 +00:00
|
|
|
|
Console.WriteLine($"[{DateTime.Now}] 6 digit code: {string.Join(' ', digits.Select(d => d.ToString()))}");
|
2024-12-17 18:41:30 +00:00
|
|
|
|
// get the 6 digit code from the user
|
2024-12-20 09:23:49 +00:00
|
|
|
|
while (true)
|
|
|
|
|
{
|
2024-12-17 18:41:30 +00:00
|
|
|
|
string? code = Console.ReadLine()?.Trim();
|
2024-12-20 09:23:49 +00:00
|
|
|
|
if (code == null || code.Take(6).Any(c => !char.IsDigit(c)))
|
|
|
|
|
{
|
2024-12-17 18:41:30 +00:00
|
|
|
|
Console.WriteLine("Invalid code!");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
byte[] codeBytes = code
|
|
|
|
|
.Take(6) // take the first 6 characters
|
|
|
|
|
.Select(d => byte.Parse(d.ToString())) // parse into bytes
|
|
|
|
|
.ToArray();
|
2024-12-20 09:23:49 +00:00
|
|
|
|
// Debug print the inserted value to see it works :)
|
|
|
|
|
Console.WriteLine(string.Join(' ', codeBytes.Select(b => b.ToString())));
|
|
|
|
|
|
|
|
|
|
// Sign the 6 digit code & Generate ConfirmRegister message
|
|
|
|
|
byte[] signed = priv.SignData(codeBytes, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
|
|
|
|
|
msg = Request.CreateRequest(RequestType.ConfirmRegister, ref counter, [.. codeBytes, .. BitConverter.GetBytes(signed.Length)]);
|
|
|
|
|
enc = sk.EncryptCfb(msg, sk.IV, PaddingMode.PKCS7); // no reason to encrpy the signature
|
|
|
|
|
payload = [.. enc, .. signed]; // should be 128 (enc) + 256 (signed)
|
|
|
|
|
await stream.WriteAsync(payload);
|
|
|
|
|
// wait for OK/NACK response (anything other than OK is a NACK)
|
2024-12-17 18:41:30 +00:00
|
|
|
|
int incoming = await stream.ReadAsync(enc);
|
2024-12-20 09:23:49 +00:00
|
|
|
|
msg = sk.DecryptCfb(enc[..incoming], sk.IV, PaddingMode.PKCS7);
|
2024-12-17 18:41:30 +00:00
|
|
|
|
string r = Encoding.UTF8.GetString(msg);
|
2024-12-20 09:23:49 +00:00
|
|
|
|
if (r == "OK")
|
|
|
|
|
{
|
2024-12-17 18:41:30 +00:00
|
|
|
|
Console.WriteLine("Registration process complete");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static RSA? LoadRSAFromFile(string file)
|
|
|
|
|
{
|
|
|
|
|
if (File.Exists(file))
|
|
|
|
|
{
|
|
|
|
|
string content = File.ReadAllText(file);
|
|
|
|
|
RSA k = RSA.Create();
|
|
|
|
|
k.ImportFromPem(content);
|
|
|
|
|
return k;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void SaveRSAKeys(string user, RSA pub, RSA priv)
|
|
|
|
|
{
|
|
|
|
|
File.WriteAllText($"pubkey_{user}.pem", pub.ExportRSAPublicKeyPem());
|
|
|
|
|
File.WriteAllText($"privkey_{user}.pem", priv.ExportRSAPrivateKeyPem());
|
2024-12-14 15:17:58 +00:00
|
|
|
|
}
|
2024-12-17 18:41:30 +00:00
|
|
|
|
|
|
|
|
|
static async Task HandleUserInput(TcpClient client, NetworkStream stream)
|
|
|
|
|
{
|
|
|
|
|
while (client.Connected)
|
|
|
|
|
{
|
2024-12-14 15:17:58 +00:00
|
|
|
|
string? input = Console.ReadLine();
|
2024-12-17 18:41:30 +00:00
|
|
|
|
if (input == null)
|
|
|
|
|
{
|
2024-12-14 15:17:58 +00:00
|
|
|
|
await Task.Delay(100);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2024-12-17 18:41:30 +00:00
|
|
|
|
else if (input.StartsWith('/'))
|
|
|
|
|
{
|
2024-12-14 17:10:09 +00:00
|
|
|
|
// Commands :D, i like commands
|
2024-12-17 18:41:30 +00:00
|
|
|
|
switch (input.ToLower())
|
|
|
|
|
{
|
2024-12-14 17:10:09 +00:00
|
|
|
|
case "/quit":
|
|
|
|
|
case "/exit":
|
|
|
|
|
case "/q!":
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-12-17 18:41:30 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
2024-12-14 15:17:58 +00:00
|
|
|
|
stream.Write(Encoding.ASCII.GetBytes(input));
|
|
|
|
|
Console.WriteLine($"[{DateTime.Now}]Sent to server: {input}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-17 18:41:30 +00:00
|
|
|
|
static async Task HandleServerInput(TcpClient client, NetworkStream stream)
|
|
|
|
|
{
|
2024-12-14 15:17:58 +00:00
|
|
|
|
byte[] buffer = new byte[1024];
|
2024-12-17 18:41:30 +00:00
|
|
|
|
while (client.Connected)
|
|
|
|
|
{
|
2024-12-14 15:17:58 +00:00
|
|
|
|
int readLen = await stream.ReadAsync(buffer);
|
2024-12-17 18:41:30 +00:00
|
|
|
|
if (readLen != 0)
|
|
|
|
|
{
|
2024-12-14 15:17:58 +00:00
|
|
|
|
string fromServer = Encoding.ASCII.GetString(buffer[..readLen]);
|
2024-12-14 17:10:09 +00:00
|
|
|
|
Console.WriteLine($"[{DateTime.Now}] From server: {fromServer}");
|
2024-12-14 15:17:58 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|