Skip to content

Commit 163fd88

Browse files
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.
1 parent 4fdc309 commit 163fd88

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
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/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)