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 B53B3605C6 for ; Fri, 5 Feb 2021 16:36:44 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id AABABAEE4 for ; Fri, 5 Feb 2021 16:36:14 +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 F122FAEDA for ; Fri, 5 Feb 2021 16:36:13 +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 BAB9E461F5 for ; Fri, 5 Feb 2021 16:36:13 +0100 (CET) From: =?UTF-8?q?Fabian=20Gr=C3=BCnbichler?= To: pbs-devel@lists.proxmox.com Date: Fri, 5 Feb 2021 16:35:29 +0100 Message-Id: <20210205153535.2578184-5-f.gruenbichler@proxmox.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210205153535.2578184-1-f.gruenbichler@proxmox.com> References: <20210205153535.2578184-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.026 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 03/10] key: rustfmt module 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, 05 Feb 2021 15:36:44 -0000 Signed-off-by: Fabian Grünbichler --- src/bin/proxmox_backup_client/key.rs | 75 ++++++++++++---------------- 1 file changed, 32 insertions(+), 43 deletions(-) diff --git a/src/bin/proxmox_backup_client/key.rs b/src/bin/proxmox_backup_client/key.rs index 43eaab5c..8a55e1ab 100644 --- a/src/bin/proxmox_backup_client/key.rs +++ b/src/bin/proxmox_backup_client/key.rs @@ -1,16 +1,12 @@ -use std::path::PathBuf; use std::convert::TryFrom; +use std::path::PathBuf; use anyhow::{bail, format_err, Error}; use serde_json::Value; use proxmox::api::api; use proxmox::api::cli::{ - ColumnConfig, - CliCommand, - CliCommandMap, - format_and_print_result_full, - get_output_format, + format_and_print_result_full, get_output_format, CliCommand, CliCommandMap, ColumnConfig, OUTPUT_FORMAT, }; use proxmox::api::router::ReturnType; @@ -18,40 +14,41 @@ use proxmox::sys::linux::tty; use proxmox::tools::fs::{file_get_contents, replace_file, CreateOptions}; use proxmox_backup::{ - tools::paperkey::{ - PaperkeyFormat, - generate_paper_key, - }, - api2::types::{ - PASSWORD_HINT_SCHEMA, - KeyInfo, - Kdf, - RsaPubKeyInfo, - }, - backup::{ - rsa_decrypt_key_config, - KeyConfig, - }, + api2::types::{Kdf, KeyInfo, RsaPubKeyInfo, PASSWORD_HINT_SCHEMA}, + backup::{rsa_decrypt_key_config, KeyConfig}, tools, + tools::paperkey::{generate_paper_key, PaperkeyFormat}, }; pub const DEFAULT_ENCRYPTION_KEY_FILE_NAME: &str = "encryption-key.json"; pub const DEFAULT_MASTER_PUBKEY_FILE_NAME: &str = "master-public.pem"; pub fn find_default_master_pubkey() -> Result, Error> { - super::find_xdg_file(DEFAULT_MASTER_PUBKEY_FILE_NAME, "default master public key file") + super::find_xdg_file( + DEFAULT_MASTER_PUBKEY_FILE_NAME, + "default master public key file", + ) } pub fn place_default_master_pubkey() -> Result { - super::place_xdg_file(DEFAULT_MASTER_PUBKEY_FILE_NAME, "default master public key file") + super::place_xdg_file( + DEFAULT_MASTER_PUBKEY_FILE_NAME, + "default master public key file", + ) } pub fn find_default_encryption_key() -> Result, Error> { - super::find_xdg_file(DEFAULT_ENCRYPTION_KEY_FILE_NAME, "default encryption key file") + super::find_xdg_file( + DEFAULT_ENCRYPTION_KEY_FILE_NAME, + "default encryption key file", + ) } pub fn place_default_encryption_key() -> Result { - super::place_xdg_file(DEFAULT_ENCRYPTION_KEY_FILE_NAME, "default encryption key file") + super::place_xdg_file( + DEFAULT_ENCRYPTION_KEY_FILE_NAME, + "default encryption key file", + ) } pub fn read_optional_default_encryption_key() -> Result>, Error> { @@ -100,11 +97,7 @@ pub fn get_encryption_key_password() -> Result, Error> { }, )] /// Create a new encryption key. -fn create( - kdf: Option, - path: Option, - hint: Option -) -> Result<(), Error> { +fn create(kdf: Option, path: Option, hint: Option) -> Result<(), Error> { let path = match path { Some(path) => PathBuf::from(path), None => { @@ -196,8 +189,7 @@ async fn import_with_master_key( let master_key = file_get_contents(&master_keyfile)?; let password = tty::read_password("Master Key Password: ")?; - let master_key = - openssl::pkey::PKey::private_key_from_pem_passphrase(&master_key, &password) + let master_key = openssl::pkey::PKey::private_key_from_pem_passphrase(&master_key, &password) .map_err(|err| format_err!("failed to read PEM-formatted private key - {}", err))? .rsa() .map_err(|err| format_err!("not a valid private RSA key - {}", err))?; @@ -216,7 +208,6 @@ async fn import_with_master_key( key_config.created = created; // keep original value key_config.store(path, true)?; - } Kdf::Scrypt | Kdf::PBKDF2 => { let password = tty::read_and_verify_password("New Password: ")?; @@ -259,10 +250,9 @@ fn change_passphrase( let path = match path { Some(path) => PathBuf::from(path), None => { - let path = find_default_encryption_key()? - .ok_or_else(|| { - format_err!("no encryption file provided and no default file found") - })?; + let path = find_default_encryption_key()?.ok_or_else(|| { + format_err!("no encryption file provided and no default file found") + })?; println!("updating default key at: {:?}", path); path } @@ -284,7 +274,7 @@ fn change_passphrase( } let mut key_config = KeyConfig::without_password(key)?; - key_config.created = created; // keep original value + key_config.created = created; // keep original value key_config.store(&path, true)?; } @@ -375,7 +365,7 @@ fn import_master_pubkey(path: String) -> Result<(), Error> { println!("Modulus: {}", info.modulus); println!("Exponent: {}", info.exponent); println!("Length: {}", info.length); - }, + } Err(err) => bail!("Unable to decode PEM data - {}", err), }; @@ -400,10 +390,8 @@ fn create_master_key() -> Result<(), Error> { let bits = 4096; println!("Generating {}-bit RSA key..", bits); let rsa = openssl::rsa::Rsa::generate(bits)?; - let public = openssl::rsa::Rsa::from_public_components( - rsa.n().to_owned()?, - rsa.e().to_owned()?, - )?; + let public = + openssl::rsa::Rsa::from_public_components(rsa.n().to_owned()?, rsa.e().to_owned()?)?; let info = RsaPubKeyInfo::try_from(public)?; println!("Modulus: {}", info.modulus); println!("Exponent: {}", info.exponent); @@ -419,7 +407,8 @@ fn create_master_key() -> Result<(), Error> { replace_file(filename_pub, pub_key.as_slice(), CreateOptions::new())?; let cipher = openssl::symm::Cipher::aes_256_cbc(); - let priv_key: Vec = pkey.private_key_to_pem_pkcs8_passphrase(cipher, password.as_bytes())?; + let priv_key: Vec = + pkey.private_key_to_pem_pkcs8_passphrase(cipher, password.as_bytes())?; let filename_priv = "master-private.pem"; println!("Writing private master key to {}", filename_priv); -- 2.20.1