-
Notifications
You must be signed in to change notification settings - Fork 0
/
InMemoryCryptoService.java
226 lines (187 loc) · 7.99 KB
/
InMemoryCryptoService.java
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package org.fuin.dddcqrsunit;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import org.fuin.ddd4j.ddd.DecryptionFailedException;
import org.fuin.ddd4j.ddd.DuplicateEncryptionKeyIdException;
import org.fuin.ddd4j.ddd.EncryptedData;
import org.fuin.ddd4j.ddd.EncryptedDataService;
import org.fuin.ddd4j.ddd.EncryptionKeyIdUnknownException;
import org.fuin.ddd4j.ddd.EncryptionKeyVersionUnknownException;
import org.fuin.utils4j.Utils4J;
/**
* Simple in-memory crypto service for testing purposes.
*/
public final class InMemoryCryptoService implements EncryptedDataService {
private static final String ALGORITHM = "AES";
private static final int KEY_SIZE = 256;
private static final int GCM_IV_LENGTH = 12;
private static final int GCM_TAG_LENGTH = 16;
private static final String CIPHER_NAME = "AES/GCM/NoPadding";
private Set<String> keyIds;
private Map<String, Map<Integer, Key>> keys;
/**
* Default constructor.
*/
public InMemoryCryptoService() {
super();
this.keyIds = new HashSet<>();
this.keys = new HashMap<>();
}
private int nextKey(final String keyId) {
final Map<Integer, Key> keyVersions = keys.computeIfAbsent(keyId, k -> new HashMap<>());
final int nextVersion = calculateNextVersion(keyVersions);
keyVersions.computeIfAbsent(nextVersion, k -> new Key(createSecretKey(), createIvParameterSpec()));
return nextVersion;
}
private static Integer findLatestVersion(final Map<Integer, ?> map) {
int latestVersion = 0;
final Iterator<Integer> it = map.keySet().iterator();
while (it.hasNext()) {
final Integer version = it.next();
if (version > latestVersion) {
latestVersion = version;
}
}
return latestVersion;
}
private static int calculateNextVersion(final Map<Integer, ?> map) {
return findLatestVersion(map) + 1;
}
private static IvParameterSpec createIvParameterSpec() {
final byte[] ivBytes = new byte[GCM_IV_LENGTH];
new SecureRandom().nextBytes(ivBytes);
return new IvParameterSpec(ivBytes);
}
private static SecretKey createSecretKey() {
try {
final KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
keyGenerator.init(KEY_SIZE);
final SecretKey key = keyGenerator.generateKey();
return new SecretKeySpec(key.getEncoded(), "AES");
} catch (final NoSuchAlgorithmException ex) {
throw new RuntimeException("Failed to create secret key", ex);
}
}
@Override
public boolean keyExists(@NotEmpty String keyId) {
return keyIds.contains(keyId);
}
@Override
public void createKey(@NotEmpty String keyId) throws DuplicateEncryptionKeyIdException {
Utils4J.checkNotEmpty(keyId, keyId);
if (keyIds.contains(keyId)) {
throw new DuplicateEncryptionKeyIdException(keyId);
}
final String keyId1 = keyId;
nextKey(keyId1);
keyIds.add(keyId1);
}
@Override
public String rotateKey(@NotEmpty String keyId) throws EncryptionKeyIdUnknownException {
Utils4J.checkNotEmpty(keyId, keyId);
if (!keyIds.contains(keyId)) {
throw new EncryptionKeyIdUnknownException(keyId);
}
return "" + nextKey(keyId);
}
@Override
public String getKeyVersion(@NotEmpty String keyId) throws EncryptionKeyIdUnknownException {
Utils4J.checkNotEmpty(keyId, keyId);
if (!keyIds.contains(keyId)) {
throw new EncryptionKeyIdUnknownException(keyId);
}
final Map<Integer, Key> keyVersions = keys.get(keyId);
return "" + findLatestVersion(keyVersions);
}
@Override
public EncryptedData encrypt(@NotEmpty String keyId, @NotEmpty String dataType, @NotEmpty String contentType, @NotEmpty byte[] data)
throws EncryptionKeyIdUnknownException {
if (!keyIds.contains(keyId)) {
throw new EncryptionKeyIdUnknownException(keyId);
}
final Map<Integer, Key> keyVersions = keys.get(keyId);
final int keyVersion = findLatestVersion(keyVersions);
final Key key = keyVersions.get(keyVersion);
final SecretKey secretKey = key.getSecretKey();
final IvParameterSpec ivParameterSpec = key.getIvParameterSpec();
try {
final Cipher cipher = Cipher.getInstance(CIPHER_NAME);
final GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(GCM_TAG_LENGTH * 8, ivParameterSpec.getIV());
cipher.init(Cipher.ENCRYPT_MODE, secretKey, gcmParameterSpec);
byte[] encryptedData = cipher.doFinal(data);
return new EncryptedData(keyId, "" + keyVersion, dataType, contentType, encryptedData);
} catch (final NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException
| IllegalBlockSizeException | BadPaddingException ex) {
throw new RuntimeException("Failed to encrypt data of type '" + dataType + "' with key '" + keyId + "'", ex);
}
}
@Override
public @NotEmpty byte[] decrypt(@NotNull EncryptedData encryptedData)
throws EncryptionKeyIdUnknownException, EncryptionKeyVersionUnknownException, DecryptionFailedException {
final String keyId = encryptedData.getKeyId();
if (!keyIds.contains(keyId)) {
throw new EncryptionKeyIdUnknownException(keyId);
}
final Map<Integer, Key> keyVersions = keys.get(keyId);
final int keyVersion = Integer.parseInt(encryptedData.getKeyVersion());
final Key key = keyVersions.get(keyVersion);
if (key == null) {
throw new EncryptionKeyVersionUnknownException(encryptedData.getKeyVersion());
}
final SecretKey secretKey = key.getSecretKey();
final IvParameterSpec ivParameterSpec = key.getIvParameterSpec();
try {
final Cipher cipher = Cipher.getInstance(CIPHER_NAME);
final GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(GCM_TAG_LENGTH * 8, ivParameterSpec.getIV());
cipher.init(Cipher.DECRYPT_MODE, secretKey, gcmParameterSpec);
return cipher.doFinal(encryptedData.getEncryptedData());
} catch (final InvalidKeyException | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException
| NoSuchAlgorithmException | NoSuchPaddingException ex) {
throw new DecryptionFailedException(ex);
}
}
/**
* Combines secret key and IV.
*/
private static final class Key {
private final SecretKey secretKey;
private final IvParameterSpec ivParameterSpec;
/**
* Constructor with mandatory data.
*
* @param secretKey
* Secret key.
* @param ivParameterSpec
* Initialization vector.
*/
public Key(SecretKey secretKey, IvParameterSpec ivParameterSpec) {
super();
this.secretKey = secretKey;
this.ivParameterSpec = ivParameterSpec;
}
public SecretKey getSecretKey() {
return secretKey;
}
public IvParameterSpec getIvParameterSpec() {
return ivParameterSpec;
}
}
}