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
4 changes: 2 additions & 2 deletions rust/.cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
linker = "clang"
rustflags = [
"-C", "link-arg=-fuse-ld=/usr/bin/mold",
"-C", "target-feature=-crt-static"
"-C", "target-feature=+crt-static"
]

# Should be the default to have fully static rust programs in CI
[target.aarch64-unknown-linux-musl]
linker = "clang"
rustflags = [
"-C", "link-arg=-fuse-ld=/usr/bin/mold",
"-C", "target-feature=-crt-static"
"-C", "target-feature=+crt-static"
]

[build]
Expand Down
11 changes: 11 additions & 0 deletions rust/.config/nextest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,14 @@ store-success-output = true
# Note that if a description can be extracted from the output, it is always stored in the
# <description> element.
store-failure-output = true

[test-groups]
serial-integration = { max-threads = 1 }

[[profile.default.overrides]]
filter = 'test(/^serial::/)'
test-group = 'serial-integration'

[[profile.ci.overrides]]
filter = 'test(/^serial::/)'
test-group = "serial-integration"
2 changes: 1 addition & 1 deletion rust/Earthfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
VERSION 0.8

IMPORT github.com/input-output-hk/catalyst-ci/earthly/rust:v3.4.9 AS rust-ci
IMPORT github.com/input-output-hk/catalyst-ci/earthly/rust:v3.5.17 AS rust-ci
IMPORT ../ AS repo-ci

COPY_SRC:
Expand Down
33 changes: 25 additions & 8 deletions rust/c509-certificate/examples/cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ const SELF_SIGNED_INT: u8 = 2;

/// A function to generate C509 certificate.
fn generate(
file: &PathBuf, output: Option<PathBuf>, private_key: Option<&PrivateKey>,
file: &PathBuf,
output: Option<PathBuf>,
private_key: Option<&PrivateKey>,
key_type: Option<String>,
) -> anyhow::Result<()> {
let data = fs::read_to_string(file)?;
Expand Down Expand Up @@ -209,7 +211,10 @@ fn generate(
}

/// Write a data to a file given an output path.
fn write_to_output_file(output: PathBuf, data: &[u8]) -> anyhow::Result<()> {
fn write_to_output_file(
output: PathBuf,
data: &[u8],
) -> anyhow::Result<()> {
let mut file = File::create(output).map_err(|e| anyhow::anyhow!(e))?;
file.write_all(data).map_err(|e| anyhow::anyhow!(e))?;
Ok(())
Expand All @@ -219,7 +224,9 @@ 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<Vec<Attribute>>, subject: Vec<Attribute>,
self_signed: bool,
issuer: Option<Vec<Attribute>>,
subject: Vec<Attribute>,
) -> anyhow::Result<Vec<Attribute>> {
if self_signed {
Ok(subject)
Expand All @@ -230,7 +237,8 @@ fn determine_issuer(

/// Validate the certificate type.
fn validate_certificate_type(
self_signed: bool, certificate_type: Option<u8>,
self_signed: bool,
certificate_type: Option<u8>,
) -> anyhow::Result<()> {
if self_signed && certificate_type.unwrap_or(SELF_SIGNED_INT) != SELF_SIGNED_INT {
return Err(anyhow::anyhow!(
Expand Down Expand Up @@ -260,7 +268,10 @@ fn get_key_type(key_type: Option<String>) -> anyhow::Result<(Oid<'static>, Optio
}

/// Parse date string to u64.
fn parse_or_default_date(date_option: Option<String>, default: u64) -> Result<u64, anyhow::Error> {
fn parse_or_default_date(
date_option: Option<String>,
default: u64,
) -> Result<u64, anyhow::Error> {
match date_option {
Some(date) => {
DateTime::parse_from_rfc3339(&date)
Expand All @@ -284,7 +295,10 @@ fn parse_serial_number(serial_number: Option<UnwrappedBigUint>) -> UnwrappedBigU
// -------------------verify-----------------------

/// Verify the signature of the certificate given public key file path.
fn verify(file: &PathBuf, public_key: PathBuf) -> anyhow::Result<()> {
fn verify(
file: &PathBuf,
public_key: PathBuf,
) -> anyhow::Result<()> {
let cert = fs::read(file)?;
let pk = PublicKey::from_file(public_key)?;
match c509_certificate::verify(&cert, &pk) {
Expand All @@ -297,7 +311,10 @@ fn verify(file: &PathBuf, public_key: PathBuf) -> anyhow::Result<()> {
// -------------------decode-----------------------

/// Decode the certificate to JSON.
fn decode(file: &PathBuf, output: Option<PathBuf>) -> anyhow::Result<()> {
fn decode(
file: &PathBuf,
output: Option<PathBuf>,
) -> anyhow::Result<()> {
let cert = fs::read(file)?;
let mut d = minicbor::Decoder::new(&cert);
let c509 = c509_certificate::c509::C509::decode(&mut d, &mut ())?;
Expand Down Expand Up @@ -347,7 +364,7 @@ fn time_to_string(time: u64) -> anyhow::Result<String> {
// Attempt to convert the timestamp and handle errors if they occur
let timestamp: i64 = time
.try_into()
.map_err(|e| anyhow::anyhow!("Failed to convert time: {:?}", e))?;
.map_err(|e| anyhow::anyhow!("Failed to convert time: {e:?}"))?;

// Convert the timestamp to a DateTime and handle any potential errors
let datetime = DateTime::from_timestamp(timestamp, 0)
Expand Down
14 changes: 11 additions & 3 deletions rust/c509-certificate/src/algorithm_identifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ pub struct AlgorithmIdentifier {
impl AlgorithmIdentifier {
/// Create new instance of `AlgorithmIdentifier`.
#[must_use]
pub fn new(oid: Oid<'static>, param: Option<String>) -> Self {
pub fn new(
oid: Oid<'static>,
param: Option<String>,
) -> Self {
Self {
c509_oid: C509oid::new(oid),
param,
Expand All @@ -58,7 +61,9 @@ impl AlgorithmIdentifier {

impl Encode<()> for AlgorithmIdentifier {
fn encode<W: Write>(
&self, e: &mut Encoder<W>, ctx: &mut (),
&self,
e: &mut Encoder<W>,
ctx: &mut (),
) -> Result<(), minicbor::encode::Error<W::Error>> {
match &self.param {
// [ algorithm: ~oid, parameters: bytes ]
Expand All @@ -77,7 +82,10 @@ impl Encode<()> for AlgorithmIdentifier {
}

impl Decode<'_, ()> for AlgorithmIdentifier {
fn decode(d: &mut Decoder<'_>, ctx: &mut ()) -> Result<Self, minicbor::decode::Error> {
fn decode(
d: &mut Decoder<'_>,
ctx: &mut (),
) -> Result<Self, minicbor::decode::Error> {
// [ algorithm: ~oid, parameters: bytes ]
if decode_datatype(d, "Algorithm Identifier")? == minicbor::data::Type::Array {
let len = decode_array_len(d, "Algorithm Identifier")?;
Expand Down
32 changes: 25 additions & 7 deletions rust/c509-certificate/src/attributes/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ impl Attribute {
}

/// Add a value to `Attribute`.
pub fn add_value(&mut self, value: AttributeValue) {
pub fn add_value(
&mut self,
value: AttributeValue,
) {
self.value.push(value);
}

Expand Down Expand Up @@ -98,8 +101,13 @@ impl<'de> Deserialize<'de> for Attribute {
}

impl Serialize for Attribute {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: serde::Serializer {
fn serialize<S>(
&self,
serializer: S,
) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let helper = Helper {
oid: self.registered_oid().c509_oid().oid().to_string(),
value: self.value.clone(),
Expand All @@ -110,7 +118,9 @@ impl Serialize for Attribute {

impl Encode<()> for Attribute {
fn encode<W: Write>(
&self, e: &mut Encoder<W>, ctx: &mut (),
&self,
e: &mut Encoder<W>,
ctx: &mut (),
) -> Result<(), minicbor::encode::Error<W::Error>> {
// Encode CBOR int if available
if let Some(&oid) = self
Expand Down Expand Up @@ -145,7 +155,10 @@ impl Encode<()> for Attribute {
}

impl Decode<'_, ()> for Attribute {
fn decode(d: &mut Decoder<'_>, ctx: &mut ()) -> Result<Self, minicbor::decode::Error> {
fn decode(
d: &mut Decoder<'_>,
ctx: &mut (),
) -> Result<Self, minicbor::decode::Error> {
// Handle CBOR int
let mut attr = if decode_datatype(d, "Attribute as OID int")? == minicbor::data::Type::U8 {
let i = decode_helper(d, "Attribute as OID int", ctx)?;
Expand Down Expand Up @@ -193,7 +206,9 @@ pub enum AttributeValue {

impl Encode<()> for AttributeValue {
fn encode<W: Write>(
&self, e: &mut Encoder<W>, ctx: &mut (),
&self,
e: &mut Encoder<W>,
ctx: &mut (),
) -> Result<(), minicbor::encode::Error<W::Error>> {
match self {
AttributeValue::Text(text) => encode_helper(e, "Attribute value", ctx, text)?,
Expand All @@ -204,7 +219,10 @@ impl Encode<()> for AttributeValue {
}

impl Decode<'_, ()> for AttributeValue {
fn decode(d: &mut Decoder<'_>, ctx: &mut ()) -> Result<Self, minicbor::decode::Error> {
fn decode(
d: &mut Decoder<'_>,
ctx: &mut (),
) -> Result<Self, minicbor::decode::Error> {
match decode_datatype(d, "Attribute value")? {
minicbor::data::Type::String => {
Ok(AttributeValue::Text(decode_helper(
Expand Down
14 changes: 11 additions & 3 deletions rust/c509-certificate/src/attributes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ impl Attributes {

/// Add an `Attribute` to the `Attributes`.
/// and set `Attribute` value to support multiple value.
pub fn add_attribute(&mut self, attribute: Attribute) {
pub fn add_attribute(
&mut self,
attribute: Attribute,
) {
self.0.push(attribute.set_multi_value());
}
}
Expand All @@ -54,7 +57,9 @@ impl Default for Attributes {

impl Encode<()> for Attributes {
fn encode<W: Write>(
&self, e: &mut Encoder<W>, ctx: &mut (),
&self,
e: &mut Encoder<W>,
ctx: &mut (),
) -> Result<(), minicbor::encode::Error<W::Error>> {
if self.0.is_empty() {
return Err(minicbor::encode::Error::message(
Expand All @@ -74,7 +79,10 @@ impl Encode<()> for Attributes {
}

impl Decode<'_, ()> for Attributes {
fn decode(d: &mut Decoder<'_>, ctx: &mut ()) -> Result<Self, minicbor::decode::Error> {
fn decode(
d: &mut Decoder<'_>,
ctx: &mut (),
) -> Result<Self, minicbor::decode::Error> {
let len = decode_array_len(d, "Attributes")?;
if len == 0 {
return Err(minicbor::decode::Error::message("Attributes is empty"));
Expand Down
9 changes: 7 additions & 2 deletions rust/c509-certificate/src/big_uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ impl From<UnwrappedBigUint> for u64 {

impl Encode<()> for UnwrappedBigUint {
fn encode<W: Write>(
&self, e: &mut Encoder<W>, _ctx: &mut (),
&self,
e: &mut Encoder<W>,
_ctx: &mut (),
) -> Result<(), minicbor::encode::Error<W::Error>> {
let bytes = self.0.to_be_bytes();
// Trim leading zeros
Expand All @@ -52,7 +54,10 @@ impl Encode<()> for UnwrappedBigUint {
}

impl Decode<'_, ()> for UnwrappedBigUint {
fn decode(d: &mut Decoder<'_>, _ctx: &mut ()) -> Result<Self, minicbor::decode::Error> {
fn decode(
d: &mut Decoder<'_>,
_ctx: &mut (),
) -> Result<Self, minicbor::decode::Error> {
// Turn bytes into u64
let b = decode_bytes(d, "Unwrapped big uint")?
.iter()
Expand Down
14 changes: 11 additions & 3 deletions rust/c509-certificate/src/c509.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ pub struct C509 {
impl C509 {
/// Create a new instance of C509 Certificate .
#[must_use]
pub fn new(tbs_cert: TbsCert, issuer_signature_value: Option<Vec<u8>>) -> Self {
pub fn new(
tbs_cert: TbsCert,
issuer_signature_value: Option<Vec<u8>>,
) -> Self {
Self {
tbs_cert,
issuer_signature_value,
Expand All @@ -45,7 +48,9 @@ impl C509 {

impl Encode<()> for C509 {
fn encode<W: Write>(
&self, e: &mut Encoder<W>, ctx: &mut (),
&self,
e: &mut Encoder<W>,
ctx: &mut (),
) -> Result<(), minicbor::encode::Error<W::Error>> {
self.tbs_cert.encode(e, ctx)?;
match self.issuer_signature_value {
Expand All @@ -57,7 +62,10 @@ impl Encode<()> for C509 {
}

impl Decode<'_, ()> for C509 {
fn decode(d: &mut Decoder<'_>, ctx: &mut ()) -> Result<Self, minicbor::decode::Error> {
fn decode(
d: &mut Decoder<'_>,
ctx: &mut (),
) -> Result<Self, minicbor::decode::Error> {
let tbs_cert = TbsCert::decode(d, ctx)?;
let issuer_signature_value = match decode_datatype(d, "C509 Issuer Signature value")? {
minicbor::data::Type::Bytes => Some(decode_bytes(d, "C509 Issuer Signature value")?),
Expand Down
22 changes: 16 additions & 6 deletions rust/c509-certificate/src/cert_tbs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,15 @@ impl TbsCert {
#[must_use]
#[allow(clippy::too_many_arguments)]
pub fn new(
c509_certificate_type: u8, certificate_serial_number: UnwrappedBigUint,
issuer_signature_algorithm: IssuerSignatureAlgorithm, issuer: Option<Name>,
validity_not_before: Time, validity_not_after: Time, subject: Name,
subject_public_key_algorithm: SubjectPubKeyAlgorithm, subject_public_key: Vec<u8>,
c509_certificate_type: u8,
certificate_serial_number: UnwrappedBigUint,
issuer_signature_algorithm: IssuerSignatureAlgorithm,
issuer: Option<Name>,
validity_not_before: Time,
validity_not_after: Time,
subject: Name,
subject_public_key_algorithm: SubjectPubKeyAlgorithm,
subject_public_key: Vec<u8>,
extensions: Extensions,
) -> Self {
Self {
Expand Down Expand Up @@ -149,7 +154,9 @@ impl TbsCert {

impl Encode<()> for TbsCert {
fn encode<W: Write>(
&self, e: &mut Encoder<W>, ctx: &mut (),
&self,
e: &mut Encoder<W>,
ctx: &mut (),
) -> Result<(), minicbor::encode::Error<W::Error>> {
encode_helper(e, "Certificate type", ctx, &self.c509_certificate_type)?;
self.certificate_serial_number.encode(e, ctx)?;
Expand All @@ -166,7 +173,10 @@ impl Encode<()> for TbsCert {
}

impl Decode<'_, ()> for TbsCert {
fn decode(d: &mut Decoder<'_>, ctx: &mut ()) -> Result<Self, minicbor::decode::Error> {
fn decode(
d: &mut Decoder<'_>,
ctx: &mut (),
) -> Result<Self, minicbor::decode::Error> {
let cert_type = decode_helper(d, "Certificate type", ctx)?;
let serial_number = UnwrappedBigUint::decode(d, ctx)?;
let issuer_signature_algorithm = IssuerSignatureAlgorithm::decode(d, ctx)?;
Expand Down
Loading
Loading