Skip to content

Commit

Permalink
feat: Improvements to the message decryption process (#251)
Browse files Browse the repository at this point in the history
  • Loading branch information
texastony committed May 27, 2021
1 parent 0819547 commit 0ce0cb2
Show file tree
Hide file tree
Showing 32 changed files with 1,406 additions and 121 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "src/test/resources/aws-encryption-sdk-test-vectors"]
path = src/test/resources/aws-encryption-sdk-test-vectors
url = https://github.com/awslabs/private-aws-encryption-sdk-test-vectors-staging.git
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## 2.2.0 -- 2021-05-27

* feat: Improvements to the message decryption process.

See https://github.com/aws/aws-encryption-sdk-java/security/advisories/GHSA-55xh-53m6-936r


## 2.0.0 -- 2020-09-24

* feat!: Updates to the AWS Encryption SDK. 4678ffa
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ You can get the latest release from Maven:
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-encryption-sdk-java</artifactId>
<version>2.0.0</version>
<version>2.2.0</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion codebuild/corretto11.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ phases:
java: corretto11
build:
commands:
- 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'
- 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"
2 changes: 1 addition & 1 deletion codebuild/corretto8.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ phases:
java: corretto8
build:
commands:
- 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'
- 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"
2 changes: 1 addition & 1 deletion codebuild/openjdk11.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ phases:
java: openjdk11
build:
commands:
- 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'
- 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"
2 changes: 1 addition & 1 deletion codebuild/openjdk8.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ phases:
java: openjdk8
build:
commands:
- 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'
- 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"
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.amazonaws</groupId>
<artifactId>aws-encryption-sdk-java</artifactId>
<version>2.0.0</version>
<version>2.2.0</version>
<packaging>jar</packaging>

<name>aws-encryption-sdk-java</name>
Expand Down Expand Up @@ -60,9 +60,9 @@
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.7.1</version>
<scope>test</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

package com.amazonaws.crypto.examples;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.GeneralSecurityException;
Expand Down Expand Up @@ -138,10 +140,16 @@ private static void standardDecrypt(final String kmsArn, final String fileName)
// use an encryption context. For an example, see the other SDK samples.
final FileInputStream in = new FileInputStream(fileName + ".encrypted");
final FileOutputStream out = new FileOutputStream(fileName + ".decrypted");
final CryptoOutputStream<?> decryptingStream = crypto.createDecryptingStream(provider, out);
// Since we are using a signing algorithm suite, we avoid streaming decryption directly to the output file,
// to ensure that the trailing signature is verified before writing any untrusted plaintext to disk.
final ByteArrayOutputStream plaintextBuffer = new ByteArrayOutputStream();
final CryptoOutputStream<?> decryptingStream = crypto.createDecryptingStream(provider, plaintextBuffer);
IOUtils.copy(in, decryptingStream);
in.close();
decryptingStream.close();
final ByteArrayInputStream plaintextReader = new ByteArrayInputStream(plaintextBuffer.toByteArray());
IOUtils.copy(plaintextReader, out);
out.close();
}

private static void escrowDecrypt(final String fileName) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import javax.crypto.spec.SecretKeySpec;

import com.amazonaws.encryptionsdk.AwsCrypto;
import com.amazonaws.encryptionsdk.CryptoAlgorithm;
import com.amazonaws.encryptionsdk.CryptoInputStream;
import com.amazonaws.encryptionsdk.MasterKey;
import com.amazonaws.encryptionsdk.jce.JceMasterKey;
Expand Down Expand Up @@ -53,8 +54,11 @@ public static void main(String[] args) throws IOException {
// that this client will only decrypt encrypted messages that were created with a committing algorithm suite.
// This is the default commitment policy if you build the client with `AwsCrypto.builder().build()`
// or `AwsCrypto.standard()`.
// This also chooses to encrypt with an algorithm suite that doesn't include signing for faster decryption,
// since this use case assumes that the contexts that encrypt and decrypt are equally trusted.
final AwsCrypto crypto = AwsCrypto.builder()
.withCommitmentPolicy(CommitmentPolicy.RequireEncryptRequireDecrypt)
.withEncryptionAlgorithm(CryptoAlgorithm.ALG_AES_256_GCM_HKDF_SHA512_COMMIT_KEY)
.build();

// Create an encryption context to identify this ciphertext
Expand All @@ -71,14 +75,16 @@ public static void main(String[] args) throws IOException {
out.close();

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

// Return the plaintext data
// Write the plaintext data to disk.
out = new FileOutputStream(srcFile + ".decrypted");
IOUtils.copy(decryptingStream, out);
decryptingStream.close();
Expand Down

0 comments on commit 0ce0cb2

Please sign in to comment.