use std::collections::HashMap;
use std::io::{self, Read, Seek, Write};
use secure_gate::{RevealSecret, RevealSecretMut};
use zip::{CompressionMethod, ZipArchive};
use crate::classify::{member_matches_path, zip_entry_name};
use crate::decrypt::{
member_for_archive, no_member, screen_shape, strip_manifest, DecryptError, DecryptLimits,
};
use crate::decrypt_stream::{
fault_error, find_key, gcm_key, gcm_member, member_shape, read_error, DecryptOutcome,
IntegrityOutcome, Sizes,
};
use crate::limits::MANIFEST_READ_CAP;
use crate::stream::{
chunk_buffer, BlowfishDecryptReader, CbcDecryptReader, ChecksumGate, Fault, InflateReader,
CHUNK,
};
use crate::types::{Cipher, EntryEncryption};
use crate::zip_err;
use crate::zip_stream::{DeflatedSource, ZipSink, ZipStreamError};
use crate::Classification;
const MANIFEST_PATH: &str = "META-INF/manifest.xml";
fn integrity_of(row: &EntryEncryption) -> IntegrityOutcome {
match row.cipher {
Cipher::AesGcmW3c => IntegrityOutcome::Verified,
Cipher::AesCbcW3c | Cipher::BlowfishCfb8 => IntegrityOutcome::PasswordCheckOnly,
}
}
struct Plan<'a> {
row: &'a EntryEncryption,
expected: u64,
len: u64,
}
pub(crate) fn per_entry<R: Read + Seek, S: ZipSink>(
archive: &mut ZipArchive<R>,
class: &Classification,
out: &mut S,
password: &str,
limits: &DecryptLimits,
sizes: Sizes,
) -> Result<DecryptOutcome, DecryptError> {
let mut plans: HashMap<String, Plan<'_>> = HashMap::new();
let mut superseded: Vec<(usize, Plan<'_>)> = Vec::new();
for (row_index, row) in class.encrypted_entries.iter().enumerate() {
let expected = sizes.declared(row.size)?;
let (index, name) = member_for_archive(archive, &row.path, no_member(row_index))?;
let (len, head) = member_shape(archive, index)?;
screen_shape(row, len, &head)?;
let plan = Plan { row, expected, len };
if let Some(earlier) = plans.insert(name, plan) {
superseded.push((index, earlier));
}
}
for (index, plan) in &superseded {
key_for(archive, *index, plan, password, limits)?;
}
let manifest = read_manifest(archive)?;
let stripped = strip_manifest(&manifest)?;
let mut weakest = IntegrityOutcome::Verified;
let mut produced = 0u64;
for index in 0..archive.len() {
let (name, method, crc, size) = {
let file = archive
.by_index(index)
.map_err(|e| DecryptError::Zip(zip_err::message(&e)))?;
(
zip_entry_name(file.name_raw()),
file.compression(),
file.crc32(),
file.size(),
)
};
if member_matches_path(&name, MANIFEST_PATH) {
let n = stripped.len() as u64;
out.deflated(&name, n, &mut stripped.as_slice())
.map_err(zip_stream_error(limits))?;
} else if let Some(plan) = plans.get(&name) {
let budget = limits.max_output_bytes.saturating_sub(produced);
let row_limits = DecryptLimits {
max_output_bytes: budget,
..*limits
};
decrypt_member(archive, index, &name, plan, out, password, &row_limits)
.map_err(|e| with_total_limit(e, limits))?;
produced += plan.expected;
if integrity_of(plan.row) == IntegrityOutcome::PasswordCheckOnly {
weakest = IntegrityOutcome::PasswordCheckOnly;
}
} else {
match method {
CompressionMethod::Stored => {
let mut file = archive
.by_index(index)
.map_err(|e| DecryptError::Zip(zip_err::message(&e)))?;
out.stored(&name, crc, size, &mut file)
.map_err(zip_stream_error(limits))?
}
CompressionMethod::Deflated => {
let mut src = PlainDeflated { archive, index };
out.deflated_from(&name, size, &mut src)
.map_err(zip_stream_error(limits))?;
}
other => {
return Err(DecryptError::Zip(format!(
"member {name:?} uses compression {other:?}, which cannot be re-written"
)))
}
}
}
}
Ok(DecryptOutcome { integrity: weakest })
}
fn with_total_limit(e: DecryptError, limits: &DecryptLimits) -> DecryptError {
match e {
DecryptError::OutputLimit { .. } => DecryptError::OutputLimit {
limit: limits.max_output_bytes,
},
other => other,
}
}
fn read_manifest<R: Read + Seek>(archive: &mut ZipArchive<R>) -> Result<Vec<u8>, DecryptError> {
let (index, _) = member_for_archive(archive, MANIFEST_PATH, || {
DecryptError::Internal("the manifest classify read is not in the archive".into())
})?;
let file = archive
.by_index(index)
.map_err(|e| DecryptError::Zip(zip_err::message(&e)))?;
let mut buf = Vec::new();
file.take(MANIFEST_READ_CAP as u64 + 1)
.read_to_end(&mut buf)
.map_err(read_error)?;
if buf.len() > MANIFEST_READ_CAP {
return Err(DecryptError::Zip("zip entry too large".into()));
}
Ok(buf)
}
pub(crate) fn checksum_key<R: Read + Seek>(
archive: &mut ZipArchive<R>,
index: usize,
row: &EntryEncryption,
password: &str,
limits: &DecryptLimits,
) -> Result<crate::sensitive::DerivedKey, DecryptError> {
find_key(row, password, limits, |k| {
let file = archive
.by_index(index)
.map_err(|e| DecryptError::Zip(zip_err::message(&e)))?;
let mut gate = gated(file, row, k).map_err(fault_error(limits))?;
match gate.verify() {
Ok(()) => Ok(true),
Err(e) => match Fault::take(e) {
Ok(Fault::WrongPassword) => Ok(false),
Ok(other) => Err(fault_error(limits)(other)),
Err(e) => Err(DecryptError::Zip(e.to_string())),
},
}
})
}
fn key_for<R: Read + Seek>(
archive: &mut ZipArchive<R>,
index: usize,
plan: &Plan<'_>,
password: &str,
limits: &DecryptLimits,
) -> Result<(), DecryptError> {
match plan.row.cipher {
Cipher::AesGcmW3c => {
gcm_key(archive, index, plan.row, plan.len, password, limits).map(|_| ())
}
_ => checksum_key(archive, index, plan.row, password, limits).map(|_| ()),
}
}
pub(crate) fn gated<'a>(
file: impl Read + 'a,
row: &EntryEncryption,
key: &[u8],
) -> Result<ChecksumGate<Box<dyn Read + 'a>>, Fault> {
let plain: Box<dyn Read + 'a> = match row.cipher {
Cipher::AesCbcW3c => Box::new(CbcDecryptReader::new(file, key, &row.iv)?),
Cipher::BlowfishCfb8 => Box::new(BlowfishDecryptReader::new(file, key, &row.iv)?),
Cipher::AesGcmW3c => {
return Err(Fault::Internal(
"an AES-GCM member reached the checksum path",
))
}
};
ChecksumGate::new(plain, row.checksum.clone())
}
#[allow(clippy::too_many_arguments)]
fn decrypt_member<R: Read + Seek>(
archive: &mut ZipArchive<R>,
index: usize,
name: &str,
plan: &Plan<'_>,
out: &mut dyn ZipSink,
password: &str,
limits: &DecryptLimits,
) -> Result<(), DecryptError> {
let row = plan.row;
if row.cipher == Cipher::AesGcmW3c {
return gcm_member(archive, index, row, plan.len, password, limits, |plain| {
write_inflated(out, name, plain, plan.expected, limits)
});
}
let key = checksum_key(archive, index, row, password, limits)?;
let file = archive
.by_index(index)
.map_err(|e| DecryptError::Zip(zip_err::message(&e)))?;
use secure_gate::RevealSecret;
key.with_secret(|k| -> Result<(), DecryptError> {
let mut plain = gated(file, row, k).map_err(fault_error(limits))?;
write_inflated(out, name, &mut plain, plan.expected, limits)?;
let mut sink = crate::decrypt_stream::buffer()?;
use secure_gate::RevealSecretMut;
while sink
.with_secret_mut(|b| plain.read(b))
.map_err(read_error)?
> 0
{}
Ok(())
})
}
fn write_inflated(
out: &mut dyn ZipSink,
name: &str,
deflated: &mut dyn Read,
expected: u64,
limits: &DecryptLimits,
) -> Result<(), DecryptError> {
let mut src = EncryptedDeflated {
deflated,
expected,
limit: limits.max_output_bytes,
};
out.deflated_from(name, expected, &mut src)
.map_err(zip_stream_error(limits))?;
Ok(())
}
struct PlainDeflated<'a, R> {
archive: &'a mut ZipArchive<R>,
index: usize,
}
impl<R: Read + Seek> PlainDeflated<'_, R> {
fn open(&mut self) -> Result<zip::read::ZipFile<'_, R>, ZipStreamError> {
self.archive
.by_index(self.index)
.map_err(|e| ZipStreamError::Corrupt(zip_err::message(&e)))
}
}
impl<R: Read + Seek> DeflatedSource for PlainDeflated<'_, R> {
fn inflated(
&mut self,
take: &mut dyn FnMut(&mut dyn Read) -> Result<u64, ZipStreamError>,
) -> Result<u64, ZipStreamError> {
take(&mut self.open()?)
}
fn copy_deflated(&mut self, out: &mut dyn Write) -> Result<(u32, u64), ZipStreamError> {
{
let mut raw = self
.archive
.by_index_raw(self.index)
.map_err(|e| ZipStreamError::Corrupt(zip_err::message(&e)))?;
let mut buf = chunk_buffer(CHUNK).map_err(|f| ZipStreamError::Read(f.into_io()))?;
loop {
let n = buf
.with_secret_mut(|b| raw.read(b))
.map_err(ZipStreamError::Read)?;
if n == 0 {
break;
}
buf.with_secret(|b| out.write_all(&b[..n]))
.map_err(ZipStreamError::Write)?;
}
}
crc_to_end(&mut self.open()?)
}
}
struct EncryptedDeflated<'a> {
deflated: &'a mut dyn Read,
expected: u64,
limit: u64,
}
impl DeflatedSource for EncryptedDeflated<'_> {
fn inflated(
&mut self,
take: &mut dyn FnMut(&mut dyn Read) -> Result<u64, ZipStreamError>,
) -> Result<u64, ZipStreamError> {
let mut inflate = InflateReader::new(&mut *self.deflated, self.expected, self.limit)
.map_err(|f| ZipStreamError::Read(f.into_io()))?;
take(&mut inflate)
}
fn copy_deflated(&mut self, out: &mut dyn Write) -> Result<(u32, u64), ZipStreamError> {
let mut tap = LatchedWrite { out, failed: None };
let result =
InflateReader::with_tap(&mut *self.deflated, self.expected, self.limit, &mut tap)
.map_err(|f| ZipStreamError::Read(f.into_io()))
.and_then(|mut inflate| crc_to_end(&mut inflate));
match tap.failed.take() {
Some(e) => Err(ZipStreamError::Write(e)),
None => result,
}
}
}
struct LatchedWrite<'a> {
out: &'a mut dyn Write,
failed: Option<io::Error>,
}
impl Write for LatchedWrite<'_> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.out.write(buf).map_err(|e| {
let stand_in = io::Error::new(e.kind(), "writing the output failed");
self.failed.get_or_insert(e);
stand_in
})
}
fn flush(&mut self) -> io::Result<()> {
self.out.flush()
}
}
fn crc_to_end(r: &mut dyn Read) -> Result<(u32, u64), ZipStreamError> {
let mut hasher = crc32fast::Hasher::new();
let mut total = 0u64;
let mut buf = chunk_buffer(CHUNK).map_err(|f| ZipStreamError::Read(f.into_io()))?;
loop {
let n = buf
.with_secret_mut(|b| r.read(b))
.map_err(ZipStreamError::Read)?;
if n == 0 {
return Ok((hasher.finalize(), total));
}
buf.with_secret(|b| hasher.update(&b[..n]));
total += n as u64;
}
}
pub(crate) fn zip_stream_error(
limits: &DecryptLimits,
) -> impl Fn(ZipStreamError) -> DecryptError + '_ {
move |e| match e {
ZipStreamError::Read(e) => match Fault::take(e) {
Ok(fault) => fault_error(limits)(fault),
Err(e) => DecryptError::Zip(e.to_string()),
},
ZipStreamError::Write(e) => DecryptError::Io(e),
ZipStreamError::Corrupt(s) => DecryptError::Zip(s),
}
}