@@ -15,8 +15,8 @@ structopt = "0.3.14" | |||||
chrono = "0.4.11" | chrono = "0.4.11" | ||||
futures = "0.3.5" | futures = "0.3.5" | ||||
toml = "0.5.6" | toml = "0.5.6" | ||||
ilmp = { git = "https://github.com/izzabelle/ilmp" } | |||||
# ilmp = { path = "../ilmp"} | |||||
# ilmp = { git = "https://github.com/izzabelle/ilmp" } | |||||
ilmp = { path = "../ilmp"} | |||||
ring = "0.16.13" | ring = "0.16.13" | ||||
thiserror = "1.0.18" | thiserror = "1.0.18" | ||||
orion = "0.15.1" | orion = "0.15.1" | ||||
@@ -4,12 +4,8 @@ use crate::Result; | |||||
use async_std::{io, net::TcpStream, task}; | use async_std::{io, net::TcpStream, task}; | ||||
use futures::io::{ReadHalf, WriteHalf}; | use futures::io::{ReadHalf, WriteHalf}; | ||||
use futures_util::io::AsyncReadExt; | use futures_util::io::AsyncReadExt; | ||||
use ilmp::{ | |||||
encrypt::{EncryptKind, Encryption, SymmetricEncrypt}, | |||||
Sendable, | |||||
}; | |||||
use ilmp::{encrypt::SymmetricEncrypt, Sendable}; | |||||
use lazy_static::lazy_static; | use lazy_static::lazy_static; | ||||
use orion::aead; | |||||
use std::sync::Mutex; | use std::sync::Mutex; | ||||
lazy_static! { | lazy_static! { | ||||
@@ -47,12 +43,8 @@ pub async fn outgoing(mut write: WriteHalf<TcpStream>, encryption: SymmetricEncr | |||||
pub async fn incoming(mut read: ReadHalf<TcpStream>, encryption: SymmetricEncrypt) -> Result<()> { | pub async fn incoming(mut read: ReadHalf<TcpStream>, encryption: SymmetricEncrypt) -> Result<()> { | ||||
loop { | loop { | ||||
let packet = ilmp::read(&mut read).await?; | |||||
if let Some(mut packet) = packet { | |||||
if packet.encrypt_kind == EncryptKind::Symmetric { | |||||
packet.contents = aead::open(encryption.key().unwrap(), &packet.contents)?; | |||||
} | |||||
let packet = ilmp::read(&mut read, &encryption).await?; | |||||
if let Some(packet) = packet { | |||||
let res = match packet.kind { | let res = match packet.kind { | ||||
ilmp::PacketKind::Message => ilmp::Message::from_packet(packet), | ilmp::PacketKind::Message => ilmp::Message::from_packet(packet), | ||||
_ => panic!("bad packet"), | _ => panic!("bad packet"), | ||||
@@ -45,15 +45,16 @@ pub async fn initialize_connection( | |||||
ilmp::write(write, agreement_packet, &encrypt::NoEncrypt::new()).await?; | ilmp::write(write, agreement_packet, &encrypt::NoEncrypt::new()).await?; | ||||
// receive peer's pub key | // receive peer's pub key | ||||
let packet = ilmp::read(read).await?.unwrap(); | |||||
let packet = ilmp::read(read, &encrypt::NoEncrypt::new()).await?.unwrap(); | |||||
let agreement_packet = ilmp::Agreement::from_packet(packet)?; | let agreement_packet = ilmp::Agreement::from_packet(packet)?; | ||||
let peer_pub_key = | let peer_pub_key = | ||||
agreement::UnparsedPublicKey::new(&agreement::X25519, agreement_packet.public_key); | agreement::UnparsedPublicKey::new(&agreement::X25519, agreement_packet.public_key); | ||||
// generate aead key | // generate aead key | ||||
agreement::agree_ephemeral(my_priv_key, &peer_pub_key, MsgError::Ring, |key_material| { | agreement::agree_ephemeral(my_priv_key, &peer_pub_key, MsgError::Ring, |key_material| { | ||||
let key_material = | |||||
digest::digest(&digest::SHA256, key_material.as_ref().into()).as_ref().to_vec(); | |||||
let key_material = digest::digest(&digest::SHA256, key_material.as_ref().into()) | |||||
.as_ref() | |||||
.to_vec(); | |||||
Ok(aead::SecretKey::from_slice(&key_material)?) | Ok(aead::SecretKey::from_slice(&key_material)?) | ||||
}) | }) | ||||
} | } |
@@ -11,7 +11,6 @@ use ilmp::encrypt; | |||||
use ilmp::encrypt::Encryption; | use ilmp::encrypt::Encryption; | ||||
use ilmp::Sendable; | use ilmp::Sendable; | ||||
use lazy_static::lazy_static; | use lazy_static::lazy_static; | ||||
use orion::aead; | |||||
use std::collections::HashMap; | use std::collections::HashMap; | ||||
use uuid::Uuid; | use uuid::Uuid; | ||||
@@ -58,12 +57,8 @@ async fn handle_stream( | |||||
encryption: encrypt::SymmetricEncrypt, | encryption: encrypt::SymmetricEncrypt, | ||||
) -> Result<()> { | ) -> Result<()> { | ||||
loop { | loop { | ||||
let packet = ilmp::read(&mut stream).await?; | |||||
if let Some(mut packet) = packet { | |||||
if packet.encrypt_kind == encrypt::EncryptKind::Symmetric { | |||||
packet.contents = aead::open(encryption.key().unwrap(), &packet.contents)?; | |||||
} | |||||
let packet = ilmp::read(&mut stream, &encryption).await?; | |||||
if let Some(packet) = packet { | |||||
let res = match packet.kind { | let res = match packet.kind { | ||||
ilmp::PacketKind::Message => ilmp::Message::from_packet(packet), | ilmp::PacketKind::Message => ilmp::Message::from_packet(packet), | ||||
_ => panic!("bad packet"), | _ => panic!("bad packet"), | ||||
@@ -80,9 +75,10 @@ async fn handle_stream( | |||||
Ok(()) | Ok(()) | ||||
} | } | ||||
async fn relay_packet<T>(packet: T, encryption: &encrypt::SymmetricEncrypt) -> Result<()> | |||||
async fn relay_packet<T, E>(packet: T, encryption: &E) -> Result<()> | |||||
where | where | ||||
T: Clone + Sendable, | T: Clone + Sendable, | ||||
E: Encryption, | |||||
{ | { | ||||
let mut locked_write_streams = WRITE_STREAMS.lock().await; | let mut locked_write_streams = WRITE_STREAMS.lock().await; | ||||
let stream = futures::stream::iter(locked_write_streams.iter_mut()); | let stream = futures::stream::iter(locked_write_streams.iter_mut()); | ||||