diff --git a/rust/Earthfile b/rust/Earthfile index 373108a861..7c939d18ef 100644 --- a/rust/Earthfile +++ b/rust/Earthfile @@ -1,6 +1,6 @@ VERSION 0.8 -IMPORT github.com/input-output-hk/catalyst-ci/earthly/rust:v3.4.9 AS rust-ci +IMPORT github.com/input-output-hk/catalyst-ci/earthly/rust:v3.5.3 AS rust-ci IMPORT ../ AS repo-ci COPY_SRC: diff --git a/rust/c509-certificate/examples/cli/main.rs b/rust/c509-certificate/examples/cli/main.rs index cf80c5fc85..e63d9e5f8c 100644 --- a/rust/c509-certificate/examples/cli/main.rs +++ b/rust/c509-certificate/examples/cli/main.rs @@ -142,7 +142,9 @@ const SELF_SIGNED_INT: u8 = 2; /// A function to generate C509 certificate. fn generate( - file: &PathBuf, output: Option, private_key: Option<&PrivateKey>, + file: &PathBuf, + output: Option, + private_key: Option<&PrivateKey>, key_type: Option, ) -> anyhow::Result<()> { let data = fs::read_to_string(file)?; @@ -209,7 +211,10 @@ fn generate( } /// Write a data to a file given an output path. -fn write_to_output_file(output: PathBuf, data: &[u8]) -> anyhow::Result<()> { +fn write_to_output_file( + output: PathBuf, + data: &[u8], +) -> anyhow::Result<()> { let mut file = File::create(output).map_err(|e| anyhow::anyhow!(e))?; file.write_all(data).map_err(|e| anyhow::anyhow!(e))?; Ok(()) @@ -219,7 +224,9 @@ fn write_to_output_file(output: PathBuf, data: &[u8]) -> anyhow::Result<()> { /// If self-signed is true, issuer is the same as subject. /// Otherwise, issuer must be present. fn determine_issuer( - self_signed: bool, issuer: Option>, subject: Vec, + self_signed: bool, + issuer: Option>, + subject: Vec, ) -> anyhow::Result> { if self_signed { Ok(subject) @@ -230,7 +237,8 @@ fn determine_issuer( /// Validate the certificate type. fn validate_certificate_type( - self_signed: bool, certificate_type: Option, + self_signed: bool, + certificate_type: Option, ) -> anyhow::Result<()> { if self_signed && certificate_type.unwrap_or(SELF_SIGNED_INT) != SELF_SIGNED_INT { return Err(anyhow::anyhow!( @@ -260,7 +268,10 @@ fn get_key_type(key_type: Option) -> anyhow::Result<(Oid<'static>, Optio } /// Parse date string to u64. -fn parse_or_default_date(date_option: Option, default: u64) -> Result { +fn parse_or_default_date( + date_option: Option, + default: u64, +) -> Result { match date_option { Some(date) => { DateTime::parse_from_rfc3339(&date) @@ -284,7 +295,10 @@ fn parse_serial_number(serial_number: Option) -> UnwrappedBigU // -------------------verify----------------------- /// Verify the signature of the certificate given public key file path. -fn verify(file: &PathBuf, public_key: PathBuf) -> anyhow::Result<()> { +fn verify( + file: &PathBuf, + public_key: PathBuf, +) -> anyhow::Result<()> { let cert = fs::read(file)?; let pk = PublicKey::from_file(public_key)?; match c509_certificate::verify(&cert, &pk) { @@ -297,7 +311,10 @@ fn verify(file: &PathBuf, public_key: PathBuf) -> anyhow::Result<()> { // -------------------decode----------------------- /// Decode the certificate to JSON. -fn decode(file: &PathBuf, output: Option) -> anyhow::Result<()> { +fn decode( + file: &PathBuf, + output: Option, +) -> anyhow::Result<()> { let cert = fs::read(file)?; let mut d = minicbor::Decoder::new(&cert); let c509 = c509_certificate::c509::C509::decode(&mut d, &mut ())?; diff --git a/rust/c509-certificate/src/algorithm_identifier.rs b/rust/c509-certificate/src/algorithm_identifier.rs index c30d7d32bf..a8a7fc95c6 100644 --- a/rust/c509-certificate/src/algorithm_identifier.rs +++ b/rust/c509-certificate/src/algorithm_identifier.rs @@ -36,7 +36,10 @@ pub struct AlgorithmIdentifier { impl AlgorithmIdentifier { /// Create new instance of `AlgorithmIdentifier`. #[must_use] - pub fn new(oid: Oid<'static>, param: Option) -> Self { + pub fn new( + oid: Oid<'static>, + param: Option, + ) -> Self { Self { c509_oid: C509oid::new(oid), param, @@ -58,7 +61,9 @@ impl AlgorithmIdentifier { impl Encode<()> for AlgorithmIdentifier { fn encode( - &self, e: &mut Encoder, ctx: &mut (), + &self, + e: &mut Encoder, + ctx: &mut (), ) -> Result<(), minicbor::encode::Error> { match &self.param { // [ algorithm: ~oid, parameters: bytes ] @@ -77,7 +82,10 @@ impl Encode<()> for AlgorithmIdentifier { } impl Decode<'_, ()> for AlgorithmIdentifier { - fn decode(d: &mut Decoder<'_>, ctx: &mut ()) -> Result { + fn decode( + d: &mut Decoder<'_>, + ctx: &mut (), + ) -> Result { // [ algorithm: ~oid, parameters: bytes ] if decode_datatype(d, "Algorithm Identifier")? == minicbor::data::Type::Array { let len = decode_array_len(d, "Algorithm Identifier")?; diff --git a/rust/c509-certificate/src/attributes/attribute.rs b/rust/c509-certificate/src/attributes/attribute.rs index 4a179e1891..f1629ce41f 100644 --- a/rust/c509-certificate/src/attributes/attribute.rs +++ b/rust/c509-certificate/src/attributes/attribute.rs @@ -63,7 +63,10 @@ impl Attribute { } /// Add a value to `Attribute`. - pub fn add_value(&mut self, value: AttributeValue) { + pub fn add_value( + &mut self, + value: AttributeValue, + ) { self.value.push(value); } @@ -98,8 +101,13 @@ impl<'de> Deserialize<'de> for Attribute { } impl Serialize for Attribute { - fn serialize(&self, serializer: S) -> Result - where S: serde::Serializer { + fn serialize( + &self, + serializer: S, + ) -> Result + where + S: serde::Serializer, + { let helper = Helper { oid: self.registered_oid().c509_oid().oid().to_string(), value: self.value.clone(), @@ -110,7 +118,9 @@ impl Serialize for Attribute { impl Encode<()> for Attribute { fn encode( - &self, e: &mut Encoder, ctx: &mut (), + &self, + e: &mut Encoder, + ctx: &mut (), ) -> Result<(), minicbor::encode::Error> { // Encode CBOR int if available if let Some(&oid) = self @@ -145,7 +155,10 @@ impl Encode<()> for Attribute { } impl Decode<'_, ()> for Attribute { - fn decode(d: &mut Decoder<'_>, ctx: &mut ()) -> Result { + fn decode( + d: &mut Decoder<'_>, + ctx: &mut (), + ) -> Result { // Handle CBOR int let mut attr = if decode_datatype(d, "Attribute as OID int")? == minicbor::data::Type::U8 { let i = decode_helper(d, "Attribute as OID int", ctx)?; @@ -193,7 +206,9 @@ pub enum AttributeValue { impl Encode<()> for AttributeValue { fn encode( - &self, e: &mut Encoder, ctx: &mut (), + &self, + e: &mut Encoder, + ctx: &mut (), ) -> Result<(), minicbor::encode::Error> { match self { AttributeValue::Text(text) => encode_helper(e, "Attribute value", ctx, text)?, @@ -204,7 +219,10 @@ impl Encode<()> for AttributeValue { } impl Decode<'_, ()> for AttributeValue { - fn decode(d: &mut Decoder<'_>, ctx: &mut ()) -> Result { + fn decode( + d: &mut Decoder<'_>, + ctx: &mut (), + ) -> Result { match decode_datatype(d, "Attribute value")? { minicbor::data::Type::String => { Ok(AttributeValue::Text(decode_helper( diff --git a/rust/c509-certificate/src/attributes/mod.rs b/rust/c509-certificate/src/attributes/mod.rs index e608244c44..ed16375c4d 100644 --- a/rust/c509-certificate/src/attributes/mod.rs +++ b/rust/c509-certificate/src/attributes/mod.rs @@ -41,7 +41,10 @@ impl Attributes { /// Add an `Attribute` to the `Attributes`. /// and set `Attribute` value to support multiple value. - pub fn add_attribute(&mut self, attribute: Attribute) { + pub fn add_attribute( + &mut self, + attribute: Attribute, + ) { self.0.push(attribute.set_multi_value()); } } @@ -54,7 +57,9 @@ impl Default for Attributes { impl Encode<()> for Attributes { fn encode( - &self, e: &mut Encoder, ctx: &mut (), + &self, + e: &mut Encoder, + ctx: &mut (), ) -> Result<(), minicbor::encode::Error> { if self.0.is_empty() { return Err(minicbor::encode::Error::message( @@ -74,7 +79,10 @@ impl Encode<()> for Attributes { } impl Decode<'_, ()> for Attributes { - fn decode(d: &mut Decoder<'_>, ctx: &mut ()) -> Result { + fn decode( + d: &mut Decoder<'_>, + ctx: &mut (), + ) -> Result { let len = decode_array_len(d, "Attributes")?; if len == 0 { return Err(minicbor::decode::Error::message("Attributes is empty")); diff --git a/rust/c509-certificate/src/big_uint.rs b/rust/c509-certificate/src/big_uint.rs index a6e67ad0a5..cba17bad52 100644 --- a/rust/c509-certificate/src/big_uint.rs +++ b/rust/c509-certificate/src/big_uint.rs @@ -36,7 +36,9 @@ impl From for u64 { impl Encode<()> for UnwrappedBigUint { fn encode( - &self, e: &mut Encoder, _ctx: &mut (), + &self, + e: &mut Encoder, + _ctx: &mut (), ) -> Result<(), minicbor::encode::Error> { let bytes = self.0.to_be_bytes(); // Trim leading zeros @@ -52,7 +54,10 @@ impl Encode<()> for UnwrappedBigUint { } impl Decode<'_, ()> for UnwrappedBigUint { - fn decode(d: &mut Decoder<'_>, _ctx: &mut ()) -> Result { + fn decode( + d: &mut Decoder<'_>, + _ctx: &mut (), + ) -> Result { // Turn bytes into u64 let b = decode_bytes(d, "Unwrapped big uint")? .iter() diff --git a/rust/c509-certificate/src/c509.rs b/rust/c509-certificate/src/c509.rs index 4018740e24..ca1369b998 100644 --- a/rust/c509-certificate/src/c509.rs +++ b/rust/c509-certificate/src/c509.rs @@ -23,7 +23,10 @@ pub struct C509 { impl C509 { /// Create a new instance of C509 Certificate . #[must_use] - pub fn new(tbs_cert: TbsCert, issuer_signature_value: Option>) -> Self { + pub fn new( + tbs_cert: TbsCert, + issuer_signature_value: Option>, + ) -> Self { Self { tbs_cert, issuer_signature_value, @@ -45,7 +48,9 @@ impl C509 { impl Encode<()> for C509 { fn encode( - &self, e: &mut Encoder, ctx: &mut (), + &self, + e: &mut Encoder, + ctx: &mut (), ) -> Result<(), minicbor::encode::Error> { self.tbs_cert.encode(e, ctx)?; match self.issuer_signature_value { @@ -57,7 +62,10 @@ impl Encode<()> for C509 { } impl Decode<'_, ()> for C509 { - fn decode(d: &mut Decoder<'_>, ctx: &mut ()) -> Result { + fn decode( + d: &mut Decoder<'_>, + ctx: &mut (), + ) -> Result { let tbs_cert = TbsCert::decode(d, ctx)?; let issuer_signature_value = match decode_datatype(d, "C509 Issuer Signature value")? { minicbor::data::Type::Bytes => Some(decode_bytes(d, "C509 Issuer Signature value")?), diff --git a/rust/c509-certificate/src/cert_tbs.rs b/rust/c509-certificate/src/cert_tbs.rs index fc88dbdbfa..695b4efcec 100644 --- a/rust/c509-certificate/src/cert_tbs.rs +++ b/rust/c509-certificate/src/cert_tbs.rs @@ -48,10 +48,15 @@ impl TbsCert { #[must_use] #[allow(clippy::too_many_arguments)] pub fn new( - c509_certificate_type: u8, certificate_serial_number: UnwrappedBigUint, - issuer_signature_algorithm: IssuerSignatureAlgorithm, issuer: Option, - validity_not_before: Time, validity_not_after: Time, subject: Name, - subject_public_key_algorithm: SubjectPubKeyAlgorithm, subject_public_key: Vec, + c509_certificate_type: u8, + certificate_serial_number: UnwrappedBigUint, + issuer_signature_algorithm: IssuerSignatureAlgorithm, + issuer: Option, + validity_not_before: Time, + validity_not_after: Time, + subject: Name, + subject_public_key_algorithm: SubjectPubKeyAlgorithm, + subject_public_key: Vec, extensions: Extensions, ) -> Self { Self { @@ -149,7 +154,9 @@ impl TbsCert { impl Encode<()> for TbsCert { fn encode( - &self, e: &mut Encoder, ctx: &mut (), + &self, + e: &mut Encoder, + ctx: &mut (), ) -> Result<(), minicbor::encode::Error> { encode_helper(e, "Certificate type", ctx, &self.c509_certificate_type)?; self.certificate_serial_number.encode(e, ctx)?; @@ -166,7 +173,10 @@ impl Encode<()> for TbsCert { } impl Decode<'_, ()> for TbsCert { - fn decode(d: &mut Decoder<'_>, ctx: &mut ()) -> Result { + fn decode( + d: &mut Decoder<'_>, + ctx: &mut (), + ) -> Result { let cert_type = decode_helper(d, "Certificate type", ctx)?; let serial_number = UnwrappedBigUint::decode(d, ctx)?; let issuer_signature_algorithm = IssuerSignatureAlgorithm::decode(d, ctx)?; diff --git a/rust/c509-certificate/src/extensions/alt_name.rs b/rust/c509-certificate/src/extensions/alt_name.rs index 0f9896c858..f977219ca0 100644 --- a/rust/c509-certificate/src/extensions/alt_name.rs +++ b/rust/c509-certificate/src/extensions/alt_name.rs @@ -36,14 +36,19 @@ impl AlternativeName { impl Encode<()> for AlternativeName { fn encode( - &self, e: &mut Encoder, ctx: &mut (), + &self, + e: &mut Encoder, + ctx: &mut (), ) -> Result<(), minicbor::encode::Error> { self.0.encode(e, ctx) } } impl Decode<'_, ()> for AlternativeName { - fn decode(d: &mut Decoder<'_>, ctx: &mut ()) -> Result { + fn decode( + d: &mut Decoder<'_>, + ctx: &mut (), + ) -> Result { GeneralNamesOrText::decode(d, ctx).map(AlternativeName::new) } } @@ -64,7 +69,9 @@ pub enum GeneralNamesOrText { impl Encode<()> for GeneralNamesOrText { fn encode( - &self, e: &mut Encoder, ctx: &mut (), + &self, + e: &mut Encoder, + ctx: &mut (), ) -> Result<(), minicbor::encode::Error> { match self { GeneralNamesOrText::GeneralNames(gns) => { @@ -88,7 +95,10 @@ impl Encode<()> for GeneralNamesOrText { } impl Decode<'_, ()> for GeneralNamesOrText { - fn decode(d: &mut Decoder<'_>, ctx: &mut ()) -> Result { + fn decode( + d: &mut Decoder<'_>, + ctx: &mut (), + ) -> Result { match decode_datatype(d, "Alternative Name - General Names")? { // If it is a string it is a GeneralNames with only 1 DNSName minicbor::data::Type::String => { diff --git a/rust/c509-certificate/src/extensions/extension/mod.rs b/rust/c509-certificate/src/extensions/extension/mod.rs index c442d7ccad..57061923e3 100644 --- a/rust/c509-certificate/src/extensions/extension/mod.rs +++ b/rust/c509-certificate/src/extensions/extension/mod.rs @@ -33,7 +33,11 @@ pub struct Extension { impl Extension { /// Create a new instance of `Extension` using `OID` and value. #[must_use] - pub fn new(oid: Oid<'static>, value: ExtensionValue, critical: bool) -> Self { + pub fn new( + oid: Oid<'static>, + value: ExtensionValue, + critical: bool, + ) -> Self { Self { registered_oid: C509oidRegistered::new(oid, EXTENSIONS_LOOKUP.get_int_to_oid_table()), critical, @@ -83,8 +87,13 @@ impl<'de> Deserialize<'de> for Extension { } impl Serialize for Extension { - fn serialize(&self, serializer: S) -> Result - where S: serde::Serializer { + fn serialize( + &self, + serializer: S, + ) -> Result + where + S: serde::Serializer, + { let helper = Helper { oid: self.registered_oid.c509_oid().oid().to_string(), value: self.value.clone(), @@ -99,7 +108,9 @@ impl Encode<()> for Extension { // - (extensionID: int, extensionValue: any) // - (extensionID: ~oid, ? critical: true, extensionValue: bytes) fn encode( - &self, e: &mut Encoder, ctx: &mut (), + &self, + e: &mut Encoder, + ctx: &mut (), ) -> Result<(), minicbor::encode::Error> { // Handle CBOR int based on OID mapping if let Some(&mapped_oid) = self @@ -133,7 +144,10 @@ impl Encode<()> for Extension { } impl Decode<'_, ()> for Extension { - fn decode(d: &mut Decoder<'_>, ctx: &mut ()) -> Result { + fn decode( + d: &mut Decoder<'_>, + ctx: &mut (), + ) -> Result { match decode_datatype(d, "Extension")? { // Check whether OID is an int // Even the encoding is i16, the minicbor decoder doesn't know what type we encoded, @@ -214,7 +228,9 @@ impl ExtensionValueTypeTrait for ExtensionValueType { impl Encode<()> for ExtensionValue { fn encode( - &self, e: &mut Encoder, ctx: &mut (), + &self, + e: &mut Encoder, + ctx: &mut (), ) -> Result<(), minicbor::encode::Error> { match self { ExtensionValue::Int(value) => { @@ -239,7 +255,10 @@ impl Encode<()> for ExtensionValue { impl Decode<'_, C> for ExtensionValue where C: ExtensionValueTypeTrait + Debug { - fn decode(d: &mut Decoder<'_>, ctx: &mut C) -> Result { + fn decode( + d: &mut Decoder<'_>, + ctx: &mut C, + ) -> Result { match ctx.get_type() { ExtensionValueType::Int => { let value = decode_helper(d, "Extension value", ctx)?; diff --git a/rust/c509-certificate/src/extensions/mod.rs b/rust/c509-certificate/src/extensions/mod.rs index 5f1a99d579..112123a4a0 100644 --- a/rust/c509-certificate/src/extensions/mod.rs +++ b/rust/c509-certificate/src/extensions/mod.rs @@ -49,7 +49,10 @@ impl Extensions { } /// Add an `Extension` to the `Extensions`. - pub fn add_extension(&mut self, extension: Extension) { + pub fn add_extension( + &mut self, + extension: Extension, + ) { self.0.push(extension); } } @@ -62,7 +65,9 @@ impl Default for Extensions { impl Encode<()> for Extensions { fn encode( - &self, e: &mut Encoder, ctx: &mut (), + &self, + e: &mut Encoder, + ctx: &mut (), ) -> Result<(), minicbor::encode::Error> { // If there is only one extension and it is KeyUsage, encode as int // encoding as absolute value of the second int and the sign of the first int @@ -98,7 +103,10 @@ impl Encode<()> for Extensions { } impl Decode<'_, ()> for Extensions { - fn decode(d: &mut Decoder<'_>, _ctx: &mut ()) -> Result { + fn decode( + d: &mut Decoder<'_>, + _ctx: &mut (), + ) -> Result { // If only KeyUsage is in the extension -> will only contain an int if decode_datatype(d, "Extensions KeyUsage")? == minicbor::data::Type::U8 || decode_datatype(d, "Extensions KeyUsage")? == minicbor::data::Type::I8 diff --git a/rust/c509-certificate/src/general_names/data.rs b/rust/c509-certificate/src/general_names/data.rs index 0099a38183..45b73fe933 100644 --- a/rust/c509-certificate/src/general_names/data.rs +++ b/rust/c509-certificate/src/general_names/data.rs @@ -69,7 +69,11 @@ impl IntegerToGNTable { } /// Add a new integer to `GeneralNameTypeRegistry` map table. - pub(crate) fn add(&mut self, k: i16, v: GeneralNameTypeRegistry) { + pub(crate) fn add( + &mut self, + k: i16, + v: GeneralNameTypeRegistry, + ) { self.0.add(k, v); } diff --git a/rust/c509-certificate/src/general_names/general_name.rs b/rust/c509-certificate/src/general_names/general_name.rs index 990bde6a82..2e5d6b8186 100644 --- a/rust/c509-certificate/src/general_names/general_name.rs +++ b/rust/c509-certificate/src/general_names/general_name.rs @@ -42,7 +42,10 @@ pub struct GeneralName { impl GeneralName { /// Create a new instance of `GeneralName`. #[must_use] - pub fn new(gn_type: GeneralNameTypeRegistry, value: GeneralNameValue) -> Self { + pub fn new( + gn_type: GeneralNameTypeRegistry, + value: GeneralNameValue, + ) -> Self { Self { gn_type, value } } @@ -61,7 +64,9 @@ impl GeneralName { impl Encode<()> for GeneralName { fn encode( - &self, e: &mut Encoder, ctx: &mut (), + &self, + e: &mut Encoder, + ctx: &mut (), ) -> Result<(), minicbor::encode::Error> { // Encode GeneralNameType as int let i = get_int_from_gn(self.gn_type).map_err(minicbor::encode::Error::message)?; @@ -73,7 +78,10 @@ impl Encode<()> for GeneralName { } impl Decode<'_, ()> for GeneralName { - fn decode(d: &mut Decoder<'_>, _ctx: &mut ()) -> Result { + fn decode( + d: &mut Decoder<'_>, + _ctx: &mut (), + ) -> Result { if decode_datatype(d, "General Name as OID int")? == minicbor::data::Type::U8 || decode_datatype(d, "General Name as OID int")? == minicbor::data::Type::I8 { @@ -158,7 +166,9 @@ impl GeneralNameValueTrait for GeneralNameValueType { impl Encode<()> for GeneralNameValue { fn encode( - &self, e: &mut Encoder, ctx: &mut (), + &self, + e: &mut Encoder, + ctx: &mut (), ) -> Result<(), minicbor::encode::Error> { match self { GeneralNameValue::Text(value) => { @@ -188,7 +198,10 @@ impl Encode<()> for GeneralNameValue { impl Decode<'_, C> for GeneralNameValue where C: GeneralNameValueTrait + Debug { - fn decode(d: &mut Decoder<'_>, ctx: &mut C) -> Result { + fn decode( + d: &mut Decoder<'_>, + ctx: &mut C, + ) -> Result { match ctx.get_type() { GeneralNameValueType::Text => { let value = decode_helper(d, "General Name value", ctx)?; diff --git a/rust/c509-certificate/src/general_names/mod.rs b/rust/c509-certificate/src/general_names/mod.rs index 996a5a5a00..d681380287 100644 --- a/rust/c509-certificate/src/general_names/mod.rs +++ b/rust/c509-certificate/src/general_names/mod.rs @@ -38,7 +38,10 @@ impl GeneralNames { } /// Add a new `GeneralName` to the `GeneralNames`. - pub fn add_general_name(&mut self, gn: GeneralName) { + pub fn add_general_name( + &mut self, + gn: GeneralName, + ) { self.0.push(gn); } } @@ -51,7 +54,9 @@ impl Default for GeneralNames { impl Encode<()> for GeneralNames { fn encode( - &self, e: &mut Encoder, ctx: &mut (), + &self, + e: &mut Encoder, + ctx: &mut (), ) -> Result<(), minicbor::encode::Error> { if self.0.is_empty() { return Err(minicbor::encode::Error::message( @@ -71,7 +76,10 @@ impl Encode<()> for GeneralNames { } impl Decode<'_, ()> for GeneralNames { - fn decode(d: &mut Decoder<'_>, ctx: &mut ()) -> Result { + fn decode( + d: &mut Decoder<'_>, + ctx: &mut (), + ) -> Result { let len = decode_array_len(d, "General Names")?; let mut gn = GeneralNames::new(); for _ in 0..len / 2 { diff --git a/rust/c509-certificate/src/general_names/other_name_hw_module.rs b/rust/c509-certificate/src/general_names/other_name_hw_module.rs index 7e2660cc08..b9f14e5c8e 100644 --- a/rust/c509-certificate/src/general_names/other_name_hw_module.rs +++ b/rust/c509-certificate/src/general_names/other_name_hw_module.rs @@ -29,7 +29,10 @@ pub struct OtherNameHardwareModuleName { impl OtherNameHardwareModuleName { /// Create a new instance of `OtherNameHardwareModuleName`. #[must_use] - pub fn new(hw_type: Oid<'static>, hw_serial_num: Vec) -> Self { + pub fn new( + hw_type: Oid<'static>, + hw_serial_num: Vec, + ) -> Self { Self { hw_type: C509oid::new(hw_type), hw_serial_num, @@ -51,7 +54,9 @@ impl OtherNameHardwareModuleName { impl Encode<()> for OtherNameHardwareModuleName { fn encode( - &self, e: &mut Encoder, ctx: &mut (), + &self, + e: &mut Encoder, + ctx: &mut (), ) -> Result<(), minicbor::encode::Error> { encode_array_len(e, "OtherNameHardwareModule", 2)?; self.hw_type.encode(e, ctx)?; @@ -65,7 +70,10 @@ impl Encode<()> for OtherNameHardwareModuleName { } impl<'a> Decode<'a, ()> for OtherNameHardwareModuleName { - fn decode(d: &mut Decoder<'a>, ctx: &mut ()) -> Result { + fn decode( + d: &mut Decoder<'a>, + ctx: &mut (), + ) -> Result { decode_array_len(d, "OtherNameHardwareModule")?; let hw_type = C509oid::decode(d, ctx)?; let hw_serial_num = decode_bytes(d, "OtherNameHardwareModule serial number")?; diff --git a/rust/c509-certificate/src/helper/decode.rs b/rust/c509-certificate/src/helper/decode.rs index d43ef62e5e..6f219560af 100644 --- a/rust/c509-certificate/src/helper/decode.rs +++ b/rust/c509-certificate/src/helper/decode.rs @@ -4,9 +4,13 @@ use minicbor::{decode, Decoder}; /// Generic helper function for decoding different types. pub(crate) fn decode_helper<'a, T, C>( - d: &mut Decoder<'a>, from: &str, context: &mut C, + d: &mut Decoder<'a>, + from: &str, + context: &mut C, ) -> Result -where T: minicbor::Decode<'a, C> { +where + T: minicbor::Decode<'a, C>, +{ T::decode(d, context).map_err(|e| { decode::Error::message(format!( "Failed to decode {:?} in {from}: {e}", @@ -16,7 +20,10 @@ where T: minicbor::Decode<'a, C> { } /// Helper function for decoding bytes. -pub(crate) fn decode_bytes(d: &mut Decoder, from: &str) -> Result, decode::Error> { +pub(crate) fn decode_bytes( + d: &mut Decoder, + from: &str, +) -> Result, decode::Error> { d.bytes().map(<[u8]>::to_vec).map_err(|e| { decode::Error::message(format!( "Failed to decode bytes in {from}: @@ -26,7 +33,10 @@ pub(crate) fn decode_bytes(d: &mut Decoder, from: &str) -> Result, decod } /// Helper function for decoding array. -pub(crate) fn decode_array_len(d: &mut Decoder, from: &str) -> Result { +pub(crate) fn decode_array_len( + d: &mut Decoder, + from: &str, +) -> Result { d.array() .map_err(|e| { decode::Error::message(format!( @@ -40,7 +50,10 @@ pub(crate) fn decode_array_len(d: &mut Decoder, from: &str) -> Result Result<(), decode::Error> { +pub(crate) fn decode_null( + d: &mut Decoder, + from: &str, +) -> Result<(), decode::Error> { d.null().map_err(|e| { decode::Error::message(format!( "Failed to decode null in {from}: @@ -51,7 +64,8 @@ pub(crate) fn decode_null(d: &mut Decoder, from: &str) -> Result<(), decode::Err /// Helper function for decoding datatype. pub(crate) fn decode_datatype( - d: &mut Decoder, from: &str, + d: &mut Decoder, + from: &str, ) -> Result { d.datatype().map_err(|e| { decode::Error::message(format!( diff --git a/rust/c509-certificate/src/helper/encode.rs b/rust/c509-certificate/src/helper/encode.rs index 3184ce0a4c..286b9ba329 100644 --- a/rust/c509-certificate/src/helper/encode.rs +++ b/rust/c509-certificate/src/helper/encode.rs @@ -7,9 +7,14 @@ use minicbor::{ /// Generic helper function for encoding different types. pub(crate) fn encode_helper( - e: &mut Encoder, from: &str, ctx: &mut C, value: &T, + e: &mut Encoder, + from: &str, + ctx: &mut C, + value: &T, ) -> Result<(), encode::Error> -where T: minicbor::Encode { +where + T: minicbor::Encode, +{ T::encode(value, e, ctx).map_err(|err| { encode::Error::with_message( err, @@ -25,7 +30,9 @@ where T: minicbor::Encode { /// Helper function for encoding bytes. pub(crate) fn encode_bytes( - e: &mut Encoder, from: &str, value: &[u8], + e: &mut Encoder, + from: &str, + value: &[u8], ) -> Result<(), encode::Error> { e.bytes(value).map_err(|err| { encode::Error::with_message(err, format!("Failed to encode bytes in {from}")) @@ -35,7 +42,8 @@ pub(crate) fn encode_bytes( /// Helper function for encoding null. pub(crate) fn encode_null( - e: &mut Encoder, from: &str, + e: &mut Encoder, + from: &str, ) -> Result<(), encode::Error> { e.null().map_err(|err| { encode::Error::with_message(err, format!("Failed to encode null in {from}")) @@ -45,7 +53,9 @@ pub(crate) fn encode_null( /// Helper function for encoding array. pub(crate) fn encode_array_len( - e: &mut Encoder, from: &str, len: u64, + e: &mut Encoder, + from: &str, + len: u64, ) -> Result<(), encode::Error> { e.array(len).map_err(|err| { encode::Error::with_message(err, format!("Failed to encode array in {from}")) diff --git a/rust/c509-certificate/src/issuer_sig_algo/mod.rs b/rust/c509-certificate/src/issuer_sig_algo/mod.rs index 3fb049d2fa..8715634353 100644 --- a/rust/c509-certificate/src/issuer_sig_algo/mod.rs +++ b/rust/c509-certificate/src/issuer_sig_algo/mod.rs @@ -38,7 +38,10 @@ pub struct IssuerSignatureAlgorithm { impl IssuerSignatureAlgorithm { /// Create new instance of `IssuerSignatureAlgorithm` where it registered with /// Issuer Signature Algorithm lookup table. - pub fn new(oid: Oid<'static>, param: Option) -> Self { + pub fn new( + oid: Oid<'static>, + param: Option, + ) -> Self { Self { registered_oid: C509oidRegistered::new( oid.clone(), @@ -82,8 +85,13 @@ impl<'de> Deserialize<'de> for IssuerSignatureAlgorithm { } impl Serialize for IssuerSignatureAlgorithm { - fn serialize(&self, serializer: S) -> Result - where S: serde::Serializer { + fn serialize( + &self, + serializer: S, + ) -> Result + where + S: serde::Serializer, + { let helper = Helper { oid: self.registered_oid.c509_oid().oid().to_string(), param: self.algo_identifier.param().clone(), @@ -94,7 +102,9 @@ impl Serialize for IssuerSignatureAlgorithm { impl Encode<()> for IssuerSignatureAlgorithm { fn encode( - &self, e: &mut Encoder, ctx: &mut (), + &self, + e: &mut Encoder, + ctx: &mut (), ) -> Result<(), minicbor::encode::Error> { if let Some(&i) = self .registered_oid @@ -111,7 +121,10 @@ impl Encode<()> for IssuerSignatureAlgorithm { } impl Decode<'_, ()> for IssuerSignatureAlgorithm { - fn decode(d: &mut Decoder<'_>, ctx: &mut ()) -> Result { + fn decode( + d: &mut Decoder<'_>, + ctx: &mut (), + ) -> Result { match decode_datatype(d, "Issuer Signature Algorithm")? { // Check i16 for -256 and -256 minicbor::data::Type::U8 | minicbor::data::Type::I16 => { diff --git a/rust/c509-certificate/src/lib.rs b/rust/c509-certificate/src/lib.rs index b2292d9eaf..c72952aa34 100644 --- a/rust/c509-certificate/src/lib.rs +++ b/rust/c509-certificate/src/lib.rs @@ -79,7 +79,10 @@ pub mod time; /// # Errors /// /// Returns an error if the generated data is invalid. -pub fn generate(tbs_cert: &TbsCert, private_key: Option<&PrivateKey>) -> anyhow::Result> { +pub fn generate( + tbs_cert: &TbsCert, + private_key: Option<&PrivateKey>, +) -> anyhow::Result> { // Encode the TbsCert let encoded_tbs = { let mut buffer = Vec::new(); @@ -109,7 +112,10 @@ pub fn generate(tbs_cert: &TbsCert, private_key: Option<&PrivateKey>) -> anyhow: /// # Errors /// Returns an error if the `issuer_signature_value` is invalid or the signature cannot be /// verified. -pub fn verify(c509: &[u8], public_key: &PublicKey) -> anyhow::Result<()> { +pub fn verify( + c509: &[u8], + public_key: &PublicKey, +) -> anyhow::Result<()> { let mut d = minicbor::Decoder::new(c509); let c509 = C509::decode(&mut d, &mut ())?; let mut encoded_tbs = Vec::new(); diff --git a/rust/c509-certificate/src/name/mod.rs b/rust/c509-certificate/src/name/mod.rs index d5032a64fc..9e118195ea 100644 --- a/rust/c509-certificate/src/name/mod.rs +++ b/rust/c509-certificate/src/name/mod.rs @@ -57,14 +57,19 @@ impl Name { impl Encode<()> for Name { fn encode( - &self, e: &mut Encoder, ctx: &mut (), + &self, + e: &mut Encoder, + ctx: &mut (), ) -> Result<(), minicbor::encode::Error> { self.0.encode(e, ctx) } } impl Decode<'_, ()> for Name { - fn decode(d: &mut Decoder<'_>, ctx: &mut ()) -> Result { + fn decode( + d: &mut Decoder<'_>, + ctx: &mut (), + ) -> Result { NameValue::decode(d, ctx).map(Name::new) } } @@ -86,7 +91,9 @@ pub enum NameValue { impl Encode<()> for NameValue { fn encode( - &self, e: &mut Encoder, ctx: &mut (), + &self, + e: &mut Encoder, + ctx: &mut (), ) -> Result<(), minicbor::encode::Error> { match self { NameValue::Attribute(attrs) => { @@ -137,7 +144,10 @@ impl Encode<()> for NameValue { } impl Decode<'_, ()> for NameValue { - fn decode(d: &mut Decoder<'_>, ctx: &mut ()) -> Result { + fn decode( + d: &mut Decoder<'_>, + ctx: &mut (), + ) -> Result { match decode_datatype(d, "Name")? { minicbor::data::Type::Array => { let len = decode_array_len(d, "Attributes")?; @@ -166,7 +176,8 @@ impl Decode<'_, ()> for NameValue { /// Encode common name value. fn encode_cn_value( - e: &mut Encoder, cn_value: &AttributeValue, + e: &mut Encoder, + cn_value: &AttributeValue, ) -> Result<(), minicbor::encode::Error> { let hex_regex = Regex::new(r"^[0-9a-f]+$").map_err(minicbor::encode::Error::message)?; let eui64_regex = diff --git a/rust/c509-certificate/src/oid.rs b/rust/c509-certificate/src/oid.rs index db4524a518..a426507f4e 100644 --- a/rust/c509-certificate/src/oid.rs +++ b/rust/c509-certificate/src/oid.rs @@ -28,7 +28,10 @@ pub struct C509oidRegistered { impl C509oidRegistered { /// Create a new instance of `C509oidRegistered`. - pub(crate) fn new(oid: Oid<'static>, table: &'static IntegerToOidTable) -> Self { + pub(crate) fn new( + oid: Oid<'static>, + table: &'static IntegerToOidTable, + ) -> Self { Self { c509_oid: C509oid::new(oid), registration_table: table, @@ -85,8 +88,13 @@ impl<'de> Deserialize<'de> for C509oid { } impl Serialize for C509oid { - fn serialize(&self, serializer: S) -> Result - where S: serde::Serializer { + fn serialize( + &self, + serializer: S, + ) -> Result + where + S: serde::Serializer, + { let helper = Helper { oid: self.0.to_string(), }; @@ -103,7 +111,9 @@ impl Encode<()> for C509oid { /// A vector of bytes containing the CBOR encoded OID. /// If the encoding fails, it will return an error. fn encode( - &self, e: &mut Encoder, _ctx: &mut (), + &self, + e: &mut Encoder, + _ctx: &mut (), ) -> Result<(), minicbor::encode::Error> { let oid_bytes = self.0.as_bytes(); encode_bytes(e, "C509 OID", oid_bytes) @@ -118,7 +128,10 @@ impl Decode<'_, ()> for C509oid { /// /// A C509oid instance. /// If the decoding fails, it will return an error. - fn decode(d: &mut Decoder, _ctx: &mut ()) -> Result { + fn decode( + d: &mut Decoder, + _ctx: &mut (), + ) -> Result { let oid_bytes = decode_bytes(d, "C509 OID")?; let oid = Oid::new(oid_bytes.into()); Ok(C509oid::new(oid)) diff --git a/rust/c509-certificate/src/signing.rs b/rust/c509-certificate/src/signing.rs index 6fc732c8ad..b50b8b6a7d 100644 --- a/rust/c509-certificate/src/signing.rs +++ b/rust/c509-certificate/src/signing.rs @@ -33,12 +33,18 @@ struct FileError { #[allow(dead_code)] impl FileError { /// Create a new `FileError` instance from a string location. - fn from_string(location: String, msg: Option) -> Self { + fn from_string( + location: String, + msg: Option, + ) -> Self { Self { location, msg } } /// Create a new `FileError` instance from a path location. - fn from_path>(path: P, msg: Option) -> Self { + fn from_path>( + path: P, + msg: Option, + ) -> Self { Self { location: path.as_ref().display().to_string(), msg, @@ -47,7 +53,10 @@ impl FileError { } impl Display for FileError { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + fn fmt( + &self, + f: &mut std::fmt::Formatter<'_>, + ) -> std::fmt::Result { let msg = format!("Cannot open or read file at {0}", self.location); let err = self .msg @@ -78,7 +87,10 @@ impl PrivateKey { /// Sign the message with the current private key. /// Returns the signature bytes. #[must_use] - pub fn sign(&self, msg: &[u8]) -> Vec { + pub fn sign( + &self, + msg: &[u8], + ) -> Vec { self.0.sign(msg).to_vec() } } @@ -128,7 +140,11 @@ impl PublicKey { /// /// # Errors /// Returns an error if the signature is invalid. - pub fn verify(&self, msg: &[u8], signature_bytes: &[u8]) -> anyhow::Result<()> { + pub fn verify( + &self, + msg: &[u8], + signature_bytes: &[u8], + ) -> anyhow::Result<()> { let signature_bytes = signature_bytes.try_into().map_err(|_| { anyhow::anyhow!( "Invalid signature bytes size: expected {}, provided {}.", diff --git a/rust/c509-certificate/src/subject_pub_key_algo/mod.rs b/rust/c509-certificate/src/subject_pub_key_algo/mod.rs index b3d923bd1c..b379a75ad5 100644 --- a/rust/c509-certificate/src/subject_pub_key_algo/mod.rs +++ b/rust/c509-certificate/src/subject_pub_key_algo/mod.rs @@ -39,7 +39,10 @@ pub struct SubjectPubKeyAlgorithm { impl SubjectPubKeyAlgorithm { /// Create new instance of `SubjectPubKeyAlgorithm` where it registered with /// Subject Public Key Algorithm lookup table. - pub fn new(oid: Oid<'static>, param: Option) -> Self { + pub fn new( + oid: Oid<'static>, + param: Option, + ) -> Self { Self { registered_oid: C509oidRegistered::new( oid.clone(), @@ -83,8 +86,13 @@ impl<'de> Deserialize<'de> for SubjectPubKeyAlgorithm { } impl Serialize for SubjectPubKeyAlgorithm { - fn serialize(&self, serializer: S) -> Result - where S: serde::Serializer { + fn serialize( + &self, + serializer: S, + ) -> Result + where + S: serde::Serializer, + { let helper = Helper { oid: self.registered_oid.c509_oid().oid().to_string(), param: self.algo_identifier.param().clone(), @@ -95,7 +103,9 @@ impl Serialize for SubjectPubKeyAlgorithm { impl Encode<()> for SubjectPubKeyAlgorithm { fn encode( - &self, e: &mut Encoder, ctx: &mut (), + &self, + e: &mut Encoder, + ctx: &mut (), ) -> Result<(), minicbor::encode::Error> { if let Some(&i) = self .registered_oid @@ -112,7 +122,10 @@ impl Encode<()> for SubjectPubKeyAlgorithm { } impl Decode<'_, ()> for SubjectPubKeyAlgorithm { - fn decode(d: &mut Decoder<'_>, ctx: &mut ()) -> Result { + fn decode( + d: &mut Decoder<'_>, + ctx: &mut (), + ) -> Result { // Check u8 for 0 - 28 if decode_datatype(d, "Subject public key algorithm")? == minicbor::data::Type::U8 { let i = decode_helper(d, "Subject public key algorithm", ctx)?; diff --git a/rust/c509-certificate/src/tables.rs b/rust/c509-certificate/src/tables.rs index 67e83fde4f..3c526115a6 100644 --- a/rust/c509-certificate/src/tables.rs +++ b/rust/c509-certificate/src/tables.rs @@ -15,7 +15,11 @@ pub(crate) trait TableTrait { /// Create new instance of the map table. fn new() -> Self; /// Add the key-value pair to the map table. - fn add(&mut self, k: K, v: V); + fn add( + &mut self, + k: K, + v: V, + ); /// Get the bimap of the map table. fn get_map(&self) -> &BiMap; } @@ -38,7 +42,11 @@ impl TableTrait for IntTable { } /// Add the key-value pair to the map table. - fn add(&mut self, k: i16, v: T) { + fn add( + &mut self, + k: i16, + v: T, + ) { self.map.insert(k, v); } @@ -67,7 +75,11 @@ impl IntegerToOidTable { } /// Add the key-value pair to the map table. - pub(crate) fn add(&mut self, k: i16, v: Oid<'static>) { + pub(crate) fn add( + &mut self, + k: i16, + v: Oid<'static>, + ) { self.table.add(k, v); } diff --git a/rust/c509-certificate/src/time.rs b/rust/c509-certificate/src/time.rs index b3e74a178f..ae6ba280d8 100644 --- a/rust/c509-certificate/src/time.rs +++ b/rust/c509-certificate/src/time.rs @@ -45,7 +45,9 @@ impl From