Skip to content

Commit

Permalink
pem: Account for API changes in 1.x to 3.x upgrade.
Browse files Browse the repository at this point in the history
  • Loading branch information
flihp committed Aug 4, 2023
1 parent 29ce936 commit 709c164
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 21 deletions.
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.

2 changes: 1 addition & 1 deletion dice-cert-tmpl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
23 changes: 11 additions & 12 deletions dice-cert-tmpl/src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand All @@ -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)?;
Expand Down Expand Up @@ -153,15 +153,14 @@ pub fn write_csr<T: Write>(
) -> Result<(), Box<dyn Error>> {
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())?;
}
Expand Down
15 changes: 7 additions & 8 deletions dice-mfg/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -623,22 +623,21 @@ fn sized_blob_from_pem_path(p: &PathBuf) -> Result<SizedBlob> {
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<W: Write>(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())?)
Expand Down

0 comments on commit 709c164

Please sign in to comment.