feat: resorder envelops

This commit is contained in:
2023-10-01 12:15:19 +08:00
parent 0f7f60e3ff
commit 79109c61eb
5 changed files with 27 additions and 13 deletions

View File

@@ -2,7 +2,9 @@ use openpgp_card_pcsc::PcscBackend;
use rust_util::{opt_result, opt_value_result, simple_error, warning, XResult};
pub fn get_card() -> XResult<PcscBackend> {
let card_list = opt_result!(PcscBackend::cards(None), "Read OpenPGP card list failed: {}");
let card_list = opt_result!(
PcscBackend::cards(None), "Read OpenPGP card list failed: {}"
);
if card_list.is_empty() {
return simple_error!("Cannot find any card");
}

View File

@@ -96,7 +96,9 @@ pub fn decrypt_single(path: &PathBuf, pin: &Option<String>, slot: &Option<String
let mut file_out = File::create(path_out)?;
let start = Instant::now();
util_msg::print_lastline(&format!("Decrypting file: {}{} ...", path_display, iff!(meta.compress, " [compressed]", "")));
util_msg::print_lastline(
&format!("Decrypting file: {}{} ...", path_display, iff!(meta.compress, " [compressed]", ""))
);
let _ = decrypt_file(&mut file_in, &mut file_out, &key, &nonce, meta.compress)?;
util_msg::clear_lastline();
let encrypt_duration = start.elapsed();
@@ -155,7 +157,9 @@ fn try_decrypt_key(envelop: &TinyEncryptEnvelop, pin: &Option<String>, slot: &Op
match envelop.r#type {
TinyEncryptEnvelopType::Pgp => try_decrypt_key_pgp(envelop, pin),
TinyEncryptEnvelopType::Ecdh => try_decrypt_key_ecdh(envelop, pin, slot),
unknown_type => return simple_error!("Unknown or not supported type: {}", unknown_type.get_name())
unknown_type => {
return simple_error!("Unknown or not supported type: {}", unknown_type.get_name());
}
}
}
@@ -166,7 +170,7 @@ fn try_decrypt_key_ecdh(envelop: &TinyEncryptEnvelop, pin: &Option<String>, slot
}
let e_pub_key = &wrap_key.header.e_pub_key;
let e_pub_key_bytes = opt_result!(util::decode_base64_url_no_pad(e_pub_key), "Invalid envelop: {}");
let (_, subject_public_key_info) = opt_result!( SubjectPublicKeyInfo::from_der(&e_pub_key_bytes), "Invalid envelop: {}");
let (_, subject_public_key_info) = opt_result!(SubjectPublicKeyInfo::from_der(&e_pub_key_bytes), "Invalid envelop: {}");
let slot = read_slot(slot)?;
let pin = read_pin(pin);

View File

@@ -51,7 +51,9 @@ pub fn encrypt(cmd_encrypt: CmdEncrypt) -> XResult<()> {
let envelops = config.find_envelops(&cmd_encrypt.profile)?;
if envelops.is_empty() { return simple_error!("Cannot find any valid envelops"); }
debugging!("Found envelops: {:?}", envelops);
let envelop_tkids: Vec<_> = envelops.iter().map(|e| format!("{}:{}", e.r#type.get_name(), e.kid)).collect();
let envelop_tkids: Vec<_> = envelops.iter()
.map(|e| format!("{}:{}", e.r#type.get_name(), e.kid))
.collect();
information!("Matched {} envelop(s): \n- {}", envelops.len(), envelop_tkids.join("\n- "));
debugging!("Cmd encrypt: {:?}", cmd_encrypt);

View File

@@ -22,7 +22,9 @@ pub struct CmdInfo {
pub fn info(cmd_info: CmdInfo) -> XResult<()> {
let path_display = format!("{}", cmd_info.path.display());
let mut file_in = opt_result!(File::open(&cmd_info.path), "Open file: {} failed: {}", &path_display);
let meta = opt_result!(file::read_tiny_encrypt_meta_and_normalize(&mut file_in), "Read file: {}, failed: {}", &path_display);
let meta = opt_result!(
file::read_tiny_encrypt_meta_and_normalize(&mut file_in), "Read file: {}, failed: {}", &path_display
);
if cmd_info.raw_meta {
success!("Meta data:\n{}", serde_json::to_string_pretty(&meta).expect("SHOULD NOT HAPPEN"));

View File

@@ -49,8 +49,10 @@ pub struct TinyEncryptConfigEnvelop {
impl TinyEncryptConfig {
pub fn load(file: &str) -> XResult<Self> {
let resolved_file = resolve_file_path(file);
let config_contents = opt_result!(fs::read_to_string(&resolved_file), "Read file: {}, failed: {}", file);
// TODO replace with Human JSON
let config_contents = opt_result!(
fs::read_to_string(&resolved_file), "Read file: {}, failed: {}", file
);
// TODO Replace with Human JSON
Ok(opt_result!(serde_json::from_str(&config_contents), "Parse file: {}, failed: {}", file))
}
@@ -72,15 +74,17 @@ impl TinyEncryptConfig {
});
}
}
let mut envelops: Vec<_> = matched_envelops_map.values().map(|envelop| *envelop).collect();
let mut envelops: Vec<_> = matched_envelops_map.values()
.map(|envelop| *envelop)
.collect();
if envelops.is_empty() {
return simple_error!("Profile: {} has no valid envelopes found", profile);
}
envelops.sort_by(|e1, e2| {
if e1.r#type < e2.r#type { return Ordering::Less; }
if e1.r#type > e2.r#type { return Ordering::Greater; }
if e1.kid < e2.kid { return Ordering::Less; }
if e1.kid > e2.kid { return Ordering::Greater; }
if e1.r#type < e2.r#type { return Ordering::Greater; }
if e1.r#type > e2.r#type { return Ordering::Less; }
if e1.kid < e2.kid { return Ordering::Greater; }
if e1.kid > e2.kid { return Ordering::Less; }
Ordering::Equal
});
Ok(envelops)