-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathEncryptionMaterials.java
More file actions
197 lines (166 loc) · 6.84 KB
/
Copy pathEncryptionMaterials.java
File metadata and controls
197 lines (166 loc) · 6.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package software.amazon.encryption.s3.materials;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import software.amazon.awssdk.services.s3.model.S3Request;
import software.amazon.encryption.s3.S3EncryptionClientException;
import software.amazon.encryption.s3.algorithms.AlgorithmSuite;
import software.amazon.encryption.s3.internal.CipherMode;
import software.amazon.encryption.s3.internal.CipherProvider;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.Provider;
import java.util.Collections;
import java.util.List;
import java.util.Map;
final public class EncryptionMaterials implements CryptographicMaterials {
// Original request
private final S3Request _s3Request;
// Identifies what sort of crypto algorithms we want to use
private final AlgorithmSuite _algorithmSuite;
// Additional information passed into encrypted that is required on decryption as well
// Should NOT contain sensitive information
private final Map<String, String> _encryptionContext;
private final MaterialsDescription _materialsDescription;
private final List<EncryptedDataKey> _encryptedDataKeys;
private final byte[] _plaintextDataKey;
private final Provider _cryptoProvider;
private final long _plaintextLength;
private final long _ciphertextLength;
private EncryptionMaterials(Builder builder) {
this._s3Request = builder._s3Request;
this._algorithmSuite = builder._algorithmSuite;
this._encryptionContext = builder._encryptionContext;
this._encryptedDataKeys = builder._encryptedDataKeys;
this._plaintextDataKey = builder._plaintextDataKey;
this._cryptoProvider = builder._cryptoProvider;
this._plaintextLength = builder._plaintextLength;
this._ciphertextLength = _plaintextLength + _algorithmSuite.cipherTagLengthBytes();
this._materialsDescription = builder._materialsDescription;
}
static public Builder builder() {
return new Builder();
}
public S3Request s3Request() {
return _s3Request;
}
public AlgorithmSuite algorithmSuite() {
return _algorithmSuite;
}
/**
* Note that the underlying implementation uses a Collections.unmodifiableMap which is
* immutable.
*/
@Override
@SuppressFBWarnings(value = "EI_EXPOSE_REP", justification = "False positive; underlying"
+ " implementation is immutable")
public Map<String, String> encryptionContext() {
return _encryptionContext;
}
/**
* Note that the underlying implementation uses a Collections.unmodifiableList which is
* immutable.
*/
@SuppressFBWarnings(value = "EI_EXPOSE_REP", justification = "False positive; underlying"
+ " implementation is immutable")
public List<EncryptedDataKey> encryptedDataKeys() {
return _encryptedDataKeys;
}
public byte[] plaintextDataKey() {
if (_plaintextDataKey == null) {
return null;
}
return _plaintextDataKey.clone();
}
public long getPlaintextLength() {
return _plaintextLength;
}
public long getCiphertextLength() {
return _ciphertextLength;
}
public SecretKey dataKey() {
return new SecretKeySpec(_plaintextDataKey, algorithmSuite().dataKeyAlgorithm());
}
public Provider cryptoProvider() {
return _cryptoProvider;
}
public MaterialsDescription materialsDescription() {
return _materialsDescription;
}
@Override
public CipherMode cipherMode() {
return CipherMode.ENCRYPT;
}
@Override
public Cipher getCipher(byte[] iv) {
return CipherProvider.createAndInitCipher(this, iv);
}
public Builder toBuilder() {
return new Builder()
.s3Request(_s3Request)
.algorithmSuite(_algorithmSuite)
.encryptionContext(_encryptionContext)
.encryptedDataKeys(_encryptedDataKeys)
.plaintextDataKey(_plaintextDataKey)
.cryptoProvider(_cryptoProvider)
.materialsDescription(_materialsDescription)
.plaintextLength(_plaintextLength);
}
static public class Builder {
private S3Request _s3Request = null;
private AlgorithmSuite _algorithmSuite = AlgorithmSuite.ALG_AES_256_GCM_IV12_TAG16_NO_KDF;
private Map<String, String> _encryptionContext = Collections.emptyMap();
private List<EncryptedDataKey> _encryptedDataKeys = Collections.emptyList();
private byte[] _plaintextDataKey = null;
private long _plaintextLength = -1;
private Provider _cryptoProvider = null;
private MaterialsDescription _materialsDescription = MaterialsDescription.builder().build();
private Builder() {
}
public Builder s3Request(S3Request s3Request) {
_s3Request = s3Request;
return this;
}
public Builder algorithmSuite(AlgorithmSuite algorithmSuite) {
_algorithmSuite = algorithmSuite;
return this;
}
public Builder materialsDescription(MaterialsDescription materialsDescription) {
_materialsDescription = materialsDescription == null
? MaterialsDescription.builder().build()
: materialsDescription;
return this;
}
public Builder encryptionContext(Map<String, String> encryptionContext) {
_encryptionContext = encryptionContext == null
? Collections.emptyMap()
: Collections.unmodifiableMap(encryptionContext);
return this;
}
public Builder encryptedDataKeys(List<EncryptedDataKey> encryptedDataKeys) {
_encryptedDataKeys = encryptedDataKeys == null
? Collections.emptyList()
: Collections.unmodifiableList(encryptedDataKeys);
return this;
}
public Builder plaintextDataKey(byte[] plaintextDataKey) {
_plaintextDataKey = plaintextDataKey == null ? null : plaintextDataKey.clone();
return this;
}
public Builder cryptoProvider(Provider cryptoProvider) {
_cryptoProvider = cryptoProvider;
return this;
}
public Builder plaintextLength(long plaintextLength) {
_plaintextLength = plaintextLength;
return this;
}
public EncryptionMaterials build() {
if (!_materialsDescription.isEmpty() && !_encryptionContext.isEmpty()) {
throw new S3EncryptionClientException("MaterialsDescription and EncryptionContext cannot both be set!");
}
return new EncryptionMaterials(this);
}
}
}