Skip to content

Commit abfc8d9

Browse files
checkstyle?
1 parent 24ee4f4 commit abfc8d9

File tree

6 files changed

+58
-62
lines changed

6 files changed

+58
-62
lines changed

src/main/java/com/amazonaws/encryptionsdk/CMMHandler.java

+7-8
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import com.amazonaws.encryptionsdk.model.EncryptionMaterialsHandler;
1111
import com.amazonaws.encryptionsdk.model.EncryptionMaterialsRequest;
1212
import com.amazonaws.encryptionsdk.model.KeyBlob;
13-
1413
import java.nio.ByteBuffer;
1514
import java.util.ArrayList;
1615
import java.util.List;
@@ -65,10 +64,10 @@ private GetEncryptionMaterialsInput getEncryptionMaterialsRequestInput(
6564
public DecryptionMaterialsHandler decryptMaterials(
6665
DecryptionMaterialsRequest request, CommitmentPolicy commitmentPolicy) {
6766
if (cmm != null && mplCMM == null) {
68-
// This is an implementation of the legacy native CryptoMaterialsManager interface from ESDK-Java.
67+
// This is an implementation of the legacy native CryptoMaterialsManager interface from
68+
// ESDK-Java.
6969
DecryptionMaterials materials = cmm.decryptMaterials(request);
70-
if (materials.getEncryptionContext().isEmpty()
71-
&& !request.getEncryptionContext().isEmpty()) {
70+
if (materials.getEncryptionContext().isEmpty() && !request.getEncryptionContext().isEmpty()) {
7271
// If the request specified an encryption context,
7372
// and we are using the legacy native CMM,
7473
// add the encryptionContext to the materials.
@@ -82,7 +81,8 @@ public DecryptionMaterialsHandler decryptMaterials(
8281
// It now sets the encryptionContext attribute with the value from the ciphertext's headers.
8382
//
8483
// But custom CMMs' behavior was not updated.
85-
// However, there is no custom CMM before version 3.0 that could set an encryptionContext attribute.
84+
// However, there is no custom CMM before version 3.0 that could set an encryptionContext
85+
// attribute.
8686
// The encryptionContext attribute was only introduced to decryptMaterials objects
8787
// in ESDK 3.0, so no CMM could have configured this attribute before 3.0.
8888
// As a result, the ESDK assumes that any native CMM
@@ -91,9 +91,8 @@ public DecryptionMaterialsHandler decryptMaterials(
9191
//
9292
// If a custom CMM implementation conflicts with this assumption.
9393
// that CMM implementation MUST move to the MPL.
94-
materials = materials.toBuilder()
95-
.setEncryptionContext(request.getEncryptionContext())
96-
.build();
94+
materials =
95+
materials.toBuilder().setEncryptionContext(request.getEncryptionContext()).build();
9796
}
9897
return new DecryptionMaterialsHandler(materials);
9998
} else {

src/test/java/com/amazonaws/crypto/examples/v2/CustomCMMExampleTest.java

+6-9
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,21 @@
88
import com.amazonaws.encryptionsdk.kms.KmsMasterKeyProvider;
99
import org.junit.Test;
1010

11-
1211
public class CustomCMMExampleTest {
1312

1413
@Test
1514
public void testCustomCMMExample() {
16-
CryptoMaterialsManager cmm = new CustomCMMExample.SigningSuiteOnlyCMM(
17-
KmsMasterKeyProvider.builder().buildStrict(KMSTestFixtures.US_WEST_2_KEY_ID)
18-
);
15+
CryptoMaterialsManager cmm =
16+
new CustomCMMExample.SigningSuiteOnlyCMM(
17+
KmsMasterKeyProvider.builder().buildStrict(KMSTestFixtures.US_WEST_2_KEY_ID));
1918
CustomCMMExample.encryptAndDecryptWithCMM(cmm);
2019
}
2120

2221
@Test
2322
public void testV2Cmm() {
24-
V2DefaultCryptoMaterialsManager cmm = new V2DefaultCryptoMaterialsManager(
25-
KmsMasterKeyProvider.builder().buildStrict(KMSTestFixtures.US_WEST_2_KEY_ID)
26-
);
23+
V2DefaultCryptoMaterialsManager cmm =
24+
new V2DefaultCryptoMaterialsManager(
25+
KmsMasterKeyProvider.builder().buildStrict(KMSTestFixtures.US_WEST_2_KEY_ID));
2726
CustomCMMExample.encryptAndDecryptWithCMM(cmm);
2827
}
2928
}
30-
31-

src/test/java/com/amazonaws/crypto/examples/v2/V2DefaultCryptoMaterialsManager.java

+35-33
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,29 @@
2828
import static com.amazonaws.encryptionsdk.internal.Utils.assertNonNull;
2929

3030
/*
31-
This is a copy-paste of the DefaultCryptoMaterialsManager implementation
32-
from the final commit of the V2 ESDK: 1870a082358d59e32c60d74116d6f43c0efa466b
33-
ESDK V3 implicitly changed the contract between CMMs and the ESDK.
34-
After V3, DecryptMaterials has an `encryptionContext` attribute,
35-
and CMMs are expected to set this attribute.
36-
The V3 commit modified this DefaultCMM's `decryptMaterials` implementation
37-
to set encryptionContext on returned DecryptionMaterials objects.
38-
However, there are custom implementations of the legacy native CMM
39-
that do not set encryptionContext.
40-
This CMM is used to explicitly assert that the V2 implementation of
41-
the DefaultCMM is compatible with V3 logic,
42-
which implicitly asserts that custom implementations of V2-compatible CMMs
43-
are also compatible with V3 logic.
44-
*/
31+
This is a copy-paste of the DefaultCryptoMaterialsManager implementation
32+
from the final commit of the V2 ESDK: 1870a082358d59e32c60d74116d6f43c0efa466b
33+
ESDK V3 implicitly changed the contract between CMMs and the ESDK.
34+
After V3, DecryptMaterials has an `encryptionContext` attribute,
35+
and CMMs are expected to set this attribute.
36+
The V3 commit modified this DefaultCMM's `decryptMaterials` implementation
37+
to set encryptionContext on returned DecryptionMaterials objects.
38+
However, there are custom implementations of the legacy native CMM
39+
that do not set encryptionContext.
40+
This CMM is used to explicitly assert that the V2 implementation of
41+
the DefaultCMM is compatible with V3 logic,
42+
which implicitly asserts that custom implementations of V2-compatible CMMs
43+
are also compatible with V3 logic.
44+
*/
4545
public class V2DefaultCryptoMaterialsManager implements CryptoMaterialsManager {
4646
private final MasterKeyProvider<?> mkp;
4747

4848
private final CryptoAlgorithm DEFAULT_CRYPTO_ALGORITHM =
49-
CryptoAlgorithm.ALG_AES_256_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384;
49+
CryptoAlgorithm.ALG_AES_256_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384;
5050

51-
/** @param mkp The master key provider to delegate to */
51+
/**
52+
* @param mkp The master key provider to delegate to
53+
*/
5254
public V2DefaultCryptoMaterialsManager(MasterKeyProvider<?> mkp) {
5355
assertNonNull(mkp, "mkp");
5456
this.mkp = mkp;
@@ -73,7 +75,7 @@ public EncryptionMaterials getMaterialsForEncrypt(EncryptionMaterialsRequest req
7375
trailingKeys = generateTrailingSigKeyPair(algo);
7476
if (context.containsKey(Constants.EC_PUBLIC_KEY_FIELD)) {
7577
throw new IllegalArgumentException(
76-
"EncryptionContext contains reserved field " + Constants.EC_PUBLIC_KEY_FIELD);
78+
"EncryptionContext contains reserved field " + Constants.EC_PUBLIC_KEY_FIELD);
7779
}
7880
// make mutable
7981
context = new HashMap<>(context);
@@ -95,8 +97,8 @@ public EncryptionMaterials getMaterialsForEncrypt(EncryptionMaterialsRequest req
9597

9698
@SuppressWarnings("unchecked")
9799
final List<MasterKey> mks =
98-
(List<MasterKey>)
99-
assertNonNull(mkp, "provider").getMasterKeysForEncryption(mkRequestBuilder.build());
100+
(List<MasterKey>)
101+
assertNonNull(mkp, "provider").getMasterKeysForEncryption(mkRequestBuilder.build());
100102

101103
if (mks.isEmpty()) {
102104
throw new IllegalArgumentException("No master keys provided");
@@ -114,20 +116,20 @@ public EncryptionMaterials getMaterialsForEncrypt(EncryptionMaterialsRequest req
114116

115117
//noinspection unchecked
116118
return EncryptionMaterials.newBuilder()
117-
.setAlgorithm(algo)
118-
.setCleartextDataKey(dataKey.getKey())
119-
.setEncryptedDataKeys(keyBlobs)
120-
.setEncryptionContext(context)
121-
.setTrailingSignatureKey(trailingKeys == null ? null : trailingKeys.getPrivate())
122-
.setMasterKeys(mks)
123-
.build();
119+
.setAlgorithm(algo)
120+
.setCleartextDataKey(dataKey.getKey())
121+
.setEncryptedDataKeys(keyBlobs)
122+
.setEncryptionContext(context)
123+
.setTrailingSignatureKey(trailingKeys == null ? null : trailingKeys.getPrivate())
124+
.setMasterKeys(mks)
125+
.build();
124126
}
125127

126128
@Override
127129
public DecryptionMaterials decryptMaterials(DecryptionMaterialsRequest request) {
128130
DataKey<?> dataKey =
129-
mkp.decryptDataKey(
130-
request.getAlgorithm(), request.getEncryptedDataKeys(), request.getEncryptionContext());
131+
mkp.decryptDataKey(
132+
request.getAlgorithm(), request.getEncryptedDataKeys(), request.getEncryptionContext());
131133

132134
if (dataKey == null) {
133135
throw new CannotUnwrapDataKeyException("Could not decrypt any data keys");
@@ -151,9 +153,9 @@ public DecryptionMaterials decryptMaterials(DecryptionMaterialsRequest request)
151153
}
152154

153155
return DecryptionMaterials.newBuilder()
154-
.setDataKey(dataKey)
155-
.setTrailingSignatureKey(pubKey)
156-
.build();
156+
.setDataKey(dataKey)
157+
.setTrailingSignatureKey(pubKey)
158+
.build();
157159
}
158160

159161
private PublicKey deserializeTrailingKeyFromEc(CryptoAlgorithm algo, String pubKey) {
@@ -162,11 +164,11 @@ private PublicKey deserializeTrailingKeyFromEc(CryptoAlgorithm algo, String pubK
162164

163165
private static String serializeTrailingKeyForEc(CryptoAlgorithm algo, KeyPair trailingKeys) {
164166
return TrailingSignatureAlgorithm.forCryptoAlgorithm(algo)
165-
.serializePublicKey(trailingKeys.getPublic());
167+
.serializePublicKey(trailingKeys.getPublic());
166168
}
167169

168170
private static KeyPair generateTrailingSigKeyPair(CryptoAlgorithm algo)
169-
throws GeneralSecurityException {
171+
throws GeneralSecurityException {
170172
return TrailingSignatureAlgorithm.forCryptoAlgorithm(algo).generateKey();
171173
}
172174
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@
5050
import com.amazonaws.encryptionsdk.model.CipherBlockHeadersTest;
5151
import com.amazonaws.encryptionsdk.model.CipherFrameHeadersTest;
5252
import com.amazonaws.encryptionsdk.model.CiphertextHeadersTest;
53-
import com.amazonaws.encryptionsdk.model.DecryptionMaterialsTest;
5453
import com.amazonaws.encryptionsdk.model.DecryptionMaterialsRequestTest;
54+
import com.amazonaws.encryptionsdk.model.DecryptionMaterialsTest;
5555
import com.amazonaws.encryptionsdk.model.EncryptionMaterialsRequestTest;
5656
import com.amazonaws.encryptionsdk.model.KeyBlobTest;
5757
import com.amazonaws.encryptionsdk.multi.MultipleMasterKeyTest;

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

+6-7
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,22 @@
33

44
package com.amazonaws.encryptionsdk;
55

6+
import static org.junit.Assert.assertEquals;
7+
import static org.junit.Assert.assertTrue;
8+
import static org.mockito.Mockito.mock;
9+
import static org.mockito.Mockito.when;
10+
611
import com.amazonaws.encryptionsdk.model.DecryptionMaterials;
712
import com.amazonaws.encryptionsdk.model.DecryptionMaterialsHandler;
813
import com.amazonaws.encryptionsdk.model.DecryptionMaterialsRequest;
914
import com.amazonaws.encryptionsdk.model.KeyBlob;
10-
import org.junit.Test;
11-
1215
import java.security.PublicKey;
1316
import java.util.ArrayList;
1417
import java.util.Collections;
1518
import java.util.HashMap;
1619
import java.util.List;
1720
import java.util.Map;
18-
19-
import static org.junit.Assert.assertEquals;
20-
import static org.junit.Assert.assertTrue;
21-
import static org.mockito.Mockito.mock;
22-
import static org.mockito.Mockito.when;
21+
import org.junit.Test;
2322

2423
public class CMMHandlerTest {
2524

src/test/java/com/amazonaws/encryptionsdk/model/DecryptionMaterialsTest.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33

44
package com.amazonaws.encryptionsdk.model;
55

6-
import org.junit.Test;
6+
import static org.junit.Assert.assertEquals;
7+
import static org.mockito.Mockito.mock;
78

89
import java.util.Collections;
910
import java.util.Map;
10-
11-
import static org.junit.Assert.assertEquals;
12-
import static org.mockito.Mockito.mock;
11+
import org.junit.Test;
1312

1413
public class DecryptionMaterialsTest {
1514

0 commit comments

Comments
 (0)