1 use std
::path
::PathBuf
;
3 use std
::process
::{Stdio, Command}
;
5 use anyhow
::{bail, format_err, Error}
;
6 use serde
::{Deserialize, Serialize}
;
10 use proxmox
::api
::cli
::{
14 format_and_print_result_full
,
18 use proxmox
::sys
::linux
::tty
;
19 use proxmox
::tools
::fs
::{file_get_contents, replace_file, CreateOptions}
;
21 use proxmox_backup
::backup
::{
22 encrypt_key_with_passphrase
,
30 use proxmox_backup
::tools
;
33 #[derive(Debug, Serialize, Deserialize)]
34 #[serde(rename_all = "lowercase")]
35 /// Paperkey output format
36 pub enum PaperkeyFormat
{
37 /// Format as Utf8 text. Includes QR codes as ascii-art.
39 /// Format as Html. Includes QR codes as png images.
43 pub const DEFAULT_ENCRYPTION_KEY_FILE_NAME
: &str = "encryption-key.json";
44 pub const MASTER_PUBKEY_FILE_NAME
: &str = "master-public.pem";
46 pub fn find_master_pubkey() -> Result
<Option
<PathBuf
>, Error
> {
47 super::find_xdg_file(MASTER_PUBKEY_FILE_NAME
, "main public key file")
50 pub fn place_master_pubkey() -> Result
<PathBuf
, Error
> {
51 super::place_xdg_file(MASTER_PUBKEY_FILE_NAME
, "main public key file")
54 pub fn find_default_encryption_key() -> Result
<Option
<PathBuf
>, Error
> {
55 super::find_xdg_file(DEFAULT_ENCRYPTION_KEY_FILE_NAME
, "default encryption key file")
58 pub fn place_default_encryption_key() -> Result
<PathBuf
, Error
> {
59 super::place_xdg_file(DEFAULT_ENCRYPTION_KEY_FILE_NAME
, "default encryption key file")
62 pub fn read_optional_default_encryption_key() -> Result
<Option
<Vec
<u8>>, Error
> {
63 find_default_encryption_key()?
64 .map(file_get_contents
)
68 pub fn get_encryption_key_password() -> Result
<Vec
<u8>, Error
> {
69 // fixme: implement other input methods
71 use std
::env
::VarError
::*;
72 match std
::env
::var("PBS_ENCRYPTION_PASSWORD") {
73 Ok(p
) => return Ok(p
.as_bytes().to_vec()),
74 Err(NotUnicode(_
)) => bail
!("PBS_ENCRYPTION_PASSWORD contains bad characters"),
80 // If we're on a TTY, query the user for a password
81 if tty
::stdin_isatty() {
82 return Ok(tty
::read_password("Encryption Key Password: ")?
);
85 bail
!("no password input mechanism available");
97 "Output file. Without this the key will become the new default encryption key.",
103 /// Create a new encryption key.
104 fn create(kdf
: Option
<Kdf
>, path
: Option
<String
>) -> Result
<(), Error
> {
105 let path
= match path
{
106 Some(path
) => PathBuf
::from(path
),
108 let path
= place_default_encryption_key()?
;
109 println
!("creating default key at: {:?}", path
);
114 let kdf
= kdf
.unwrap_or_default();
116 let mut key_array
= [0u8; 32];
117 proxmox
::sys
::linux
::fill_with_random_data(&mut key_array
)?
;
118 let crypt_config
= CryptConfig
::new(key_array
.clone())?
;
119 let key
= key_array
.to_vec();
123 let created
= proxmox
::tools
::time
::epoch_i64();
133 fingerprint
: Some(crypt_config
.fingerprint()),
137 Kdf
::Scrypt
| Kdf
::PBKDF2
=> {
138 // always read passphrase from tty
139 if !tty
::stdin_isatty() {
140 bail
!("unable to read passphrase - no tty");
143 let password
= tty
::read_and_verify_password("Encryption Key Password: ")?
;
145 let mut key_config
= encrypt_key_with_passphrase(&key
, &password
, kdf
)?
;
146 key_config
.fingerprint
= Some(crypt_config
.fingerprint());
148 store_key_config(&path
, false, key_config
)?
;
163 description
: "Key file. Without this the default key's password will be changed.",
169 /// Change the encryption key's password.
170 fn change_passphrase(kdf
: Option
<Kdf
>, path
: Option
<String
>) -> Result
<(), Error
> {
171 let path
= match path
{
172 Some(path
) => PathBuf
::from(path
),
174 let path
= find_default_encryption_key()?
176 format_err
!("no encryption file provided and no default file found")
178 println
!("updating default key at: {:?}", path
);
183 let kdf
= kdf
.unwrap_or_default();
185 if !tty
::stdin_isatty() {
186 bail
!("unable to change passphrase - no tty");
189 let (key
, created
, fingerprint
) = load_and_decrypt_key(&path
, &get_encryption_key_password
)?
;
193 let modified
= proxmox
::tools
::time
::epoch_i64();
200 created
, // keep original value
203 fingerprint
: Some(fingerprint
),
207 Kdf
::Scrypt
| Kdf
::PBKDF2
=> {
208 let password
= tty
::read_and_verify_password("New Password: ")?
;
210 let mut new_key_config
= encrypt_key_with_passphrase(&key
, &password
, kdf
)?
;
211 new_key_config
.created
= created
; // keep original value
212 new_key_config
.fingerprint
= Some(fingerprint
);
214 store_key_config(&path
, true, new_key_config
)?
;
228 #[derive(Deserialize, Serialize)]
229 /// Encryption Key Information
234 /// Key creation time
236 /// Key modification time
239 pub fingerprint
: Option
<String
>,
246 description
: "Key file. Without this the default key's metadata will be shown.",
250 schema
: OUTPUT_FORMAT
,
256 /// Print the encryption key's metadata.
258 path
: Option
<String
>,
260 ) -> Result
<(), Error
> {
261 let path
= match path
{
262 Some(path
) => PathBuf
::from(path
),
264 let path
= find_default_encryption_key()?
266 format_err
!("no encryption file provided and no default file found")
273 let config
: KeyConfig
= serde_json
::from_slice(&file_get_contents(path
.clone())?
)?
;
275 let output_format
= get_output_format(¶m
);
278 path
: format
!("{:?}", path
),
279 kdf
: match config
.kdf
{
280 Some(KeyDerivationConfig
::PBKDF2 { .. }
) => Kdf
::PBKDF2
,
281 Some(KeyDerivationConfig
::Scrypt { .. }
) => Kdf
::Scrypt
,
284 created
: config
.created
,
285 modified
: config
.modified
,
286 fingerprint
: match config
.fingerprint
{
287 Some(ref fp
) => Some(format
!("{}", fp
)),
292 let options
= proxmox
::api
::cli
::default_table_format_options()
293 .column(ColumnConfig
::new("path"))
294 .column(ColumnConfig
::new("kdf"))
295 .column(ColumnConfig
::new("created").renderer(tools
::format
::render_epoch
))
296 .column(ColumnConfig
::new("modified").renderer(tools
::format
::render_epoch
))
297 .column(ColumnConfig
::new("fingerprint"));
299 let schema
= &KeyInfo
::API_SCHEMA
;
301 format_and_print_result_full(&mut serde_json
::to_value(info
)?
, schema
, &output_format
, &options
);
310 description
: "Path to the PEM formatted RSA public key.",
315 /// Import an RSA public key used to put an encrypted version of the symmetric backup encryption
316 /// key onto the backup server along with each backup.
317 fn import_master_pubkey(path
: String
) -> Result
<(), Error
> {
318 let pem_data
= file_get_contents(&path
)?
;
320 if let Err(err
) = openssl
::pkey
::PKey
::public_key_from_pem(&pem_data
) {
321 bail
!("Unable to decode PEM data - {}", err
);
324 let target_path
= place_master_pubkey()?
;
326 replace_file(&target_path
, &pem_data
, CreateOptions
::new())?
;
328 println
!("Imported public master key to {:?}", target_path
);
334 /// Create an RSA public/private key pair used to put an encrypted version of the symmetric backup
335 /// encryption key onto the backup server along with each backup.
336 fn create_master_key() -> Result
<(), Error
> {
337 // we need a TTY to query the new password
338 if !tty
::stdin_isatty() {
339 bail
!("unable to create master key - no tty");
342 let rsa
= openssl
::rsa
::Rsa
::generate(4096)?
;
343 let pkey
= openssl
::pkey
::PKey
::from_rsa(rsa
)?
;
345 let password
= String
::from_utf8(tty
::read_and_verify_password("Master Key Password: ")?
)?
;
347 let pub_key
: Vec
<u8> = pkey
.public_key_to_pem()?
;
348 let filename_pub
= "master-public.pem";
349 println
!("Writing public master key to {}", filename_pub
);
350 replace_file(filename_pub
, pub_key
.as_slice(), CreateOptions
::new())?
;
352 let cipher
= openssl
::symm
::Cipher
::aes_256_cbc();
353 let priv_key
: Vec
<u8> = pkey
.private_key_to_pem_pkcs8_passphrase(cipher
, password
.as_bytes())?
;
355 let filename_priv
= "master-private.pem";
356 println
!("Writing private master key to {}", filename_priv
);
357 replace_file(filename_priv
, priv_key
.as_slice(), CreateOptions
::new())?
;
366 description
: "Key file. Without this the default key's will be used.",
370 description
: "Include the specified subject as titel text.",
374 type: PaperkeyFormat
,
375 description
: "Output format. Text or Html.",
381 /// Generate a printable, human readable text file containing the encryption key.
383 /// This also includes a scanable QR code for fast key restore.
385 path
: Option
<String
>,
386 subject
: Option
<String
>,
387 output_format
: Option
<PaperkeyFormat
>,
388 ) -> Result
<(), Error
> {
389 let path
= match path
{
390 Some(path
) => PathBuf
::from(path
),
392 let path
= find_default_encryption_key()?
394 format_err
!("no encryption file provided and no default file found")
400 let data
= file_get_contents(&path
)?
;
401 let data
= std
::str::from_utf8(&data
)?
;
403 let format
= output_format
.unwrap_or(PaperkeyFormat
::Html
);
406 PaperkeyFormat
::Html
=> paperkey_html(data
, subject
),
407 PaperkeyFormat
::Text
=> paperkey_text(data
, subject
),
411 pub fn cli() -> CliCommandMap
{
412 let key_create_cmd_def
= CliCommand
::new(&API_METHOD_CREATE
)
413 .arg_param(&["path"])
414 .completion_cb("path", tools
::complete_file_name
);
416 let key_change_passphrase_cmd_def
= CliCommand
::new(&API_METHOD_CHANGE_PASSPHRASE
)
417 .arg_param(&["path"])
418 .completion_cb("path", tools
::complete_file_name
);
420 let key_create_master_key_cmd_def
= CliCommand
::new(&API_METHOD_CREATE_MASTER_KEY
);
421 let key_import_master_pubkey_cmd_def
= CliCommand
::new(&API_METHOD_IMPORT_MASTER_PUBKEY
)
422 .arg_param(&["path"])
423 .completion_cb("path", tools
::complete_file_name
);
425 let key_show_cmd_def
= CliCommand
::new(&API_METHOD_SHOW_KEY
)
426 .arg_param(&["path"])
427 .completion_cb("path", tools
::complete_file_name
);
429 let paper_key_cmd_def
= CliCommand
::new(&API_METHOD_PAPER_KEY
)
430 .arg_param(&["path"])
431 .completion_cb("path", tools
::complete_file_name
);
434 .insert("create", key_create_cmd_def
)
435 .insert("create-master-key", key_create_master_key_cmd_def
)
436 .insert("import-master-pubkey", key_import_master_pubkey_cmd_def
)
437 .insert("change-passphrase", key_change_passphrase_cmd_def
)
438 .insert("show", key_show_cmd_def
)
439 .insert("paperkey", paper_key_cmd_def
)
442 fn paperkey_html(data
: &str, subject
: Option
<String
>) -> Result
<(), Error
> {
444 let img_size_pt
= 500;
446 println
!("<!DOCTYPE html>");
447 println
!("<html lang=\"en\">");
449 println
!("<meta charset=\"utf-8\">");
450 println
!("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">");
451 println
!("<title>Proxmox Backup Paperkey</title>");
452 println
!("<style type=\"text/css\">");
455 println
!(" font-size: 12pt;");
456 println
!(" font-family: monospace;");
457 println
!(" white-space: pre-wrap;");
458 println
!(" line-break: anywhere;");
461 println
!("</style>");
467 if let Some(subject
) = subject
{
468 println
!("<p>Subject: {}</p>", subject
);
471 if data
.starts_with("-----BEGIN ENCRYPTED PRIVATE KEY-----\n") {
472 let lines
: Vec
<String
> = data
.lines()
473 .map(|s
| s
.trim_end())
474 .filter(|s
| !s
.is_empty())
478 if !lines
[lines
.len()-1].starts_with("-----END ENCRYPTED PRIVATE KEY-----") {
479 bail
!("unexpected key format");
482 if lines
.len() < 20 {
483 bail
!("unexpected key format");
486 const BLOCK_SIZE
: usize = 20;
487 let blocks
= (lines
.len() + BLOCK_SIZE
-1)/BLOCK_SIZE
;
490 let start
= i
*BLOCK_SIZE
;
491 let mut end
= start
+ BLOCK_SIZE
;
492 if end
> lines
.len() {
495 let data
= &lines
[start
..end
];
497 println
!("<div style=\"page-break-inside: avoid;page-break-after: always\">");
500 for l
in start
..end
{
501 println
!("{:02}: {}", l
, lines
[l
]);
506 let data
= data
.join("\n");
507 let qr_code
= generate_qr_code("svg", data
.as_bytes())?
;
508 let qr_code
= base64
::encode_config(&qr_code
, base64
::STANDARD_NO_PAD
);
510 println
!("<center>");
512 println
!("width=\"{}pt\" height=\"{}pt\"", img_size_pt
, img_size_pt
);
513 println
!("src=\"data:image/svg+xml;base64,{}\"/>", qr_code
);
514 println
!("</center>");
523 let key_config
: KeyConfig
= serde_json
::from_str(&data
)?
;
524 let key_text
= serde_json
::to_string_pretty(&key_config
)?
;
526 println
!("<div style=\"page-break-inside: avoid\">");
530 println
!("-----BEGIN PROXMOX BACKUP KEY-----");
532 for line
in key_text
.lines() {
533 println
!("{}", line
);
536 println
!("-----END PROXMOX BACKUP KEY-----");
540 let qr_code
= generate_qr_code("svg", key_text
.as_bytes())?
;
541 let qr_code
= base64
::encode_config(&qr_code
, base64
::STANDARD_NO_PAD
);
543 println
!("<center>");
545 println
!("width=\"{}pt\" height=\"{}pt\"", img_size_pt
, img_size_pt
);
546 println
!("src=\"data:image/svg+xml;base64,{}\"/>", qr_code
);
547 println
!("</center>");
557 fn paperkey_text(data
: &str, subject
: Option
<String
>) -> Result
<(), Error
> {
559 if let Some(subject
) = subject
{
560 println
!("Subject: {}\n", subject
);
563 if data
.starts_with("-----BEGIN ENCRYPTED PRIVATE KEY-----\n") {
564 let lines
: Vec
<String
> = data
.lines()
565 .map(|s
| s
.trim_end())
566 .filter(|s
| !s
.is_empty())
570 if !lines
[lines
.len()-1].starts_with("-----END ENCRYPTED PRIVATE KEY-----") {
571 bail
!("unexpected key format");
574 if lines
.len() < 20 {
575 bail
!("unexpected key format");
578 const BLOCK_SIZE
: usize = 5;
579 let blocks
= (lines
.len() + BLOCK_SIZE
-1)/BLOCK_SIZE
;
582 let start
= i
*BLOCK_SIZE
;
583 let mut end
= start
+ BLOCK_SIZE
;
584 if end
> lines
.len() {
587 let data
= &lines
[start
..end
];
589 for l
in start
..end
{
590 println
!("{:-2}: {}", l
, lines
[l
]);
592 let data
= data
.join("\n");
593 let qr_code
= generate_qr_code("utf8i", data
.as_bytes())?
;
594 let qr_code
= String
::from_utf8(qr_code
)
595 .map_err(|_
| format_err
!("Failed to read qr code (got non-utf8 data)"))?
;
596 println
!("{}", qr_code
);
597 println
!("{}", char::from(12u8)); // page break
603 let key_config
: KeyConfig
= serde_json
::from_str(&data
)?
;
604 let key_text
= serde_json
::to_string_pretty(&key_config
)?
;
606 println
!("-----BEGIN PROXMOX BACKUP KEY-----");
607 println
!("{}", key_text
);
608 println
!("-----END PROXMOX BACKUP KEY-----");
610 let qr_code
= generate_qr_code("utf8i", key_text
.as_bytes())?
;
611 let qr_code
= String
::from_utf8(qr_code
)
612 .map_err(|_
| format_err
!("Failed to read qr code (got non-utf8 data)"))?
;
614 println
!("{}", qr_code
);
619 fn generate_qr_code(output_type
: &str, data
: &[u8]) -> Result
<Vec
<u8>, Error
> {
621 let mut child
= Command
::new("qrencode")
622 .args(&["-t", output_type
, "-m0", "-s1", "-lm", "--output", "-"])
623 .stdin(Stdio
::piped())
624 .stdout(Stdio
::piped())
628 let stdin
= child
.stdin
.as_mut()
629 .ok_or_else(|| format_err
!("Failed to open stdin"))?
;
630 stdin
.write_all(data
)
631 .map_err(|_
| format_err
!("Failed to write to stdin"))?
;
634 let output
= child
.wait_with_output()
635 .map_err(|_
| format_err
!("Failed to read stdout"))?
;
637 let output
= crate::tools
::command_output(output
, None
)?
;