2021-01-21 17:23:07 +00:00
|
|
|
//! Store Tape encryptions keys
|
|
|
|
//!
|
|
|
|
//! This module can store 256bit encryption keys for tape backups,
|
|
|
|
//! indexed by key fingerprint.
|
|
|
|
//!
|
|
|
|
//! We store the plain key (unencrypted), as well as a encrypted
|
2021-03-10 15:37:09 +00:00
|
|
|
//! version protected by password (see struct `KeyConfig`)
|
2021-01-21 17:23:07 +00:00
|
|
|
//!
|
|
|
|
//! Tape backups store the password protected version on tape, so that
|
2021-03-10 15:37:09 +00:00
|
|
|
//! it is possible to restore the key from tape if you know the
|
2021-01-21 17:23:07 +00:00
|
|
|
//! password.
|
|
|
|
|
2021-01-19 11:35:15 +00:00
|
|
|
use std::collections::HashMap;
|
2021-01-18 06:16:06 +00:00
|
|
|
|
|
|
|
use anyhow::{bail, Error};
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
2021-11-23 16:57:00 +00:00
|
|
|
use proxmox_sys::fs::file_read_optional_string;
|
2021-08-30 09:49:22 +00:00
|
|
|
use pbs_api_types::Fingerprint;
|
2021-09-07 08:37:08 +00:00
|
|
|
|
|
|
|
use crate::key_config::KeyConfig;
|
|
|
|
use crate::{open_backup_lockfile, replace_secret_config, replace_backup_config};
|
2021-01-18 06:16:06 +00:00
|
|
|
|
|
|
|
mod hex_key {
|
|
|
|
use serde::{self, Deserialize, Serializer, Deserializer};
|
2021-11-23 16:57:00 +00:00
|
|
|
use hex::FromHex;
|
|
|
|
|
2021-01-18 06:16:06 +00:00
|
|
|
pub fn serialize<S>(
|
|
|
|
csum: &[u8; 32],
|
|
|
|
serializer: S,
|
|
|
|
) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: Serializer,
|
|
|
|
{
|
2021-11-23 16:57:00 +00:00
|
|
|
let s = hex::encode(csum);
|
2021-01-18 06:16:06 +00:00
|
|
|
serializer.serialize_str(&s)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn deserialize<'de, D>(
|
|
|
|
deserializer: D,
|
|
|
|
) -> Result<[u8; 32], D::Error>
|
|
|
|
where
|
|
|
|
D: Deserializer<'de>,
|
|
|
|
{
|
|
|
|
let s = String::deserialize(deserializer)?;
|
2021-11-23 16:57:00 +00:00
|
|
|
<[u8; 32]>::from_hex(&s).map_err(serde::de::Error::custom)
|
2021-01-18 06:16:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-21 06:23:51 +00:00
|
|
|
/// Store Hardware Encryption keys (plain, unprotected keys)
|
2021-01-18 06:16:06 +00:00
|
|
|
#[derive(Deserialize, Serialize)]
|
|
|
|
pub struct EncryptionKeyInfo {
|
2021-01-21 17:23:07 +00:00
|
|
|
/// Key fingerprint (we verify the fingerprint on load)
|
2021-01-19 05:19:18 +00:00
|
|
|
pub fingerprint: Fingerprint,
|
2021-01-21 17:23:07 +00:00
|
|
|
/// The plain encryption key
|
2021-01-18 06:16:06 +00:00
|
|
|
#[serde(with = "hex_key")]
|
|
|
|
pub key: [u8; 32],
|
2021-01-19 05:19:18 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 06:16:06 +00:00
|
|
|
impl EncryptionKeyInfo {
|
2021-01-19 05:19:18 +00:00
|
|
|
pub fn new(key: [u8; 32], fingerprint: Fingerprint) -> Self {
|
|
|
|
Self { fingerprint, key }
|
|
|
|
}
|
|
|
|
}
|
2021-01-18 06:16:06 +00:00
|
|
|
|
|
|
|
pub const TAPE_KEYS_FILENAME: &str = "/etc/proxmox-backup/tape-encryption-keys.json";
|
2021-01-19 05:19:18 +00:00
|
|
|
pub const TAPE_KEY_CONFIG_FILENAME: &str = "/etc/proxmox-backup/tape-encryption-key-config.json";
|
2021-01-18 06:16:06 +00:00
|
|
|
pub const TAPE_KEYS_LOCKFILE: &str = "/etc/proxmox-backup/.tape-encryption-keys.lck";
|
|
|
|
|
2021-01-21 06:23:51 +00:00
|
|
|
/// Load tape encryption keys (plain, unprotected keys)
|
2021-01-18 06:16:06 +00:00
|
|
|
pub fn load_keys() -> Result<(HashMap<Fingerprint, EncryptionKeyInfo>, [u8;32]), Error> {
|
|
|
|
|
|
|
|
let content = file_read_optional_string(TAPE_KEYS_FILENAME)?;
|
|
|
|
let content = content.unwrap_or_else(|| String::from("[]"));
|
|
|
|
|
|
|
|
let digest = openssl::sha::sha256(content.as_bytes());
|
|
|
|
|
2021-01-19 05:19:18 +00:00
|
|
|
let key_list: Vec<EncryptionKeyInfo> = serde_json::from_str(&content)?;
|
2021-01-18 06:16:06 +00:00
|
|
|
|
|
|
|
let mut map = HashMap::new();
|
2021-01-19 05:19:18 +00:00
|
|
|
|
|
|
|
for item in key_list {
|
2021-01-21 10:56:54 +00:00
|
|
|
let key_config = KeyConfig::without_password(item.key)?; // to compute fingerprint
|
|
|
|
let expected_fingerprint = key_config.fingerprint.unwrap();
|
2021-01-18 06:16:06 +00:00
|
|
|
if item.fingerprint != expected_fingerprint {
|
|
|
|
bail!(
|
|
|
|
"inconsistent fingerprint ({} != {})",
|
|
|
|
item.fingerprint,
|
|
|
|
expected_fingerprint,
|
|
|
|
);
|
|
|
|
}
|
2021-01-19 05:19:18 +00:00
|
|
|
|
|
|
|
if map.insert(item.fingerprint.clone(), item).is_some() {
|
|
|
|
bail!("found duplicate fingerprint");
|
|
|
|
}
|
2021-01-18 06:16:06 +00:00
|
|
|
}
|
2021-01-19 05:19:18 +00:00
|
|
|
|
|
|
|
Ok((map, digest))
|
|
|
|
}
|
|
|
|
|
2021-01-21 06:23:51 +00:00
|
|
|
/// Load tape encryption key configurations (password protected keys)
|
2021-01-19 11:35:15 +00:00
|
|
|
pub fn load_key_configs() -> Result<(HashMap<Fingerprint, KeyConfig>, [u8;32]), Error> {
|
2021-01-19 05:19:18 +00:00
|
|
|
|
|
|
|
let content = file_read_optional_string(TAPE_KEY_CONFIG_FILENAME)?;
|
|
|
|
let content = content.unwrap_or_else(|| String::from("[]"));
|
|
|
|
|
|
|
|
let digest = openssl::sha::sha256(content.as_bytes());
|
|
|
|
|
2021-01-19 11:35:15 +00:00
|
|
|
let key_list: Vec<KeyConfig> = serde_json::from_str(&content)?;
|
2021-01-19 05:19:18 +00:00
|
|
|
|
|
|
|
let mut map = HashMap::new();
|
|
|
|
|
2021-01-19 11:35:15 +00:00
|
|
|
for key_config in key_list {
|
|
|
|
match key_config.fingerprint {
|
2021-01-19 05:19:18 +00:00
|
|
|
Some(ref fingerprint) => {
|
2021-01-19 11:35:15 +00:00
|
|
|
if map.insert(fingerprint.clone(), key_config).is_some() {
|
2021-01-19 05:19:18 +00:00
|
|
|
bail!("found duplicate fingerprint");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => bail!("missing fingerprint"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-18 06:16:06 +00:00
|
|
|
Ok((map, digest))
|
|
|
|
}
|
|
|
|
|
2021-01-21 17:23:07 +00:00
|
|
|
/// Store tape encryption keys (plain, unprotected keys)
|
|
|
|
///
|
|
|
|
/// The file is only accessible by user root (mode 0600).
|
2021-01-18 06:16:06 +00:00
|
|
|
pub fn save_keys(map: HashMap<Fingerprint, EncryptionKeyInfo>) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let mut list = Vec::new();
|
|
|
|
|
|
|
|
for (_fp, item) in map {
|
|
|
|
list.push(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
let raw = serde_json::to_string_pretty(&list)?;
|
2021-09-02 10:47:11 +00:00
|
|
|
replace_secret_config(TAPE_KEYS_FILENAME, raw.as_bytes())
|
2021-01-18 06:16:06 +00:00
|
|
|
}
|
|
|
|
|
2021-01-21 17:23:07 +00:00
|
|
|
/// Store tape encryption key configurations (password protected keys)
|
2021-01-19 11:35:15 +00:00
|
|
|
pub fn save_key_configs(map: HashMap<Fingerprint, KeyConfig>) -> Result<(), Error> {
|
2021-01-19 05:19:18 +00:00
|
|
|
|
|
|
|
let mut list = Vec::new();
|
|
|
|
|
|
|
|
for (_fp, item) in map {
|
|
|
|
list.push(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
let raw = serde_json::to_string_pretty(&list)?;
|
2021-09-07 08:37:08 +00:00
|
|
|
replace_backup_config(TAPE_KEY_CONFIG_FILENAME, raw.as_bytes())
|
2021-01-19 05:19:18 +00:00
|
|
|
}
|
|
|
|
|
2021-01-21 17:23:07 +00:00
|
|
|
/// Insert a new key
|
|
|
|
///
|
|
|
|
/// Get the lock, load both files, insert the new key, store files.
|
2021-01-21 06:46:21 +00:00
|
|
|
pub fn insert_key(key: [u8;32], key_config: KeyConfig, force: bool) -> Result<(), Error> {
|
2021-01-19 05:19:18 +00:00
|
|
|
|
2021-07-20 11:51:54 +00:00
|
|
|
let _lock = open_backup_lockfile(TAPE_KEYS_LOCKFILE, None, true)?;
|
2021-01-19 05:19:18 +00:00
|
|
|
|
|
|
|
let (mut key_map, _) = load_keys()?;
|
|
|
|
let (mut config_map, _) = load_key_configs()?;
|
|
|
|
|
|
|
|
let fingerprint = match key_config.fingerprint.clone() {
|
|
|
|
Some(fingerprint) => fingerprint,
|
|
|
|
None => bail!("missing encryption key fingerprint - internal error"),
|
|
|
|
};
|
|
|
|
|
2021-01-21 06:46:21 +00:00
|
|
|
if !force {
|
2021-01-21 09:56:52 +00:00
|
|
|
if config_map.get(&fingerprint).is_some() {
|
2021-01-21 06:46:21 +00:00
|
|
|
bail!("encryption key '{}' already exists.", fingerprint);
|
|
|
|
}
|
2021-01-19 05:19:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let item = EncryptionKeyInfo::new(key, fingerprint.clone());
|
|
|
|
key_map.insert(fingerprint.clone(), item);
|
|
|
|
save_keys(key_map)?;
|
|
|
|
|
2021-01-19 11:35:15 +00:00
|
|
|
config_map.insert(fingerprint.clone(), key_config);
|
2021-01-19 05:19:18 +00:00
|
|
|
save_key_configs(config_map)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-01-18 06:16:06 +00:00
|
|
|
// shell completion helper
|
2021-01-21 17:23:07 +00:00
|
|
|
/// Complete tape encryption key fingerprints
|
2021-01-18 06:16:06 +00:00
|
|
|
pub fn complete_key_fingerprint(_arg: &str, _param: &HashMap<String, String>) -> Vec<String> {
|
2021-01-19 05:19:18 +00:00
|
|
|
let data = match load_key_configs() {
|
2021-01-18 06:16:06 +00:00
|
|
|
Ok((data, _digest)) => data,
|
|
|
|
Err(_) => return Vec::new(),
|
|
|
|
};
|
|
|
|
|
2021-11-22 07:19:09 +00:00
|
|
|
data.keys().map(|fp| fp.signature()).collect()
|
2021-01-18 06:16:06 +00:00
|
|
|
}
|