Skip to content

Commit 9307933

Browse files
Create keyring trace and add to encryption and decryption materials. (#134)
* Create keyring trace and add to encryption and decryption materials. *Issue #, if available:* #102 *Description of changes:* Creating a keyring trace and adding to encryption and decryption materials to allow for auditing actions a keyring has taken on encryption materials. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license. # Check any applicable: - [ ] Were any files moved? Moving files changes their URL, which breaks all hyperlinks to the files.
1 parent 3ef8958 commit 9307933

File tree

6 files changed

+315
-2
lines changed

6 files changed

+315
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except
5+
* in compliance with the License. A copy of the License is located at
6+
*
7+
* http://aws.amazon.com/apache2.0
8+
*
9+
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
14+
package com.amazonaws.encryptionsdk.keyrings;
15+
16+
import org.apache.commons.lang3.builder.ToStringBuilder;
17+
import org.apache.commons.lang3.builder.ToStringStyle;
18+
19+
import java.util.ArrayList;
20+
import java.util.Arrays;
21+
import java.util.Collections;
22+
import java.util.HashSet;
23+
import java.util.List;
24+
25+
/**
26+
* A keyring trace containing all of the actions that keyrings have taken on a set of encryption materials.
27+
*/
28+
public class KeyringTrace {
29+
30+
private final List<KeyringTraceEntry> entries = new ArrayList<>();
31+
32+
/**
33+
* Add a new entry to the keyring trace.
34+
*
35+
* @param keyNamespace The namespace for the key.
36+
* @param keyName The name of the key.
37+
* @param flags A set of one or more KeyringTraceFlag enums
38+
* indicating what actions were taken by a keyring.
39+
*/
40+
public void add(String keyNamespace, String keyName, KeyringTraceFlag... flags) {
41+
entries.add(new KeyringTraceEntry(keyNamespace, keyName,
42+
new HashSet<>(Arrays.asList(flags))));
43+
}
44+
45+
/**
46+
* Gets an unmodifiable list of `KeyringTraceEntry`s ordered sequentially
47+
* according to the order the actions were taken, with the earliest action
48+
* corresponding to the first `KeyringTraceEntry` in the list.
49+
*
50+
* @return An unmodifiable list of `KeyringTraceEntry`s
51+
*/
52+
public List<KeyringTraceEntry> getEntries() {
53+
return Collections.unmodifiableList(entries);
54+
}
55+
56+
@Override
57+
public String toString() {
58+
return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
59+
.append("entries", entries)
60+
.toString();
61+
}
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except
5+
* in compliance with the License. A copy of the License is located at
6+
*
7+
* http://aws.amazon.com/apache2.0
8+
*
9+
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
14+
package com.amazonaws.encryptionsdk.keyrings;
15+
16+
import org.apache.commons.lang3.builder.ToStringBuilder;
17+
import org.apache.commons.lang3.builder.ToStringStyle;
18+
19+
import java.util.Collections;
20+
import java.util.Objects;
21+
import java.util.Set;
22+
23+
import static org.apache.commons.lang3.Validate.notBlank;
24+
import static org.apache.commons.lang3.Validate.notEmpty;
25+
26+
/**
27+
* A representation of an action that a keyring has taken on a data key.
28+
*/
29+
public class KeyringTraceEntry {
30+
31+
private final String keyNamespace;
32+
private final String keyName;
33+
private final Set<KeyringTraceFlag> flags;
34+
35+
/**
36+
* Constructs a new `KeyringTraceEntry`.
37+
*
38+
* @param keyNamespace The namespace for the key.
39+
* @param keyName The name of the key.
40+
* @param flags A set of one or more KeyringTraceFlag enums
41+
* indicating what actions were taken by a keyring.
42+
*/
43+
KeyringTraceEntry(final String keyNamespace, final String keyName, final Set<KeyringTraceFlag> flags) {
44+
notBlank(keyNamespace, "keyNamespace is required");
45+
notBlank(keyName, "keyName is required");
46+
notEmpty(flags, "At least one flag is required");
47+
48+
this.keyNamespace = keyNamespace;
49+
this.keyName = keyName;
50+
this.flags = Collections.unmodifiableSet(flags);
51+
}
52+
53+
/**
54+
* Returns the key namespace.
55+
*
56+
* @return The key namespace.
57+
*/
58+
public String getKeyNamespace() {
59+
return this.keyNamespace;
60+
}
61+
62+
/**
63+
* Returns the key name.
64+
*
65+
* @return The key name.
66+
*/
67+
public String getKeyName() {
68+
return this.keyName;
69+
}
70+
71+
/**
72+
* Returns an unmodifiable set of flags that indicate
73+
* which actions were taken by a keyring.
74+
*
75+
* @return The unmodifiable set of flags.
76+
*/
77+
public Set<KeyringTraceFlag> getFlags() {
78+
return this.flags;
79+
}
80+
81+
@Override
82+
public boolean equals(Object o) {
83+
if (this == o) return true;
84+
if (o == null || getClass() != o.getClass()) return false;
85+
KeyringTraceEntry that = (KeyringTraceEntry) o;
86+
return Objects.equals(keyNamespace, that.keyNamespace) &&
87+
Objects.equals(keyName, that.keyName) &&
88+
Objects.equals(flags, that.flags);
89+
}
90+
91+
@Override
92+
public int hashCode() {
93+
return Objects.hash(keyNamespace, keyName, flags);
94+
}
95+
96+
@Override
97+
public String toString() {
98+
return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
99+
.append("keyNamespace", this.keyNamespace)
100+
.append("keyName", this.keyName)
101+
.append("flags", this.flags)
102+
.toString();
103+
}
104+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except
5+
* in compliance with the License. A copy of the License is located at
6+
*
7+
* http://aws.amazon.com/apache2.0
8+
*
9+
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
14+
package com.amazonaws.encryptionsdk.keyrings;
15+
16+
/**
17+
* Enum representing the possible actions a keyring may take on the
18+
* different wrapping keys it manages.
19+
*/
20+
public enum KeyringTraceFlag {
21+
22+
/**
23+
* A flag to represent that a keyring has generated a plaintext data key.
24+
*/
25+
GENERATED_DATA_KEY,
26+
27+
/**
28+
* A flag to represent that a keyring has created an encrypted data key.
29+
*/
30+
ENCRYPTED_DATA_KEY,
31+
32+
/**
33+
* A flag to represent that a keyring has obtained the
34+
* corresponding plaintext data key from an encrypted data key.
35+
*/
36+
DECRYPTED_DATA_KEY,
37+
38+
/**
39+
* A flag to represent that the keyring has cryptographically
40+
* bound the encryption context to a newly created encrypted data key.
41+
*/
42+
SIGNED_ENCRYPTION_CONTEXT,
43+
44+
/**
45+
* A flag to represent that the keyring has verified that an encrypted
46+
* data key was originally created with a particular encryption context.
47+
*/
48+
VERIFIED_ENCRYPTION_CONTEXT
49+
}

src/main/java/com/amazonaws/encryptionsdk/model/DecryptionMaterials.java

+18
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33
import java.security.PublicKey;
44

55
import com.amazonaws.encryptionsdk.DataKey;
6+
import com.amazonaws.encryptionsdk.keyrings.KeyringTrace;
67

78
public final class DecryptionMaterials {
89
private final DataKey<?> dataKey;
910
private final PublicKey trailingSignatureKey;
11+
private final KeyringTrace keyringTrace;
1012

1113
private DecryptionMaterials(Builder b) {
1214
dataKey = b.getDataKey();
1315
trailingSignatureKey = b.getTrailingSignatureKey();
16+
keyringTrace = b.getKeyringTrace();
1417
}
1518

1619
public DataKey<?> getDataKey() {
@@ -21,6 +24,10 @@ public PublicKey getTrailingSignatureKey() {
2124
return trailingSignatureKey;
2225
}
2326

27+
public KeyringTrace getKeyringTrace() {
28+
return keyringTrace;
29+
}
30+
2431
public static Builder newBuilder() {
2532
return new Builder();
2633
}
@@ -32,10 +39,12 @@ public Builder toBuilder() {
3239
public static final class Builder {
3340
private DataKey<?> dataKey;
3441
private PublicKey trailingSignatureKey;
42+
private KeyringTrace keyringTrace;
3543

3644
private Builder(DecryptionMaterials result) {
3745
this.dataKey = result.getDataKey();
3846
this.trailingSignatureKey = result.getTrailingSignatureKey();
47+
this.keyringTrace = result.getKeyringTrace();
3948
}
4049

4150
private Builder() {}
@@ -58,6 +67,15 @@ public Builder setTrailingSignatureKey(PublicKey trailingSignatureKey) {
5867
return this;
5968
}
6069

70+
public KeyringTrace getKeyringTrace() {
71+
return keyringTrace;
72+
}
73+
74+
public Builder setKeyringTrace(KeyringTrace keyringTrace) {
75+
this.keyringTrace = keyringTrace;
76+
return this;
77+
}
78+
6179
public DecryptionMaterials build() {
6280
return new DecryptionMaterials(this);
6381
}

src/main/java/com/amazonaws/encryptionsdk/model/EncryptionMaterials.java

+17-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import com.amazonaws.encryptionsdk.CryptoAlgorithm;
1313
import com.amazonaws.encryptionsdk.MasterKey;
14+
import com.amazonaws.encryptionsdk.keyrings.KeyringTrace;
1415

1516
/**
1617
* Contains the cryptographic materials needed for an encryption operation.
@@ -24,6 +25,7 @@ public final class EncryptionMaterials {
2425
private final SecretKey cleartextDataKey;
2526
private final PrivateKey trailingSignatureKey;
2627
private final List<MasterKey> masterKeys;
28+
private final KeyringTrace keyringTrace;
2729

2830
private EncryptionMaterials(Builder b) {
2931
this.algorithm = b.algorithm;
@@ -32,6 +34,7 @@ private EncryptionMaterials(Builder b) {
3234
this.cleartextDataKey = b.cleartextDataKey;
3335
this.trailingSignatureKey = b.trailingSignatureKey;
3436
this.masterKeys = b.getMasterKeys();
37+
this.keyringTrace = b.keyringTrace;
3538
}
3639

3740
public Builder toBuilder() {
@@ -100,12 +103,13 @@ public List<MasterKey> getMasterKeys() {
100103
Objects.equals(encryptedDataKeys, that.encryptedDataKeys) &&
101104
Objects.equals(cleartextDataKey, that.cleartextDataKey) &&
102105
Objects.equals(trailingSignatureKey, that.trailingSignatureKey) &&
103-
Objects.equals(masterKeys, that.masterKeys);
106+
Objects.equals(masterKeys, that.masterKeys) &&
107+
Objects.equals(keyringTrace, that.keyringTrace);
104108
}
105109

106110
@Override public int hashCode() {
107111
return Objects.hash(algorithm, encryptionContext, encryptedDataKeys, cleartextDataKey, trailingSignatureKey,
108-
masterKeys);
112+
masterKeys, keyringTrace);
109113
}
110114

111115
public static class Builder {
@@ -115,6 +119,7 @@ public static class Builder {
115119
private SecretKey cleartextDataKey;
116120
private PrivateKey trailingSignatureKey;
117121
private List<MasterKey> masterKeys = Collections.emptyList();
122+
private KeyringTrace keyringTrace;
118123

119124
private Builder() {}
120125

@@ -125,6 +130,7 @@ private Builder(EncryptionMaterials r) {
125130
cleartextDataKey = r.cleartextDataKey;
126131
trailingSignatureKey = r.trailingSignatureKey;
127132
setMasterKeys(r.masterKeys);
133+
keyringTrace = r.keyringTrace;
128134
}
129135

130136
public EncryptionMaterials build() {
@@ -184,5 +190,14 @@ public Builder setMasterKeys(List<MasterKey> masterKeys) {
184190
this.masterKeys = Collections.unmodifiableList(new ArrayList<>(masterKeys));
185191
return this;
186192
}
193+
194+
public KeyringTrace getKeyringTrace() {
195+
return keyringTrace;
196+
}
197+
198+
public Builder setKeyringTrace(KeyringTrace keyringTrace) {
199+
this.keyringTrace = keyringTrace;
200+
return this;
201+
}
187202
}
188203
}

0 commit comments

Comments
 (0)