|
| 1 | +//! Request-Reply with challenge-response authentication (insecure demo). |
| 2 | +//! |
| 3 | +//! Protocol: |
| 4 | +//! 1. Server sends a random 32-byte challenge |
| 5 | +//! 2. Client computes a signature using a shared secret and sends it back |
| 6 | +//! 3. Server verifies the signature and accepts or rejects |
| 7 | +
|
| 8 | +use bytes::Bytes; |
| 9 | +use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt}; |
| 10 | +use tokio_stream::StreamExt; |
| 11 | + |
| 12 | +use msg::{RepSocket, ReqSocket, hooks::{ConnectionHook, Error, HookResult}, tcp::Tcp}; |
| 13 | + |
| 14 | +const SECRET: [u8; 32] = *b"this_is_a_32_byte_secret_key!!!!"; |
| 15 | + |
| 16 | +/// Simple XOR-based MAC |
| 17 | +fn compute_mac(secret: &[u8; 32], challenge: &[u8; 32]) -> [u8; 32] { |
| 18 | + let mut mac = [0u8; 32]; |
| 19 | + for i in 0..32 { |
| 20 | + mac[i] = secret[i] ^ challenge[i] ^ secret[(i + 1) % 32]; |
| 21 | + } |
| 22 | + mac |
| 23 | +} |
| 24 | + |
| 25 | +struct ChallengeServerHook { |
| 26 | + secret: [u8; 32], |
| 27 | +} |
| 28 | + |
| 29 | +impl ChallengeServerHook { |
| 30 | + fn new(secret: [u8; 32]) -> Self { |
| 31 | + Self { secret } |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +#[derive(Debug, thiserror::Error)] |
| 36 | +#[error("client authentication failed")] |
| 37 | +struct ServerAuthError; |
| 38 | + |
| 39 | +impl<Io> ConnectionHook<Io> for ChallengeServerHook |
| 40 | +where |
| 41 | + Io: AsyncRead + AsyncWrite + Send + Unpin + 'static, |
| 42 | +{ |
| 43 | + type Error = ServerAuthError; |
| 44 | + |
| 45 | + async fn on_connection(&self, mut io: Io) -> HookResult<Io, Self::Error> { |
| 46 | + let challenge: [u8; 32] = rand::random(); |
| 47 | + io.write_all(&challenge).await?; |
| 48 | + |
| 49 | + let mut response = [0u8; 32]; |
| 50 | + io.read_exact(&mut response).await?; |
| 51 | + |
| 52 | + let expected = compute_mac(&self.secret, &challenge); |
| 53 | + if response != expected { |
| 54 | + io.write_all(&[0]).await?; |
| 55 | + return Err(Error::hook(ServerAuthError)); |
| 56 | + } |
| 57 | + |
| 58 | + io.write_all(&[1]).await?; |
| 59 | + Ok(io) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +struct ChallengeClientHook { |
| 64 | + secret: [u8; 32], |
| 65 | +} |
| 66 | + |
| 67 | +impl ChallengeClientHook { |
| 68 | + fn new(secret: [u8; 32]) -> Self { |
| 69 | + Self { secret } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +#[derive(Debug, thiserror::Error)] |
| 74 | +#[error("server rejected authentication")] |
| 75 | +struct ClientAuthError; |
| 76 | + |
| 77 | +impl<Io> ConnectionHook<Io> for ChallengeClientHook |
| 78 | +where |
| 79 | + Io: AsyncRead + AsyncWrite + Send + Unpin + 'static, |
| 80 | +{ |
| 81 | + type Error = ClientAuthError; |
| 82 | + |
| 83 | + async fn on_connection(&self, mut io: Io) -> HookResult<Io, Self::Error> { |
| 84 | + let mut challenge = [0u8; 32]; |
| 85 | + io.read_exact(&mut challenge).await?; |
| 86 | + |
| 87 | + let response = compute_mac(&self.secret, &challenge); |
| 88 | + io.write_all(&response).await?; |
| 89 | + |
| 90 | + let mut verdict = [0u8; 1]; |
| 91 | + io.read_exact(&mut verdict).await?; |
| 92 | + |
| 93 | + if verdict[0] != 1 { |
| 94 | + return Err(Error::hook(ClientAuthError)); |
| 95 | + } |
| 96 | + |
| 97 | + Ok(io) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +#[tokio::main] |
| 102 | +async fn main() { |
| 103 | + let mut rep = |
| 104 | + RepSocket::new(Tcp::default()).with_connection_hook(ChallengeServerHook::new(SECRET)); |
| 105 | + rep.bind("0.0.0.0:4445").await.unwrap(); |
| 106 | + |
| 107 | + let mut req = |
| 108 | + ReqSocket::new(Tcp::default()).with_connection_hook(ChallengeClientHook::new(SECRET)); |
| 109 | + req.connect("0.0.0.0:4445").await.unwrap(); |
| 110 | + |
| 111 | + tokio::spawn(async move { |
| 112 | + let req = rep.next().await.unwrap(); |
| 113 | + println!("Server received: {:?}", req.msg()); |
| 114 | + req.respond(Bytes::from("authenticated response")).unwrap(); |
| 115 | + }); |
| 116 | + |
| 117 | + let res: Bytes = req.request(Bytes::from("hello")).await.unwrap(); |
| 118 | + println!("Client received: {res:?}"); |
| 119 | +} |
0 commit comments