From a539f4621e74a1ab722f96d152278d87121550c5 Mon Sep 17 00:00:00 2001 From: ivmarkov Date: Thu, 4 May 2023 06:13:36 +0000 Subject: [PATCH] More crypto fixes --- matter/Cargo.toml | 2 +- matter/src/crypto/crypto_rustcrypto.rs | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/matter/Cargo.toml b/matter/Cargo.toml index 4eb2a74..b077a7a 100644 --- a/matter/Cargo.toml +++ b/matter/Cargo.toml @@ -69,7 +69,7 @@ p256 = { version = "0.13.0", default-features = false, features = ["arithmetic", elliptic-curve = { version = "0.13.2", optional = true } crypto-bigint = { version = "0.4", default-features = false, optional = true } rand_core = { version = "0.6", default-features = false, optional = true } -x509-cert = { version = "0.2.0", default-features = false, features = ["pem", "std"], optional = true } # TODO: requires `alloc` +x509-cert = { version = "0.2.0", default-features = false, features = ["pem"], optional = true } # TODO: requires `alloc` # to compute the check digit verhoeff = "1" diff --git a/matter/src/crypto/crypto_rustcrypto.rs b/matter/src/crypto/crypto_rustcrypto.rs index 6f975cb..6212c96 100644 --- a/matter/src/crypto/crypto_rustcrypto.rs +++ b/matter/src/crypto/crypto_rustcrypto.rs @@ -34,7 +34,7 @@ use p256::{ use sha2::Digest; use x509_cert::{ attr::AttributeType, - der::{asn1::BitString, Any, Encode}, + der::{asn1::BitString, Any, Encode, Writer}, name::RdnSequence, request::CertReq, spki::{AlgorithmIdentifier, SubjectPublicKeyInfoOwned}, @@ -205,7 +205,7 @@ impl KeyPair { attributes: Default::default(), }; let mut message = vec![]; - info.encode(&mut message).unwrap(); + info.encode(&mut VecWriter(&mut message)).unwrap(); // Can't use self.sign_msg as the signature has to be in DER format let private_key = self.private_key()?; @@ -375,3 +375,13 @@ impl<'a> ccm::aead::Buffer for SliceBuffer<'a> { self.len = len; } } + +struct VecWriter<'a>(&'a mut alloc::vec::Vec); + +impl<'a> Writer for VecWriter<'a> { + fn write(&mut self, slice: &[u8]) -> x509_cert::der::Result<()> { + self.0.extend_from_slice(slice); + + Ok(()) + } +}