@@ 121,7 121,9 @@ fn handle_client(mut stream: TcpStream, pubkey: PublicKey, seckey: SecretKey) ->
match b[0] {
0 => handle_receiver(stream, &pubkey, &seckey),
1 => handle_sender(stream, &pubkey, &seckey),
- 2 => handle_remover(stream, &pubkey, &seckey),
+ // 2 => handle_namer(stream, &pubkey, &seckey),
+ // 3 => handle_lister(stream, &pubkey, &seckey),
+ 4 => handle_remover(stream, &pubkey, &seckey),
_ => Ok(()),
}
}
@@ 159,9 161,8 @@ fn handle_sender(mut stream: TcpStream, pubkey: &PublicKey, seckey: &SecretKey)
Ok(())
}
-fn handle_receiver(mut stream: TcpStream, pubkey: &PublicKey, seckey: &SecretKey) -> anyhow::Result<()> {
- let (user_id, session_key) = handshake(&mut stream, pubkey, seckey)?;
- let mut stream = EncryptedStream{stream, key: session_key, nonce: Nonce([0; NONCEBYTES])};
+fn handle_receiver(stream: TcpStream, pubkey: &PublicKey, seckey: &SecretKey) -> anyhow::Result<()> {
+ let (user_id, mut stream) = login(stream, pubkey, seckey)?;
// Read their device ID and new receiving key.
let buf = stream.receive(4+PUBLICKEYBYTES)?;
let device_id: [u8; 4] = buf[0..4].try_into().unwrap();
@@ 179,10 180,8 @@ fn handle_receiver(mut stream: TcpStream, pubkey: &PublicKey, seckey: &SecretKey
}
}
-fn handle_remover(mut stream: TcpStream, pubkey: &PublicKey, seckey: &SecretKey) -> anyhow::Result<()> {
- let (user_id, session_key) = handshake(&mut stream, pubkey, seckey)?;
- let mut stream = EncryptedStream{stream, key: session_key, nonce: Nonce([0; NONCEBYTES])};
- // Read the device ID to revoke.
+fn handle_remover(stream: TcpStream, pubkey: &PublicKey, seckey: &SecretKey) -> anyhow::Result<()> {
+ let (user_id, mut stream) = login(stream, pubkey, seckey)?;
let device_id = stream.receive(4)?;
let path = format!("{}/{}", base64_encode(user_id.0), base64_encode(device_id));
fs::remove_dir_all(format!("inboxes/{}", path)).context("failed to remove inbox")?;
@@ 191,7 190,7 @@ fn handle_remover(mut stream: TcpStream, pubkey: &PublicKey, seckey: &SecretKey)
Ok(())
}
-fn handshake(stream: &mut TcpStream, pubkey: &PublicKey, seckey: &SecretKey) -> anyhow::Result<(PublicKey, PrecomputedKey)> {
+fn login(mut stream: TcpStream, pubkey: &PublicKey, seckey: &SecretKey) -> anyhow::Result<(PublicKey, EncryptedStream)> {
// We have already sent the server's public key.
// Read the client's ID.
let mut buf = [0; PUBLICKEYBYTES + SEALBYTES];
@@ 199,7 198,8 @@ fn handshake(stream: &mut TcpStream, pubkey: &PublicKey, seckey: &SecretKey) ->
let user_id_buf = sealedbox::open(&buf, pubkey, seckey).map_err(|_| anyhow!("failed to decrypt user's ID"))?;
let user_id = PublicKey::from_slice(&user_id_buf).unwrap();
let session_key = precompute(&user_id, seckey);
- Ok((user_id, session_key))
+ let stream = EncryptedStream{stream, key: session_key, nonce: Nonce([0; NONCEBYTES])};
+ Ok((user_id, stream))
}
struct EncryptedStream {