Skip to content

Commit

Permalink
fix: comment and syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
bkioshn committed Jul 11, 2024
1 parent 144e90d commit e8ec464
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 50 deletions.
43 changes: 21 additions & 22 deletions catalyst-gateway-crates/c509-certificate/src/c509_algo_iden.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
//! C509 Algorithm Identifier
//! This module handle the AlgorithmIdentifier type where OID does not fall into the table.
//! This module handle the `AlgorithmIdentifier` type where OID does not fall into the
//! table.
//!
//! ```cddl
//! AlgorithmIdentifier = int / ~oid / [ algorithm: ~oid, parameters: bytes ]
//! ```
//!
//! For more information about AlgorithmIdentifier,
//! For more information about `AlgorithmIdentifier`,
//! visit [C509 Certificate](https://datatracker.ietf.org/doc/draft-ietf-cose-cbor-encoded-cert/09/)
use asn1_rs::Oid;
Expand All @@ -16,14 +17,15 @@ use crate::c509_oid::C509oid;
/// A struct represents the `AlgorithmIdentifier` type.
#[derive(Debug, Clone, PartialEq)]
pub struct AlgorithmIdentifier {
// A `C509oid`
/// A `C509oid`
oid: C509oid,
// An optional parameter string
/// An optional parameter string
param: Option<String>,
}

impl AlgorithmIdentifier {
/// Create new instance of `AlgorithmIdentifier`.
#[must_use]
pub fn new(oid: Oid<'static>, param: Option<String>) -> Self {
Self {
oid: C509oid::new(oid),
Expand Down Expand Up @@ -64,25 +66,22 @@ impl Encode<()> for AlgorithmIdentifier {

impl Decode<'_, ()> for AlgorithmIdentifier {
fn decode(d: &mut Decoder<'_>, ctx: &mut ()) -> Result<Self, minicbor::decode::Error> {
match d.datatype()? {
// [ algorithm: ~oid, parameters: bytes ]
minicbor::data::Type::Array => {
let len = d.array()?.ok_or(minicbor::decode::Error::message(
"Failed to get array length",
))?;
if len != 2 {
return Err(minicbor::decode::Error::message("Array length must be 2"));
}
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)))
},
// [ algorithm: ~oid, parameters: bytes ]
if d.datatype()? == minicbor::data::Type::Array {
let len = d.array()?.ok_or(minicbor::decode::Error::message(
"Failed to get array length",
))?;
if len != 2 {
return Err(minicbor::decode::Error::message("Array length must be 2"));
}
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)))
// ~oid
_ => {
let oid = C509oid::decode(d, ctx)?;
Ok(AlgorithmIdentifier::new(oid.get_oid(), None))
},
} else {
let oid = C509oid::decode(d, ctx)?;
Ok(AlgorithmIdentifier::new(oid.get_oid(), None))
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Signature algorithm data provides a necessary information for encoding and decoding of C509
//! `issuerSignatureAlgorithm`. See [C509 Certificate](https://datatracker.ietf.org/doc/draft-ietf-cose-cbor-encoded-cert/09/)
//! Signature algorithm data provides a necessary information for encoding and decoding of
//! C509 `issuerSignatureAlgorithm`. See [C509 Certificate](https://datatracker.ietf.org/doc/draft-ietf-cose-cbor-encoded-cert/09/)
//! Section 9.10 C509 Signature Algorithms Registry for more information.
use anyhow::Error;
Expand All @@ -8,10 +8,14 @@ use once_cell::sync::Lazy;

use crate::tables::IntegerToOidTable;

/// Type of algorithm data
/// INT | OID | Name
type AlgorithmDataTuple = (i16, Oid<'static>, &'static str);

/// Signature algorithm data table.
#[rustfmt::skip]
const SIG_ALGO_DATA: [AlgorithmDataTuple; 24] = [
// Int | OID | Name
(-256, oid!(1.2.840.113549.1.1.5), "RSASSA-PKCS1-v1_5 with SHA-1"),
(-255, oid!(1.2.840.10045.4.1), "ECDSA with SHA-1"),
(0, oid!(1.2.840.10045.4.3.2), "ECDSA with SHA-256"),
Expand All @@ -38,17 +42,20 @@ const SIG_ALGO_DATA: [AlgorithmDataTuple; 24] = [
(45, oid!(1.2.156.10197.1.501), "SM2 with SM3"),
];

/// A struct of data that contains lookup tables for `IssuerSignatureAlgorithm`.
pub(crate) struct IssuerSigAlgoData {
/// A table of integer to OID, provide a bidirectional lookup.
int_to_oid_table: IntegerToOidTable,
}

impl IssuerSigAlgoData {
/// Get the `IntegerToOidTable`
pub(crate) fn get_int_to_oid_table(&self) -> &IntegerToOidTable {
&self.int_to_oid_table
}
}

/// Define static lookup for extensions table
/// Define static lookup for issuer signature algorithm table
static ISSUER_SIG_ALGO_TABLE: Lazy<IssuerSigAlgoData> = Lazy::new(|| {
let mut int_to_oid_table = IntegerToOidTable::new();

Expand All @@ -59,6 +66,7 @@ static ISSUER_SIG_ALGO_TABLE: Lazy<IssuerSigAlgoData> = Lazy::new(|| {
IssuerSigAlgoData { int_to_oid_table }
});

/// Static reference to the `IssuerSigAlgoData` lookup table.
pub(crate) static ISSUER_SIG_ALGO_LOOKUP: &Lazy<IssuerSigAlgoData> = &ISSUER_SIG_ALGO_TABLE;

/// Get the OID from the int value.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! C509 Issuer Signature Algorithm as a part of `TBSCertificate` used in C509 Certificate.
//! C509 Issuer Signature Algorithm as a part of `TBSCertificate` used in C509
//! Certificate.
//!
//! ```cddl
//! issuerSignatureAlgorithm: AlgorithmIdentifier
Expand All @@ -14,7 +15,7 @@ use crate::{c509_algo_iden::AlgorithmIdentifier, c509_oid::C509oidRegistered};
/// A struct represents the `IssuerSignatureAlgorithm`
#[derive(Debug, Clone, PartialEq)]
pub struct IssuerSignatureAlgorithm {
/// The registered OID of the `Extension`.
/// The registered OID of the `IssuerSignatureAlgorithm`.
registered_oid: C509oidRegistered,
/// An `AlgorithmIdentifier` type
algo_iden: AlgorithmIdentifier,
Expand Down Expand Up @@ -55,6 +56,7 @@ impl Encode<()> for IssuerSignatureAlgorithm {
impl Decode<'_, ()> for IssuerSignatureAlgorithm {
fn decode(d: &mut Decoder<'_>, ctx: &mut ()) -> Result<Self, minicbor::decode::Error> {
match d.datatype()? {
// Check i16 for -256 and -256
minicbor::data::Type::U8 | minicbor::data::Type::I16 => {
let i = d.i16()?;
let oid = get_oid_from_int(i).map_err(minicbor::decode::Error::message)?;
Expand Down Expand Up @@ -106,7 +108,7 @@ mod test_issuer_signature_algorithm {
isa.encode(&mut encoder, &mut ())
.expect("Failed to encode IssuerSignatureAlgorithm");

// 2.16.840 .1 .101 .3 .4 .2 .1 - int 12: 0x49608648016503040201
// 2.16.840 .1 .101 .3 .4 .2 .1: 0x49608648016503040201
assert_eq!(hex::encode(buffer.clone()), "49608648016503040201");

let mut decoder = Decoder::new(&buffer);
Expand All @@ -127,9 +129,12 @@ mod test_issuer_signature_algorithm {
isa.encode(&mut encoder, &mut ())
.expect("Failed to encode IssuerSignatureAlgorithm");
// Array of 2 items: 0x82
// 2.16.840 .1 .101 .3 .4 .2 .1 - int 12: 0x49608648016503040201
// 2.16.840 .1 .101 .3 .4 .2 .1: 0x49608648016503040201
// bytes "example": 0x476578616d706c65
assert_eq!(hex::encode(buffer.clone()), "8249608648016503040201476578616d706c65");
assert_eq!(
hex::encode(buffer.clone()),
"8249608648016503040201476578616d706c65"
);

let mut decoder = Decoder::new(&buffer);
let decoded_isa = IssuerSignatureAlgorithm::decode(&mut decoder, &mut ())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Public key algorithm data provides a necessary information for encoding and decoding of C509
//! `subjectPublicKeyAlgorithm`. See [C509 Certificate](https://datatracker.ietf.org/doc/draft-ietf-cose-cbor-encoded-cert/09/)
//! Public key algorithm data provides a necessary information for encoding and decoding
//! of C509 `subjectPublicKeyAlgorithm`. See [C509 Certificate](https://datatracker.ietf.org/doc/draft-ietf-cose-cbor-encoded-cert/09/)
//! Section 9.11 C509 Public Key Algorithms Registry for more information.
use anyhow::Error;
Expand All @@ -8,10 +8,14 @@ use once_cell::sync::Lazy;

use crate::tables::IntegerToOidTable;

/// Type of algorithm data
/// INT | OID | Name
type AlgorithmDataTuple = (i16, Oid<'static>, &'static str);

/// Public key algorithm data table.
#[rustfmt::skip]
const PUB_KEY_ALGO_DATA: [AlgorithmDataTuple; 16] = [
// Int | OID | Name
(0, oid!(1.2.840.113549.1.1.1), "RSA"),
(1, oid!(1.2.840.10045.2.1), "EC Public Key (Weierstraß) with secp256r1"),
(2, oid!(1.2.840.10045.2.1), "EC Public Key (Weierstraß) with secp384r1"),
Expand All @@ -30,17 +34,20 @@ const PUB_KEY_ALGO_DATA: [AlgorithmDataTuple; 16] = [
(28, oid!(1.2.840.10045.2.1), "EC Public Key (Weierstraß) with sm2p256v1"),
];

/// A struct of data that contains lookup tables for `SubjectPublickeyAlgorithm`.
pub(crate) struct SubjectPubKeyAlgoData {
/// A table of integer to OID, provide a bidirectional lookup.
int_to_oid_table: IntegerToOidTable,
}

impl SubjectPubKeyAlgoData {
/// Get the `IntegerToOidTable`
pub(crate) fn get_int_to_oid_table(&self) -> &IntegerToOidTable {
&self.int_to_oid_table
}
}

/// Define static lookup for extensions table
/// Define static lookup for subject publickey table
static SUBJECT_PUB_KEY_ALGO_TABLE: Lazy<SubjectPubKeyAlgoData> = Lazy::new(|| {
let mut int_to_oid_table = IntegerToOidTable::new();

Expand All @@ -51,6 +58,7 @@ static SUBJECT_PUB_KEY_ALGO_TABLE: Lazy<SubjectPubKeyAlgoData> = Lazy::new(|| {
SubjectPubKeyAlgoData { int_to_oid_table }
});

/// Static reference to the `SubjectPubKeyAlgoData` lookup table.
pub(crate) static SUBJECT_PUB_KEY_ALGO_LOOKUP: &Lazy<SubjectPubKeyAlgoData> =
&SUBJECT_PUB_KEY_ALGO_TABLE;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! C509 Issuer Signature Algorithm as a part of `TBSCertificate` used in C509 Certificate.
//! C509 Issuer Signature Algorithm as a part of `TBSCertificate` used in C509
//! Certificate.
//!
//! ```cddl
//! subjectPublicKeyAlgorithm: AlgorithmIdentifier
Expand Down Expand Up @@ -54,19 +55,17 @@ impl Encode<()> for SubjectPubKeyAlgorithm {

impl Decode<'_, ()> for SubjectPubKeyAlgorithm {
fn decode(d: &mut Decoder<'_>, ctx: &mut ()) -> Result<Self, minicbor::decode::Error> {
match d.datatype()? {
minicbor::data::Type::U8 | minicbor::data::Type::I16 => {
let i = d.i16()?;
let oid = get_oid_from_int(i).map_err(minicbor::decode::Error::message)?;
Ok(Self::new(oid, None))
},
_ => {
let algo_iden = AlgorithmIdentifier::decode(d, ctx)?;
Ok(SubjectPubKeyAlgorithm::new(
algo_iden.get_oid(),
algo_iden.get_param(),
))
},
// Check u8 for 0 - 28
if d.datatype()? == minicbor::data::Type::U8 {
let i = d.i16()?;
let oid = get_oid_from_int(i).map_err(minicbor::decode::Error::message)?;
Ok(Self::new(oid, None))
} else {
let algo_iden = AlgorithmIdentifier::decode(d, ctx)?;
Ok(SubjectPubKeyAlgorithm::new(
algo_iden.get_oid(),
algo_iden.get_param(),
))
}
}
}
Expand Down Expand Up @@ -106,7 +105,7 @@ mod test_subject_public_key_algorithm {
spka.encode(&mut encoder, &mut ())
.expect("Failed to encode SubjectPubKeyAlgorithm");

// 2.16.840 .1 .101 .3 .4 .2 .1 - int 12: 0x49608648016503040201
// 2.16.840 .1 .101 .3 .4 .2 .1: 0x49608648016503040201
assert_eq!(hex::encode(buffer.clone()), "49608648016503040201");

let mut decoder = Decoder::new(&buffer);
Expand All @@ -127,7 +126,7 @@ mod test_subject_public_key_algorithm {
spka.encode(&mut encoder, &mut ())
.expect("Failed to encode SubjectPubKeyAlgorithm");
// Array of 2 items: 0x82
// 2.16.840 .1 .101 .3 .4 .2 .1 - int 12: 0x49608648016503040201
// 2.16.840 .1 .101 .3 .4 .2 .1: 0x49608648016503040201
// bytes "example": 0x476578616d706c65
assert_eq!(
hex::encode(buffer.clone()),
Expand Down
2 changes: 1 addition & 1 deletion catalyst-gateway-crates/c509-certificate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use wasm_bindgen::prelude::wasm_bindgen;
pub mod c509_algo_iden;
pub mod c509_extensions;
pub mod c509_general_names;
pub mod c509_oid;
pub mod c509_issuer_sig_algo;
pub mod c509_oid;
pub mod c509_subject_pub_key_algo;
mod tables;

Expand Down

0 comments on commit e8ec464

Please sign in to comment.