1 //! Store Tape encryptions keys
3 //! This module can store 256bit encryption keys for tape backups,
4 //! indexed by key fingerprint.
6 //! We store the plain key (unencrypted), as well as a encrypted
7 //! version protected by password (see struct `KeyConfig`)
9 //! Tape backups store the password protected version on tape, so that
10 //! it is possible to restore the key from tape if you know the
13 use std
::collections
::HashMap
;
15 use anyhow
::{bail, Error}
;
16 use serde
::{Deserialize, Serialize}
;
18 use proxmox
::tools
::fs
::{
19 file_read_optional_string
,
33 use serde
::{self, Deserialize, Serializer, Deserializer}
;
38 ) -> Result
<S
::Ok
, S
::Error
>
42 let s
= proxmox
::tools
::digest_to_hex(csum
);
43 serializer
.serialize_str(&s
)
46 pub fn deserialize
<'de
, D
>(
48 ) -> Result
<[u8; 32], D
::Error
>
52 let s
= String
::deserialize(deserializer
)?
;
53 proxmox
::tools
::hex_to_digest(&s
).map_err(serde
::de
::Error
::custom
)
57 /// Store Hardware Encryption keys (plain, unprotected keys)
58 #[derive(Deserialize, Serialize)]
59 pub struct EncryptionKeyInfo
{
60 /// Key fingerprint (we verify the fingerprint on load)
61 pub fingerprint
: Fingerprint
,
62 /// The plain encryption key
63 #[serde(with = "hex_key")]
67 impl EncryptionKeyInfo
{
68 pub fn new(key
: [u8; 32], fingerprint
: Fingerprint
) -> Self {
69 Self { fingerprint, key }
73 pub const TAPE_KEYS_FILENAME
: &str = "/etc/proxmox-backup/tape-encryption-keys.json";
74 pub const TAPE_KEY_CONFIG_FILENAME
: &str = "/etc/proxmox-backup/tape-encryption-key-config.json";
75 pub const TAPE_KEYS_LOCKFILE
: &str = "/etc/proxmox-backup/.tape-encryption-keys.lck";
77 /// Load tape encryption keys (plain, unprotected keys)
78 pub fn load_keys() -> Result
<(HashMap
<Fingerprint
, EncryptionKeyInfo
>, [u8;32]), Error
> {
80 let content
= file_read_optional_string(TAPE_KEYS_FILENAME
)?
;
81 let content
= content
.unwrap_or_else(|| String
::from("[]"));
83 let digest
= openssl
::sha
::sha256(content
.as_bytes());
85 let key_list
: Vec
<EncryptionKeyInfo
> = serde_json
::from_str(&content
)?
;
87 let mut map
= HashMap
::new();
89 for item
in key_list
{
90 let key_config
= KeyConfig
::without_password(item
.key
)?
; // to compute fingerprint
91 let expected_fingerprint
= key_config
.fingerprint
.unwrap();
92 if item
.fingerprint
!= expected_fingerprint
{
94 "inconsistent fingerprint ({} != {})",
100 if map
.insert(item
.fingerprint
.clone(), item
).is_some() {
101 bail
!("found duplicate fingerprint");
108 /// Load tape encryption key configurations (password protected keys)
109 pub fn load_key_configs() -> Result
<(HashMap
<Fingerprint
, KeyConfig
>, [u8;32]), Error
> {
111 let content
= file_read_optional_string(TAPE_KEY_CONFIG_FILENAME
)?
;
112 let content
= content
.unwrap_or_else(|| String
::from("[]"));
114 let digest
= openssl
::sha
::sha256(content
.as_bytes());
116 let key_list
: Vec
<KeyConfig
> = serde_json
::from_str(&content
)?
;
118 let mut map
= HashMap
::new();
120 for key_config
in key_list
{
121 match key_config
.fingerprint
{
122 Some(ref fingerprint
) => {
123 if map
.insert(fingerprint
.clone(), key_config
).is_some() {
124 bail
!("found duplicate fingerprint");
127 None
=> bail
!("missing fingerprint"),
134 /// Store tape encryption keys (plain, unprotected keys)
136 /// The file is only accessible by user root (mode 0600).
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 /// Store tape encryption key configurations (password protected keys)
161 pub fn save_key_configs(map
: HashMap
<Fingerprint
, KeyConfig
>) -> Result
<(), Error
> {
163 let mut list
= Vec
::new();
165 for (_fp
, item
) in map
{
169 let raw
= serde_json
::to_string_pretty(&list
)?
;
171 let backup_user
= crate::backup
::backup_user()?
;
172 let mode
= nix
::sys
::stat
::Mode
::from_bits_truncate(0o0640);
173 // set the correct owner/group/permissions while saving file
174 // owner(rw) = root, group(r)= backup
175 let options
= CreateOptions
::new()
177 .owner(nix
::unistd
::ROOT
)
178 .group(backup_user
.gid
);
180 replace_file(TAPE_KEY_CONFIG_FILENAME
, raw
.as_bytes(), options
)?
;
187 /// Get the lock, load both files, insert the new key, store files.
188 pub fn insert_key(key
: [u8;32], key_config
: KeyConfig
, force
: bool
) -> Result
<(), Error
> {
190 let _lock
= open_file_locked(
192 std
::time
::Duration
::new(10, 0),
196 let (mut key_map
, _
) = load_keys()?
;
197 let (mut config_map
, _
) = load_key_configs()?
;
199 let fingerprint
= match key_config
.fingerprint
.clone() {
200 Some(fingerprint
) => fingerprint
,
201 None
=> bail
!("missing encryption key fingerprint - internal error"),
205 if config_map
.get(&fingerprint
).is_some() {
206 bail
!("encryption key '{}' already exists.", fingerprint
);
210 let item
= EncryptionKeyInfo
::new(key
, fingerprint
.clone());
211 key_map
.insert(fingerprint
.clone(), item
);
214 config_map
.insert(fingerprint
.clone(), key_config
);
215 save_key_configs(config_map
)?
;
220 // shell completion helper
221 /// Complete tape encryption key fingerprints
222 pub fn complete_key_fingerprint(_arg
: &str, _param
: &HashMap
<String
, String
>) -> Vec
<String
> {
223 let data
= match load_key_configs() {
224 Ok((data
, _digest
)) => data
,
225 Err(_
) => return Vec
::new(),
228 data
.keys().map(|fp
| crate::tools
::format
::as_fingerprint(fp
.bytes())).collect()