Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions rust/c509-certificate/examples/cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,22 +297,26 @@ fn decode(file: &PathBuf, output: Option<PathBuf>) -> 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)?;
Expand All @@ -327,7 +331,7 @@ fn decode(file: &PathBuf, output: Option<PathBuf>) -> anyhow::Result<()> {

/// Extract a `Attributes` from a `Name`.
fn extract_attributes(name: &Name) -> anyhow::Result<Attributes> {
match name.get_value() {
match name.value() {
NameValue::Attributes(attrs) => Ok(attrs.clone()),
_ => Err(anyhow::anyhow!("Expected Attributes")),
}
Expand Down
23 changes: 14 additions & 9 deletions rust/c509-certificate/src/algorithm_identifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
}
Expand All @@ -33,18 +33,20 @@ impl AlgorithmIdentifier {
#[must_use]
pub fn new(oid: Oid<'static>, param: Option<String>) -> 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<String> {
#[must_use]
pub fn param(&self) -> &Option<String> {
&self.param
}
}
Expand All @@ -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(())
Expand All @@ -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))
}
}
}
27 changes: 14 additions & 13 deletions rust/c509-certificate/src/attributes/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AttributeValue> {
&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.
Expand Down Expand Up @@ -88,7 +89,7 @@ impl Serialize for Attribute {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
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)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions rust/c509-certificate/src/attributes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@ mod data;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Attributes(Vec<Attribute>);

impl Default for Attributes {
fn default() -> Self {
Self::new()
}
}

impl Attributes {
/// Create a new instance of `Attributes` as empty vector.
#[must_use]
Expand All @@ -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<W: Write>(
&self, e: &mut Encoder<W>, ctx: &mut (),
Expand Down Expand Up @@ -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)
Expand All @@ -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");
Expand Down
12 changes: 12 additions & 0 deletions rust/c509-certificate/src/big_uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ impl UnwrappedBigUint {
}
}

impl From<u64> for UnwrappedBigUint {
fn from(value: u64) -> Self {
UnwrappedBigUint::new(value)
}
}

impl From<UnwrappedBigUint> for u64 {
fn from(unwrapped_big_uint: UnwrappedBigUint) -> Self {
unwrapped_big_uint.0
}
}

impl Encode<()> for UnwrappedBigUint {
fn encode<W: Write>(
&self, e: &mut Encoder<W>, _ctx: &mut (),
Expand Down
4 changes: 2 additions & 2 deletions rust/c509-certificate/src/c509.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<u8>> {
pub fn issuer_signature_value(&self) -> &Option<Vec<u8>> {
&self.issuer_signature_value
}
}
Expand Down
16 changes: 8 additions & 8 deletions rust/c509-certificate/src/extensions/alt_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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)?;
}
Expand All @@ -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 => {
Expand Down Expand Up @@ -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()),
));
Expand Down Expand Up @@ -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()),
));
Expand Down
21 changes: 10 additions & 11 deletions rust/c509-certificate/src/extensions/extension/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -80,7 +80,7 @@ impl Serialize for Extension {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
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,
};
Expand All @@ -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<W: Write>(
&self, e: &mut Encoder<W>, ctx: &mut (),
) -> Result<(), minicbor::encode::Error<W::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 {
Expand All @@ -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)?;
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
))
Expand Down
Loading