use std::collections::HashSet;
use lion_core::state::RevocationState;
use lion_core::{CapId, Capability, Kernel, PluginId, Right, Rights, SealedTag, SecurityLevel};
use crate::error::KernelError;
use crate::namespace::NamespaceMap;
const PRIVATE_ID_START: CapId = 1_u128 << 127;
pub struct CapabilityManager {
kernel: Kernel,
anchors: HashSet<CapId>,
next_anchor_id: CapId,
}
impl CapabilityManager {
pub fn with_key(key: [u8; 32]) -> Result<Self, KernelError> {
Ok(Self {
kernel: Kernel::with_key(key).map_err(|e| KernelError::Core(e.to_string()))?,
anchors: HashSet::new(),
next_anchor_id: CapId::MAX - 1,
})
}
pub fn register_actor(
&mut self,
plugin_id: PluginId,
level: SecurityLevel,
) -> Result<(), KernelError> {
if plugin_id == 0 {
return Err(KernelError::Core("actor id must be nonzero".into()));
}
self.kernel
.register_plugin(plugin_id, level, 0)
.map_err(|e| KernelError::Core(e.to_string()))
}
pub fn mint(
&mut self,
plugin_id: PluginId,
namespace: &str,
rights: Rights,
) -> Result<CapId, KernelError> {
self.require_actor(plugin_id)?;
if rights.is_empty() {
return Err(KernelError::DelegationFailed(
"rights cannot be empty".into(),
));
}
self.ensure_grant_id_available()?;
let anchor_id = self.next_anchor_id;
if !(PRIVATE_ID_START..CapId::MAX).contains(&anchor_id) {
return Err(KernelError::IdSpaceExhausted);
}
let anchor = Capability::new(
anchor_id,
plugin_id,
NamespaceMap::resource_id(namespace),
rights,
None,
self.kernel.state().kernel().key_epoch(),
SealedTag::from_bytes([0; 32]),
)
.map_err(|e| KernelError::Core(e.to_string()))?;
self.kernel
.insert_cap_raw(anchor, plugin_id)
.map_err(|e| KernelError::Core(e.to_string()))?;
self.anchors.insert(anchor_id);
self.next_anchor_id -= 1;
self.kernel
.delegate_cap(anchor_id, plugin_id, rights)
.map_err(|e| KernelError::DelegationFailed(e.to_string()))
}
pub fn bootstrap_root_capability(
&mut self,
plugin_id: PluginId,
namespace: &str,
rights: Rights,
) -> Result<CapId, KernelError> {
self.mint(plugin_id, namespace, rights)
}
pub fn compose(
&mut self,
parent_cap_id: CapId,
target_plugin: PluginId,
requested_rights: Rights,
) -> Result<CapId, KernelError> {
self.require_live_grant(parent_cap_id)?;
self.require_actor(target_plugin)?;
self.ensure_grant_id_available()?;
self.kernel
.delegate_cap(parent_cap_id, target_plugin, requested_rights)
.map_err(|e| KernelError::DelegationFailed(e.to_string()))
}
pub fn get_cap(&self, cap_id: CapId) -> Option<&Capability> {
(cap_id < PRIVATE_ID_START)
.then(|| self.kernel.get_cap(cap_id))
.flatten()
}
pub fn verify_seal(&self, cap: &Capability) -> bool {
cap.parent() != Some(CapId::MAX) && self.kernel.state().kernel().verify_cap_seal(cap)
}
pub fn is_live(&self, cap_id: CapId) -> bool {
chain_is_live(
self.kernel.state().kernel().revocation(),
&self.anchors,
cap_id,
)
}
pub fn validate(
&self,
plugin_id: PluginId,
cap_id: CapId,
namespace: &str,
required_right: Right,
) -> Result<(), KernelError> {
if !self.kernel.plugin_holds(plugin_id, cap_id) {
return Err(KernelError::InsufficientRights {
needed: format!("{required_right}"),
have: "no capability held".into(),
});
}
let cap = self.require_live_grant(cap_id)?;
if cap.holder() != plugin_id {
return Err(KernelError::InsufficientRights {
needed: format!("{required_right}"),
have: "capability belongs to another actor".into(),
});
}
if cap.target() != NamespaceMap::resource_id(namespace) {
return Err(KernelError::NamespaceDenied(namespace.to_string()));
}
if !cap.has_right(required_right) {
return Err(KernelError::InsufficientRights {
needed: format!("{required_right}"),
have: format!("{}", cap.rights()),
});
}
Ok(())
}
pub fn revoke(&mut self, cap_id: CapId) -> Result<(), KernelError> {
self.get_cap(cap_id)
.ok_or(KernelError::CapabilityRevoked(cap_id))?;
self.kernel
.revoke_cap(cap_id)
.map_err(|e| KernelError::Core(e.to_string()))
}
fn require_actor(&self, plugin_id: PluginId) -> Result<(), KernelError> {
if plugin_id == 0 || self.kernel.plugin_level(plugin_id).is_none() {
return Err(KernelError::UnknownActor(plugin_id.to_string()));
}
Ok(())
}
fn ensure_grant_id_available(&self) -> Result<(), KernelError> {
if self.kernel.state().kernel().next_cap_id() >= PRIVATE_ID_START {
return Err(KernelError::IdSpaceExhausted);
}
Ok(())
}
fn require_live_grant(&self, cap_id: CapId) -> Result<&Capability, KernelError> {
let cap = self
.get_cap(cap_id)
.ok_or(KernelError::CapabilityRevoked(cap_id))?;
if !self.kernel.plugin_holds(cap.holder(), cap_id) {
return Err(KernelError::UnknownActor(cap.holder().to_string()));
}
if !self.verify_seal(cap) {
return Err(KernelError::InvalidSeal(cap_id));
}
if !self.is_live(cap_id) {
return Err(KernelError::CapabilityRevoked(cap_id));
}
Ok(cap)
}
}
fn chain_is_live(state: &RevocationState, anchors: &HashSet<CapId>, cap_id: CapId) -> bool {
if cap_id >= PRIVATE_ID_START {
return false;
}
let mut current = cap_id;
for _ in 0..state.cap_count() {
let Some(cap) = state.get(current) else {
return false;
};
if !cap.is_valid() {
return false;
}
if anchors.contains(¤t) {
return current >= PRIVATE_ID_START && current != CapId::MAX && cap.parent().is_none();
}
if current >= PRIVATE_ID_START {
return false;
}
match cap.parent() {
Some(parent) => current = parent,
None => return false,
}
}
false
}
#[cfg(test)]
#[path = "capability_tests.rs"]
mod tests;