Skip to content

Commit dcbc562

Browse files
fix: The final frame can not be larger than the Frame Length (#166)
* Add validation to ensure the length of the final frame in the final frame header does not exceed the frame size specified in the message header. * Validate that frame length is positive for framed data * Reverting removal of variable frame length code * Reverting removal of variable frame length code * Fix spacing after if
1 parent 4fdc309 commit dcbc562

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

src/main/java/com/amazonaws/encryptionsdk/internal/FrameDecryptionHandler.java

+5
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ public ProcessingSummary processBytes(final byte[] in, final int off, final int
133133
int protectedContentLen = -1;
134134
if (currentFrameHeaders_.isFinalFrame()) {
135135
protectedContentLen = currentFrameHeaders_.getFrameContentLength();
136+
137+
// The final frame should not be able to exceed the frameLength
138+
if (frameSize_ > 0 && protectedContentLen > frameSize_) {
139+
throw new BadCiphertextException("Final frame length exceeds frame length.");
140+
}
136141
} else {
137142
protectedContentLen = frameSize_;
138143
}

src/main/java/com/amazonaws/encryptionsdk/model/CiphertextHeaders.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -859,4 +859,4 @@ public void setHeaderNonce(final byte[] headerNonce) {
859859
public void setHeaderTag(final byte[] headerTag) {
860860
headerTag_ = headerTag.clone();
861861
}
862-
}
862+
}

src/test/java/com/amazonaws/encryptionsdk/internal/FrameDecryptionHandlerTest.java

+18-1
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@
1515

1616
import static org.junit.Assert.assertTrue;
1717

18+
import java.nio.ByteBuffer;
1819
import java.security.SecureRandom;
1920

2021
import javax.crypto.SecretKey;
2122
import javax.crypto.spec.SecretKeySpec;
2223

24+
import com.amazonaws.encryptionsdk.TestUtils;
25+
import com.amazonaws.encryptionsdk.exception.BadCiphertextException;
2326
import org.junit.Before;
2427
import org.junit.Test;
2528

@@ -72,4 +75,18 @@ public void decryptMaxContentLength() {
7275
frameDecryptionHandler_.processBytes(in, 0, in.length, out, 0);
7376
frameDecryptionHandler_.processBytes(in, 0, Integer.MAX_VALUE, out, 0);
7477
}
75-
}
78+
79+
@Test(expected = BadCiphertextException.class)
80+
public void finalFrameLengthTooLarge() {
81+
82+
final ByteBuffer byteBuffer = ByteBuffer.allocate(25);
83+
byteBuffer.put(TestUtils.unsignedBytesToSignedBytes(
84+
new int[] {255, 255, 255, 255, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}));
85+
byteBuffer.putInt(AwsCrypto.getDefaultFrameSize() + 1);
86+
87+
final byte[] in = byteBuffer.array();
88+
final byte[] out = new byte[in.length];
89+
90+
frameDecryptionHandler_.processBytes(in, 0, in.length, out, 0);
91+
}
92+
}

0 commit comments

Comments
 (0)