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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
//! `Commissioner` — the state-machine cursor.
#![forbid(unsafe_code)]
use std::sync::Arc;
use matter_cert::time::MatterTime;
use crate::attestation::PaaTrustStore;
use crate::noc::{FabricRecord, NocRng};
use crate::setup::SetupPayload;
use crate::state_machine::action::{Action, Expectation};
use crate::state_machine::error::CommissioningError;
use crate::state_machine::stage::Stage;
#[cfg(feature = "tracing")]
use tracing::instrument;
/// Fallback failsafe extension (seconds) applied at
/// `Stage::FailsafeBeforeNetworkEnable` when the device did not report a
/// usable `ConnectMaxTimeSeconds`. Chosen generously (Thread attach +
/// SRP registration is slower than Wi-Fi association); the C1 Wi-Fi path
/// adopts it harmlessly. Matter Core Spec §11.9.5.4 defines the attribute
/// but does not mandate a minimum, so a conservative default is safest.
pub(crate) const DEFAULT_CONNECT_MAX_TIME_SECONDS: u16 = 90;
/// Wi-Fi station credentials supplied to `AddOrUpdateWiFiNetwork`.
///
/// `ssid` must be 1–32 bytes (Matter Core Spec §11.9 constraints).
/// `credentials` must be 0–64 bytes — empty means open network, ≤64
/// bytes covers WPA2/WPA3 PSK lengths.
///
/// `Debug` is hand-written to redact `credentials` (renders only the
/// length). `Clone` is derived. Validation runs in
/// `Commissioner::new` (M6.5.2 Task 13).
#[derive(Clone, PartialEq, Eq)]
pub struct WiFiCredentials {
/// SSID bytes, 1–32 bytes.
pub ssid: Vec<u8>,
/// Pre-shared key / passphrase bytes, 0–64 bytes. Empty means
/// open network.
pub credentials: Vec<u8>,
}
impl core::fmt::Debug for WiFiCredentials {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("WiFiCredentials")
.field("ssid", &format_args!("<{} bytes>", self.ssid.len()))
.field(
"credentials",
&format_args!("<redacted, {} bytes>", self.credentials.len()),
)
.finish()
}
}
/// Operational-network credentials for the commissionee, selecting which
/// network-provisioning sub-cursor the state machine runs after `AddNOC`.
///
/// Mirrors chip's `AutoCommissioner`: network provisioning runs only for
/// the concrete network type whose credentials are supplied.
///
/// - [`NetworkCredentials::WiFi`] provisions Wi-Fi via
/// `AddOrUpdateWiFiNetwork` + `ConnectNetwork`.
/// - [`NetworkCredentials::Thread`] provisions Thread from an operational
/// dataset via `AddOrUpdateThreadNetwork` + `ConnectNetwork` (the
/// Extended PAN ID is the `ConnectNetwork` `network_id`); the dataset is
/// self-validated at [`ThreadDataset`](crate::ThreadDataset)
/// construction.
/// - [`NetworkCredentials::AlreadyOnNetwork`] skips network provisioning
/// entirely — correct both for Ethernet-only devices and for devices
/// already reachable on their operational network (the usual
/// IP-commissioning case, e.g. a second-fabric commission).
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum NetworkCredentials {
/// Provision Wi-Fi using the supplied station credentials.
WiFi(WiFiCredentials),
/// Provision Thread using the supplied operational dataset.
Thread(crate::thread_dataset::ThreadDataset),
/// Device is already on an operational network; skip provisioning.
AlreadyOnNetwork,
}
/// Configuration passed to [`Commissioner::new`].
///
/// All fields are by-reference where possible so the state machine
/// can share long-lived caller-owned resources (the fabric record, the
/// trust store, the setup payload) without copying.
///
/// **Not `#[non_exhaustive]`** — callers build this as a struct literal
/// with all public fields populated. Adding a field is a breaking change,
/// accepted for a pre-1.0 unpublished crate. `#[non_exhaustive]` stays on
/// [`Action`], [`Expectation`], [`Stage`], and [`CommissioningError`] —
/// those are read by callers, not constructed by them.
pub struct CommissionerConfig<'a> {
/// 16-byte attestation challenge derived from the active PASE
/// session. Matter Core Spec §3.6.4: bytes `[32..48]` of the
/// 48-byte PASE session key blob (exposed as
/// `PaseSessionKeys::attestation_key`).
pub pase_attestation_challenge: [u8; 16],
/// The commissioner's fabric record (RCAC keypair + signer + IPK).
/// Constructed via [`FabricRecord::new_root_only`] from M6.3.
pub fabric: &'a FabricRecord,
/// The setup payload parsed from QR or manual code (M6.1). Used
/// to cross-check VID/PID against the DAC's subject during
/// attestation verification.
pub setup_payload: &'a SetupPayload,
/// Trusted PAA roots for attestation chain validation (M6.2).
pub paa_trust_store: &'a PaaTrustStore,
/// Trusted CSA Certification Declaration signing roots (M6.4.3).
/// Tests can use `CdSigningRoots::with_csa_test_roots()`; production
/// callers supply CSA-published roots via `CdSigningRoots::from_pem`.
pub cd_signing_roots: &'a crate::attestation::CdSigningRoots,
/// The commissioner's own operational node ID on this fabric.
/// Must be non-zero.
pub commissioner_node_id: u64,
/// The operational node ID being assigned to the device on this
/// fabric. Must be non-zero and distinct from
/// `commissioner_node_id`.
pub assigned_node_id: u64,
/// 16-byte Identity Protection Key (IPK) epoch key for `AddNOC`.
/// Matter Core Spec §4.15.2. Must not be all-zero (rejected by
/// the device-side `AddNOC` handler).
pub ipk_epoch_key: [u8; 16],
/// CASE admin subject for `AddNOC` (typically the commissioner's
/// own operational node ID).
pub case_admin_subject: u64,
/// Admin vendor ID for `AddNOC`.
pub admin_vendor_id: u16,
/// Wall-clock time at construction. Used for NOC + RCAC validity
/// windows and for chain verification's `not_before` / `not_after`
/// checks.
pub now: MatterTime,
/// RNG for nonces (`CSRNonce`, `AttestationNonce`) and NOC serials.
pub rng: Arc<dyn NocRng>,
/// Operational-network credentials for the commissionee.
///
/// [`NetworkCredentials::AlreadyOnNetwork`] skips the network
/// sub-cursor entirely, mirroring chip's `AutoCommissioner`: network
/// provisioning runs ONLY when concrete credentials are supplied. It is
/// correct both for Ethernet-only devices and for devices already
/// reachable on their operational network (the usual case for IP
/// commissioning, e.g. a second-fabric commission). Supplying
/// [`NetworkCredentials::WiFi`] or [`NetworkCredentials::Thread`]
/// forces provisioning via `Stage::NetworkSetup`.
pub network: NetworkCredentials,
}
/// The commissioning state machine cursor.
///
/// One `Commissioner` per in-flight commissioning. `Send` but `!Sync`.
/// See module docs in [`crate::state_machine`] for the driver-loop
/// example.
// `commissioner_node_id` mirrors Matter Core Spec terminology
// (commissioner node ID vs. assigned node ID). Renaming to satisfy
// the lint would obscure the spec mapping.
#[allow(clippy::struct_field_names)]
pub struct Commissioner {
stage: Stage,
// Configuration captured at construction time. Storage slots for
// M6.4.2+ (`pai_der`, `dac_der`, `attestation_response`, CSR /
// NOC artefacts, the CASE-awaiting flag, etc.) are added in
// later tasks as the corresponding stages land — keeping the
// struct minimal here avoids per-task churn on the field list.
#[allow(dead_code)] // Used by attestation/CSR verification in M6.4.2+.
pase_attestation_challenge: [u8; 16],
#[allow(dead_code)] // Used by NOC issuance + chain validation in M6.4.4.
fabric: FabricRecord,
#[allow(dead_code)] // Used by chain validation in M6.4.2.
paa_trust_store: PaaTrustStore,
cd_signing_roots: crate::attestation::CdSigningRoots,
#[allow(dead_code)] // Used by VID/PID cross-check in M6.4.2.
setup_payload: SetupPayload,
#[allow(dead_code)] // Used by NOC subject in M6.4.4.
commissioner_node_id: u64,
#[allow(dead_code)] // Used by NOC subject in M6.4.4.
assigned_node_id: u64,
#[allow(dead_code)] // Used by AddNOC payload in M6.4.4.
ipk_epoch_key: [u8; 16],
#[allow(dead_code)] // Used by AddNOC payload in M6.4.4.
case_admin_subject: u64,
#[allow(dead_code)] // Used by AddNOC payload in M6.4.4.
admin_vendor_id: u16,
#[allow(dead_code)] // Used by cert validity windows in M6.4.2 + M6.4.4.
now: MatterTime,
#[allow(dead_code)] // Used for nonce generation in M6.4.2 + M6.4.4.
rng: Arc<dyn NocRng>,
// Attestation slots — populated by SendPaiCertRequest /
// SendDacCertRequest / SendAttestationRequest, consumed by
// AttestationVerification (M6.4.2 T18-T21).
pai_der: Option<Vec<u8>>,
dac_der: Option<Vec<u8>>,
attestation_nonce: Option<[u8; 32]>,
attestation_response: Option<crate::attestation::AttestationResponse>,
// CSR + NOC slots — populated by SendOpCertSigningRequest /
// ValidateCsr / GenerateNocChain, consumed by SendTrustedRootCert
// and SendNoc (M6.4.4 T35-T40).
csr_nonce: Option<[u8; 32]>,
csr_response: Option<crate::noc::CsrResponse>,
verified_csr: Option<crate::noc::VerifiedCsr>,
issued_noc: Option<matter_cert::MatterCertificate>,
issued_noc_public_key: Option<[u8; 65]>,
/// Operational-network credentials captured from config at
/// construction; consumed by the network-provisioning sub-cursor
/// (`Stage::NetworkSetup`, `AddOrUpdateWiFiNetwork` for Wi-Fi or
/// `AddOrUpdateThreadNetwork` for Thread).
/// [`NetworkCredentials::AlreadyOnNetwork`] skips provisioning.
network: NetworkCredentials,
/// Maximum failsafe expiry the device accepts, in seconds.
/// Initialised to 60 (the M6.4 fallback) and updated from
/// `BasicCommissioningInfo::failsafe_expiry_length_seconds` once
/// the `Expectation::CommissioningInfo` response arrives. The first
/// `Stage::ArmFailsafe` consumes this.
failsafe_expiry_seconds: u16,
/// Device-declared `ConnectMaxTimeSeconds` (`NetworkCommissioning`
/// attribute `0x0003`), captured from the
/// `Expectation::NetworkCommissioningInfo` read. `0` means unread /
/// absent, in which case [`Self::network_enable_failsafe_seconds`]
/// falls back to [`DEFAULT_CONNECT_MAX_TIME_SECONDS`]. Sizes the
/// `Stage::FailsafeBeforeNetworkEnable` failsafe extension so Thread
/// attach (slower than Wi-Fi association) has room to complete before
/// the failsafe expires.
connect_max_time_seconds: u16,
/// Monotonically-increasing breadcrumb attached to every
/// breadcrumb-bearing cluster command. Matter Core Spec §11.10
/// uses breadcrumb so an interrupted commissioning can be resumed
/// from the last acknowledged step. Initialised to `1` in
/// `Commissioner::new`; incremented after every breadcrumb emit.
breadcrumb_counter: u64,
/// `true` after [`Stage::FindOperationalForComplete`] emits
/// `Action::EstablishCase`; cleared by
/// [`Commissioner::on_case_established`] (success) or by
/// `on_response(Expectation::CaseFailed, _)` (failure).
awaiting_case_session: bool,
/// The Expectation the state machine last emitted with `poll()`.
/// `None` while not waiting for a response (terminal stages, or
/// pre-poll).
awaiting: Option<Expectation>,
/// Cached pending Action so repeated `poll()` calls between
/// `on_response`s are idempotent. Cleared when the cursor advances.
pending_action: Option<Action>,
/// Rendered summary of why the state machine entered `Failed`,
/// stashed by `on_response`'s error path and read by the
/// `Stage::Failed` arm of `dispatch_stage` so `Action::Abort.reason`
/// surfaces the real failure (not a hard-coded placeholder).
last_failure: Option<String>,
}
impl Commissioner {
/// Construct a new commissioner from a validated config.
///
/// # Errors
///
/// Returns [`CommissioningError::InvalidConfig`] if any field fails
/// basic validation: zero `commissioner_node_id`, zero
/// `assigned_node_id`, `commissioner_node_id == assigned_node_id`,
/// or all-zero `ipk_epoch_key`.
pub fn new(cfg: CommissionerConfig<'_>) -> Result<Self, CommissioningError> {
if cfg.commissioner_node_id == 0 {
return Err(CommissioningError::InvalidConfig(
"commissioner_node_id must be non-zero",
));
}
if cfg.assigned_node_id == 0 {
return Err(CommissioningError::InvalidConfig(
"assigned_node_id must be non-zero",
));
}
if cfg.assigned_node_id == cfg.commissioner_node_id {
return Err(CommissioningError::InvalidConfig(
"assigned_node_id must differ from commissioner_node_id",
));
}
if cfg.ipk_epoch_key == [0u8; 16] {
return Err(CommissioningError::InvalidConfig(
"ipk_epoch_key must not be all-zero",
));
}
// Only Wi-Fi credentials carry length bounds here; `Thread`
// datasets are self-validated at `ThreadDataset` construction and
// `AlreadyOnNetwork` carries no data to check.
if let NetworkCredentials::WiFi(creds) = &cfg.network {
if creds.ssid.is_empty() {
return Err(CommissioningError::InvalidConfig(
"network: Wi-Fi ssid must not be empty",
));
}
if creds.ssid.len() > 32 {
return Err(CommissioningError::InvalidConfig(
"network: Wi-Fi ssid must be ≤32 bytes",
));
}
if creds.credentials.len() > 64 {
return Err(CommissioningError::InvalidConfig(
"network: Wi-Fi credentials must be ≤64 bytes",
));
}
}
Ok(Self {
stage: Stage::SecurePairing,
pase_attestation_challenge: cfg.pase_attestation_challenge,
fabric: cfg.fabric.clone(),
paa_trust_store: cfg.paa_trust_store.clone(),
cd_signing_roots: cfg.cd_signing_roots.clone(),
setup_payload: cfg.setup_payload.clone(),
commissioner_node_id: cfg.commissioner_node_id,
assigned_node_id: cfg.assigned_node_id,
ipk_epoch_key: cfg.ipk_epoch_key,
case_admin_subject: cfg.case_admin_subject,
admin_vendor_id: cfg.admin_vendor_id,
now: cfg.now,
rng: cfg.rng,
pai_der: None,
dac_der: None,
attestation_nonce: None,
attestation_response: None,
csr_nonce: None,
csr_response: None,
verified_csr: None,
issued_noc: None,
issued_noc_public_key: None,
network: cfg.network,
failsafe_expiry_seconds: 60,
connect_max_time_seconds: 0,
breadcrumb_counter: 1,
awaiting_case_session: false,
awaiting: None,
pending_action: None,
last_failure: None,
})
}
/// Current cursor position. Useful for logging + tests.
#[must_use]
pub fn stage(&self) -> Stage {
self.stage
}
/// The operational-network credentials captured at construction.
/// Read by tests and by the network-provisioning dispatch/routing
/// (Task 5 consumes this to select the Thread sub-cursor).
#[allow(dead_code)] // Consumed by tests now; by Thread routing in Task 5.
pub(crate) fn network(&self) -> &NetworkCredentials {
&self.network
}
/// **Test-only.** Jumps the cursor to `stage` and applies any opt-in
/// seeds in `seeds`. Consumes `self` and returns the repositioned
/// `Commissioner`.
///
/// Use this in integration tests when a real M6.4 attestation +
/// NOC-issuance flow is not yet available (the M6.4.6 real-fixture
/// e2e driver is still operator-touch deferred — see
/// `TODO-1.0.md`). Never use in production code.
///
/// Behind the `__test_shortcuts` feature flag.
#[cfg(feature = "__test_shortcuts")]
#[must_use]
pub fn position_at_stage_for_test(mut self, stage: Stage, seeds: TestStateSeeds) -> Self {
self.stage = stage;
if let Some(pk) = seeds.synthetic_noc_pubkey {
self.issued_noc_public_key = Some(pk);
}
self
}
/// Drive the state machine forward.
///
/// Returns the next [`Action`] the caller must perform. Idempotent:
/// calling `poll` twice without an intervening `on_response` returns
/// the same `Action`.
///
/// # Errors
///
/// Returns the typed error that caused a transition into
/// [`Stage::Failed`] — when this happens, the cursor advances to
/// `Failed` and the next `poll()` call emits an
/// [`Action::Abort`] with a rendered summary of the failure.
#[cfg_attr(feature = "tracing", instrument(skip(self), fields(stage = ?self.stage)))]
pub fn poll(&mut self) -> Result<Action, CommissioningError> {
if let Some(act) = self.pending_action.clone() {
return Ok(act);
}
let action = self.dispatch_stage()?;
self.pending_action = Some(action.clone());
Ok(action)
}
/// Failsafe extension (seconds) for `Stage::FailsafeBeforeNetworkEnable`.
///
/// Uses the device-reported `ConnectMaxTimeSeconds` when non-zero,
/// else [`DEFAULT_CONNECT_MAX_TIME_SECONDS`]. Sized to give the device
/// room to associate with the operational network (Thread attach is
/// slower than Wi-Fi association) before the failsafe expires.
fn network_enable_failsafe_seconds(&self) -> u16 {
if self.connect_max_time_seconds > 0 {
self.connect_max_time_seconds
} else {
DEFAULT_CONNECT_MAX_TIME_SECONDS
}
}
/// Record the device's `ConnectMaxTimeSeconds` (`NetworkCommissioning`
/// attribute `0x0003`), read alongside the `FeatureMap` at
/// `Stage::ReadNetworkCommissioningInfo`. Consumed by
/// [`Self::network_enable_failsafe_seconds`] to size the
/// `FailsafeBeforeNetworkEnable` extension. Called by the driver's
/// read-dispatch after the `FeatureMap` response is applied; a `0`
/// value (unread/absent) leaves the default in force.
pub(crate) fn set_connect_max_time_seconds(&mut self, seconds: u16) {
self.connect_max_time_seconds = seconds;
}
/// The device-reported `ConnectMaxTimeSeconds`, or `0` if the device
/// hasn't reported it yet (default before
/// `Stage::ReadNetworkCommissioningInfo` completes, or the device
/// reported `0`).
///
/// Consumed by the driver ([`crate::driver::commission`]) to size the
/// BLE-path `ConnectNetwork` response deadline from the same value that
/// [`Self::network_enable_failsafe_seconds`] uses for the failsafe
/// extension (spec D7: both must be sized from `ConnectMaxTimeSeconds`).
#[must_use]
pub(crate) fn connect_max_time_seconds(&self) -> u16 {
self.connect_max_time_seconds
}
/// Helper: emit an `ArmFailsafe` action at the current stage.
///
/// Used by both `Stage::ArmFailsafe` and
/// `Stage::FailsafeBeforeNetworkEnable`, which share identical action
/// logic. The failsafe expiry differs: the first arm uses the
/// device's `failsafe_expiry_length_seconds`; the pre-`ConnectNetwork`
/// extension uses [`Self::network_enable_failsafe_seconds`].
fn arm_failsafe_action(&mut self) -> Action {
use crate::clusters::general_commissioning as gc;
use crate::state_machine::action::SessionContext;
let expiry_seconds = if self.stage == Stage::FailsafeBeforeNetworkEnable {
self.network_enable_failsafe_seconds()
} else {
self.failsafe_expiry_seconds
};
let breadcrumb = self.next_breadcrumb();
let payload = gc::encode_arm_fail_safe(expiry_seconds, breadcrumb);
self.awaiting = Some(Expectation::ArmFailsafeResponse);
Action::Invoke {
session: SessionContext::Pase,
endpoint: 0,
cluster: gc::CLUSTER_ID,
command: gc::command_id::ARM_FAIL_SAFE,
payload,
expect: Expectation::ArmFailsafeResponse,
}
}
/// Compute the next [`Action`] for the current [`Stage`].
///
/// Called by [`Self::poll`] only when there is no cached
/// `pending_action`. Walks `Stage::SecurePairing` forward to the
/// first wire stage by self-recursion; stages past
/// `Stage::ConfigRegulatory` short-circuit to `Stage::Failed` until
/// M6.4.2+ tasks land.
// Lint carve-out: the per-stage arms each carry their own
// payload-shape comments, so collapsing them into smaller helpers
// would obscure the cluster-command mapping the function
// documents. Each new stage adds a small fixed arm.
#[allow(clippy::too_many_lines)]
fn dispatch_stage(&mut self) -> Result<Action, CommissioningError> {
use crate::clusters::general_commissioning as gc;
use crate::state_machine::action::SessionContext;
match self.stage {
Stage::SecurePairing => {
// Entry → first wire stage. Advance and re-dispatch.
self.stage = Stage::ReadCommissioningInfo;
self.dispatch_stage()
}
Stage::ReadCommissioningInfo => {
self.awaiting = Some(Expectation::CommissioningInfo);
Ok(Action::ReadAttribute {
session: SessionContext::Pase,
endpoint: 0,
cluster: gc::CLUSTER_ID,
attributes: &[
// GeneralCommissioning attribute ids per spec §11.10.6
// (confirmed against a real device's report).
0x0000, // Breadcrumb
0x0001, // BasicCommissioningInfo (failsafe_expiry_length_seconds, …)
0x0002, // RegulatoryConfig
0x0004, // SupportsConcurrentConnection
],
expect: Expectation::CommissioningInfo,
})
}
Stage::ArmFailsafe | Stage::FailsafeBeforeNetworkEnable => {
Ok(self.arm_failsafe_action())
}
Stage::ConfigRegulatory => {
let breadcrumb = self.next_breadcrumb();
// FIXME(temp, uncommitted): hardcoding IndoorOutdoor(2) exceeds
// stricter devices' LocationCapability → SetRegulatoryConfig
// returns ValueOutsideRange (errorCode 1). The chip-faithful fix
// is to read attr 0x03 (LocationCapability) in ReadCommissioningInfo
// and echo it here. Indoor(0) is the safe universal value (accepted
// by Indoor-only AND IndoorOutdoor devices) — minimal unblock.
let payload = gc::encode_set_regulatory_config(
gc::RegulatoryLocation::Indoor,
"XX",
breadcrumb,
);
self.awaiting = Some(Expectation::SetRegulatoryConfigResponse);
Ok(Action::Invoke {
session: SessionContext::Pase,
endpoint: 0,
cluster: gc::CLUSTER_ID,
command: gc::command_id::SET_REGULATORY_CONFIG,
payload,
expect: Expectation::SetRegulatoryConfigResponse,
})
}
Stage::SendPaiCertRequest => {
use crate::noc::{encode_certificate_chain_request, CertChainType};
let payload = encode_certificate_chain_request(CertChainType::Pai);
self.awaiting = Some(Expectation::PaiCertChainResponse);
Ok(Action::Invoke {
session: SessionContext::Pase,
endpoint: 0,
cluster: 0x003E,
command: 0x02,
payload,
expect: Expectation::PaiCertChainResponse,
})
}
Stage::SendDacCertRequest => {
use crate::noc::{encode_certificate_chain_request, CertChainType};
let payload = encode_certificate_chain_request(CertChainType::Dac);
self.awaiting = Some(Expectation::DacCertChainResponse);
Ok(Action::Invoke {
session: SessionContext::Pase,
endpoint: 0,
cluster: 0x003E,
command: 0x02,
payload,
expect: Expectation::DacCertChainResponse,
})
}
Stage::SendAttestationRequest => {
use crate::noc::encode_attestation_request;
let mut nonce = [0u8; 32];
self.rng
.fill(&mut nonce)
.map_err(CommissioningError::from)?;
let payload = encode_attestation_request(&nonce);
self.attestation_nonce = Some(nonce);
self.awaiting = Some(Expectation::AttestationResponse);
Ok(Action::Invoke {
session: SessionContext::Pase,
endpoint: 0,
cluster: 0x003E,
command: 0x00,
payload,
expect: Expectation::AttestationResponse,
})
}
Stage::AttestationVerification => {
match self.run_attestation_verification() {
Ok(()) => {
self.advance(Stage::SendOpCertSigningRequest);
self.dispatch_stage()
}
Err(err) => {
// Poll-time failure: align with the contract documented
// on `poll()` — cursor advances to `Failed`, the next
// `poll()` emits `Action::Abort` with a rendered reason.
self.last_failure = Some(err.to_string());
self.stage = Stage::Failed;
self.awaiting = None;
self.pending_action = None;
Err(err)
}
}
}
Stage::SendOpCertSigningRequest => {
use crate::noc::encode_csr_request;
let mut nonce = [0u8; 32];
self.rng
.fill(&mut nonce)
.map_err(CommissioningError::from)?;
// Spec §11.18.5.5 `CSRRequest`. `is_for_update_noc` is
// hard-coded false: M6.4 only commissions new fabrics.
let payload = encode_csr_request(&nonce, false);
self.csr_nonce = Some(nonce);
self.awaiting = Some(Expectation::CsrResponse);
Ok(Action::Invoke {
session: SessionContext::Pase,
endpoint: 0,
cluster: 0x003E,
command: 0x04,
payload,
expect: Expectation::CsrResponse,
})
}
Stage::ValidateCsr => {
// Off-wire: M6.3's three-check verify_csr_response gate.
self.run_validate_csr()?;
self.advance(Stage::GenerateNocChain);
self.dispatch_stage()
}
Stage::GenerateNocChain => {
// Off-wire: build + sign the NOC under the fabric's RCAC.
self.run_generate_noc_chain()?;
self.advance(Stage::SendTrustedRootCert);
self.dispatch_stage()
}
Stage::SendTrustedRootCert => {
use crate::noc::encode_add_trusted_root;
// RCAC is already TLV-serialisable via matter-cert's
// `to_tlv`. Surfaces as NocError::CertBuild on the rare
// re-serialisation failure path (codec / extension shape
// regression). Sanity: `FabricRecord::new_root_only` round-
// tripped the cert through `verify_signed_by` at
// construction, so the bytes are well-formed by here.
let rcac_tlv =
self.fabric.root_cert.to_tlv().map_err(|e| {
CommissioningError::from(crate::noc::NocError::CertBuild(e))
})?;
#[cfg(feature = "tracing")]
tracing::debug!(
rcac_tlv = %crate::hexdump::hex(&rcac_tlv),
"sending AddTrustedRootCertificate"
);
let payload = encode_add_trusted_root(&rcac_tlv);
self.awaiting = Some(Expectation::AddTrustedRootResponse);
Ok(Action::Invoke {
session: SessionContext::Pase,
endpoint: 0,
cluster: 0x003E,
command: 0x0B,
payload,
expect: Expectation::AddTrustedRootResponse,
})
}
Stage::SendNoc => {
use crate::noc::encode_add_noc;
let noc = self
.issued_noc
.as_ref()
.ok_or(CommissioningError::OutOfOrderResponse(self.stage))?;
let noc_tlv = noc
.to_tlv()
.map_err(|e| CommissioningError::from(crate::noc::NocError::CertBuild(e)))?;
// ICAC slot is `None` in M6.4: only RCAC -> NOC chains
// are issued. ICAC support is M6.3.x / M8 work.
let payload = encode_add_noc(
&noc_tlv,
None,
&self.ipk_epoch_key,
self.case_admin_subject,
self.admin_vendor_id,
);
self.awaiting = Some(Expectation::NocResponse);
Ok(Action::Invoke {
session: SessionContext::Pase,
endpoint: 0,
cluster: 0x003E,
command: 0x06,
payload,
expect: Expectation::NocResponse,
})
}
Stage::ReadNetworkCommissioningInfo => {
self.awaiting = Some(Expectation::NetworkCommissioningInfo);
Ok(Action::ReadAttribute {
session: SessionContext::Pase,
endpoint: 0,
cluster: crate::clusters::network_commissioning::CLUSTER_ID,
attributes: &[
crate::clusters::network_commissioning::attribute_id::FEATURE_MAP,
// ConnectMaxTimeSeconds (spec §11.9.5.4) — sizes the
// FailsafeBeforeNetworkEnable extension (D7). Thread
// attach is slower than Wi-Fi association.
crate::clusters::network_commissioning::attribute_id::CONNECT_MAX_TIME_SECONDS,
],
expect: Expectation::NetworkCommissioningInfo,
})
}
Stage::NetworkSetup => {
use crate::clusters::network_commissioning as nc;
// Select the provisioning command by the supplied
// credential type. The FeatureMap-cross-check at
// `Expectation::NetworkCommissioningInfo` guarantees the
// device actually supports this network type before we
// reach here, so `AlreadyOnNetwork` never lands in this
// arm — treat it as an out-of-order state, not a silent
// skip.
let breadcrumb = self.next_breadcrumb();
let (command, payload) = match &self.network {
NetworkCredentials::WiFi(creds) => (
nc::command_id::ADD_OR_UPDATE_WIFI_NETWORK,
nc::encode_add_or_update_wifi_network(
&creds.ssid,
&creds.credentials,
breadcrumb,
),
),
NetworkCredentials::Thread(dataset) => (
nc::command_id::ADD_OR_UPDATE_THREAD_NETWORK,
nc::encode_add_or_update_thread_network(dataset.as_bytes(), breadcrumb),
),
NetworkCredentials::AlreadyOnNetwork => {
return Err(CommissioningError::OutOfOrderResponse(self.stage));
}
};
self.awaiting = Some(Expectation::NetworkConfigResponse);
Ok(Action::Invoke {
session: SessionContext::Pase,
endpoint: 0,
cluster: nc::CLUSTER_ID,
command,
payload,
expect: Expectation::NetworkConfigResponse,
})
}
Stage::NetworkEnable => {
use crate::clusters::network_commissioning as nc;
// `ConnectNetwork` takes an opaque `network_id`: the SSID
// for Wi-Fi, the Extended PAN ID for Thread (spec §11.9.6.6).
let breadcrumb = self.next_breadcrumb();
let payload = match &self.network {
NetworkCredentials::WiFi(creds) => {
nc::encode_connect_network(&creds.ssid, breadcrumb)
}
NetworkCredentials::Thread(dataset) => {
nc::encode_connect_network(&dataset.ext_pan_id(), breadcrumb)
}
NetworkCredentials::AlreadyOnNetwork => {
return Err(CommissioningError::OutOfOrderResponse(self.stage));
}
};
self.awaiting = Some(Expectation::ConnectNetworkResponse);
Ok(Action::Invoke {
session: SessionContext::Pase,
endpoint: 0,
cluster: nc::CLUSTER_ID,
command: nc::command_id::CONNECT_NETWORK,
payload,
expect: Expectation::ConnectNetworkResponse,
})
}
Stage::EvictPreviousCaseSessions => {
// New-fabric commissioning has no prior CASE session
// to evict. M8 multi-fabric work will emit
// Action::EvictCase here.
self.advance(Stage::FindOperationalForComplete);
self.dispatch_stage()
}
Stage::FindOperationalForComplete => {
self.awaiting_case_session = true;
Ok(Action::EstablishCase {
fabric_id: self.fabric.fabric_id,
peer_node_id: self.assigned_node_id,
})
}
Stage::SendComplete => {
let payload = gc::encode_commissioning_complete();
self.awaiting = Some(Expectation::CommissioningCompleteResponse);
Ok(Action::Invoke {
session: SessionContext::Case,
endpoint: 0,
cluster: gc::CLUSTER_ID,
command: gc::command_id::COMMISSIONING_COMPLETE,
payload,
expect: Expectation::CommissioningCompleteResponse,
})
}
Stage::Cleanup => {
let public_key = self
.issued_noc_public_key
.ok_or(CommissioningError::OutOfOrderResponse(self.stage))?;
Ok(Action::Done(crate::state_machine::CommissionedFabric {
fabric: self.fabric.clone(),
peer_node_id: self.assigned_node_id,
peer_root_public_key: public_key,
terminated_at: Stage::Cleanup,
}))
}
Stage::Failed => {
// Subsequent poll() after a failure surfaces the Abort.
// The state machine stays in Failed.
self.awaiting = None;
let reason = self
.last_failure
.clone()
.unwrap_or_else(|| "commissioning aborted".to_string());
Ok(Action::Abort {
send_disarm_failsafe: true,
reason,
})
} // Every `Stage` variant has its own arm above. `Stage` is
// `#[non_exhaustive]` for cross-crate consumers, but within
// this crate the match is exhaustive — no `_ =>` arm needed.
}
}
/// Feed a response payload back into the state machine.
///
/// `expect` MUST match the [`Expectation`] from the last `poll()`'s
/// emitted `Action`.
///
/// # Errors
///
/// - [`CommissioningError::OutOfOrderResponse`] if the state machine
/// isn't currently waiting for a response.
/// - [`CommissioningError::UnexpectedResponseKind`] if `expect`
/// doesn't match the last `Action`'s `Expectation`. The cursor
/// does not advance.
/// - [`CommissioningError::MalformedResponse`] if `payload` fails
/// to decode at the cluster-command level.
/// - [`CommissioningError::DeviceImStatus`] if the device returned a
/// non-OK Interaction Model status.
///
/// Any error other than `OutOfOrderResponse` and
/// `UnexpectedResponseKind` transitions the cursor to
/// [`Stage::Failed`]; the next `poll()` call emits
/// [`Action::Abort`] with a rendered summary.
#[cfg_attr(feature = "tracing", instrument(skip(self, payload), fields(stage = ?self.stage, expectation = ?expect)))]
pub fn on_response(
&mut self,
expect: Expectation,
payload: &[u8],
) -> Result<(), CommissioningError> {
if expect == Expectation::CaseFailed {
// CaseFailed bypasses the awaiting check — the caller
// signals failure of the EstablishCase action explicitly,
// and EstablishCase tracks readiness via
// `awaiting_case_session`, not `awaiting`.
if !self.awaiting_case_session {
return Err(CommissioningError::OutOfOrderResponse(self.stage));
}
self.awaiting_case_session = false;
self.stage = Stage::Failed;
self.awaiting = None;
self.pending_action = None;
self.last_failure = Some(CommissioningError::CaseEstablishmentFailed.to_string());
return Err(CommissioningError::CaseEstablishmentFailed);
}
let Some(awaiting) = self.awaiting else {
return Err(CommissioningError::OutOfOrderResponse(self.stage));
};
if awaiting != expect {
return Err(CommissioningError::UnexpectedResponseKind {
expected: awaiting,
got: expect,
});
}
match self.handle_response(expect, payload) {
Ok(()) => Ok(()),
Err(err) => {
self.last_failure = Some(err.to_string());
self.stage = Stage::Failed;
self.awaiting = None;
self.pending_action = None;
Err(err)
}
}
}
/// Signal that CASE establishment (mDNS find-operational + the
/// SIGMA-I handshake — both M6.6 mechanics, owned by the driver)
/// has succeeded. The state machine advances from
/// [`Stage::FindOperationalForComplete`] to [`Stage::SendComplete`].
///
/// # Errors
///
/// Returns [`CommissioningError::OutOfOrderResponse`] if the state
/// machine isn't currently awaiting CASE establishment (i.e., the
/// cursor is not at `FindOperationalForComplete` or the
/// `EstablishCase` action hasn't been emitted yet).
#[cfg_attr(feature = "tracing", instrument(skip(self)))]
pub fn on_case_established(&mut self) -> Result<(), CommissioningError> {
if !self.awaiting_case_session {
return Err(CommissioningError::OutOfOrderResponse(self.stage));
}
self.awaiting_case_session = false;
self.advance(Stage::SendComplete);
Ok(())
}
#[allow(clippy::too_many_lines)]
fn handle_response(
&mut self,
expect: Expectation,
payload: &[u8],
) -> Result<(), CommissioningError> {
use crate::clusters::general_commissioning as gc;
match expect {
Expectation::CommissioningInfo => {
Self::assert_tlv_well_formed(self.stage, payload)?;
// Best-effort: scan the response for a BasicCommissioningInfo
// struct and update failsafe_expiry_seconds. Malformed or
// missing → keep the M6.4 fallback (60s) silently.
if let Some(info) = gc::decode_basic_commissioning_info(payload) {
if info.failsafe_expiry_length_seconds > 0 {
self.failsafe_expiry_seconds = info.failsafe_expiry_length_seconds;
}
}
self.advance(Stage::ArmFailsafe);
Ok(())
}
Expectation::ArmFailsafeResponse => {
let resp = gc::decode_arm_fail_safe_response(payload)?;
if resp.error_code != 0 {
return Err(CommissioningError::DeviceImStatus {
stage: self.stage,
im_status: u16::from(resp.error_code),
});
}
let next = match self.stage {
Stage::ArmFailsafe => Stage::ConfigRegulatory,
Stage::FailsafeBeforeNetworkEnable => Stage::NetworkEnable,
other => {
return Err(CommissioningError::OutOfOrderResponse(other));
}
};
self.advance(next);
Ok(())
}
Expectation::SetRegulatoryConfigResponse => {
let resp = gc::decode_set_regulatory_config_response(payload)?;
if resp.error_code != 0 {
return Err(CommissioningError::DeviceImStatus {
stage: Stage::ConfigRegulatory,
im_status: u16::from(resp.error_code),
});
}
self.advance(Stage::SendPaiCertRequest);
Ok(())
}
Expectation::PaiCertChainResponse => {
let resp = crate::noc::decode_certificate_chain_response(payload)?;
self.pai_der = Some(resp.certificate);
self.advance(Stage::SendDacCertRequest);
Ok(())
}
Expectation::DacCertChainResponse => {
let resp = crate::noc::decode_certificate_chain_response(payload)?;
self.dac_der = Some(resp.certificate);
self.advance(Stage::SendAttestationRequest);
Ok(())
}
Expectation::AttestationResponse => {
let resp = crate::noc::decode_attestation_response(payload)?;
self.attestation_response = Some(resp);
self.advance(Stage::AttestationVerification);
Ok(())
}
Expectation::CsrResponse => {
let resp = crate::noc::decode_csr_response(payload)?;
self.csr_response = Some(resp);
self.advance(Stage::ValidateCsr);
Ok(())
}
Expectation::AddTrustedRootResponse => {
// `AddTrustedRootCertificate` has no typed response —
// success is a status-only ack at the Interaction Model
// layer. The caller surfaces the IM status as a 1-byte
// payload: `0x00` = success, anything else = error.
if payload.first() != Some(&0u8) {
return Err(CommissioningError::DeviceImStatus {
stage: Stage::SendTrustedRootCert,
im_status: u16::from(payload.first().copied().unwrap_or(0xFF)),
});
}
self.advance(Stage::SendNoc);
Ok(())
}
Expectation::NocResponse => {
let resp = crate::noc::decode_noc_response(payload)?;
if resp.status != 0 {
return Err(CommissioningError::DeviceImStatus {
stage: Stage::SendNoc,
im_status: u16::from(resp.status),
});
}
self.advance(Stage::ReadNetworkCommissioningInfo);
Ok(())
}
Expectation::NetworkCommissioningInfo => {
use crate::clusters::network_commissioning as nc;
use crate::state_machine::NetworkKind;
let features = nc::decode_feature_map(payload)?;
// A FeatureMap with no recognised interface bit is
// malformed regardless of the supplied credentials — a
// NetworkCommissioning cluster always exposes at least one
// of Wi-Fi / Thread / Ethernet.
if features.is_empty() {
return Err(CommissioningError::MalformedResponse(
Stage::ReadNetworkCommissioningInfo,
));
}
// Route by the *supplied* credential type, cross-checked
// against the device FeatureMap. This resolves the
// dual-stack ordering ambiguity (a Wi-Fi+Thread device is
// provisioned per the caller's chosen credential type, not
// by feature-bit order) and rejects a mismatch (credential
// type absent from the FeatureMap) instead of silently
// skipping provisioning.
match &self.network {
NetworkCredentials::WiFi(_) => {
if !features.contains(nc::NetworkCommissioningFeature::WIFI) {
return Err(CommissioningError::NetworkFeatureUnsupported {
needed: NetworkKind::WiFi,
});
}
self.advance(Stage::NetworkSetup);
}
NetworkCredentials::Thread(_) => {
if !features.contains(nc::NetworkCommissioningFeature::THREAD) {
return Err(CommissioningError::NetworkFeatureUnsupported {
needed: NetworkKind::Thread,
});
}
self.advance(Stage::NetworkSetup);
}
NetworkCredentials::AlreadyOnNetwork => {
// No credentials to provision: the device is
// already reachable on its operational network (IP
// commissioning reached it there — e.g. a
// second-fabric commission of an already-provisioned
// device, or an Ethernet-only device). Mirror
// chip's AutoCommissioner: skip the network
// sub-cursor entirely (observed necessary on a real
// device: Tapo P110M, M6.6.5 validation).
self.advance(Stage::EvictPreviousCaseSessions);
}
}
Ok(())
}
Expectation::NetworkConfigResponse => {
use crate::clusters::network_commissioning as nc;
let resp = nc::decode_network_config_response(Stage::NetworkSetup, payload)?;
if resp.networking_status != 0 {
return Err(CommissioningError::NetworkRejected {
stage: Stage::NetworkSetup,
networking_status: resp.networking_status,
debug_text: resp.debug_text,
remediation_hint: nc::remediation_for(resp.networking_status),
});
}
self.advance(Stage::FailsafeBeforeNetworkEnable);
Ok(())
}
Expectation::ConnectNetworkResponse => {
use crate::clusters::network_commissioning as nc;
let resp = nc::decode_connect_network_response(Stage::NetworkEnable, payload)?;
if resp.networking_status != 0 {
return Err(CommissioningError::NetworkRejected {
stage: Stage::NetworkEnable,
networking_status: resp.networking_status,
debug_text: resp.debug_text,
remediation_hint: nc::remediation_for(resp.networking_status),
});
}
self.advance(Stage::EvictPreviousCaseSessions);
Ok(())
}
Expectation::CommissioningCompleteResponse => {
let (error_code, _debug) =
gc::decode_commissioning_error_response(Stage::SendComplete, payload)?;
if error_code != 0 {
return Err(CommissioningError::DeviceImStatus {
stage: Stage::SendComplete,
im_status: u16::from(error_code),
});
}
self.advance(Stage::Cleanup);
Ok(())
}
// `Expectation::CaseFailed` is handled by `on_response`'s
// pre-awaiting fast path and never reaches handle_response.
_ => Err(CommissioningError::OutOfOrderResponse(self.stage)),
}
}
fn next_breadcrumb(&mut self) -> u64 {
let b = self.breadcrumb_counter;
self.breadcrumb_counter = b.saturating_add(1);
b
}
fn advance(&mut self, next: Stage) {
self.stage = next;
self.awaiting = None;
self.pending_action = None;
}
/// Off-wire attestation verification chain (M6.4.2 T21).
///
/// Consumes the PAI/DAC DER + `AttestationResponse` + nonce captured
/// by [`Stage::SendPaiCertRequest`] / [`Stage::SendDacCertRequest`]
/// / [`Stage::SendAttestationRequest`] and runs M6.2's verifier
/// chain end-to-end:
///
/// 1. Parse PAI/DAC DER.
/// 2. `verify_chain` — webpki path validation + Matter VID/PID
/// overlay (M6.2.2).
/// 3. `verify_attestation_response` — ECDSA signature over
/// `attestation_elements || attestation_challenge` (M6.2.3).
/// 4. `extract_attestation_elements_fields` — pull the
/// `attestation_nonce` echo + CD bytes out of the TLV blob.
/// 5. Confirm the device echoed the nonce we sent.
/// 6. `verify_certification_declaration` — verify the CSA-signed CD
/// embedded in `attestation_elements` against
/// [`crate::attestation::CdSigningRoots`] and confirm the
/// declared VID/PID match what the DAC subject claimed.
fn run_attestation_verification(&mut self) -> Result<(), CommissioningError> {
use crate::attestation::{
extract_attestation_elements_fields, verify_attestation_response, verify_chain,
AttestationError, Dac, Pai,
};
let pai_der = self
.pai_der
.as_ref()
.ok_or(CommissioningError::OutOfOrderResponse(self.stage))?;
let dac_der = self
.dac_der
.as_ref()
.ok_or(CommissioningError::OutOfOrderResponse(self.stage))?;
let response = self
.attestation_response
.as_ref()
.ok_or(CommissioningError::OutOfOrderResponse(self.stage))?;
let expected_nonce = self
.attestation_nonce
.ok_or(CommissioningError::OutOfOrderResponse(self.stage))?;
// 1. Parse chain certs.
let pai = Pai::from_der(pai_der)?;
let dac = Dac::from_der(dac_der)?;
#[cfg(feature = "tracing")]
tracing::debug!(
dac_der = %crate::hexdump::hex(dac_der),
pai_der = %crate::hexdump::hex(pai_der),
"verifying attestation chain"
);
// 2. Chain validation (M6.2.2 — webpki path validation + VID/PID overlay).
// The returned `ChainVerification` carries the VID/PID that
// both webpki and the Matter overlay agreed on; we re-use
// those for the CD check below so a single source of truth
// drives both the chain validation and the CD VID/PID
// equality check.
let chain = verify_chain(&dac, &pai, &self.paa_trust_store, self.now)?;
// 3. AttestationResponse signature (M6.2.3).
verify_attestation_response(response, &self.pase_attestation_challenge, dac.public_key())?;
// 4. Extract attestation_elements fields: CD bytes (M6.4.3 will verify),
// nonce echo, timestamp.
let fields = extract_attestation_elements_fields(&response.attestation_elements)?;
if fields.attestation_nonce != expected_nonce {
return Err(CommissioningError::Attestation(
AttestationError::ResponseElementsMalformed,
));
}
// 5. CD verification — verify the device's declared VID/PID
// against the CSA-signed Certification Declaration extracted
// from `attestation_elements`.
#[cfg(feature = "tracing")]
tracing::debug!(
cd_cms = %crate::hexdump::hex(&fields.certification_declaration),
"verifying certification declaration"
);
crate::attestation::verify_certification_declaration(
&fields.certification_declaration,
chain.vendor_id,
chain.product_id,
&self.cd_signing_roots,
)?;
Ok(())
}
/// Off-wire CSR verification (M6.4.4 `Stage::ValidateCsr`).
///
/// Consumes the `CsrResponse` captured by `Stage::SendOpCertSigningRequest`
/// plus the DAC DER captured earlier by `Stage::SendDacCertRequest`,
/// and runs M6.3's `verify_csr_response` three-check atomic gate:
///
/// 1. PKCS#10 self-signature on the embedded CSR.
/// 2. The device's `CSRNonce` echo equals the commissioner-issued nonce.
/// 3. The DAC's attestation signature over
/// `nocsr_elements || attestation_challenge`.
fn run_validate_csr(&mut self) -> Result<(), CommissioningError> {
use crate::attestation::Dac;
use crate::noc::verify_csr_response;
let resp = self
.csr_response
.as_ref()
.ok_or(CommissioningError::OutOfOrderResponse(self.stage))?;
let dac_der = self
.dac_der
.as_ref()
.ok_or(CommissioningError::OutOfOrderResponse(self.stage))?;
let csr_nonce = self
.csr_nonce
.ok_or(CommissioningError::OutOfOrderResponse(self.stage))?;
let dac = Dac::from_der(dac_der)?;
let verified = verify_csr_response(
&resp.nocsr_elements,
&resp.attestation_signature,
&csr_nonce,
&self.pase_attestation_challenge,
dac.public_key(),
)?;
self.verified_csr = Some(verified);
Ok(())
}
/// Off-wire NOC issuance (M6.4.4 `Stage::GenerateNocChain`).
///
/// Consumes the [`crate::noc::VerifiedCsr`] populated by
/// [`Self::run_validate_csr`] and mints a NOC signed by the fabric's
/// RCAC via M6.3's `issue_noc`.
///
/// Validity window: M6.4 uses `(self.now, MatterTime::NO_EXPIRY)` —
/// the same convention `issue_noc`'s own unit test uses. M8 may
/// tighten this to a bounded operational-cert lifetime per Matter
/// Core Spec §6.4 once persistence + rotation policy lands.
///
/// CATs (CASE Authenticated Tags) are empty in M6.4; tag-based
/// access control comes later.
fn run_generate_noc_chain(&mut self) -> Result<(), CommissioningError> {
use crate::noc::issue_noc;
let verified = self
.verified_csr
.as_ref()
.ok_or(CommissioningError::OutOfOrderResponse(self.stage))?;
let noc = issue_noc(
&self.fabric,
verified,
self.assigned_node_id,
&[],
(self.now, MatterTime::NO_EXPIRY),
self.rng.as_ref(),
)?;
// Cache the NOC public key (the same bytes the verified CSR
// committed to) for later use — currently only consumed by
// M6.4.5's PASE -> CASE handoff in `CommissionedFabric`. Stored
// here so the SendNoc stage doesn't have to re-derive it.
self.issued_noc_public_key = Some(*verified.public_key.as_bytes());
self.issued_noc = Some(noc);
Ok(())
}
fn assert_tlv_well_formed(stage: Stage, payload: &[u8]) -> Result<(), CommissioningError> {
use matter_codec::{ContainerKind, Element, Tag, TlvReader};
let mut reader = TlvReader::new(payload);
match reader
.next()
.map_err(|_| CommissioningError::MalformedResponse(stage))?
{
Some(Element::ContainerStart {
tag: Tag::Anonymous,
kind: ContainerKind::Structure,
}) => {}
_ => return Err(CommissioningError::MalformedResponse(stage)),
}
// Walk to ContainerEnd; ignore contents for M6.4.1.
loop {
match reader
.next()
.map_err(|_| CommissioningError::MalformedResponse(stage))?
{
None => return Err(CommissioningError::MalformedResponse(stage)),
Some(Element::ContainerEnd) => return Ok(()),
Some(_) => {}
}
}
}
}
/// Test-only state seeds for [`Commissioner::position_at_stage_for_test`].
///
/// Each field is `None` by default — the caller opts in to each seed
/// explicitly. **Never use in production.**
#[cfg(feature = "__test_shortcuts")]
#[derive(Default, Debug, Clone, Copy)]
pub struct TestStateSeeds {
/// Override `issued_noc_public_key` (normally populated when the
/// state machine actually issues a NOC). Set to a synthetic SEC1-
/// uncompressed P-256 byte pattern (e.g. `[0xCC; 65]`) when
/// fast-forwarding past the NOC-issuance stages.
pub synthetic_noc_pubkey: Option<[u8; 65]>,
}
#[cfg(test)]
mod tests {
// Test-code carve-out: see CLAUDE.md.
#![allow(clippy::unwrap_used, clippy::expect_used)]
use super::*;
use crate::attestation::CdSigningRoots;
use crate::noc::{FabricRecord, NocRng, SystemNocRng};
use crate::setup::{
CommissioningFlow, DiscoveryCapabilities, Discriminator, Passcode, SetupPayload,
};
use crate::state_machine::{Action, Expectation};
use crate::PaaTrustStore;
use matter_cert::time::MatterTime;
use matter_crypto::{RingSigner, Signer};
use std::sync::Arc;
fn make_setup_payload() -> SetupPayload {
SetupPayload {
version: 0,
vendor_id: Some(0xFFF1),
product_id: Some(0x8000),
commissioning_flow: CommissioningFlow::Standard,
discovery_capabilities: DiscoveryCapabilities::ON_NETWORK,
discriminator: Discriminator::new(0x0F00).expect("valid discriminator"),
passcode: Passcode::new(20_202_021).expect("valid passcode"),
}
}
fn make_fabric_record() -> FabricRecord {
let (signer, _pkcs8) = RingSigner::generate().unwrap();
let signer: Arc<dyn Signer> = Arc::new(signer);
FabricRecord::new_root_only(
/* fabric_id */ 0x0000_0000_0000_0001,
signer,
/* not_before */ MatterTime::from_unix_secs(1_704_067_200),
/* not_after */ MatterTime::from_unix_secs(1_735_689_600),
/* rcac_id */ 42,
&SystemNocRng,
)
.unwrap()
}
fn base_config<'a>(
fabric: &'a FabricRecord,
setup: &'a SetupPayload,
paa: &'a PaaTrustStore,
cd: &'a crate::attestation::CdSigningRoots,
rng: Arc<dyn NocRng>,
) -> CommissionerConfig<'a> {
CommissionerConfig {
pase_attestation_challenge: [0u8; 16],
fabric,
setup_payload: setup,
paa_trust_store: paa,
cd_signing_roots: cd,
commissioner_node_id: 0x1,
assigned_node_id: 0x2,
ipk_epoch_key: [0x42_u8; 16],
case_admin_subject: 0x1,
admin_vendor_id: 0xFFF1,
now: MatterTime::from_unix_secs(1_704_067_200),
rng,
network: NetworkCredentials::AlreadyOnNetwork,
}
}
#[test]
fn new_rejects_zero_commissioner_node_id() {
let fabric = make_fabric_record();
let setup = make_setup_payload();
let paa = PaaTrustStore::with_csa_test_roots();
let cd = CdSigningRoots::with_csa_test_roots();
let rng: Arc<dyn NocRng> = Arc::new(SystemNocRng);
let mut cfg = base_config(&fabric, &setup, &paa, &cd, rng);
cfg.commissioner_node_id = 0;
// Cannot use `expect_err`: `Commissioner` does not impl Debug
// because `FabricRecord` (a stored field) is not Debug.
let Err(err) = Commissioner::new(cfg) else {
panic!("zero commissioner_node_id should fail");
};
assert!(
matches!(err, CommissioningError::InvalidConfig(_)),
"got {err:?}"
);
}
#[test]
fn new_rejects_zero_assigned_node_id() {
let fabric = make_fabric_record();
let setup = make_setup_payload();
let paa = PaaTrustStore::with_csa_test_roots();
let cd = CdSigningRoots::with_csa_test_roots();
let rng: Arc<dyn NocRng> = Arc::new(SystemNocRng);
let mut cfg = base_config(&fabric, &setup, &paa, &cd, rng);
cfg.assigned_node_id = 0;
let Err(err) = Commissioner::new(cfg) else {
panic!("zero assigned_node_id should fail");
};
assert!(
matches!(err, CommissioningError::InvalidConfig(_)),
"got {err:?}"
);
}
#[test]
fn new_rejects_equal_commissioner_and_assigned_ids() {
let fabric = make_fabric_record();
let setup = make_setup_payload();
let paa = PaaTrustStore::with_csa_test_roots();
let cd = CdSigningRoots::with_csa_test_roots();
let rng: Arc<dyn NocRng> = Arc::new(SystemNocRng);
let mut cfg = base_config(&fabric, &setup, &paa, &cd, rng);
cfg.commissioner_node_id = 0x42;
cfg.assigned_node_id = 0x42;
let Err(err) = Commissioner::new(cfg) else {
panic!("equal IDs should fail");
};
assert!(
matches!(err, CommissioningError::InvalidConfig(_)),
"got {err:?}"
);
}
#[test]
fn new_rejects_zero_ipk_epoch_key() {
let fabric = make_fabric_record();
let setup = make_setup_payload();
let paa = PaaTrustStore::with_csa_test_roots();
let cd = CdSigningRoots::with_csa_test_roots();
let rng: Arc<dyn NocRng> = Arc::new(SystemNocRng);
let mut cfg = base_config(&fabric, &setup, &paa, &cd, rng);
cfg.ipk_epoch_key = [0u8; 16];
let Err(err) = Commissioner::new(cfg) else {
panic!("zero IPK should fail");
};
assert!(
matches!(err, CommissioningError::InvalidConfig(_)),
"got {err:?}"
);
}
#[test]
fn new_returns_secure_pairing_stage() {
let fabric = make_fabric_record();
let setup = make_setup_payload();
let paa = PaaTrustStore::with_csa_test_roots();
let cd = CdSigningRoots::with_csa_test_roots();
let rng: Arc<dyn NocRng> = Arc::new(SystemNocRng);
let cfg = base_config(&fabric, &setup, &paa, &cd, rng);
let sm = Commissioner::new(cfg).expect("valid config should construct");
assert_eq!(sm.stage(), Stage::SecurePairing);
}
#[test]
fn poll_from_secure_pairing_emits_read_commissioning_info() {
let fabric = make_fabric_record();
let setup = make_setup_payload();
let paa = PaaTrustStore::with_csa_test_roots();
let cd = CdSigningRoots::with_csa_test_roots();
let rng: Arc<dyn NocRng> = Arc::new(SystemNocRng);
let cfg = base_config(&fabric, &setup, &paa, &cd, rng);
let mut sm = Commissioner::new(cfg).expect("valid config");
let act = sm.poll().expect("poll succeeds");
match act {
Action::ReadAttribute {
session,
endpoint,
cluster,
attributes,
expect,
} => {
assert_eq!(session, crate::state_machine::SessionContext::Pase);
assert_eq!(endpoint, 0);
assert_eq!(cluster, 0x0030);
assert_eq!(expect, Expectation::CommissioningInfo);
assert!(!attributes.is_empty());
}
other => panic!("expected ReadAttribute, got {other:?}"),
}
assert_eq!(sm.stage(), Stage::ReadCommissioningInfo);
}
#[test]
fn poll_is_idempotent_between_responses() {
let fabric = make_fabric_record();
let setup = make_setup_payload();
let paa = PaaTrustStore::with_csa_test_roots();
let cd = CdSigningRoots::with_csa_test_roots();
let rng: Arc<dyn NocRng> = Arc::new(SystemNocRng);
let cfg = base_config(&fabric, &setup, &paa, &cd, rng);
let mut sm = Commissioner::new(cfg).expect("valid config");
let act1 = sm.poll().expect("first poll");
let act2 = sm.poll().expect("second poll");
match (act1, act2) {
(
Action::ReadAttribute {
cluster: c1,
expect: e1,
..
},
Action::ReadAttribute {
cluster: c2,
expect: e2,
..
},
) => {
assert_eq!(c1, c2);
assert_eq!(e1, e2);
}
other => panic!("idempotent poll returned different variants: {other:?}"),
}
}
#[test]
fn full_happy_path_through_config_regulatory_lands_on_send_pai_cert_request() {
let fabric = make_fabric_record();
let setup = make_setup_payload();
let paa = PaaTrustStore::with_csa_test_roots();
let cd = CdSigningRoots::with_csa_test_roots();
let rng: Arc<dyn crate::noc::NocRng> = Arc::new(SystemNocRng);
let cfg = base_config(&fabric, &setup, &paa, &cd, rng);
let mut sm = Commissioner::new(cfg).expect("valid config");
// SecurePairing → ReadCommissioningInfo
let _ = sm.poll().expect("poll #1");
let canned_info = encode_read_commissioning_info_response();
sm.on_response(Expectation::CommissioningInfo, &canned_info)
.expect("commissioning info accepted");
assert_eq!(sm.stage(), Stage::ArmFailsafe);
// ArmFailsafe
let _ = sm.poll().expect("poll #2");
sm.on_response(
Expectation::ArmFailsafeResponse,
&[0x15, 0x24, 0x00, 0x00, 0x18],
)
.expect("arm failsafe ok");
assert_eq!(sm.stage(), Stage::ConfigRegulatory);
// ConfigRegulatory
let _ = sm.poll().expect("poll #3");
sm.on_response(
Expectation::SetRegulatoryConfigResponse,
&[0x15, 0x24, 0x00, 0x00, 0x18],
)
.expect("config regulatory ok");
assert_eq!(sm.stage(), Stage::SendPaiCertRequest);
// M6.4.2: SendPaiCertRequest now actually emits an Invoke.
match sm.poll().expect("poll #4") {
Action::Invoke {
cluster,
command,
expect,
..
} => {
assert_eq!(cluster, 0x003E);
assert_eq!(command, 0x02);
assert_eq!(expect, Expectation::PaiCertChainResponse);
}
other => panic!("expected Invoke, got {other:?}"),
}
}
#[test]
fn arm_failsafe_busy_response_aborts_with_device_im_status() {
let fabric = make_fabric_record();
let setup = make_setup_payload();
let paa = PaaTrustStore::with_csa_test_roots();
let cd = CdSigningRoots::with_csa_test_roots();
let rng: Arc<dyn crate::noc::NocRng> = Arc::new(SystemNocRng);
let cfg = base_config(&fabric, &setup, &paa, &cd, rng);
let mut sm = Commissioner::new(cfg).expect("valid config");
let _ = sm.poll().expect("poll info");
sm.on_response(
Expectation::CommissioningInfo,
&encode_read_commissioning_info_response(),
)
.expect("commissioning info ok");
let _ = sm.poll().expect("poll arm failsafe");
// Device returns BusyWithOtherAdmin: error_code = 4 (spec §11.10.5.1).
let err = sm
.on_response(
Expectation::ArmFailsafeResponse,
&[0x15, 0x24, 0x00, 0x04, 0x18],
)
.expect_err("busy should fail");
assert!(matches!(
err,
CommissioningError::DeviceImStatus {
stage: Stage::ArmFailsafe,
im_status: 4,
}
));
assert_eq!(sm.stage(), Stage::Failed);
match sm.poll().expect("abort emission") {
Action::Abort {
send_disarm_failsafe,
reason,
} => {
assert!(send_disarm_failsafe);
assert!(reason.contains("ArmFailsafe"), "reason was {reason}");
assert!(reason.contains("0x4"), "reason was {reason}");
}
other => panic!("expected Abort, got {other:?}"),
}
}
#[test]
fn out_of_order_response_returns_error_without_advancing() {
let fabric = make_fabric_record();
let setup = make_setup_payload();
let paa = PaaTrustStore::with_csa_test_roots();
let cd = CdSigningRoots::with_csa_test_roots();
let rng: Arc<dyn crate::noc::NocRng> = Arc::new(SystemNocRng);
let cfg = base_config(&fabric, &setup, &paa, &cd, rng);
let mut sm = Commissioner::new(cfg).expect("valid config");
// No poll called — state machine isn't waiting on anything.
let err = sm
.on_response(Expectation::ArmFailsafeResponse, &[])
.expect_err("should reject out-of-order");
assert!(matches!(err, CommissioningError::OutOfOrderResponse(_)));
assert_eq!(sm.stage(), Stage::SecurePairing);
}
#[test]
fn wrong_expectation_returns_unexpected_response_kind() {
let fabric = make_fabric_record();
let setup = make_setup_payload();
let paa = PaaTrustStore::with_csa_test_roots();
let cd = CdSigningRoots::with_csa_test_roots();
let rng: Arc<dyn crate::noc::NocRng> = Arc::new(SystemNocRng);
let cfg = base_config(&fabric, &setup, &paa, &cd, rng);
let mut sm = Commissioner::new(cfg).expect("valid config");
let _ = sm.poll().expect("poll");
let err = sm
.on_response(Expectation::ArmFailsafeResponse, &[])
.expect_err("wrong kind should fail");
assert!(matches!(
err,
CommissioningError::UnexpectedResponseKind {
expected: Expectation::CommissioningInfo,
got: Expectation::ArmFailsafeResponse,
}
));
// Wrong-kind does NOT advance the cursor.
assert_eq!(sm.stage(), Stage::ReadCommissioningInfo);
}
fn encode_read_commissioning_info_response() -> Vec<u8> {
// Minimal well-formed anonymous struct. M6.4.1 doesn't parse
// individual attributes yet.
vec![0x15, 0x18]
}
// --- M6.4.2 T18-T21: attestation flow tests ---
fn drive_to_send_pai_cert_request(sm: &mut Commissioner) {
let _ = sm.poll().expect("poll info");
sm.on_response(Expectation::CommissioningInfo, &[0x15, 0x18])
.expect("info ok");
let _ = sm.poll().expect("poll arm failsafe");
sm.on_response(
Expectation::ArmFailsafeResponse,
&[0x15, 0x24, 0x00, 0x00, 0x18],
)
.expect("arm ok");
let _ = sm.poll().expect("poll config regulatory");
sm.on_response(
Expectation::SetRegulatoryConfigResponse,
&[0x15, 0x24, 0x00, 0x00, 0x18],
)
.expect("regulatory ok");
}
fn synthetic_cert_chain_response(cert: &[u8]) -> Vec<u8> {
use matter_codec::{Tag, TlvWriter};
let mut buf = Vec::new();
let mut w = TlvWriter::new(&mut buf);
w.start_structure(Tag::Anonymous).expect("infallible");
w.put_bytes(Tag::Context(0), cert).expect("infallible");
w.end_container().expect("infallible");
buf
}
fn nonce_from_attestation_invoke(act: &Action) -> [u8; 32] {
match act {
Action::Invoke { payload, .. } => {
use matter_codec::{Element, Tag, TlvReader, Value};
let mut r = TlvReader::new(payload);
let _ = r.next().expect("reader").expect("anon-struct-start");
loop {
match r.next().expect("reader") {
Some(Element::Scalar {
tag: Tag::Context(0),
value: Value::Bytes(b),
}) => {
return b.as_slice().try_into().expect("32 bytes");
}
Some(_) => {}
None => panic!("no nonce found"),
}
}
}
other => panic!("expected Invoke, got {other:?}"),
}
}
#[test]
fn poll_at_send_pai_emits_certificate_chain_request_pai() {
let fabric = make_fabric_record();
let setup = make_setup_payload();
let paa = PaaTrustStore::with_csa_test_roots();
let cd = CdSigningRoots::with_csa_test_roots();
let rng: Arc<dyn crate::noc::NocRng> = Arc::new(SystemNocRng);
let cfg = base_config(&fabric, &setup, &paa, &cd, rng);
let mut sm = Commissioner::new(cfg).expect("valid config");
drive_to_send_pai_cert_request(&mut sm);
assert_eq!(sm.stage(), Stage::SendPaiCertRequest);
match sm.poll().expect("poll PAI") {
Action::Invoke {
cluster,
command,
expect,
payload,
..
} => {
assert_eq!(cluster, 0x003E);
assert_eq!(command, 0x02);
assert_eq!(expect, Expectation::PaiCertChainResponse);
// CertificateChainTypeEnum (spec §11.18.5.2): 2 = PAI.
assert_eq!(payload, vec![0x15, 0x24, 0x00, 0x02, 0x18]);
}
other => panic!("expected Invoke, got {other:?}"),
}
}
#[test]
fn poll_at_send_dac_emits_certificate_chain_request_dac() {
let fabric = make_fabric_record();
let setup = make_setup_payload();
let paa = PaaTrustStore::with_csa_test_roots();
let cd = CdSigningRoots::with_csa_test_roots();
let rng: Arc<dyn crate::noc::NocRng> = Arc::new(SystemNocRng);
let cfg = base_config(&fabric, &setup, &paa, &cd, rng);
let mut sm = Commissioner::new(cfg).expect("valid config");
drive_to_send_pai_cert_request(&mut sm);
let _ = sm.poll().expect("poll PAI");
let pai_response = synthetic_cert_chain_response(&[0xAA, 0xBB, 0xCC]);
sm.on_response(Expectation::PaiCertChainResponse, &pai_response)
.expect("PAI accepted");
assert_eq!(sm.stage(), Stage::SendDacCertRequest);
match sm.poll().expect("poll DAC") {
Action::Invoke {
cluster,
command,
expect,
payload,
..
} => {
assert_eq!(cluster, 0x003E);
assert_eq!(command, 0x02);
assert_eq!(expect, Expectation::DacCertChainResponse);
// CertificateChainTypeEnum (spec §11.18.5.2): 1 = DAC.
assert_eq!(payload, vec![0x15, 0x24, 0x00, 0x01, 0x18]);
}
other => panic!("expected Invoke, got {other:?}"),
}
}
#[test]
fn send_attestation_request_uses_fresh_random_nonce_each_time() {
let fabric = make_fabric_record();
let setup = make_setup_payload();
let paa = PaaTrustStore::with_csa_test_roots();
let cd = CdSigningRoots::with_csa_test_roots();
let rng_a: Arc<dyn crate::noc::NocRng> = Arc::new(SystemNocRng);
let cfg_a = base_config(&fabric, &setup, &paa, &cd, rng_a);
let mut sm_a = Commissioner::new(cfg_a).expect("valid config");
drive_to_send_pai_cert_request(&mut sm_a);
let _ = sm_a.poll().expect("poll PAI a");
let pai_response = synthetic_cert_chain_response(&[0xAA]);
sm_a.on_response(Expectation::PaiCertChainResponse, &pai_response)
.expect("ok");
let _ = sm_a.poll().expect("poll DAC a");
let dac_response = synthetic_cert_chain_response(&[0xBB]);
sm_a.on_response(Expectation::DacCertChainResponse, &dac_response)
.expect("ok");
let nonce_a = nonce_from_attestation_invoke(&sm_a.poll().expect("poll att a"));
let rng_b: Arc<dyn crate::noc::NocRng> = Arc::new(SystemNocRng);
let cfg_b = base_config(&fabric, &setup, &paa, &cd, rng_b);
let mut sm_b = Commissioner::new(cfg_b).expect("valid config");
drive_to_send_pai_cert_request(&mut sm_b);
let _ = sm_b.poll().expect("poll PAI b");
sm_b.on_response(Expectation::PaiCertChainResponse, &pai_response)
.expect("ok");
let _ = sm_b.poll().expect("poll DAC b");
sm_b.on_response(Expectation::DacCertChainResponse, &dac_response)
.expect("ok");
let nonce_b = nonce_from_attestation_invoke(&sm_b.poll().expect("poll att b"));
assert_ne!(
nonce_a, nonce_b,
"two independent runs should use different random nonces"
);
}
// --- M6.4.4 T35-T40: CSR + NOC issuance flow tests ---
/// Extract the 32-byte `CSRNonce` from a `CSRRequest` Invoke payload.
/// Mirrors `nonce_from_attestation_invoke` — same TLV shape, both
/// pull the bytes at context tag 0 inside the anonymous outer struct.
fn nonce_from_csr_invoke(act: &Action) -> [u8; 32] {
match act {
Action::Invoke { payload, .. } => {
use matter_codec::{Element, Tag, TlvReader, Value};
let mut r = TlvReader::new(payload);
let _ = r.next().expect("reader").expect("anon-struct-start");
loop {
match r.next().expect("reader") {
Some(Element::Scalar {
tag: Tag::Context(0),
value: Value::Bytes(b),
}) => {
return b.as_slice().try_into().expect("32 bytes");
}
Some(_) => {}
None => panic!("no nonce found"),
}
}
}
other => panic!("expected Invoke, got {other:?}"),
}
}
/// Glass-box test: jumps the cursor straight to
/// `Stage::SendOpCertSigningRequest` (bypassing PAI/DAC/Att, which
/// would otherwise demand real fixtures M6.4.2's verifier accepts)
/// and checks the emitted `CSRRequest` Invoke's nonce randomness
/// across two independent commissioner instances. The full
/// integration drive ships in T41 with real matter.js fixtures.
#[test]
fn send_op_cert_signing_request_emits_csr_with_random_nonce() {
let fabric = make_fabric_record();
let setup = make_setup_payload();
let paa = PaaTrustStore::with_csa_test_roots();
let cd = CdSigningRoots::with_csa_test_roots();
let rng_a: Arc<dyn crate::noc::NocRng> = Arc::new(SystemNocRng);
let cfg_a = base_config(&fabric, &setup, &paa, &cd, rng_a);
let mut sm_a = Commissioner::new(cfg_a).expect("valid config");
// Jump the cursor + plant the prerequisite DAC slot. Glass-box
// crate-private access is fine inside the in-module `tests`
// submodule.
sm_a.stage = Stage::SendOpCertSigningRequest;
sm_a.dac_der = Some(vec![0xAA, 0xBB]);
let act_a = sm_a.poll().expect("poll csr a");
let nonce_a = nonce_from_csr_invoke(&act_a);
let rng_b: Arc<dyn crate::noc::NocRng> = Arc::new(SystemNocRng);
let cfg_b = base_config(&fabric, &setup, &paa, &cd, rng_b);
let mut sm_b = Commissioner::new(cfg_b).expect("valid config");
sm_b.stage = Stage::SendOpCertSigningRequest;
sm_b.dac_der = Some(vec![0xAA, 0xBB]);
let act_b = sm_b.poll().expect("poll csr b");
let nonce_b = nonce_from_csr_invoke(&act_b);
assert_ne!(
nonce_a, nonce_b,
"two independent runs should use different CSR nonces"
);
match act_a {
Action::Invoke {
cluster,
command,
expect,
..
} => {
assert_eq!(cluster, 0x003E);
assert_eq!(command, 0x04);
assert_eq!(expect, Expectation::CsrResponse);
}
other => panic!("expected Invoke, got {other:?}"),
}
}
/// Glass-box test: with the CSR + NOC artefacts pre-populated and
/// the cursor placed at `Stage::SendNoc`, `poll()` must emit an
/// `AddNOC` Invoke targeting cluster `0x003E` / command `0x06`.
/// Then drive the synthetic `NOCResponse { status: 0 }` through
/// `on_response` and assert the cursor lands on
/// `Stage::ReadNetworkCommissioningInfo`.
#[test]
fn drive_through_send_noc_with_synthetic_noc_response() {
use matter_cert::{
BasicConstraints, DistinguishedName, DnAttribute, Extensions, MatterCertificate,
PublicKey,
};
let fabric = make_fabric_record();
let setup = make_setup_payload();
let paa = PaaTrustStore::with_csa_test_roots();
let cd = CdSigningRoots::with_csa_test_roots();
let rng: Arc<dyn crate::noc::NocRng> = Arc::new(SystemNocRng);
let cfg = base_config(&fabric, &setup, &paa, &cd, rng);
let mut sm = Commissioner::new(cfg).expect("valid config");
// Skip past attestation/CSR machinery — we want to test the
// SendNoc dispatch arm + the NOC response handler in isolation.
// Plant a structurally-valid synthetic NOC the AddNOC encoder
// can re-serialise. (The device side wouldn't accept it, but
// we're not talking to a device — we feed a canned response.)
let mut key_bytes = [0u8; 65];
key_bytes[0] = 0x04;
let synthetic_noc = MatterCertificate::builder()
.serial(vec![1, 2, 3])
.issuer(fabric.root_cert.subject().clone())
.subject(DistinguishedName::new(vec![
DnAttribute::FabricId(fabric.fabric_id),
DnAttribute::NodeId(0x2),
]))
.validity(
MatterTime::from_unix_secs(1_704_067_200),
MatterTime::NO_EXPIRY,
)
.public_key(PublicKey::new(key_bytes).expect("valid sec1 prefix"))
.extensions(
Extensions::builder()
.basic_constraints(Some(BasicConstraints::new(false, None)))
.build(),
)
.build_unsigned()
.expect("builder")
.assemble([0u8; 64]);
sm.stage = Stage::SendNoc;
sm.issued_noc = Some(synthetic_noc);
sm.issued_noc_public_key = Some(key_bytes);
match sm.poll().expect("poll SendNoc") {
Action::Invoke {
cluster,
command,
expect,
..
} => {
assert_eq!(cluster, 0x003E);
assert_eq!(command, 0x06);
assert_eq!(expect, Expectation::NocResponse);
}
other => panic!("expected Invoke, got {other:?}"),
}
// Synthetic NOCResponse: anonymous struct with status=0 + fabric_index=1.
let mut noc_response = Vec::new();
{
use matter_codec::{Tag, TlvWriter};
let mut w = TlvWriter::new(&mut noc_response);
w.start_structure(Tag::Anonymous).expect("infallible");
w.put_uint(Tag::Context(0), 0).expect("infallible"); // status = OK
w.put_uint(Tag::Context(1), 1).expect("infallible"); // fabric_index = 1
w.end_container().expect("infallible");
}
sm.on_response(Expectation::NocResponse, &noc_response)
.expect("NocResponse accepted");
assert_eq!(sm.stage(), Stage::ReadNetworkCommissioningInfo);
}
/// Glass-box test: a non-zero NOC status surfaces as
/// `CommissioningError::DeviceImStatus { stage: SendNoc, ... }`
/// and transitions the cursor to `Failed`.
#[test]
fn send_noc_failure_status_aborts_with_device_im_status() {
use matter_cert::{
BasicConstraints, DistinguishedName, DnAttribute, Extensions, MatterCertificate,
PublicKey,
};
let fabric = make_fabric_record();
let setup = make_setup_payload();
let paa = PaaTrustStore::with_csa_test_roots();
let cd = CdSigningRoots::with_csa_test_roots();
let rng: Arc<dyn crate::noc::NocRng> = Arc::new(SystemNocRng);
let cfg = base_config(&fabric, &setup, &paa, &cd, rng);
let mut sm = Commissioner::new(cfg).expect("valid config");
let mut key_bytes = [0u8; 65];
key_bytes[0] = 0x04;
let synthetic_noc = MatterCertificate::builder()
.serial(vec![9])
.issuer(fabric.root_cert.subject().clone())
.subject(DistinguishedName::new(vec![
DnAttribute::FabricId(fabric.fabric_id),
DnAttribute::NodeId(0x2),
]))
.validity(
MatterTime::from_unix_secs(1_704_067_200),
MatterTime::NO_EXPIRY,
)
.public_key(PublicKey::new(key_bytes).expect("valid sec1 prefix"))
.extensions(
Extensions::builder()
.basic_constraints(Some(BasicConstraints::new(false, None)))
.build(),
)
.build_unsigned()
.expect("builder")
.assemble([0u8; 64]);
sm.stage = Stage::SendNoc;
sm.issued_noc = Some(synthetic_noc);
sm.issued_noc_public_key = Some(key_bytes);
let _ = sm.poll().expect("poll SendNoc");
// status = 9 (InvalidNOC, spec §11.18.6.1).
let mut bad_response = Vec::new();
{
use matter_codec::{Tag, TlvWriter};
let mut w = TlvWriter::new(&mut bad_response);
w.start_structure(Tag::Anonymous).expect("infallible");
w.put_uint(Tag::Context(0), 9).expect("infallible");
w.end_container().expect("infallible");
}
let err = sm
.on_response(Expectation::NocResponse, &bad_response)
.expect_err("non-zero NOC status should fail");
assert!(matches!(
err,
CommissioningError::DeviceImStatus {
stage: Stage::SendNoc,
im_status: 9,
}
));
assert_eq!(sm.stage(), Stage::Failed);
}
/// Glass-box test: `SendTrustedRootCert` emits an `AddTrustedRootCertificate`
/// Invoke whose payload TLV starts with anonymous-struct + context-0
/// octet-string carrying the RCAC TLV bytes. A subsequent `[0x00]`
/// status-ack advances the cursor to `Stage::SendNoc`.
#[test]
fn send_trusted_root_cert_emits_invoke_and_status_ack_advances() {
let fabric = make_fabric_record();
let setup = make_setup_payload();
let paa = PaaTrustStore::with_csa_test_roots();
let cd = CdSigningRoots::with_csa_test_roots();
let rng: Arc<dyn crate::noc::NocRng> = Arc::new(SystemNocRng);
let cfg = base_config(&fabric, &setup, &paa, &cd, rng);
let mut sm = Commissioner::new(cfg).expect("valid config");
sm.stage = Stage::SendTrustedRootCert;
match sm.poll().expect("poll SendTrustedRootCert") {
Action::Invoke {
cluster,
command,
expect,
payload,
..
} => {
assert_eq!(cluster, 0x003E);
assert_eq!(command, 0x0B);
assert_eq!(expect, Expectation::AddTrustedRootResponse);
// Sanity: payload is at least the anonymous-struct
// wrapper + a non-trivial octet-string of RCAC TLV.
assert!(payload.len() > 16, "RCAC TLV too short: {}", payload.len());
assert_eq!(payload[0], 0x15); // anonymous struct start
}
other => panic!("expected Invoke, got {other:?}"),
}
// Status-ack of 0x00 (success) advances to SendNoc.
sm.on_response(Expectation::AddTrustedRootResponse, &[0x00])
.expect("status-ack accepted");
assert_eq!(sm.stage(), Stage::SendNoc);
}
// --- M6.4.5 T44-T47: PASE -> CASE handoff + CommissioningComplete tests ---
#[test]
fn find_operational_for_complete_emits_establish_case() {
let fabric = make_fabric_record();
let setup = make_setup_payload();
let paa = PaaTrustStore::with_csa_test_roots();
let cd = crate::attestation::CdSigningRoots::with_csa_test_roots();
let rng: Arc<dyn crate::noc::NocRng> = Arc::new(SystemNocRng);
let cfg = base_config(&fabric, &setup, &paa, &cd, rng);
let mut sm = Commissioner::new(cfg).expect("valid config");
sm.stage = Stage::FindOperationalForComplete;
match sm.poll().expect("poll establish case") {
Action::EstablishCase {
fabric_id,
peer_node_id,
} => {
assert_eq!(fabric_id, fabric.fabric_id);
assert_eq!(peer_node_id, 0x2); // matches base_config's assigned_node_id
}
other => panic!("expected EstablishCase, got {other:?}"),
}
assert!(sm.awaiting_case_session);
}
#[test]
fn on_case_established_advances_to_send_complete() {
let fabric = make_fabric_record();
let setup = make_setup_payload();
let paa = PaaTrustStore::with_csa_test_roots();
let cd = crate::attestation::CdSigningRoots::with_csa_test_roots();
let rng: Arc<dyn crate::noc::NocRng> = Arc::new(SystemNocRng);
let cfg = base_config(&fabric, &setup, &paa, &cd, rng);
let mut sm = Commissioner::new(cfg).expect("valid config");
sm.stage = Stage::FindOperationalForComplete;
let _ = sm.poll().expect("emit EstablishCase");
sm.on_case_established().expect("case established");
assert_eq!(sm.stage(), Stage::SendComplete);
assert!(!sm.awaiting_case_session);
}
#[test]
fn on_case_established_without_pending_emits_out_of_order() {
let fabric = make_fabric_record();
let setup = make_setup_payload();
let paa = PaaTrustStore::with_csa_test_roots();
let cd = crate::attestation::CdSigningRoots::with_csa_test_roots();
let rng: Arc<dyn crate::noc::NocRng> = Arc::new(SystemNocRng);
let cfg = base_config(&fabric, &setup, &paa, &cd, rng);
let mut sm = Commissioner::new(cfg).expect("valid config");
let err = sm.on_case_established().expect_err("no pending establish");
assert!(matches!(err, CommissioningError::OutOfOrderResponse(_)));
}
#[test]
fn send_complete_emits_invoke_over_case_session() {
let fabric = make_fabric_record();
let setup = make_setup_payload();
let paa = PaaTrustStore::with_csa_test_roots();
let cd = crate::attestation::CdSigningRoots::with_csa_test_roots();
let rng: Arc<dyn crate::noc::NocRng> = Arc::new(SystemNocRng);
let cfg = base_config(&fabric, &setup, &paa, &cd, rng);
let mut sm = Commissioner::new(cfg).expect("valid config");
sm.stage = Stage::SendComplete;
match sm.poll().expect("poll send complete") {
Action::Invoke {
session,
cluster,
command,
expect,
payload,
..
} => {
assert_eq!(session, crate::state_machine::SessionContext::Case);
assert_eq!(cluster, 0x0030);
assert_eq!(command, 0x04); // CommissioningComplete
assert_eq!(expect, Expectation::CommissioningCompleteResponse);
// CommissioningComplete carries no payload fields — empty struct.
assert_eq!(payload, vec![0x15, 0x18]);
}
other => panic!("expected Invoke, got {other:?}"),
}
}
#[test]
fn send_complete_success_advances_to_cleanup() {
let fabric = make_fabric_record();
let setup = make_setup_payload();
let paa = PaaTrustStore::with_csa_test_roots();
let cd = crate::attestation::CdSigningRoots::with_csa_test_roots();
let rng: Arc<dyn crate::noc::NocRng> = Arc::new(SystemNocRng);
let cfg = base_config(&fabric, &setup, &paa, &cd, rng);
let mut sm = Commissioner::new(cfg).expect("valid config");
sm.stage = Stage::SendComplete;
let _ = sm.poll().expect("emit invoke");
sm.on_response(
Expectation::CommissioningCompleteResponse,
&[0x15, 0x24, 0x00, 0x00, 0x18], // error_code = 0
)
.expect("complete ok");
assert_eq!(sm.stage(), Stage::Cleanup);
}
#[test]
fn cleanup_emits_done_with_noc_public_key() {
let fabric = make_fabric_record();
let setup = make_setup_payload();
let paa = PaaTrustStore::with_csa_test_roots();
let cd = crate::attestation::CdSigningRoots::with_csa_test_roots();
let rng: Arc<dyn crate::noc::NocRng> = Arc::new(SystemNocRng);
let cfg = base_config(&fabric, &setup, &paa, &cd, rng);
let mut sm = Commissioner::new(cfg).expect("valid config");
sm.stage = Stage::Cleanup;
sm.issued_noc_public_key = Some([0xCA; 65]);
match sm.poll().expect("poll cleanup") {
Action::Done(cf) => {
assert_eq!(cf.peer_node_id, 0x2);
assert_eq!(cf.peer_root_public_key, [0xCA; 65]);
assert_eq!(cf.terminated_at, Stage::Cleanup);
assert_eq!(cf.fabric.fabric_id, fabric.fabric_id);
}
other => panic!("expected Done, got {other:?}"),
}
}
// --- M6.4.5 T49: CaseFailed negative coverage ---
#[test]
fn case_failed_response_aborts_with_case_establishment_failed() {
let fabric = make_fabric_record();
let setup = make_setup_payload();
let paa = PaaTrustStore::with_csa_test_roots();
let cd = crate::attestation::CdSigningRoots::with_csa_test_roots();
let rng: Arc<dyn crate::noc::NocRng> = Arc::new(SystemNocRng);
let cfg = base_config(&fabric, &setup, &paa, &cd, rng);
let mut sm = Commissioner::new(cfg).expect("valid config");
// Glass-box: jump to FindOperationalForComplete (skipping the
// attestation + CSR + NOC stages that need real fixtures).
sm.stage = Stage::FindOperationalForComplete;
let _ = sm.poll().expect("emit EstablishCase");
assert!(sm.awaiting_case_session);
// Caller signals CASE establishment failure.
let err = sm
.on_response(Expectation::CaseFailed, &[])
.expect_err("CaseFailed should error");
assert!(matches!(err, CommissioningError::CaseEstablishmentFailed));
assert_eq!(sm.stage(), Stage::Failed);
assert!(!sm.awaiting_case_session);
// Subsequent poll emits Action::Abort with send_disarm_failsafe=true.
match sm.poll().expect("emit abort") {
Action::Abort {
send_disarm_failsafe,
reason,
} => {
assert!(send_disarm_failsafe);
assert!(
reason.contains("CASE"),
"abort reason should mention CASE: {reason}"
);
}
other => panic!("expected Abort, got {other:?}"),
}
}
#[test]
fn case_failed_when_not_awaiting_returns_out_of_order() {
let fabric = make_fabric_record();
let setup = make_setup_payload();
let paa = PaaTrustStore::with_csa_test_roots();
let cd = crate::attestation::CdSigningRoots::with_csa_test_roots();
let rng: Arc<dyn crate::noc::NocRng> = Arc::new(SystemNocRng);
let cfg = base_config(&fabric, &setup, &paa, &cd, rng);
let mut sm = Commissioner::new(cfg).expect("valid config");
let err = sm
.on_response(Expectation::CaseFailed, &[])
.expect_err("CaseFailed without pending should error");
assert!(matches!(err, CommissioningError::OutOfOrderResponse(_)));
}
/// Returns a fully-populated, valid [`CommissionerConfig`] for use in
/// unit tests that only need to mutate one field. All held references
/// are leaked so the config is `'static`; acceptable for test code.
fn sample_valid_config() -> CommissionerConfig<'static> {
use std::sync::OnceLock;
static FABRIC: OnceLock<FabricRecord> = OnceLock::new();
static SETUP: OnceLock<SetupPayload> = OnceLock::new();
static PAA: OnceLock<PaaTrustStore> = OnceLock::new();
static CD: OnceLock<crate::attestation::CdSigningRoots> = OnceLock::new();
let fabric = FABRIC.get_or_init(make_fabric_record);
let setup = SETUP.get_or_init(make_setup_payload);
let paa = PAA.get_or_init(PaaTrustStore::with_csa_test_roots);
let cd = CD.get_or_init(crate::attestation::CdSigningRoots::with_csa_test_roots);
let rng: Arc<dyn NocRng> = Arc::new(SystemNocRng);
CommissionerConfig {
pase_attestation_challenge: [0u8; 16],
fabric,
setup_payload: setup,
paa_trust_store: paa,
cd_signing_roots: cd,
commissioner_node_id: 0x1,
assigned_node_id: 0x2,
ipk_epoch_key: [0x42_u8; 16],
case_admin_subject: 0x1,
admin_vendor_id: 0xFFF1,
now: MatterTime::from_unix_secs(1_704_067_200),
rng,
network: NetworkCredentials::AlreadyOnNetwork,
}
}
#[test]
fn empty_ssid_is_rejected() {
let mut config = sample_valid_config();
config.network = NetworkCredentials::WiFi(WiFiCredentials {
ssid: vec![],
credentials: vec![],
});
let Err(err) = Commissioner::new(config) else {
panic!("empty ssid should fail");
};
assert!(
matches!(err, CommissioningError::InvalidConfig(m) if m.contains("ssid")),
"got {err:?}",
);
}
#[test]
fn oversize_ssid_is_rejected() {
let mut config = sample_valid_config();
config.network = NetworkCredentials::WiFi(WiFiCredentials {
ssid: vec![b'a'; 33],
credentials: vec![],
});
let Err(err) = Commissioner::new(config) else {
panic!("33-byte ssid should fail");
};
assert!(
matches!(err, CommissioningError::InvalidConfig(m) if m.contains("≤32")),
"got {err:?}",
);
}
#[test]
fn oversize_credentials_is_rejected() {
let mut config = sample_valid_config();
config.network = NetworkCredentials::WiFi(WiFiCredentials {
ssid: b"matter".to_vec(),
credentials: vec![0u8; 65],
});
let Err(err) = Commissioner::new(config) else {
panic!("65-byte credentials should fail");
};
assert!(
matches!(err, CommissioningError::InvalidConfig(m) if m.contains("≤64")),
"got {err:?}",
);
}
#[test]
fn wifi_credentials_none_is_accepted() {
let mut config = sample_valid_config();
config.network = NetworkCredentials::AlreadyOnNetwork;
Commissioner::new(config).expect("AlreadyOnNetwork should pass validation");
}
#[test]
fn network_credentials_thread_variant_accepted() {
// Minimal well-formed dataset: a single Extended PAN ID TLV
// (type 0x02, length 8). ThreadDataset::new self-validates.
let ds =
crate::thread_dataset::ThreadDataset::new(vec![0x02, 0x08, 0, 0, 0, 0, 0, 0, 0, 0])
.expect("minimal ext-pan-id dataset is valid");
let mut config = sample_valid_config();
config.network = NetworkCredentials::Thread(ds);
let c = Commissioner::new(config).expect("Thread network should pass validation");
assert!(matches!(c.network(), NetworkCredentials::Thread(_)));
}
#[test]
fn wifi_credentials_debug_redacts_passphrase() {
let creds = WiFiCredentials {
ssid: b"matter".to_vec(),
credentials: b"hunter22".to_vec(),
};
let rendered = format!("{creds:?}");
assert!(
!rendered.contains("hunter22"),
"Debug must not contain credentials bytes: {rendered}",
);
assert!(rendered.contains("redacted"), "got {rendered}");
assert!(
rendered.contains('8'),
"credentials length should appear: {rendered}"
);
assert!(
rendered.contains('6'),
"ssid length should appear: {rendered}"
);
}
// --- M6.5.2 T14: failsafe expiry derivation from BasicCommissioningInfo ---
#[test]
fn failsafe_expiry_derives_from_basic_commissioning_info() {
let mut sm = Commissioner::new(sample_valid_config()).expect("valid config");
// Advance to ReadCommissioningInfo and feed a BasicCommissioningInfo with 120s.
let _initial = sm.poll().expect("initial poll");
let response = vec![
0x15, 0x25, 0x00, 0x78, 0x00, // u16 = 120
0x18,
];
sm.on_response(Expectation::CommissioningInfo, &response)
.expect("commissioning info accepted");
// Now ArmFailsafe should emit with expiry=120.
let action = sm.poll().expect("arm-failsafe poll");
match action {
Action::Invoke {
payload,
cluster,
command,
..
} => {
assert_eq!(cluster, 0x0030);
assert_eq!(command, 0x00);
// ArmFailSafe payload byte for expiry: TLV-encoded u8/u16 at context tag 0.
// For value 120 the smallest-width encoding is u8 = 0x24 0x00 0x78.
assert!(
payload.windows(3).any(|w| w == [0x24, 0x00, 0x78]),
"ArmFailSafe payload should carry expiry=120: {payload:02x?}",
);
}
other => panic!("expected Invoke, got {other:?}"),
}
}
#[test]
fn failsafe_expiry_falls_back_to_60_on_empty_basic_commissioning_info() {
let mut sm = Commissioner::new(sample_valid_config()).expect("valid config");
let _initial = sm.poll().expect("initial poll");
// Feed a well-formed empty struct — decode_basic_commissioning_info
// returns None when the failsafe field is missing, so the M6.4
// fallback of 60s applies.
sm.on_response(Expectation::CommissioningInfo, &[0x15, 0x18])
.expect("empty struct accepted");
let action = sm.poll().expect("arm-failsafe poll");
if let Action::Invoke { payload, .. } = action {
// 60 = 0x3C, anonymous struct with context-tag-0 u8.
assert!(
payload.windows(3).any(|w| w == [0x24, 0x00, 0x3C]),
"ArmFailSafe payload should carry expiry=60 fallback: {payload:02x?}",
);
}
}
// --- M6.5.2 T15: breadcrumb monotonicity ---
#[test]
fn breadcrumb_increases_across_commands() {
fn extract_uint_at_tag(payload: &[u8], tag_num: u8) -> Option<u64> {
use matter_codec::{Element, Tag, TlvReader, Value};
let mut reader = TlvReader::new(payload);
while let Ok(Some(elem)) = reader.next() {
if let Element::Scalar {
tag: Tag::Context(t),
value: Value::Uint(v),
} = elem
{
if t == tag_num {
return Some(v);
}
}
}
None
}
let mut sm = Commissioner::new(sample_valid_config()).expect("valid config");
let mut breadcrumbs: Vec<u64> = Vec::new();
// Poll #1: ReadCommissioningInfo (no breadcrumb).
let _ = sm.poll().expect("read commissioning info");
sm.on_response(Expectation::CommissioningInfo, &[0x15, 0x18])
.expect("info accepted");
// Poll #2: ArmFailsafe — first breadcrumb-bearing command.
let action = sm.poll().expect("arm-failsafe");
if let Action::Invoke { payload, .. } = action {
if let Some(b) = extract_uint_at_tag(&payload, 1) {
breadcrumbs.push(b);
}
}
sm.on_response(
Expectation::ArmFailsafeResponse,
&[0x15, 0x24, 0x00, 0x00, 0x18],
)
.expect("arm-failsafe ok");
// Poll #3: SetRegulatoryConfig — second breadcrumb-bearing command.
let action = sm.poll().expect("set-regulatory");
if let Action::Invoke { payload, .. } = action {
if let Some(b) = extract_uint_at_tag(&payload, 2) {
breadcrumbs.push(b);
}
}
assert_eq!(
breadcrumbs.len(),
2,
"should have extracted exactly two breadcrumbs, got {breadcrumbs:?}",
);
assert!(
breadcrumbs[0] < breadcrumbs[1],
"breadcrumbs should be strictly increasing: {breadcrumbs:?}",
);
}
}