| 1 |
use anyhow::Context; |
| 2 |
|
| 3 |
use chrono::DateTime; |
| 4 |
use chrono::Utc; |
| 5 |
|
| 6 |
use sequoia_openpgp as openpgp; |
| 7 |
use openpgp::Result; |
| 8 |
use openpgp::cert::KeyBuilder; |
| 9 |
use openpgp::serialize::Serialize; |
| 10 |
use openpgp::types::KeyFlags; |
| 11 |
|
| 12 |
use crate::Sq; |
| 13 |
use crate::cli::key::subkey::add::Command; |
| 14 |
use crate::cli::types::EncryptPurpose; |
| 15 |
use crate::common; |
| 16 |
use crate::sq::TrustThreshold; |
| 17 |
|
| 18 |
/// Add a new Subkey for an existing primary key |
| 19 |
/// |
| 20 |
/// Creates a subkey with features (e.g. `KeyFlags`, `CipherSuite`) based on |
| 21 |
/// user input (or application-wide defaults if not specified). |
| 22 |
/// If no specific expiry is requested, the subkey never expires. |
| 23 |
pub fn dispatch(sq: Sq, command: Command) -> Result<()> |
| 24 |
{ |
| 25 |
let cert = |
| 26 |
sq.resolve_cert(&command.cert, TrustThreshold::Full)?.0; |
| 27 |
|
| 28 |
let valid_cert = cert.with_policy(sq.policy, sq.time)?; |
| 29 |
|
| 30 |
let validity = command |
| 31 |
.expiration |
| 32 |
.as_duration(DateTime::<Utc>::from(sq.time))?; |
| 33 |
|
| 34 |
let keyflags = KeyFlags::empty() |
| 35 |
.set_authentication_to(command.can_authenticate) |
| 36 |
.set_signing_to(command.can_sign) |
| 37 |
.set_storage_encryption_to(matches!( |
| 38 |
command.can_encrypt, |
| 39 |
Some(EncryptPurpose::Storage) | Some(EncryptPurpose::Universal) |
| 40 |
)) |
| 41 |
.set_transport_encryption_to(matches!( |
| 42 |
command.can_encrypt, |
| 43 |
Some(EncryptPurpose::Transport) | Some(EncryptPurpose::Universal) |
| 44 |
)); |
| 45 |
|
| 46 |
// If a password is needed to use the key, the user will be prompted. |
| 47 |
let (primary_key, password) = |
| 48 |
match sq.get_primary_key(&cert, None) { |
| 49 |
Ok(key) => { |
| 50 |
// Don't use a password, or prompt for one. |
| 51 |
if let Some(password_file) = command.new_password_file { |
| 52 |
let password = std::fs::read(&password_file) |
| 53 |
.with_context(|| { |
| 54 |
format!("Reading {}", password_file.display()) |
| 55 |
})?; |
| 56 |
(key, Some(password.into())) |
| 57 |
} else if command.without_password { |
| 58 |
(key, None) |
| 59 |
} else { |
| 60 |
(key, common::password::prompt_for_new_or_none( |
| 61 |
&sq, "subkey")?) |
| 62 |
} |
| 63 |
} |
| 64 |
Err(error) => { |
| 65 |
return Err(error) |
| 66 |
} |
| 67 |
}; |
| 68 |
|
| 69 |
let new_cert = KeyBuilder::new(keyflags) |
| 70 |
.set_creation_time(sq.time) |
| 71 |
.set_cipher_suite( |
| 72 |
sq.config.cipher_suite(&command.cipher_suite, |
| 73 |
command.cipher_suite_source)) |
| 74 |
.set_password(password) |
| 75 |
.subkey(valid_cert)? |
| 76 |
.set_key_validity_period(validity)? |
| 77 |
.set_primary_key_signer(primary_key) |
| 78 |
.attach_cert()?; |
| 79 |
|
| 80 |
if let Some(output) = command.output { |
| 81 |
let mut sink = output.for_secrets().create_safe(&sq)?; |
| 82 |
if false { |
| 83 |
new_cert.as_tsk().serialize(&mut sink)?; |
| 84 |
} else { |
| 85 |
new_cert.as_tsk().armored().serialize(&mut sink)?; |
| 86 |
} |
| 87 |
} else { |
| 88 |
sq.import_key(new_cert, &mut Default::default())?; |
| 89 |
} |
| 90 |
|
| 91 |
Ok(()) |
| 92 |
} |