Skip to content

Commit

Permalink
#374 Validate AES extra data record before calculating header size
Browse files Browse the repository at this point in the history
  • Loading branch information
srikanth-lingala committed Mar 22, 2022
1 parent 445c161 commit 82e8984
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/main/java/net/lingala/zip4j/io/inputstream/ZipInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.headers.HeaderReader;
import net.lingala.zip4j.headers.HeaderSignature;
import net.lingala.zip4j.model.AESExtraDataRecord;
import net.lingala.zip4j.model.DataDescriptor;
import net.lingala.zip4j.model.ExtraDataRecord;
import net.lingala.zip4j.model.FileHeader;
Expand Down Expand Up @@ -347,14 +348,13 @@ private long getCompressedSize(LocalFileHeader localFileHeader) throws ZipExcept
return localFileHeader.getCompressedSize() - getEncryptionHeaderSize(localFileHeader);
}

private int getEncryptionHeaderSize(LocalFileHeader localFileHeader) {
private int getEncryptionHeaderSize(LocalFileHeader localFileHeader) throws ZipException {
if (!localFileHeader.isEncrypted()) {
return 0;
}

if (localFileHeader.getEncryptionMethod().equals(EncryptionMethod.AES)) {
return InternalZipConstants.AES_AUTH_LENGTH + InternalZipConstants.AES_PASSWORD_VERIFIER_LENGTH
+ localFileHeader.getAesExtraDataRecord().getAesKeyStrength().getSaltLength();
return getAesEncryptionHeaderSize(localFileHeader.getAesExtraDataRecord());
} else if (localFileHeader.getEncryptionMethod().equals(EncryptionMethod.ZIP_STANDARD)) {
return InternalZipConstants.STD_DEC_HDR_SIZE;
} else {
Expand All @@ -377,6 +377,15 @@ private void readUntilEndOfEntry() throws IOException {
this.entryEOFReached = true;
}

private int getAesEncryptionHeaderSize(AESExtraDataRecord aesExtraDataRecord) throws ZipException {
if (aesExtraDataRecord == null || aesExtraDataRecord.getAesKeyStrength() == null) {
throw new ZipException("AesExtraDataRecord not found or invalid for Aes encrypted entry");
}

return InternalZipConstants.AES_AUTH_LENGTH + InternalZipConstants.AES_PASSWORD_VERIFIER_LENGTH
+ aesExtraDataRecord.getAesKeyStrength().getSaltLength();
}

private boolean isEncryptionMethodZipStandard(LocalFileHeader localFileHeader) {
return localFileHeader.isEncrypted() && EncryptionMethod.ZIP_STANDARD.equals(localFileHeader.getEncryptionMethod());
}
Expand Down

0 comments on commit 82e8984

Please sign in to comment.