From 709c164a870fa32929f09fbac1adc6a90b58309a Mon Sep 17 00:00:00 2001 From: Philip Tricca Date: Fri, 4 Aug 2023 10:21:49 -0700 Subject: [PATCH] pem: Account for API changes in 1.x to 3.x upgrade. --- Cargo.lock | 1 + dice-cert-tmpl/Cargo.toml | 2 +- dice-cert-tmpl/src/encoding.rs | 23 +++++++++++------------ dice-mfg/src/lib.rs | 15 +++++++-------- 4 files changed, 20 insertions(+), 21 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ed5abd3..d93a7ea 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -878,6 +878,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed3127afbfc30b4cad60c34aeb741fb562a808642b81142bcf4afb73142da960" dependencies = [ "base64 0.21.2", + "serde", ] [[package]] diff --git a/dice-cert-tmpl/Cargo.toml b/dice-cert-tmpl/Cargo.toml index aeb48c6..fdbde29 100644 --- a/dice-cert-tmpl/Cargo.toml +++ b/dice-cert-tmpl/Cargo.toml @@ -7,6 +7,6 @@ license = "MPL-2.0" [dependencies] clap = { workspace = true, features = ["derive"] } dice-mfg-msgs = { path = "../dice-mfg-msgs" } -pem.workspace = true +pem = { workspace = true, default-features = true } salty.workspace = true tempfile.workspace = true diff --git a/dice-cert-tmpl/src/encoding.rs b/dice-cert-tmpl/src/encoding.rs index d44daa4..4e8dab2 100644 --- a/dice-cert-tmpl/src/encoding.rs +++ b/dice-cert-tmpl/src/encoding.rs @@ -71,11 +71,11 @@ fn decode_obj( let obj = fs::read_to_string(path)?; let parsed = pem::parse(obj)?; - if parsed.tag != tag { + if parsed.tag() != tag { return Err(Box::new(EncodingError::BadTag)); } - Ok(parsed.contents) + Ok(parsed.into_contents()) } Encoding::DER => Ok(fs::read(path)?), Encoding::RAW => Err(Box::new(EncodingError::InvalidEncoding)), @@ -95,14 +95,14 @@ pub fn decode_key( let key_str = fs::read_to_string(path)?; let key_pem = pem::parse(key_str)?; - if key_pem.tag != PRIV_KEY_TAG { + if key_pem.tag() != PRIV_KEY_TAG { return Err(Box::new(EncodingError::BadTag)); } - if key_pem.contents.len() != 0x30 { + if key_pem.contents().len() != 0x30 { return Err(Box::new(EncodingError::InvalidEncoding)); } - Ok(key_pem.contents[0x10..].to_vec()) + Ok(key_pem.contents()[0x10..].to_vec()) } Encoding::DER => { let key_der = fs::read(path)?; @@ -153,15 +153,14 @@ pub fn write_csr( ) -> Result<(), Box> { match encoding { Encoding::PEM => { - let pem = pem::Pem { - tag: String::from(PEM_CSR_TAG), - contents: csr.to_vec(), - }; + let pem = pem::Pem::new( + String::from(PEM_CSR_TAG), + csr.to_vec(), + ); let csr_pem = pem::encode_config( &pem, - pem::EncodeConfig { - line_ending: pem::LineEnding::LF, - }, + pem::EncodeConfig::new() + .set_line_ending(pem::LineEnding::LF) ); f.write_all(csr_pem.as_bytes())?; } diff --git a/dice-mfg/src/lib.rs b/dice-mfg/src/lib.rs index 1824d84..5b16065 100644 --- a/dice-mfg/src/lib.rs +++ b/dice-mfg/src/lib.rs @@ -623,22 +623,21 @@ fn sized_blob_from_pem_path(p: &PathBuf) -> Result { let cert = pem::parse(cert)?; // Error type doesn't implement std Error - Ok(SizedBlob::try_from(&cert.contents[..])?) + Ok(SizedBlob::try_from(cert.contents())?) } pub fn save_csr(mut w: W, csr: SizedBlob) -> Result<()> { let size = usize::from(csr.size); // encode as PEM - let pem = pem::Pem { - tag: String::from("CERTIFICATE REQUEST"), - contents: csr.as_bytes()[..size].to_vec(), - }; + let pem = pem::Pem::new( + String::from("CERTIFICATE REQUEST"), + csr.as_bytes()[..size].to_vec(), + ); let csr_pem = pem::encode_config( &pem, - pem::EncodeConfig { - line_ending: pem::LineEnding::LF, - }, + pem::EncodeConfig::new() + .set_line_ending(pem::LineEnding::LF), ); Ok(w.write_all(csr_pem.as_bytes())?)