wip registering ip addresses

This commit is contained in:
2025-12-18 00:34:57 +01:00
parent e902070c82
commit 6ac06ccfe5
4 changed files with 187 additions and 92 deletions

View File

@@ -10,7 +10,7 @@ pub enum MathError {
DivisionByZero, DivisionByZero,
NonPositiveLogarithm, NonPositiveLogarithm,
NegativeSquareRoot, NegativeSquareRoot,
} }
/// ///
/// contains the ecdsa private key, the ecdsa public key and the username /// contains the ecdsa private key, the ecdsa public key and the username
@@ -48,22 +48,33 @@ pub fn formatPubKey(crypto_pair: CryptographicSignature) -> String {
hex::encode(pubkey_bytes) hex::encode(pubkey_bytes)
} }
pub fn sign_message(crypto_pair: CryptographicSignature, message: [u8; 1024]) -> [u8; 1024] { pub fn sign_message(crypto_pair: CryptographicSignature, message: Vec<u8>) -> Vec<u8> {
let digest = Sha256::digest(&message[0..992]); let length_bytes: [u8; 2] = message[5..7]
let str = hex::encode(digest); .try_into()
let signature = crypto_pair.priv_key.sign_prehash_recoverable(str.as_bytes()); .expect("slice with incorrect length");
let mut signed_message = [0;1024]; let msg_length = u16::from_be_bytes(length_bytes);
signed_message[..992].copy_from_slice(&message[..992]); println!("{}", msg_length);
let digest = Sha256::digest(&message[..8 + msg_length as usize]);
let signature = crypto_pair.priv_key.sign_prehash_recoverable(&digest);
let message_length = 12 + msg_length as usize + 32;
let mut signed_message = Vec::with_capacity(message_length);
println!("{}", message_length);
signed_message.extend_from_slice(&message[..8 + msg_length as usize]);
signed_message.pop();
println!("signed_tmp:{:?}", signed_message);
match signature { match signature {
Ok(signature) => { Ok(signature) => {
println!("Signature: {:?}", signature); //println!("Signature: {:?}", signature);
let r = signature.0.r(); let r = signature.0.r();
let r_bytes = r.to_bytes(); // Returns a GenericArray/bytes object let r_bytes = r.to_bytes(); // Returns a GenericArray/bytes object
signed_message[992..].copy_from_slice(&r_bytes[..32]); signed_message.extend_from_slice(&r_bytes[..32]);
println!("signed:{:?}", signed_message);
println!("rbytes:{:?}", &r_bytes[..32]);
signed_message signed_message
}, }
Err(e) => { Err(e) => {
panic!("error"); panic!("error");
} }
@@ -83,7 +94,7 @@ mod tests {
println!("pubkey : {}",formatted_pubkey); println!("pubkey : {}",formatted_pubkey);
}*/ }*/
#[test] /*#[test]
fn signing_message() { fn signing_message() {
let username = String::from("quoicoubeh"); let username = String::from("quoicoubeh");
let crypto_pair = CryptographicSignature::new(username); let crypto_pair = CryptographicSignature::new(username);
@@ -93,7 +104,5 @@ mod tests {
let signed_message = sign_message(crypto_pair, ser); let signed_message = sign_message(crypto_pair, ser);
println!("unsigned_message: {:?}", ser); println!("unsigned_message: {:?}", ser);
println!("signed_message: {:?}", signed_message); println!("signed_message: {:?}", signed_message);
} }*/
} }

View File

View File

@@ -11,18 +11,60 @@ pub struct HandshakeMessage {
msg_type: u8, msg_type: u8,
length: u16, length: u16,
extensions: u32, extensions: u32,
name: [u8; 981], name: Vec<u8>,
signature: [u8; 32], signature: Vec<u8>,
} }
impl UDPMessage { impl UDPMessage {
pub fn ping(id: u32) -> UDPMessage { pub fn ping(id: u32) -> UDPMessage {
UDPMessage { id: id, msg_type: 0, length: 0, body: [0; 985], signature: [0; 32]} UDPMessage {
id: id,
msg_type: 0,
length: 0,
body: [0; 985],
signature: [0; 32],
}
} }
pub fn error(id: u32) -> UDPMessage { pub fn error(id: u32) -> UDPMessage {
UDPMessage {
id: id,
msg_type: 129,
length: 0,
body: [0; 985],
signature: [0; 32],
}
}
UDPMessage { id: id, msg_type: 129, length: 0, body: [0; 985], signature: [0; 32]} pub fn parse(received_message: [u8; 1024]) -> 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");
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,
}
}
pub fn display(&self) {
println!("ID: {:?}", self.id);
println!("Message Type: {}", self.msg_type);
println!("Length: {:?}", self.length);
let good_length = usize::min(self.length as usize, 985);
println!("name: {:?}", &self.body[..good_length]);
println!("Signature: {:?}", self.signature);
} }
} }
@@ -32,69 +74,74 @@ impl HandshakeMessage {
println!("Message Type: {}", self.msg_type); println!("Message Type: {}", self.msg_type);
println!("Length: {:?}", self.length); println!("Length: {:?}", self.length);
println!("extensions: {:?}", self.extensions); println!("extensions: {:?}", self.extensions);
let good_length = usize::min(self.length as usize, 981); println!("name: {:?}", &self.name[..(self.length - 4) as usize]);
println!("name: {:?}", &self.name[..good_length]);
println!("Signature: {:?}", self.signature); println!("Signature: {:?}", self.signature);
} }
pub fn hello(id: u32, length: u16, username: String) -> HandshakeMessage { pub fn hello(id: u32, length: u16, username: String) -> HandshakeMessage {
let username_bytes = username.as_bytes(); let name_vec = username.trim_end_matches(char::from(0)).as_bytes().to_vec();
HandshakeMessage {
let mut name: [u8; 981] = [0; 981]; id: id,
msg_type: 1,
let length_to_copy = username_bytes.len().min(981); length: length,
name[..length_to_copy].copy_from_slice(&username_bytes[..length_to_copy]); extensions: 0,
HandshakeMessage {id: id, msg_type: 1, length: length, extensions: 0, name: name, signature: [0;32]} name: name_vec,
signature: vec![0; 64],
}
} }
pub fn helloReply(id: u32, length: u16, username: String) -> HandshakeMessage { pub fn helloReply(id: u32, length: u16, username: String) -> HandshakeMessage {
let username_bytes = username.as_bytes(); let name_vec = username.trim_end_matches(char::from(0)).as_bytes().to_vec();
HandshakeMessage {
let mut name: [u8; 981] = [0; 981]; id: id,
msg_type: 130,
let length_to_copy = username_bytes.len().min(981); length: length,
name[..length_to_copy].copy_from_slice(&username_bytes[..length_to_copy]); extensions: 0,
HandshakeMessage {id: id, msg_type: 130, length: length, extensions: 0, name: name, signature: [0;32]} name: name_vec,
signature: vec![0; 64],
}
} }
pub fn serialize(&self) -> [u8; 1024] { pub fn serialize(&self) -> Vec<u8> {
let mut buffer = [0u8; 1024]; let mut out = Vec::with_capacity(4 + 1 + 2 + 4 + self.name.len() + self.signature.len());
let mut offset = 0;
buffer[offset..offset + 4].copy_from_slice(&self.id.to_be_bytes()); // id: u32 little-endian
offset += 4; out.extend_from_slice(&self.id.to_be_bytes());
buffer[offset] = self.msg_type; // msg_type: u8
offset += 1; out.push(self.msg_type);
buffer[offset..offset + 2].copy_from_slice(&self.length.to_be_bytes()); out.extend_from_slice(&self.length.to_be_bytes());
offset += 2;
buffer[offset..offset + 4].copy_from_slice(&self.extensions.to_be_bytes()); out.extend_from_slice(&self.extensions.to_be_bytes());
offset += 4;
buffer[offset..offset + 981].copy_from_slice(&self.name); out.extend_from_slice(&self.name);
offset += 981;
buffer[offset..offset + 32].copy_from_slice(&self.signature); out.extend_from_slice(&self.signature);
buffer out
} }
pub fn parse(received_message: [u8; 1024]) -> HandshakeMessage { pub fn parse(received_message: Vec<u8>) -> HandshakeMessage {
let id_bytes: [u8; 4] = received_message[0..4].try_into().expect("Taille incorrecte"); let id_bytes: [u8; 4] = received_message[0..4]
let length_bytes: [u8; 2] = received_message[5..7].try_into().expect("Taille incorrecte"); .try_into()
let extensions_bytes: [u8; 4] = received_message[7..11].try_into().expect("Taille incorrecte"); .expect("Taille incorrecte");
let name_bytes: [u8; 981] = received_message[11..992].try_into().expect("Taille incorrecte"); let length_bytes: [u8; 2] = received_message[5..7]
let signature_bytes: [u8; 32] = received_message[992..1024].try_into().expect("Taille incorrecte"); .try_into()
.expect("Taille incorrecte");
let msg_length = u16::from_be_bytes(length_bytes);
let extensions_bytes: [u8; 4] = received_message[7..11]
.try_into()
.expect("Taille incorrecte");
let name_bytes = &received_message[11..12 + msg_length as usize];
let signature_bytes =
&received_message[12 + msg_length as usize..(13 + msg_length + 32) as usize];
HandshakeMessage { HandshakeMessage {
id: u32::from_be_bytes(id_bytes), id: u32::from_be_bytes(id_bytes),
msg_type: received_message[4], msg_type: received_message[4],
length: u16::from_be_bytes(length_bytes), length: u16::from_be_bytes(length_bytes),
extensions: u32::from_be_bytes(extensions_bytes), extensions: u32::from_be_bytes(extensions_bytes),
name: name_bytes, name: name_bytes.to_vec(),
signature: signature_bytes, signature: signature_bytes.to_vec(),
} }
} }
} }
@@ -103,8 +150,6 @@ fn convert_to_u16(bytes: [u8; 2]) -> u16 {
((bytes[0] as u16) << 8) | (bytes[1] as u16) ((bytes[0] as u16) << 8) | (bytes[1] as u16)
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
// Note this useful idiom: importing names from outer (for mod tests) scope. // Note this useful idiom: importing names from outer (for mod tests) scope.
@@ -127,4 +172,3 @@ mod tests {
parsed.display(); parsed.display();
}*/ }*/
} }

View File

@@ -1,45 +1,74 @@
use bytes::Bytes;
use crate::cryptographic_signature::{CryptographicSignature, formatPubKey, sign_message}; use crate::cryptographic_signature::{CryptographicSignature, formatPubKey, sign_message};
use crate::messages_structure::HandshakeMessage; use crate::messages_structure::{HandshakeMessage, UDPMessage};
use std::net::UdpSocket; use std::net::UdpSocket;
/// ///
/// Registration with the server happens in two steps: first, the client /// 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. /// 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>{ async fn register_with_the_server(
crypto_pair: CryptographicSignature,
) -> Result<(), reqwest::Error> {
let client = reqwest::Client::new(); let client = reqwest::Client::new();
let uri = format!("https://jch.irif.fr:8443/peers/{}/key", crypto_pair.username); let uri = format!(
"https://jch.irif.fr:8443/peers/{}/key",
crypto_pair.username
);
let encoded_point = crypto_pair.pub_key.to_encoded_point(false); let encoded_point = crypto_pair.pub_key.to_encoded_point(false);
let pubkey_bytes = encoded_point.as_ref().to_vec(); let pubkey_bytes = encoded_point.as_ref().to_vec();
let pubkey_bytes_minus = pubkey_bytes[1..].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 // 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) let res = client.put(uri).body(pubkey_bytes_minus).send().await?;
.body(pubkey_bytes_minus)
.send()
.await?;
if res.status().is_success() { if res.status().is_success() {
println!("Successfully registered with the server."); println!("Successfully registered with the server.");
} else { } else {
eprintln!("Failed to register with the server. Status: {}", res.status()); eprintln!(
"Failed to register with the server. Status: {}",
res.status()
);
let str = hex::encode(res.bytes().await?); let str = hex::encode(res.bytes().await?);
eprintln!("erreur : {}", str); eprintln!("erreur : {}", str);
} }
println!("register ip adresses");
register_ip_addresses(crypto_pair);
Ok(()) Ok(())
} }
async fn get_socket_address(username: String) -> Result<Bytes, reqwest::Error> {
let client = reqwest::Client::new();
let uri = format!("https://jch.irif.fr:8443/peers/{}/addresses", username);
let res = client.get(uri).send().await?;
if res.status().is_success() {
println!("Successfully retreived the addresses.");
} else {
eprintln!(
"Failed to register with the server. Status: {}",
res.status()
);
}
let body: Bytes = res.bytes().await?;
Ok(body)
}
/// It then /// It then
/// registers each of its IP addresses by sending a Hello request to the server. /// 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 /// 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 doesnt reply to the Hello /// to receive requests by sending a Hello request to the client. If the client doesnt reply to the Hello
/// request with a properly signed message, its address will not be published by the server. /// request with a properly signed message, its address will not be published by the server.
fn register_ip_addresses(crypto_pair: CryptographicSignature) { fn register_ip_addresses(crypto_pair: CryptographicSignature) {
let socket = UdpSocket::bind("127.0.0.1:4242").expect("bind failed"); let socket = UdpSocket::bind("0.0.0.0:0").expect("bind failed");
let username_size = crypto_pair.username.len(); let username_size = crypto_pair.username.len();
let hello_handshake = HandshakeMessage::hello(0, username_size as u16, crypto_pair.username.clone()); let hello_handshake =
HandshakeMessage::hello(545, username_size as u16 + 4, crypto_pair.username.clone());
//HandshakeMessage::display(&hello_handshake);
let hello_handshake_serialized = hello_handshake.serialize(); let hello_handshake_serialized = hello_handshake.serialize();
let message_signed = sign_message(crypto_pair, hello_handshake_serialized); let message_signed = sign_message(crypto_pair, hello_handshake_serialized.to_vec());
socket.send_to(&message_signed, "jch.irif.fr:8443").expect("send failed"); socket
.send_to(&message_signed, "81.194.30.229:8443")
.expect("send failed");
let mut buf = [0u8; 1024]; let mut buf = [0u8; 1024];
socket.recv_from(&mut buf).expect("receive failed"); socket.recv_from(&mut buf).expect("receive failed");
let hello_handshake_received = HandshakeMessage::parse(buf); let hello_handshake_received = UDPMessage::parse(buf);
hello_handshake_received.display(); hello_handshake_received.display();
} }
@@ -48,12 +77,25 @@ mod tests {
// Note this useful idiom: importing names from outer (for mod tests) scope. // Note this useful idiom: importing names from outer (for mod tests) scope.
use super::*; use super::*;
/*#[tokio::test] #[tokio::test]
async fn creating_cryptographic_signature() { async fn registering_with_server() {
let username = String::from("charlie_kirk"); let username = String::from("gamemixtreize");
let crypto_pair = CryptographicSignature::new(username); let crypto_pair = CryptographicSignature::new(username);
if let Err(e) = register_with_the_server(crypto_pair).await { if let Err(e) = register_with_the_server(crypto_pair).await {
eprintln!("Error during registration: {}", e); eprintln!("Error during registration: {}", e);
} }
}
/*#[tokio::test]
async fn retreive_socket_addr() {
let username = String::from("ipjkndqfshjldfsjlbsdfjhhj");
match get_socket_address(username).await {
Ok(body) => {
println!("{:?}",body);
}
Err(e) => {
eprintln!("Erreur HTTP: {}", e);
}
}
}*/ }*/
} }