From 145e77e437df83b9cf76c7fc9fc7b9bed9b03334 Mon Sep 17 00:00:00 2001 From: Rusty Striker Date: Sat, 28 Dec 2024 11:13:51 +0200 Subject: [PATCH] work on SendMessage --- client/Message.cs | 72 +++++++++++++++++++++++++++++++++++++++++++++++ client/Program.cs | 2 +- server/Program.cs | 2 ++ 3 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 client/Message.cs diff --git a/client/Message.cs b/client/Message.cs new file mode 100644 index 0000000..34eef94 --- /dev/null +++ b/client/Message.cs @@ -0,0 +1,72 @@ + + +using System; +using System.Collections.Generic; +using System.Security.Cryptography; +using System.Text; +using lib; + +namespace client; + +public class Message +{ + public uint Id { set; get; } + public string Sender { set; get; } + public bool IsAck { set; get; } + public string Content { set; get; } + public byte[] Signature { set; get; } + + public Message(byte[] bytes) + { + if (bytes[0] != 0) + { + Console.WriteLine("Failed parsing message, unsupported version"); + } + // skip the first byte for simplicity + bytes = bytes[1..]; + Id = BitConverter.ToUInt32(bytes, 0); + Sender = Utils.BytesToNumber(bytes[4..12]); + IsAck = bytes[12] != 0; + Content = Encoding.UTF8.GetString(bytes[13..(bytes.Length - 32)]); + Signature = bytes[(bytes.Length - 32)..]; + } + + public Message(uint Id, string Sender, bool IsAck, string Content) + { + this.Id = Id; + this.Sender = Sender; + this.IsAck = IsAck; + this.Content = Content; + Signature = []; + } + + public void CalculateSignature(RSA key) + { + Signature = key.SignData(Bytes(false), HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); + } + + public bool IsSignatureValid(RSA key) + { + return key.VerifyData(Bytes(false), Signature, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); + } + + public byte[] Bytes(bool IncludeSignature = true) + { + List msg = []; + // 0..4 + msg.AddRange(BitConverter.GetBytes(Id)); + // 4..12 + msg.AddRange(Utils.NumberToBytes(Sender)); + // 12 + msg.Add((byte)(IsAck ? 1 : 0)); + // 13..(len - 32) + msg.AddRange(Encoding.UTF8.GetBytes(Content)); + // (len - 32).. + if (IncludeSignature) // we dont want to include the signature when we sign/verify the data + { + msg.AddRange(Signature); + } + + return [0, .. msg]; + } +} \ No newline at end of file diff --git a/client/Program.cs b/client/Program.cs index 6c67243..a2b6765 100644 --- a/client/Program.cs +++ b/client/Program.cs @@ -289,7 +289,7 @@ public class Program // even if we know who sent the message we dont know which message it is unless the server also knows // and i dont want the server to be aware of that, i think it makes more sense for the server to act as a relay // and a buffer than an actual participant, so if the message is failing to decrypt that will go unnoticed. - // supposedly the sender will notice the lack of ACK and send it again + // supposedly the sender will notice the lack of response and send it again Console.WriteLine("Incoming message failed to decrypt, unknown sender"); } } diff --git a/server/Program.cs b/server/Program.cs index f6fda95..0028f5d 100644 --- a/server/Program.cs +++ b/server/Program.cs @@ -240,6 +240,8 @@ public class Program } break; case RequestType.SendMessage: + string recv = Utils.BytesToNumber(msg[3..11]); + int msgLen = BitConverter.ToInt32(msg, 11); break; default: