Skip to content

Commit

Permalink
Minor updates to RustCrypto impl
Browse files Browse the repository at this point in the history
* Remove redundant `use` in crypto
* Reorganize default platform parsing options
  • Loading branch information
esmusick authored and jhand2 committed Dec 15, 2023
1 parent 229cafb commit d7c3813
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 52 deletions.
1 change: 0 additions & 1 deletion crypto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ pub use signer::*;

#[cfg(feature = "rustcrypto")]
pub use crate::rustcrypto::*;
pub use signer::*;

#[cfg(feature = "openssl")]
pub mod openssl;
Expand Down
102 changes: 51 additions & 51 deletions platform/src/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,6 @@ use crate::{Platform, PlatformError, MAX_CHUNK_SIZE, MAX_SN_SIZE};
use cfg_if::cfg_if;
use core::cmp::min;

cfg_if! {
if #[cfg(feature = "openssl")] {
use openssl::x509::X509;
} else if #[cfg(feature = "rustcrypto")] {
use x509_cert::{
certificate::Certificate,
der::{DecodePem, Encode},
};
}
}

pub struct DefaultPlatform;

pub const AUTO_INIT_LOCALITY: u32 = 0;
Expand All @@ -34,45 +23,56 @@ pub const TEST_CERT_PEM: &[u8] = include_bytes!("test_data/cert_256.pem");
#[cfg(feature = "dpe_profile_p384_sha384")]
pub const TEST_CERT_PEM: &[u8] = include_bytes!("test_data/cert_384.pem");

impl DefaultPlatform {
cfg_if! {
if #[cfg(feature = "openssl")] {
fn parse_issuer_name() -> Vec<u8> {
X509::from_pem(TEST_CERT_PEM)
.unwrap()
.subject_name()
.to_der()
.unwrap()
}} else if #[cfg(feature = "rustcrypto")] {
fn parse_issuer_name() -> Vec<u8> {
Certificate::from_pem(TEST_CERT_PEM)
.unwrap()
.tbs_certificate
.subject
.to_der()
.unwrap()
}
}}

cfg_if! {
if #[cfg(feature = "openssl")] {
fn parse_issuer_sn() -> Vec<u8> {
X509::from_pem(TEST_CERT_PEM)
.unwrap()
.serial_number()
.to_bn()
.unwrap()
.to_vec()
}
} else if #[cfg(feature = "rustcrypto")] {
fn parse_issuer_sn() -> Vec<u8> {
Certificate::from_pem(TEST_CERT_PEM)
.unwrap()
.tbs_certificate
.serial_number
.as_bytes()
.to_vec()
cfg_if! {
if #[cfg(feature = "openssl")] {
mod parse {
use super::*;
use openssl::x509::X509;
pub struct DefaultPlatform;
impl DefaultPlatform {
pub fn parse_issuer_name() -> Vec<u8> {
X509::from_pem(TEST_CERT_PEM)
.unwrap()
.subject_name()
.to_der()
.unwrap()
}
pub fn parse_issuer_sn() -> Vec<u8> {
X509::from_pem(TEST_CERT_PEM)
.unwrap()
.serial_number()
.to_bn()
.unwrap()
.to_vec()
}
}
}
} else if #[cfg(feature = "rustcrypto")] {
mod parse {
use super::*;
use x509_cert::{
certificate::Certificate,
der::{DecodePem, Encode},
};
pub struct DefaultPlatform;
impl DefaultPlatform {
pub fn parse_issuer_name() -> Vec<u8> {
Certificate::from_pem(TEST_CERT_PEM)
.unwrap()
.tbs_certificate
.subject
.to_der()
.unwrap()
}
pub fn parse_issuer_sn() -> Vec<u8> {
Certificate::from_pem(TEST_CERT_PEM)
.unwrap()
.tbs_certificate
.serial_number
.as_bytes()
.to_vec()
}
}
}
}
}
Expand Down Expand Up @@ -101,7 +101,7 @@ impl Platform for DefaultPlatform {
}

fn get_issuer_name(&mut self, out: &mut [u8; MAX_CHUNK_SIZE]) -> Result<usize, PlatformError> {
let issuer_name = DefaultPlatform::parse_issuer_name();
let issuer_name = parse::DefaultPlatform::parse_issuer_name();
if issuer_name.len() > out.len() {
return Err(PlatformError::IssuerNameError(0));
}
Expand All @@ -110,7 +110,7 @@ impl Platform for DefaultPlatform {
}

fn get_issuer_sn(&mut self, out: &mut [u8; MAX_SN_SIZE]) -> Result<usize, PlatformError> {
let sn = DefaultPlatform::parse_issuer_sn();
let sn = parse::DefaultPlatform::parse_issuer_sn();
if sn.len() > out.len() {
return Err(PlatformError::IssuerNameError(0));
}
Expand Down

0 comments on commit d7c3813

Please sign in to comment.