Skip to content

Commit 54557a9

Browse files
authored
chore: warn against use of Encryption Context for non-kms keyrings. (#173)
* chore: warn against use of Encryption Context. * add javadoc comment
1 parent a1a22a4 commit 54557a9

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

src/main/java/software/amazon/encryption/s3/materials/AesKeyring.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,13 @@ public EncryptionMaterials generateDataKey(EncryptionMaterials materials) {
9898
return defaultGenerateDataKey(materials);
9999
}
100100

101+
@Override
102+
public EncryptionMaterials modifyMaterials(EncryptionMaterials materials) {
103+
warnIfEncryptionContextIsPresent(materials);
104+
105+
return materials;
106+
}
107+
101108
@Override
102109
public byte[] encryptDataKey(SecureRandom secureRandom,
103110
EncryptionMaterials materials)

src/main/java/software/amazon/encryption/s3/materials/RsaKeyring.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@ public EncryptionMaterials generateDataKey(EncryptionMaterials materials) {
102102
return defaultGenerateDataKey(materials);
103103
}
104104

105+
@Override
106+
public EncryptionMaterials modifyMaterials(EncryptionMaterials materials) {
107+
warnIfEncryptionContextIsPresent(materials);
108+
109+
return materials;
110+
}
111+
105112
@Override
106113
public byte[] encryptDataKey(SecureRandom secureRandom,
107114
EncryptionMaterials materials) throws GeneralSecurityException {

src/main/java/software/amazon/encryption/s3/materials/S3Keyring.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package software.amazon.encryption.s3.materials;
44

55
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
6+
import software.amazon.encryption.s3.S3EncryptionClient;
67
import software.amazon.encryption.s3.S3EncryptionClientException;
78

89
import java.nio.charset.StandardCharsets;
@@ -13,6 +14,8 @@
1314
import java.util.Map;
1415
import javax.crypto.SecretKey;
1516

17+
import org.apache.commons.logging.LogFactory;
18+
1619
/**
1720
* This serves as the base class for all the keyrings in the S3 encryption client.
1821
* Shared functionality is all performed here.
@@ -124,6 +127,24 @@ public DecryptionMaterials onDecrypt(final DecryptionMaterials materials, List<E
124127

125128
abstract protected Map<String, DecryptDataKeyStrategy> decryptDataKeyStrategies();
126129

130+
/**
131+
* Checks if an encryption context is present in the EncryptionMaterials and issues a warning
132+
* if an encryption context is found.
133+
* <p>
134+
* Encryption context is not recommended for use with
135+
* non-KMS keyrings as it may not provide additional security benefits.
136+
*
137+
* @param materials EncryptionMaterials
138+
*/
139+
public void warnIfEncryptionContextIsPresent(EncryptionMaterials materials) {
140+
materials.s3Request().overrideConfiguration()
141+
.flatMap(overrideConfiguration ->
142+
overrideConfiguration.executionAttributes()
143+
.getOptionalAttribute(S3EncryptionClient.ENCRYPTION_CONTEXT))
144+
.ifPresent(ctx -> LogFactory.getLog(getClass()).warn("Usage of Encryption Context provides no security benefit in " + getClass().getSimpleName()));
145+
146+
}
147+
127148
abstract public static class Builder<KeyringT extends S3Keyring, BuilderT extends Builder<KeyringT, BuilderT>> {
128149
private boolean _enableLegacyWrappingAlgorithms = false;
129150
private SecureRandom _secureRandom;

0 commit comments

Comments
 (0)