Skip to content

Commit 227bb77

Browse files
authored
Merge pull request #81 from kedars/bugfix/multiple_ios_fixes
Multiple fixes for iOS support
2 parents e02b316 + 21f0bb4 commit 227bb77

File tree

13 files changed

+663
-44
lines changed

13 files changed

+663
-44
lines changed

rs-matter/src/cert/asn1_writer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,8 @@ impl<'a> CertConsumer for ASN1Writer<'a> {
264264
self.write_str(0x06, oid)
265265
}
266266

267-
fn utctime(&mut self, _tag: &str, epoch: u32) -> Result<(), Error> {
268-
let matter_epoch = MATTER_EPOCH_SECS + epoch as u64;
267+
fn utctime(&mut self, _tag: &str, epoch: u64) -> Result<(), Error> {
268+
let matter_epoch = MATTER_EPOCH_SECS + epoch;
269269

270270
let dt = OffsetDateTime::from_unix_timestamp(matter_epoch as _).unwrap();
271271

rs-matter/src/cert/mod.rs

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use crate::{
2121
crypto::KeyPair,
2222
error::{Error, ErrorCode},
2323
tlv::{self, FromTLV, OctetStr, TLVArray, TLVElement, TLVWriter, TagType, ToTLV},
24-
utils::writebuf::WriteBuf,
24+
utils::{epoch::MATTER_CERT_DOESNT_EXPIRE, writebuf::WriteBuf},
2525
};
2626
use log::error;
2727
use num_derive::FromPrimitive;
@@ -650,8 +650,14 @@ impl<'a> Cert<'a> {
650650
self.issuer.encode("Issuer:", w)?;
651651

652652
w.start_seq("Validity:")?;
653-
w.utctime("Not Before:", self.not_before)?;
654-
w.utctime("Not After:", self.not_after)?;
653+
w.utctime("Not Before:", self.not_before.into())?;
654+
if self.not_after == 0 {
655+
// As per the spec a Not-After value of 0, indicates no well-defined
656+
// expiration date and should return in GeneralizedTime of 99991231235959Z
657+
w.utctime("Not After:", MATTER_CERT_DOESNT_EXPIRE)?;
658+
} else {
659+
w.utctime("Not After:", self.not_after.into())?;
660+
}
655661
w.end_seq()?;
656662

657663
self.subject.encode("Subject:", w)?;
@@ -710,8 +716,9 @@ impl<'a> CertVerifier<'a> {
710716
let k = KeyPair::new_from_public(parent.get_pubkey())?;
711717
k.verify_msg(asn1, self.cert.get_signature()).map_err(|e| {
712718
error!(
713-
"Error in signature verification of certificate: {:x?}",
714-
self.cert.get_subject_key_id()
719+
"Error in signature verification of certificate: {:x?} by {:x?}",
720+
self.cert.get_subject_key_id(),
721+
parent.get_subject_key_id()
715722
);
716723
e
717724
})?;
@@ -744,7 +751,7 @@ pub trait CertConsumer {
744751
fn start_ctx(&mut self, tag: &str, id: u8) -> Result<(), Error>;
745752
fn end_ctx(&mut self) -> Result<(), Error>;
746753
fn oid(&mut self, tag: &str, oid: &[u8]) -> Result<(), Error>;
747-
fn utctime(&mut self, tag: &str, epoch: u32) -> Result<(), Error>;
754+
fn utctime(&mut self, tag: &str, epoch: u64) -> Result<(), Error>;
748755
}
749756

750757
const MAX_DEPTH: usize = 10;
@@ -826,6 +833,16 @@ mod tests {
826833
);
827834
}
828835

836+
#[test]
837+
fn test_zero_value_of_not_after_field() {
838+
let noc = Cert::new(&test_vectors::NOC_NOT_AFTER_ZERO).unwrap();
839+
let rca = Cert::new(&test_vectors::RCA_FOR_NOC_NOT_AFTER_ZERO).unwrap();
840+
841+
let v = noc.verify_chain_start();
842+
let v = v.add_cert(&rca).unwrap();
843+
v.finalise().unwrap();
844+
}
845+
829846
#[test]
830847
fn test_cert_corrupted() {
831848
use crate::error::ErrorCode;
@@ -1112,5 +1129,47 @@ mod tests {
11121129
0x16, 0x80, 0x14, 0x72, 0xc2, 0x01, 0xf7, 0x57, 0x19, 0x13, 0xb3, 0x48, 0xca, 0x00,
11131130
0xca, 0x7b, 0x45, 0xf4, 0x77, 0x46, 0x68, 0xc9, 0x7e,
11141131
];
1132+
1133+
/// An NOC that contains a Not-After validity field of '0'
1134+
pub const NOC_NOT_AFTER_ZERO: [u8; 251] = [
1135+
0x15, 0x30, 0x1, 0x1, 0x1, 0x24, 0x2, 0x1, 0x37, 0x3, 0x27, 0x14, 0xfc, 0x8d, 0xcf,
1136+
0x45, 0x19, 0xff, 0x9a, 0x9a, 0x24, 0x15, 0x1, 0x18, 0x26, 0x4, 0x21, 0x39, 0x5a, 0x2c,
1137+
0x24, 0x5, 0x0, 0x37, 0x6, 0x24, 0x15, 0x1, 0x26, 0x11, 0x6c, 0x4a, 0x95, 0xd2, 0x18,
1138+
0x24, 0x7, 0x1, 0x24, 0x8, 0x1, 0x30, 0x9, 0x41, 0x4, 0x41, 0x7f, 0xb1, 0x61, 0xb0,
1139+
0xbe, 0x19, 0x41, 0x81, 0xb9, 0x9f, 0xe8, 0x7b, 0xdd, 0xdf, 0xc4, 0x46, 0xe0, 0x74,
1140+
0xba, 0x83, 0x21, 0xda, 0x3d, 0xf7, 0x88, 0x68, 0x14, 0xa6, 0x9d, 0xa9, 0x14, 0x88,
1141+
0x94, 0x1e, 0xd3, 0x86, 0x62, 0xc7, 0x6f, 0xb4, 0x79, 0xd2, 0xaf, 0x34, 0xe7, 0xd6,
1142+
0x4d, 0x87, 0x29, 0x67, 0x10, 0x73, 0xb9, 0x81, 0xe0, 0x9, 0xe1, 0x13, 0xbb, 0x6a,
1143+
0xd2, 0x21, 0xaa, 0x37, 0xa, 0x35, 0x1, 0x28, 0x1, 0x18, 0x24, 0x2, 0x1, 0x36, 0x3,
1144+
0x4, 0x2, 0x4, 0x1, 0x18, 0x30, 0x4, 0x14, 0x98, 0xaf, 0xa1, 0x3d, 0x41, 0x67, 0x7a,
1145+
0x34, 0x8c, 0x67, 0x6c, 0xcc, 0x17, 0x6e, 0xd5, 0x58, 0xd8, 0x2b, 0x86, 0x8, 0x30, 0x5,
1146+
0x14, 0xf8, 0xcf, 0xd0, 0x45, 0x6b, 0xe, 0xd1, 0x6f, 0xc5, 0x67, 0xdf, 0x81, 0xd7,
1147+
0xe9, 0xb7, 0xeb, 0x39, 0x78, 0xec, 0x40, 0x18, 0x30, 0xb, 0x40, 0xf9, 0x80, 0x94,
1148+
0xbf, 0xcf, 0x72, 0xa5, 0x54, 0x87, 0x12, 0x35, 0xc, 0x38, 0x79, 0xa8, 0xb, 0x21, 0x94,
1149+
0xb5, 0x71, 0x2, 0xcb, 0xb, 0xda, 0xf9, 0x6c, 0x54, 0xcb, 0x50, 0x4b, 0x2, 0x5, 0xea,
1150+
0xff, 0xfd, 0xb2, 0x1b, 0x24, 0x30, 0x79, 0xb1, 0x69, 0x87, 0xa5, 0x7, 0xc6, 0x76,
1151+
0x15, 0x70, 0xc0, 0xec, 0x14, 0xd3, 0x9f, 0x1a, 0xa7, 0xe1, 0xca, 0x25, 0x2e, 0x44,
1152+
0xfc, 0x96, 0x4d, 0x18,
1153+
];
1154+
pub const RCA_FOR_NOC_NOT_AFTER_ZERO: [u8; 251] = [
1155+
0x15, 0x30, 0x1, 0x1, 0x0, 0x24, 0x2, 0x1, 0x37, 0x3, 0x27, 0x14, 0xfc, 0x8d, 0xcf,
1156+
0x45, 0x19, 0xff, 0x9a, 0x9a, 0x24, 0x15, 0x1, 0x18, 0x26, 0x4, 0xb1, 0x2a, 0x38, 0x2c,
1157+
0x26, 0x5, 0x31, 0x5e, 0x19, 0x2e, 0x37, 0x6, 0x27, 0x14, 0xfc, 0x8d, 0xcf, 0x45, 0x19,
1158+
0xff, 0x9a, 0x9a, 0x24, 0x15, 0x1, 0x18, 0x24, 0x7, 0x1, 0x24, 0x8, 0x1, 0x30, 0x9,
1159+
0x41, 0x4, 0x15, 0x69, 0x1e, 0x7b, 0x6a, 0xea, 0x5, 0xdb, 0xf8, 0x4b, 0xfd, 0xdc, 0x6c,
1160+
0x75, 0x46, 0x74, 0xb0, 0x60, 0xdb, 0x4, 0x71, 0xb6, 0xd0, 0x52, 0xf2, 0xf8, 0xe6,
1161+
0xbb, 0xd, 0xe5, 0x60, 0x1f, 0x84, 0x66, 0x4f, 0x3c, 0x90, 0x89, 0xa6, 0xc6, 0x99,
1162+
0x61, 0xfb, 0x89, 0xf7, 0xa, 0xa6, 0xe4, 0xa2, 0x21, 0xd3, 0x37, 0x30, 0x1b, 0xd2,
1163+
0x11, 0xc5, 0xcc, 0x0, 0xf4, 0x7a, 0x14, 0xfc, 0x3c, 0x37, 0xa, 0x35, 0x1, 0x29, 0x1,
1164+
0x18, 0x24, 0x2, 0x60, 0x30, 0x4, 0x14, 0xf8, 0xcf, 0xd0, 0x45, 0x6b, 0xe, 0xd1, 0x6f,
1165+
0xc5, 0x67, 0xdf, 0x81, 0xd7, 0xe9, 0xb7, 0xeb, 0x39, 0x78, 0xec, 0x40, 0x30, 0x5,
1166+
0x14, 0xf8, 0xcf, 0xd0, 0x45, 0x6b, 0xe, 0xd1, 0x6f, 0xc5, 0x67, 0xdf, 0x81, 0xd7,
1167+
0xe9, 0xb7, 0xeb, 0x39, 0x78, 0xec, 0x40, 0x18, 0x30, 0xb, 0x40, 0x4c, 0xae, 0xac,
1168+
0xc1, 0x26, 0xdd, 0x56, 0xc, 0x85, 0x86, 0xbc, 0xeb, 0xa2, 0xb5, 0xb7, 0xdf, 0x49,
1169+
0x92, 0x62, 0xcd, 0x2a, 0xb6, 0x4e, 0xc5, 0x31, 0x7c, 0xd9, 0xb, 0x1c, 0xe9, 0x6e,
1170+
0xe5, 0x82, 0xc7, 0xb8, 0xda, 0x22, 0x31, 0x7b, 0x23, 0x5a, 0x2a, 0xe6, 0x76, 0x28,
1171+
0xb6, 0xd4, 0xc7, 0x7b, 0x1c, 0x9c, 0x85, 0x71, 0x5f, 0xe6, 0xf6, 0x21, 0x50, 0x5c,
1172+
0xa7, 0x7c, 0xc7, 0x1d, 0x9a, 0x18,
1173+
];
11151174
}
11161175
}

rs-matter/src/cert/printer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ impl<'a, 'b> CertConsumer for CertPrinter<'a, 'b> {
122122
}
123123
Ok(())
124124
}
125-
fn utctime(&mut self, tag: &str, epoch: u32) -> Result<(), Error> {
126-
let matter_epoch = MATTER_EPOCH_SECS + epoch as u64;
125+
fn utctime(&mut self, tag: &str, epoch: u64) -> Result<(), Error> {
126+
let matter_epoch = MATTER_EPOCH_SECS + epoch;
127127

128128
let dt = OffsetDateTime::from_unix_timestamp(matter_epoch as _).unwrap();
129129

rs-matter/src/data_model/root_endpoint.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@ use super::{
1515
sdm::{
1616
admin_commissioning::{self, AdminCommCluster},
1717
dev_att::DevAttDataFetcher,
18+
ethernet_nw_diagnostics::{self, EthNwDiagCluster},
1819
failsafe::FailSafe,
1920
general_commissioning::{self, GenCommCluster},
21+
general_diagnostics::{self, GenDiagCluster},
22+
group_key_management,
23+
group_key_management::GrpKeyMgmtCluster,
2024
noc::{self, NocCluster},
2125
nw_commissioning::{self, NwCommCluster},
2226
},
@@ -33,17 +37,23 @@ pub type RootEndpointHandler<'a> = handler_chain_type!(
3337
NwCommCluster,
3438
AdminCommCluster<'a>,
3539
NocCluster<'a>,
36-
AccessControlCluster<'a>
40+
AccessControlCluster<'a>,
41+
GenDiagCluster,
42+
EthNwDiagCluster,
43+
GrpKeyMgmtCluster
3744
);
3845

39-
pub const CLUSTERS: [Cluster<'static>; 7] = [
46+
pub const CLUSTERS: [Cluster<'static>; 10] = [
4047
descriptor::CLUSTER,
4148
cluster_basic_information::CLUSTER,
4249
general_commissioning::CLUSTER,
4350
nw_commissioning::CLUSTER,
4451
admin_commissioning::CLUSTER,
4552
noc::CLUSTER,
4653
access_control::CLUSTER,
54+
general_diagnostics::CLUSTER,
55+
ethernet_nw_diagnostics::CLUSTER,
56+
group_key_management::CLUSTER,
4757
];
4858

4959
pub const fn endpoint(id: EndptId) -> Endpoint<'static> {
@@ -95,6 +105,21 @@ pub fn wrap<'a>(
95105
rand: Rand,
96106
) -> RootEndpointHandler<'a> {
97107
EmptyHandler
108+
.chain(
109+
endpoint_id,
110+
group_key_management::ID,
111+
GrpKeyMgmtCluster::new(rand),
112+
)
113+
.chain(
114+
endpoint_id,
115+
ethernet_nw_diagnostics::ID,
116+
EthNwDiagCluster::new(rand),
117+
)
118+
.chain(
119+
endpoint_id,
120+
general_diagnostics::ID,
121+
GenDiagCluster::new(rand),
122+
)
98123
.chain(
99124
endpoint_id,
100125
access_control::ID,
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/*
2+
*
3+
* Copyright (c) 2023 Project CHIP Authors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
use core::convert::TryInto;
19+
20+
use crate::{
21+
attribute_enum, cmd_enter, command_enum, data_model::objects::AttrType, data_model::objects::*,
22+
error::Error, tlv::TLVElement, transport::exchange::Exchange, utils::rand::Rand,
23+
};
24+
use log::info;
25+
use strum::{EnumDiscriminants, FromRepr};
26+
27+
pub const ID: u32 = 0x0037;
28+
29+
#[derive(FromRepr, EnumDiscriminants)]
30+
#[repr(u16)]
31+
pub enum Attributes {
32+
PacketRxCount(AttrType<u64>) = 0x02,
33+
PacketTxCount(AttrType<u64>) = 0x03,
34+
}
35+
36+
attribute_enum!(Attributes);
37+
38+
#[derive(FromRepr, EnumDiscriminants)]
39+
#[repr(u32)]
40+
pub enum Commands {
41+
ResetCounts = 0x0,
42+
}
43+
44+
command_enum!(Commands);
45+
46+
pub const CLUSTER: Cluster<'static> = Cluster {
47+
id: ID as _,
48+
feature_map: 0,
49+
attributes: &[
50+
FEATURE_MAP,
51+
ATTRIBUTE_LIST,
52+
Attribute::new(
53+
AttributesDiscriminants::PacketRxCount as u16,
54+
Access::RV,
55+
Quality::NONE,
56+
),
57+
Attribute::new(
58+
AttributesDiscriminants::PacketTxCount as u16,
59+
Access::RV,
60+
Quality::FIXED,
61+
),
62+
],
63+
commands: &[CommandsDiscriminants::ResetCounts as _],
64+
};
65+
66+
pub struct EthNwDiagCluster {
67+
data_ver: Dataver,
68+
}
69+
70+
impl EthNwDiagCluster {
71+
pub fn new(rand: Rand) -> Self {
72+
Self {
73+
data_ver: Dataver::new(rand),
74+
}
75+
}
76+
77+
pub fn read(&self, attr: &AttrDetails, encoder: AttrDataEncoder) -> Result<(), Error> {
78+
if let Some(writer) = encoder.with_dataver(self.data_ver.get())? {
79+
if attr.is_system() {
80+
CLUSTER.read(attr.attr_id, writer)
81+
} else {
82+
match attr.attr_id.try_into()? {
83+
Attributes::PacketRxCount(codec) => codec.encode(writer, 1),
84+
Attributes::PacketTxCount(codec) => codec.encode(writer, 1),
85+
}
86+
}
87+
} else {
88+
Ok(())
89+
}
90+
}
91+
92+
pub fn write(&self, _attr: &AttrDetails, data: AttrData) -> Result<(), Error> {
93+
let _data = data.with_dataver(self.data_ver.get())?;
94+
95+
self.data_ver.changed();
96+
97+
Ok(())
98+
}
99+
100+
pub fn invoke(
101+
&self,
102+
_exchange: &Exchange,
103+
cmd: &CmdDetails,
104+
_data: &TLVElement,
105+
_encoder: CmdDataEncoder,
106+
) -> Result<(), Error> {
107+
match cmd.cmd_id.try_into()? {
108+
Commands::ResetCounts => {
109+
cmd_enter!("ResetCounts: Not yet supported");
110+
}
111+
}
112+
113+
self.data_ver.changed();
114+
115+
Ok(())
116+
}
117+
}
118+
119+
impl Handler for EthNwDiagCluster {
120+
fn read(&self, attr: &AttrDetails, encoder: AttrDataEncoder) -> Result<(), Error> {
121+
EthNwDiagCluster::read(self, attr, encoder)
122+
}
123+
124+
fn write(&self, attr: &AttrDetails, data: AttrData) -> Result<(), Error> {
125+
EthNwDiagCluster::write(self, attr, data)
126+
}
127+
128+
fn invoke(
129+
&self,
130+
exchange: &Exchange,
131+
cmd: &CmdDetails,
132+
data: &TLVElement,
133+
encoder: CmdDataEncoder,
134+
) -> Result<(), Error> {
135+
EthNwDiagCluster::invoke(self, exchange, cmd, data, encoder)
136+
}
137+
}
138+
139+
// TODO: Might be removed once the `on` member is externalized
140+
impl NonBlockingHandler for EthNwDiagCluster {}
141+
142+
impl ChangeNotifier<()> for EthNwDiagCluster {
143+
fn consume_change(&mut self) -> Option<()> {
144+
self.data_ver.consume_change(())
145+
}
146+
}

rs-matter/src/data_model/sdm/general_commissioning.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,15 @@ struct FailSafeParams {
121121
bread_crumb: u8,
122122
}
123123

124+
#[derive(ToTLV)]
125+
struct BasicCommissioningInfo {
126+
expiry_len: u16,
127+
max_cmltv_failsafe_secs: u16,
128+
}
129+
124130
pub struct GenCommCluster<'a> {
125131
data_ver: Dataver,
126-
expiry_len: u16,
132+
basic_comm_info: BasicCommissioningInfo,
127133
failsafe: &'a RefCell<FailSafe>,
128134
}
129135

@@ -133,7 +139,10 @@ impl<'a> GenCommCluster<'a> {
133139
data_ver: Dataver::new(rand),
134140
failsafe,
135141
// TODO: Arch-Specific
136-
expiry_len: 120,
142+
basic_comm_info: BasicCommissioningInfo {
143+
expiry_len: 120,
144+
max_cmltv_failsafe_secs: 120,
145+
},
137146
}
138147
}
139148

@@ -157,10 +166,8 @@ impl<'a> GenCommCluster<'a> {
157166
codec.encode(writer, RegLocationType::IndoorOutdoor as _)
158167
}
159168
Attributes::BasicCommissioningInfo(_) => {
160-
writer.start_struct(AttrDataWriter::TAG)?;
161-
writer.u16(TagType::Context(0), self.expiry_len)?;
162-
writer.end_container()?;
163-
169+
self.basic_comm_info
170+
.to_tlv(&mut writer, AttrDataWriter::TAG)?;
164171
writer.complete()
165172
}
166173
}

0 commit comments

Comments
 (0)