1 use std
::convert
::TryFrom
;
2 use std
::path
::PathBuf
;
4 use anyhow
::{bail, format_err, Error}
;
8 use proxmox
::api
::cli
::{
9 format_and_print_result_full
, get_output_format
, CliCommand
, CliCommandMap
, ColumnConfig
,
12 use proxmox
::api
::router
::ReturnType
;
13 use proxmox
::sys
::linux
::tty
;
14 use proxmox
::tools
::fs
::{file_get_contents, replace_file, CreateOptions}
;
17 api2
::types
::{Kdf, KeyInfo, RsaPubKeyInfo, PASSWORD_HINT_SCHEMA}
,
18 backup
::{rsa_decrypt_key_config, KeyConfig}
,
20 tools
::paperkey
::{generate_paper_key, PaperkeyFormat}
,
23 pub const DEFAULT_ENCRYPTION_KEY_FILE_NAME
: &str = "encryption-key.json";
24 pub const DEFAULT_MASTER_PUBKEY_FILE_NAME
: &str = "master-public.pem";
26 pub fn find_default_master_pubkey() -> Result
<Option
<PathBuf
>, Error
> {
28 DEFAULT_MASTER_PUBKEY_FILE_NAME
,
29 "default master public key file",
33 pub fn place_default_master_pubkey() -> Result
<PathBuf
, Error
> {
34 super::place_xdg_file(
35 DEFAULT_MASTER_PUBKEY_FILE_NAME
,
36 "default master public key file",
40 pub fn find_default_encryption_key() -> Result
<Option
<PathBuf
>, Error
> {
42 DEFAULT_ENCRYPTION_KEY_FILE_NAME
,
43 "default encryption key file",
47 pub fn place_default_encryption_key() -> Result
<PathBuf
, Error
> {
48 super::place_xdg_file(
49 DEFAULT_ENCRYPTION_KEY_FILE_NAME
,
50 "default encryption key file",
55 pub fn read_optional_default_encryption_key() -> Result
<Option
<Vec
<u8>>, Error
> {
56 find_default_encryption_key()?
57 .map(file_get_contents
)
62 static mut TEST_DEFAULT_ENCRYPTION_KEY
: Result
<Option
<Vec
<u8>>, Error
> = Ok(None
);
65 pub fn read_optional_default_encryption_key() -> Result
<Option
<Vec
<u8>>, Error
> {
66 // not safe when multiple concurrent test cases end up here!
68 match &TEST_DEFAULT_ENCRYPTION_KEY
{
69 Ok(key
) => Ok(key
.clone()),
70 Err(_
) => bail
!("test error"),
76 // not safe when multiple concurrent test cases end up here!
77 pub unsafe fn set_test_encryption_key(value
: Result
<Option
<Vec
<u8>>, Error
>) {
78 TEST_DEFAULT_ENCRYPTION_KEY
= value
;
81 pub fn get_encryption_key_password() -> Result
<Vec
<u8>, Error
> {
82 // fixme: implement other input methods
84 use std
::env
::VarError
::*;
85 match std
::env
::var("PBS_ENCRYPTION_PASSWORD") {
86 Ok(p
) => return Ok(p
.as_bytes().to_vec()),
87 Err(NotUnicode(_
)) => bail
!("PBS_ENCRYPTION_PASSWORD contains bad characters"),
93 // If we're on a TTY, query the user for a password
94 if tty
::stdin_isatty() {
95 return Ok(tty
::read_password("Encryption Key Password: ")?
);
98 bail
!("no password input mechanism available");
110 "Output file. Without this the key will become the new default encryption key.",
114 schema
: PASSWORD_HINT_SCHEMA
,
120 /// Create a new encryption key.
121 fn create(kdf
: Option
<Kdf
>, path
: Option
<String
>, hint
: Option
<String
>) -> Result
<(), Error
> {
122 let path
= match path
{
123 Some(path
) => PathBuf
::from(path
),
125 let path
= place_default_encryption_key()?
;
126 println
!("creating default key at: {:?}", path
);
131 let kdf
= kdf
.unwrap_or_default();
133 let mut key
= [0u8; 32];
134 proxmox
::sys
::linux
::fill_with_random_data(&mut key
)?
;
139 bail
!("password hint not allowed for Kdf::None");
142 let key_config
= KeyConfig
::without_password(key
)?
;
144 key_config
.store(path
, false)?
;
146 Kdf
::Scrypt
| Kdf
::PBKDF2
=> {
147 // always read passphrase from tty
148 if !tty
::stdin_isatty() {
149 bail
!("unable to read passphrase - no tty");
152 let password
= tty
::read_and_verify_password("Encryption Key Password: ")?
;
154 let mut key_config
= KeyConfig
::with_key(&key
, &password
, kdf
)?
;
155 key_config
.hint
= hint
;
157 key_config
.store(&path
, false)?
;
168 description
: "(Private) master key to use.",
170 "encrypted-keyfile": {
171 description
: "RSA-encrypted keyfile to import.",
179 "Output file. Without this the key will become the new default encryption key.",
183 schema
: PASSWORD_HINT_SCHEMA
,
189 /// Import an encrypted backup of an encryption key using a (private) master key.
190 async
fn import_with_master_key(
191 master_keyfile
: String
,
192 encrypted_keyfile
: String
,
194 path
: Option
<String
>,
195 hint
: Option
<String
>,
196 ) -> Result
<(), Error
> {
197 let path
= match path
{
198 Some(path
) => PathBuf
::from(path
),
200 let path
= place_default_encryption_key()?
;
202 bail
!("Please remove default encryption key at {:?} before importing to default location (or choose a non-default one).", path
);
204 println
!("Importing key to default location at: {:?}", path
);
209 let encrypted_key
= file_get_contents(&encrypted_keyfile
)?
;
210 let master_key
= file_get_contents(&master_keyfile
)?
;
211 let password
= tty
::read_password("Master Key Password: ")?
;
213 let master_key
= openssl
::pkey
::PKey
::private_key_from_pem_passphrase(&master_key
, &password
)
214 .map_err(|err
| format_err
!("failed to read PEM-formatted private key - {}", err
))?
216 .map_err(|err
| format_err
!("not a valid private RSA key - {}", err
))?
;
218 let (key
, created
, _fingerprint
) =
219 rsa_decrypt_key_config(master_key
, &encrypted_key
, &get_encryption_key_password
)?
;
221 let kdf
= kdf
.unwrap_or_default();
225 bail
!("password hint not allowed for Kdf::None");
228 let mut key_config
= KeyConfig
::without_password(key
)?
;
229 key_config
.created
= created
; // keep original value
231 key_config
.store(path
, true)?
;
233 Kdf
::Scrypt
| Kdf
::PBKDF2
=> {
234 let password
= tty
::read_and_verify_password("New Password: ")?
;
236 let mut new_key_config
= KeyConfig
::with_key(&key
, &password
, kdf
)?
;
237 new_key_config
.created
= created
; // keep original value
238 new_key_config
.hint
= hint
;
240 new_key_config
.store(path
, true)?
;
255 description
: "Key file. Without this the default key's password will be changed.",
259 schema
: PASSWORD_HINT_SCHEMA
,
265 /// Change the encryption key's password.
266 fn change_passphrase(
268 path
: Option
<String
>,
269 hint
: Option
<String
>,
270 ) -> Result
<(), Error
> {
271 let path
= match path
{
272 Some(path
) => PathBuf
::from(path
),
274 let path
= find_default_encryption_key()?
.ok_or_else(|| {
275 format_err
!("no encryption file provided and no default file found")
277 println
!("updating default key at: {:?}", path
);
282 let kdf
= kdf
.unwrap_or_default();
284 if !tty
::stdin_isatty() {
285 bail
!("unable to change passphrase - no tty");
288 let key_config
= KeyConfig
::load(&path
)?
;
289 let (key
, created
, _fingerprint
) = key_config
.decrypt(&get_encryption_key_password
)?
;
294 bail
!("password hint not allowed for Kdf::None");
297 let mut key_config
= KeyConfig
::without_password(key
)?
;
298 key_config
.created
= created
; // keep original value
300 key_config
.store(&path
, true)?
;
302 Kdf
::Scrypt
| Kdf
::PBKDF2
=> {
303 let password
= tty
::read_and_verify_password("New Password: ")?
;
305 let mut new_key_config
= KeyConfig
::with_key(&key
, &password
, kdf
)?
;
306 new_key_config
.created
= created
; // keep original value
307 new_key_config
.hint
= hint
;
309 new_key_config
.store(&path
, true)?
;
320 description
: "Key file. Without this the default key's metadata will be shown.",
324 schema
: OUTPUT_FORMAT
,
330 /// Print the encryption key's metadata.
331 fn show_key(path
: Option
<String
>, param
: Value
) -> Result
<(), Error
> {
332 let path
= match path
{
333 Some(path
) => PathBuf
::from(path
),
334 None
=> find_default_encryption_key()?
335 .ok_or_else(|| format_err
!("no encryption file provided and no default file found"))?
,
338 let config
: KeyConfig
= serde_json
::from_slice(&file_get_contents(path
.clone())?
)?
;
340 let output_format
= get_output_format(¶m
);
342 let mut info
: KeyInfo
= (&config
).into();
343 info
.path
= Some(format
!("{:?}", path
));
345 let options
= proxmox
::api
::cli
::default_table_format_options()
346 .column(ColumnConfig
::new("path"))
347 .column(ColumnConfig
::new("kdf"))
348 .column(ColumnConfig
::new("created").renderer(tools
::format
::render_epoch
))
349 .column(ColumnConfig
::new("modified").renderer(tools
::format
::render_epoch
))
350 .column(ColumnConfig
::new("fingerprint"))
351 .column(ColumnConfig
::new("hint"));
353 let return_type
= ReturnType
::new(false, &KeyInfo
::API_SCHEMA
);
355 format_and_print_result_full(
356 &mut serde_json
::to_value(info
)?
,
369 description
: "Path to the PEM formatted RSA public key.",
374 /// Import an RSA public key used to put an encrypted version of the symmetric backup encryption
375 /// key onto the backup server along with each backup.
377 /// The imported key will be used as default master key for future invocations by the same local
379 fn import_master_pubkey(path
: String
) -> Result
<(), Error
> {
380 let pem_data
= file_get_contents(&path
)?
;
382 match openssl
::pkey
::PKey
::public_key_from_pem(&pem_data
) {
384 let info
= RsaPubKeyInfo
::try_from(key
.rsa()?
)?
;
385 println
!("Found following key at {:?}", path
);
386 println
!("Modulus: {}", info
.modulus
);
387 println
!("Exponent: {}", info
.exponent
);
388 println
!("Length: {}", info
.length
);
390 Err(err
) => bail
!("Unable to decode PEM data - {}", err
),
393 let target_path
= place_default_master_pubkey()?
;
395 replace_file(&target_path
, &pem_data
, CreateOptions
::new())?
;
397 println
!("Imported public master key to {:?}", target_path
);
403 /// Create an RSA public/private key pair used to put an encrypted version of the symmetric backup
404 /// encryption key onto the backup server along with each backup.
405 fn create_master_key() -> Result
<(), Error
> {
406 // we need a TTY to query the new password
407 if !tty
::stdin_isatty() {
408 bail
!("unable to create master key - no tty");
412 println
!("Generating {}-bit RSA key..", bits
);
413 let rsa
= openssl
::rsa
::Rsa
::generate(bits
)?
;
415 openssl
::rsa
::Rsa
::from_public_components(rsa
.n().to_owned()?
, rsa
.e().to_owned()?
)?
;
416 let info
= RsaPubKeyInfo
::try_from(public
)?
;
417 println
!("Modulus: {}", info
.modulus
);
418 println
!("Exponent: {}", info
.exponent
);
421 let pkey
= openssl
::pkey
::PKey
::from_rsa(rsa
)?
;
423 let password
= String
::from_utf8(tty
::read_and_verify_password("Master Key Password: ")?
)?
;
425 let pub_key
: Vec
<u8> = pkey
.public_key_to_pem()?
;
426 let filename_pub
= "master-public.pem";
427 println
!("Writing public master key to {}", filename_pub
);
428 replace_file(filename_pub
, pub_key
.as_slice(), CreateOptions
::new())?
;
430 let cipher
= openssl
::symm
::Cipher
::aes_256_cbc();
431 let priv_key
: Vec
<u8> =
432 pkey
.private_key_to_pem_pkcs8_passphrase(cipher
, password
.as_bytes())?
;
434 let filename_priv
= "master-private.pem";
435 println
!("Writing private master key to {}", filename_priv
);
436 replace_file(filename_priv
, priv_key
.as_slice(), CreateOptions
::new())?
;
445 description
: "Path to the PEM formatted RSA public key. Default location will be used if not specified.",
449 schema
: OUTPUT_FORMAT
,
455 /// List information about master key
456 fn show_master_pubkey(path
: Option
<String
>, param
: Value
) -> Result
<(), Error
> {
457 let path
= match path
{
458 Some(path
) => PathBuf
::from(path
),
459 None
=> find_default_master_pubkey()?
460 .ok_or_else(|| format_err
!("No path specified and no default master key available."))?
,
463 let path
= path
.canonicalize()?
;
465 let output_format
= get_output_format(¶m
);
467 let pem_data
= file_get_contents(path
.clone())?
;
468 let rsa
= openssl
::rsa
::Rsa
::public_key_from_pem(&pem_data
)?
;
470 let mut info
= RsaPubKeyInfo
::try_from(rsa
)?
;
471 info
.path
= Some(path
.display().to_string());
473 let options
= proxmox
::api
::cli
::default_table_format_options()
474 .column(ColumnConfig
::new("path"))
475 .column(ColumnConfig
::new("modulus"))
476 .column(ColumnConfig
::new("exponent"))
477 .column(ColumnConfig
::new("length"));
479 let return_type
= ReturnType
::new(false, &RsaPubKeyInfo
::API_SCHEMA
);
481 format_and_print_result_full(
482 &mut serde_json
::to_value(info
)?
,
495 description
: "Key file. Without this the default key's will be used.",
499 description
: "Include the specified subject as titel text.",
503 type: PaperkeyFormat
,
509 /// Generate a printable, human readable text file containing the encryption key.
511 /// This also includes a scanable QR code for fast key restore.
513 path
: Option
<String
>,
514 subject
: Option
<String
>,
515 output_format
: Option
<PaperkeyFormat
>,
516 ) -> Result
<(), Error
> {
517 let path
= match path
{
518 Some(path
) => PathBuf
::from(path
),
519 None
=> find_default_encryption_key()?
520 .ok_or_else(|| format_err
!("no encryption file provided and no default file found"))?
,
523 let data
= file_get_contents(&path
)?
;
524 let data
= String
::from_utf8(data
)?
;
526 generate_paper_key(std
::io
::stdout(), &data
, subject
, output_format
)
529 pub fn cli() -> CliCommandMap
{
530 let key_create_cmd_def
= CliCommand
::new(&API_METHOD_CREATE
)
531 .arg_param(&["path"])
532 .completion_cb("path", tools
::complete_file_name
);
534 let key_import_with_master_key_cmd_def
= CliCommand
::new(&API_METHOD_IMPORT_WITH_MASTER_KEY
)
535 .arg_param(&["master-keyfile"])
536 .completion_cb("master-keyfile", tools
::complete_file_name
)
537 .arg_param(&["encrypted-keyfile"])
538 .completion_cb("encrypted-keyfile", tools
::complete_file_name
)
539 .arg_param(&["path"])
540 .completion_cb("path", tools
::complete_file_name
);
542 let key_change_passphrase_cmd_def
= CliCommand
::new(&API_METHOD_CHANGE_PASSPHRASE
)
543 .arg_param(&["path"])
544 .completion_cb("path", tools
::complete_file_name
);
546 let key_create_master_key_cmd_def
= CliCommand
::new(&API_METHOD_CREATE_MASTER_KEY
);
547 let key_import_master_pubkey_cmd_def
= CliCommand
::new(&API_METHOD_IMPORT_MASTER_PUBKEY
)
548 .arg_param(&["path"])
549 .completion_cb("path", tools
::complete_file_name
);
550 let key_show_master_pubkey_cmd_def
= CliCommand
::new(&API_METHOD_SHOW_MASTER_PUBKEY
)
551 .arg_param(&["path"])
552 .completion_cb("path", tools
::complete_file_name
);
554 let key_show_cmd_def
= CliCommand
::new(&API_METHOD_SHOW_KEY
)
555 .arg_param(&["path"])
556 .completion_cb("path", tools
::complete_file_name
);
558 let paper_key_cmd_def
= CliCommand
::new(&API_METHOD_PAPER_KEY
)
559 .arg_param(&["path"])
560 .completion_cb("path", tools
::complete_file_name
);
563 .insert("create", key_create_cmd_def
)
564 .insert("import-with-master-key", key_import_with_master_key_cmd_def
)
565 .insert("create-master-key", key_create_master_key_cmd_def
)
566 .insert("import-master-pubkey", key_import_master_pubkey_cmd_def
)
567 .insert("change-passphrase", key_change_passphrase_cmd_def
)
568 .insert("show", key_show_cmd_def
)
569 .insert("show-master-pubkey", key_show_master_pubkey_cmd_def
)
570 .insert("paperkey", paper_key_cmd_def
)