You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
1.0 KiB
59 lines
1.0 KiB
//! Socket encryption wrapper
|
|
use super::*;
|
|
use cryptohelpers::{
|
|
rsa::{
|
|
RsaPublicKey,
|
|
RsaPrivateKey,
|
|
},
|
|
sha256,
|
|
};
|
|
use chacha20stream::{
|
|
AsyncSink,
|
|
};
|
|
use std::sync::Arc;
|
|
use tokio::{
|
|
sync::{
|
|
RwLock,
|
|
},
|
|
io::{
|
|
self,
|
|
DuplexStream,
|
|
},
|
|
};
|
|
|
|
/// Encrypted socket information.
|
|
#[derive(Debug)]
|
|
struct ESockInfo {
|
|
us: RsaPrivateKey,
|
|
them: Option<RsaPublicKey>,
|
|
}
|
|
|
|
/// A tx+rx socket.
|
|
#[pin_project]
|
|
#[derive(Debug)]
|
|
pub struct ESock<W, R> {
|
|
info: RwLock<ESockInfo>,
|
|
|
|
#[pin]
|
|
// Raw (not encrypted) reader
|
|
rx: R,
|
|
#[pin]
|
|
tx: AsyncSink<W>,
|
|
}
|
|
|
|
/// Write half for `ESock`.
|
|
#[pin_project]
|
|
#[derive(Debug)]
|
|
pub struct ESockWriteHalf<W>(Arc<ESockInfo>, #[pin] AsyncSink<W>);
|
|
|
|
/// Read half for `ESock`.
|
|
#[pin_project]
|
|
#[derive(Debug)]
|
|
pub struct ESockReadHalf<R>(
|
|
Arc<ESockInfo>,
|
|
|
|
#[pin] R, // read from this (raw.)
|
|
#[pin] AsyncSink<DuplexStream>, // sink raw from `R` here, outputs decrypted bytes into next.
|
|
#[pin] DuplexStream, // read decrypted bytes from here.
|
|
);
|