Skip to content

Commit 707d370

Browse files
committed
NOC CAT: The CAT IDs in the NoC are only 32-bit, account for that
1 parent 0149d30 commit 707d370

File tree

5 files changed

+30
-21
lines changed

5 files changed

+30
-21
lines changed

matter/src/acl.rs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,10 @@ fn get_noc_cat_version(id: u64) -> u64 {
9090
id & NOC_CAT_VERSION_MASK
9191
}
9292

93-
pub fn gen_noc_cat(id: u16, version: u16) -> u64 {
94-
NOC_CAT_SUBJECT_PREFIX | ((id as u64) << 16) | version as u64
93+
/// Generate CAT that is embeddedable in the NoC
94+
/// This only generates the 32-bit CAT ID
95+
pub fn gen_noc_cat(id: u16, version: u16) -> u32 {
96+
((id as u32) << 16) | version as u32
9597
}
9698

9799
pub struct AccessorSubjects([u64; MAX_ACCESSOR_SUBJECTS]);
@@ -103,10 +105,10 @@ impl AccessorSubjects {
103105
a
104106
}
105107

106-
pub fn add(&mut self, subject: u64) -> Result<(), Error> {
108+
pub fn add_catid(&mut self, subject: u32) -> Result<(), Error> {
107109
for (i, val) in self.0.iter().enumerate() {
108110
if *val == 0 {
109-
self.0[i] = subject;
111+
self.0[i] = NOC_CAT_SUBJECT_PREFIX | (subject as u64);
110112
return Ok(());
111113
}
112114
}
@@ -287,6 +289,10 @@ impl AclEntry {
287289
Ok(())
288290
}
289291

292+
pub fn add_subject_catid(&mut self, cat_id: u32) -> Result<(), Error> {
293+
self.add_subject(NOC_CAT_SUBJECT_PREFIX | cat_id as u64)
294+
}
295+
290296
pub fn add_target(&mut self, target: Target) -> Result<(), Error> {
291297
let index = self
292298
.targets
@@ -650,7 +656,7 @@ mod tests {
650656
let v3 = 3;
651657
// Accessor has nodeif and CAT 0xABCD_0002
652658
let mut subjects = AccessorSubjects::new(112233);
653-
subjects.add(gen_noc_cat(allow_cat, v2)).unwrap();
659+
subjects.add_catid(gen_noc_cat(allow_cat, v2)).unwrap();
654660

655661
let accessor = Accessor::new(2, subjects, AuthMode::Case, am.clone());
656662
let path = GenericPath::new(Some(1), Some(1234), None);
@@ -659,19 +665,20 @@ mod tests {
659665

660666
// Deny for CAT id mismatch
661667
let mut new = AclEntry::new(2, Privilege::VIEW, AuthMode::Case);
662-
new.add_subject(gen_noc_cat(disallow_cat, v2)).unwrap();
668+
new.add_subject_catid(gen_noc_cat(disallow_cat, v2))
669+
.unwrap();
663670
am.add(new).unwrap();
664671
assert_eq!(req.allow(), false);
665672

666673
// Deny of CAT version mismatch
667674
let mut new = AclEntry::new(2, Privilege::VIEW, AuthMode::Case);
668-
new.add_subject(gen_noc_cat(allow_cat, v3)).unwrap();
675+
new.add_subject_catid(gen_noc_cat(allow_cat, v3)).unwrap();
669676
am.add(new).unwrap();
670677
assert_eq!(req.allow(), false);
671678

672679
// Allow for CAT match
673680
let mut new = AclEntry::new(2, Privilege::VIEW, AuthMode::Case);
674-
new.add_subject(gen_noc_cat(allow_cat, v2)).unwrap();
681+
new.add_subject_catid(gen_noc_cat(allow_cat, v2)).unwrap();
675682
am.add(new).unwrap();
676683
assert_eq!(req.allow(), true);
677684
}
@@ -687,7 +694,7 @@ mod tests {
687694
let v3 = 3;
688695
// Accessor has nodeif and CAT 0xABCD_0003
689696
let mut subjects = AccessorSubjects::new(112233);
690-
subjects.add(gen_noc_cat(allow_cat, v3)).unwrap();
697+
subjects.add_catid(gen_noc_cat(allow_cat, v3)).unwrap();
691698

692699
let accessor = Accessor::new(2, subjects, AuthMode::Case, am.clone());
693700
let path = GenericPath::new(Some(1), Some(1234), None);
@@ -696,13 +703,14 @@ mod tests {
696703

697704
// Deny for CAT id mismatch
698705
let mut new = AclEntry::new(2, Privilege::VIEW, AuthMode::Case);
699-
new.add_subject(gen_noc_cat(disallow_cat, v2)).unwrap();
706+
new.add_subject_catid(gen_noc_cat(disallow_cat, v2))
707+
.unwrap();
700708
am.add(new).unwrap();
701709
assert_eq!(req.allow(), false);
702710

703711
// Allow for CAT match and version more than ACL version
704712
let mut new = AclEntry::new(2, Privilege::VIEW, AuthMode::Case);
705-
new.add_subject(gen_noc_cat(allow_cat, v2)).unwrap();
713+
new.add_subject_catid(gen_noc_cat(allow_cat, v2)).unwrap();
706714
am.add(new).unwrap();
707715
assert_eq!(req.allow(), true);
708716
}

matter/src/cert/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -319,12 +319,13 @@ impl DistNames {
319319
})
320320
}
321321

322-
fn u64_arr(&self, match_id: DnTags, output: &mut [u64]) {
322+
fn u32_arr(&self, match_id: DnTags, output: &mut [u32]) {
323323
let mut out_index = 0;
324324
for (_, val) in self.dn.iter().filter(|(id, _)| *id == match_id as u8) {
325325
if let DistNameValue::Uint(a) = val {
326326
if out_index < output.len() {
327-
output[out_index] = *a;
327+
// CatIds are actually just 32-bit
328+
output[out_index] = *a as u32;
328329
out_index += 1;
329330
}
330331
}
@@ -555,8 +556,8 @@ impl Cert {
555556
self.subject.u64(DnTags::NodeId).ok_or(Error::NoNodeId)
556557
}
557558

558-
pub fn get_cat_ids(&self, output: &mut [u64]) {
559-
self.subject.u64_arr(DnTags::NocCat, output)
559+
pub fn get_cat_ids(&self, output: &mut [u32]) {
560+
self.subject.u32_arr(DnTags::NocCat, output)
560561
}
561562

562563
pub fn get_fabric_id(&self) -> Result<u64, Error> {

matter/src/data_model/core.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ impl DataModel {
224224
AccessorSubjects::new(sess.get_peer_node_id().unwrap_or_default());
225225
for i in c.cat_ids {
226226
if i != 0 {
227-
let _ = subject.add(i);
227+
let _ = subject.add_catid(i);
228228
}
229229
}
230230
Accessor::new(c.fab_idx, subject, AuthMode::Case, self.acl_mgr.clone())

matter/src/transport/session.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,18 @@ use super::{
3838
};
3939

4040
pub const MAX_CAT_IDS_PER_NOC: usize = 3;
41+
pub type NocCatIds = [u32; MAX_CAT_IDS_PER_NOC];
42+
4143
const MATTER_AES128_KEY_SIZE: usize = 16;
4244

4345
#[derive(Debug, Default, Copy, Clone, PartialEq)]
4446
pub struct CaseDetails {
4547
pub fab_idx: u8,
46-
pub cat_ids: [u64; MAX_CAT_IDS_PER_NOC],
48+
pub cat_ids: NocCatIds,
4749
}
4850

4951
impl CaseDetails {
50-
pub fn new(fab_idx: u8, cat_ids: &[u64; MAX_CAT_IDS_PER_NOC]) -> Self {
52+
pub fn new(fab_idx: u8, cat_ids: &NocCatIds) -> Self {
5153
Self {
5254
fab_idx,
5355
cat_ids: *cat_ids,
@@ -69,8 +71,6 @@ impl Default for SessionMode {
6971
}
7072
}
7173

72-
pub type NocCatIds = [u64; MAX_CAT_IDS_PER_NOC];
73-
7474
#[derive(Debug)]
7575
pub struct Session {
7676
peer_addr: Address,

matter/tests/data_model/acl_and_dataver.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ fn exact_write_attribute_noc_cat() {
420420

421421
// Add ACL to allow our peer to access any endpoint
422422
let mut acl = AclEntry::new(1, Privilege::ADMIN, AuthMode::Case);
423-
acl.add_subject(cat_in_acl).unwrap();
423+
acl.add_subject_catid(cat_in_acl).unwrap();
424424
im.acl_mgr.add(acl).unwrap();
425425

426426
// Test 1: Exact write to an attribute with permission should grant

0 commit comments

Comments
 (0)