Skip to content

Commit

Permalink
verifier: Add option to select encoding (der or pem) for cert command.
Browse files Browse the repository at this point in the history
  • Loading branch information
flihp committed Dec 4, 2023
1 parent 8f4cd75 commit ab4acdf
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions verifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ anyhow = { workspace = true, features = ["std"] }
clap.workspace = true
env_logger.workspace = true
log.workspace = true
pem-rfc7468 = { workspace = true, features = ["alloc", "std"] }
sha3.workspace = true
tempfile.workspace = true
35 changes: 34 additions & 1 deletion verifier/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use anyhow::{anyhow, Context, Result};
use clap::{Parser, Subcommand, ValueEnum};
use env_logger::Builder;
use log::{debug, info, LevelFilter};
use pem_rfc7468::LineEnding;
use sha3::{Digest, Sha3_256};
use std::{
fmt::{self, Debug, Formatter},
Expand Down Expand Up @@ -38,6 +39,10 @@ struct Args {
enum AttestCommand {
/// Get a certificate from the Attest task.
Cert {
/// Target encoding for certificate.
#[clap(long, env, default_value_t = Encoding::Der)]
encoding: Encoding,

/// Index of certificate in certificate chain.
#[clap(long, env)]
index: u32,
Expand Down Expand Up @@ -91,6 +96,22 @@ impl fmt::Display for Interface {
}
}

/// An enum of the possible certificate encodings.
#[derive(Clone, Debug, ValueEnum)]
enum Encoding {
Der,
Pem,
}

impl fmt::Display for Encoding {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
Encoding::Der => write!(f, "der"),
Encoding::Pem => write!(f, "pem"),
}
}
}

/// Nonce is a newtype around an appropriately sized byte array. The newtype
/// is convenient way to encapsulate the required conversion functions.
struct Nonce([u8; 32]);
Expand Down Expand Up @@ -376,11 +397,23 @@ fn main() -> Result<()> {
let attest = AttestHiffy::new(args.interface);

match args.command {
AttestCommand::Cert { index } => {
AttestCommand::Cert { encoding, index } => {
let cert_len = attest.cert_len(index)?;
let mut out = vec![0u8; cert_len as usize];
attest.cert(index, &mut out)?;

let out = match encoding {
Encoding::Der => out,
Encoding::Pem => {
let pem = pem_rfc7468::encode_string(
"CERTIFICATE",
LineEnding::default(),
&out,
)?;
pem.as_bytes().to_vec()
}
};

io::stdout().write_all(&out)?;
io::stdout().flush()?;
}
Expand Down

0 comments on commit ab4acdf

Please sign in to comment.