Bläddra i källkod

working encryption!

master
Isabelle L. 5 år sedan
förälder
incheckning
db63dc576e
4 ändrade filer med 24 tillägg och 8 borttagningar
  1. +1
    -0
      Cargo.lock
  2. +5
    -3
      src/client.rs
  3. +1
    -1
      src/lib.rs
  4. +17
    -4
      src/server.rs

+ 1
- 0
Cargo.lock Visa fil

@@ -345,6 +345,7 @@ dependencies = [
"crc32fast",
"futures",
"futures-util",
"orion",
"ring",
"serde",
"serde_json",


+ 5
- 3
src/client.rs Visa fil

@@ -3,6 +3,7 @@ use crate::config::ClientConfig as Config;
use crate::Result;
use async_std::net::TcpStream;
use futures_util::io::AsyncReadExt;
use ilmp::encrypt;

/// wraps the client
pub async fn client(port: u16) -> Result<()> {
@@ -13,14 +14,15 @@ pub async fn client(port: u16) -> Result<()> {
let (mut read, mut write) = stream.split();

let key = crate::initialize_connection(&mut read, &mut write).await?;
println!("{:?}", key);
let encryption = encrypt::SymmetricEncrypt::new(key);
println!("successfully hardened connection");

/*let message = ilmp::Message::new(
let message = ilmp::Message::new(
"Isabelle".to_owned(),
"oh god oh fuck this shit actually works".to_owned(),
);

ilmp::write(&mut stream, message, encrypt::NoEncrypt::new()).await?;*/
ilmp::write(&mut write, message, &encryption).await?;

loop {}
}

+ 1
- 1
src/lib.rs Visa fil

@@ -42,7 +42,7 @@ pub async fn initialize_connection(
agreement::EphemeralPrivateKey::generate(&agreement::X25519, &rng).expect("ring broke");
let my_pub_key = my_priv_key.compute_public_key().expect("ring broke");
let agreement_packet = ilmp::Agreement::new(my_pub_key.as_ref().into());
ilmp::write(write, agreement_packet, encrypt::NoEncrypt::new()).await?;
ilmp::write(write, agreement_packet, &encrypt::NoEncrypt::new()).await?;

// receive peer's pub key
let packet = ilmp::read(read).await?.unwrap();


+ 17
- 4
src/server.rs Visa fil

@@ -6,8 +6,11 @@ use async_std::{
};
use futures::io::{ReadHalf, WriteHalf};
use futures_util::{io::AsyncReadExt, stream::StreamExt};
use ilmp::encrypt;
use ilmp::encrypt::Encryption;
use ilmp::Sendable;
use lazy_static::lazy_static;
use orion::aead;
use std::{collections::HashMap, sync::Mutex};
use uuid::Uuid;

@@ -32,20 +35,30 @@ pub async fn server(port: u16) -> Result<()> {

let (mut read, mut write) = stream.split();
let stream_id = Uuid::new_v4();

let key = crate::initialize_connection(&mut read, &mut write).await?;
println!("{:?}", key);
let encryption = encrypt::SymmetricEncrypt::new(key);
println!("successfully hardened connection");

WRITE_STREAMS.lock().expect("could not aqcuire lock").insert(stream_id.clone(), write);
task::spawn(handle_stream(read, stream_id));
task::spawn(handle_stream(read, stream_id, encryption));
}

Ok(())
}

async fn handle_stream(mut stream: ReadHalf<TcpStream>, stream_id: Uuid) -> Result<()> {
async fn handle_stream(
mut stream: ReadHalf<TcpStream>,
stream_id: Uuid,
encryption: encrypt::SymmetricEncrypt,
) -> Result<()> {
loop {
let packet = ilmp::read(&mut stream).await?;
if let Some(packet) = packet {
if let Some(mut packet) = packet {
if packet.encrypt_kind == encrypt::EncryptKind::Symmetric {
packet.contents = aead::open(encryption.key().unwrap(), &packet.contents)?;
}

let res = match packet.kind {
ilmp::PacketKind::Message => ilmp::Message::from_packet(packet),
_ => panic!("bad packet"),


Laddar…
Avbryt
Spara