Skip to content

Commit 78586f3

Browse files
authored
Merge pull request #22 from kedars/bugfix/misc_data_model_changes
Miscellaneous data model changes
2 parents d518be9 + 034d169 commit 78586f3

File tree

4 files changed

+37
-49
lines changed

4 files changed

+37
-49
lines changed

matter/src/cert/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ use crate::{
2626
use log::error;
2727
use num_derive::FromPrimitive;
2828

29-
use self::{asn1_writer::ASN1Writer, printer::CertPrinter};
29+
pub use self::asn1_writer::ASN1Writer;
30+
use self::printer::CertPrinter;
3031

3132
// As per https://datatracker.ietf.org/doc/html/rfc5280
3233

matter/src/crypto/crypto_mbedtls.rs

Lines changed: 16 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@ use mbedtls::{
2929
};
3030

3131
use super::CryptoKeyPair;
32-
use crate::error::Error;
32+
use crate::{
33+
// TODO: We should move ASN1Writer out of Cert,
34+
// so Crypto doesn't have to depend on Cert
35+
cert::{ASN1Writer, CertConsumer},
36+
error::Error,
37+
};
3338

3439
pub struct HmacSha256 {
3540
inner: Hmac,
@@ -183,7 +188,7 @@ impl CryptoKeyPair for KeyPair {
183188

184189
// current rust-mbedTLS APIs the signature to be in DER format
185190
let mut mbedtls_sign = [0u8; super::EC_SIGNATURE_LEN_BYTES * 3];
186-
let len = convert_r_s_to_asn1_sign(signature, &mut mbedtls_sign);
191+
let len = convert_r_s_to_asn1_sign(signature, &mut mbedtls_sign)?;
187192
let mbedtls_sign = &mbedtls_sign[..len];
188193

189194
if let Err(e) = tmp_key.verify(hash::Type::Sha256, &msg_hash, mbedtls_sign) {
@@ -195,51 +200,16 @@ impl CryptoKeyPair for KeyPair {
195200
}
196201
}
197202

198-
fn convert_r_s_to_asn1_sign(signature: &[u8], mbedtls_sign: &mut [u8]) -> usize {
199-
let mut offset = 0;
200-
mbedtls_sign[offset] = 0x30;
201-
offset += 1;
202-
let mut len = 68;
203-
if (signature[0] & 0x80) == 0x80 {
204-
len += 1;
205-
}
206-
if (signature[32] & 0x80) == 0x80 {
207-
len += 1;
208-
}
209-
mbedtls_sign[offset] = len;
210-
offset += 1;
211-
mbedtls_sign[offset] = 0x02;
212-
offset += 1;
213-
if (signature[0] & 0x80) == 0x80 {
214-
// It seems if topmost bit is 1, there is an extra 0
215-
mbedtls_sign[offset] = 33;
216-
offset += 1;
217-
mbedtls_sign[offset] = 0;
218-
offset += 1;
219-
} else {
220-
mbedtls_sign[offset] = 32;
221-
offset += 1;
222-
}
223-
mbedtls_sign[offset..(offset + 32)].copy_from_slice(&signature[..32]);
224-
offset += 32;
225-
226-
mbedtls_sign[offset] = 0x02;
227-
offset += 1;
228-
if (signature[32] & 0x80) == 0x80 {
229-
// It seems if topmost bit is 1, there is an extra 0
230-
mbedtls_sign[offset] = 33;
231-
offset += 1;
232-
mbedtls_sign[offset] = 0;
233-
offset += 1;
234-
} else {
235-
mbedtls_sign[offset] = 32;
236-
offset += 1;
237-
}
238-
239-
mbedtls_sign[offset..(offset + 32)].copy_from_slice(&signature[32..64]);
240-
offset += 32;
203+
fn convert_r_s_to_asn1_sign(signature: &[u8], mbedtls_sign: &mut [u8]) -> Result<usize, Error> {
204+
let r = &signature[0..32];
205+
let s = &signature[32..64];
241206

242-
offset
207+
let mut wr = ASN1Writer::new(mbedtls_sign);
208+
wr.start_seq("")?;
209+
wr.integer("r", r)?;
210+
wr.integer("s", s)?;
211+
wr.end_seq()?;
212+
Ok(wr.as_slice().len())
243213
}
244214

245215
// mbedTLS sign() function directly encodes the signature in ASN1. The lower level function

matter/src/data_model/sdm/general_commissioning.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ enum CommissioningError {
3333
ErrValueOutsideRange = 1,
3434
ErrInvalidAuth = 2,
3535
ErrNotCommissioning = 3,
36+
ErrBusyWithOtherAdmin = 4,
3637
}
3738

3839
pub const ID: u32 = 0x0030;
@@ -180,17 +181,18 @@ impl GenCommCluster {
180181
cmd_enter!("ARM Fail Safe");
181182

182183
let p = FailSafeParams::from_tlv(&cmd_req.data)?;
184+
let mut status = CommissioningError::Ok as u8;
183185

184186
if self
185187
.failsafe
186188
.arm(p.expiry_len, cmd_req.trans.session.get_session_mode())
187189
.is_err()
188190
{
189-
return Err(IMStatusCode::Busy);
191+
status = CommissioningError::ErrBusyWithOtherAdmin as u8;
190192
}
191193

192194
let cmd_data = CommonResponse {
193-
error_code: CommissioningError::Ok as u8,
195+
error_code: status,
194196
debug_txt: "".to_owned(),
195197
};
196198
let resp = ib::InvResp::cmd_new(

matter/src/data_model/system_model/descriptor.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ impl DescriptorCluster {
6767
Access::RV,
6868
Quality::NONE,
6969
)?,
70+
Attribute::new(
71+
Attributes::ClientList as u16,
72+
AttrValue::Custom,
73+
Access::RV,
74+
Quality::NONE,
75+
)?,
7076
];
7177
c.base.add_attributes(&attrs[..])?;
7278
Ok(c)
@@ -124,6 +130,12 @@ impl DescriptorCluster {
124130
}
125131
let _ = tw.end_container();
126132
}
133+
134+
fn encode_client_list(&self, tag: TagType, tw: &mut TLVWriter) {
135+
// No Clients supported
136+
let _ = tw.start_array(tag);
137+
let _ = tw.end_container();
138+
}
127139
}
128140

129141
impl ClusterType for DescriptorCluster {
@@ -145,6 +157,9 @@ impl ClusterType for DescriptorCluster {
145157
Some(Attributes::PartsList) => encoder.encode(EncodeValue::Closure(&|tag, tw| {
146158
self.encode_parts_list(tag, tw)
147159
})),
160+
Some(Attributes::ClientList) => encoder.encode(EncodeValue::Closure(&|tag, tw| {
161+
self.encode_client_list(tag, tw)
162+
})),
148163
_ => {
149164
error!("Attribute not supported: this shouldn't happen");
150165
}

0 commit comments

Comments
 (0)