2023-02-15 10:14:13 +00:00
|
|
|
//! Connection handling and send/receive queues
|
|
|
|
|
2023-02-15 14:50:54 +00:00
|
|
|
pub mod handshake;
|
2023-02-15 10:14:13 +00:00
|
|
|
mod packet;
|
2023-02-15 14:50:54 +00:00
|
|
|
|
|
|
|
use ::std::vec::Vec;
|
|
|
|
|
|
|
|
pub use handshake::Handshake;
|
|
|
|
pub use packet::ConnectionID as ID;
|
2023-02-25 14:36:14 +00:00
|
|
|
pub use packet::{Packet, PacketData};
|
2023-02-15 14:50:54 +00:00
|
|
|
|
2023-02-26 09:44:21 +00:00
|
|
|
use crate::enc::{
|
|
|
|
hkdf::HkdfSha3,
|
|
|
|
sym::{CipherKind, CipherRecv, CipherSend},
|
|
|
|
};
|
|
|
|
|
2023-02-15 14:50:54 +00:00
|
|
|
/// Version of the fenrir protocol in use
|
|
|
|
#[derive(::num_derive::FromPrimitive, Debug, Copy, Clone)]
|
|
|
|
#[repr(u8)]
|
|
|
|
pub enum ProtocolVersion {
|
|
|
|
/// First Fenrir Protocol Version
|
|
|
|
V0 = 0,
|
|
|
|
}
|
2023-02-25 14:36:14 +00:00
|
|
|
impl ProtocolVersion {
|
|
|
|
/// actual length of the protocol version field
|
|
|
|
pub const fn len() -> usize {
|
|
|
|
1
|
|
|
|
}
|
|
|
|
/// Serialize into raw bytes
|
|
|
|
pub fn serialize(&self, out: &mut u8) {
|
|
|
|
*out = *self as u8;
|
|
|
|
}
|
|
|
|
}
|
2023-02-25 21:25:52 +00:00
|
|
|
|
|
|
|
/// A single connection and its data
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Connection {
|
|
|
|
/// Connection ID
|
|
|
|
pub id: ID,
|
|
|
|
/// The main hkdf used for all secrets in this connection
|
2023-02-26 09:44:21 +00:00
|
|
|
pub hkdf: HkdfSha3,
|
|
|
|
/// Cipher for decrypting data
|
|
|
|
pub cipher_recv: CipherRecv,
|
|
|
|
/// Cipher for encrypting data
|
|
|
|
pub cipher_send: CipherSend,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Role: used to set the correct secrets
|
|
|
|
/// * Server: Connection is Incoming
|
|
|
|
/// * Client: Connection is Outgoing
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
#[repr(u8)]
|
|
|
|
pub enum Role {
|
|
|
|
/// Server: we receive the connection
|
|
|
|
Server = 0,
|
|
|
|
/// Client: we initate the connection
|
|
|
|
Client,
|
2023-02-25 21:25:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Connection {
|
2023-02-26 09:44:21 +00:00
|
|
|
pub(crate) fn new(hkdf: HkdfSha3, cipher: CipherKind, role: Role) -> Self {
|
|
|
|
let (secret_recv, secret_send) = match role {
|
|
|
|
Role::Server => {
|
|
|
|
(hkdf.get_secret(b"to_server"), hkdf.get_secret(b"to_client"))
|
|
|
|
}
|
|
|
|
Role::Client => {
|
|
|
|
(hkdf.get_secret(b"to_client"), hkdf.get_secret(b"to_server"))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let mut cipher_recv = CipherRecv::new(cipher, secret_recv);
|
|
|
|
let mut cipher_send = CipherSend::new(cipher, secret_send);
|
|
|
|
|
|
|
|
Self {
|
|
|
|
id: ID::Handshake,
|
|
|
|
hkdf,
|
|
|
|
cipher_recv,
|
|
|
|
cipher_send,
|
|
|
|
}
|
2023-02-25 21:25:52 +00:00
|
|
|
}
|
|
|
|
}
|