diff --git a/rust/c509-certificate/examples/cli/main.rs b/rust/c509-certificate/examples/cli/main.rs index e7c662de28e..9ef57c157f6 100644 --- a/rust/c509-certificate/examples/cli/main.rs +++ b/rust/c509-certificate/examples/cli/main.rs @@ -297,22 +297,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_signature_algorithm: Some(tbs_cert.get_issuer_signature_algorithm().clone()), - issuer: Some(extract_attributes(tbs_cert.get_issuer())?), - validity_not_before: Some(time_to_string(tbs_cert.get_validity_not_before().to_u64())?), - validity_not_after: Some(time_to_string(tbs_cert.get_validity_not_after().to_u64())?), - subject: extract_attributes(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_signature_algorithm: Some(tbs_cert.issuer_signature_algorithm().clone()), + issuer: Some(extract_attributes(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_attributes(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_value: c509.get_issuer_signature_value().clone(), + subject_public_key: tbs_cert.subject_public_key().encode_hex(), + extensions: tbs_cert.extensions().clone(), + issuer_signature_value: c509.issuer_signature_value().clone(), }; let data = serde_json::to_string(&c509_json)?; @@ -327,7 +331,7 @@ fn decode(file: &PathBuf, output: Option) -> anyhow::Result<()> { /// Extract a `Attributes` from a `Name`. fn extract_attributes(name: &Name) -> anyhow::Result { - match name.get_value() { + match name.value() { NameValue::Attributes(attrs) => Ok(attrs.clone()), _ => Err(anyhow::anyhow!("Expected Attributes")), } diff --git a/rust/c509-certificate/src/algorithm_identifier.rs b/rust/c509-certificate/src/algorithm_identifier.rs index 285878c975d..d6a3e1e1ccb 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 ce5e0b74881..b1da9793cc6 100644 --- a/rust/c509-certificate/src/attributes/attribute.rs +++ b/rust/c509-certificate/src/attributes/attribute.rs @@ -39,19 +39,20 @@ impl Attribute { } } - /// Add a value to `Attribute`. - pub fn add_value(&mut self, value: AttributeValue) { - self.value.push(value); + /// Get the value of `Attribute`. + #[must_use] + pub fn value(&self) -> &[AttributeValue] { + &self.value } /// Get the registered OID of `Attribute`. - pub(crate) fn get_registered_oid(&self) -> &C509oidRegistered { + pub(crate) fn registered_oid(&self) -> &C509oidRegistered { &self.registered_oid } - /// Get the value of `Attribute`. - pub(crate) fn get_value(&self) -> &Vec { - &self.value + /// Add a value to `Attribute`. + pub fn add_value(&mut self, value: AttributeValue) { + self.value.push(value); } /// Set whether `Attribute` can have multiple value. @@ -88,7 +89,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) @@ -102,14 +103,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)?; + // Encode unwrapped CBOR OID + self.registered_oid().c509_oid().encode(e, ctx)?; } // Check if the attribute value is empty @@ -141,7 +142,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 ead810ec22e..898c3233408 100644 --- a/rust/c509-certificate/src/attributes/mod.rs +++ b/rust/c509-certificate/src/attributes/mod.rs @@ -24,12 +24,6 @@ mod data; #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] 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] @@ -45,11 +39,17 @@ impl Attributes { /// 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 (), @@ -82,7 +82,7 @@ impl Decode<'_, ()> for Attributes { // The attribute type is included in an array, so divide by 2 for _ in 0..len / 2 { let attribute = Attribute::decode(d, &mut ())?; - attributes.add_attr(attribute); + attributes.add_attribute(attribute); } Ok(attributes) @@ -106,7 +106,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 ca87c056173..722965ae08c 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 095ed9c56d5..3c261af0002 100644 --- a/rust/c509-certificate/src/extensions/extension/mod.rs +++ b/rust/c509-certificate/src/extensions/extension/mod.rs @@ -37,19 +37,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 } } @@ -80,7 +80,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, }; @@ -92,16 +92,15 @@ impl Encode<()> for Extension { // Extension can be encoded as: // - (extensionID: int, extensionValue: any) // - (extensionID: ~oid, ? critical: true, extensionValue: bytes) - // - (extensionID: pen, ? critical: true, extensionValue: bytes) fn encode( &self, e: &mut Encoder, ctx: &mut (), ) -> Result<(), minicbor::encode::Error> { // 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 { @@ -111,8 +110,8 @@ 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)?; + // Handle unwrapped CBOR OID + self.registered_oid.c509_oid().encode(e, ctx)?; if self.critical { e.bool(self.critical)?; } @@ -150,7 +149,7 @@ impl Decode<'_, ()> for Extension { )) }, _ => { - // Handle unwrapped CBOR OID or CBOR PEN + // Handle unwrapped CBOR OID let c509_oid = C509oid::decode(d, ctx)?; // Critical flag is optional, so if exist, this mean we have to decode it let critical = if d.datatype()? == minicbor::data::Type::Bool { @@ -163,7 +162,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 51784aaedc4..f97268496d4 100644 --- a/rust/c509-certificate/src/extensions/mod.rs +++ b/rust/c509-certificate/src/extensions/mod.rs @@ -32,12 +32,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] @@ -45,15 +39,21 @@ impl Extensions { Self(Vec::new()) } + /// Get the inner vector of `Extensions`. + #[must_use] + pub fn extensions(&self) -> &[Extension] { + &self.0 + } + /// 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 { - &self.0 +impl Default for Extensions { + fn default() -> Self { + Self::new() } } @@ -64,16 +64,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(()); }, @@ -106,7 +100,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, @@ -120,7 +114,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) @@ -139,7 +133,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, @@ -162,7 +156,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, @@ -185,13 +179,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 e7d400b9ac9..6a15e2ebd70 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 3e0867c33d2..d46413ea52e 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] @@ -31,15 +25,21 @@ impl GeneralNames { Self(Vec::new()) } + /// Get the inner of `GeneralName`. + #[must_use] + pub fn general_names(&self) -> &[GeneralName] { + &self.0 + } + /// 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 { - &self.0 +impl Default for GeneralNames { + fn default() -> Self { + Self::new() } } @@ -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 a2ca40d225a..808de48c1dd 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 6d11d114771..75c04bc0692 100644 --- a/rust/c509-certificate/src/name/mod.rs +++ b/rust/c509-certificate/src/name/mod.rs @@ -48,7 +48,7 @@ impl Name { /// Get the value of the `Name`. #[must_use] - pub fn get_value(&self) -> &NameValue { + pub fn value(&self) -> &NameValue { &self.0 } } @@ -97,12 +97,12 @@ impl Encode<()> for NameValue { ))?; // If Name contains a single Attribute of type CommonName if attrs.attributes().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", @@ -271,7 +271,7 @@ fn create_attributes_with_cn(text: String) -> NameValue { let mut attr = Attribute::new(COMMON_NAME_OID); attr.add_value(AttributeValue::Text(text)); let mut attrs = Attributes::new(); - attrs.add_attr(attr); + attrs.add_attribute(attr); NameValue::Attributes(attrs) } @@ -288,7 +288,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 attrs = Attributes::new(); - attrs.add_attr(attr); + attrs.add_attribute(attr); ( Name::new(NameValue::Attributes(attrs)), @@ -321,7 +321,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 attrs = Attributes::new(); - attrs.add_attr(attr); + attrs.add_attribute(attr); let name = Name::new(NameValue::Attributes(attrs)); name.encode(&mut encoder, &mut ()) @@ -345,7 +345,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 attrs = Attributes::new(); - attrs.add_attr(attr); + attrs.add_attribute(attr); let name = Name::new(NameValue::Attributes(attrs)); name.encode(&mut encoder, &mut ()) @@ -366,7 +366,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 attrs = Attributes::new(); - attrs.add_attr(attr); + attrs.add_attribute(attr); ( Name::new(NameValue::Attributes(attrs)), @@ -400,7 +400,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 attrs = Attributes::new(); - attrs.add_attr(attr); + attrs.add_attribute(attr); let name = Name::new(NameValue::Attributes(attrs)); name.encode(&mut encoder, &mut ()) @@ -426,7 +426,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 attrs = Attributes::new(); - attrs.add_attr(attr); + attrs.add_attribute(attr); let name = Name::new(NameValue::Attributes(attrs)); @@ -448,7 +448,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 attrs = Attributes::new(); - attrs.add_attr(attr); + attrs.add_attribute(attr); let name = Name::new(NameValue::Attributes(attrs)); @@ -484,11 +484,11 @@ pub(crate) mod test_name { attr5.add_value(AttributeValue::Text("802.1AR CA".to_string())); let mut attrs = Attributes::new(); - attrs.add_attr(attr1); - attrs.add_attr(attr2); - attrs.add_attr(attr3); - attrs.add_attr(attr4); - attrs.add_attr(attr5); + attrs.add_attribute(attr1); + attrs.add_attribute(attr2); + attrs.add_attribute(attr3); + attrs.add_attribute(attr4); + attrs.add_attribute(attr5); ( Name::new(NameValue::Attributes(attrs)), diff --git a/rust/c509-certificate/src/oid.rs b/rust/c509-certificate/src/oid.rs index 2db0b90a3c7..6148dcfb43c 100644 --- a/rust/c509-certificate/src/oid.rs +++ b/rust/c509-certificate/src/oid.rs @@ -17,7 +17,7 @@ use crate::tables::IntegerToOidTable; #[derive(Debug, Clone, PartialEq)] pub struct C509oidRegistered { /// The `C509oid`. - oid: C509oid, + c509_oid: C509oid, /// The registration table. registration_table: &'static IntegerToOidTable, } @@ -26,19 +26,19 @@ impl C509oidRegistered { /// Create a new instance of `C509oidRegistered`. pub(crate) fn new(oid: Oid<'static>, table: &'static IntegerToOidTable) -> Self { Self { - oid: C509oid::new(oid), + c509_oid: C509oid::new(oid), registration_table: table, } } /// Get the `C509oid`. #[must_use] - pub fn get_c509_oid(&self) -> C509oid { - self.oid.clone() + pub fn c509_oid(&self) -> &C509oid { + &self.c509_oid } /// Get the registration table. - pub(crate) fn get_table(&self) -> &'static IntegerToOidTable { + pub(crate) fn table(&self) -> &'static IntegerToOidTable { self.registration_table } } @@ -56,6 +56,20 @@ struct Helper { oid: String, } +impl C509oid { + /// Create an new instance of `C509oid`. + #[must_use] + pub fn new(oid: Oid<'static>) -> Self { + Self(oid) + } + + /// Get the underlying OID of the `C509oid` + #[must_use] + pub fn oid(&self) -> &Oid<'static> { + &self.0 + } +} + impl<'de> Deserialize<'de> for C509oid { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de> { @@ -76,21 +90,6 @@ impl Serialize for C509oid { } } -impl C509oid { - /// Create an new instance of `C509oid`. - /// Default value of PEN flag is false - #[must_use] - pub fn new(oid: Oid<'static>) -> Self { - Self(oid) - } - - /// Get the underlying OID of the `C509oid` - #[must_use] - pub fn get_oid(self) -> Oid<'static> { - self.0 - } -} - impl Encode<()> for C509oid { /// Encode an OID /// Encode as an unwrapped OID (~oid) - as bytes string without tag. 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 f347159040e..e0e10551ebf 100644 --- a/rust/c509-certificate/src/tbs_cert.rs +++ b/rust/c509-certificate/src/tbs_cert.rs @@ -62,13 +62,13 @@ 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 } @@ -80,45 +80,51 @@ impl TbsCert { /// 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 issuer_signature_algorithm(&self) -> &IssuerSignatureAlgorithm { + &self.issuer_signature_algorithm + } } impl Encode<()> for TbsCert { @@ -257,7 +263,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, @@ -390,24 +396,24 @@ pub(crate) mod test_tbs_cert { attr6.add_value(AttributeValue::Text("Wt1234".to_string())); let mut attrs = Attributes::new(); - attrs.add_attr(attr1); - attrs.add_attr(attr2); - attrs.add_attr(attr3); - attrs.add_attr(attr4); - attrs.add_attr(attr5); - attrs.add_attr(attr6); + attrs.add_attribute(attr1); + attrs.add_attribute(attr2); + attrs.add_attribute(attr3); + attrs.add_attribute(attr4); + attrs.add_attribute(attr5); + attrs.add_attribute(attr6); Name::new(NameValue::Attributes(attrs)) } 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( [ @@ -418,7 +424,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, @@ -427,12 +433,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 1244473ffa2..6fb9b0896ee 100644 --- a/rust/c509-certificate/src/time.rs +++ b/rust/c509-certificate/src/time.rs @@ -19,13 +19,25 @@ impl Time { Self(time) } - /// Get the time in u64. + /// Get the u64 of `Time`. #[must_use] - pub fn to_u64(&self) -> u64 { + pub fn time(&self) -> u64 { self.0 } } +impl From for Time { + fn from(value: u64) -> Self { + Time::new(value) + } +} + +impl From