Skip to content

Commit

Permalink
Vical builder WIP: certificate_info builder and related changes
Browse files Browse the repository at this point in the history
  • Loading branch information
anton-pronin committed Dec 17, 2024
1 parent 4273e75 commit 88129ff
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::BTreeMap;
use std::str::FromStr;
use crate::{
definitions::{
helpers::ByteStr,
Expand Down Expand Up @@ -44,17 +46,108 @@ impl From<CertificateInfos> for ciborium::Value {
#[isomdl(crate = "crate")]
pub struct CertificateInfo {
pub certificate: ByteStr,
//TODO: check if 20 byte serial number best fits to ByteStr, or the size is correct at all
pub serial_number: ByteStr,
pub ski: ByteStr,
pub doc_type: DocTypes,
pub certificate_profile: Option<CertificateProfiles>,
pub issuing_authority: Option<Latin1>,
//TODO: check for actual country code format: ISO3166-1 or ISO3166-2 depending on the issuing
pub issuing_country: Option<Alpha2>,
pub state_or_province_name: Option<Latin1>,
pub issuer: Option<ByteStr>,
pub subject: Option<ByteStr>,
pub not_before: Option<TDate>,
pub extensions: Option<Extensions>
}

pub struct CertificateInfoBuilder {
pub certificate: Vec<u8>,
pub serial_number: Vec<u8>,
pub ski: Vec<u8>,
pub doc_type: Vec<String>,
pub certificate_profile: Option<Vec<String>>,
pub issuing_authority: Option<String>,
pub issuing_country: Option<String>,
pub state_or_province_name: Option<String>,
pub issuer: Option<Vec<u8>>,
pub subject: Option<Vec<u8>>,
pub not_before: Option<String>,
pub extensions: Option<BTreeMap<String, Vec<u8>>>,
}
impl CertificateInfoBuilder {
pub fn new(certificate: Vec<u8>, serial_number: Vec<u8>, ski: Vec<u8>, doc_type: Vec<String>) -> Self {
CertificateInfoBuilder {
certificate,
serial_number,
ski,
doc_type,
certificate_profile: None,
issuing_authority: None,
issuing_country: None,
state_or_province_name: None,
issuer: None,
subject: None,
not_before: None,
extensions: None,
}
}
pub fn certificate_profile(mut self, profiles: Vec<String>) -> Self {
self.certificate_profile = Some(profiles);
self
}
pub fn issuing_authority(mut self, authority: String) -> Self {
self.issuing_authority = Some(authority);
self
}
pub fn issuing_country(mut self, country: String) -> Self {
self.issuing_country = Some(country);
self
}
pub fn state_or_province_name(mut self, state_or_province_name: String) -> Self {
self.state_or_province_name = Some(state_or_province_name);
self
}
pub fn issuer(mut self, issuer: Vec<u8>) -> Self {
self.issuer = Some(issuer);
self
}
pub fn subject(mut self, subject: Vec<u8>) -> Self {
self.subject = Some(subject);
self
}
pub fn not_before(mut self, not_before: String) -> Self {
self.not_before = Some(not_before);
self
}
pub fn extensions(mut self, extensions: BTreeMap<String, Vec<u8>>) -> Self {
self.extensions = Some(extensions);
self
}
pub fn build(self) -> CertificateInfo {
CertificateInfo {
certificate: ByteStr::from(self.certificate),
serial_number: ByteStr::from(self.serial_number),
ski: ByteStr::from(self.ski),
doc_type: DocTypes::new(self.doc_type),
certificate_profile: match self.certificate_profile {
Some(p) => Some(CertificateProfiles::new(p)),
None => None,
},
issuing_authority: match self.issuing_authority {
Some(s) => Some(Latin1::from_str(s.as_str()).unwrap()),
None => None
},
issuing_country: self.issuing_country.map(|c| Alpha2::from_str(c.as_str()).unwrap()),
state_or_province_name: self.state_or_province_name.map(|s| Latin1::from_str(s.as_str()).unwrap()),
issuer: self.issuer.map(ByteStr::from),
subject: self.subject.map(ByteStr::from),
not_before: match self.not_before {
Some(s) => Some(TDate::from_str(s.as_str()).unwrap()),
None => None,
},
extensions: match self.extensions {
Some(s) => Some(Extensions::new(s)),
None => None,
},
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::str::FromStr;
use isomdl_macros::FromJson;
use crate::definitions::helpers::NonEmptyVec;
use crate::definitions::namespaces::latin1::Latin1;
Expand All @@ -7,6 +8,12 @@ use crate::definitions::traits::ToCbor;
#[isomdl(crate = "crate")]
pub struct CertificateProfiles(NonEmptyVec<Latin1>);

impl CertificateProfiles {
pub fn new(profiles: Vec<String>) -> Self {
let v: Vec<Latin1> = profiles.iter().map(|s| Latin1::from_str(s.as_str()).unwrap()).collect();
Self(NonEmptyVec::try_from(v).unwrap())
}
}
impl From<CertificateProfiles> for ciborium::Value {
fn from(value: CertificateProfiles) -> ciborium::Value {
ciborium::Value::Array(value.0.into_inner().into_iter().map(|v| v.to_cbor()).collect())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::str::FromStr;
use isomdl_macros::FromJson;
use crate::definitions::helpers::NonEmptyVec;
use crate::definitions::namespaces::latin1::Latin1;
Expand All @@ -7,6 +8,12 @@ use crate::definitions::traits::ToCbor;
#[isomdl(crate = "crate")]
pub struct DocTypes(NonEmptyVec<Latin1>);

impl DocTypes {
pub fn new(doc_types: Vec<String>) -> Self {
let v: Vec<Latin1> = doc_types.iter().map(|s| Latin1::from_str(s.as_str()).unwrap()).collect();
Self(NonEmptyVec::try_from(v).unwrap())
}
}
impl From<DocTypes> for ciborium::Value {
fn from(value: DocTypes) -> ciborium::Value {
ciborium::Value::Array(value.0.into_inner().into_iter().map(|v| v.to_cbor()).collect())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ use crate::definitions::traits::ToCbor;
pub struct Extensions(BTreeMap<String, ByteStr>);

impl Extensions {
pub fn new(extensions: BTreeMap<String, ByteStr>) -> Self {
Self(extensions)
pub fn new(extensions: BTreeMap<String, Vec<u8>>) -> Self {
let v = extensions.iter().map( | (k, v)| (k.clone(), ByteStr::from(v.clone()))).collect();
Self(v)
}
}
impl From<Extensions> for ciborium::Value {
Expand Down
5 changes: 2 additions & 3 deletions src/definitions/namespaces/org_iso_18013_5_1_vical/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use std::collections::BTreeMap;
use std::str::FromStr;
pub use certificate_info::{CertificateInfos, CertificateInfo};
pub use extension::Extensions;
use crate::definitions::helpers::ByteStr;
pub use super::latin1::Latin1;
pub use super::org_iso_18013_5_1::TDate;
use crate::macros::{FromJson, ToCbor};
Expand Down Expand Up @@ -41,7 +40,7 @@ pub struct VicalBuilder {
certificate_infos: Vec<CertificateInfo>,
vical_issue_id: Option<u32>,
next_update: Option<String>,
extensions: Option<BTreeMap<String, ByteStr>>,
extensions: Option<BTreeMap<String, Vec<u8>>>,
}

impl VicalBuilder {
Expand All @@ -68,7 +67,7 @@ impl VicalBuilder {
self.certificate_infos = certificate_infos;
self
}
pub fn extensions(mut self, extensions: BTreeMap<String, ByteStr>) -> Self {
pub fn extensions(mut self, extensions: BTreeMap<String, Vec<u8>>) -> Self {
self.extensions = Some(extensions);
self
}
Expand Down

0 comments on commit 88129ff

Please sign in to comment.