use crate::clusters::generated::tls_certificate_management as spec_tls;
use crate::dm::spec::{Conforming, Optional};
use crate::dm::{Resolved, ResolvedCommand};
use crate::im::{ClusterHandler, ClusterId, CommandId, InteractionContext, Status, StatusIb};
use crate::msg::FabricIndex;
use crate::tlv::{Tag, TlvWriter, ToTlv};
use super::{
CERTIFICATE_MAX, ClientEntry, FINGERPRINT_FIELD_MAX, INTERMEDIATES_MAX, NONCE_LEN, RootEntry,
Slot, TlsCaid, TlsCcdid, TlsCertificateHooks, TlsTables, clear_client, fingerprint, visible,
};
use crate::clusters::Cluster;
pub use spec_tls::attribute::{
MAX_CLIENT_CERTIFICATES, MAX_ROOT_CERTIFICATES, PROVISIONED_CLIENT_CERTIFICATES,
PROVISIONED_ROOT_CERTIFICATES,
};
pub use spec_tls::command::{
CLIENT_CSR, CLIENT_CSR_RESPONSE, FIND_CLIENT_CERTIFICATE, FIND_CLIENT_CERTIFICATE_RESPONSE,
FIND_ROOT_CERTIFICATE, FIND_ROOT_CERTIFICATE_RESPONSE, LOOKUP_CLIENT_CERTIFICATE,
LOOKUP_CLIENT_CERTIFICATE_RESPONSE, LOOKUP_ROOT_CERTIFICATE, LOOKUP_ROOT_CERTIFICATE_RESPONSE,
PROVISION_CLIENT_CERTIFICATE, PROVISION_ROOT_CERTIFICATE, PROVISION_ROOT_CERTIFICATE_RESPONSE,
REMOVE_CLIENT_CERTIFICATE, REMOVE_ROOT_CERTIFICATE,
};
pub use spec_tls::{ID, PICS, REVISION};
#[derive(Debug)]
pub struct CertificateManagement<
'a,
H: TlsCertificateHooks,
const R: usize,
const C: usize,
const E: usize,
> {
tables: &'a TlsTables<R, C, E>,
hooks: &'a H,
}
impl<'a, H: TlsCertificateHooks, const R: usize, const C: usize, const E: usize>
CertificateManagement<'a, H, R, C, E>
{
#[must_use]
pub const fn new(tables: &'a TlsTables<R, C, E>, hooks: &'a H) -> Self {
Self { tables, hooks }
}
pub fn conforming(
feature_map: u32,
optional: &Optional<'_>,
) -> crate::error::Result<Conforming<4, 9, 6, 0>> {
Conforming::new(&spec_tls::CLUSTER, feature_map, optional)
}
#[must_use]
pub const fn tables(&self) -> &'a TlsTables<R, C, E> {
self.tables
}
}
fn find_root<const R: usize, const C: usize, const E: usize>(
tables: &TlsTables<R, C, E>,
fabric: FabricIndex,
caid: TlsCaid,
) -> Result<RootEntry, Status> {
tables
.roots()
.iter()
.find(|r| r.caid == caid && r.fabric_index == fabric)
.copied()
.ok_or(Status::NotFound)
}
fn find_client<const R: usize, const C: usize, const E: usize>(
tables: &TlsTables<R, C, E>,
fabric: FabricIndex,
ccdid: TlsCcdid,
) -> Result<ClientEntry, Status> {
tables
.clients()
.iter()
.find(|c| c.ccdid == ccdid && c.fabric_index == fabric)
.copied()
.ok_or(Status::NotFound)
}
impl<H: TlsCertificateHooks, const R: usize, const C: usize, const E: usize> ClusterHandler
for CertificateManagement<'_, H, R, C, E>
{
fn on_lifecycle(&self, event: crate::im::Lifecycle) {
if let crate::im::Lifecycle::FabricRemoved(fabric) = event {
self.tables.remove_fabric(fabric, self.hooks);
}
}
fn read(
&self,
resolved: &Resolved<'_>,
ctx: &InteractionContext<'_>,
w: &mut TlvWriter<'_>,
tag: Tag,
) -> Result<(), Status> {
let full = |r: crate::error::Result<()>| r.map_err(|_| Status::ResourceExhausted);
match resolved.attribute {
MAX_ROOT_CERTIFICATES => full(w.unsigned(tag, u64::from(self.tables.max_roots()))),
MAX_CLIENT_CERTIFICATES => full(w.unsigned(tag, u64::from(self.tables.max_clients()))),
PROVISIONED_ROOT_CERTIFICATES => {
let roots = self.tables.roots();
full(w.start_array(tag))?;
for root in roots.iter().filter(|r| visible(ctx, r.fabric_index)) {
full(w.start_structure(Tag::Anonymous))?;
full(w.unsigned(Tag::Context(0), u64::from(root.caid)))?;
if ctx.large_messages {
self.hooks.write_certificate(
Slot::Root {
fabric_index: root.fabric_index,
caid: root.caid,
},
w,
Tag::Context(1),
)?;
}
full(w.unsigned(Tag::Context(254), u64::from(root.fabric_index.0)))?;
full(w.end_container())?;
}
full(w.end_container())
}
PROVISIONED_CLIENT_CERTIFICATES => {
let clients = self.tables.clients();
full(w.start_array(tag))?;
for client in clients.iter().filter(|c| visible(ctx, c.fabric_index)) {
full(w.start_structure(Tag::Anonymous))?;
full(w.unsigned(Tag::Context(0), u64::from(client.ccdid)))?;
self.write_client_certificate(client, ctx.large_messages, w)?;
full(w.unsigned(Tag::Context(254), u64::from(client.fabric_index.0)))?;
full(w.end_container())?;
}
full(w.end_container())
}
_ => Err(Status::UnsupportedAttribute),
}
}
fn invoke(
&self,
resolved: &ResolvedCommand<'_>,
fields: Option<&[u8]>,
ctx: &InteractionContext<'_>,
w: &mut TlvWriter<'_>,
tag: Tag,
) -> Result<Option<CommandId>, StatusIb> {
let fabric = ctx
.fabric_index
.filter(|f| f.0 != 0)
.ok_or(StatusIb::from(Status::UnsupportedAccess))?;
let payload = fields.ok_or(StatusIb::from(Status::InvalidCommand))?;
match resolved.command.id {
PROVISION_ROOT_CERTIFICATE => self.provision_root(fabric, payload, w, tag),
FIND_ROOT_CERTIFICATE => self.find_roots(fabric, payload, w, tag),
LOOKUP_ROOT_CERTIFICATE => self.lookup_root(fabric, payload, w, tag),
REMOVE_ROOT_CERTIFICATE => self.remove_root(fabric, payload),
CLIENT_CSR => self.client_csr(fabric, payload, w, tag),
PROVISION_CLIENT_CERTIFICATE => self.provision_client(fabric, payload),
FIND_CLIENT_CERTIFICATE => self.find_clients(fabric, payload, w, tag),
LOOKUP_CLIENT_CERTIFICATE => self.lookup_client(fabric, payload, w, tag),
REMOVE_CLIENT_CERTIFICATE => self.remove_client(fabric, payload),
_ => Err(Status::UnsupportedCommand.into()),
}
}
}
impl<H: TlsCertificateHooks, const R: usize, const C: usize, const E: usize>
CertificateManagement<'_, H, R, C, E>
{
fn provision_root(
&self,
fabric: FabricIndex,
payload: &[u8],
w: &mut TlvWriter<'_>,
tag: Tag,
) -> Result<Option<CommandId>, StatusIb> {
let decoded: spec_tls::ProvisionRootCertificateFields<'_> =
crate::clusters::decode_fields(payload)?;
if decoded.certificate.len() > CERTIFICATE_MAX {
return Err(Status::ConstraintError.into());
}
if !self.tables.time_known() {
return Err(Status::InvalidInState.into());
}
if !self.hooks.is_valid_certificate(decoded.certificate) {
return Err(Status::DynamicConstraintError.into());
}
let print = fingerprint(decoded.certificate);
if self
.tables
.roots()
.iter()
.any(|r| r.fabric_index == fabric && r.fingerprint == print)
{
return Err(Status::AlreadyExists.into());
}
let caid = match decoded.caid.0 {
None => {
if self.tables.count_roots(fabric) >= usize::from(self.tables.max_roots()) {
return Err(Status::ResourceExhausted.into());
}
let taken =
|candidate: TlsCaid| self.tables.roots().iter().any(|r| r.caid == candidate);
let caid = TlsTables::<R, C, E>::allocate(&self.tables.next_caid, taken)
.ok_or(StatusIb::from(Status::ResourceExhausted))?;
self.hooks.save(
Slot::Root {
fabric_index: fabric,
caid,
},
decoded.certificate,
)?;
self.tables
.roots
.borrow_mut()
.push(RootEntry {
fabric_index: fabric,
caid,
fingerprint: print,
})
.map_err(|_| StatusIb::from(Status::ResourceExhausted))?;
caid
}
Some(caid) => {
let existing = find_root(self.tables, fabric, caid)?;
self.hooks.save(
Slot::Root {
fabric_index: fabric,
caid: existing.caid,
},
decoded.certificate,
)?;
if let Some(entry) = self
.tables
.roots
.borrow_mut()
.iter_mut()
.find(|r| r.caid == caid && r.fabric_index == fabric)
{
entry.fingerprint = print;
}
caid
}
};
spec_tls::ProvisionRootCertificateResponseFields { caid }
.to_tlv(w, tag)
.map_err(|_| StatusIb::from(Status::Failure))?;
Ok(Some(PROVISION_ROOT_CERTIFICATE_RESPONSE))
}
fn find_roots(
&self,
fabric: FabricIndex,
payload: &[u8],
w: &mut TlvWriter<'_>,
tag: Tag,
) -> Result<Option<CommandId>, StatusIb> {
let decoded: spec_tls::FindRootCertificateFields = crate::clusters::decode_fields(payload)?;
if self.tables.roots().is_empty() {
return Err(Status::NotFound.into());
}
let wanted = decoded.caid.0;
let matches: heapless::Vec<RootEntry, R> = self
.tables
.roots()
.iter()
.filter(|r| r.fabric_index == fabric && wanted.is_none_or(|caid| r.caid == caid))
.copied()
.collect();
if matches.is_empty() {
return Err(Status::NotFound.into());
}
let full = |r: crate::error::Result<()>| r.map_err(|_| StatusIb::from(Status::Failure));
full(w.start_structure(tag))?;
full(w.start_array(Tag::Context(0)))?;
for root in &matches {
full(w.start_structure(Tag::Anonymous))?;
full(w.unsigned(Tag::Context(0), u64::from(root.caid)))?;
self.hooks.write_certificate(
Slot::Root {
fabric_index: root.fabric_index,
caid: root.caid,
},
w,
Tag::Context(1),
)?;
full(w.unsigned(Tag::Context(254), u64::from(root.fabric_index.0)))?;
full(w.end_container())?;
}
full(w.end_container())?;
full(w.end_container())?;
Ok(Some(FIND_ROOT_CERTIFICATE_RESPONSE))
}
fn lookup_root(
&self,
fabric: FabricIndex,
payload: &[u8],
w: &mut TlvWriter<'_>,
tag: Tag,
) -> Result<Option<CommandId>, StatusIb> {
let decoded: spec_tls::LookupRootCertificateFields<'_> =
crate::clusters::decode_fields(payload)?;
if decoded.fingerprint.len() > FINGERPRINT_FIELD_MAX {
return Err(Status::ConstraintError.into());
}
if self.tables.roots().is_empty() {
return Err(Status::NotFound.into());
}
let caid = self
.tables
.roots()
.iter()
.find(|r| r.fabric_index == fabric && r.fingerprint.as_slice() == decoded.fingerprint)
.map(|r| r.caid)
.ok_or(StatusIb::from(Status::NotFound))?;
spec_tls::LookupRootCertificateResponseFields { caid }
.to_tlv(w, tag)
.map_err(|_| StatusIb::from(Status::Failure))?;
Ok(Some(LOOKUP_ROOT_CERTIFICATE_RESPONSE))
}
fn remove_root(
&self,
fabric: FabricIndex,
payload: &[u8],
) -> Result<Option<CommandId>, StatusIb> {
let decoded: spec_tls::RemoveRootCertificateFields =
crate::clusters::decode_fields(payload)?;
if self.tables.roots().is_empty() {
return Err(Status::NotFound.into());
}
let entry = find_root(self.tables, fabric, decoded.caid)?;
if self
.tables
.endpoints()
.iter()
.any(|e| e.caid == decoded.caid)
{
return Err(Status::InvalidInState.into());
}
self.hooks.clear(Slot::Root {
fabric_index: entry.fabric_index,
caid: entry.caid,
});
self.tables
.roots
.borrow_mut()
.retain(|r| !(r.caid == entry.caid && r.fabric_index == fabric));
Ok(None)
}
fn client_csr(
&self,
fabric: FabricIndex,
payload: &[u8],
w: &mut TlvWriter<'_>,
tag: Tag,
) -> Result<Option<CommandId>, StatusIb> {
let decoded: spec_tls::ClientCSRFields<'_> = crate::clusters::decode_fields(payload)?;
if decoded.nonce.len() != NONCE_LEN {
return Err(Status::ConstraintError.into());
}
let ccdid = match decoded.ccdid.0 {
None => {
if self.tables.count_clients(fabric) >= usize::from(self.tables.max_clients()) {
return Err(Status::ResourceExhausted.into());
}
let taken = |candidate: TlsCcdid| {
self.tables.clients().iter().any(|c| c.ccdid == candidate)
};
let ccdid = TlsTables::<R, C, E>::allocate(&self.tables.next_ccdid, taken)
.ok_or(StatusIb::from(Status::ResourceExhausted))?;
self.hooks.generate_key(ccdid)?;
self.tables
.clients
.borrow_mut()
.push(ClientEntry {
fabric_index: fabric,
ccdid,
fingerprint: None,
intermediates: 0,
})
.map_err(|_| StatusIb::from(Status::ResourceExhausted))?;
ccdid
}
Some(ccdid) => find_client(self.tables, fabric, ccdid)?.ccdid,
};
let full = |r: crate::error::Result<()>| r.map_err(|_| StatusIb::from(Status::Failure));
full(w.start_structure(tag))?;
full(w.unsigned(Tag::Context(0), u64::from(ccdid)))?;
self.hooks
.write_csr(ccdid, decoded.nonce, w, Tag::Context(1), Tag::Context(2))?;
full(w.end_container())?;
Ok(Some(CLIENT_CSR_RESPONSE))
}
fn provision_client(
&self,
fabric: FabricIndex,
payload: &[u8],
) -> Result<Option<CommandId>, StatusIb> {
let decoded: spec_tls::ProvisionClientCertificateFields<'_> =
crate::clusters::decode_fields(payload)?;
if decoded.client_certificate.len() > CERTIFICATE_MAX {
return Err(Status::ConstraintError.into());
}
if self.tables.clients().is_empty() {
return Err(Status::NotFound.into());
}
let print = fingerprint(decoded.client_certificate);
if self
.tables
.clients()
.iter()
.any(|c| c.fabric_index == fabric && c.fingerprint == Some(print))
{
return Err(Status::AlreadyExists.into());
}
let entry = find_client(self.tables, fabric, decoded.ccdid)?;
if !self.hooks.is_valid_certificate(decoded.client_certificate) {
return Err(Status::DynamicConstraintError.into());
}
if !self
.hooks
.key_matches(entry.ccdid, decoded.client_certificate)
{
return Err(Status::DynamicConstraintError.into());
}
let mut count = 0u8;
for intermediate in decoded.intermediate_certificates.iter() {
let der = intermediate.map_err(|_| StatusIb::from(Status::InvalidCommand))?;
if der.len() > CERTIFICATE_MAX || usize::from(count) >= INTERMEDIATES_MAX {
return Err(Status::ConstraintError.into());
}
if !self.hooks.is_valid_certificate(der) {
return Err(Status::DynamicConstraintError.into());
}
count = count.saturating_add(1);
}
clear_client(self.hooks, fabric, entry.ccdid, entry.intermediates);
self.hooks.save(
Slot::Client {
fabric_index: fabric,
ccdid: entry.ccdid,
},
decoded.client_certificate,
)?;
let mut index = 0u8;
for intermediate in decoded.intermediate_certificates.iter() {
let der = intermediate.map_err(|_| StatusIb::from(Status::InvalidCommand))?;
self.hooks.save(
Slot::Intermediate {
fabric_index: fabric,
ccdid: entry.ccdid,
index,
},
der,
)?;
index = index.saturating_add(1);
}
if let Some(stored) = self
.tables
.clients
.borrow_mut()
.iter_mut()
.find(|c| c.ccdid == entry.ccdid && c.fabric_index == fabric)
{
stored.fingerprint = Some(print);
stored.intermediates = count;
}
Ok(None)
}
fn find_clients(
&self,
fabric: FabricIndex,
payload: &[u8],
w: &mut TlvWriter<'_>,
tag: Tag,
) -> Result<Option<CommandId>, StatusIb> {
let decoded: spec_tls::FindClientCertificateFields =
crate::clusters::decode_fields(payload)?;
if self.tables.clients().is_empty() {
return Err(Status::NotFound.into());
}
let wanted = decoded.ccdid.0;
let matches: heapless::Vec<ClientEntry, C> = self
.tables
.clients()
.iter()
.filter(|c| c.fabric_index == fabric && wanted.is_none_or(|ccdid| c.ccdid == ccdid))
.copied()
.collect();
if matches.is_empty() {
return Err(Status::NotFound.into());
}
let full = |r: crate::error::Result<()>| r.map_err(|_| StatusIb::from(Status::Failure));
full(w.start_structure(tag))?;
full(w.start_array(Tag::Context(0)))?;
for client in &matches {
full(w.start_structure(Tag::Anonymous))?;
full(w.unsigned(Tag::Context(0), u64::from(client.ccdid)))?;
self.write_client_certificate(client, true, w)?;
full(w.unsigned(Tag::Context(254), u64::from(client.fabric_index.0)))?;
full(w.end_container())?;
}
full(w.end_container())?;
full(w.end_container())?;
Ok(Some(FIND_CLIENT_CERTIFICATE_RESPONSE))
}
fn lookup_client(
&self,
fabric: FabricIndex,
payload: &[u8],
w: &mut TlvWriter<'_>,
tag: Tag,
) -> Result<Option<CommandId>, StatusIb> {
let decoded: spec_tls::LookupClientCertificateFields<'_> =
crate::clusters::decode_fields(payload)?;
if decoded.fingerprint.len() > FINGERPRINT_FIELD_MAX {
return Err(Status::ConstraintError.into());
}
if self.tables.clients().is_empty() {
return Err(Status::NotFound.into());
}
let ccdid = self
.tables
.clients()
.iter()
.find(|c| {
c.fabric_index == fabric
&& c.fingerprint
.is_some_and(|print| print.as_slice() == decoded.fingerprint)
})
.map(|c| c.ccdid)
.ok_or(StatusIb::from(Status::NotFound))?;
spec_tls::LookupClientCertificateResponseFields { ccdid }
.to_tlv(w, tag)
.map_err(|_| StatusIb::from(Status::Failure))?;
Ok(Some(LOOKUP_CLIENT_CERTIFICATE_RESPONSE))
}
fn remove_client(
&self,
fabric: FabricIndex,
payload: &[u8],
) -> Result<Option<CommandId>, StatusIb> {
let decoded: spec_tls::RemoveClientCertificateFields =
crate::clusters::decode_fields(payload)?;
if self.tables.clients().is_empty() {
return Err(Status::NotFound.into());
}
let entry = find_client(self.tables, fabric, decoded.ccdid)?;
if self
.tables
.endpoints()
.iter()
.any(|e| e.ccdid == Some(decoded.ccdid))
{
return Err(Status::InvalidInState.into());
}
clear_client(self.hooks, fabric, entry.ccdid, entry.intermediates);
self.hooks.remove_key(entry.ccdid);
self.tables
.clients
.borrow_mut()
.retain(|c| !(c.ccdid == entry.ccdid && c.fabric_index == fabric));
Ok(None)
}
fn write_client_certificate(
&self,
client: &ClientEntry,
large: bool,
w: &mut TlvWriter<'_>,
) -> Result<(), Status> {
let full = |r: crate::error::Result<()>| r.map_err(|_| Status::ResourceExhausted);
match client.fingerprint {
None => full(w.null(Tag::Context(1)))?,
Some(_) if !large => {}
Some(_) => self.hooks.write_certificate(
Slot::Client {
fabric_index: client.fabric_index,
ccdid: client.ccdid,
},
w,
Tag::Context(1),
)?,
}
if !large && client.intermediates > 0 {
return Ok(());
}
full(w.start_array(Tag::Context(2)))?;
for index in 0..client.intermediates {
self.hooks.write_certificate(
Slot::Intermediate {
fabric_index: client.fabric_index,
ccdid: client.ccdid,
index,
},
w,
Tag::Anonymous,
)?;
}
full(w.end_container())
}
}
impl<H: TlsCertificateHooks, const R: usize, const C: usize, const E: usize> Cluster
for CertificateManagement<'_, H, R, C, E>
{
const ID: ClusterId = ID;
}