Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
1192b4d
fix: naming + add neccesary function
bkioshn Sep 5, 2024
3c33087
fix(rust/c509-certificate): add encode + decode helper functions
bkioshn Sep 10, 2024
8f31127
Merge branch 'feat/c509-v11' into fix/c509-refactor
bkioshn Sep 16, 2024
9ea697b
fix(rust/c509-certificate): cleanup
bkioshn Sep 16, 2024
0b87b18
fix(rust/c509-certificate): cleanup
bkioshn Sep 17, 2024
e37dfee
Merge branch 'fix/c509-rename-functions-additions' into fix/c509-deco…
bkioshn Sep 17, 2024
565cd6d
fix(rust/c509-certificate): cleanup
bkioshn Sep 17, 2024
4992fe8
Merge branch 'feat/c509-v11' into fix/c509-decode-encode-helper
bkioshn Sep 18, 2024
2b94ac4
fix(rust/c509-certificate): remove unnecessary allow(dead_code)
bkioshn Sep 19, 2024
e7be49f
fix(rust/c509-certificate): fix encode decode Name
bkioshn Sep 19, 2024
3ae639c
chore(rust/c509-certificate): fix comment
bkioshn Sep 19, 2024
1b5a83a
fix(rust/c509-certificate): add + rewrite test cases
bkioshn Sep 19, 2024
1bd1160
Merge branch 'feat/c509-v11' into test/c509-test-v11
bkioshn Sep 19, 2024
303becd
chore(rust/c509-certificate): earthly no cache
bkioshn Sep 19, 2024
3a292ea
chore(rust/c509-certificate): earthly no cache
bkioshn Sep 20, 2024
442af11
chore(rust/c509-certificate): earthly no cache
bkioshn Sep 20, 2024
f5b3196
chore(rust/c509-certificate): remove earthly no-cache
bkioshn Sep 20, 2024
74f2a38
fix(rust/c509-certificate): add more test comments
bkioshn Sep 20, 2024
4793b9b
Merge branch 'feat/c509-v11' into test/c509-test-v11
stevenj Sep 21, 2024
12485e7
Merge branch 'feat/c509-v11' into test/c509-test-v11
stevenj Sep 21, 2024
223e860
fix(rust/c509-certificate): clippy lints
stevenj Sep 21, 2024
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
18 changes: 9 additions & 9 deletions rust/c509-certificate/examples/cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{

use asn1_rs::{oid, Oid};
use c509_certificate::{
attributes::Attributes,
attributes::attribute::Attribute,
big_uint::UnwrappedBigUint,
extensions::Extensions,
issuer_sig_algo::IssuerSignatureAlgorithm,
Expand Down Expand Up @@ -108,15 +108,15 @@ struct C509Json {
issuer_signature_algorithm: Option<IssuerSignatureAlgorithm>,
/// Optional issuer of the certificate,
/// if not provided, issuer is the same as subject.
issuer: Option<Attributes>,
issuer: Option<Vec<Attribute>>,
/// Optional validity not before date,
/// if not provided, set to current time.
validity_not_before: Option<String>,
/// Optional validity not after date,
/// if not provided, set to no expire date 9999-12-31T23:59:59+00:00.
validity_not_after: Option<String>,
/// Attributes of the subject.
subject: Attributes,
subject: Vec<Attribute>,
/// Optional subject public key algorithm of the certificate,
/// if not provided, set to Ed25519.
subject_public_key_algorithm: Option<SubjectPubKeyAlgorithm>,
Expand Down Expand Up @@ -184,10 +184,10 @@ fn generate(
c509_json
.issuer_signature_algorithm
.unwrap_or(IssuerSignatureAlgorithm::new(key_type.0.clone(), ED25519.1)),
Some(Name::new(NameValue::Attributes(issuer))),
Some(Name::new(NameValue::Attribute(issuer))),
Time::new(not_before),
Time::new(not_after),
Name::new(NameValue::Attributes(c509_json.subject)),
Name::new(NameValue::Attribute(c509_json.subject)),
c509_json
.subject_public_key_algorithm
.unwrap_or(SubjectPubKeyAlgorithm::new(key_type.0, key_type.1)),
Expand Down Expand Up @@ -219,8 +219,8 @@ fn write_to_output_file(output: PathBuf, data: &[u8]) -> anyhow::Result<()> {
/// If self-signed is true, issuer is the same as subject.
/// Otherwise, issuer must be present.
fn determine_issuer(
self_signed: bool, issuer: Option<Attributes>, subject: Attributes,
) -> anyhow::Result<Attributes> {
self_signed: bool, issuer: Option<Vec<Attribute>>, subject: Vec<Attribute>,
) -> anyhow::Result<Vec<Attribute>> {
if self_signed {
Ok(subject)
} else {
Expand Down Expand Up @@ -330,9 +330,9 @@ fn decode(file: &PathBuf, output: Option<PathBuf>) -> anyhow::Result<()> {
}

/// Extract a `Attributes` from a `Name`.
fn extract_attributes(name: &Name) -> anyhow::Result<Attributes> {
fn extract_attributes(name: &Name) -> anyhow::Result<Vec<Attribute>> {
match name.value() {
NameValue::Attributes(attrs) => Ok(attrs.clone()),
NameValue::Attribute(attrs) => Ok(attrs.clone()),
_ => Err(anyhow::anyhow!("Expected Attributes")),
}
}
Expand Down
3 changes: 2 additions & 1 deletion rust/c509-certificate/src/attributes/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,8 @@ mod test_attribute {
attribute
.encode(&mut encoder, &mut ())
.expect("Failed to encode Attribute");
// Email Address example@example.com: 0x00736578616d706c65406578616d706c652e636f6d
// 1.2.840 .113549 .1 .9 .1 in attribute int = 0x00
// Email Address example@example.com: 0x736578616d706c65406578616d706c652e636f6d
assert_eq!(
hex::encode(buffer.clone()),
"00736578616d706c65406578616d706c652e636f6d"
Expand Down
2 changes: 1 addition & 1 deletion rust/c509-certificate/src/attributes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ mod test_attributes {
.encode(&mut encoder, &mut ())
.expect("Failed to encode Attributes");
// 1 Attribute (array len 2 (attribute type + value)): 0x82
// Email Address: 0x00
// Email Address attribute int: 0x00
// Attribute value (array len 2): 0x82
// example@example.com: 0x736578616d706c65406578616d706c652e636f6d
assert_eq!(
Expand Down
2 changes: 2 additions & 0 deletions rust/c509-certificate/src/big_uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ mod test_big_uint {
b_uint
.encode(&mut encoder, &mut ())
.expect("Failed to encode UnwrappedBigUint");
// 128269 (h'01F50D'): CBOR 0x4301f50d
assert_eq!(hex::encode(buffer.clone()), "4301f50d");

let mut decoder = minicbor::Decoder::new(&buffer);
Expand All @@ -97,6 +98,7 @@ mod test_big_uint {
b_uint
.encode(&mut encoder, &mut ())
.expect("Failed to encode UnwrappedBigUint");
// 9112578475118446130 (h'7E7661D7B54E4632'): CBOR 0x487e7661d7b54e4632
assert_eq!(hex::encode(buffer.clone()), "487e7661d7b54e4632");

let mut decoder = minicbor::Decoder::new(&buffer);
Expand Down
2 changes: 1 addition & 1 deletion rust/c509-certificate/src/extensions/extension/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ mod test_extension {
let mut buffer = Vec::new();
let mut encoder = Encoder::new(&mut buffer);

// Not PEN OID and not in the registry table
// Not in the registry table
// Value should be bytes
let ext = Extension::new(
oid!(2.16.840 .1 .101 .3 .4 .2 .1),
Expand Down
10 changes: 10 additions & 0 deletions rust/c509-certificate/src/general_names/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,16 @@ mod test_general_names {
gns.encode(&mut encoder, &mut ())
.expect("Failed to encode GeneralNames");
// Array of 4 GeneralName (type, value) so 8 items: 0x88
// Unsigned int 2 for DNSName: 0x02
// DNSName with "example.com": 0x6b6578616d706c652e636f6d
// OtherNameHardwareModuleName negative 1: 0x20
// Array of 2 items: 0x82
// OID 2.16.840 .1 .101 .3 .4 .2 .1: 0x49608648016503040201
// vec![0x01, 0x02, 0x03, 0x04]: 0x4401020304
// IPAddress: 0x07
// IPAddress Value in bytes string 192, 168, 1, 1: 0x44c0a80101
// RegisteredID: 0x08
// OID 2.16.840 .1 .101 .3 .4 .2 .1: 0x49608648016503040201
assert_eq!(hex::encode(buffer.clone()), "88026b6578616d706c652e636f6d20824960864801650304020144010203040744c0a801010849608648016503040201");

let mut decoder = Decoder::new(&buffer);
Expand Down
12 changes: 6 additions & 6 deletions rust/c509-certificate/src/helper/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub(crate) fn decode_helper<'a, T, C>(
) -> Result<T, decode::Error>
where T: minicbor::Decode<'a, C> {
T::decode(d, context).map_err(|e| {
decode::Error::message(&format!(
decode::Error::message(format!(
"Failed to decode {:?} in {from}: {e}",
std::any::type_name::<T>()
))
Expand All @@ -18,7 +18,7 @@ where T: minicbor::Decode<'a, C> {
/// Helper function for decoding bytes.
pub(crate) fn decode_bytes(d: &mut Decoder, from: &str) -> Result<Vec<u8>, decode::Error> {
d.bytes().map(<[u8]>::to_vec).map_err(|e| {
decode::Error::message(&format!(
decode::Error::message(format!(
"Failed to decode bytes in {from}:
{e}"
))
Expand All @@ -29,20 +29,20 @@ pub(crate) fn decode_bytes(d: &mut Decoder, from: &str) -> Result<Vec<u8>, decod
pub(crate) fn decode_array_len(d: &mut Decoder, from: &str) -> Result<u64, decode::Error> {
d.array()
.map_err(|e| {
decode::Error::message(&format!(
decode::Error::message(format!(
"Failed to decode array in {from}:
{e}"
))
})?
.ok_or(decode::Error::message(&format!(
.ok_or(decode::Error::message(format!(
"Failed to decode array in {from}, unexpected indefinite length",
)))
}

/// Helper function for decoding null.
pub(crate) fn decode_null(d: &mut Decoder, from: &str) -> Result<(), decode::Error> {
d.null().map_err(|e| {
decode::Error::message(&format!(
decode::Error::message(format!(
"Failed to decode null in {from}:
{e}"
))
Expand All @@ -54,7 +54,7 @@ pub(crate) fn decode_datatype(
d: &mut Decoder, from: &str,
) -> Result<minicbor::data::Type, decode::Error> {
d.datatype().map_err(|e| {
decode::Error::message(&format!(
decode::Error::message(format!(
"Failed to decode datatype in {from}:
{e}"
))
Expand Down
8 changes: 4 additions & 4 deletions rust/c509-certificate/src/helper/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ where T: minicbor::Encode<C> {
T::encode(value, e, ctx).map_err(|err| {
encode::Error::with_message(
err,
&format!(
format!(
"Failed to encode {:?} in {from}",
std::any::type_name::<T>()
),
Expand All @@ -28,7 +28,7 @@ pub(crate) fn encode_bytes<W: Write>(
e: &mut Encoder<W>, from: &str, value: &[u8],
) -> Result<(), encode::Error<W::Error>> {
e.bytes(value).map_err(|err| {
encode::Error::with_message(err, &format!("Failed to encode bytes in {from}"))
encode::Error::with_message(err, format!("Failed to encode bytes in {from}"))
})?;
Ok(())
}
Expand All @@ -38,7 +38,7 @@ pub(crate) fn encode_null<W: Write>(
e: &mut Encoder<W>, from: &str,
) -> Result<(), encode::Error<W::Error>> {
e.null().map_err(|err| {
encode::Error::with_message(err, &format!("Failed to encode null in {from}"))
encode::Error::with_message(err, format!("Failed to encode null in {from}"))
})?;
Ok(())
}
Expand All @@ -48,7 +48,7 @@ pub(crate) fn encode_array_len<W: Write>(
e: &mut Encoder<W>, from: &str, len: u64,
) -> Result<(), encode::Error<W::Error>> {
e.array(len).map_err(|err| {
encode::Error::with_message(err, &format!("Failed to encode array in {from}"))
encode::Error::with_message(err, format!("Failed to encode array in {from}"))
})?;
Ok(())
}
4 changes: 2 additions & 2 deletions rust/c509-certificate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,13 @@ mod test {
use std::str::FromStr;

use signing::tests::private_key_str;
use tbs_cert::test_tbs_cert::tbs;
use tbs_cert::test_tbs_cert::tbs_1;

use super::*;

#[test]
fn test_generate_and_verify_signed_c509_cert() {
let tbs_cert = tbs();
let (tbs_cert, _) = tbs_1();

let private_key = FromStr::from_str(&private_key_str()).expect(
"Cannot create
Expand Down
Loading