use crate::cert::dn::DnAttributeKind;
use crate::error::{Error, ErrorCode, Result, bail};
use crate::msg::{CaseAuthenticatedTag, NodeId};
pub const ANCHOR_ICAC_OU: &str = "jf-anchor-icac";
pub const NODE_ID_MAX: u64 = 0xFFFF_FFEE_FFFF_FFFF;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct JointFabricTags {
pub administrator: CaseAuthenticatedTag,
pub anchor: CaseAuthenticatedTag,
}
impl JointFabricTags {
pub const fn new(version: u16) -> Result<Self> {
if version == 0 {
bail!(InvalidArgument)
}
Ok(Self {
administrator: CaseAuthenticatedTag::new(
CaseAuthenticatedTag::ADMINISTRATOR_IDENTIFIER,
version,
),
anchor: CaseAuthenticatedTag::new(CaseAuthenticatedTag::ANCHOR_IDENTIFIER, version),
})
}
#[must_use]
pub fn grants_administrator(&self, tag: CaseAuthenticatedTag) -> bool {
tag == self.administrator
}
#[must_use]
pub fn grants_anchor(&self, tag: CaseAuthenticatedTag) -> bool {
tag == self.anchor
}
pub const fn revoked(&self) -> Result<Self> {
let Some(next) = self.administrator.version().checked_add(1) else {
bail!(NoSpace)
};
Self::new(next)
}
}
#[must_use]
pub fn is_allocatable(node: NodeId) -> bool {
node.0 > 0 && node.0 < NODE_ID_MAX.saturating_add(1)
}
#[derive(Debug)]
pub struct Revocation<const N: usize> {
tags: JointFabricTags,
outstanding: heapless::Vec<NodeId, N>,
}
impl<const N: usize> Revocation<N> {
pub fn begin(
previous: &JointFabricTags,
nodes: impl IntoIterator<Item = NodeId>,
) -> Result<Self> {
let tags = previous.revoked()?;
let mut outstanding = heapless::Vec::new();
for node in nodes {
outstanding
.push(node)
.map_err(|_| Error::new(ErrorCode::NoSpace))?;
}
Ok(Self { tags, outstanding })
}
#[must_use]
pub const fn tags(&self) -> &JointFabricTags {
&self.tags
}
pub fn updated(&mut self, node: NodeId) -> bool {
let before = self.outstanding.len();
self.outstanding.retain(|n| *n != node);
self.outstanding.len() != before
}
#[must_use]
pub fn outstanding(&self) -> &[NodeId] {
&self.outstanding
}
#[must_use]
pub fn is_complete(&self) -> bool {
self.outstanding.is_empty()
}
}
pub fn is_anchor_icac<'a>(subject: impl IntoIterator<Item = (DnAttributeKind, &'a [u8])>) -> bool {
subject.into_iter().any(|(kind, value)| {
kind == DnAttributeKind::OrgUnitName && value == ANCHOR_ICAC_OU.as_bytes()
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_two_tags_use_the_reserved_identifiers() {
let tags = JointFabricTags::new(1).expect("version 1");
assert_eq!(tags.administrator.identifier(), 0xFFFF);
assert_eq!(tags.anchor.identifier(), 0xFFFE);
assert_eq!(tags.administrator.version(), 1);
assert!(tags.administrator.is_valid());
}
#[test]
fn version_zero_is_not_a_tag() {
assert!(JointFabricTags::new(0).is_err());
}
#[test]
fn revocation_leaves_the_old_version_behind() {
let before = JointFabricTags::new(1).expect("v1");
let after = before.revoked().expect("v2");
assert_eq!(after.administrator.version(), 2);
assert!(!after.grants_administrator(before.administrator));
assert!(after.grants_administrator(after.administrator));
assert!(!after.grants_anchor(before.anchor));
}
#[test]
fn a_fabric_that_has_run_out_of_versions_says_so() {
let last = JointFabricTags::new(u16::MAX).expect("v65535");
assert!(last.revoked().is_err());
}
#[test]
fn the_node_id_range_excludes_both_ends() {
assert!(!is_allocatable(NodeId(0)));
assert!(is_allocatable(NodeId(1)));
assert!(is_allocatable(NodeId(NODE_ID_MAX)));
assert!(!is_allocatable(NodeId(0xFFFF_FFEF_FFFF_FFFF)));
assert!(!is_allocatable(NodeId(u64::MAX)));
}
#[test]
fn the_anchor_marker_is_on_the_subject() {
assert!(is_anchor_icac([(
DnAttributeKind::OrgUnitName,
ANCHOR_ICAC_OU.as_bytes()
)]));
assert!(!is_anchor_icac([(
DnAttributeKind::OrgUnitName,
b"Acme Lighting".as_slice()
)]));
assert!(!is_anchor_icac([(
DnAttributeKind::CommonName,
ANCHOR_ICAC_OU.as_bytes()
)]));
}
#[test]
fn a_revocation_is_only_complete_when_every_node_has_moved() {
let before = JointFabricTags::new(1).expect("v1");
let nodes = [NodeId(1), NodeId(2), NodeId(3)];
let mut walk = Revocation::<8>::begin(&before, nodes).expect("begin");
assert_eq!(walk.tags().administrator.version(), 2);
assert!(!walk.is_complete());
assert!(walk.updated(NodeId(2)));
assert!(!walk.updated(NodeId(2)));
assert_eq!(walk.outstanding(), &[NodeId(1), NodeId(3)]);
assert!(!walk.is_complete());
assert!(walk.updated(NodeId(1)));
assert!(walk.updated(NodeId(3)));
assert!(walk.is_complete());
}
}