i think the server's register process is done
This commit is contained in:
parent
c4524fb62e
commit
90a3be5754
6 changed files with 314 additions and 59 deletions
|
@ -19,7 +19,8 @@ public class Program
|
|||
.FirstOrDefault() ?? "0000"; // get the value or deafult if it doesnt exist
|
||||
|
||||
// On boot, check if a key is available
|
||||
RSA serverKey = LoadRSAFromFile("key_server.pem")!;
|
||||
RSA? serverKey = LoadRSAFromFile("server_key.pem");
|
||||
if (serverKey == null) { Console.WriteLine("Could not find server key, please run server before clients!"); return; }
|
||||
RSA pubKey = LoadRSAFromFile($"pubkey_{user}.pem") ?? RSA.Create(2048);
|
||||
RSA privKey = LoadRSAFromFile($"privkey_{user}.pem") ?? pubKey;
|
||||
|
||||
|
@ -33,15 +34,16 @@ public class Program
|
|||
Aes sk = Aes.Create(); // creates an AES-256 key
|
||||
if (needsRegister)
|
||||
{
|
||||
while(true) {
|
||||
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);
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
else
|
||||
|
@ -49,7 +51,6 @@ public class Program
|
|||
|
||||
}
|
||||
|
||||
|
||||
var inputTask = Task.Run(async () => await HandleUserInput(client, stream));
|
||||
var serverInput = Task.Run(async () => await HandleServerInput(client, stream));
|
||||
|
||||
|
@ -60,28 +61,36 @@ public class Program
|
|||
async static Task RegisterClient(string user, RSA pub, RSA priv, RSA server, Aes sk, NetworkStream stream)
|
||||
{
|
||||
byte counter = 0;
|
||||
// first send the `Register` message
|
||||
// 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
|
||||
byte[] pubBytes = pub.ExportRSAPublicKey();
|
||||
byte[] data = new byte[16];
|
||||
byte[] data = new byte[12];
|
||||
Array.Copy(Utils.NumberToBytes(user), data, 8);
|
||||
Array.Copy(BitConverter.GetBytes(pubBytes.Length), 0, data, 8, 4);
|
||||
Array.Copy(BitConverter.GetBytes(sk.Key.Length), 0, data, 12, 4);
|
||||
byte[] msg = Request.CreateRequest(RequestType.Register, ref counter, data);
|
||||
byte[] finalPayload = [.. msg, .. pubBytes, .. sk.Key];
|
||||
// signing a hash here is pretty useless tbh
|
||||
// if oscar is changing this message somehow it will just fail the registration process and will restart eventually
|
||||
// so kinda pointless, esp when its signed with the server's key
|
||||
byte[] enc = server.Encrypt(finalPayload, RSAEncryptionPadding.OaepSHA256);
|
||||
await stream.WriteAsync(enc);
|
||||
// get the 6 digit code
|
||||
// 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)
|
||||
byte[] digits = new byte[6];
|
||||
await stream.ReadExactlyAsync(digits, 0, 6);
|
||||
int len = 0;
|
||||
while (len != 6)
|
||||
{
|
||||
len = await stream.ReadAsync(digits);
|
||||
}
|
||||
// print the 6 digit code
|
||||
Console.WriteLine($"[{DateTime.Now}] 6 digit code: {string.Join(' ',digits.Select(d => d.ToString()))}");
|
||||
Console.WriteLine($"[{DateTime.Now}] 6 digit code: {string.Join(' ', digits.Select(d => d.ToString()))}");
|
||||
// get the 6 digit code from the user
|
||||
while(true) {
|
||||
while (true)
|
||||
{
|
||||
string? code = Console.ReadLine()?.Trim();
|
||||
if(code == null || code.Take(6).Any(c => !char.IsDigit(c))) {
|
||||
if (code == null || code.Take(6).Any(c => !char.IsDigit(c)))
|
||||
{
|
||||
Console.WriteLine("Invalid code!");
|
||||
continue;
|
||||
}
|
||||
|
@ -89,15 +98,21 @@ public class Program
|
|||
.Take(6) // take the first 6 characters
|
||||
.Select(d => byte.Parse(d.ToString())) // parse into bytes
|
||||
.ToArray();
|
||||
msg = Request.CreateRequest(RequestType.ConfirmRegister, ref counter, codeBytes);
|
||||
byte[] signed = priv.SignData(msg, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
|
||||
enc = sk.EncryptCfb(msg, Array.Empty<byte>(), PaddingMode.None); // no reason to encrpy the signature
|
||||
finalPayload = [.. enc, .. signed]; // should be 128 (enc) + 256 (signed)
|
||||
await stream.WriteAsync(finalPayload);
|
||||
// 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)
|
||||
int incoming = await stream.ReadAsync(enc);
|
||||
msg = sk.DecryptCfb(enc[..incoming], Array.Empty<byte>(), PaddingMode.Zeros);
|
||||
msg = sk.DecryptCfb(enc[..incoming], sk.IV, PaddingMode.PKCS7);
|
||||
string r = Encoding.UTF8.GetString(msg);
|
||||
if(r == "OK") {
|
||||
if (r == "OK")
|
||||
{
|
||||
Console.WriteLine("Registration process complete");
|
||||
break;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue