Skip to content

Commit c50834a

Browse files
bkioshnstevenj
andauthored
fix(rust/c509-certificate): Rename functions + add necessary functions (#26)
* fix: naming + add neccesary function * fix(rust/c509-certificate): cleanup * fix(rust/c509-certificate): cleanup --------- Co-authored-by: Steven Johnson <[email protected]>
1 parent b94854d commit c50834a

File tree

19 files changed

+256
-187
lines changed

19 files changed

+256
-187
lines changed

rust/c509-certificate/examples/cli/main.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -297,22 +297,26 @@ fn decode(file: &PathBuf, output: Option<PathBuf>) -> anyhow::Result<()> {
297297
let mut d = minicbor::Decoder::new(&cert);
298298
let c509 = c509_certificate::c509::C509::decode(&mut d, &mut ())?;
299299

300-
let tbs_cert = c509.get_tbs_cert();
301-
let is_self_signed = tbs_cert.get_c509_certificate_type() == SELF_SIGNED_INT;
300+
let tbs_cert = c509.tbs_cert();
301+
let is_self_signed = tbs_cert.c509_certificate_type() == SELF_SIGNED_INT;
302302
let c509_json = C509Json {
303303
self_signed: is_self_signed,
304-
certificate_type: Some(tbs_cert.get_c509_certificate_type()),
305-
serial_number: Some(tbs_cert.get_certificate_serial_number().clone()),
306-
issuer_signature_algorithm: Some(tbs_cert.get_issuer_signature_algorithm().clone()),
307-
issuer: Some(extract_attributes(tbs_cert.get_issuer())?),
308-
validity_not_before: Some(time_to_string(tbs_cert.get_validity_not_before().to_u64())?),
309-
validity_not_after: Some(time_to_string(tbs_cert.get_validity_not_after().to_u64())?),
310-
subject: extract_attributes(tbs_cert.get_subject())?,
311-
subject_public_key_algorithm: Some(tbs_cert.get_subject_public_key_algorithm().clone()),
304+
certificate_type: Some(tbs_cert.c509_certificate_type()),
305+
serial_number: Some(tbs_cert.certificate_serial_number().clone()),
306+
issuer_signature_algorithm: Some(tbs_cert.issuer_signature_algorithm().clone()),
307+
issuer: Some(extract_attributes(tbs_cert.issuer())?),
308+
validity_not_before: Some(time_to_string(
309+
tbs_cert.validity_not_before().clone().into(),
310+
)?),
311+
validity_not_after: Some(time_to_string(
312+
tbs_cert.validity_not_after().clone().into(),
313+
)?),
314+
subject: extract_attributes(tbs_cert.subject())?,
315+
subject_public_key_algorithm: Some(tbs_cert.subject_public_key_algorithm().clone()),
312316
// Return a hex formation of the public key
313-
subject_public_key: tbs_cert.get_subject_public_key().encode_hex(),
314-
extensions: tbs_cert.get_extensions().clone(),
315-
issuer_signature_value: c509.get_issuer_signature_value().clone(),
317+
subject_public_key: tbs_cert.subject_public_key().encode_hex(),
318+
extensions: tbs_cert.extensions().clone(),
319+
issuer_signature_value: c509.issuer_signature_value().clone(),
316320
};
317321

318322
let data = serde_json::to_string(&c509_json)?;
@@ -327,7 +331,7 @@ fn decode(file: &PathBuf, output: Option<PathBuf>) -> anyhow::Result<()> {
327331

328332
/// Extract a `Attributes` from a `Name`.
329333
fn extract_attributes(name: &Name) -> anyhow::Result<Attributes> {
330-
match name.get_value() {
334+
match name.value() {
331335
NameValue::Attributes(attrs) => Ok(attrs.clone()),
332336
_ => Err(anyhow::anyhow!("Expected Attributes")),
333337
}

rust/c509-certificate/src/algorithm_identifier.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crate::oid::C509oid;
2323
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
2424
pub struct AlgorithmIdentifier {
2525
/// A `C509oid`
26-
oid: C509oid,
26+
c509_oid: C509oid,
2727
/// An optional parameter string
2828
param: Option<String>,
2929
}
@@ -33,18 +33,20 @@ impl AlgorithmIdentifier {
3333
#[must_use]
3434
pub fn new(oid: Oid<'static>, param: Option<String>) -> Self {
3535
Self {
36-
oid: C509oid::new(oid),
36+
c509_oid: C509oid::new(oid),
3737
param,
3838
}
3939
}
4040

4141
/// Get the OID.
42-
pub(crate) fn get_oid(&self) -> Oid<'static> {
43-
self.oid.clone().get_oid()
42+
#[must_use]
43+
pub fn oid(&self) -> &Oid<'static> {
44+
self.c509_oid.oid()
4445
}
4546

4647
/// Get the parameter.
47-
pub(crate) fn get_param(&self) -> &Option<String> {
48+
#[must_use]
49+
pub fn param(&self) -> &Option<String> {
4850
&self.param
4951
}
5052
}
@@ -57,12 +59,12 @@ impl Encode<()> for AlgorithmIdentifier {
5759
// [ algorithm: ~oid, parameters: bytes ]
5860
Some(p) => {
5961
e.array(2)?;
60-
self.oid.encode(e, ctx)?;
62+
self.c509_oid.encode(e, ctx)?;
6163
e.bytes(p.as_bytes())?;
6264
},
6365
// ~oid
6466
None => {
65-
self.oid.encode(e, ctx)?;
67+
self.c509_oid.encode(e, ctx)?;
6668
},
6769
}
6870
Ok(())
@@ -82,11 +84,14 @@ impl Decode<'_, ()> for AlgorithmIdentifier {
8284
let c509_oid = C509oid::decode(d, ctx)?;
8385
let param =
8486
String::from_utf8(d.bytes()?.to_vec()).map_err(minicbor::decode::Error::message)?;
85-
Ok(AlgorithmIdentifier::new(c509_oid.get_oid(), Some(param)))
87+
Ok(AlgorithmIdentifier::new(
88+
c509_oid.oid().clone(),
89+
Some(param),
90+
))
8691
// ~oid
8792
} else {
8893
let oid = C509oid::decode(d, ctx)?;
89-
Ok(AlgorithmIdentifier::new(oid.get_oid(), None))
94+
Ok(AlgorithmIdentifier::new(oid.oid().clone(), None))
9095
}
9196
}
9297
}

rust/c509-certificate/src/attributes/attribute.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,20 @@ impl Attribute {
3939
}
4040
}
4141

42-
/// Add a value to `Attribute`.
43-
pub fn add_value(&mut self, value: AttributeValue) {
44-
self.value.push(value);
42+
/// Get the value of `Attribute`.
43+
#[must_use]
44+
pub fn value(&self) -> &[AttributeValue] {
45+
&self.value
4546
}
4647

4748
/// Get the registered OID of `Attribute`.
48-
pub(crate) fn get_registered_oid(&self) -> &C509oidRegistered {
49+
pub(crate) fn registered_oid(&self) -> &C509oidRegistered {
4950
&self.registered_oid
5051
}
5152

52-
/// Get the value of `Attribute`.
53-
pub(crate) fn get_value(&self) -> &Vec<AttributeValue> {
54-
&self.value
53+
/// Add a value to `Attribute`.
54+
pub fn add_value(&mut self, value: AttributeValue) {
55+
self.value.push(value);
5556
}
5657

5758
/// Set whether `Attribute` can have multiple value.
@@ -88,7 +89,7 @@ impl Serialize for Attribute {
8889
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
8990
where S: serde::Serializer {
9091
let helper = Helper {
91-
oid: self.registered_oid.get_c509_oid().get_oid().to_string(),
92+
oid: self.registered_oid().c509_oid().oid().to_string(),
9293
value: self.value.clone(),
9394
};
9495
helper.serialize(serializer)
@@ -102,14 +103,14 @@ impl Encode<()> for Attribute {
102103
// Encode CBOR int if available
103104
if let Some(&oid) = self
104105
.registered_oid
105-
.get_table()
106+
.table()
106107
.get_map()
107-
.get_by_right(&self.registered_oid.get_c509_oid().get_oid())
108+
.get_by_right(self.registered_oid().c509_oid().oid())
108109
{
109110
e.i16(oid)?;
110111
} else {
111-
// Encode unwrapped CBOR OID or CBOR PEN
112-
self.registered_oid.get_c509_oid().encode(e, ctx)?;
112+
// Encode unwrapped CBOR OID
113+
self.registered_oid().c509_oid().encode(e, ctx)?;
113114
}
114115

115116
// Check if the attribute value is empty
@@ -141,7 +142,7 @@ impl Decode<'_, ()> for Attribute {
141142
} else {
142143
// Handle unwrapped CBOR OID or CBOR PEN
143144
let c509_oid: C509oid = d.decode()?;
144-
Attribute::new(c509_oid.get_oid())
145+
Attribute::new(c509_oid.oid().clone())
145146
};
146147

147148
// Handle attribute value

rust/c509-certificate/src/attributes/mod.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,6 @@ mod data;
2424
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
2525
pub struct Attributes(Vec<Attribute>);
2626

27-
impl Default for Attributes {
28-
fn default() -> Self {
29-
Self::new()
30-
}
31-
}
32-
3327
impl Attributes {
3428
/// Create a new instance of `Attributes` as empty vector.
3529
#[must_use]
@@ -45,11 +39,17 @@ impl Attributes {
4539

4640
/// Add an `Attribute` to the `Attributes`.
4741
/// and set `Attribute` value to support multiple value.
48-
pub fn add_attr(&mut self, attribute: Attribute) {
42+
pub fn add_attribute(&mut self, attribute: Attribute) {
4943
self.0.push(attribute.set_multi_value());
5044
}
5145
}
5246

47+
impl Default for Attributes {
48+
fn default() -> Self {
49+
Self::new()
50+
}
51+
}
52+
5353
impl Encode<()> for Attributes {
5454
fn encode<W: Write>(
5555
&self, e: &mut Encoder<W>, ctx: &mut (),
@@ -82,7 +82,7 @@ impl Decode<'_, ()> for Attributes {
8282
// The attribute type is included in an array, so divide by 2
8383
for _ in 0..len / 2 {
8484
let attribute = Attribute::decode(d, &mut ())?;
85-
attributes.add_attr(attribute);
85+
attributes.add_attribute(attribute);
8686
}
8787

8888
Ok(attributes)
@@ -106,7 +106,7 @@ mod test_attributes {
106106
attr.add_value(AttributeValue::Text("[email protected]".to_string()));
107107
attr.add_value(AttributeValue::Text("[email protected]".to_string()));
108108
let mut attributes = Attributes::new();
109-
attributes.add_attr(attr);
109+
attributes.add_attribute(attr);
110110
attributes
111111
.encode(&mut encoder, &mut ())
112112
.expect("Failed to encode Attributes");

rust/c509-certificate/src/big_uint.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ impl UnwrappedBigUint {
2121
}
2222
}
2323

24+
impl From<u64> for UnwrappedBigUint {
25+
fn from(value: u64) -> Self {
26+
UnwrappedBigUint::new(value)
27+
}
28+
}
29+
30+
impl From<UnwrappedBigUint> for u64 {
31+
fn from(unwrapped_big_uint: UnwrappedBigUint) -> Self {
32+
unwrapped_big_uint.0
33+
}
34+
}
35+
2436
impl Encode<()> for UnwrappedBigUint {
2537
fn encode<W: Write>(
2638
&self, e: &mut Encoder<W>, _ctx: &mut (),

rust/c509-certificate/src/c509.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ impl C509 {
2626

2727
/// Get the `TBSCertificate` of the C509 Certificate.
2828
#[must_use]
29-
pub fn get_tbs_cert(&self) -> &TbsCert {
29+
pub fn tbs_cert(&self) -> &TbsCert {
3030
&self.tbs_cert
3131
}
3232

3333
/// Get the `IssuerSignatureValue` of the C509 Certificate.
3434
#[must_use]
35-
pub fn get_issuer_signature_value(&self) -> &Option<Vec<u8>> {
35+
pub fn issuer_signature_value(&self) -> &Option<Vec<u8>> {
3636
&self.issuer_signature_value
3737
}
3838
}

rust/c509-certificate/src/extensions/alt_name.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ impl AlternativeName {
2121
Self(value)
2222
}
2323

24-
/// Get the inner of Alternative Name.
24+
/// Get the general name which can be general names or text.
2525
#[must_use]
26-
pub fn get_inner(&self) -> &GeneralNamesOrText {
26+
pub fn general_name(&self) -> &GeneralNamesOrText {
2727
&self.0
2828
}
2929
}
@@ -61,12 +61,12 @@ impl Encode<()> for GeneralNamesOrText {
6161
match self {
6262
GeneralNamesOrText::GeneralNames(gns) => {
6363
let gn = gns
64-
.get_inner()
64+
.general_names()
6565
.first()
6666
.ok_or(minicbor::encode::Error::message("GeneralNames is empty"))?;
6767
// Check whether there is only 1 item in the array which is a DNSName
68-
if gns.get_inner().len() == 1 && gn.get_gn_type().is_dns_name() {
69-
gn.get_gn_value().encode(e, ctx)?;
68+
if gns.general_names().len() == 1 && gn.gn_type().is_dns_name() {
69+
gn.gn_value().encode(e, ctx)?;
7070
} else {
7171
gns.encode(e, ctx)?;
7272
}
@@ -89,7 +89,7 @@ impl Decode<'_, ()> for GeneralNamesOrText {
8989
GeneralNameValue::Text(d.str()?.to_string()),
9090
);
9191
let mut gns = GeneralNames::new();
92-
gns.add_gn(gn_dns);
92+
gns.add_general_name(gn_dns);
9393
Ok(GeneralNamesOrText::GeneralNames(gns))
9494
},
9595
minicbor::data::Type::Array => {
@@ -120,7 +120,7 @@ mod test_alt_name {
120120
let mut buffer = Vec::new();
121121
let mut encoder = Encoder::new(&mut buffer);
122122
let mut gns = GeneralNames::new();
123-
gns.add_gn(GeneralName::new(
123+
gns.add_general_name(GeneralName::new(
124124
GeneralNameTypeRegistry::DNSName,
125125
GeneralNameValue::Text("example.com".to_string()),
126126
));
@@ -151,7 +151,7 @@ mod test_alt_name {
151151

152152
// If only text, it should be GeneralNames with only 1 DNSName
153153
let mut gns = GeneralNames::new();
154-
gns.add_gn(GeneralName::new(
154+
gns.add_general_name(GeneralName::new(
155155
GeneralNameTypeRegistry::DNSName,
156156
GeneralNameValue::Text("example.com".to_string()),
157157
));

rust/c509-certificate/src/extensions/extension/mod.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,19 @@ impl Extension {
3737

3838
/// Get the value of the `Extension` in `ExtensionValue`.
3939
#[must_use]
40-
pub fn get_value(&self) -> &ExtensionValue {
40+
pub fn value(&self) -> &ExtensionValue {
4141
&self.value
4242
}
4343

4444
/// Get the critical flag of the `Extension`.
4545
#[must_use]
46-
pub fn get_critical(&self) -> bool {
46+
pub fn critical(&self) -> bool {
4747
self.critical
4848
}
4949

5050
/// Get the registered OID of the `Extension`.
5151
#[must_use]
52-
pub fn get_registered_oid(&self) -> &C509oidRegistered {
52+
pub(crate) fn registered_oid(&self) -> &C509oidRegistered {
5353
&self.registered_oid
5454
}
5555
}
@@ -80,7 +80,7 @@ impl Serialize for Extension {
8080
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
8181
where S: serde::Serializer {
8282
let helper = Helper {
83-
oid: self.registered_oid.get_c509_oid().get_oid().to_string(),
83+
oid: self.registered_oid.c509_oid().oid().to_string(),
8484
value: self.value.clone(),
8585
critical: self.critical,
8686
};
@@ -92,16 +92,15 @@ impl Encode<()> for Extension {
9292
// Extension can be encoded as:
9393
// - (extensionID: int, extensionValue: any)
9494
// - (extensionID: ~oid, ? critical: true, extensionValue: bytes)
95-
// - (extensionID: pen, ? critical: true, extensionValue: bytes)
9695
fn encode<W: Write>(
9796
&self, e: &mut Encoder<W>, ctx: &mut (),
9897
) -> Result<(), minicbor::encode::Error<W::Error>> {
9998
// Handle CBOR int based on OID mapping
10099
if let Some(&mapped_oid) = self
101100
.registered_oid
102-
.get_table()
101+
.table()
103102
.get_map()
104-
.get_by_right(&self.registered_oid.get_c509_oid().get_oid())
103+
.get_by_right(self.registered_oid.c509_oid().oid())
105104
{
106105
// Determine encoded OID value based on critical flag
107106
let encoded_oid = if self.critical {
@@ -111,8 +110,8 @@ impl Encode<()> for Extension {
111110
};
112111
e.i16(encoded_oid)?;
113112
} else {
114-
// Handle unwrapped CBOR OID or CBOR PEN
115-
self.registered_oid.get_c509_oid().encode(e, ctx)?;
113+
// Handle unwrapped CBOR OID
114+
self.registered_oid.c509_oid().encode(e, ctx)?;
116115
if self.critical {
117116
e.bool(self.critical)?;
118117
}
@@ -150,7 +149,7 @@ impl Decode<'_, ()> for Extension {
150149
))
151150
},
152151
_ => {
153-
// Handle unwrapped CBOR OID or CBOR PEN
152+
// Handle unwrapped CBOR OID
154153
let c509_oid = C509oid::decode(d, ctx)?;
155154
// Critical flag is optional, so if exist, this mean we have to decode it
156155
let critical = if d.datatype()? == minicbor::data::Type::Bool {
@@ -163,7 +162,7 @@ impl Decode<'_, ()> for Extension {
163162
let extension_value = ExtensionValue::Bytes(d.bytes()?.to_vec());
164163

165164
Ok(Extension::new(
166-
c509_oid.get_oid(),
165+
c509_oid.oid().clone(),
167166
extension_value,
168167
critical,
169168
))

0 commit comments

Comments
 (0)