16
16
import com .amazonaws .encryptionsdk .EncryptedDataKey ;
17
17
import com .amazonaws .encryptionsdk .exception .AwsCryptoException ;
18
18
import com .amazonaws .encryptionsdk .exception .CannotUnwrapDataKeyException ;
19
- import com .amazonaws .encryptionsdk .exception . MalformedArnException ;
19
+ import com .amazonaws .encryptionsdk .kms . AwsKmsCmkId ;
20
20
import com .amazonaws .encryptionsdk .kms .DataKeyEncryptionDao ;
21
21
import com .amazonaws .encryptionsdk .kms .DataKeyEncryptionDao .DecryptDataKeyResult ;
22
22
import com .amazonaws .encryptionsdk .kms .DataKeyEncryptionDao .GenerateDataKeyResult ;
23
- import com .amazonaws .encryptionsdk .kms .KmsUtils ;
24
23
import com .amazonaws .encryptionsdk .model .DecryptionMaterials ;
25
24
import com .amazonaws .encryptionsdk .model .EncryptionMaterials ;
26
25
import com .amazonaws .encryptionsdk .model .KeyBlob ;
31
30
import java .util .Set ;
32
31
33
32
import static com .amazonaws .encryptionsdk .EncryptedDataKey .PROVIDER_ENCODING ;
34
- import static com .amazonaws .encryptionsdk .kms . KmsUtils . KMS_PROVIDER_ID ;
35
- import static com .amazonaws .encryptionsdk .kms .KmsUtils . isArnWellFormed ;
33
+ import static com .amazonaws .encryptionsdk .internal . Constants . AWS_KMS_PROVIDER_ID ;
34
+ import static com .amazonaws .encryptionsdk .kms .AwsKmsCmkId . isKeyIdWellFormed ;
36
35
import static java .util .Collections .emptyList ;
37
36
import static java .util .Collections .unmodifiableList ;
38
37
import static java .util .Objects .requireNonNull ;
44
43
class AwsKmsKeyring implements Keyring {
45
44
46
45
private final DataKeyEncryptionDao dataKeyEncryptionDao ;
47
- private final List <String > keyIds ;
48
- private final String generatorKeyId ;
46
+ private final List <AwsKmsCmkId > keyIds ;
47
+ private final AwsKmsCmkId generatorKeyId ;
49
48
private final boolean isDiscovery ;
50
49
51
- AwsKmsKeyring (DataKeyEncryptionDao dataKeyEncryptionDao , List <String > keyIds , String generatorKeyId ) {
50
+ AwsKmsKeyring (DataKeyEncryptionDao dataKeyEncryptionDao , List <AwsKmsCmkId > keyIds , AwsKmsCmkId generatorKeyId ) {
52
51
requireNonNull (dataKeyEncryptionDao , "dataKeyEncryptionDao is required" );
53
52
this .dataKeyEncryptionDao = dataKeyEncryptionDao ;
54
53
this .keyIds = keyIds == null ? emptyList () : unmodifiableList (new ArrayList <>(keyIds ));
55
54
this .generatorKeyId = generatorKeyId ;
56
55
this .isDiscovery = this .generatorKeyId == null && this .keyIds .isEmpty ();
57
56
58
- if (!this .keyIds .stream ().allMatch (KmsUtils ::isArnWellFormed )) {
59
- throw new MalformedArnException ("keyIds must contain only CMK aliases and well formed ARNs" );
60
- }
61
-
62
- if (generatorKeyId != null ) {
63
- if (!isArnWellFormed (generatorKeyId )) {
64
- throw new MalformedArnException ("generatorKeyId must be either a CMK alias or a well formed ARN" );
65
- }
66
- if (this .keyIds .contains (generatorKeyId )) {
67
- throw new IllegalArgumentException ("KeyIds should not contain the generatorKeyId" );
68
- }
57
+ if (this .keyIds .contains (generatorKeyId )) {
58
+ throw new IllegalArgumentException ("KeyIds should not contain the generatorKeyId" );
69
59
}
70
60
}
71
61
@@ -86,7 +76,7 @@ public EncryptionMaterials onEncrypt(EncryptionMaterials encryptionMaterials) {
86
76
throw new AwsCryptoException ("Encryption materials must contain either a plaintext data key or a generator" );
87
77
}
88
78
89
- final List <String > keyIdsToEncrypt = new ArrayList <>(keyIds );
79
+ final List <AwsKmsCmkId > keyIdsToEncrypt = new ArrayList <>(keyIds );
90
80
91
81
// If the input encryption materials do not contain a plaintext data key and a generator is defined onEncrypt
92
82
// MUST attempt to generate a new plaintext data key and encrypt that data key by calling KMS GenerateDataKey.
@@ -100,7 +90,7 @@ public EncryptionMaterials onEncrypt(EncryptionMaterials encryptionMaterials) {
100
90
101
91
// Given a plaintext data key in the encryption materials, OnEncrypt MUST attempt
102
92
// to encrypt the plaintext data key using each CMK specified in it's key IDs list.
103
- for (String keyId : keyIdsToEncrypt ) {
93
+ for (AwsKmsCmkId keyId : keyIdsToEncrypt ) {
104
94
resultMaterials = encryptDataKey (keyId , resultMaterials );
105
95
}
106
96
@@ -113,17 +103,20 @@ private EncryptionMaterials generateDataKey(final EncryptionMaterials encryption
113
103
114
104
return encryptionMaterials
115
105
.withCleartextDataKey (result .getPlaintextDataKey (),
116
- new KeyringTraceEntry (KMS_PROVIDER_ID , generatorKeyId , KeyringTraceFlag .GENERATED_DATA_KEY ))
106
+ new KeyringTraceEntry (AWS_KMS_PROVIDER_ID , generatorKeyId .toString (),
107
+ KeyringTraceFlag .GENERATED_DATA_KEY ))
117
108
.withEncryptedDataKey (new KeyBlob (result .getEncryptedDataKey ()),
118
- new KeyringTraceEntry (KMS_PROVIDER_ID , generatorKeyId , KeyringTraceFlag .ENCRYPTED_DATA_KEY , KeyringTraceFlag .SIGNED_ENCRYPTION_CONTEXT ));
109
+ new KeyringTraceEntry (AWS_KMS_PROVIDER_ID , generatorKeyId .toString (),
110
+ KeyringTraceFlag .ENCRYPTED_DATA_KEY , KeyringTraceFlag .SIGNED_ENCRYPTION_CONTEXT ));
119
111
}
120
112
121
- private EncryptionMaterials encryptDataKey (final String keyId , final EncryptionMaterials encryptionMaterials ) {
113
+ private EncryptionMaterials encryptDataKey (final AwsKmsCmkId keyId , final EncryptionMaterials encryptionMaterials ) {
122
114
final EncryptedDataKey encryptedDataKey = dataKeyEncryptionDao .encryptDataKey (keyId ,
123
115
encryptionMaterials .getCleartextDataKey (), encryptionMaterials .getEncryptionContext ());
124
116
125
117
return encryptionMaterials .withEncryptedDataKey (new KeyBlob (encryptedDataKey ),
126
- new KeyringTraceEntry (KMS_PROVIDER_ID , keyId , KeyringTraceFlag .ENCRYPTED_DATA_KEY , KeyringTraceFlag .SIGNED_ENCRYPTION_CONTEXT ));
118
+ new KeyringTraceEntry (AWS_KMS_PROVIDER_ID , keyId .toString (),
119
+ KeyringTraceFlag .ENCRYPTED_DATA_KEY , KeyringTraceFlag .SIGNED_ENCRYPTION_CONTEXT ));
127
120
}
128
121
129
122
@ Override
@@ -135,7 +128,7 @@ public DecryptionMaterials onDecrypt(DecryptionMaterials decryptionMaterials, Li
135
128
return decryptionMaterials ;
136
129
}
137
130
138
- final Set <String > configuredKeyIds = new HashSet <>(keyIds );
131
+ final Set <AwsKmsCmkId > configuredKeyIds = new HashSet <>(keyIds );
139
132
140
133
if (generatorKeyId != null ) {
141
134
configuredKeyIds .add (generatorKeyId );
@@ -148,7 +141,7 @@ public DecryptionMaterials onDecrypt(DecryptionMaterials decryptionMaterials, Li
148
141
decryptionMaterials .getAlgorithm (), decryptionMaterials .getEncryptionContext ());
149
142
150
143
return decryptionMaterials .withCleartextDataKey (result .getPlaintextDataKey (),
151
- new KeyringTraceEntry (KMS_PROVIDER_ID , result .getKeyArn (),
144
+ new KeyringTraceEntry (AWS_KMS_PROVIDER_ID , result .getKeyArn (),
152
145
KeyringTraceFlag .DECRYPTED_DATA_KEY , KeyringTraceFlag .VERIFIED_ENCRYPTION_CONTEXT ));
153
146
} catch (CannotUnwrapDataKeyException e ) {
154
147
continue ;
@@ -159,14 +152,14 @@ public DecryptionMaterials onDecrypt(DecryptionMaterials decryptionMaterials, Li
159
152
return decryptionMaterials ;
160
153
}
161
154
162
- private boolean okToDecrypt (EncryptedDataKey encryptedDataKey , Set <String > configuredKeyIds ) {
155
+ private boolean okToDecrypt (EncryptedDataKey encryptedDataKey , Set <AwsKmsCmkId > configuredKeyIds ) {
163
156
// Only attempt to decrypt keys provided by KMS
164
- if (!encryptedDataKey .getProviderId ().equals (KMS_PROVIDER_ID )) {
157
+ if (!encryptedDataKey .getProviderId ().equals (AWS_KMS_PROVIDER_ID )) {
165
158
return false ;
166
159
}
167
160
168
- // If the key ARN cannot be parsed, skip it
169
- if (!isArnWellFormed (new String (encryptedDataKey .getProviderInformation (), PROVIDER_ENCODING )))
161
+ // If the key ID cannot be parsed, skip it
162
+ if (!isKeyIdWellFormed (new String (encryptedDataKey .getProviderInformation (), PROVIDER_ENCODING )))
170
163
{
171
164
return false ;
172
165
}
@@ -180,6 +173,7 @@ private boolean okToDecrypt(EncryptedDataKey encryptedDataKey, Set<String> confi
180
173
// OnDecrypt MUST attempt to decrypt each input encrypted data key in the input
181
174
// encrypted data key list where the key provider info has a value equal to one
182
175
// of the ARNs in this keyring's key IDs or the generator
183
- return configuredKeyIds .contains (new String (encryptedDataKey .getProviderInformation (), PROVIDER_ENCODING ));
176
+ return configuredKeyIds .contains (
177
+ AwsKmsCmkId .fromString (new String (encryptedDataKey .getProviderInformation (), PROVIDER_ENCODING )));
184
178
}
185
179
}
0 commit comments