server handshake handling

This commit is contained in:
2026-01-05 02:48:58 +01:00
parent c748dfa71d
commit f51b8e999c
5 changed files with 96 additions and 12 deletions

View File

@@ -1,4 +1,5 @@
use crate::{
NetworkEvent,
cryptographic_signature::{
CryptographicSignature, get_peer_key, sign_message, verify_signature,
},
@@ -45,6 +46,7 @@ pub fn handle_recevied_message(
crypto_pair: &CryptographicSignature,
socket_addr: &SocketAddr,
senders: &MultipleSenders,
server_name: &String,
) {
if recevied_message.len() < 4 {
return;
@@ -73,6 +75,7 @@ pub fn handle_recevied_message(
// Handle handshake
if message_type == 1 {
let mut resp_to_serv = false;
println!("verify the signature");
let parsed_received_message = HandshakeMessage::parse(recevied_message.to_vec());
let received_name = String::from_utf8(parsed_received_message.name).expect("error");
@@ -81,12 +84,17 @@ pub fn handle_recevied_message(
.block_on(get_peer_key(&received_name))
.expect("failed to retrieve public key");
if received_name == server_name.to_string() {
resp_to_serv = true;
}
if !verify_signature(peer_pubkey, recevied_message) {
println!(
"incorrect signature from given peer: {}, ignoring message {}",
&received_name, id
);
} else {
// verify if this is a server handshake request
let username_size = crypto_pair.username.len();
let hello_handshake = HandshakeMessage::helloReply(
id as u32,
@@ -96,7 +104,7 @@ pub fn handle_recevied_message(
//HandshakeMessage::display(&hello_handshake);
let hello_handshake_serialized = hello_handshake.serialize();
let message_signed = sign_message(crypto_pair, &hello_handshake_serialized);
senders.send_via(0, message_signed, socket_addr.to_string());
senders.send_via(0, message_signed, socket_addr.to_string(), resp_to_serv);
let mut list = messages_list.lock().expect("Failed to lock messages_list");
match list.get(&id) {
Some(_) => {