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
,
21 use serde
::{self, Deserialize, Serializer, Deserializer}
;
26 ) -> Result
<S
::Ok
, S
::Error
>
30 let s
= proxmox
::tools
::digest_to_hex(csum
);
31 serializer
.serialize_str(&s
)
34 pub fn deserialize
<'de
, D
>(
36 ) -> Result
<[u8; 32], D
::Error
>
40 let s
= String
::deserialize(deserializer
)?
;
41 proxmox
::tools
::hex_to_digest(&s
).map_err(serde
::de
::Error
::custom
)
45 /// Store Hardware Encryption keys (plain, unprotected keys)
46 #[derive(Deserialize, Serialize)]
47 pub struct EncryptionKeyInfo
{
48 pub fingerprint
: Fingerprint
,
49 #[serde(with = "hex_key")]
53 impl EncryptionKeyInfo
{
54 pub fn new(key
: [u8; 32], fingerprint
: Fingerprint
) -> Self {
55 Self { fingerprint, key }
59 pub const TAPE_KEYS_FILENAME
: &str = "/etc/proxmox-backup/tape-encryption-keys.json";
60 pub const TAPE_KEY_CONFIG_FILENAME
: &str = "/etc/proxmox-backup/tape-encryption-key-config.json";
61 pub const TAPE_KEYS_LOCKFILE
: &str = "/etc/proxmox-backup/.tape-encryption-keys.lck";
63 /// Load tape encryption keys (plain, unprotected keys)
64 pub fn load_keys() -> Result
<(HashMap
<Fingerprint
, EncryptionKeyInfo
>, [u8;32]), Error
> {
66 let content
= file_read_optional_string(TAPE_KEYS_FILENAME
)?
;
67 let content
= content
.unwrap_or_else(|| String
::from("[]"));
69 let digest
= openssl
::sha
::sha256(content
.as_bytes());
71 let key_list
: Vec
<EncryptionKeyInfo
> = serde_json
::from_str(&content
)?
;
73 let mut map
= HashMap
::new();
75 for item
in key_list
{
76 let key_config
= KeyConfig
::without_password(item
.key
)?
; // to compute fingerprint
77 let expected_fingerprint
= key_config
.fingerprint
.unwrap();
78 if item
.fingerprint
!= expected_fingerprint
{
80 "inconsistent fingerprint ({} != {})",
86 if map
.insert(item
.fingerprint
.clone(), item
).is_some() {
87 bail
!("found duplicate fingerprint");
94 /// Load tape encryption key configurations (password protected keys)
95 pub fn load_key_configs() -> Result
<(HashMap
<Fingerprint
, KeyConfig
>, [u8;32]), Error
> {
97 let content
= file_read_optional_string(TAPE_KEY_CONFIG_FILENAME
)?
;
98 let content
= content
.unwrap_or_else(|| String
::from("[]"));
100 let digest
= openssl
::sha
::sha256(content
.as_bytes());
102 let key_list
: Vec
<KeyConfig
> = serde_json
::from_str(&content
)?
;
104 let mut map
= HashMap
::new();
106 for key_config
in key_list
{
107 match key_config
.fingerprint
{
108 Some(ref fingerprint
) => {
109 if map
.insert(fingerprint
.clone(), key_config
).is_some() {
110 bail
!("found duplicate fingerprint");
113 None
=> bail
!("missing fingerprint"),
120 pub fn save_keys(map
: HashMap
<Fingerprint
, EncryptionKeyInfo
>) -> Result
<(), Error
> {
122 let mut list
= Vec
::new();
124 for (_fp
, item
) in map
{
128 let raw
= serde_json
::to_string_pretty(&list
)?
;
130 let mode
= nix
::sys
::stat
::Mode
::from_bits_truncate(0o0600);
131 // set the correct owner/group/permissions while saving file
132 // owner(rw) = root, group(r)= root
133 let options
= CreateOptions
::new()
135 .owner(nix
::unistd
::ROOT
)
136 .group(nix
::unistd
::Gid
::from_raw(0));
138 replace_file(TAPE_KEYS_FILENAME
, raw
.as_bytes(), options
)?
;
143 pub fn save_key_configs(map
: HashMap
<Fingerprint
, KeyConfig
>) -> Result
<(), Error
> {
145 let mut list
= Vec
::new();
147 for (_fp
, item
) in map
{
151 let raw
= serde_json
::to_string_pretty(&list
)?
;
153 let backup_user
= crate::backup
::backup_user()?
;
154 let mode
= nix
::sys
::stat
::Mode
::from_bits_truncate(0o0640);
155 // set the correct owner/group/permissions while saving file
156 // owner(rw) = root, group(r)= backup
157 let options
= CreateOptions
::new()
159 .owner(nix
::unistd
::ROOT
)
160 .group(backup_user
.gid
);
162 replace_file(TAPE_KEY_CONFIG_FILENAME
, raw
.as_bytes(), options
)?
;
167 pub fn insert_key(key
: [u8;32], key_config
: KeyConfig
, force
: bool
) -> Result
<(), Error
> {
169 let _lock
= open_file_locked(
171 std
::time
::Duration
::new(10, 0),
175 let (mut key_map
, _
) = load_keys()?
;
176 let (mut config_map
, _
) = load_key_configs()?
;
178 let fingerprint
= match key_config
.fingerprint
.clone() {
179 Some(fingerprint
) => fingerprint
,
180 None
=> bail
!("missing encryption key fingerprint - internal error"),
184 if config_map
.get(&fingerprint
).is_some() {
185 bail
!("encryption key '{}' already exists.", fingerprint
);
189 let item
= EncryptionKeyInfo
::new(key
, fingerprint
.clone());
190 key_map
.insert(fingerprint
.clone(), item
);
193 config_map
.insert(fingerprint
.clone(), key_config
);
194 save_key_configs(config_map
)?
;
199 // shell completion helper
200 pub fn complete_key_fingerprint(_arg
: &str, _param
: &HashMap
<String
, String
>) -> Vec
<String
> {
201 let data
= match load_key_configs() {
202 Ok((data
, _digest
)) => data
,
203 Err(_
) => return Vec
::new(),
206 data
.keys().map(|fp
| crate::tools
::format
::as_fingerprint(fp
.bytes())).collect()