From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 761A0619AB for ; Fri, 20 Nov 2020 17:40:22 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 710F0151F1 for ; Fri, 20 Nov 2020 17:39:52 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [212.186.127.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id ACADA151E7 for ; Fri, 20 Nov 2020 17:39:51 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 74A0D43D48 for ; Fri, 20 Nov 2020 17:39:51 +0100 (CET) From: =?UTF-8?q?Fabian=20Gr=C3=BCnbichler?= To: pbs-devel@lists.proxmox.com Date: Fri, 20 Nov 2020 17:38:38 +0100 Message-Id: <20201120163845.1225080-9-f.gruenbichler@proxmox.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20201120163845.1225080-1-f.gruenbichler@proxmox.com> References: <20201120163845.1225080-1-f.gruenbichler@proxmox.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.025 Adjusted score from AWL reputation of From: address KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [key.rs] Subject: [pbs-devel] [PATCH proxmox-backup 08/13] paperkey: refactor common code X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 20 Nov 2020 16:40:22 -0000 from formatting functions to main function, and pass along the key data lines instead of the full string. Signed-off-by: Fabian Grünbichler --- src/bin/proxmox_backup_client/key.rs | 104 +++++++++++++-------------- 1 file changed, 52 insertions(+), 52 deletions(-) diff --git a/src/bin/proxmox_backup_client/key.rs b/src/bin/proxmox_backup_client/key.rs index ea7e8c82..e56308b7 100644 --- a/src/bin/proxmox_backup_client/key.rs +++ b/src/bin/proxmox_backup_client/key.rs @@ -385,13 +385,47 @@ fn paper_key( }; let data = file_get_contents(&path)?; - let data = std::str::from_utf8(&data)?; + let data = String::from_utf8(data)?; + + let (data, is_private_key) = if data.starts_with("-----BEGIN ENCRYPTED PRIVATE KEY-----\n") { + let lines: Vec = data + .lines() + .map(|s| s.trim_end()) + .filter(|s| !s.is_empty()) + .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"); + } + + (lines, true) + } else { + match serde_json::from_str::(&data) { + Ok(key_config) => { + let lines = serde_json::to_string_pretty(&key_config)? + .lines() + .map(String::from) + .collect(); + + (lines, false) + }, + Err(err) => { + eprintln!("Couldn't parse '{:?}' as KeyConfig - {}", path, err); + bail!("Neither a PEM-formatted private key, nor a PBS key file."); + }, + } + }; let format = output_format.unwrap_or(PaperkeyFormat::Html); match format { - PaperkeyFormat::Html => paperkey_html(data, subject), - PaperkeyFormat::Text => paperkey_text(data, subject), + PaperkeyFormat::Html => paperkey_html(&data, subject, is_private_key), + PaperkeyFormat::Text => paperkey_text(&data, subject, is_private_key), } } @@ -426,7 +460,7 @@ pub fn cli() -> CliCommandMap { .insert("paperkey", paper_key_cmd_def) } -fn paperkey_html(data: &str, subject: Option) -> Result<(), Error> { +fn paperkey_html(lines: &[String], subject: Option, is_private: bool) -> Result<(), Error> { let img_size_pt = 500; @@ -455,21 +489,7 @@ fn paperkey_html(data: &str, subject: Option) -> Result<(), Error> { println!("

Subject: {}

", subject); } - if data.starts_with("-----BEGIN ENCRYPTED PRIVATE KEY-----\n") { - let lines: Vec = data.lines() - .map(|s| s.trim_end()) - .filter(|s| !s.is_empty()) - .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"); - } - + if is_private { const BLOCK_SIZE: usize = 20; let blocks = (lines.len() + BLOCK_SIZE -1)/BLOCK_SIZE; @@ -490,8 +510,7 @@ fn paperkey_html(data: &str, subject: Option) -> Result<(), Error> { println!("

"); - let data = data.join("\n"); - let qr_code = generate_qr_code("svg", data.as_bytes())?; + let qr_code = generate_qr_code("svg", data)?; let qr_code = base64::encode_config(&qr_code, base64::STANDARD_NO_PAD); println!("
"); @@ -507,16 +526,13 @@ fn paperkey_html(data: &str, subject: Option) -> Result<(), Error> { return Ok(()); } - let key_config: KeyConfig = serde_json::from_str(&data)?; - let key_text = serde_json::to_string_pretty(&key_config)?; - println!("
"); println!("

"); println!("-----BEGIN PROXMOX BACKUP KEY-----"); - for line in key_text.lines() { + for line in lines { println!("{}", line); } @@ -524,7 +540,7 @@ fn paperkey_html(data: &str, subject: Option) -> Result<(), Error> { println!("

"); - let qr_code = generate_qr_code("svg", key_text.as_bytes())?; + let qr_code = generate_qr_code("svg", lines)?; let qr_code = base64::encode_config(&qr_code, base64::STANDARD_NO_PAD); println!("
"); @@ -541,27 +557,13 @@ fn paperkey_html(data: &str, subject: Option) -> Result<(), Error> { Ok(()) } -fn paperkey_text(data: &str, subject: Option) -> Result<(), Error> { +fn paperkey_text(lines: &[String], subject: Option, is_private: bool) -> Result<(), Error> { if let Some(subject) = subject { println!("Subject: {}\n", subject); } - if data.starts_with("-----BEGIN ENCRYPTED PRIVATE KEY-----\n") { - let lines: Vec = data.lines() - .map(|s| s.trim_end()) - .filter(|s| !s.is_empty()) - .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"); - } - + if is_private { const BLOCK_SIZE: usize = 5; let blocks = (lines.len() + BLOCK_SIZE -1)/BLOCK_SIZE; @@ -576,8 +578,7 @@ fn paperkey_text(data: &str, subject: Option) -> Result<(), Error> { for l in start..end { println!("{:-2}: {}", l, lines[l]); } - let data = data.join("\n"); - let qr_code = generate_qr_code("utf8i", data.as_bytes())?; + let qr_code = generate_qr_code("utf8i", data)?; let qr_code = String::from_utf8(qr_code) .map_err(|_| format_err!("Failed to read qr code (got non-utf8 data)"))?; println!("{}", qr_code); @@ -587,14 +588,13 @@ fn paperkey_text(data: &str, subject: Option) -> Result<(), Error> { return Ok(()); } - let key_config: KeyConfig = serde_json::from_str(&data)?; - let key_text = serde_json::to_string_pretty(&key_config)?; - println!("-----BEGIN PROXMOX BACKUP KEY-----"); - println!("{}", key_text); + for line in lines { + println!("{}", line); + } println!("-----END PROXMOX BACKUP KEY-----"); - let qr_code = generate_qr_code("utf8i", key_text.as_bytes())?; + let qr_code = generate_qr_code("utf8i", &lines)?; let qr_code = String::from_utf8(qr_code) .map_err(|_| format_err!("Failed to read qr code (got non-utf8 data)"))?; @@ -603,8 +603,7 @@ fn paperkey_text(data: &str, subject: Option) -> Result<(), Error> { Ok(()) } -fn generate_qr_code(output_type: &str, data: &[u8]) -> Result, Error> { - +fn generate_qr_code(output_type: &str, lines: &[String]) -> Result, Error> { let mut child = Command::new("qrencode") .args(&["-t", output_type, "-m0", "-s1", "-lm", "--output", "-"]) .stdin(Stdio::piped()) @@ -614,7 +613,8 @@ fn generate_qr_code(output_type: &str, data: &[u8]) -> Result, Error> { { let stdin = child.stdin.as_mut() .ok_or_else(|| format_err!("Failed to open stdin"))?; - stdin.write_all(data) + let data = lines.join("\n"); + stdin.write_all(data.as_bytes()) .map_err(|_| format_err!("Failed to write to stdin"))?; } -- 2.20.1