1 use std
::collections
::HashMap
;
3 use anyhow
::{bail, Error}
;
4 use serde
::{Deserialize, Serialize}
;
6 use proxmox
::tools
::fs
::{
7 file_read_optional_string
,
19 encrypt_key_with_passphrase
,
24 use serde
::{self, Deserialize, Serializer, Deserializer}
;
29 ) -> Result
<S
::Ok
, S
::Error
>
33 let s
= proxmox
::tools
::digest_to_hex(csum
);
34 serializer
.serialize_str(&s
)
37 pub fn deserialize
<'de
, D
>(
39 ) -> Result
<[u8; 32], D
::Error
>
43 let s
= String
::deserialize(deserializer
)?
;
44 proxmox
::tools
::hex_to_digest(&s
).map_err(serde
::de
::Error
::custom
)
48 /// Store Hardware Encryption keys (private part)
49 #[derive(Deserialize, Serialize)]
50 pub struct EncryptionKeyInfo
{
51 pub fingerprint
: Fingerprint
,
52 #[serde(with = "hex_key")]
56 pub fn compute_tape_key_fingerprint(key
: &[u8; 32]) -> Result
<Fingerprint
, Error
> {
57 let crypt_config
= CryptConfig
::new(key
.clone())?
;
58 Ok(crypt_config
.fingerprint())
61 pub fn generate_tape_encryption_key(password
: &[u8]) -> Result
<([u8; 32], KeyConfig
), Error
> {
63 let mut key
= [0u8; 32];
64 proxmox
::sys
::linux
::fill_with_random_data(&mut key
)?
;
66 let mut key_config
= encrypt_key_with_passphrase(&key
, password
, Kdf
::Scrypt
)?
;
67 key_config
.fingerprint
= Some(compute_tape_key_fingerprint(&key
)?
);
71 impl EncryptionKeyInfo
{
72 pub fn new(key
: [u8; 32], fingerprint
: Fingerprint
) -> Self {
73 Self { fingerprint, key }
77 pub const TAPE_KEYS_FILENAME
: &str = "/etc/proxmox-backup/tape-encryption-keys.json";
78 pub const TAPE_KEY_CONFIG_FILENAME
: &str = "/etc/proxmox-backup/tape-encryption-key-config.json";
79 pub const TAPE_KEYS_LOCKFILE
: &str = "/etc/proxmox-backup/.tape-encryption-keys.lck";
81 /// Load tape encryption keys (private part)
82 pub fn load_keys() -> Result
<(HashMap
<Fingerprint
, EncryptionKeyInfo
>, [u8;32]), Error
> {
84 let content
= file_read_optional_string(TAPE_KEYS_FILENAME
)?
;
85 let content
= content
.unwrap_or_else(|| String
::from("[]"));
87 let digest
= openssl
::sha
::sha256(content
.as_bytes());
89 let key_list
: Vec
<EncryptionKeyInfo
> = serde_json
::from_str(&content
)?
;
91 let mut map
= HashMap
::new();
93 for item
in key_list
{
94 let expected_fingerprint
= compute_tape_key_fingerprint(&item
.key
)?
;
95 if item
.fingerprint
!= expected_fingerprint
{
97 "inconsistent fingerprint ({} != {})",
103 if map
.insert(item
.fingerprint
.clone(), item
).is_some() {
104 bail
!("found duplicate fingerprint");
111 /// Load tape encryption key configurations (public part)
112 pub fn load_key_configs() -> Result
<(HashMap
<Fingerprint
, KeyConfig
>, [u8;32]), Error
> {
114 let content
= file_read_optional_string(TAPE_KEY_CONFIG_FILENAME
)?
;
115 let content
= content
.unwrap_or_else(|| String
::from("[]"));
117 let digest
= openssl
::sha
::sha256(content
.as_bytes());
119 let key_list
: Vec
<KeyConfig
> = serde_json
::from_str(&content
)?
;
121 let mut map
= HashMap
::new();
123 for key_config
in key_list
{
124 match key_config
.fingerprint
{
125 Some(ref fingerprint
) => {
126 if map
.insert(fingerprint
.clone(), key_config
).is_some() {
127 bail
!("found duplicate fingerprint");
130 None
=> bail
!("missing fingerprint"),
137 pub fn save_keys(map
: HashMap
<Fingerprint
, EncryptionKeyInfo
>) -> Result
<(), Error
> {
139 let mut list
= Vec
::new();
141 for (_fp
, item
) in map
{
145 let raw
= serde_json
::to_string_pretty(&list
)?
;
147 let mode
= nix
::sys
::stat
::Mode
::from_bits_truncate(0o0600);
148 // set the correct owner/group/permissions while saving file
149 // owner(rw) = root, group(r)= root
150 let options
= CreateOptions
::new()
152 .owner(nix
::unistd
::ROOT
)
153 .group(nix
::unistd
::Gid
::from_raw(0));
155 replace_file(TAPE_KEYS_FILENAME
, raw
.as_bytes(), options
)?
;
160 pub fn save_key_configs(map
: HashMap
<Fingerprint
, KeyConfig
>) -> Result
<(), Error
> {
162 let mut list
= Vec
::new();
164 for (_fp
, item
) in map
{
168 let raw
= serde_json
::to_string_pretty(&list
)?
;
170 let backup_user
= crate::backup
::backup_user()?
;
171 let mode
= nix
::sys
::stat
::Mode
::from_bits_truncate(0o0600);
172 // set the correct owner/group/permissions while saving file
173 // owner(rw) = root, group(r)= backup
174 let options
= CreateOptions
::new()
176 .owner(nix
::unistd
::ROOT
)
177 .group(backup_user
.gid
);
179 replace_file(TAPE_KEY_CONFIG_FILENAME
, raw
.as_bytes(), options
)?
;
184 pub fn insert_key(key
: [u8;32], key_config
: KeyConfig
) -> Result
<(), Error
> {
186 let _lock
= open_file_locked(
188 std
::time
::Duration
::new(10, 0),
192 let (mut key_map
, _
) = load_keys()?
;
193 let (mut config_map
, _
) = load_key_configs()?
;
195 let fingerprint
= match key_config
.fingerprint
.clone() {
196 Some(fingerprint
) => fingerprint
,
197 None
=> bail
!("missing encryption key fingerprint - internal error"),
200 if let Some(_
) = config_map
.get(&fingerprint
) {
201 bail
!("encryption key '{}' already exists.", fingerprint
);
204 let item
= EncryptionKeyInfo
::new(key
, fingerprint
.clone());
205 key_map
.insert(fingerprint
.clone(), item
);
208 config_map
.insert(fingerprint
.clone(), key_config
);
209 save_key_configs(config_map
)?
;
215 // shell completion helper
216 pub fn complete_key_fingerprint(_arg
: &str, _param
: &HashMap
<String
, String
>) -> Vec
<String
> {
217 let data
= match load_key_configs() {
218 Ok((data
, _digest
)) => data
,
219 Err(_
) => return Vec
::new(),
222 data
.keys().map(|fp
| crate::tools
::format
::as_fingerprint(fp
.bytes())).collect()