rs-matter-node-core 0.5.0

Native Rust implementation of the Matter (Smart-Home) ecosystem - what the node is built on: fabrics, ACL, fail-safe
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/*
 *
 *    Copyright (c) 2026 Project CHIP Authors
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

//! Commissioning handover: suspend a half-done commissioning on one node and
//! resume it on another.

use core::fmt::{self, Debug};
use core::marker::PhantomData;

use crate::acl::AclEntry;
use crate::basic_info::RegulatoryLocationType;
use crate::crypto::{CanonAeadKey, CanonPkcSecretKey};
use crate::error::{Error, ErrorCode};
use crate::fabric::Fabric;
use crate::networks::{Networks, NetworksError, WirelessCreds};
use crate::tlv::{
    EitherIter, FromTLV, OctetStr, Octets, TLVArray, TLVArrayOrSlice, TLVElement, TLVTag, TLVWrite,
    TlvError, TlvErrorCode, ToTLV, Utf8Str, TLV,
};

/// The envelope version.
pub const HANDOVER_VERSION: u8 = 1;

/// A borrowed view of one staged network, convertible from and to
/// [`WirelessCreds`].
pub trait HandoverNetwork: Copy {
    /// This view type at lifetime `'c`.
    type Borrowed<'c>: HandoverNetwork + FromTLV<'c> + ToTLV;

    /// The view of `creds`, if of this kind.
    fn from_creds<'c>(creds: &WirelessCreds<'c>) -> Option<Self::Borrowed<'c>>;

    /// The credentials of this view.
    fn creds(&self) -> WirelessCreds<'_>;
}

/// A staged Wi-Fi network; same tags as the `AddOrUpdateWiFiNetwork` arguments.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, FromTLV, ToTLV)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[tlvargs(lifetime = "'a")]
pub struct HandoverWifi<'a> {
    pub ssid: OctetStr<'a>,
    pub credentials: OctetStr<'a>,
}

impl HandoverNetwork for HandoverWifi<'_> {
    type Borrowed<'c> = HandoverWifi<'c>;

    fn from_creds<'c>(creds: &WirelessCreds<'c>) -> Option<Self::Borrowed<'c>> {
        match creds {
            WirelessCreds::Wifi { ssid, pass } => Some(HandoverWifi {
                ssid: Octets(ssid),
                credentials: Octets(pass),
            }),
            WirelessCreds::Thread { .. } => None,
        }
    }

    fn creds(&self) -> WirelessCreds<'_> {
        WirelessCreds::Wifi {
            ssid: self.ssid.0,
            pass: self.credentials.0,
        }
    }
}

/// A staged Thread network; same tag as the `AddOrUpdateThreadNetwork` argument.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, FromTLV, ToTLV)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[tlvargs(lifetime = "'a")]
pub struct HandoverThread<'a> {
    pub operational_dataset: OctetStr<'a>,
}

impl HandoverNetwork for HandoverThread<'_> {
    type Borrowed<'c> = HandoverThread<'c>;

    fn from_creds<'c>(creds: &WirelessCreds<'c>) -> Option<Self::Borrowed<'c>> {
        match creds {
            WirelessCreds::Thread { dataset_tlv } => Some(HandoverThread {
                operational_dataset: Octets(dataset_tlv),
            }),
            WirelessCreds::Wifi { .. } => None,
        }
    }

    fn creds(&self) -> WirelessCreds<'_> {
        WirelessCreds::Thread {
            dataset_tlv: self.operational_dataset.0,
        }
    }
}

/// The staged networks of kind `T` ([`HandoverWifi`] or [`HandoverThread`]),
/// serialized as a TLV array of `T`.
///
/// Never materialized: read lazily from the TLV, a slice, or the live
/// [`Networks`] store, whose credentials are only reachable via callbacks.
/// Hence [`for_each`](Self::for_each) instead of an iterator, and hence
/// `tlv_iter` errors with `InvalidState` for a store-backed instance
/// (`to_tlv` always works).
pub struct HandoverNetworks<'a, T> {
    source: NetworksSource<'a, T>,
    _kind: PhantomData<fn() -> T>,
}

enum NetworksSource<'a, T> {
    Tlv(TLVElement<'a>),
    Slice(&'a [T]),
    Store(&'a dyn Networks),
}

impl<'a, T> HandoverNetworks<'a, T>
where
    T: HandoverNetwork,
{
    /// The networks in `slice`.
    pub const fn from_slice(slice: &'a [T]) -> Self {
        Self {
            source: NetworksSource::Slice(slice),
            _kind: PhantomData,
        }
    }

    /// The networks of kind `T` in the store.
    pub const fn from_store(networks: &'a dyn Networks) -> Self {
        Self {
            source: NetworksSource::Store(networks),
            _kind: PhantomData,
        }
    }

    /// Call `f` with each network's credentials, in order.
    pub fn for_each(
        &self,
        f: &mut dyn FnMut(&WirelessCreds<'_>) -> Result<(), Error>,
    ) -> Result<(), Error> {
        match &self.source {
            NetworksSource::Tlv(element) => {
                for network in TLVArray::<T::Borrowed<'a>>::new(element.clone())?.iter() {
                    f(&network?.creds())?;
                }

                Ok(())
            }
            NetworksSource::Slice(slice) => {
                for network in *slice {
                    f(&network.creds())?;
                }

                Ok(())
            }
            NetworksSource::Store(networks) => networks.networks(&mut |network_id| {
                networks
                    .creds(network_id, &mut |creds| {
                        if T::from_creds(creds).is_some() {
                            f(creds)?;
                        }

                        Ok(())
                    })
                    .map(|_| ())
                    .map_err(networks_error)
            }),
        }
    }

    /// Whether there are no networks.
    pub fn is_empty(&self) -> Result<bool, Error> {
        let mut empty = true;

        self.for_each(&mut |_| {
            empty = false;
            Ok(())
        })?;

        Ok(empty)
    }
}

impl<'a, T> FromTLV<'a> for HandoverNetworks<'a, T>
where
    T: HandoverNetwork + 'a,
{
    fn from_tlv(element: &TLVElement<'a>) -> Result<Self, TlvError> {
        Ok(Self {
            source: NetworksSource::Tlv(
                TLVArray::<T::Borrowed<'a>>::new(element.clone())?
                    .element()
                    .clone(),
            ),
            _kind: PhantomData,
        })
    }
}

impl<'a, T> ToTLV for HandoverNetworks<'a, T>
where
    T: HandoverNetwork + ToTLV,
{
    fn to_tlv<W: TLVWrite>(&self, tag: &TLVTag, mut tw: W) -> Result<(), TlvError> {
        tw.start_array(tag)?;

        // `for_each` reports the shared `Error`, so the `TlvError` of the inner
        // `to_tlv` is carried out of the closure rather than flattened - it can be
        // `NoSpace`, which the caller needs in order to chunk.
        let mut tlv_err = None;

        self.for_each(&mut |creds| {
            // `for_each` only yields networks of kind `T`
            let network = unwrap!(T::from_creds(creds));

            if let Err(err) = network.to_tlv(&TLVTag::Anonymous, &mut tw) {
                tlv_err = Some(err.code());
                Err(err)?;
            }

            Ok(())
        })
        .map_err(|err| {
            TlvError::new(tlv_err.unwrap_or_else(|| {
                // Reading the networks out of the store failed. There is no TLV
                // code for that, so log the cause before flattening it away.
                error!("Cannot read the networks to serialize: {}", err);
                TlvErrorCode::NotFound
            }))
        })?;

        tw.end_container()
    }

    fn tlv_iter(&self, tag: TLVTag) -> impl Iterator<Item = Result<TLV<'_>, TlvError>> {
        match &self.source {
            NetworksSource::Tlv(element) => {
                EitherIter::First(EitherIter::First(element.tlv_iter(tag)))
            }
            NetworksSource::Slice(slice) => {
                EitherIter::First(EitherIter::Second(slice.tlv_iter(tag)))
            }
            // Callbacks cannot drive an iterator; use `to_tlv`
            NetworksSource::Store(_) => {
                error!("A store-backed `HandoverNetworks` cannot be iterated; use `to_tlv`");

                EitherIter::Second(core::iter::once(Err(TlvErrorCode::NotFound.into())))
            }
        }
    }
}

impl<T> Debug for HandoverNetworks<'_, T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self.source {
            NetworksSource::Tlv(element) => f.debug_tuple("Tlv").field(element).finish(),
            NetworksSource::Slice(slice) => f.debug_tuple("Slice").field(&slice.len()).finish(),
            NetworksSource::Store(_) => f.debug_tuple("Store").finish(),
        }
    }
}

#[cfg(feature = "defmt")]
impl<T> defmt::Format for HandoverNetworks<'_, T> {
    fn format(&self, f: defmt::Formatter<'_>) {
        match &self.source {
            NetworksSource::Tlv(element) => defmt::write!(f, "Tlv({})", element),
            NetworksSource::Slice(slice) => defmt::write!(f, "Slice({})", slice.len()),
            NetworksSource::Store(_) => defmt::write!(f, "Store"),
        }
    }
}

/// The regulatory configuration, as set by `SetRegulatoryConfig`.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, FromTLV, ToTLV)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[tlvargs(lifetime = "'a")]
pub struct HandoverRegulatory<'a> {
    /// Two-letter ISO 3166-1 country code.
    pub country_code: Utf8Str<'a>,
    pub location_type: RegulatoryLocationType,
}

/// The commissioning handover envelope.
///
/// Needed for NFC (NTL) commissioning, where phase 1 - PASE, `ArmFailSafe`,
/// `AddNOC`, the ACL writes, `AddOrUpdate*Network` - runs on the NFC subsystem
/// with the main CPU off, and phase 2 - operational discovery, CASE,
/// `CommissioningComplete` - runs on the host once it boots. The outcome of
/// phase 1 travels between the two as a [`CommissioningHandover`]: a
/// versioned TLV envelope, mostly made of spec-defined TLV, so a vendor NFC
/// stack can produce it without `rs-matter`.
///
/// # Security
///
/// The envelope carries the node's operational private key, the IPK and the
/// network credentials. The channel it travels over is the security boundary.
#[derive(Debug, FromTLV, ToTLV)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[tlvargs(lifetime = "'a")]
pub struct CommissioningHandover<'a> {
    pub version: u8,
    pub root_ca: OctetStr<'a>,
    pub icac: Option<OctetStr<'a>>,
    pub noc: OctetStr<'a>,
    pub secret_key: CanonPkcSecretKey,
    pub ipk: CanonAeadKey,
    pub admin_vendor_id: u16,
    pub label: Utf8Str<'a>,
    /// `FabricIndex` is ignored on import.
    pub acl: TLVArrayOrSlice<'a, AclEntry>,
    pub wifi_networks: HandoverNetworks<'a, HandoverWifi<'a>>,
    pub thread_networks: HandoverNetworks<'a, HandoverThread<'a>>,
    pub regulatory: Option<HandoverRegulatory<'a>>,
    pub fail_safe_remaining_secs: u16,
    pub breadcrumb: u64,
}

impl CommissioningHandover<'_> {
    /// Structural validation: version, non-empty ACL, well-formed regulatory
    /// config. Certificates and keys are checked on import.
    pub fn validate(&self) -> Result<(), Error> {
        if self.version != HANDOVER_VERSION {
            error!(
                "Unsupported commissioning handover version {} (expected {})",
                self.version, HANDOVER_VERSION
            );
            Err(ErrorCode::InvalidData)?;
        }

        if self.acl.iter()?.next().is_none() {
            error!("Commissioning handover carries no ACL entries");
            Err(ErrorCode::InvalidData)?;
        }

        if let Some(regulatory) = &self.regulatory {
            if regulatory.country_code.len() != 2 {
                error!("Commissioning handover carries an invalid country code");
                Err(ErrorCode::InvalidData)?;
            }
        }

        Ok(())
    }

    /// Build the envelope for the pending `fabric` and hand it to `f`.
    pub fn with_pending<N, F, R>(
        fabric: &Fabric,
        networks: N,
        regulatory: Option<HandoverRegulatory<'_>>,
        fail_safe_remaining_secs: u16,
        breadcrumb: u64,
        f: F,
    ) -> Result<R, Error>
    where
        N: Networks,
        F: FnOnce(&CommissioningHandover<'_>) -> Result<R, Error>,
    {
        let handover = CommissioningHandover {
            version: HANDOVER_VERSION,
            root_ca: Octets(fabric.root_ca()),
            icac: (!fabric.icac().is_empty()).then(|| Octets(fabric.icac())),
            noc: Octets(fabric.noc()),
            secret_key: CanonPkcSecretKey::new_from_ref(fabric.secret_key()),
            ipk: CanonAeadKey::new_from_ref(fabric.ipk().epoch_key()),
            admin_vendor_id: fabric.vendor_id(),
            label: fabric.label(),
            acl: TLVArrayOrSlice::new_slice(fabric.acl()),
            wifi_networks: HandoverNetworks::from_store(&networks),
            thread_networks: HandoverNetworks::from_store(&networks),
            regulatory,
            fail_safe_remaining_secs,
            breadcrumb,
        };

        f(&handover)
    }
}

/// Stage the networks of `handover` into `networks`, as `AddOrUpdate*Network`
/// would.
pub fn stage_networks<N>(handover: &CommissioningHandover<'_>, mut networks: N) -> Result<(), Error>
where
    N: Networks,
{
    let mut staged = false;

    let mut stage = |creds: &WirelessCreds<'_>| {
        networks.add_or_update(creds).map_err(networks_error)?;
        staged = true;

        Ok(())
    };

    handover.wifi_networks.for_each(&mut stage)?;
    handover.thread_networks.for_each(&mut stage)?;

    // No `ConnectNetwork` will follow: the node must connect by itself,
    // so hand the store to the connectivity manager right away
    if staged {
        networks.set_managed(true)?;
    }

    Ok(())
}

fn networks_error(err: NetworksError) -> Error {
    match err {
        NetworksError::Other(err) => err,
        NetworksError::NetworkIdNotFound => ErrorCode::NotFound.into(),
        NetworksError::DuplicateNetworkId
        | NetworksError::OutOfRange
        | NetworksError::BoundsExceeded => ErrorCode::InvalidData.into(),
    }
}

#[cfg(test)]
#[cfg_attr(coverage_nightly, coverage(off))]
mod tests {
    use super::*;

    use crate::acl::{AuthMode, Target};
    use crate::dm::Privilege;
    use crate::utils::storage::Vec;
    use crate::utils::storage::WriteBuf;

    fn collect_acl<const N: usize>(acl: &TLVArrayOrSlice<'_, AclEntry>) -> Vec<AclEntry, N> {
        acl.iter().unwrap().map(|item| item.unwrap()).collect()
    }

    /// The network IDs of all networks, in order.
    fn collect_network_ids<T: HandoverNetwork, const N: usize>(
        networks: &HandoverNetworks<'_, T>,
    ) -> Vec<Vec<u8, 64>, N> {
        let mut collected = Vec::new();

        networks
            .for_each(&mut |creds| {
                collected
                    .push(Vec::from_slice(creds.id()?).unwrap())
                    .map_err(|_| ErrorCode::ResourceExhausted)?;
                Ok(())
            })
            .unwrap();

        collected
    }

    #[test]
    fn round_trip() {
        let mut admin = AclEntry::new(None, Privilege::ADMIN, AuthMode::Case);
        admin.add_subject(112233).unwrap();

        let mut operate = AclEntry::new(None, Privilege::OPERATE, AuthMode::Case);
        operate.add_subject(0xFFFF_FFFD_0001_0001).unwrap();
        operate
            .add_target(Target::new(Some(1), None, None))
            .unwrap();

        let acl = [admin, operate];
        let wifi = [HandoverWifi {
            ssid: Octets(b"home"),
            credentials: Octets(b"secret"),
        }];
        // Just an Extended PAN ID TLV
        let thread = [HandoverThread {
            operational_dataset: Octets(&[0x02, 0x08, 1, 2, 3, 4, 5, 6, 7, 8]),
        }];

        let handover = CommissioningHandover {
            version: HANDOVER_VERSION,
            root_ca: Octets(&[1, 2, 3]),
            icac: None,
            noc: Octets(&[4, 5, 6]),
            secret_key: CanonPkcSecretKey::from([7u8; 32]),
            ipk: CanonAeadKey::from([8u8; 16]),
            admin_vendor_id: 0xfff1,
            label: "lab",
            acl: TLVArrayOrSlice::new_slice(&acl),
            wifi_networks: HandoverNetworks::from_slice(&wifi),
            thread_networks: HandoverNetworks::from_slice(&thread),
            regulatory: Some(HandoverRegulatory {
                country_code: "BG",
                location_type: RegulatoryLocationType::IndoorOutdoor,
            }),
            fail_safe_remaining_secs: 42,
            breadcrumb: 7,
        };

        let mut buf = [0u8; 512];
        let mut wb = WriteBuf::new(&mut buf);
        handover.to_tlv(&TLVTag::Anonymous, &mut wb).unwrap();
        let len = wb.get_tail();

        let parsed = CommissioningHandover::from_tlv(&TLVElement::new(&buf[..len])).unwrap();
        parsed.validate().unwrap();

        assert_eq!(parsed.version, HANDOVER_VERSION);
        assert_eq!(parsed.root_ca.0, &[1, 2, 3]);
        assert!(parsed.icac.is_none());
        assert_eq!(parsed.noc.0, &[4, 5, 6]);
        assert_eq!(parsed.secret_key.access(), &[7u8; 32]);
        assert_eq!(parsed.ipk.access(), &[8u8; 16]);
        assert_eq!(parsed.admin_vendor_id, 0xfff1);
        assert_eq!(parsed.label, "lab");
        assert_eq!(parsed.fail_safe_remaining_secs, 42);
        assert_eq!(parsed.breadcrumb, 7);
        assert_eq!(parsed.regulatory.unwrap().country_code, "BG");
        assert_eq!(collect_acl::<4>(&parsed.acl).as_slice(), &acl);

        let wifi_ids = collect_network_ids::<_, 2>(&parsed.wifi_networks);
        assert_eq!(wifi_ids.len(), 1);
        assert_eq!(wifi_ids[0], b"home");

        let thread_ids = collect_network_ids::<_, 2>(&parsed.thread_networks);
        assert_eq!(thread_ids.len(), 1);
        assert_eq!(thread_ids[0], &[1, 2, 3, 4, 5, 6, 7, 8]);

        // `tlv_iter` must agree with `to_tlv`
        let mut buf2 = [0u8; 512];
        let mut wb2 = WriteBuf::new(&mut buf2);
        for tlv in handover.tlv_iter(TLVTag::Anonymous) {
            let tlv = tlv.unwrap();
            wb2.tlv(&tlv.tag, &tlv.value).unwrap();
        }
        let len2 = wb2.get_tail();
        assert_eq!(&buf[..len], &buf2[..len2]);
    }

    #[test]
    fn rejects_empty_acl_and_bad_version() {
        let handover = CommissioningHandover {
            version: HANDOVER_VERSION + 1,
            root_ca: Octets(&[]),
            icac: None,
            noc: Octets(&[]),
            secret_key: CanonPkcSecretKey::new(),
            ipk: CanonAeadKey::new(),
            admin_vendor_id: 0,
            label: "",
            acl: TLVArrayOrSlice::new_slice(&[]),
            wifi_networks: HandoverNetworks::from_slice(&[]),
            thread_networks: HandoverNetworks::from_slice(&[]),
            regulatory: None,
            fail_safe_remaining_secs: 0,
            breadcrumb: 0,
        };

        assert!(handover.validate().is_err());

        let handover = CommissioningHandover {
            version: HANDOVER_VERSION,
            ..handover
        };
        assert!(handover.validate().is_err());

        let admin = [AclEntry::new(None, Privilege::ADMIN, AuthMode::Case)];
        let handover = CommissioningHandover {
            acl: TLVArrayOrSlice::new_slice(&admin),
            ..handover
        };
        assert!(handover.validate().is_ok());
        assert!(handover.wifi_networks.is_empty().unwrap());
    }
}