From 1192b4d1692d9a690c7b1c6c1f22ea4e97f43995 Mon Sep 17 00:00:00 2001 From: bkioshn Date: Thu, 5 Sep 2024 19:50:29 +0700 Subject: [PATCH 1/3] fix: naming + add neccesary function --- rust/c509-certificate/examples/cli/main.rs | 32 ++++++++------ .../src/algorithm_identifier.rs | 23 ++++++---- .../src/attributes/attribute.rs | 32 +++++++------- rust/c509-certificate/src/attributes/mod.rs | 24 ++++++---- rust/c509-certificate/src/big_uint.rs | 12 +++++ rust/c509-certificate/src/c509.rs | 4 +- .../src/extensions/alt_name.rs | 16 +++---- .../src/extensions/extension/mod.rs | 16 +++---- rust/c509-certificate/src/extensions/mod.rs | 40 +++++++---------- .../src/general_names/general_name.rs | 4 +- .../c509-certificate/src/general_names/mod.rs | 32 +++++++------- .../src/general_names/other_name_hw_module.rs | 14 +++++- .../src/issuer_sig_algo/mod.rs | 25 ++++++++--- rust/c509-certificate/src/lib.rs | 4 +- rust/c509-certificate/src/name/mod.rs | 34 +++++++------- rust/c509-certificate/src/name/rdn.rs | 27 ++++++------ rust/c509-certificate/src/oid.rs | 20 ++++----- .../src/subject_pub_key_algo/mod.rs | 24 +++++++--- rust/c509-certificate/src/tbs_cert.rs | 44 +++++++++---------- rust/c509-certificate/src/time.rs | 14 ++++-- 20 files changed, 252 insertions(+), 189 deletions(-) diff --git a/rust/c509-certificate/examples/cli/main.rs b/rust/c509-certificate/examples/cli/main.rs index 816446f487e..082ba3f90b4 100644 --- a/rust/c509-certificate/examples/cli/main.rs +++ b/rust/c509-certificate/examples/cli/main.rs @@ -288,22 +288,26 @@ fn decode(file: &PathBuf, output: Option) -> anyhow::Result<()> { let mut d = minicbor::Decoder::new(&cert); let c509 = c509_certificate::c509::C509::decode(&mut d, &mut ())?; - let tbs_cert = c509.get_tbs_cert(); - let is_self_signed = tbs_cert.get_c509_certificate_type() == SELF_SIGNED_INT; + let tbs_cert = c509.tbs_cert(); + let is_self_signed = tbs_cert.c509_certificate_type() == SELF_SIGNED_INT; let c509_json = C509Json { self_signed: is_self_signed, - certificate_type: Some(tbs_cert.get_c509_certificate_type()), - serial_number: Some(tbs_cert.get_certificate_serial_number().clone()), - issuer: Some(extract_relative_distinguished_name(tbs_cert.get_issuer())?), - validity_not_before: Some(time_to_string(tbs_cert.get_validity_not_before().to_i64())?), - validity_not_after: Some(time_to_string(tbs_cert.get_validity_not_after().to_i64())?), - subject: extract_relative_distinguished_name(tbs_cert.get_subject())?, - subject_public_key_algorithm: Some(tbs_cert.get_subject_public_key_algorithm().clone()), + certificate_type: Some(tbs_cert.c509_certificate_type()), + serial_number: Some(tbs_cert.certificate_serial_number().clone()), + issuer: Some(extract_relative_distinguished_name(tbs_cert.issuer())?), + validity_not_before: Some(time_to_string( + tbs_cert.validity_not_before().clone().into(), + )?), + validity_not_after: Some(time_to_string( + tbs_cert.validity_not_after().clone().into(), + )?), + subject: extract_relative_distinguished_name(tbs_cert.subject())?, + subject_public_key_algorithm: Some(tbs_cert.subject_public_key_algorithm().clone()), // Return a hex formation of the public key - subject_public_key: tbs_cert.get_subject_public_key().encode_hex(), - extensions: tbs_cert.get_extensions().clone(), - issuer_signature_algorithm: Some(tbs_cert.get_issuer_signature_algorithm().clone()), - issuer_signature_value: c509.get_issuer_signature_value().clone(), + subject_public_key: tbs_cert.subject_public_key().encode_hex(), + extensions: tbs_cert.extensions().clone(), + issuer_signature_algorithm: Some(tbs_cert.issuer_signature_algorithm().clone()), + issuer_signature_value: c509.issuer_signature_value().clone(), }; let data = serde_json::to_string(&c509_json)?; @@ -318,7 +322,7 @@ fn decode(file: &PathBuf, output: Option) -> anyhow::Result<()> { /// Extract a `RelativeDistinguishedName` from a `Name`. fn extract_relative_distinguished_name(name: &Name) -> anyhow::Result { - match name.get_value() { + match name.value() { NameValue::RelativeDistinguishedName(rdn) => Ok(rdn.clone()), _ => Err(anyhow::anyhow!("Expected RelativeDistinguishedName")), } diff --git a/rust/c509-certificate/src/algorithm_identifier.rs b/rust/c509-certificate/src/algorithm_identifier.rs index 89bca4b118d..839e997e83c 100644 --- a/rust/c509-certificate/src/algorithm_identifier.rs +++ b/rust/c509-certificate/src/algorithm_identifier.rs @@ -23,7 +23,7 @@ use crate::oid::C509oid; #[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] pub struct AlgorithmIdentifier { /// A `C509oid` - oid: C509oid, + c509_oid: C509oid, /// An optional parameter string param: Option, } @@ -33,18 +33,20 @@ impl AlgorithmIdentifier { #[must_use] pub fn new(oid: Oid<'static>, param: Option) -> Self { Self { - oid: C509oid::new(oid), + c509_oid: C509oid::new(oid), param, } } /// Get the OID. - pub(crate) fn get_oid(&self) -> Oid<'static> { - self.oid.clone().get_oid() + #[must_use] + pub fn oid(&self) -> &Oid<'static> { + self.c509_oid.oid() } /// Get the parameter. - pub(crate) fn get_param(&self) -> &Option { + #[must_use] + pub fn param(&self) -> &Option { &self.param } } @@ -57,12 +59,12 @@ impl Encode<()> for AlgorithmIdentifier { // [ algorithm: ~oid, parameters: bytes ] Some(p) => { e.array(2)?; - self.oid.encode(e, ctx)?; + self.c509_oid.encode(e, ctx)?; e.bytes(p.as_bytes())?; }, // ~oid None => { - self.oid.encode(e, ctx)?; + self.c509_oid.encode(e, ctx)?; }, } Ok(()) @@ -82,11 +84,14 @@ impl Decode<'_, ()> for AlgorithmIdentifier { let c509_oid = C509oid::decode(d, ctx)?; let param = String::from_utf8(d.bytes()?.to_vec()).map_err(minicbor::decode::Error::message)?; - Ok(AlgorithmIdentifier::new(c509_oid.get_oid(), Some(param))) + Ok(AlgorithmIdentifier::new( + c509_oid.oid().clone(), + Some(param), + )) // ~oid } else { let oid = C509oid::decode(d, ctx)?; - Ok(AlgorithmIdentifier::new(oid.get_oid(), None)) + Ok(AlgorithmIdentifier::new(oid.oid().clone(), None)) } } } diff --git a/rust/c509-certificate/src/attributes/attribute.rs b/rust/c509-certificate/src/attributes/attribute.rs index 1aa99740a91..31b2be95b16 100644 --- a/rust/c509-certificate/src/attributes/attribute.rs +++ b/rust/c509-certificate/src/attributes/attribute.rs @@ -45,23 +45,21 @@ impl Attribute { self.value.push(value); } - /// Get the registered OID of `Attribute`. - pub(crate) fn get_registered_oid(&self) -> &C509oidRegistered { - &self.registered_oid - } - /// Get the value of `Attribute`. - pub(crate) fn get_value(&self) -> &Vec { + #[must_use] + pub fn value(&self) -> &[AttributeValue] { &self.value } + /// Get the registered OID of `Attribute`. + pub(crate) fn registered_oid(&self) -> &C509oidRegistered { + &self.registered_oid + } + /// Set whether `Attribute` can be PEN encoded. - pub(crate) fn set_pen_supported(self) -> Self { - Self { - registered_oid: self.registered_oid.pen_encoded(), - multi_value: self.multi_value, - value: self.value, - } + pub(crate) fn set_pen_supported(mut self) -> Self { + self.registered_oid = self.registered_oid.pen_encoded(); + self } /// Set whether `Attribute` can have multiple value. @@ -98,7 +96,7 @@ impl Serialize for Attribute { fn serialize(&self, serializer: S) -> Result where S: serde::Serializer { let helper = Helper { - oid: self.registered_oid.get_c509_oid().get_oid().to_string(), + oid: self.registered_oid.c509_oid().oid().to_string(), value: self.value.clone(), }; helper.serialize(serializer) @@ -112,14 +110,14 @@ impl Encode<()> for Attribute { // Encode CBOR int if available if let Some(&oid) = self .registered_oid - .get_table() + .table() .get_map() - .get_by_right(&self.registered_oid.get_c509_oid().get_oid()) + .get_by_right(self.registered_oid.c509_oid().oid()) { e.i16(oid)?; } else { // Encode unwrapped CBOR OID or CBOR PEN - self.registered_oid.get_c509_oid().encode(e, ctx)?; + self.registered_oid.c509_oid().encode(e, ctx)?; } // Check if the attribute value is empty @@ -151,7 +149,7 @@ impl Decode<'_, ()> for Attribute { } else { // Handle unwrapped CBOR OID or CBOR PEN let c509_oid: C509oid = d.decode()?; - Attribute::new(c509_oid.get_oid()) + Attribute::new(c509_oid.oid().clone()) }; // Handle attribute value diff --git a/rust/c509-certificate/src/attributes/mod.rs b/rust/c509-certificate/src/attributes/mod.rs index b188558e18a..a340d0cedf9 100644 --- a/rust/c509-certificate/src/attributes/mod.rs +++ b/rust/c509-certificate/src/attributes/mod.rs @@ -23,12 +23,6 @@ mod data; #[derive(Debug, Clone, PartialEq)] pub struct Attributes(Vec); -impl Default for Attributes { - fn default() -> Self { - Self::new() - } -} - impl Attributes { /// Create a new instance of `Attributes` as empty vector. #[must_use] @@ -36,13 +30,25 @@ impl Attributes { Self(Vec::new()) } + /// Get a reference to the vector of `Attribute`. + #[must_use] + pub fn attributes(&self) -> &[Attribute] { + &self.0 + } + /// Add an `Attribute` to the `Attributes`. /// and set `Attribute` value to support multiple value. - pub fn add_attr(&mut self, attribute: Attribute) { + pub fn add_attribute(&mut self, attribute: Attribute) { self.0.push(attribute.set_multi_value()); } } +impl Default for Attributes { + fn default() -> Self { + Self::new() + } +} + impl Encode<()> for Attributes { fn encode( &self, e: &mut Encoder, ctx: &mut (), @@ -73,7 +79,7 @@ impl Decode<'_, ()> for Attributes { for _ in 0..len { let attribute = Attribute::decode(d, &mut ())?; - attributes.add_attr(attribute); + attributes.add_attribute(attribute); } Ok(attributes) @@ -97,7 +103,7 @@ mod test_attributes { attr.add_value(AttributeValue::Text("example@example.com".to_string())); attr.add_value(AttributeValue::Text("example@example.com".to_string())); let mut attributes = Attributes::new(); - attributes.add_attr(attr); + attributes.add_attribute(attr); attributes .encode(&mut encoder, &mut ()) .expect("Failed to encode Attributes"); diff --git a/rust/c509-certificate/src/big_uint.rs b/rust/c509-certificate/src/big_uint.rs index a28e93305d8..bf7aec98c85 100644 --- a/rust/c509-certificate/src/big_uint.rs +++ b/rust/c509-certificate/src/big_uint.rs @@ -21,6 +21,18 @@ impl UnwrappedBigUint { } } +impl From for UnwrappedBigUint { + fn from(value: u64) -> Self { + UnwrappedBigUint::new(value) + } +} + +impl From for u64 { + fn from(unwrapped_big_uint: UnwrappedBigUint) -> Self { + unwrapped_big_uint.0 + } +} + impl Encode<()> for UnwrappedBigUint { fn encode( &self, e: &mut Encoder, _ctx: &mut (), diff --git a/rust/c509-certificate/src/c509.rs b/rust/c509-certificate/src/c509.rs index 75cc23bfa9f..8c2543d907d 100644 --- a/rust/c509-certificate/src/c509.rs +++ b/rust/c509-certificate/src/c509.rs @@ -26,13 +26,13 @@ impl C509 { /// Get the `TBSCertificate` of the C509 Certificate. #[must_use] - pub fn get_tbs_cert(&self) -> &TbsCert { + pub fn tbs_cert(&self) -> &TbsCert { &self.tbs_cert } /// Get the `IssuerSignatureValue` of the C509 Certificate. #[must_use] - pub fn get_issuer_signature_value(&self) -> &Option> { + pub fn issuer_signature_value(&self) -> &Option> { &self.issuer_signature_value } } diff --git a/rust/c509-certificate/src/extensions/alt_name.rs b/rust/c509-certificate/src/extensions/alt_name.rs index f5448ff032d..9681fec63df 100644 --- a/rust/c509-certificate/src/extensions/alt_name.rs +++ b/rust/c509-certificate/src/extensions/alt_name.rs @@ -21,9 +21,9 @@ impl AlternativeName { Self(value) } - /// Get the inner of Alternative Name. + /// Get the general name which can be general names or text. #[must_use] - pub fn get_inner(&self) -> &GeneralNamesOrText { + pub fn general_name(&self) -> &GeneralNamesOrText { &self.0 } } @@ -61,12 +61,12 @@ impl Encode<()> for GeneralNamesOrText { match self { GeneralNamesOrText::GeneralNames(gns) => { let gn = gns - .get_inner() + .general_names() .first() .ok_or(minicbor::encode::Error::message("GeneralNames is empty"))?; // Check whether there is only 1 item in the array which is a DNSName - if gns.get_inner().len() == 1 && gn.get_gn_type().is_dns_name() { - gn.get_gn_value().encode(e, ctx)?; + if gns.general_names().len() == 1 && gn.gn_type().is_dns_name() { + gn.gn_value().encode(e, ctx)?; } else { gns.encode(e, ctx)?; } @@ -89,7 +89,7 @@ impl Decode<'_, ()> for GeneralNamesOrText { GeneralNameValue::Text(d.str()?.to_string()), ); let mut gns = GeneralNames::new(); - gns.add_gn(gn_dns); + gns.add_general_name(gn_dns); Ok(GeneralNamesOrText::GeneralNames(gns)) }, minicbor::data::Type::Array => { @@ -120,7 +120,7 @@ mod test_alt_name { let mut buffer = Vec::new(); let mut encoder = Encoder::new(&mut buffer); let mut gns = GeneralNames::new(); - gns.add_gn(GeneralName::new( + gns.add_general_name(GeneralName::new( GeneralNameTypeRegistry::DNSName, GeneralNameValue::Text("example.com".to_string()), )); @@ -151,7 +151,7 @@ mod test_alt_name { // If only text, it should be GeneralNames with only 1 DNSName let mut gns = GeneralNames::new(); - gns.add_gn(GeneralName::new( + gns.add_general_name(GeneralName::new( GeneralNameTypeRegistry::DNSName, GeneralNameValue::Text("example.com".to_string()), )); diff --git a/rust/c509-certificate/src/extensions/extension/mod.rs b/rust/c509-certificate/src/extensions/extension/mod.rs index b2b82e54926..3978c95da03 100644 --- a/rust/c509-certificate/src/extensions/extension/mod.rs +++ b/rust/c509-certificate/src/extensions/extension/mod.rs @@ -38,19 +38,19 @@ impl Extension { /// Get the value of the `Extension` in `ExtensionValue`. #[must_use] - pub fn get_value(&self) -> &ExtensionValue { + pub fn value(&self) -> &ExtensionValue { &self.value } /// Get the critical flag of the `Extension`. #[must_use] - pub fn get_critical(&self) -> bool { + pub fn critical(&self) -> bool { self.critical } /// Get the registered OID of the `Extension`. #[must_use] - pub fn get_registered_oid(&self) -> &C509oidRegistered { + pub(crate) fn registered_oid(&self) -> &C509oidRegistered { &self.registered_oid } } @@ -81,7 +81,7 @@ impl Serialize for Extension { fn serialize(&self, serializer: S) -> Result where S: serde::Serializer { let helper = Helper { - oid: self.registered_oid.get_c509_oid().get_oid().to_string(), + oid: self.registered_oid.c509_oid().oid().to_string(), value: self.value.clone(), critical: self.critical, }; @@ -100,9 +100,9 @@ impl Encode<()> for Extension { // Handle CBOR int based on OID mapping if let Some(&mapped_oid) = self .registered_oid - .get_table() + .table() .get_map() - .get_by_right(&self.registered_oid.get_c509_oid().get_oid()) + .get_by_right(self.registered_oid.c509_oid().oid()) { // Determine encoded OID value based on critical flag let encoded_oid = if self.critical { @@ -113,7 +113,7 @@ impl Encode<()> for Extension { e.i16(encoded_oid)?; } else { // Handle unwrapped CBOR OID or CBOR PEN - self.registered_oid.get_c509_oid().encode(e, ctx)?; + self.registered_oid.c509_oid().encode(e, ctx)?; if self.critical { e.bool(self.critical)?; } @@ -164,7 +164,7 @@ impl Decode<'_, ()> for Extension { let extension_value = ExtensionValue::Bytes(d.bytes()?.to_vec()); Ok(Extension::new( - c509_oid.get_oid(), + c509_oid.oid().clone(), extension_value, critical, )) diff --git a/rust/c509-certificate/src/extensions/mod.rs b/rust/c509-certificate/src/extensions/mod.rs index 1288f9e642b..ea6d9e2ac61 100644 --- a/rust/c509-certificate/src/extensions/mod.rs +++ b/rust/c509-certificate/src/extensions/mod.rs @@ -34,12 +34,6 @@ static KEY_USAGE_OID: Oid<'static> = oid!(2.5.29 .15); #[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] pub struct Extensions(Vec); -impl Default for Extensions { - fn default() -> Self { - Self::new() - } -} - impl Extensions { /// Create a new instance of `Extensions` as empty vector. #[must_use] @@ -48,17 +42,23 @@ impl Extensions { } /// Add an `Extension` to the `Extensions`. - pub fn add_ext(&mut self, extension: Extension) { + pub fn add_extension(&mut self, extension: Extension) { self.0.push(extension); } /// Get the inner vector of `Extensions`. #[must_use] - pub fn get_inner(&self) -> &Vec { + pub fn extensions(&self) -> &[Extension] { &self.0 } } +impl Default for Extensions { + fn default() -> Self { + Self::new() + } +} + impl Encode<()> for Extensions { fn encode( &self, e: &mut Encoder, ctx: &mut (), @@ -66,16 +66,10 @@ impl Encode<()> for Extensions { // 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 if let Some(extension) = self.0.first() { - if self.0.len() == 1 - && extension.get_registered_oid().get_c509_oid().get_oid() == KEY_USAGE_OID - { - match extension.get_value() { + if self.0.len() == 1 && extension.registered_oid().c509_oid().oid() == &KEY_USAGE_OID { + match extension.value() { ExtensionValue::Int(value) => { - let ku_value = if extension.get_critical() { - -value - } else { - *value - }; + let ku_value = if extension.critical() { -value } else { *value }; e.i64(ku_value)?; return Ok(()); }, @@ -108,7 +102,7 @@ impl Decode<'_, ()> for Extensions { let extension_value = ExtensionValue::Int(value); let mut extensions = Extensions::new(); - extensions.add_ext(Extension::new( + extensions.add_extension(Extension::new( KEY_USAGE_OID.clone(), extension_value, critical, @@ -122,7 +116,7 @@ impl Decode<'_, ()> for Extensions { let mut extensions = Extensions::new(); for _ in 0..len { let extension = Extension::decode(d, &mut ())?; - extensions.add_ext(extension); + extensions.add_extension(extension); } Ok(extensions) @@ -141,7 +135,7 @@ mod test_extensions { let mut encoder = Encoder::new(&mut buffer); let mut exts = Extensions::new(); - exts.add_ext(Extension::new( + exts.add_extension(Extension::new( oid!(2.5.29 .15), ExtensionValue::Int(2), false, @@ -164,7 +158,7 @@ mod test_extensions { let mut encoder = Encoder::new(&mut buffer); let mut exts = Extensions::new(); - exts.add_ext(Extension::new( + exts.add_extension(Extension::new( oid!(2.5.29 .15), ExtensionValue::Int(2), true, @@ -187,13 +181,13 @@ mod test_extensions { let mut encoder = Encoder::new(&mut buffer); let mut exts = Extensions::new(); - exts.add_ext(Extension::new( + exts.add_extension(Extension::new( oid!(2.5.29 .15), ExtensionValue::Int(2), false, )); - exts.add_ext(Extension::new( + exts.add_extension(Extension::new( oid!(2.5.29 .14), ExtensionValue::Bytes([1, 2, 3, 4].to_vec()), false, diff --git a/rust/c509-certificate/src/general_names/general_name.rs b/rust/c509-certificate/src/general_names/general_name.rs index 0549f37bb4c..e6da848f19d 100644 --- a/rust/c509-certificate/src/general_names/general_name.rs +++ b/rust/c509-certificate/src/general_names/general_name.rs @@ -37,13 +37,13 @@ impl GeneralName { /// Get the `GeneralName` type. #[must_use] - pub fn get_gn_type(&self) -> &GeneralNameTypeRegistry { + pub fn gn_type(&self) -> &GeneralNameTypeRegistry { &self.gn_type } /// Get the value of the `GeneralName` in `GeneralNameValue`. #[must_use] - pub fn get_gn_value(&self) -> &GeneralNameValue { + pub fn gn_value(&self) -> &GeneralNameValue { &self.value } } diff --git a/rust/c509-certificate/src/general_names/mod.rs b/rust/c509-certificate/src/general_names/mod.rs index 1998733be30..240c83b32c8 100644 --- a/rust/c509-certificate/src/general_names/mod.rs +++ b/rust/c509-certificate/src/general_names/mod.rs @@ -18,12 +18,6 @@ use serde::{Deserialize, Serialize}; #[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] pub struct GeneralNames(Vec); -impl Default for GeneralNames { - fn default() -> Self { - Self::new() - } -} - impl GeneralNames { /// Create a new instance of `GeneralNames` as empty vector. #[must_use] @@ -32,17 +26,23 @@ impl GeneralNames { } /// Add a new `GeneralName` to the `GeneralNames`. - pub fn add_gn(&mut self, gn: GeneralName) { + pub fn add_general_name(&mut self, gn: GeneralName) { self.0.push(gn); } /// Get the inner of `GeneralName`. #[must_use] - pub fn get_inner(&self) -> &Vec { + pub fn general_names(&self) -> &Vec { &self.0 } } +impl Default for GeneralNames { + fn default() -> Self { + Self::new() + } +} + impl Encode<()> for GeneralNames { fn encode( &self, e: &mut Encoder, ctx: &mut (), @@ -68,7 +68,7 @@ impl Decode<'_, ()> for GeneralNames { ))?; let mut gn = GeneralNames::new(); for _ in 0..len / 2 { - gn.add_gn(GeneralName::decode(d, ctx)?); + gn.add_general_name(GeneralName::decode(d, ctx)?); } Ok(gn) } @@ -94,22 +94,22 @@ mod test_general_names { let mut encoder = Encoder::new(&mut buffer); let mut gns = GeneralNames::new(); - gns.add_gn(GeneralName::new( + gns.add_general_name(GeneralName::new( GeneralNameTypeRegistry::DNSName, GeneralNameValue::Text("example.com".to_string()), )); - gns.add_gn(GeneralName::new( + gns.add_general_name(GeneralName::new( GeneralNameTypeRegistry::OtherNameHardwareModuleName, GeneralNameValue::OtherNameHWModuleName(OtherNameHardwareModuleName::new( oid!(2.16.840 .1 .101 .3 .4 .2 .1), vec![0x01, 0x02, 0x03, 0x04], )), )); - gns.add_gn(GeneralName::new( + gns.add_general_name(GeneralName::new( GeneralNameTypeRegistry::IPAddress, GeneralNameValue::Bytes(Ipv4Addr::new(192, 168, 1, 1).octets().to_vec()), )); - gns.add_gn(GeneralName::new( + gns.add_general_name(GeneralName::new( GeneralNameTypeRegistry::RegisteredID, GeneralNameValue::Oid(C509oid::new(oid!(2.16.840 .1 .101 .3 .4 .2 .1))), )); @@ -130,15 +130,15 @@ mod test_general_names { let mut encoder = Encoder::new(&mut buffer); let mut gns = GeneralNames::new(); - gns.add_gn(GeneralName::new( + gns.add_general_name(GeneralName::new( GeneralNameTypeRegistry::DNSName, GeneralNameValue::Text("example.com".to_string()), )); - gns.add_gn(GeneralName::new( + gns.add_general_name(GeneralName::new( GeneralNameTypeRegistry::DNSName, GeneralNameValue::Text("example.com".to_string()), )); - gns.add_gn(GeneralName::new( + gns.add_general_name(GeneralName::new( GeneralNameTypeRegistry::DNSName, GeneralNameValue::Text("example.com".to_string()), )); 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 b57697ce79a..9e6ba200005 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,6 +29,18 @@ impl OtherNameHardwareModuleName { hw_serial_num, } } + + /// Get the c509 OID hardware type. + #[must_use] + pub fn hw_type(&self) -> &C509oid { + &self.hw_type + } + + /// Get the hardware serial number. + #[must_use] + pub fn hw_serial_num(&self) -> &[u8] { + &self.hw_serial_num + } } impl Encode<()> for OtherNameHardwareModuleName { @@ -48,7 +60,7 @@ impl<'a> Decode<'a, ()> for OtherNameHardwareModuleName { let hw_type = C509oid::decode(d, ctx)?; let hw_serial_num = d.bytes()?.to_vec(); Ok(OtherNameHardwareModuleName::new( - hw_type.get_oid(), + hw_type.oid().clone(), hw_serial_num, )) } diff --git a/rust/c509-certificate/src/issuer_sig_algo/mod.rs b/rust/c509-certificate/src/issuer_sig_algo/mod.rs index ff63acae59a..db15c1f6884 100644 --- a/rust/c509-certificate/src/issuer_sig_algo/mod.rs +++ b/rust/c509-certificate/src/issuer_sig_algo/mod.rs @@ -37,7 +37,20 @@ impl IssuerSignatureAlgorithm { algo_identifier: AlgorithmIdentifier::new(oid, param), } } + + /// Get the algorithm identifier. + #[must_use] + pub fn algo_identifier(&self) -> &AlgorithmIdentifier { + &self.algo_identifier + } + + /// Get the registered OID. + #[allow(dead_code)] + pub(crate) fn registered_oid(&self) -> &C509oidRegistered { + &self.registered_oid + } } + /// Helper struct for deserialize and serialize `IssuerSignatureAlgorithm`. #[derive(Debug, Deserialize, Serialize)] struct Helper { @@ -62,8 +75,8 @@ impl Serialize for IssuerSignatureAlgorithm { fn serialize(&self, serializer: S) -> Result where S: serde::Serializer { let helper = Helper { - oid: self.registered_oid.get_c509_oid().get_oid().to_string(), - param: self.algo_identifier.get_param().clone(), + oid: self.registered_oid.c509_oid().oid().to_string(), + param: self.algo_identifier.param().clone(), }; helper.serialize(serializer) } @@ -75,9 +88,9 @@ impl Encode<()> for IssuerSignatureAlgorithm { ) -> Result<(), minicbor::encode::Error> { if let Some(&i) = self .registered_oid - .get_table() + .table() .get_map() - .get_by_right(&self.registered_oid.get_c509_oid().get_oid()) + .get_by_right(self.registered_oid.c509_oid().oid()) { e.i16(i)?; } else { @@ -99,8 +112,8 @@ impl Decode<'_, ()> for IssuerSignatureAlgorithm { _ => { let algo_identifier = AlgorithmIdentifier::decode(d, ctx)?; Ok(IssuerSignatureAlgorithm::new( - algo_identifier.get_oid(), - algo_identifier.get_param().clone(), + algo_identifier.oid().clone(), + algo_identifier.param().clone(), )) }, } diff --git a/rust/c509-certificate/src/lib.rs b/rust/c509-certificate/src/lib.rs index ae223655b93..b242c6a0003 100644 --- a/rust/c509-certificate/src/lib.rs +++ b/rust/c509-certificate/src/lib.rs @@ -104,8 +104,8 @@ pub fn verify(c509: &[u8], public_key: &PublicKey) -> anyhow::Result<()> { let c509 = C509::decode(&mut d, &mut ())?; let mut encoded_tbs = Vec::new(); let mut encoder = minicbor::Encoder::new(&mut encoded_tbs); - c509.get_tbs_cert().encode(&mut encoder, &mut ())?; - let issuer_sig = c509.get_issuer_signature_value().clone().ok_or(anyhow!( + c509.tbs_cert().encode(&mut encoder, &mut ())?; + let issuer_sig = c509.issuer_signature_value().clone().ok_or(anyhow!( "Signature verification failed, No issuer signature" ))?; public_key.verify(&encoded_tbs, &issuer_sig) diff --git a/rust/c509-certificate/src/name/mod.rs b/rust/c509-certificate/src/name/mod.rs index c4144390775..09134f954bb 100644 --- a/rust/c509-certificate/src/name/mod.rs +++ b/rust/c509-certificate/src/name/mod.rs @@ -51,7 +51,7 @@ impl Name { /// Get the value of the `Name`. #[must_use] - pub fn get_value(&self) -> &NameValue { + pub fn value(&self) -> &NameValue { &self.0 } } @@ -91,18 +91,18 @@ impl Encode<()> for NameValue { ) -> Result<(), minicbor::encode::Error> { match self { NameValue::RelativeDistinguishedName(rdn) => { - let attr = rdn.get_attributes(); + let attr = rdn.attributes(); let attr_first = attr.first().ok_or(minicbor::encode::Error::message( "Cannot get the first Attribute", ))?; // If Name contains a single Attribute of type CommonName if attr.len() == 1 - && attr_first.get_registered_oid().get_c509_oid().get_oid() == COMMON_NAME_OID + && attr_first.registered_oid().c509_oid().oid() == &COMMON_NAME_OID { // Get the value of the attribute let cn_value = attr_first - .get_value() + .value() .first() .ok_or(minicbor::encode::Error::message( "Cannot get the first Attribute value", @@ -275,7 +275,7 @@ fn create_rdn_with_cn_attr(text: String) -> NameValue { let mut attr = Attribute::new(COMMON_NAME_OID); attr.add_value(AttributeValue::Text(text)); let mut rdn = RelativeDistinguishedName::new(); - rdn.add_attr(attr); + rdn.add_attribute(attr); NameValue::RelativeDistinguishedName(rdn) } @@ -292,7 +292,7 @@ pub(crate) mod test_name { let mut attr = Attribute::new(oid!(2.5.4 .3)); attr.add_value(AttributeValue::Text("RFC test CA".to_string())); let mut rdn = RelativeDistinguishedName::new(); - rdn.add_attr(attr); + rdn.add_attribute(attr); ( Name::new(NameValue::RelativeDistinguishedName(rdn)), @@ -325,7 +325,7 @@ pub(crate) mod test_name { let mut attr = Attribute::new(oid!(2.5.4 .3)); attr.add_value(AttributeValue::Text("000123abcd".to_string())); let mut rdn = RelativeDistinguishedName::new(); - rdn.add_attr(attr); + rdn.add_attribute(attr); let name = Name::new(NameValue::RelativeDistinguishedName(rdn)); name.encode(&mut encoder, &mut ()) @@ -349,7 +349,7 @@ pub(crate) mod test_name { let mut attr = Attribute::new(oid!(2.5.4 .3)); attr.add_value(AttributeValue::Text("000123ABCD".to_string())); let mut rdn = RelativeDistinguishedName::new(); - rdn.add_attr(attr); + rdn.add_attribute(attr); let name = Name::new(NameValue::RelativeDistinguishedName(rdn)); name.encode(&mut encoder, &mut ()) @@ -370,7 +370,7 @@ pub(crate) mod test_name { let mut attr = Attribute::new(oid!(2.5.4 .3)); attr.add_value(AttributeValue::Text("01-23-45-FF-FE-67-89-AB".to_string())); let mut rdn = RelativeDistinguishedName::new(); - rdn.add_attr(attr); + rdn.add_attribute(attr); ( Name::new(NameValue::RelativeDistinguishedName(rdn)), @@ -404,7 +404,7 @@ pub(crate) mod test_name { let mut attr = Attribute::new(oid!(2.5.4 .3)); attr.add_value(AttributeValue::Text("01-23-45-ff-fe-67-89-AB".to_string())); let mut rdn = RelativeDistinguishedName::new(); - rdn.add_attr(attr); + rdn.add_attribute(attr); let name = Name::new(NameValue::RelativeDistinguishedName(rdn)); name.encode(&mut encoder, &mut ()) @@ -430,7 +430,7 @@ pub(crate) mod test_name { let mut attr = Attribute::new(oid!(2.5.4 .3)); attr.add_value(AttributeValue::Text("01-23-45-67-89-AB-00-01".to_string())); let mut rdn = RelativeDistinguishedName::new(); - rdn.add_attr(attr); + rdn.add_attribute(attr); let name = Name::new(NameValue::RelativeDistinguishedName(rdn)); @@ -452,7 +452,7 @@ pub(crate) mod test_name { let mut attr = Attribute::new(oid!(2.5.4 .3)); attr.add_value(AttributeValue::Text("01-23-45-67-89-ab-00-01".to_string())); let mut rdn = RelativeDistinguishedName::new(); - rdn.add_attr(attr); + rdn.add_attribute(attr); let name = Name::new(NameValue::RelativeDistinguishedName(rdn)); @@ -487,11 +487,11 @@ pub(crate) mod test_name { attr5.add_value(AttributeValue::Text("802.1AR CA".to_string())); let mut rdn = RelativeDistinguishedName::new(); - rdn.add_attr(attr1); - rdn.add_attr(attr2); - rdn.add_attr(attr3); - rdn.add_attr(attr4); - rdn.add_attr(attr5); + rdn.add_attribute(attr1); + rdn.add_attribute(attr2); + rdn.add_attribute(attr3); + rdn.add_attribute(attr4); + rdn.add_attribute(attr5); ( Name::new(NameValue::RelativeDistinguishedName(rdn)), diff --git a/rust/c509-certificate/src/name/rdn.rs b/rust/c509-certificate/src/name/rdn.rs index 13f5eb12f09..7874f509833 100644 --- a/rust/c509-certificate/src/name/rdn.rs +++ b/rust/c509-certificate/src/name/rdn.rs @@ -18,12 +18,6 @@ use crate::attributes::attribute::Attribute; #[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] pub struct RelativeDistinguishedName(Vec); -impl Default for RelativeDistinguishedName { - fn default() -> Self { - Self::new() - } -} - impl RelativeDistinguishedName { /// Create a new instance of `RelativeDistinguishedName` as empty vector. #[must_use] @@ -32,17 +26,24 @@ impl RelativeDistinguishedName { } /// Add an `Attribute` to the `RelativeDistinguishedName`. - pub fn add_attr(&mut self, attribute: Attribute) { + pub fn add_attribute(&mut self, attribute: Attribute) { // RelativeDistinguishedName support pen encoding self.0.push(attribute.set_pen_supported()); } /// Get the a vector of `Attribute`. - pub(crate) fn get_attributes(&self) -> &Vec { + #[must_use] + pub fn attributes(&self) -> &[Attribute] { &self.0 } } +impl Default for RelativeDistinguishedName { + fn default() -> Self { + Self::new() + } +} + impl Encode<()> for RelativeDistinguishedName { // ```cddl // RelativeDistinguishedName = Attribute / [ 2* Attribute ] @@ -87,10 +88,10 @@ impl Decode<'_, ()> for RelativeDistinguishedName { } // The attribute type is included in an array, so divide by 2 for _ in 0..len / 2 { - rdn.add_attr(Attribute::decode(d, ctx)?); + rdn.add_attribute(Attribute::decode(d, ctx)?); } }, - _ => rdn.add_attr(Attribute::decode(d, ctx)?), + _ => rdn.add_attribute(Attribute::decode(d, ctx)?), } Ok(rdn) } @@ -115,7 +116,7 @@ mod test_relative_distinguished_name { attr.add_value(AttributeValue::Text("example@example.com".to_string())); let mut rdn = RelativeDistinguishedName::new(); - rdn.add_attr(attr); + rdn.add_attribute(attr); rdn.encode(&mut encoder, &mut ()) .expect("Failed to encode RDN"); // Email Address: 0x00 @@ -142,8 +143,8 @@ mod test_relative_distinguished_name { attr2.add_value(AttributeValue::Text("example".to_string())); let mut rdns = RelativeDistinguishedName::new(); - rdns.add_attr(attr1); - rdns.add_attr(attr2); + rdns.add_attribute(attr1); + rdns.add_attribute(attr2); rdns.encode(&mut encoder, &mut ()) .expect("Failed to encode RDN"); diff --git a/rust/c509-certificate/src/oid.rs b/rust/c509-certificate/src/oid.rs index 2a8ed92fd9f..1eb1fdacaa1 100644 --- a/rust/c509-certificate/src/oid.rs +++ b/rust/c509-certificate/src/oid.rs @@ -38,21 +38,21 @@ impl C509oidRegistered { } } - /// Is PEN Encoding supported for this OID. + /// Get the `C509oid`. + #[must_use] + pub fn c509_oid(&self) -> &C509oid { + &self.oid + } + + /// Get whether PEN Encoding supported for this OID. /// Depends on each registration table. pub(crate) fn pen_encoded(mut self) -> Self { self.oid.pen_supported = true; self } - /// Get the `C509oid`. - #[must_use] - pub fn get_c509_oid(&self) -> C509oid { - self.oid.clone() - } - /// Get the registration table. - pub(crate) fn get_table(&self) -> &'static IntegerToOidTable { + pub(crate) fn table(&self) -> &'static IntegerToOidTable { self.registration_table } } @@ -114,8 +114,8 @@ impl C509oid { /// Get the underlying OID of the `C509oid` #[must_use] - pub fn get_oid(self) -> Oid<'static> { - self.oid.clone() + pub fn oid(&self) -> &Oid<'static> { + &self.oid } } 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 014ad6e6cc7..bd59a9b6112 100644 --- a/rust/c509-certificate/src/subject_pub_key_algo/mod.rs +++ b/rust/c509-certificate/src/subject_pub_key_algo/mod.rs @@ -39,6 +39,18 @@ impl SubjectPubKeyAlgorithm { algo_identifier: AlgorithmIdentifier::new(oid, param), } } + + /// Get the algorithm identifier. + #[must_use] + pub fn algo_identifier(&self) -> &AlgorithmIdentifier { + &self.algo_identifier + } + + /// Get the registered OID. + #[allow(dead_code)] + pub(crate) fn registered_oid(&self) -> &C509oidRegistered { + &self.registered_oid + } } /// Helper struct for deserialize and serialize `SubjectPubKeyAlgorithm`. @@ -65,8 +77,8 @@ impl Serialize for SubjectPubKeyAlgorithm { fn serialize(&self, serializer: S) -> Result where S: serde::Serializer { let helper = Helper { - oid: self.registered_oid.get_c509_oid().get_oid().to_string(), - param: self.algo_identifier.get_param().clone(), + oid: self.registered_oid.c509_oid().oid().to_string(), + param: self.algo_identifier.param().clone(), }; helper.serialize(serializer) } @@ -78,9 +90,9 @@ impl Encode<()> for SubjectPubKeyAlgorithm { ) -> Result<(), minicbor::encode::Error> { if let Some(&i) = self .registered_oid - .get_table() + .table() .get_map() - .get_by_right(&self.registered_oid.get_c509_oid().get_oid()) + .get_by_right(self.registered_oid.c509_oid().oid()) { e.i16(i)?; } else { @@ -100,8 +112,8 @@ impl Decode<'_, ()> for SubjectPubKeyAlgorithm { } else { let algo_identifier = AlgorithmIdentifier::decode(d, ctx)?; Ok(SubjectPubKeyAlgorithm::new( - algo_identifier.get_oid(), - algo_identifier.get_param().clone(), + algo_identifier.oid().clone(), + algo_identifier.param().clone(), )) } } diff --git a/rust/c509-certificate/src/tbs_cert.rs b/rust/c509-certificate/src/tbs_cert.rs index c9f4b555856..9a4dfe598aa 100644 --- a/rust/c509-certificate/src/tbs_cert.rs +++ b/rust/c509-certificate/src/tbs_cert.rs @@ -60,61 +60,61 @@ impl TbsCert { /// Get the certificate type. #[must_use] - pub fn get_c509_certificate_type(&self) -> u8 { + pub fn c509_certificate_type(&self) -> u8 { self.c509_certificate_type } /// Get the certificate serial number. #[must_use] - pub fn get_certificate_serial_number(&self) -> &UnwrappedBigUint { + pub fn certificate_serial_number(&self) -> &UnwrappedBigUint { &self.certificate_serial_number } /// Get the issuer. #[must_use] - pub fn get_issuer(&self) -> &Name { + pub fn issuer(&self) -> &Name { &self.issuer } /// Get the validity not before. #[must_use] - pub fn get_validity_not_before(&self) -> &Time { + pub fn validity_not_before(&self) -> &Time { &self.validity_not_before } /// Get the validity not after. #[must_use] - pub fn get_validity_not_after(&self) -> &Time { + pub fn validity_not_after(&self) -> &Time { &self.validity_not_after } /// Get the subject. #[must_use] - pub fn get_subject(&self) -> &Name { + pub fn subject(&self) -> &Name { &self.subject } /// Get the subject public key algorithm. #[must_use] - pub fn get_subject_public_key_algorithm(&self) -> &SubjectPubKeyAlgorithm { + pub fn subject_public_key_algorithm(&self) -> &SubjectPubKeyAlgorithm { &self.subject_public_key_algorithm } /// Get the subject public key. #[must_use] - pub fn get_subject_public_key(&self) -> &[u8] { + pub fn subject_public_key(&self) -> &[u8] { &self.subject_public_key } /// Get the extensions. #[must_use] - pub fn get_extensions(&self) -> &Extensions { + pub fn extensions(&self) -> &Extensions { &self.extensions } /// Get the issuer signature algorithm. #[must_use] - pub fn get_issuer_signature_algorithm(&self) -> &IssuerSignatureAlgorithm { + pub fn issuer_signature_algorithm(&self) -> &IssuerSignatureAlgorithm { &self.issuer_signature_algorithm } } @@ -253,7 +253,7 @@ pub(crate) mod test_tbs_cert { pub(crate) fn tbs() -> TbsCert { fn extensions() -> Extensions { let mut exts = Extensions::new(); - exts.add_ext(Extension::new( + exts.add_extension(Extension::new( oid!(2.5.29 .15), ExtensionValue::Int(1), false, @@ -385,24 +385,24 @@ pub(crate) mod test_tbs_cert { attr6.add_value(AttributeValue::Text("Wt1234".to_string())); let mut rdn = RelativeDistinguishedName::new(); - rdn.add_attr(attr1); - rdn.add_attr(attr2); - rdn.add_attr(attr3); - rdn.add_attr(attr4); - rdn.add_attr(attr5); - rdn.add_attr(attr6); + rdn.add_attribute(attr1); + rdn.add_attribute(attr2); + rdn.add_attribute(attr3); + rdn.add_attribute(attr4); + rdn.add_attribute(attr5); + rdn.add_attribute(attr6); Name::new(NameValue::RelativeDistinguishedName(rdn)) } fn extensions() -> Extensions { let mut exts = Extensions::new(); - exts.add_ext(Extension::new( + exts.add_extension(Extension::new( oid!(2.5.29 .19), ExtensionValue::Int(-2), false, )); - exts.add_ext(Extension::new( + exts.add_extension(Extension::new( oid!(2.5.29 .14), ExtensionValue::Bytes( [ @@ -413,7 +413,7 @@ pub(crate) mod test_tbs_cert { ), false, )); - exts.add_ext(Extension::new( + exts.add_extension(Extension::new( oid!(2.5.29 .15), ExtensionValue::Int(5), true, @@ -422,12 +422,12 @@ pub(crate) mod test_tbs_cert { let hw = OtherNameHardwareModuleName::new(oid!(1.3.6 .1 .4 .1 .6175 .10 .1), vec![ 0x01, 0x02, 0x03, 0x04, ]); - gns.add_gn(GeneralName::new( + gns.add_general_name(GeneralName::new( GeneralNameTypeRegistry::OtherNameHardwareModuleName, GeneralNameValue::OtherNameHWModuleName(hw), )); - exts.add_ext(Extension::new( + exts.add_extension(Extension::new( oid!(2.5.29 .17), ExtensionValue::AlternativeName(AlternativeName::new( GeneralNamesOrText::GeneralNames(gns), diff --git a/rust/c509-certificate/src/time.rs b/rust/c509-certificate/src/time.rs index 48c370f22e6..45776ce94b5 100644 --- a/rust/c509-certificate/src/time.rs +++ b/rust/c509-certificate/src/time.rs @@ -16,11 +16,17 @@ impl Time { pub fn new(time: i64) -> Self { Self(time) } +} - /// Get the time in i64. - #[must_use] - pub fn to_i64(&self) -> i64 { - self.0 +impl From for Time { + fn from(value: i64) -> Self { + Time::new(value) + } +} + +impl From