From 1844037488ae589f2c1b82d4b72de73218c11aae Mon Sep 17 00:00:00 2001 From: wikano Date: Fri, 19 Dec 2025 23:08:02 +0100 Subject: [PATCH] code cleanup and documentation --- client-network/src/cryptographic_signature.rs | 40 +++++++-------- client-network/src/lib.rs | 5 +- client-network/src/messages_structure.rs | 44 ++++++++--------- client-network/src/protocol.rs | 49 ------------------- client-network/src/registration.rs | 26 ++++++---- 5 files changed, 59 insertions(+), 105 deletions(-) delete mode 100644 client-network/src/protocol.rs diff --git a/client-network/src/cryptographic_signature.rs b/client-network/src/cryptographic_signature.rs index f201f2c..340db5c 100644 --- a/client-network/src/cryptographic_signature.rs +++ b/client-network/src/cryptographic_signature.rs @@ -6,12 +6,6 @@ use p256::ecdsa::{ use rand_core::OsRng; use sha2::{Digest, Sha256}; -pub enum MathError { - DivisionByZero, - NonPositiveLogarithm, - NegativeSquareRoot, -} - /// /// contains the ecdsa private key, the ecdsa public key and the username /// @@ -48,6 +42,9 @@ pub fn formatPubKey(crypto_pair: CryptographicSignature) -> String { hex::encode(pubkey_bytes) } +/// +/// takes a serialized message and adds the signature using the private key +/// pub fn sign_message(crypto_pair: CryptographicSignature, message: Vec) -> Vec { let length_bytes: [u8; 2] = message[5..7] .try_into() @@ -68,7 +65,6 @@ pub fn sign_message(crypto_pair: CryptographicSignature, message: Vec) -> Ve println!("signed_tmp:{:?}", signed_message); match signature { Ok(signature) => { - //println!("Signature: {:?}", signature); let r = signature.0.r(); let s = signature.0.s(); @@ -87,26 +83,30 @@ pub fn sign_message(crypto_pair: CryptographicSignature, message: Vec) -> Ve #[cfg(test)] mod tests { - // Note this useful idiom: importing names from outer (for mod tests) scope. use super::*; - /*#[test] + /// + /// creates a cryptographic signature + /// + #[test] fn creating_cryptographic_signature() { - let username = String::from("quoicoubeh"); + let username = String::from("gamixtreize"); let crypto_pair = CryptographicSignature::new(username); - let formatted_pubkey =formatPubKey(crypto_pair); - println!("pubkey : {}",formatted_pubkey); - }*/ + let formatted_pubkey = formatPubKey(crypto_pair); + println!("pubkey : {}", formatted_pubkey); + } - /*#[test] + /// + /// signs a message + /// + #[test] fn signing_message() { - let username = String::from("quoicoubeh"); - let crypto_pair = CryptographicSignature::new(username); - let username_b = String::from("quoicoubeh"); - let handshake = HandshakeMessage::hello(0, 12, username_b); + let username = String::from("gamixtreize"); + let crypto_pair = CryptographicSignature::new(username.clone()); + let handshake = HandshakeMessage::hello(0, 12, username); let ser = handshake.serialize(); - let signed_message = sign_message(crypto_pair, ser); + let signed_message = sign_message(crypto_pair, ser.clone()); println!("unsigned_message: {:?}", ser); println!("signed_message: {:?}", signed_message); - }*/ + } } diff --git a/client-network/src/lib.rs b/client-network/src/lib.rs index aedf0b8..957c39a 100644 --- a/client-network/src/lib.rs +++ b/client-network/src/lib.rs @@ -1,8 +1,7 @@ -mod data; -mod protocol; mod cryptographic_signature; -mod registration; +mod data; mod messages_structure; +mod registration; /// Messages sent to the Network thread by the GUI. pub enum NetworkCommand { diff --git a/client-network/src/messages_structure.rs b/client-network/src/messages_structure.rs index 88a47c1..fd4691e 100644 --- a/client-network/src/messages_structure.rs +++ b/client-network/src/messages_structure.rs @@ -2,8 +2,8 @@ pub struct UDPMessage { id: u32, msg_type: u8, length: u16, - body: [u8; 985], - signature: [u8; 32], + body: Vec, + signature: Vec, } pub struct HandshakeMessage { @@ -21,8 +21,8 @@ impl UDPMessage { id: id, msg_type: 0, length: 0, - body: [0; 985], - signature: [0; 32], + body: vec![0; 985], + signature: vec![0; 32], } } @@ -31,30 +31,28 @@ impl UDPMessage { id: id, msg_type: 129, length: 0, - body: [0; 985], - signature: [0; 32], + body: vec![0; 985], + signature: vec![0; 32], } } - pub fn parse(received_message: [u8; 1024]) -> UDPMessage { + pub fn parse(received_message: Vec) -> UDPMessage { let id_bytes: [u8; 4] = received_message[0..4] .try_into() .expect("Taille incorrecte"); let length_bytes: [u8; 2] = received_message[5..7] .try_into() .expect("Taille incorrecte"); - let name_bytes: [u8; 985] = received_message[7..992] - .try_into() - .expect("Taille incorrecte"); - let signature_bytes: [u8; 32] = received_message[992..1024] - .try_into() - .expect("Taille incorrecte"); + let msg_length = u16::from_be_bytes(length_bytes); + let name_bytes = &received_message[7..msg_length as usize + 8]; + let signature_bytes = + &received_message[msg_length as usize + 8..msg_length as usize + 9 + 32]; UDPMessage { id: u32::from_be_bytes(id_bytes), msg_type: received_message[4], length: u16::from_be_bytes(length_bytes), - body: name_bytes, - signature: signature_bytes, + body: name_bytes.to_vec(), + signature: signature_bytes.to_vec(), } } @@ -146,23 +144,21 @@ impl HandshakeMessage { } } -fn convert_to_u16(bytes: [u8; 2]) -> u16 { - ((bytes[0] as u16) << 8) | (bytes[1] as u16) -} - #[cfg(test)] mod tests { // Note this useful idiom: importing names from outer (for mod tests) scope. use super::*; - /*#[tokio::test] - async fn creating_cryptographic_signature() { + /// creates an handshake message + #[tokio::test] + async fn creating_handshake_msg() { let username = String::from("charlie_kirk"); let handshake = HandshakeMessage::hello(0, 12, username); handshake.display(); - }*/ + } - /*#[tokio::test] + /// parses an handshake message + #[tokio::test] async fn parse_handshakemessage() { let username = String::from("charlie_kirk"); let handshake = HandshakeMessage::hello(0, 12, username); @@ -170,5 +166,5 @@ mod tests { let parsed = HandshakeMessage::parse(ser); handshake.display(); parsed.display(); - }*/ + } } diff --git a/client-network/src/protocol.rs b/client-network/src/protocol.rs deleted file mode 100644 index 627f40e..0000000 --- a/client-network/src/protocol.rs +++ /dev/null @@ -1,49 +0,0 @@ -use http::{Request, Response}; -use p256::ecdsa::{ - Signature, SigningKey, VerifyingKey, - signature::{Signer, Verifier}, -}; -use rand_core::OsRng; - -struct KeyRegistration { - priv_key: SigningKey, - pub_key: VerifyingKey, - username: String, -} - -impl KeyRegistration { - fn new(username: String) -> KeyRegistration { - let priv_key = SigningKey::random(&mut OsRng); - let pub_key = VerifyingKey::from(&priv_key); - KeyRegistration { - priv_key: priv_key, - pub_key: pub_key, - username: username, - } - } -} - -async fn register_with_the_server(key: KeyRegistration) -> Result { - let client = reqwest::Client::new(); - - let pubkey_ser = key.pub_key.to_encoded_point(false); - let pubkey_str = hex::encode(pubkey_ser); - let uri = format!("https://jch.irif.fr:8443/peers/{}/key", key.username); - let resp = client.put(uri).send().await?.error_for_status()?; - - resp.bytes().await -} - -/*#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn key_genereation() { - let keys = KeyRegistration::new(); - let pubkey = keys.pub_key; - let pubkey_ser = pubkey.to_encoded_point(false); - println!("string pubkey: {}", hex::encode(pubkey_ser)); - println!("string privkey: {}", hex::encode(keys.priv_key.to_bytes())) - } -}*/ diff --git a/client-network/src/registration.rs b/client-network/src/registration.rs index ac8bfc3..41e64ff 100644 --- a/client-network/src/registration.rs +++ b/client-network/src/registration.rs @@ -3,9 +3,10 @@ use bytes::Bytes; use crate::cryptographic_signature::{CryptographicSignature, formatPubKey, sign_message}; use crate::messages_structure::{HandshakeMessage, UDPMessage}; use std::net::UdpSocket; + +/// +/// sends the cryptographic signature to the server using a PUT request over the HTTP API. /// -/// Registration with the server happens in two steps: first, the client -/// sends its cryptographic signature to the server using a PUT request over the HTTP API. async fn register_with_the_server( crypto_pair: CryptographicSignature, ) -> Result<(), reqwest::Error> { @@ -17,7 +18,6 @@ async fn register_with_the_server( let encoded_point = crypto_pair.pub_key.to_encoded_point(false); let pubkey_bytes = encoded_point.as_ref().to_vec(); let pubkey_bytes_minus = pubkey_bytes[1..].to_vec(); - // In order to register with the server, a peer ϕ makes a PUT request to the URL /peers/ϕ/key with its 64-byte public key in the body let res = client.put(uri).body(pubkey_bytes_minus).send().await?; if res.status().is_success() { println!("Successfully registered with the server."); @@ -34,6 +34,10 @@ async fn register_with_the_server( Ok(()) } +/// +/// sends a get request to the server to get the socket address of the given peer +/// + async fn get_socket_address(username: String) -> Result { let client = reqwest::Client::new(); let uri = format!("https://jch.irif.fr:8443/peers/{}/addresses", username); @@ -50,11 +54,9 @@ async fn get_socket_address(username: String) -> Result { Ok(body) } -/// It then -/// registers each of its IP addresses by sending a Hello request to the server. -/// After the client sends a Hello request to the server, the server will verify that the client is able -/// to receive requests by sending a Hello request to the client. If the client doesn’t reply to the Hello -/// request with a properly signed message, its address will not be published by the server. +/// +/// registers the IP addresses by sending a Hello request to the server. +/// fn register_ip_addresses(crypto_pair: CryptographicSignature) { let socket = UdpSocket::bind("0.0.0.0:0").expect("bind failed"); let username_size = crypto_pair.username.len(); @@ -68,7 +70,7 @@ fn register_ip_addresses(crypto_pair: CryptographicSignature) { .expect("send failed"); let mut buf = [0u8; 1024]; socket.recv_from(&mut buf).expect("receive failed"); - let hello_handshake_received = UDPMessage::parse(buf); + let hello_handshake_received = UDPMessage::parse(buf.to_vec()); hello_handshake_received.display(); } @@ -77,6 +79,9 @@ mod tests { // Note this useful idiom: importing names from outer (for mod tests) scope. use super::*; + /// + /// does the procedure to register with the server + /// #[tokio::test] async fn registering_with_server() { let username = String::from("gamixtreize"); @@ -86,6 +91,9 @@ mod tests { } } + /// + /// retreives the socket address of a given peer + /// #[tokio::test] async fn retreive_socket_addr() { let username = String::from("ipjkndqfshjldfsjlbsdfjhhj");