Skip to content

Commit ebda774

Browse files
Merge head of master into keyring (#169)
* Add a basic example for encrypting and decrypting with a KMS CMK (#136) * *Issue #, if available:* #108 *Description of changes:* Add a basic example for encrypting and decrypting with a KMS CMK. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license. # Check any applicable: - [ ] Were any files moved? Moving files changes their URL, which breaks all hyperlinks to the files. * Add test and Maven plugin to include examples directory as test source * Update docs in prep for 1.6.1 (#133) * Update docs in prep for 1.6.1 * Actually bump version for release * Fix for new versions of gpg * Refactor JceMasterKey to extract logic to be shared by raw keyrings. (#139) * Refactor JceMasterKey to extract logic to be shared by raw keyrings. *Issue #, if available:* #102 *Description of changes:* In anticipation of the RawAesKeyring and RawRsaKeyring needing logic currently embedded in the JceMasterKey, this change extracts that logic into the JceKeyCipher class so it may be shared. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license. - [ ] Were any files moved? Moving files changes their URL, which breaks all hyperlinks to the 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 Co-authored-by: SalusaSecondus <[email protected]> Co-authored-by: Greg Rubin <[email protected]>
1 parent b893a0e commit ebda774

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)