2023-10-19 22:06:34 +00:00
|
|
|
use std::{
|
|
|
|
collections::{HashMap, VecDeque},
|
|
|
|
io::{Read, Write},
|
|
|
|
path::Path,
|
|
|
|
str::FromStr,
|
|
|
|
};
|
|
|
|
|
|
|
|
use keyfork_derive_openpgp::derive_util::{
|
|
|
|
request::{DerivationAlgorithm, DerivationRequest},
|
|
|
|
DerivationPath,
|
|
|
|
};
|
|
|
|
use openpgp::{
|
|
|
|
armor::{Kind, Writer},
|
|
|
|
cert::{Cert, CertParser, ValidCert},
|
|
|
|
packet::{Packet, Tag, UserID, PKESK, SEIP},
|
2023-11-02 06:01:34 +00:00
|
|
|
parse::{
|
|
|
|
stream::{DecryptionHelper, DecryptorBuilder, VerificationHelper},
|
|
|
|
Parse,
|
|
|
|
},
|
2023-10-19 22:06:34 +00:00
|
|
|
policy::{NullPolicy, Policy, StandardPolicy},
|
|
|
|
serialize::{
|
2023-11-05 05:45:47 +00:00
|
|
|
stream::{ArbitraryWriter, Encryptor2, LiteralWriter, Message, Recipient, Signer},
|
2023-10-19 22:06:34 +00:00
|
|
|
Marshal,
|
|
|
|
},
|
|
|
|
types::KeyFlags,
|
2023-11-05 19:57:22 +00:00
|
|
|
Fingerprint, KeyID, PacketPile,
|
2023-10-19 22:06:34 +00:00
|
|
|
};
|
|
|
|
pub use sequoia_openpgp as openpgp;
|
|
|
|
use sharks::{Share, Sharks};
|
|
|
|
|
|
|
|
mod keyring;
|
|
|
|
use keyring::Keyring;
|
|
|
|
|
2023-11-02 06:01:34 +00:00
|
|
|
mod smartcard;
|
|
|
|
use smartcard::SmartcardManager;
|
|
|
|
|
2023-11-05 19:57:22 +00:00
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
|
|
pub enum Error {
|
|
|
|
#[error("Error with creating Share: {0}")]
|
|
|
|
Share(String),
|
2023-10-19 22:06:34 +00:00
|
|
|
|
2023-11-05 19:57:22 +00:00
|
|
|
#[error("Error combining shares: {0}")]
|
|
|
|
CombineShares(String),
|
2023-10-19 22:06:34 +00:00
|
|
|
|
2023-11-05 19:57:22 +00:00
|
|
|
#[error("Derived secret hash {0} != expected {1}")]
|
|
|
|
InvalidSecret(Fingerprint, Fingerprint),
|
|
|
|
|
|
|
|
#[error("OpenPGP error: {0}")]
|
|
|
|
Sequoia(#[source] anyhow::Error),
|
|
|
|
|
|
|
|
#[error("OpenPGP IO error: {0}")]
|
|
|
|
SequoiaIo(#[source] std::io::Error),
|
|
|
|
|
|
|
|
#[error("Keyring error: {0}")]
|
2023-11-05 22:21:54 +00:00
|
|
|
Keyring(#[from] keyring::Error),
|
2023-10-19 22:06:34 +00:00
|
|
|
|
2023-11-05 19:57:22 +00:00
|
|
|
#[error("Smartcard error: {0}")]
|
2023-11-05 22:21:54 +00:00
|
|
|
Smartcard(#[from] smartcard::Error),
|
2023-10-19 22:06:34 +00:00
|
|
|
|
2023-11-05 19:57:22 +00:00
|
|
|
#[error("IO error: {0}")]
|
|
|
|
Io(#[source] std::io::Error),
|
|
|
|
|
|
|
|
#[error("Derivation path: {0}")]
|
|
|
|
DerivationPath(#[from] keyfork_derive_openpgp::derive_util::path::Error),
|
|
|
|
|
|
|
|
#[error("Derivation request: {0}")]
|
|
|
|
DerivationRequest(#[from] keyfork_derive_openpgp::derive_util::request::DerivationError),
|
|
|
|
|
|
|
|
#[error("Keyfork OpenPGP: {0}")]
|
|
|
|
KeyforkOpenPGP(#[from] keyfork_derive_openpgp::Error),
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type Result<T, E = Error> = std::result::Result<T, E>;
|
2023-10-19 22:06:34 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct EncryptedMessage {
|
|
|
|
pkesks: Vec<PKESK>,
|
|
|
|
message: SEIP,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EncryptedMessage {
|
|
|
|
pub fn with_swap(pkesks: &mut Vec<PKESK>, seip: SEIP) -> Self {
|
|
|
|
Self {
|
|
|
|
pkesks: std::mem::take(pkesks),
|
|
|
|
message: seip,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-02 06:01:34 +00:00
|
|
|
pub fn decrypt_with<H>(&self, policy: &'_ dyn Policy, decryptor: H) -> Result<Vec<u8>>
|
|
|
|
where
|
|
|
|
H: VerificationHelper + DecryptionHelper,
|
|
|
|
{
|
2023-10-19 22:06:34 +00:00
|
|
|
let mut packets = vec![];
|
|
|
|
|
|
|
|
for pkesk in &self.pkesks {
|
|
|
|
let mut packet = vec![];
|
2023-11-05 19:57:22 +00:00
|
|
|
pkesk.serialize(&mut packet).map_err(Error::Sequoia)?;
|
2023-10-19 22:06:34 +00:00
|
|
|
let message = Message::new(&mut packets);
|
2023-11-05 19:57:22 +00:00
|
|
|
let mut message = ArbitraryWriter::new(message, Tag::PKESK).map_err(Error::Sequoia)?;
|
|
|
|
message.write_all(&packet).map_err(Error::SequoiaIo)?;
|
|
|
|
message.finalize().map_err(Error::Sequoia)?;
|
2023-10-19 22:06:34 +00:00
|
|
|
}
|
|
|
|
let mut packet = vec![];
|
2023-11-05 19:57:22 +00:00
|
|
|
self.message
|
|
|
|
.serialize(&mut packet)
|
|
|
|
.map_err(Error::Sequoia)?;
|
2023-10-19 22:06:34 +00:00
|
|
|
let message = Message::new(&mut packets);
|
2023-11-05 19:57:22 +00:00
|
|
|
let mut message = ArbitraryWriter::new(message, Tag::SEIP).map_err(Error::Sequoia)?;
|
|
|
|
message.write_all(&packet).map_err(Error::SequoiaIo)?;
|
|
|
|
message.finalize().map_err(Error::Sequoia)?;
|
2023-10-19 22:06:34 +00:00
|
|
|
|
2023-11-05 19:57:22 +00:00
|
|
|
let mut decryptor = DecryptorBuilder::from_bytes(&packets)
|
|
|
|
.map_err(Error::Sequoia)?
|
|
|
|
.with_policy(policy, None, decryptor)
|
|
|
|
.map_err(Error::Sequoia)?;
|
2023-10-19 22:06:34 +00:00
|
|
|
|
|
|
|
let mut content = vec![];
|
2023-11-05 19:57:22 +00:00
|
|
|
decryptor
|
|
|
|
.read_to_end(&mut content)
|
|
|
|
.map_err(Error::SequoiaIo)?;
|
2023-10-19 22:06:34 +00:00
|
|
|
Ok(content)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn discover_certs(path: impl AsRef<Path>) -> Result<Vec<Cert>> {
|
|
|
|
let path = path.as_ref();
|
|
|
|
|
|
|
|
if path.is_file() {
|
|
|
|
let mut vec = vec![];
|
2023-11-05 19:57:22 +00:00
|
|
|
for cert in CertParser::from_file(path).map_err(Error::Sequoia)? {
|
|
|
|
vec.push(cert.map_err(Error::Sequoia)?);
|
2023-10-19 22:06:34 +00:00
|
|
|
}
|
|
|
|
Ok(vec)
|
|
|
|
} else {
|
|
|
|
let mut vec = vec![];
|
|
|
|
for entry in path
|
2023-11-05 19:57:22 +00:00
|
|
|
.read_dir()
|
|
|
|
.map_err(Error::Io)?
|
2023-10-19 22:06:34 +00:00
|
|
|
.filter_map(Result::ok)
|
|
|
|
.filter(|p| p.path().is_file())
|
|
|
|
{
|
2023-11-05 19:57:22 +00:00
|
|
|
vec.push(Cert::from_file(entry.path()).map_err(Error::Sequoia)?);
|
2023-10-19 22:06:34 +00:00
|
|
|
}
|
|
|
|
Ok(vec)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-20 01:10:02 +00:00
|
|
|
pub fn parse_messages(reader: impl Read + Send + Sync) -> Result<VecDeque<EncryptedMessage>> {
|
2023-10-19 22:06:34 +00:00
|
|
|
let mut pkesks = Vec::new();
|
|
|
|
let mut encrypted_messages = VecDeque::new();
|
|
|
|
|
2023-11-05 19:57:22 +00:00
|
|
|
for packet in PacketPile::from_reader(reader)
|
|
|
|
.map_err(Error::Sequoia)?
|
|
|
|
.into_children()
|
|
|
|
{
|
2023-10-19 22:06:34 +00:00
|
|
|
match packet {
|
|
|
|
Packet::PKESK(p) => pkesks.push(p),
|
|
|
|
Packet::SEIP(s) => {
|
|
|
|
encrypted_messages.push_back(EncryptedMessage::with_swap(&mut pkesks, s));
|
|
|
|
}
|
|
|
|
s => {
|
|
|
|
panic!("Invalid variant found: {}", s.tag());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(encrypted_messages)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_encryption_keys<'a>(
|
|
|
|
cert: &'a ValidCert,
|
|
|
|
) -> openpgp::cert::prelude::ValidKeyAmalgamationIter<
|
|
|
|
'a,
|
|
|
|
openpgp::packet::key::PublicParts,
|
|
|
|
openpgp::packet::key::UnspecifiedRole,
|
|
|
|
> {
|
|
|
|
cert.keys()
|
|
|
|
.alive()
|
|
|
|
.revoked(false)
|
|
|
|
.supported()
|
|
|
|
.for_storage_encryption()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_decryption_keys<'a>(
|
|
|
|
cert: &'a ValidCert,
|
|
|
|
) -> openpgp::cert::prelude::ValidKeyAmalgamationIter<
|
|
|
|
'a,
|
|
|
|
openpgp::packet::key::SecretParts,
|
|
|
|
openpgp::packet::key::UnspecifiedRole,
|
|
|
|
> {
|
|
|
|
cert.keys()
|
|
|
|
/*
|
|
|
|
.alive()
|
|
|
|
.revoked(false)
|
|
|
|
.supported()
|
|
|
|
*/
|
|
|
|
.for_storage_encryption()
|
|
|
|
.secret()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn combine(
|
|
|
|
threshold: u8,
|
|
|
|
certs: Vec<Cert>,
|
|
|
|
metadata: EncryptedMessage,
|
|
|
|
messages: Vec<EncryptedMessage>,
|
|
|
|
mut output: impl Write,
|
|
|
|
) -> Result<()> {
|
|
|
|
// Be as liberal as possible when decrypting.
|
|
|
|
// We don't want to invalidate someone's keys just because the old sig expired.
|
|
|
|
let policy = NullPolicy::new();
|
|
|
|
|
2023-11-05 05:45:47 +00:00
|
|
|
let mut keyring = Keyring::new(certs)?;
|
|
|
|
let mut manager = SmartcardManager::new()?;
|
2023-11-04 01:42:33 +00:00
|
|
|
let content = if keyring.is_empty() {
|
|
|
|
let card_fp = manager.load_any_card()?;
|
|
|
|
eprintln!("key discovery is empty, using hardware smartcard: {card_fp}");
|
|
|
|
metadata.decrypt_with(&policy, &mut manager)?
|
|
|
|
} else {
|
|
|
|
metadata.decrypt_with(&policy, &mut keyring)?
|
|
|
|
};
|
2023-10-19 22:06:34 +00:00
|
|
|
|
2023-11-05 19:57:22 +00:00
|
|
|
let mut cert_parser = CertParser::from_bytes(&content).map_err(Error::Sequoia)?;
|
2023-10-19 22:06:34 +00:00
|
|
|
let root_cert = match cert_parser.next() {
|
|
|
|
Some(Ok(c)) => c,
|
|
|
|
Some(Err(e)) => panic!("Could not find root (first) certificate: {e}"),
|
|
|
|
None => panic!("No certs found in cert parser"),
|
|
|
|
};
|
2023-11-05 19:57:22 +00:00
|
|
|
let certs = cert_parser
|
|
|
|
.collect::<openpgp::Result<Vec<_>>>()
|
|
|
|
.map_err(Error::Sequoia)?;
|
2023-11-04 01:42:33 +00:00
|
|
|
keyring.set_root_cert(root_cert.clone());
|
|
|
|
manager.set_root_cert(root_cert);
|
2023-10-19 22:06:34 +00:00
|
|
|
let mut messages: HashMap<KeyID, EncryptedMessage> =
|
|
|
|
HashMap::from_iter(certs.iter().map(|c| c.keyid()).zip(messages));
|
|
|
|
let mut decrypted_messages: HashMap<KeyID, Vec<u8>> = HashMap::new();
|
|
|
|
|
|
|
|
// NOTE: This is ONLY stable because we control the generation of PKESK packets and
|
|
|
|
// encode the policy to ourselves.
|
|
|
|
for valid_cert in certs.iter().map(|cert| cert.with_policy(&policy, None)) {
|
2023-11-05 19:57:22 +00:00
|
|
|
let valid_cert = valid_cert.map_err(Error::Sequoia)?;
|
2023-10-19 22:06:34 +00:00
|
|
|
// get keys from keyring for cert
|
|
|
|
let Some(secret_cert) = keyring.get_cert_for_primary_keyid(&valid_cert.keyid()) else {
|
|
|
|
continue;
|
|
|
|
};
|
2023-11-05 19:57:22 +00:00
|
|
|
let secret_cert = secret_cert
|
|
|
|
.with_policy(&policy, None)
|
|
|
|
.map_err(Error::Sequoia)?;
|
2023-10-19 22:06:34 +00:00
|
|
|
let keys = get_decryption_keys(&secret_cert).collect::<Vec<_>>();
|
|
|
|
if !keys.is_empty() {
|
|
|
|
if let Some(message) = messages.get_mut(&valid_cert.keyid()) {
|
|
|
|
for (pkesk, key) in message.pkesks.iter_mut().zip(keys) {
|
|
|
|
pkesk.set_recipient(key.keyid());
|
|
|
|
}
|
|
|
|
// we have a pkesk, decrypt via keyring
|
|
|
|
let result = message.decrypt_with(&policy, &mut keyring);
|
|
|
|
match result {
|
|
|
|
Ok(message) => {
|
|
|
|
decrypted_messages.insert(valid_cert.keyid(), message);
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
eprintln!(
|
|
|
|
"Could not decrypt with fingerprint {}: {}",
|
|
|
|
valid_cert.keyid(),
|
|
|
|
e
|
|
|
|
);
|
|
|
|
// do nothing, key will be retained
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// clean decrypted messages from encrypted messages
|
|
|
|
messages.retain(|k, _v| !decrypted_messages.contains_key(k));
|
|
|
|
|
|
|
|
let left_from_threshold = threshold as usize - decrypted_messages.len();
|
|
|
|
if left_from_threshold > 0 {
|
|
|
|
eprintln!("remaining keys: {left_from_threshold}, prompting yubikeys");
|
2023-11-02 06:01:34 +00:00
|
|
|
let mut remaining_usable_certs = certs
|
|
|
|
.iter()
|
|
|
|
.filter(|cert| messages.contains_key(&cert.keyid()))
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
while threshold as usize - decrypted_messages.len() > 0 {
|
|
|
|
remaining_usable_certs.retain(|cert| messages.contains_key(&cert.keyid()));
|
2023-11-04 01:42:33 +00:00
|
|
|
let mut key_by_fingerprints = HashMap::new();
|
|
|
|
let mut total_fingerprints = vec![];
|
2023-11-02 06:01:34 +00:00
|
|
|
for valid_cert in remaining_usable_certs
|
|
|
|
.iter()
|
|
|
|
.map(|cert| cert.with_policy(&policy, None))
|
|
|
|
{
|
2023-11-05 19:57:22 +00:00
|
|
|
let valid_cert = valid_cert.map_err(Error::Sequoia)?;
|
2023-11-04 01:42:33 +00:00
|
|
|
let fp = valid_cert
|
|
|
|
.keys()
|
|
|
|
.for_storage_encryption()
|
|
|
|
.map(|k| k.fingerprint())
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
for fp in &fp {
|
|
|
|
key_by_fingerprints.insert(fp.clone(), valid_cert.keyid());
|
|
|
|
}
|
|
|
|
total_fingerprints.extend(fp.iter().cloned());
|
2023-11-02 06:01:34 +00:00
|
|
|
}
|
2023-11-04 01:42:33 +00:00
|
|
|
|
|
|
|
// Iterate over all fingerprints and use key_by_fingerprints to assoc with Enc. Message
|
|
|
|
if let Some(fp) = manager.load_any_fingerprint(total_fingerprints)? {
|
|
|
|
// soundness: `key_by_fingerprints` is extended by the same fps that are then
|
|
|
|
// inserted into `total_fingerprints`
|
|
|
|
let cert_keyid = key_by_fingerprints.get(&fp).unwrap().clone();
|
|
|
|
let message = messages.remove(&cert_keyid);
|
|
|
|
if let Some(message) = message {
|
|
|
|
let message = message.decrypt_with(&policy, &mut manager)?;
|
|
|
|
decrypted_messages.insert(cert_keyid, message);
|
2023-11-02 06:01:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-10-19 22:06:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let shares = decrypted_messages
|
|
|
|
.values()
|
|
|
|
.map(|message| Share::try_from(message.as_slice()))
|
|
|
|
.collect::<Result<Vec<_>, &str>>()
|
2023-11-05 19:57:22 +00:00
|
|
|
.map_err(|e| Error::Share(e.to_string()))?;
|
|
|
|
let secret = Sharks(threshold)
|
|
|
|
.recover(&shares)
|
|
|
|
.map_err(|e| Error::CombineShares(e.to_string()))?;
|
2023-10-19 22:06:34 +00:00
|
|
|
|
2023-10-20 01:10:02 +00:00
|
|
|
let userid = UserID::from("keyfork-sss");
|
|
|
|
let kdr = DerivationRequest::new(
|
|
|
|
DerivationAlgorithm::Ed25519,
|
|
|
|
&DerivationPath::from_str("m/7366512'/0'")?,
|
|
|
|
)
|
|
|
|
.derive_with_master_seed(secret.to_vec())?;
|
|
|
|
let derived_cert = keyfork_derive_openpgp::derive(
|
|
|
|
kdr,
|
|
|
|
&[KeyFlags::empty().set_certification().set_signing()],
|
|
|
|
userid,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
// NOTE: Signatures on certs will be different. Compare fingerprints instead.
|
2023-11-05 19:57:22 +00:00
|
|
|
let derived_fp = derived_cert.fingerprint();
|
|
|
|
let expected_fp = keyring
|
|
|
|
.root_cert()
|
|
|
|
.expect("cert was previously set")
|
|
|
|
.fingerprint();
|
|
|
|
if derived_fp != expected_fp {
|
|
|
|
return Err(Error::InvalidSecret(derived_fp, expected_fp));
|
2023-10-20 01:10:02 +00:00
|
|
|
}
|
|
|
|
|
2023-11-05 19:57:22 +00:00
|
|
|
output
|
|
|
|
.write_all(smex::encode(&secret).as_bytes())
|
|
|
|
.map_err(Error::Io)?;
|
2023-10-19 22:06:34 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn split(threshold: u8, certs: Vec<Cert>, secret: &[u8], output: impl Write) -> Result<()> {
|
|
|
|
// build cert to sign encrypted shares
|
|
|
|
let userid = UserID::from("keyfork-sss");
|
|
|
|
let kdr = DerivationRequest::new(
|
|
|
|
DerivationAlgorithm::Ed25519,
|
|
|
|
&DerivationPath::from_str("m/7366512'/0'")?,
|
|
|
|
)
|
|
|
|
.derive_with_master_seed(secret.to_vec())?;
|
|
|
|
let derived_cert = keyfork_derive_openpgp::derive(
|
|
|
|
kdr,
|
|
|
|
&[KeyFlags::empty().set_certification().set_signing()],
|
|
|
|
userid,
|
|
|
|
)?;
|
|
|
|
let signing_key = derived_cert
|
|
|
|
.primary_key()
|
2023-11-05 19:57:22 +00:00
|
|
|
.parts_into_secret()
|
|
|
|
.map_err(Error::Sequoia)?
|
2023-10-19 22:06:34 +00:00
|
|
|
.key()
|
|
|
|
.clone()
|
2023-11-05 19:57:22 +00:00
|
|
|
.into_keypair()
|
|
|
|
.map_err(Error::Sequoia)?;
|
2023-10-19 22:06:34 +00:00
|
|
|
|
|
|
|
let sharks = Sharks(threshold);
|
|
|
|
let dealer = sharks.dealer(secret);
|
|
|
|
let shares = dealer.map(|s| Vec::from(&s)).collect::<Vec<_>>();
|
|
|
|
let policy = StandardPolicy::new();
|
2023-11-05 19:57:22 +00:00
|
|
|
let mut writer = Writer::new(output, Kind::Message).map_err(Error::SequoiaIo)?;
|
2023-10-19 22:06:34 +00:00
|
|
|
|
|
|
|
let mut total_recipients = vec![];
|
|
|
|
let mut messages = vec![];
|
|
|
|
|
|
|
|
for (share, cert) in shares.iter().zip(certs) {
|
|
|
|
total_recipients.push(cert.clone());
|
2023-11-05 19:57:22 +00:00
|
|
|
let valid_cert = cert.with_policy(&policy, None).map_err(Error::Sequoia)?;
|
2023-10-19 22:06:34 +00:00
|
|
|
let encryption_keys = get_encryption_keys(&valid_cert).collect::<Vec<_>>();
|
|
|
|
|
|
|
|
let mut message_output = vec![];
|
|
|
|
let message = Message::new(&mut message_output);
|
2023-11-05 05:45:47 +00:00
|
|
|
let message = Encryptor2::for_recipients(
|
2023-10-19 22:06:34 +00:00
|
|
|
message,
|
|
|
|
encryption_keys
|
|
|
|
.iter()
|
|
|
|
.map(|k| Recipient::new(KeyID::wildcard(), k.key())),
|
|
|
|
)
|
2023-11-05 19:57:22 +00:00
|
|
|
.build()
|
|
|
|
.map_err(Error::Sequoia)?;
|
|
|
|
let message = Signer::new(message, signing_key.clone())
|
|
|
|
.build()
|
|
|
|
.map_err(Error::Sequoia)?;
|
|
|
|
let mut message = LiteralWriter::new(message)
|
|
|
|
.build()
|
|
|
|
.map_err(Error::Sequoia)?;
|
|
|
|
message.write_all(share).map_err(Error::SequoiaIo)?;
|
|
|
|
message.finalize().map_err(Error::Sequoia)?;
|
2023-10-19 22:06:34 +00:00
|
|
|
|
|
|
|
messages.push(message_output);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut pp = vec![];
|
|
|
|
// store derived cert to verify provided shares
|
2023-11-05 19:57:22 +00:00
|
|
|
derived_cert.serialize(&mut pp).map_err(Error::Sequoia)?;
|
2023-10-19 22:06:34 +00:00
|
|
|
for recipient in &total_recipients {
|
2023-11-05 19:57:22 +00:00
|
|
|
recipient.serialize(&mut pp).map_err(Error::Sequoia)?;
|
2023-10-19 22:06:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// verify packet pile
|
2023-11-05 19:57:22 +00:00
|
|
|
for (packet_cert, cert) in openpgp::cert::CertParser::from_bytes(&pp)
|
|
|
|
.map_err(Error::Sequoia)?
|
2023-10-19 22:06:34 +00:00
|
|
|
.skip(1)
|
|
|
|
.zip(total_recipients.iter())
|
|
|
|
{
|
2023-11-05 19:57:22 +00:00
|
|
|
if packet_cert.map_err(Error::Sequoia)? != *cert {
|
2023-10-19 22:06:34 +00:00
|
|
|
panic!(
|
|
|
|
"packet pile could not recreate cert: {}",
|
|
|
|
cert.fingerprint()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let valid_certs = total_recipients
|
|
|
|
.iter()
|
|
|
|
.map(|c| c.with_policy(&policy, None))
|
2023-11-05 19:57:22 +00:00
|
|
|
.collect::<openpgp::Result<Vec<_>>>()
|
|
|
|
.map_err(Error::Sequoia)?;
|
2023-10-19 22:06:34 +00:00
|
|
|
|
|
|
|
let total_recipients = valid_certs.iter().flat_map(|vc| {
|
|
|
|
get_encryption_keys(vc).map(|key| Recipient::new(KeyID::wildcard(), key.key()))
|
|
|
|
});
|
|
|
|
|
|
|
|
// metadata
|
|
|
|
let mut message_output = vec![];
|
|
|
|
let message = Message::new(&mut message_output);
|
2023-11-05 19:57:22 +00:00
|
|
|
let message = Encryptor2::for_recipients(message, total_recipients)
|
|
|
|
.build()
|
|
|
|
.map_err(Error::Sequoia)?;
|
|
|
|
let mut message = LiteralWriter::new(message)
|
|
|
|
.build()
|
|
|
|
.map_err(Error::Sequoia)?;
|
|
|
|
message.write_all(&pp).map_err(Error::SequoiaIo)?;
|
|
|
|
message.finalize().map_err(Error::Sequoia)?;
|
|
|
|
writer
|
|
|
|
.write_all(&message_output)
|
|
|
|
.map_err(Error::SequoiaIo)?;
|
2023-10-19 22:06:34 +00:00
|
|
|
|
|
|
|
for message in messages {
|
2023-11-05 19:57:22 +00:00
|
|
|
writer.write_all(&message).map_err(Error::SequoiaIo)?;
|
2023-10-19 22:06:34 +00:00
|
|
|
}
|
|
|
|
|
2023-11-05 19:57:22 +00:00
|
|
|
writer.finalize().map_err(Error::SequoiaIo)?;
|
2023-10-19 22:06:34 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|