Skip to content

Commit d59c11c

Browse files
texastonyrobin-aws
andauthored
feat: Improvements to the message decryption process (#250)
See GHSA-55xh-53m6-936r Co-authored-by: Robin Salkeld <[email protected]>
1 parent 44b26ce commit d59c11c

31 files changed

+1444
-109
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "src/test/resources/aws-encryption-sdk-test-vectors"]
2+
path = src/test/resources/aws-encryption-sdk-test-vectors
3+
url = https://github.com/awslabs/private-aws-encryption-sdk-test-vectors-staging.git

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## 1.9.0 -- 2021-05-27
4+
5+
* feat: Improvements to the message decryption process.
6+
7+
See https://github.com/aws/aws-encryption-sdk-java/security/advisories/GHSA-55xh-53m6-936r
8+
39
## 1.7.0 -- 2020-09-24
410

511
* feat: Updates to the AWS Encryption SDK. bdb31dc

README.md

Lines changed: 1 addition & 1 deletion
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.7.0</version>
59+
<version>1.9.0</version>
6060
</dependency>
6161
```
6262

codebuild/corretto11.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ phases:
66
java: corretto11
77
build:
88
commands:
9-
- mvn install -Dgpg.skip=true '-DtestVectorZip=https://github.com/awslabs/aws-encryption-sdk-test-vectors/raw/master/vectors/awses-decrypt/python-1.3.8.zip'
9+
- mvn install -Dgpg.skip=true "-DtestVectorZip=file://$CODEBUILD_SRC_DIR/src/test/resources/aws-encryption-sdk-test-vectors/vectors/awses-decrypt/python-2.2.0.zip"

codebuild/corretto8.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ phases:
66
java: corretto8
77
build:
88
commands:
9-
- mvn install -Dgpg.skip=true '-DtestVectorZip=https://github.com/awslabs/aws-encryption-sdk-test-vectors/raw/master/vectors/awses-decrypt/python-1.3.8.zip'
9+
- mvn install -Dgpg.skip=true "-DtestVectorZip=file://$CODEBUILD_SRC_DIR/src/test/resources/aws-encryption-sdk-test-vectors/vectors/awses-decrypt/python-2.2.0.zip"

codebuild/openjdk11.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ phases:
66
java: openjdk11
77
build:
88
commands:
9-
- mvn install -Dgpg.skip=true '-DtestVectorZip=https://github.com/awslabs/aws-encryption-sdk-test-vectors/raw/master/vectors/awses-decrypt/python-1.3.8.zip'
9+
- mvn install -Dgpg.skip=true "-DtestVectorZip=file://$CODEBUILD_SRC_DIR/src/test/resources/aws-encryption-sdk-test-vectors/vectors/awses-decrypt/python-2.2.0.zip"

codebuild/openjdk8.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ phases:
66
java: openjdk8
77
build:
88
commands:
9-
- mvn install -Dgpg.skip=true '-DtestVectorZip=https://github.com/awslabs/aws-encryption-sdk-test-vectors/raw/master/vectors/awses-decrypt/python-1.3.8.zip'
9+
- mvn install -Dgpg.skip=true "-DtestVectorZip=file://$CODEBUILD_SRC_DIR/src/test/resources/aws-encryption-sdk-test-vectors/vectors/awses-decrypt/python-2.2.0.zip"

pom.xml

Lines changed: 4 additions & 4 deletions
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.7.0</version>
7+
<version>1.9.0</version>
88
<packaging>jar</packaging>
99

1010
<name>aws-encryption-sdk-java</name>
@@ -60,9 +60,9 @@
6060
</dependency>
6161

6262
<dependency>
63-
<groupId>junit</groupId>
64-
<artifactId>junit</artifactId>
65-
<version>4.13.1</version>
63+
<groupId>org.junit.vintage</groupId>
64+
<artifactId>junit-vintage-engine</artifactId>
65+
<version>5.7.1</version>
6666
<scope>test</scope>
6767
</dependency>
6868

src/examples/java/com/amazonaws/crypto/examples/EscrowedEncryptExample.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
package com.amazonaws.crypto.examples;
55

6+
import java.io.ByteArrayInputStream;
7+
import java.io.ByteArrayOutputStream;
68
import java.io.FileInputStream;
79
import java.io.FileOutputStream;
810
import java.security.GeneralSecurityException;
@@ -125,10 +127,16 @@ private static void standardDecrypt(final String kmsArn, final String fileName)
125127
// use an encryption context. For an example, see the other SDK samples.
126128
final FileInputStream in = new FileInputStream(fileName + ".encrypted");
127129
final FileOutputStream out = new FileOutputStream(fileName + ".decrypted");
128-
final CryptoOutputStream<?> decryptingStream = crypto.createDecryptingStream(provider, out);
130+
// Since we are using a signing algorithm suite, we avoid streaming decryption directly to the output file,
131+
// to ensure that the trailing signature is verified before writing any untrusted plaintext to disk.
132+
final ByteArrayOutputStream plaintextBuffer = new ByteArrayOutputStream();
133+
final CryptoOutputStream<?> decryptingStream = crypto.createDecryptingStream(provider, plaintextBuffer);
129134
IOUtils.copy(in, decryptingStream);
130135
in.close();
131136
decryptingStream.close();
137+
final ByteArrayInputStream plaintextReader = new ByteArrayInputStream(plaintextBuffer.toByteArray());
138+
IOUtils.copy(plaintextReader, out);
139+
out.close();
132140
}
133141

134142
private static void escrowDecrypt(final String fileName) throws Exception {

src/examples/java/com/amazonaws/crypto/examples/FileStreamingExample.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import javax.crypto.spec.SecretKeySpec;
1515

1616
import com.amazonaws.encryptionsdk.AwsCrypto;
17+
import com.amazonaws.encryptionsdk.CryptoAlgorithm;
1718
import com.amazonaws.encryptionsdk.CryptoInputStream;
1819
import com.amazonaws.encryptionsdk.MasterKey;
1920
import com.amazonaws.encryptionsdk.jce.JceMasterKey;
@@ -49,7 +50,12 @@ public static void main(String[] args) throws IOException {
4950

5051
// Instantiate the SDK with a specific commitment policy.
5152
// ForbidEncryptAllowDecrypt is the only available policy in 1.7.0.
52-
final AwsCrypto crypto = AwsCrypto.builder().withCommitmentPolicy(CommitmentPolicy.ForbidEncryptAllowDecrypt).build();
53+
// This also chooses to encrypt with an algorithm suite that doesn't include signing for faster decryption,
54+
// since this use case assumes that the contexts that encrypt and decrypt are equally trusted.
55+
final AwsCrypto crypto = AwsCrypto.builder()
56+
.withCommitmentPolicy(CommitmentPolicy.ForbidEncryptAllowDecrypt)
57+
.withEncryptionAlgorithm(CryptoAlgorithm.ALG_AES_256_GCM_HKDF_SHA512_COMMIT_KEY)
58+
.build();
5359

5460
// Create an encryption context to identify this ciphertext
5561
Map<String, String> context = Collections.singletonMap("Example", "FileStreaming");
@@ -65,14 +71,16 @@ public static void main(String[] args) throws IOException {
6571
out.close();
6672

6773
// Decrypt the file. Verify the encryption context before returning the plaintext.
74+
// Since we encrypted using an unsigned algorithm suite, we can use the recommended
75+
// createUnsignedMessageDecryptingStream method that only accepts unsigned messages.
6876
in = new FileInputStream(srcFile + ".encrypted");
69-
CryptoInputStream<JceMasterKey> decryptingStream = crypto.createDecryptingStream(masterKey, in);
77+
CryptoInputStream<JceMasterKey> decryptingStream = crypto.createUnsignedMessageDecryptingStream(masterKey, in);
7078
// Does it contain the expected encryption context?
7179
if (!"FileStreaming".equals(decryptingStream.getCryptoResult().getEncryptionContext().get("Example"))) {
7280
throw new IllegalStateException("Bad encryption context");
7381
}
7482

75-
// Return the plaintext data
83+
// Write the plaintext data to disk.
7684
out = new FileOutputStream(srcFile + ".decrypted");
7785
IOUtils.copy(decryptingStream, out);
7886
decryptingStream.close();

0 commit comments

Comments
 (0)