diff --git a/src/tools/paperkey.rs b/src/tools/paperkey.rs index 030275cc..859e8aed 100644 --- a/src/tools/paperkey.rs +++ b/src/tools/paperkey.rs @@ -30,8 +30,16 @@ pub fn generate_paper_key( subject: Option, output_format: Option, ) -> Result<(), Error> { + let (data, is_master_key) = if data.starts_with("-----BEGIN ENCRYPTED PRIVATE KEY-----\n") + || data.starts_with("-----BEGIN RSA PRIVATE KEY-----\n") + { + let data = data.trim_end(); + if !(data.ends_with("\n-----END ENCRYPTED PRIVATE KEY-----") + || data.ends_with("\n-----END RSA PRIVATE KEY-----")) + { + bail!("unexpected key format"); + } - let (data, is_private_key) = if data.starts_with("-----BEGIN ENCRYPTED PRIVATE KEY-----\n") { let lines: Vec = data .lines() .map(|s| s.trim_end()) @@ -39,10 +47,6 @@ pub fn generate_paper_key( .map(String::from) .collect(); - if !lines[lines.len()-1].starts_with("-----END ENCRYPTED PRIVATE KEY-----") { - bail!("unexpected key format"); - } - if lines.len() < 20 { bail!("unexpected key format"); } @@ -68,8 +72,8 @@ pub fn generate_paper_key( let format = output_format.unwrap_or(PaperkeyFormat::Html); match format { - PaperkeyFormat::Html => paperkey_html(output, &data, subject, is_private_key), - PaperkeyFormat::Text => paperkey_text(output, &data, subject, is_private_key), + PaperkeyFormat::Html => paperkey_html(output, &data, subject, is_master_key), + PaperkeyFormat::Text => paperkey_text(output, &data, subject, is_master_key), } } @@ -77,7 +81,7 @@ fn paperkey_html( mut output: W, lines: &[String], subject: Option, - is_private: bool, + is_master: bool, ) -> Result<(), Error> { let img_size_pt = 500; @@ -107,7 +111,7 @@ fn paperkey_html( writeln!(output, "

Subject: {}

", subject)?; } - if is_private { + if is_master { const BLOCK_SIZE: usize = 20; let blocks = (lines.len() + BLOCK_SIZE -1)/BLOCK_SIZE;