matter_controller/state.rs
1//! In-memory controller state. These types are the *persistable* record;
2//! live signers are reconstructed from the stored PKCS#8 keys on demand.
3
4use std::sync::Arc;
5
6use matter_cert::MatterCertificate;
7use matter_commissioning::FabricRecord;
8use matter_crypto::{RingSigner, Signer};
9
10use crate::error::Error;
11
12/// The persisted material for one group key set.
13///
14/// Stored inside [`FabricEntry::group_keys`] and round-tripped through the
15/// TLV snapshot at context tags t6 (key-set array) and t7 (outbound counter).
16/// This carries only what the controller needs to *send* group-encrypted
17/// messages; a full `GroupKeySet` cluster record lives in the device, not
18/// here.
19///
20/// This is a `pub` type because callers that program group keys (e.g.
21/// higher-level fabric-management APIs) need to construct and inspect it.
22///
23/// `#[non_exhaustive]`: persisted record whose shape may grow (e.g. key policy
24/// epoch, security level flags); marking it keeps such additions non-breaking.
25/// Construct via [`GroupKeySetConfig::new`].
26#[derive(Debug, Clone, PartialEq, Eq)]
27#[non_exhaustive]
28pub struct GroupKeySetConfig {
29 /// Group Key Set ID (`GrpKeySetID`, 16-bit, spec §4.15).
30 pub key_set_id: u16,
31 /// 16-byte epoch key (`EpochKey0` / `EpochKey1` / `EpochKey2` per policy).
32 pub epoch_key: [u8; 16],
33 /// Epoch key start time in Matter epoch seconds (0 = unset / pre-operational).
34 pub epoch_start_time: u64,
35}
36
37impl GroupKeySetConfig {
38 /// Construct a [`GroupKeySetConfig`].
39 ///
40 /// Required because the struct is `#[non_exhaustive]`; external callers
41 /// cannot use struct-literal syntax.
42 #[must_use]
43 pub fn new(key_set_id: u16, epoch_key: [u8; 16], epoch_start_time: u64) -> Self {
44 Self {
45 key_set_id,
46 epoch_key,
47 epoch_start_time,
48 }
49 }
50}
51
52/// A per-fabric intermediate CA (ICAC): the issued ICAC certificate plus the
53/// PKCS#8 private key that signs NOCs under it. `None` for a flat RCAC->NOC
54/// fabric (the default).
55#[derive(Clone)]
56#[non_exhaustive]
57pub struct IcacIdentity {
58 /// The RCAC-signed ICAC certificate.
59 pub cert: MatterCertificate,
60 /// The ICAC signing key, PKCS#8 DER. `pub(crate)`: raw private-key material
61 /// is never exposed on the public surface (persisted via `snapshot`).
62 pub(crate) pkcs8: Vec<u8>,
63}
64
65impl std::fmt::Debug for IcacIdentity {
66 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
67 f.debug_struct("IcacIdentity")
68 .field("cert", &"<MatterCertificate>")
69 .field("pkcs8", &"<redacted PKCS#8>")
70 .finish()
71 }
72}
73
74/// A device commissioned onto a fabric.
75///
76/// `#[non_exhaustive]`: persisted record whose shape may grow (e.g. CAT tags,
77/// typed resumption state); marking it keeps such additions non-breaking. Only
78/// constructed inside `matter-controller`.
79#[derive(Clone)]
80#[non_exhaustive]
81pub struct DeviceEntry {
82 /// The device's operational node ID on this fabric.
83 pub node_id: u64,
84 /// The device's NOC public key (SEC1 uncompressed, `0x04 || X || Y`).
85 pub peer_noc_public_key: [u8; 65],
86 /// Cached CASE resumption record (opaque bytes).
87 pub resumption_record: Option<Vec<u8>>,
88 /// Last operational address we reached the device at (a discovery hint).
89 pub last_known_addr: Option<String>,
90 /// Device vendor id (`BasicInformation` 0x0028/0x0002), captured
91 /// best-effort after commissioning. `None` if the read did not complete.
92 pub vendor_id: Option<u16>,
93 /// Device product id (`BasicInformation` 0x0028/0x0004), captured
94 /// best-effort after commissioning. `None` if the read did not complete.
95 pub product_id: Option<u16>,
96 /// Caller-supplied opaque label attached at `commission()` time. `None`
97 /// if the caller passed none.
98 pub label: Option<String>,
99}
100
101impl std::fmt::Debug for DeviceEntry {
102 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
103 // `resumption_record` is a serialized CASE ResumptionRecord and carries
104 // a session shared secret — redact it (matches the redaction discipline
105 // in `FabricEntry`/`CommissionerIdentity`). `peer_noc_public_key`,
106 // `last_known_addr`, `vendor_id`, `product_id`, and `label` are not
107 // secret.
108 f.debug_struct("DeviceEntry")
109 .field("node_id", &self.node_id)
110 .field("peer_noc_public_key", &self.peer_noc_public_key)
111 .field(
112 "resumption_record",
113 &self
114 .resumption_record
115 .as_ref()
116 .map(|_| "<redacted; CASE secret>"),
117 )
118 .field("last_known_addr", &self.last_known_addr)
119 .field("vendor_id", &self.vendor_id)
120 .field("product_id", &self.product_id)
121 .field("label", &self.label)
122 .finish()
123 }
124}
125
126/// The controller's own stable operational identity on a fabric.
127///
128/// Minted **once** when the fabric is created (see
129/// [`MatterController::create_fabric`](crate::controller::MatterController::create_fabric))
130/// and reused for every CASE handshake, replacing the earlier per-call NOC
131/// minting.
132///
133/// `#[non_exhaustive]`: persisted identity record that may grow; marking it
134/// keeps additions non-breaking. Only constructed inside `matter-controller`.
135#[derive(Clone)]
136#[non_exhaustive]
137pub struct CommissionerIdentity {
138 /// The commissioner's stable node ID on this fabric.
139 pub node_id: u64,
140 /// The commissioner's operational private key, PKCS#8 DER. `pub(crate)`:
141 /// raw private-key material is never exposed on the public surface.
142 pub(crate) operational_pkcs8: Vec<u8>,
143 /// The commissioner's NOC, signed by the fabric RCAC.
144 pub noc: MatterCertificate,
145}
146
147impl std::fmt::Debug for CommissionerIdentity {
148 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
149 f.debug_struct("CommissionerIdentity")
150 .field("node_id", &self.node_id)
151 .field("operational_pkcs8", &"<redacted PKCS#8>")
152 .field("noc", &"<MatterCertificate>")
153 .finish()
154 }
155}
156
157/// One fabric the controller administers: trust root, IPK, the
158/// commissioner identity, and the devices commissioned onto it.
159///
160/// `#[non_exhaustive]`: persisted record that may gain fields (e.g. an ICAC
161/// tier, fabric label); marking it keeps such additions non-breaking. Only
162/// constructed inside `matter-controller`.
163#[derive(Clone)]
164#[non_exhaustive]
165pub struct FabricEntry {
166 /// Matter fabric identifier.
167 pub fabric_id: u64,
168 /// 16-byte Identity Protection Key for this fabric.
169 pub ipk: [u8; 16],
170 /// Self-signed root (RCAC) certificate.
171 pub rcac_cert: MatterCertificate,
172 /// The RCAC root signing key, PKCS#8 DER. `pub(crate)`: raw private-key
173 /// material is never exposed on the public surface.
174 pub(crate) rcac_pkcs8: Vec<u8>,
175 /// The controller's stable identity on this fabric.
176 pub commissioner: CommissionerIdentity,
177 /// Devices commissioned onto this fabric.
178 pub devices: Vec<DeviceEntry>,
179 /// Group key sets programmed on this fabric (persisted for outbound group
180 /// message encryption). Empty until the controller programs group keys.
181 pub group_keys: Vec<GroupKeySetConfig>,
182 /// The outbound group message counter for this fabric.
183 ///
184 /// Monotonically incremented each time the controller sends a group
185 /// message. Persisted so the counter survives restarts (spec §4.6.7
186 /// prohibits counter reuse across sessions / resets).
187 pub outbound_group_counter: u32,
188 /// ICD (Intermittently Connected Device) client registrations on this
189 /// fabric. Each holds the shared key + counter floor the check-in listener
190 /// uses to verify a registered device's Check-In messages. Empty until the
191 /// controller calls `register_icd_client`.
192 pub icd_clients: Vec<crate::icd::IcdRegistration>,
193 /// Optional per-fabric intermediate CA. `None` for a flat RCAC->NOC
194 /// fabric (the default); `Some` once the fabric adopts an ICAC tier.
195 pub icac: Option<IcacIdentity>,
196}
197
198impl std::fmt::Debug for FabricEntry {
199 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
200 f.debug_struct("FabricEntry")
201 .field("fabric_id", &self.fabric_id)
202 .field("ipk", &"<redacted; 16 bytes>")
203 .field("rcac_cert", &"<MatterCertificate>")
204 .field("rcac_pkcs8", &"<redacted PKCS#8>")
205 .field("commissioner", &self.commissioner)
206 .field("devices", &self.devices)
207 .field(
208 "group_keys",
209 &format!("<{} key sets>", self.group_keys.len()),
210 )
211 .field("outbound_group_counter", &self.outbound_group_counter)
212 .field(
213 "icd_clients",
214 &format!("<{} registrations>", self.icd_clients.len()),
215 )
216 .field("icac", &self.icac)
217 .finish()
218 }
219}
220
221impl FabricEntry {
222 /// Reconstruct the RCAC root signer from the stored PKCS#8 key.
223 ///
224 /// # Errors
225 ///
226 /// Returns [`Error::Signer`] if the stored key is not valid PKCS#8.
227 pub(crate) fn rcac_signer(&self) -> Result<RingSigner, Error> {
228 RingSigner::from_pkcs8(&self.rcac_pkcs8).map_err(|e| Error::Signer(e.to_string()))
229 }
230
231 /// Reconstruct the commissioner operational signer from PKCS#8.
232 ///
233 /// # Errors
234 ///
235 /// Returns [`Error::Signer`] if the stored key is not valid PKCS#8.
236 pub(crate) fn commissioner_signer(&self) -> Result<RingSigner, Error> {
237 RingSigner::from_pkcs8(&self.commissioner.operational_pkcs8)
238 .map_err(|e| Error::Signer(e.to_string()))
239 }
240
241 /// Build a [`FabricRecord`] view (used by later sub-phases for NOC
242 /// issuance and CASE). Reconstructs the RCAC signer from PKCS#8.
243 ///
244 /// # Errors
245 ///
246 /// Returns [`Error::Signer`] if the RCAC key cannot be reconstructed.
247 pub(crate) fn to_fabric_record(&self) -> Result<FabricRecord, Error> {
248 let signer = self.rcac_signer()?;
249 let root_public_key = signer.public_key().clone();
250 // Reconstruct the ICAC signer/cert when this fabric has an ICAC
251 // tier, so a restored fabric keeps signing NOCs (and CASE
252 // credentials, via `FabricRecord.icac_cert`) under the ICAC —
253 // matching what `create_fabric` sets up live.
254 let (icac_signer, icac_cert) = match &self.icac {
255 Some(icac) => {
256 let signer = RingSigner::from_pkcs8(&icac.pkcs8)
257 .map_err(|e| Error::Signer(e.to_string()))?;
258 (
259 Some(Arc::new(signer) as Arc<dyn Signer>),
260 Some(icac.cert.clone()),
261 )
262 }
263 None => (None, None),
264 };
265 Ok(FabricRecord {
266 fabric_id: self.fabric_id,
267 root_public_key,
268 root_signer: Arc::new(signer) as Arc<dyn Signer>,
269 root_cert: self.rcac_cert.clone(),
270 icac_signer,
271 icac_cert,
272 identity_protection_key: self.ipk,
273 })
274 }
275}
276
277/// The full controller state: all administered fabrics.
278///
279/// `#[non_exhaustive]`: the persisted top-level record may gain fields (e.g.
280/// schema version, controller-wide settings); marking it keeps such additions
281/// non-breaking. Construct via [`ControllerState::new`] or
282/// [`ControllerState::default`] from outside this crate.
283#[derive(Debug, Clone, Default)]
284#[non_exhaustive]
285pub struct ControllerState {
286 /// Fabrics this controller administers.
287 pub fabrics: Vec<FabricEntry>,
288}
289
290impl ControllerState {
291 /// Construct controller state from a list of administered fabrics.
292 ///
293 /// Supported construction path now that [`ControllerState`] is
294 /// `#[non_exhaustive]`; the `fabrics` field stays directly accessible.
295 #[must_use]
296 pub fn new(fabrics: Vec<FabricEntry>) -> Self {
297 Self { fabrics }
298 }
299}
300
301#[cfg(test)]
302#[allow(clippy::unwrap_used, clippy::expect_used)]
303mod tests {
304 use super::*;
305
306 #[test]
307 fn controller_state_new_builds_from_fabrics() {
308 // `ControllerState` is `#[non_exhaustive]`; `new` is the supported
309 // construction path. An empty list yields no fabrics.
310 let state = ControllerState::new(Vec::new());
311 assert!(state.fabrics.is_empty());
312 }
313
314 #[test]
315 fn reconstructed_signer_signs_and_verifies() {
316 // A standalone RingSigner round-trips through PKCS#8 and signs.
317 let (signer, pkcs8) = RingSigner::generate().expect("generate");
318 let entry_key = pkcs8.clone();
319 let reloaded = RingSigner::from_pkcs8(&entry_key).expect("reload");
320 // Both signers share the same public key.
321 assert_eq!(
322 signer.public_key().as_bytes(),
323 reloaded.public_key().as_bytes()
324 );
325 // The reloaded signer produces a verifiable signature.
326 let msg = b"controller identity";
327 let sig_bytes = reloaded.sign_p256_sha256(msg).expect("sign");
328 // `PublicKey::verify` takes a `&matter_cert::Signature`, not a raw `[u8; 64]`.
329 let sig = matter_cert::Signature::new(sig_bytes);
330 reloaded
331 .public_key()
332 .verify(msg, &sig)
333 .expect("reloaded signature verifies");
334 }
335}