Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In ZipOutputStream.PutNextEntry, account for AES overhead when calculating compressed entry size #460

Merged
merged 2 commits into from
Aug 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/ICSharpCode.SharpZipLib/Zip/ZipEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ public bool LocalHeaderRequiresZip64

if ((versionToExtract == 0) && IsCrypted)
{
trueCompressedSize += ZipConstants.CryptoHeaderSize;
trueCompressedSize += (ulong)this.EncryptionOverheadSize;
}

// TODO: A better estimation of the true limit based on compression overhead should be used
Expand Down Expand Up @@ -1013,6 +1013,26 @@ internal int AESOverheadSize
}
}

/// <summary>
/// Number of extra bytes required to hold the encryption header fields.
/// </summary>
internal int EncryptionOverheadSize
{
get
{
// Entry is not encrypted - no overhead
if (!this.IsCrypted)
return 0;

// Entry is encrypted using ZipCrypto
if (_aesEncryptionStrength == 0)
return ZipConstants.CryptoHeaderSize;

// Entry is encrypted using AES
return this.AESOverheadSize;
}
}

/// <summary>
/// Process extra data fields updating the entry based on the contents.
/// </summary>
Expand Down
13 changes: 3 additions & 10 deletions src/ICSharpCode.SharpZipLib/Zip/ZipOutputStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ public void PutNextEntry(ZipEntry entry)
}
else
{
WriteLeInt(entry.IsCrypted ? (int)entry.CompressedSize + ZipConstants.CryptoHeaderSize : (int)entry.CompressedSize);
WriteLeInt((int)entry.CompressedSize + entry.EncryptionOverheadSize);
WriteLeInt((int)entry.Size);
}
}
Expand Down Expand Up @@ -382,7 +382,7 @@ public void PutNextEntry(ZipEntry entry)
if (headerInfoAvailable)
{
ed.AddLeLong(entry.Size);
ed.AddLeLong(entry.CompressedSize);
ed.AddLeLong(entry.CompressedSize + entry.EncryptionOverheadSize);
}
else
{
Expand Down Expand Up @@ -540,14 +540,7 @@ public void CloseEntry()

if (curEntry.IsCrypted)
{
if (curEntry.AESKeySize > 0)
{
curEntry.CompressedSize += curEntry.AESOverheadSize;
}
else
{
curEntry.CompressedSize += ZipConstants.CryptoHeaderSize;
}
curEntry.CompressedSize += curEntry.EncryptionOverheadSize;
}

// Patch the header if possible
Expand Down
36 changes: 36 additions & 0 deletions test/ICSharpCode.SharpZipLib.Tests/Zip/ZipEncryptionHandling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,42 @@ public void ZipCryptoEncryption(CompressionMethod compressionMethod)
CreateZipWithEncryptedEntries("foo", 0, compressionMethod);
}

/// <summary>
/// Test Known zero length encrypted entries with ZipOutputStream.
/// These are entries where the entry size is set to 0 ahead of time, so that PutNextEntry will fill in the header and there will be no patching.
/// Test with Zip64 on and off, as the logic is different for the two.
/// </summary>
[Test]
public void ZipOutputStreamEncryptEmptyEntries(
[Values] UseZip64 useZip64,
[Values(0, 128, 256)] int keySize,
[Values(CompressionMethod.Stored, CompressionMethod.Deflated)] CompressionMethod compressionMethod)
{
using (var ms = new MemoryStream())
{
using (var zipOutputStream = new ZipOutputStream(ms))
{
zipOutputStream.IsStreamOwner = false;
zipOutputStream.Password = "password";
zipOutputStream.UseZip64 = useZip64;

ZipEntry zipEntry = new ZipEntry("emptyEntry")
{
AESKeySize = keySize,
CompressionMethod = compressionMethod,
CompressedSize = 0,
Crc = 0,
Size = 0,
};

zipOutputStream.PutNextEntry(zipEntry);
zipOutputStream.CloseEntry();
}

VerifyZipWith7Zip(ms, "password");
}
}

[Test]
[Category("Encryption")]
[Category("Zip")]
Expand Down