cryptographic signature, put registration and messages structure

This commit is contained in:
2025-12-17 01:40:52 +01:00
parent 002a667837
commit 3664d55678
5 changed files with 161 additions and 2 deletions

View File

@@ -0,0 +1,55 @@
use p256::ecdsa::{
Signature, SigningKey, VerifyingKey,
signature::{Signer, Verifier},
};
use rand_core::OsRng;
///
/// contains the ecdsa private key, the ecdsa public key and the username
///
pub struct CryptographicSignature {
priv_key: SigningKey,
pub pub_key: VerifyingKey,
pub username: String,
}
impl CryptographicSignature {
///
/// creates a CryptographicSignature
///
pub fn new(username: String) -> CryptographicSignature {
// generate a private key
let priv_key = SigningKey::random(&mut OsRng);
// extract the public key from the private key
let pub_key = VerifyingKey::from(&priv_key);
// return the new struct
CryptographicSignature {
priv_key: priv_key,
pub_key: pub_key,
username: username,
}
}
}
///
/// returns a string representing the pub_key as a String
///
pub fn formatPubKey(crypto_pair: CryptographicSignature) -> String {
let encoded_point = crypto_pair.pub_key.to_encoded_point(false);
let pubkey_bytes = encoded_point.as_bytes();
hex::encode(pubkey_bytes)
}
#[cfg(test)]
mod tests {
// Note this useful idiom: importing names from outer (for mod tests) scope.
use super::*;
#[test]
fn creating_cryptographic_signature() {
let username = String::from("quoicoubeh");
let crypto_pair = CryptographicSignature::new(username);
let formatted_pubkey =formatPubKey(crypto_pair);
println!("pubkey : {}",formatted_pubkey);
}
}

View File

@@ -1,5 +1,8 @@
mod data; mod data;
mod protocol; mod protocol;
mod cryptographic_signature;
mod registration;
mod messages_structure;
/// Messages sent to the Network thread by the GUI. /// Messages sent to the Network thread by the GUI.
pub enum NetworkCommand { pub enum NetworkCommand {

View File

@@ -0,0 +1,50 @@
struct UDPMessage {
id: [u8; 4],
msg_type: u8,
length: [u8; 2],
body: [u8; 985],
signature: [u8; 32],
}
struct HandshakeMessage {
id: [u8; 4],
msg_type: u8,
length: [u8; 2],
extensions: [u8; 4],
name: [u8; 981],
signature: [u8; 32],
}
impl UDPMessage {
pub fn ping(id: i32) -> UDPMessage {
UDPMessage { id: id.to_ne_bytes(), msg_type: 0, length: [0; 2], body: [0; 985], signature: [0; 32]}
}
pub fn error(id: i32) -> UDPMessage {
UDPMessage { id: id.to_ne_bytes(), msg_type: 129, length: [0; 2], body: [0; 985], signature: [0; 32]}
}
pub fn hello(id: i32, length: i16, username: String) -> HandshakeMessage {
let username_bytes = username.as_bytes();
let mut body: [u8; 981] = [0; 981];
let length_to_copy = username_bytes.len().min(981);
body[..length_to_copy].copy_from_slice(&username_bytes[..length_to_copy]);
HandshakeMessage {id: id.to_ne_bytes(), msg_type: 1, length: length.to_ne_bytes(), extensions: [0;4], name: body, signature: [0;32]}
}
pub fn helloReply(id: i32, length: i16, username: String) -> HandshakeMessage {
let username_bytes = username.as_bytes();
let mut body: [u8; 981] = [0; 981];
let length_to_copy = username_bytes.len().min(981);
body[..length_to_copy].copy_from_slice(&username_bytes[..length_to_copy]);
HandshakeMessage {id: id.to_ne_bytes(), msg_type: 130, length: length.to_ne_bytes(), extensions: [0;4], name: body, signature: [0;32]}
}
}

View File

@@ -0,0 +1,50 @@
use crate::cryptographic_signature::{CryptographicSignature, formatPubKey};
use std::net::UdpSocket;
///
/// 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>{
let client = reqwest::Client::new();
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 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.");
} else {
eprintln!("Failed to register with the server. Status: {}", res.status());
let str = hex::encode(res.bytes().await?);
eprintln!("erreur : {}", str);
}
Ok(())
}
/// 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 doesnt reply to the Hello
/// request with a properly signed message, its address will not be published by the server.
fn register_ip_addresses(crypto_pair: CryptographicSignature) {
let socket = UdpSocket::bind("127.0.0.1:4242");
//TODO
}
#[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() {
let username = String::from("charlie_kirk");
let crypto_pair = CryptographicSignature::new(username);
if let Err(e) = register_with_the_server(crypto_pair).await {
eprintln!("Error during registration: {}", e);
}
}
}

View File

@@ -3,8 +3,9 @@
- get rsquest to the uri /peers/ - get rsquest to the uri /peers/
## registration with the server ## registration with the server
- generation of the cryptographic key - generation of the cryptographic key OK
- put request to the uri (check if the peer is already connected) OK - put request to the uri (check if the peer is already connected) OK
- udp handshakes
- get request to the uri /peers/key to get the public key of a peer - get request to the uri /peers/key to get the public key of a peer
- get request to the uri /peers/key/addresses - get request to the uri /peers/key/addresses
@@ -25,7 +26,7 @@ fonctionnalités :
s'enregistrer avec le serveur OK s'enregistrer avec le serveur OK
rechercher un pair rechercher un pair
generer une clé publique generer une clé publique OK
rechercher les fichiers d'un pair rechercher les fichiers d'un pair
telechargement des fichiers telechargement des fichiers
choisir un dossier à partager choisir un dossier à partager