code cleanup and documentation
This commit is contained in:
@@ -6,12 +6,6 @@ use p256::ecdsa::{
|
|||||||
use rand_core::OsRng;
|
use rand_core::OsRng;
|
||||||
use sha2::{Digest, Sha256};
|
use sha2::{Digest, Sha256};
|
||||||
|
|
||||||
pub enum MathError {
|
|
||||||
DivisionByZero,
|
|
||||||
NonPositiveLogarithm,
|
|
||||||
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,6 +42,9 @@ pub fn formatPubKey(crypto_pair: CryptographicSignature) -> String {
|
|||||||
hex::encode(pubkey_bytes)
|
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<u8>) -> Vec<u8> {
|
pub fn sign_message(crypto_pair: CryptographicSignature, message: Vec<u8>) -> Vec<u8> {
|
||||||
let length_bytes: [u8; 2] = message[5..7]
|
let length_bytes: [u8; 2] = message[5..7]
|
||||||
.try_into()
|
.try_into()
|
||||||
@@ -68,7 +65,6 @@ pub fn sign_message(crypto_pair: CryptographicSignature, message: Vec<u8>) -> Ve
|
|||||||
println!("signed_tmp:{:?}", signed_message);
|
println!("signed_tmp:{:?}", signed_message);
|
||||||
match signature {
|
match signature {
|
||||||
Ok(signature) => {
|
Ok(signature) => {
|
||||||
//println!("Signature: {:?}", signature);
|
|
||||||
let r = signature.0.r();
|
let r = signature.0.r();
|
||||||
let s = signature.0.s();
|
let s = signature.0.s();
|
||||||
|
|
||||||
@@ -87,26 +83,30 @@ pub fn sign_message(crypto_pair: CryptographicSignature, message: Vec<u8>) -> Ve
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
// Note this useful idiom: importing names from outer (for mod tests) scope.
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
/*#[test]
|
///
|
||||||
|
/// creates a cryptographic signature
|
||||||
|
///
|
||||||
|
#[test]
|
||||||
fn creating_cryptographic_signature() {
|
fn creating_cryptographic_signature() {
|
||||||
let username = String::from("quoicoubeh");
|
let username = String::from("gamixtreize");
|
||||||
let crypto_pair = CryptographicSignature::new(username);
|
let crypto_pair = CryptographicSignature::new(username);
|
||||||
let formatted_pubkey =formatPubKey(crypto_pair);
|
let formatted_pubkey = formatPubKey(crypto_pair);
|
||||||
println!("pubkey : {}",formatted_pubkey);
|
println!("pubkey : {}", formatted_pubkey);
|
||||||
}*/
|
}
|
||||||
|
|
||||||
/*#[test]
|
///
|
||||||
|
/// signs a message
|
||||||
|
///
|
||||||
|
#[test]
|
||||||
fn signing_message() {
|
fn signing_message() {
|
||||||
let username = String::from("quoicoubeh");
|
let username = String::from("gamixtreize");
|
||||||
let crypto_pair = CryptographicSignature::new(username);
|
let crypto_pair = CryptographicSignature::new(username.clone());
|
||||||
let username_b = String::from("quoicoubeh");
|
let handshake = HandshakeMessage::hello(0, 12, username);
|
||||||
let handshake = HandshakeMessage::hello(0, 12, username_b);
|
|
||||||
let ser = handshake.serialize();
|
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!("unsigned_message: {:?}", ser);
|
||||||
println!("signed_message: {:?}", signed_message);
|
println!("signed_message: {:?}", signed_message);
|
||||||
}*/
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
mod data;
|
|
||||||
mod protocol;
|
|
||||||
mod cryptographic_signature;
|
mod cryptographic_signature;
|
||||||
mod registration;
|
mod data;
|
||||||
mod messages_structure;
|
mod messages_structure;
|
||||||
|
mod registration;
|
||||||
|
|
||||||
/// Messages sent to the Network thread by the GUI.
|
/// Messages sent to the Network thread by the GUI.
|
||||||
pub enum NetworkCommand {
|
pub enum NetworkCommand {
|
||||||
|
|||||||
@@ -2,8 +2,8 @@ pub struct UDPMessage {
|
|||||||
id: u32,
|
id: u32,
|
||||||
msg_type: u8,
|
msg_type: u8,
|
||||||
length: u16,
|
length: u16,
|
||||||
body: [u8; 985],
|
body: Vec<u8>,
|
||||||
signature: [u8; 32],
|
signature: Vec<u8>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct HandshakeMessage {
|
pub struct HandshakeMessage {
|
||||||
@@ -21,8 +21,8 @@ impl UDPMessage {
|
|||||||
id: id,
|
id: id,
|
||||||
msg_type: 0,
|
msg_type: 0,
|
||||||
length: 0,
|
length: 0,
|
||||||
body: [0; 985],
|
body: vec![0; 985],
|
||||||
signature: [0; 32],
|
signature: vec![0; 32],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -31,30 +31,28 @@ impl UDPMessage {
|
|||||||
id: id,
|
id: id,
|
||||||
msg_type: 129,
|
msg_type: 129,
|
||||||
length: 0,
|
length: 0,
|
||||||
body: [0; 985],
|
body: vec![0; 985],
|
||||||
signature: [0; 32],
|
signature: vec![0; 32],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse(received_message: [u8; 1024]) -> UDPMessage {
|
pub fn parse(received_message: Vec<u8>) -> UDPMessage {
|
||||||
let id_bytes: [u8; 4] = received_message[0..4]
|
let id_bytes: [u8; 4] = received_message[0..4]
|
||||||
.try_into()
|
.try_into()
|
||||||
.expect("Taille incorrecte");
|
.expect("Taille incorrecte");
|
||||||
let length_bytes: [u8; 2] = received_message[5..7]
|
let length_bytes: [u8; 2] = received_message[5..7]
|
||||||
.try_into()
|
.try_into()
|
||||||
.expect("Taille incorrecte");
|
.expect("Taille incorrecte");
|
||||||
let name_bytes: [u8; 985] = received_message[7..992]
|
let msg_length = u16::from_be_bytes(length_bytes);
|
||||||
.try_into()
|
let name_bytes = &received_message[7..msg_length as usize + 8];
|
||||||
.expect("Taille incorrecte");
|
let signature_bytes =
|
||||||
let signature_bytes: [u8; 32] = received_message[992..1024]
|
&received_message[msg_length as usize + 8..msg_length as usize + 9 + 32];
|
||||||
.try_into()
|
|
||||||
.expect("Taille incorrecte");
|
|
||||||
UDPMessage {
|
UDPMessage {
|
||||||
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),
|
||||||
body: name_bytes,
|
body: name_bytes.to_vec(),
|
||||||
signature: signature_bytes,
|
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)]
|
#[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.
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
/*#[tokio::test]
|
/// creates an handshake message
|
||||||
async fn creating_cryptographic_signature() {
|
#[tokio::test]
|
||||||
|
async fn creating_handshake_msg() {
|
||||||
let username = String::from("charlie_kirk");
|
let username = String::from("charlie_kirk");
|
||||||
let handshake = HandshakeMessage::hello(0, 12, username);
|
let handshake = HandshakeMessage::hello(0, 12, username);
|
||||||
handshake.display();
|
handshake.display();
|
||||||
}*/
|
}
|
||||||
|
|
||||||
/*#[tokio::test]
|
/// parses an handshake message
|
||||||
|
#[tokio::test]
|
||||||
async fn parse_handshakemessage() {
|
async fn parse_handshakemessage() {
|
||||||
let username = String::from("charlie_kirk");
|
let username = String::from("charlie_kirk");
|
||||||
let handshake = HandshakeMessage::hello(0, 12, username);
|
let handshake = HandshakeMessage::hello(0, 12, username);
|
||||||
@@ -170,5 +166,5 @@ mod tests {
|
|||||||
let parsed = HandshakeMessage::parse(ser);
|
let parsed = HandshakeMessage::parse(ser);
|
||||||
handshake.display();
|
handshake.display();
|
||||||
parsed.display();
|
parsed.display();
|
||||||
}*/
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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<bytes::Bytes, reqwest::Error> {
|
|
||||||
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()))
|
|
||||||
}
|
|
||||||
}*/
|
|
||||||
@@ -3,9 +3,10 @@ use bytes::Bytes;
|
|||||||
use crate::cryptographic_signature::{CryptographicSignature, formatPubKey, sign_message};
|
use crate::cryptographic_signature::{CryptographicSignature, formatPubKey, sign_message};
|
||||||
use crate::messages_structure::{HandshakeMessage, UDPMessage};
|
use crate::messages_structure::{HandshakeMessage, UDPMessage};
|
||||||
use std::net::UdpSocket;
|
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(
|
async fn register_with_the_server(
|
||||||
crypto_pair: CryptographicSignature,
|
crypto_pair: CryptographicSignature,
|
||||||
) -> Result<(), reqwest::Error> {
|
) -> 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 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
|
|
||||||
let res = client.put(uri).body(pubkey_bytes_minus).send().await?;
|
let res = client.put(uri).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.");
|
||||||
@@ -34,6 +34,10 @@ async fn register_with_the_server(
|
|||||||
Ok(())
|
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<Bytes, reqwest::Error> {
|
async fn get_socket_address(username: String) -> Result<Bytes, reqwest::Error> {
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
let uri = format!("https://jch.irif.fr:8443/peers/{}/addresses", username);
|
let uri = format!("https://jch.irif.fr:8443/peers/{}/addresses", username);
|
||||||
@@ -50,11 +54,9 @@ async fn get_socket_address(username: String) -> Result<Bytes, reqwest::Error> {
|
|||||||
Ok(body)
|
Ok(body)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// It then
|
///
|
||||||
/// registers each of its IP addresses by sending a Hello request to the server.
|
/// registers the 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.
|
|
||||||
fn register_ip_addresses(crypto_pair: CryptographicSignature) {
|
fn register_ip_addresses(crypto_pair: CryptographicSignature) {
|
||||||
let socket = UdpSocket::bind("0.0.0.0:0").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();
|
||||||
@@ -68,7 +70,7 @@ fn register_ip_addresses(crypto_pair: CryptographicSignature) {
|
|||||||
.expect("send failed");
|
.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 = UDPMessage::parse(buf);
|
let hello_handshake_received = UDPMessage::parse(buf.to_vec());
|
||||||
hello_handshake_received.display();
|
hello_handshake_received.display();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,6 +79,9 @@ 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::*;
|
||||||
|
|
||||||
|
///
|
||||||
|
/// does the procedure to register with the server
|
||||||
|
///
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn registering_with_server() {
|
async fn registering_with_server() {
|
||||||
let username = String::from("gamixtreize");
|
let username = String::from("gamixtreize");
|
||||||
@@ -86,6 +91,9 @@ mod tests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
/// retreives the socket address of a given peer
|
||||||
|
///
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn retreive_socket_addr() {
|
async fn retreive_socket_addr() {
|
||||||
let username = String::from("ipjkndqfshjldfsjlbsdfjhhj");
|
let username = String::from("ipjkndqfshjldfsjlbsdfjhhj");
|
||||||
|
|||||||
Reference in New Issue
Block a user