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.
62 lines
1.3 KiB
62 lines
1.3 KiB
use super::*;
|
|
use crypt::{
|
|
RsaPublicKey,
|
|
RsaPrivateKey,
|
|
};
|
|
|
|
/// Inner rsa data for encrypted stream read+write halves
|
|
struct EncryptedStreamMeta
|
|
{
|
|
us: RsaPrivateKey,
|
|
them: Option<RsaPublicKey>,
|
|
}
|
|
|
|
/// Writable half of `EncryptedStream`.
|
|
pub struct WriteHalf<S>
|
|
where S: AsyncWrite
|
|
{
|
|
meta: Arc<EncryptedStreamMeta>,
|
|
|
|
backing_write: Box<dual::DualStream<S>>,
|
|
}
|
|
|
|
/// Readable half of `EncryptedStream`.
|
|
pub struct ReadHalf<S>
|
|
where S: AsyncRead
|
|
{
|
|
meta: Arc<EncryptedStreamMeta>,
|
|
|
|
/// chacha20_poly1305 decrypter for incoming reads from `S`
|
|
//TODO: chacha20stream: implement a read version of AsyncSink so we don't need to keep this?
|
|
cipher: Option<Crypter>,
|
|
backing_read: Box<S>,
|
|
}
|
|
|
|
struct ReadWriteCombined<R, W>
|
|
{
|
|
/// Since chacha20stream has no AsyncRead counterpart, we have to do it ourselves.
|
|
cipher_read: Option<Crypter>,
|
|
backing_read: R,
|
|
|
|
backing_write: dual::DualStream<W>,
|
|
}
|
|
|
|
/// RSA/chacha20 encrypted stream
|
|
pub struct EncryptedStream<S>
|
|
where S: AsyncStream
|
|
{
|
|
meta: EncryptedStreamMeta,
|
|
|
|
// Keep the streams on the heap to keep this type not hueg.
|
|
backing: Box<ReadWriteCombined<S, S>>,
|
|
}
|
|
|
|
impl<S: AsyncStream> EncryptedStream<S>
|
|
{
|
|
/// Has this stream done its RSA key exchange?
|
|
pub fn has_exchanged(&self) -> bool
|
|
{
|
|
self.meta.them.is_some()
|
|
}
|
|
}
|