Skip to content

Commit 4442958

Browse files
aciocmattsb42-awsWesleyRosenblum
authored
fix: Merge mainline into keyrings, revert the keyring reverts (#209)
* Revert "Merge pull request #173 from aws/keyring" This reverts commit 015fc3c, reversing changes made to dcbc562. * Revert "Add an example for replicating the behavior of the AWS KMS MKP with the AWS KMS keyring (#178)" This reverts commit 676407a. * Revert "Updating changelog for version 1.7.0 (#174)" This reverts commit f18c383. * Updating Changelog for version 1.6.2 *Description of changes:* Updating Changelog for version 1.6.2 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. * fix: validate entire ciphertext has been processed before returning (#191) * fix: validate entire ciphertext has been processed before returning * Updating changelog * Updating version for 1.6.2 release (#192) * Revert "Merge pull request #189 from mattsb42-aws/revert" This reverts commit bd4da5b, reversing changes made to d88fe8b. * Replace failing Assert.fails with assertThrows per earlier PR changes Co-authored-by: mattsb42-aws <[email protected]> Co-authored-by: Wesley Rosenblum <[email protected]>
1 parent d88fe8b commit 4442958

File tree

10 files changed

+152
-18
lines changed

10 files changed

+152
-18
lines changed

CHANGELOG.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,17 @@ calls to `AwsCrypto.encrypt(EncryptRequest)` and `AwsCrypto.decrypt(DecryptReque
2929
[#165](https://github.com/aws/aws-encryption-sdk-java/pull/165),
3030
[#168](https://github.com/aws/aws-encryption-sdk-java/pull/168),
3131
and [#170](https://github.com/aws/aws-encryption-sdk-java/pull/170).
32-
32+
33+
## 1.6.2 -- 2020-05-26
34+
35+
### Patches
36+
* Validate final frame length does not exceed the frame size in the message header [PR #166](https://github.com/aws/aws-encryption-sdk-java/pull/166)
37+
* Validate entire ciphertext has been processed before returning [PR #191](https://github.com/aws/aws-encryption-sdk-java/pull/191)
38+
39+
### Maintenance
40+
* Update AWS Java SDK version from 1.11.561 to 1.11.704. [PR #186](https://github.com/aws/aws-encryption-sdk-java/pull/186)
41+
* Upgrade Bouncy Castle from 1.61 to 1.65 [PR #179](https://github.com/aws/aws-encryption-sdk-java/pull/179)
42+
3343
## 1.6.1 -- 2019-10-29
3444

3545
### Deprecation Warnings

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ You can get the latest release from Maven:
5656
<dependency>
5757
<groupId>com.amazonaws</groupId>
5858
<artifactId>aws-encryption-sdk-java</artifactId>
59-
<version>1.6.1</version>
59+
<version>1.6.2</version>
6060
</dependency>
6161
```
6262

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>com.amazonaws</groupId>
66
<artifactId>aws-encryption-sdk-java</artifactId>
7-
<version>1.6.1</version>
7+
<version>1.6.2</version>
88
<packaging>jar</packaging>
99

1010
<name>aws-encryption-sdk-java</name>

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

+12-5
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,15 @@
1313

1414
package com.amazonaws.encryptionsdk.internal;
1515

16-
import java.util.Arrays;
17-
18-
import javax.crypto.Cipher;
19-
import javax.crypto.SecretKey;
20-
2116
import com.amazonaws.encryptionsdk.CryptoAlgorithm;
2217
import com.amazonaws.encryptionsdk.exception.AwsCryptoException;
2318
import com.amazonaws.encryptionsdk.exception.BadCiphertextException;
2419
import com.amazonaws.encryptionsdk.model.CipherBlockHeaders;
2520

21+
import javax.crypto.Cipher;
22+
import javax.crypto.SecretKey;
23+
import java.util.Arrays;
24+
2625
/**
2726
* The block decryption handler is an implementation of CryptoHandler that
2827
* provides methods to decrypt content encrypted and stored in a single block.
@@ -97,6 +96,11 @@ public BlockDecryptionHandler(final SecretKey decryptionKey, final short nonceLe
9796
synchronized public ProcessingSummary processBytes(final byte[] in, final int off, final int len,
9897
final byte[] out,
9998
final int outOff) throws AwsCryptoException {
99+
100+
if (complete_) {
101+
throw new AwsCryptoException("Ciphertext has already been processed.");
102+
}
103+
100104
final byte[] bytesToParse = new byte[unparsedBytes_.length + len];
101105
// If there were previously unparsed bytes, add them as the first
102106
// set of bytes to be parsed in this call.
@@ -166,6 +170,9 @@ synchronized public ProcessingSummary processBytes(final byte[] in, final int of
166170
*/
167171
@Override
168172
synchronized public int doFinal(final byte[] out, final int outOff) throws BadCiphertextException {
173+
if (!complete_) {
174+
throw new BadCiphertextException("Unable to process entire ciphertext.");
175+
}
169176
return 0;
170177
}
171178

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

+4
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,10 @@ public int doFinal(final byte[] out, final int outOff) throws BadCiphertextExcep
335335
} else {
336336
int result = contentCryptoHandler_.doFinal(out, outOff);
337337

338+
if (!ciphertextHeaders_.isComplete() || !contentCryptoHandler_.isComplete()) {
339+
throw new BadCiphertextException("Unable to process entire ciphertext.");
340+
}
341+
338342
return result;
339343
}
340344
}

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

+12-3
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,16 @@ public FrameDecryptionHandler(final SecretKey decryptionKey, final short nonceLe
7878
*
7979
* @param in
8080
* the input byte array.
81-
* @param inOff
81+
* @param off
8282
* the offset into the in array where the data to be decrypted starts.
83-
* @param inLen
83+
* @param len
8484
* the number of bytes to be decrypted.
8585
* @param out
8686
* the output buffer the decrypted plaintext bytes go into.
8787
* @param outOff
8888
* the offset into the output byte array the decrypted data starts at.
8989
* @return the number of bytes written to out and processed
90-
* @throws InvalidCiphertextException
90+
* @throws BadCiphertextException
9191
* if frame number is invalid/out-of-order or if the bytes do not decrypt correctly.
9292
* @throws AwsCryptoException
9393
* if the content type found in the headers is not of frame type.
@@ -96,6 +96,11 @@ public FrameDecryptionHandler(final SecretKey decryptionKey, final short nonceLe
9696
public ProcessingSummary processBytes(final byte[] in, final int off, final int len, final byte[] out,
9797
final int outOff)
9898
throws BadCiphertextException, AwsCryptoException {
99+
100+
if (complete_) {
101+
throw new AwsCryptoException("Ciphertext has already been processed.");
102+
}
103+
99104
final long totalBytesToParse = unparsedBytes_.length + (long) len;
100105
if (totalBytesToParse > Integer.MAX_VALUE) {
101106
throw new AwsCryptoException(
@@ -200,6 +205,10 @@ public ProcessingSummary processBytes(final byte[] in, final int off, final int
200205
*/
201206
@Override
202207
public int doFinal(final byte[] out, final int outOff) {
208+
if (!complete_) {
209+
throw new BadCiphertextException("Unable to process entire ciphertext.");
210+
}
211+
203212
return 0;
204213
}
205214

src/test/java/com/amazonaws/encryptionsdk/AwsCryptoTest.java

+46
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.io.InputStream;
3434
import java.io.OutputStream;
3535
import java.nio.charset.StandardCharsets;
36+
import java.util.Arrays;
3637
import java.util.EnumSet;
3738
import java.util.HashMap;
3839
import java.util.Map;
@@ -150,6 +151,26 @@ private void doTamperedEncryptDecryptWithKeyring(final CryptoAlgorithm cryptoAlg
150151
.ciphertext(cipherText).build()));
151152
}
152153

154+
private void doTruncatedEncryptDecrypt(final CryptoAlgorithm cryptoAlg, final int byteSize, final int frameSize) {
155+
final byte[] plaintextBytes = new byte[byteSize];
156+
157+
final Map<String, String> encryptionContext = new HashMap<>(1);
158+
encryptionContext.put("ENC1", "Encrypt-decrypt test with %d" + byteSize);
159+
160+
encryptionClient_.setEncryptionAlgorithm(cryptoAlg);
161+
encryptionClient_.setEncryptionFrameSize(frameSize);
162+
163+
final byte[] cipherText = encryptionClient_.encryptData(
164+
masterKeyProvider,
165+
plaintextBytes,
166+
encryptionContext).getResult();
167+
final byte[] truncatedCipherText = Arrays.copyOf(cipherText, cipherText.length - 1);
168+
169+
assertThrows(BadCiphertextException.class, () -> encryptionClient_.decryptData(
170+
masterKeyProvider,
171+
truncatedCipherText));
172+
}
173+
153174
private void doEncryptDecryptWithParsedCiphertext(final int byteSize, final int frameSize) {
154175
final byte[] plaintextBytes = new byte[byteSize];
155176

@@ -235,6 +256,31 @@ public void encryptDecryptWithBadSignature() {
235256
}
236257
}
237258

259+
@Test
260+
public void encryptDecryptWithTruncatedCiphertext() {
261+
for (final CryptoAlgorithm cryptoAlg : EnumSet.allOf(CryptoAlgorithm.class)) {
262+
final int[] frameSizeToTest = TestUtils.getFrameSizesToTest(cryptoAlg);
263+
264+
for (int i = 0; i < frameSizeToTest.length; i++) {
265+
final int frameSize = frameSizeToTest[i];
266+
int[] bytesToTest = { 0, 1, frameSize - 1, frameSize, frameSize + 1, (int) (frameSize * 1.5),
267+
frameSize * 2, 1000000 };
268+
269+
for (int j = 0; j < bytesToTest.length; j++) {
270+
final int byteSize = bytesToTest[j];
271+
272+
if (byteSize > 500_000) {
273+
continue;
274+
}
275+
276+
if (byteSize >= 0) {
277+
doTruncatedEncryptDecrypt(cryptoAlg, byteSize, frameSize);
278+
}
279+
}
280+
}
281+
}
282+
}
283+
238284
@Test
239285
public void encryptDecryptWithParsedCiphertext() {
240286
for (final CryptoAlgorithm cryptoAlg : EnumSet.allOf(CryptoAlgorithm.class)) {

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

+24-7
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
package com.amazonaws.encryptionsdk.internal;
1515

16-
import static org.junit.Assert.assertEquals;
1716
import static org.junit.Assert.assertTrue;
1817

1918
import java.nio.ByteBuffer;
@@ -22,6 +21,7 @@
2221
import javax.crypto.SecretKey;
2322
import javax.crypto.spec.SecretKeySpec;
2423

24+
import com.amazonaws.encryptionsdk.exception.BadCiphertextException;
2525
import org.junit.Before;
2626
import org.junit.Test;
2727

@@ -58,11 +58,9 @@ public void estimateOutputSize() {
5858
assertTrue(outSize >= inLen);
5959
}
6060

61-
@Test
62-
public void decryptWithoutHeaders() {
63-
final byte[] out = new byte[1];
64-
final int returnedLen = blockDecryptionHandler_.doFinal(out, 0);
65-
assertEquals(0, returnedLen);
61+
@Test(expected= BadCiphertextException.class)
62+
public void doFinalCalledWhileNotComplete() {
63+
blockDecryptionHandler_.doFinal(new byte[1], 0);
6664
}
6765

6866
@Test(expected = AwsCryptoException.class)
@@ -90,4 +88,23 @@ public void decryptMaxContentLength() {
9088
final byte[] decryptedOut = new byte[decryptedOutLen];
9189
blockDecryptionHandler_.processBytes(outBuff.array(), 0, outBuff.array().length, decryptedOut, 0);
9290
}
93-
}
91+
92+
@Test(expected = AwsCryptoException.class)
93+
public void processBytesCalledWhileComplete() {
94+
final BlockEncryptionHandler blockEncryptionHandler = new BlockEncryptionHandler(
95+
dataKey_,
96+
nonceLen_,
97+
cryptoAlgorithm_,
98+
messageId_);
99+
final byte[] in = new byte[0];
100+
final int outLen = blockEncryptionHandler.estimateOutputSize(in.length);
101+
final byte[] out = new byte[outLen];
102+
103+
blockEncryptionHandler.processBytes(in, 0, in.length, out, 0);
104+
blockEncryptionHandler.doFinal(out, 0);
105+
106+
final byte[] decryptedOut = new byte[outLen];
107+
blockDecryptionHandler_.processBytes(out, 0, outLen, decryptedOut, 0);
108+
blockDecryptionHandler_.processBytes(out, 0, outLen, decryptedOut, 0);
109+
}
110+
}

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

+15
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.util.Collections;
1717
import java.util.Map;
1818

19+
import com.amazonaws.encryptionsdk.model.CiphertextHeaders;
1920
import com.amazonaws.encryptionsdk.keyrings.Keyring;
2021
import org.junit.Before;
2122
import org.junit.Test;
@@ -146,6 +147,20 @@ public void invalidOffsetProcessBytes() {
146147
decryptionHandler.processBytes(in, -1, in.length, out, 0);
147148
}
148149

150+
@Test(expected = BadCiphertextException.class)
151+
public void incompleteCiphertext() {
152+
byte[] ciphertext = getTestHeaders();
153+
154+
CiphertextHeaders h = new CiphertextHeaders();
155+
h.deserialize(ciphertext, 0);
156+
157+
final DecryptionHandler<StaticMasterKey> decryptionHandler = DecryptionHandler.create(masterKeyProvider_);
158+
final byte[] out = new byte[1];
159+
160+
decryptionHandler.processBytes(ciphertext, 0, ciphertext.length - 1, out, 0);
161+
decryptionHandler.doFinal(out, 0);
162+
}
163+
149164
@Test
150165
public void testNullMasterKey() {
151166
final DecryptionHandler decryptionHandler = DecryptionHandler.create(new DefaultCryptoMaterialsManager(keyring));

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

+26
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,30 @@ public void finalFrameLengthTooLarge() {
8989

9090
frameDecryptionHandler_.processBytes(in, 0, in.length, out, 0);
9191
}
92+
93+
@Test(expected = BadCiphertextException.class)
94+
public void doFinalCalledWhileNotComplete() {
95+
frameDecryptionHandler_.doFinal(new byte[1], 0);
96+
}
97+
98+
@Test(expected = AwsCryptoException.class)
99+
public void processBytesCalledWhileComplete() {
100+
final FrameEncryptionHandler frameEncryptionHandler = new FrameEncryptionHandler(
101+
dataKey_,
102+
nonceLen_,
103+
cryptoAlgorithm_,
104+
messageId_,
105+
frameSize_);
106+
final byte[] in = new byte[0];
107+
final int outLen = frameEncryptionHandler.estimateOutputSize(in.length);
108+
final byte[] out = new byte[outLen];
109+
110+
frameEncryptionHandler.processBytes(in, 0, in.length, out, 0);
111+
frameEncryptionHandler.doFinal(out, 0);
112+
113+
final byte[] decryptedOut = new byte[outLen];
114+
115+
frameDecryptionHandler_.processBytes(out, 0, out.length, decryptedOut, 0);
116+
frameDecryptionHandler_.processBytes(out, 0, out.length, decryptedOut, 0);
117+
}
92118
}

0 commit comments

Comments
 (0)