Skip to content

Commit dfd2f7e

Browse files
committed
ASN1: Handle special case for Not-After == 0
1 parent 6bec796 commit dfd2f7e

File tree

4 files changed

+74
-10
lines changed

4 files changed

+74
-10
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/utils/epoch.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ use core::time::Duration;
22

33
pub type Epoch = fn() -> Duration;
44

5+
// As per the spec, if Not After is 0, it should set the time to GeneralizedTime value of
6+
// 99991231235959Z
7+
// So CERT_DOESNT_EXPIRE value is calculated as epoch(99991231235959Z) - MATTER_EPOCH_SECS
8+
pub const MATTER_CERT_DOESNT_EXPIRE: u64 = 252455615999;
9+
510
pub const MATTER_EPOCH_SECS: u64 = 946684800; // Seconds from 1970/01/01 00:00:00 till 2000/01/01 00:00:00 UTC
611

712
pub fn dummy_epoch() -> Duration {

0 commit comments

Comments
 (0)