From e0af222ec3698d77e8fa2226fddd2bea8cdd0db4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20Gr=C3=BCnbichler?= Date: Wed, 16 Dec 2020 14:41:11 +0100 Subject: [PATCH] KeyConfig: always calculate fingerprint MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit and warn if stored and calculated fingerprint don't match. Signed-off-by: Fabian Grünbichler --- src/backup/key_derivation.rs | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/src/backup/key_derivation.rs b/src/backup/key_derivation.rs index a91b21ca..7e8480d3 100644 --- a/src/backup/key_derivation.rs +++ b/src/backup/key_derivation.rs @@ -235,13 +235,16 @@ pub fn decrypt_key( let mut result = [0u8; 32]; result.copy_from_slice(&key); - let fingerprint = match key_config.fingerprint { - Some(fingerprint) => fingerprint, - None => { - let crypt_config = CryptConfig::new(result.clone())?; - crypt_config.fingerprint() - }, - }; + let crypt_config = CryptConfig::new(result.clone())?; + let fingerprint = crypt_config.fingerprint(); + if let Some(stored_fingerprint) = key_config.fingerprint { + if fingerprint != stored_fingerprint { + eprintln!( + "KeyConfig contains wrong fingerprint {}, contained key has fingerprint {}", + stored_fingerprint, fingerprint + ); + } + } Ok((result, created, fingerprint)) } @@ -313,5 +316,22 @@ fn encrypt_decrypt_test() -> Result<(), Error> { assert_eq!(key.data, decrypted); assert_eq!(key.fingerprint, Some(fingerprint)); + let key = KeyConfig { + kdf: None, + created: proxmox::tools::time::epoch_i64(), + modified: proxmox::tools::time::epoch_i64(), + data: (0u8..32u8).collect(), + fingerprint: Some(Fingerprint::new([0u8; 32])), // wrong FP + }; + let encrypted = rsa_encrypt_key_config(public.clone(), &key).expect("encryption failed"); + let (decrypted, created, fingerprint) = + rsa_decrypt_key_config(private.clone(), &encrypted, &passphrase) + .expect("decryption failed"); + + assert_eq!(key.created, created); + assert_eq!(key.data, decrypted); + // wrong FP update by round-trip through encrypt/decrypt + assert_ne!(key.fingerprint, Some(fingerprint)); + Ok(()) }