From aee28ef5d1be59c0db218d7130fd1ae2314cc970 Mon Sep 17 00:00:00 2001 From: Ummon Date: Tue, 28 Oct 2014 12:44:08 +0100 Subject: [PATCH] First commit. --- Cargo.lock | 12 +++++++++ Cargo.toml | 15 +++++++++++ src/client.rs | 35 +++++++++++++++++++++++++ src/command.rs | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/crypto.rs | 48 +++++++++++++++++++++++++++++++++++ src/main.rs | 56 ++++++++++++++++++++++++++++++++++++++++ src/server.rs | 48 +++++++++++++++++++++++++++++++++++ 7 files changed, 283 insertions(+) create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/client.rs create mode 100644 src/command.rs create mode 100644 src/crypto.rs create mode 100644 src/main.rs create mode 100644 src/server.rs diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..fb3c642 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,12 @@ +[root] +name = "lab1_rust" +version = "0.0.1" +dependencies = [ + "openssl 0.0.0 (git+https://github.com/sfackler/rust-openssl.git#b41201c3c9fddf42d02f71d11b5d4718eb9e88c4)", +] + +[[package]] +name = "openssl" +version = "0.0.0" +source = "git+https://github.com/sfackler/rust-openssl.git#b41201c3c9fddf42d02f71d11b5d4718eb9e88c4" + diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..3f218e2 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,15 @@ +[package] + +name = "lab1_rust" +version = "0.0.1" +authors = ["Ummon "] + + +[[bin]] + +name = "lab1_rust" + + +[dependencies.openssl] + +git = "https://github.com/sfackler/rust-openssl.git" \ No newline at end of file diff --git a/src/client.rs b/src/client.rs new file mode 100644 index 0000000..8392460 --- /dev/null +++ b/src/client.rs @@ -0,0 +1,35 @@ +use std::io::{ TcpStream, IoResult }; +use command::{ Command, Packet, Error }; +use crypto::Crypto; + +pub struct Client { + socket: TcpStream, + crypto: Crypto, + current_timestamp: u64 +} + +impl Client { + pub fn new(address: &str, port: u16) -> IoResult { + Ok(Client { + socket: try!(TcpStream::connect(address, port)), + current_timestamp: 0, + crypto: Crypto::new() + }) + } + + pub fn close(&mut self) { + self.socket.close_read(); + self.socket.close_write(); + } + + pub fn start_tests(&mut self) { + + let command = Packet::newRandomCommand(self.current_timestamp); + self.send(command); + + } + + fn send(&mut self, p: Packet) { + p.write(&mut self.socket); + } +} \ No newline at end of file diff --git a/src/command.rs b/src/command.rs new file mode 100644 index 0000000..7de2afc --- /dev/null +++ b/src/command.rs @@ -0,0 +1,69 @@ +use std::io; +use std::rand::{ random, task_rng, distributions }; +use std::rand::distributions::IndependentSample; + +pub enum ReadingError { + ReadIOError(io::IoError) + // TODO... +} + +pub enum WritingError { + WriteIOError(io::IoError) + // TODO... +} + +pub type ReadingResult = Result; +pub type WritingResult = Result<(), WritingError>; + +pub enum PacketType { + CommandType, + AnswerType +} + +pub struct CommandPacket { + t: PacketType, + timestamp: u64, + commandID: u8, + commandPayload: Vec +} + +pub struct ErrorPacket { + timestamp: u64 +} + +pub enum Packet { + Command(CommandPacket), + Error(ErrorPacket) +} + +impl Packet { + pub fn newRandomCommand(timestamp: u64) -> Packet { + + let mut rng = task_rng(); + Command(CommandPacket { + t: CommandType, + timestamp: timestamp, + commandID: random::(), + commandPayload: Vec::from_fn(distributions::Range::new(7u, 40u).ind_sample(&mut rng), |idx| 0u8) + }) + } + + pub fn write(&self, output: &mut io::Writer) -> WritingResult { + + match self { + &Command(ref command) => { + output.write_u8(0); + output.write_be_u64(command.timestamp); + + + }, + &Error(error) => () + } + + Ok(()) + } + + pub fn read(input: &mut io::Reader) -> ReadingResult { + Ok(Packet::newRandomCommand(0)) + } +} \ No newline at end of file diff --git a/src/crypto.rs b/src/crypto.rs new file mode 100644 index 0000000..1e1ddc1 --- /dev/null +++ b/src/crypto.rs @@ -0,0 +1,48 @@ +use std::rand::OsRng; +use std::vec::Vec; +use std::io::IoResult; +use std::rand::Rng; + +use openssl::crypto::hash::SHA256; +use openssl::crypto::hmac::HMAC; +use std::slice::bytes::copy_memory; + +//const KEY_A: &'static [u8] = "49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d".from_hex().unwrap(); +//static KEY_A: Vec = vec![1, 2, 3]; // "49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d".from_hex().unwrap(); + +pub struct Crypto { + key_a: Vec, + key_c: Vec +} + +impl Crypto { + pub fn new() -> Crypto { + Crypto { + key_a: [125, 31, 131, 118, 143, 180, 252, 53, 211, 217, 79, 240, 128, 91, 252, 87, 104, 236, 145, 198, 163, 203, 161, 12, 53, 56, 218, 40, 221, 95, 171, 140].to_vec(), + key_c: [75, 226, 88, 31, 223, 216, 182, 216, 178, 58, 59, 193, 245, 80, 254, 128, 125, 246, 246, 224, 194, 190, 123, 123, 10, 131, 217, 183, 112, 157, 166, 102].to_vec() + } + } + + pub fn compute_mac(&self, data: &[u8]) -> [u8, ..10] { + let mut hmac = HMAC(SHA256, self.key_a.as_slice()); + hmac.update(data); + let mut result = [0u8, ..10]; + copy_memory(&mut result, hmac.finalize().slice(0, 9)); + result + } +} + +pub fn encrypt(plaindata: &Vec) -> Vec { + vec!() +} + +pub fn decrypt(cypherdata: &Vec) -> Vec { + vec!() +} + +pub fn generate_key(size_byte: uint) -> IoResult> { + let mut bytes = Vec::from_elem(size_byte, 0u8); + let mut generator = try!(OsRng::new()); + generator.fill_bytes(bytes.as_mut_slice_()); + Ok(bytes) +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..e64323f --- /dev/null +++ b/src/main.rs @@ -0,0 +1,56 @@ +extern crate openssl; + +use std::io; +use std::os; + +mod crypto; +mod command; +mod client; +mod server; + +/* +TODO + * Comment stocker les clefs? à quels critères doivent elle répondre? + * +*/ + +const PORT: u16 = 4221; + +fn print_usage() { + println!("{} | ...", os::args()[0]); +} + +fn main() { + let args = os::args(); + + if args.iter().any(|a| a.as_slice() == "--help" || a.as_slice() == "-h") { + print_usage(); + } else if args.len() > 1 && args[1].as_slice() == "genkey" { + match crypto::generate_key(256 / 8) { + Ok(key) => println!("key: {}", key), + Err(e) => println!("Unable to generate a key. Error: {}", e) + } + } else { + match server::Server::new(PORT) { + Ok(mut server) => { + println!("Server started"); + + match client::Client::new("127.0.0.1", PORT) { + Ok(mut client) => { + client.start_tests(); + client.close(); + }, + Err(e) => { + println!("Unable to create a client. Error: {}", e); + return + } + } + + println!("Press any key to quit"); + io::stdin().read_line().ok().expect("Failed to read line"); + server.close().ok().expect("Failed to close the server"); + }, + Err(e) => println!("Unable to create a new server. Error: {}", e) + } + } +} diff --git a/src/server.rs b/src/server.rs new file mode 100644 index 0000000..3eb1a46 --- /dev/null +++ b/src/server.rs @@ -0,0 +1,48 @@ +use std::io::{ TcpListener, TcpStream, Acceptor, Listener, IoError, IoResult }; +use std::io::net::tcp::{ TcpAcceptor }; +use command::{ Command, Packet, Error }; +use crypto::Crypto; + +pub struct Server { + acceptor: TcpAcceptor, + crypto: Crypto +} + +impl Server { + pub fn new(port: u16) -> IoResult { + let mut acceptor = try!(TcpListener::bind("127.0.0.1", port).listen()); + let server = Server { + acceptor: acceptor.clone(), + crypto: Crypto::new() + }; + + spawn(proc() { + loop { + for stream in acceptor.incoming() { + match stream { + Err(_) => return, + Ok(stream) => spawn(proc() { + Server::handle_client(stream); + }) + } + } + } + }); + + Ok(server) + } + + pub fn close(&mut self) -> IoResult<()> { + self.acceptor.close_accept() + } + + fn handle_client(mut stream: TcpStream) { + println!("new connection!"); + let mut current_timestamp = 0u64; + match Packet::read(&mut stream) { + Ok(Command(packet)) => println!("CommandPacket"), + Ok(Error(packet)) => println!("AnswerPacket"), + Err(io_error) => println!("IO Error") + } + } +} \ No newline at end of file -- 2.45.2