diff --git a/rs-matter/src/cert/asn1_writer.rs b/rs-matter/src/cert/asn1_writer.rs index 87fac3c..7eb1c96 100644 --- a/rs-matter/src/cert/asn1_writer.rs +++ b/rs-matter/src/cert/asn1_writer.rs @@ -264,8 +264,8 @@ impl<'a> CertConsumer for ASN1Writer<'a> { self.write_str(0x06, oid) } - fn utctime(&mut self, _tag: &str, epoch: u32) -> Result<(), Error> { - let matter_epoch = MATTER_EPOCH_SECS + epoch as u64; + fn utctime(&mut self, _tag: &str, epoch: u64) -> Result<(), Error> { + let matter_epoch = MATTER_EPOCH_SECS + epoch; let dt = OffsetDateTime::from_unix_timestamp(matter_epoch as _).unwrap(); diff --git a/rs-matter/src/cert/mod.rs b/rs-matter/src/cert/mod.rs index 8878622..9e03a52 100644 --- a/rs-matter/src/cert/mod.rs +++ b/rs-matter/src/cert/mod.rs @@ -21,7 +21,7 @@ use crate::{ crypto::KeyPair, error::{Error, ErrorCode}, tlv::{self, FromTLV, OctetStr, TLVArray, TLVElement, TLVWriter, TagType, ToTLV}, - utils::writebuf::WriteBuf, + utils::{epoch::MATTER_CERT_DOESNT_EXPIRE, writebuf::WriteBuf}, }; use log::error; use num_derive::FromPrimitive; @@ -650,8 +650,14 @@ impl<'a> Cert<'a> { self.issuer.encode("Issuer:", w)?; w.start_seq("Validity:")?; - w.utctime("Not Before:", self.not_before)?; - w.utctime("Not After:", self.not_after)?; + w.utctime("Not Before:", self.not_before.into())?; + if self.not_after == 0 { + // As per the spec a Not-After value of 0, indicates no well-defined + // expiration date and should return in GeneralizedTime of 99991231235959Z + w.utctime("Not After:", MATTER_CERT_DOESNT_EXPIRE)?; + } else { + w.utctime("Not After:", self.not_after.into())?; + } w.end_seq()?; self.subject.encode("Subject:", w)?; @@ -710,8 +716,9 @@ impl<'a> CertVerifier<'a> { let k = KeyPair::new_from_public(parent.get_pubkey())?; k.verify_msg(asn1, self.cert.get_signature()).map_err(|e| { error!( - "Error in signature verification of certificate: {:x?}", - self.cert.get_subject_key_id() + "Error in signature verification of certificate: {:x?} by {:x?}", + self.cert.get_subject_key_id(), + parent.get_subject_key_id() ); e })?; @@ -744,7 +751,7 @@ pub trait CertConsumer { fn start_ctx(&mut self, tag: &str, id: u8) -> Result<(), Error>; fn end_ctx(&mut self) -> Result<(), Error>; fn oid(&mut self, tag: &str, oid: &[u8]) -> Result<(), Error>; - fn utctime(&mut self, tag: &str, epoch: u32) -> Result<(), Error>; + fn utctime(&mut self, tag: &str, epoch: u64) -> Result<(), Error>; } const MAX_DEPTH: usize = 10; @@ -826,6 +833,16 @@ mod tests { ); } + #[test] + fn test_zero_value_of_not_after_field() { + let noc = Cert::new(&test_vectors::NOC_NOT_AFTER_ZERO).unwrap(); + let rca = Cert::new(&test_vectors::RCA_FOR_NOC_NOT_AFTER_ZERO).unwrap(); + + let v = noc.verify_chain_start(); + let v = v.add_cert(&rca).unwrap(); + v.finalise().unwrap(); + } + #[test] fn test_cert_corrupted() { use crate::error::ErrorCode; @@ -1112,5 +1129,47 @@ mod tests { 0x16, 0x80, 0x14, 0x72, 0xc2, 0x01, 0xf7, 0x57, 0x19, 0x13, 0xb3, 0x48, 0xca, 0x00, 0xca, 0x7b, 0x45, 0xf4, 0x77, 0x46, 0x68, 0xc9, 0x7e, ]; + + /// An NOC that contains a Not-After validity field of '0' + pub const NOC_NOT_AFTER_ZERO: [u8; 251] = [ + 0x15, 0x30, 0x1, 0x1, 0x1, 0x24, 0x2, 0x1, 0x37, 0x3, 0x27, 0x14, 0xfc, 0x8d, 0xcf, + 0x45, 0x19, 0xff, 0x9a, 0x9a, 0x24, 0x15, 0x1, 0x18, 0x26, 0x4, 0x21, 0x39, 0x5a, 0x2c, + 0x24, 0x5, 0x0, 0x37, 0x6, 0x24, 0x15, 0x1, 0x26, 0x11, 0x6c, 0x4a, 0x95, 0xd2, 0x18, + 0x24, 0x7, 0x1, 0x24, 0x8, 0x1, 0x30, 0x9, 0x41, 0x4, 0x41, 0x7f, 0xb1, 0x61, 0xb0, + 0xbe, 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xe9, 0xb7, 0xeb, 0x39, 0x78, 0xec, 0x40, 0x18, 0x30, 0xb, 0x40, 0x4c, 0xae, 0xac, + 0xc1, 0x26, 0xdd, 0x56, 0xc, 0x85, 0x86, 0xbc, 0xeb, 0xa2, 0xb5, 0xb7, 0xdf, 0x49, + 0x92, 0x62, 0xcd, 0x2a, 0xb6, 0x4e, 0xc5, 0x31, 0x7c, 0xd9, 0xb, 0x1c, 0xe9, 0x6e, + 0xe5, 0x82, 0xc7, 0xb8, 0xda, 0x22, 0x31, 0x7b, 0x23, 0x5a, 0x2a, 0xe6, 0x76, 0x28, + 0xb6, 0xd4, 0xc7, 0x7b, 0x1c, 0x9c, 0x85, 0x71, 0x5f, 0xe6, 0xf6, 0x21, 0x50, 0x5c, + 0xa7, 0x7c, 0xc7, 0x1d, 0x9a, 0x18, + ]; } } diff --git a/rs-matter/src/cert/printer.rs b/rs-matter/src/cert/printer.rs index a4c4efe..b037576 100644 --- a/rs-matter/src/cert/printer.rs +++ b/rs-matter/src/cert/printer.rs @@ -122,8 +122,8 @@ impl<'a, 'b> CertConsumer for CertPrinter<'a, 'b> { } Ok(()) } - fn utctime(&mut self, tag: &str, epoch: u32) -> Result<(), Error> { - let matter_epoch = MATTER_EPOCH_SECS + epoch as u64; + fn utctime(&mut self, tag: &str, epoch: u64) -> Result<(), Error> { + let matter_epoch = MATTER_EPOCH_SECS + epoch; let dt = OffsetDateTime::from_unix_timestamp(matter_epoch as _).unwrap(); diff --git a/rs-matter/src/utils/epoch.rs b/rs-matter/src/utils/epoch.rs index 8236813..630f878 100644 --- a/rs-matter/src/utils/epoch.rs +++ b/rs-matter/src/utils/epoch.rs @@ -2,6 +2,11 @@ use core::time::Duration; pub type Epoch = fn() -> Duration; +// As per the spec, if Not After is 0, it should set the time to GeneralizedTime value of +// 99991231235959Z +// So CERT_DOESNT_EXPIRE value is calculated as epoch(99991231235959Z) - MATTER_EPOCH_SECS +pub const MATTER_CERT_DOESNT_EXPIRE: u64 = 252455615999; + pub const MATTER_EPOCH_SECS: u64 = 946684800; // Seconds from 1970/01/01 00:00:00 till 2000/01/01 00:00:00 UTC pub fn dummy_epoch() -> Duration {