Skip to content

fix: The final frame can not be larger than the Frame Length #166

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

Merged
merged 5 commits into from
Apr 7, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,6 @@ public ProcessingSummary processBytes(final byte[] in, final int off, final int
if (currentFrameHeaders_ == null) {
currentFrameHeaders_ = new CipherFrameHeaders();
currentFrameHeaders_.setNonceLength(nonceLen_);
if (frameSize_ == 0) {
// if frame size in ciphertext headers is 0, the frame size
// will need to be parsed in individual frame headers.
currentFrameHeaders_.includeFrameSize(true);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we removing this logic?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After discussion, I'll revert this part of the change and have created a separate issue to track removing this code (which was a partially implemented variable frame length feature): #167

}

totalParsedBytes += currentFrameHeaders_.deserialize(bytesToParse, totalParsedBytes);
Expand All @@ -133,6 +128,11 @@ public ProcessingSummary processBytes(final byte[] in, final int off, final int
int protectedContentLen = -1;
if (currentFrameHeaders_.isFinalFrame()) {
protectedContentLen = currentFrameHeaders_.getFrameContentLength();

// The final frame should not be able to exceed the frameLength
if(protectedContentLen > frameSize_) {
throw new BadCiphertextException("Final frame length exceeds frame length.");
}
} else {
protectedContentLen = frameSize_;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ public CiphertextHeaders(final byte version, final CiphertextType type, final Cr
messageId_ = new byte[Constants.MESSAGE_ID_LEN];
RND.nextBytes(messageId_);

if(contentType == ContentType.FRAME && frameSize <= 0) {
throw new BadCiphertextException("Framed data requires a positive frame length");
}

frameLength_ = frameSize;

// Completed by construction
Expand Down Expand Up @@ -630,6 +634,10 @@ public int deserialize(final byte[] b, final int off) throws ParseException {
parsedBytes += parseHeaderTag(b, off + parsedBytes);
}

if(frameLength_ <= 0 && ContentType.deserialize(contentTypeVal_) == ContentType.FRAME) {
throw new BadCiphertextException("Framed data requires a positive frame length");
}

isComplete_ = true;
} catch (ParseException e) {
// this results when we do partial parsing and there aren't enough
Expand Down Expand Up @@ -859,4 +867,4 @@ public void setHeaderNonce(final byte[] headerNonce) {
public void setHeaderTag(final byte[] headerTag) {
headerTag_ = headerTag.clone();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@

import static org.junit.Assert.assertTrue;

import java.nio.ByteBuffer;
import java.security.SecureRandom;

import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import com.amazonaws.encryptionsdk.TestUtils;
import com.amazonaws.encryptionsdk.exception.BadCiphertextException;
import org.junit.Before;
import org.junit.Test;

Expand Down Expand Up @@ -72,4 +75,18 @@ public void decryptMaxContentLength() {
frameDecryptionHandler_.processBytes(in, 0, in.length, out, 0);
frameDecryptionHandler_.processBytes(in, 0, Integer.MAX_VALUE, out, 0);
}
}

@Test(expected = BadCiphertextException.class)
public void finalFrameLengthTooLarge() {

final ByteBuffer byteBuffer = ByteBuffer.allocate(25);
byteBuffer.put(TestUtils.unsignedBytesToSignedBytes(
new int[] {255, 255, 255, 255, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}));
byteBuffer.putInt(AwsCrypto.getDefaultFrameSize() + 1);

final byte[] in = byteBuffer.array();
final byte[] out = new byte[in.length];

frameDecryptionHandler_.processBytes(in, 0, in.length, out, 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -431,8 +431,8 @@ public void invalidFrameLength() {

readUptoNonceLen(headerBuff);

// set content type to an invalid value
headerBuff.putInt(-1);
// set frame type to an invalid value for framed data
headerBuff.putInt(0);

final CiphertextHeaders reconstructedHeaders = new CiphertextHeaders();
reconstructedHeaders.deserialize(headerBuff.array(), 0);
Expand Down