pub mod caps;
pub mod epochs;
pub mod keyring;
#[cfg(feature = "testing")]
pub mod virtual_sdk;
use crate::ids::{FenceId, LkIdentity, Slot};
use crate::keys::FrameIkm;
pub use caps::Caps;
pub use keyring::KeyRing;
#[derive(Debug)]
pub enum KeyEvent {
Install {
identity: LkIdentity,
slot: Slot,
ikm: FrameIkm,
},
Activate {
identity: LkIdentity,
slot: Slot,
ikm: FrameIkm,
},
Retire {
identity: LkIdentity,
slot: Slot,
filler: FrameIkm,
},
Forget {
identity: LkIdentity,
},
Fence {
id: FenceId,
},
Clear,
}
impl KeyEvent {
pub fn kind(&self) -> &'static str {
match self {
KeyEvent::Install { .. } => "install",
KeyEvent::Activate { .. } => "activate",
KeyEvent::Retire { .. } => "retire",
KeyEvent::Forget { .. } => "forget",
KeyEvent::Fence { .. } => "fence",
KeyEvent::Clear => "clear",
}
}
pub fn identity(&self) -> Option<&LkIdentity> {
match self {
KeyEvent::Install { identity, .. }
| KeyEvent::Activate { identity, .. }
| KeyEvent::Retire { identity, .. }
| KeyEvent::Forget { identity } => Some(identity),
KeyEvent::Fence { .. } | KeyEvent::Clear => None,
}
}
pub fn slot(&self) -> Option<Slot> {
match self {
KeyEvent::Install { slot, .. }
| KeyEvent::Activate { slot, .. }
| KeyEvent::Retire { slot, .. } => Some(*slot),
_ => None,
}
}
pub fn duplicate(&self) -> KeyEvent {
match self {
KeyEvent::Install {
identity,
slot,
ikm,
} => KeyEvent::Install {
identity: identity.clone(),
slot: *slot,
ikm: ikm.duplicate(),
},
KeyEvent::Activate {
identity,
slot,
ikm,
} => KeyEvent::Activate {
identity: identity.clone(),
slot: *slot,
ikm: ikm.duplicate(),
},
KeyEvent::Retire {
identity,
slot,
filler,
} => KeyEvent::Retire {
identity: identity.clone(),
slot: *slot,
filler: filler.duplicate(),
},
KeyEvent::Forget { identity } => KeyEvent::Forget {
identity: identity.clone(),
},
KeyEvent::Fence { id } => KeyEvent::Fence { id: *id },
KeyEvent::Clear => KeyEvent::Clear,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SinkAck {
Now,
Later(FenceId),
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum SinkError {
#[error("the SDK refused the key for slot {slot}")]
SdkRefused {
slot: u8,
},
#[error("sender key index stuck: expected {expected}, observed {observed}")]
SenderIndexStuck {
expected: u8,
observed: u8,
},
#[error("the engine downgraded a publication's encryption")]
EncryptionDowngraded,
#[error("libwebrtc debug logging would log keys; refused without the explicit opt-in")]
KeyLoggingRisk,
#[error("bridge not ready: {0}")]
NotReady(&'static str),
}
pub trait KeySink {
fn apply(&mut self, ev: KeyEvent) -> Result<SinkAck, SinkError>;
fn caps(&self) -> Caps;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SdkConfig {
pub shared_key: bool,
pub ratchet_window_size: i32,
pub failure_tolerance: i32,
pub key_ring_size: u8,
pub key_size_bits: u16,
pub kdf: &'static str,
pub salt: &'static [u8],
pub sif_trailer: &'static [u8],
pub publish_data: bool,
}
pub const SDK_CONFIG: SdkConfig = SdkConfig {
shared_key: false,
ratchet_window_size: 0,
failure_tolerance: -1,
key_ring_size: crate::ids::KEY_RING_SIZE,
key_size_bits: 128,
kdf: crate::config::KDF,
salt: crate::config::LK_SALT,
sif_trailer: b"",
publish_data: false,
};