|
1 | 1 | //! A system for creating encrypted tunnels between peers over untrusted connections. |
2 | 2 |
|
3 | | -mod tunnel; |
| 3 | +use std::{ |
| 4 | + io, |
| 5 | + pin::Pin, |
| 6 | + task::{Context, Poll}, |
| 7 | +}; |
4 | 8 |
|
5 | | -pub use sd_p2p::{Identity, IdentityErr, RemoteIdentity}; |
6 | | -pub use tunnel::*; |
| 9 | +use sd_p2p_proto::{decode, encode}; |
| 10 | +use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt, ReadBuf}; |
| 11 | + |
| 12 | +use thiserror::Error; |
| 13 | + |
| 14 | +use sd_p2p::{Identity, IdentityErr, RemoteIdentity, UnicastStream}; |
| 15 | + |
| 16 | +#[derive(Debug, Error)] |
| 17 | +pub enum TunnelError { |
| 18 | + #[error("Error writing discriminator.")] |
| 19 | + DiscriminatorWriteError, |
| 20 | + #[error("Error reading discriminator. Is this stream actually a tunnel?")] |
| 21 | + DiscriminatorReadError, |
| 22 | + #[error("Invalid discriminator. Is this stream actually a tunnel?")] |
| 23 | + InvalidDiscriminator, |
| 24 | + #[error("Error sending library id: {0:?}")] |
| 25 | + ErrorSendingLibraryId(io::Error), |
| 26 | + #[error("Error receiving library identity: {0:?}")] |
| 27 | + ErrorReceivingLibraryIdentity(decode::Error), |
| 28 | + #[error("Error decoding library identity: {0:?}")] |
| 29 | + ErrorDecodingLibraryIdentity(IdentityErr), |
| 30 | +} |
| 31 | + |
| 32 | +/// An encrypted tunnel between two libraries. |
| 33 | +/// |
| 34 | +/// This sits on top of the existing node to node encryption provided by Quic. |
| 35 | +/// |
| 36 | +/// It's primarily designed to avoid an attack where traffic flows: |
| 37 | +/// node <-> attacker node <-> node |
| 38 | +/// The attackers node can't break TLS but if they get in the middle they can present their own node identity to each side and then intercept library related traffic. |
| 39 | +/// To avoid that we use this tunnel to encrypt all library related traffic so it can only be decoded by another instance of the same library. |
| 40 | +#[derive(Debug)] |
| 41 | +pub struct Tunnel { |
| 42 | + stream: UnicastStream, |
| 43 | + library_remote_id: RemoteIdentity, |
| 44 | +} |
| 45 | + |
| 46 | +impl Tunnel { |
| 47 | + /// Create a new tunnel. |
| 48 | + /// |
| 49 | + /// This should be used by the node that initiated the request which this tunnel is used for. |
| 50 | + pub async fn initiator( |
| 51 | + mut stream: UnicastStream, |
| 52 | + library_identity: &Identity, |
| 53 | + ) -> Result<Self, TunnelError> { |
| 54 | + stream |
| 55 | + .write_all(&[b'T']) |
| 56 | + .await |
| 57 | + .map_err(|_| TunnelError::DiscriminatorWriteError)?; |
| 58 | + |
| 59 | + let mut buf = vec![]; |
| 60 | + encode::buf(&mut buf, &library_identity.to_remote_identity().get_bytes()); |
| 61 | + stream |
| 62 | + .write_all(&buf) |
| 63 | + .await |
| 64 | + .map_err(TunnelError::ErrorSendingLibraryId)?; |
| 65 | + |
| 66 | + // TODO: Do encryption things |
| 67 | + |
| 68 | + Ok(Self { |
| 69 | + stream, |
| 70 | + library_remote_id: library_identity.to_remote_identity(), |
| 71 | + }) |
| 72 | + } |
| 73 | + |
| 74 | + /// Create a new tunnel. |
| 75 | + /// |
| 76 | + /// This should be used by the node that responded to the request which this tunnel is used for. |
| 77 | + pub async fn responder(mut stream: UnicastStream) -> Result<Self, TunnelError> { |
| 78 | + let discriminator = stream |
| 79 | + .read_u8() |
| 80 | + .await |
| 81 | + .map_err(|_| TunnelError::DiscriminatorReadError)?; |
| 82 | + if discriminator != b'T' { |
| 83 | + return Err(TunnelError::InvalidDiscriminator); |
| 84 | + } |
| 85 | + |
| 86 | + // TODO: Blindly decoding this from the stream is not secure. We need a cryptographic handshake here to prove the peer on the other ends is holding the private key. |
| 87 | + let library_remote_id = decode::buf(&mut stream) |
| 88 | + .await |
| 89 | + .map_err(TunnelError::ErrorReceivingLibraryIdentity)?; |
| 90 | + |
| 91 | + let library_remote_id = RemoteIdentity::from_bytes(&library_remote_id) |
| 92 | + .map_err(TunnelError::ErrorDecodingLibraryIdentity)?; |
| 93 | + |
| 94 | + // TODO: Do encryption things |
| 95 | + |
| 96 | + Ok(Self { |
| 97 | + library_remote_id, |
| 98 | + stream, |
| 99 | + }) |
| 100 | + } |
| 101 | + |
| 102 | + /// Get the `RemoteIdentity` of the peer on the other end of the tunnel. |
| 103 | + pub fn node_remote_identity(&self) -> RemoteIdentity { |
| 104 | + self.stream.remote_identity() |
| 105 | + } |
| 106 | + |
| 107 | + /// Get the `RemoteIdentity` of the library instance on the other end of the tunnel. |
| 108 | + pub fn library_remote_identity(&self) -> RemoteIdentity { |
| 109 | + self.library_remote_id |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +impl AsyncRead for Tunnel { |
| 114 | + fn poll_read( |
| 115 | + self: Pin<&mut Self>, |
| 116 | + cx: &mut Context<'_>, |
| 117 | + buf: &mut ReadBuf<'_>, |
| 118 | + ) -> Poll<io::Result<()>> { |
| 119 | + // TODO: Do decryption |
| 120 | + |
| 121 | + Pin::new(&mut self.get_mut().stream).poll_read(cx, buf) |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +impl AsyncWrite for Tunnel { |
| 126 | + fn poll_write( |
| 127 | + self: Pin<&mut Self>, |
| 128 | + cx: &mut Context<'_>, |
| 129 | + buf: &[u8], |
| 130 | + ) -> Poll<io::Result<usize>> { |
| 131 | + // TODO: Do encryption |
| 132 | + |
| 133 | + Pin::new(&mut self.get_mut().stream).poll_write(cx, buf) |
| 134 | + } |
| 135 | + |
| 136 | + fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> { |
| 137 | + Pin::new(&mut self.get_mut().stream).poll_flush(cx) |
| 138 | + } |
| 139 | + |
| 140 | + fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> { |
| 141 | + Pin::new(&mut self.get_mut().stream).poll_shutdown(cx) |
| 142 | + } |
| 143 | +} |
0 commit comments