-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTestKmsCoverCrypt.java
More file actions
372 lines (296 loc) · 16.9 KB
/
TestKmsCoverCrypt.java
File metadata and controls
372 lines (296 loc) · 16.9 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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package com.cosmian;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.Arrays;
import java.util.Optional;
import java.util.logging.Logger;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import com.cosmian.jna.covercrypt.CoverCrypt;
import com.cosmian.jna.covercrypt.structs.Policy;
import com.cosmian.rest.abe.KmsClient;
import com.cosmian.rest.abe.data.DecryptedData;
import com.cosmian.rest.kmip.data_structures.KeyValue;
import com.cosmian.rest.kmip.objects.PrivateKey;
import com.cosmian.rest.kmip.objects.PublicKey;
import com.cosmian.rest.kmip.types.CryptographicAlgorithm;
import com.cosmian.rest.kmip.types.KeyFormatType;
import com.cosmian.rest.kmip.types.ObjectType;
import com.cosmian.rest.kmip.types.VendorAttribute;
import com.cosmian.utils.CloudproofException;
public class TestKmsCoverCrypt {
private static final Logger logger = Logger.getLogger(TestKmsCoverCrypt.class.getName());
@BeforeAll
public static void before_all() {
TestUtils.initLogging();
}
private String accessPolicyProtected() throws CloudproofException {
return "(Department::FIN || Department::MKG) && Security Level::Protected";
}
private String accessPolicyConfidential() throws CloudproofException {
return "Department::FIN && Security Level::Confidential";
}
@Test
public void testKeysImportExport() throws Exception {
if (!TestUtils.serverAvailable(TestUtils.kmsServerUrl())) {
throw new RuntimeException("No KMS Server available");
}
Policy pg = TestNativeCoverCrypt.policy();
KmsClient kmsClient = new KmsClient(TestUtils.kmsServerUrl(), TestUtils.apiKey());
String[] ids = kmsClient.createCoverCryptMasterKeyPair(pg);
logger.info("Created Master Key: Private Key ID: " + ids[0] + ", Public Key ID: " + ids[1]);
String privateMasterKeyUniqueIdentifier = ids[0];
PrivateKey privateMasterKey = kmsClient
.retrieveCoverCryptPrivateMasterKey(privateMasterKeyUniqueIdentifier);
assertEquals(KeyFormatType.CoverCryptSecretKey, privateMasterKey.getKeyBlock().getKeyFormatType());
assertEquals(CryptographicAlgorithm.CoverCrypt,
privateMasterKey.getKeyBlock().getCryptographicAlgorithm());
String publicMasterKeyUniqueIdentifier = ids[1];
PublicKey publicMasterKey = kmsClient
.retrieveCoverCryptPublicMasterKey(publicMasterKeyUniqueIdentifier);
assertEquals(KeyFormatType.CoverCryptPublicKey, publicMasterKey.getKeyBlock().getKeyFormatType());
assertEquals(CryptographicAlgorithm.CoverCrypt,
publicMasterKey.getKeyBlock().getCryptographicAlgorithm());
// try reimporting the public key with the same ID
try {
kmsClient.importCoverCryptPublicMasterKey(publicMasterKeyUniqueIdentifier, publicMasterKey,
false);
} catch (CloudproofException e) {
// expected cannot re-insert with the same id if replaceExisting is false
} catch (Exception e) {
throw e;
}
// allow overwrite
String publicMasterKeyUniqueIdentifier_ = kmsClient.importCoverCryptPublicMasterKey(
publicMasterKeyUniqueIdentifier,
publicMasterKey, true);
logger.info("Imported Public Key with id: " + publicMasterKeyUniqueIdentifier_);
// retrieve it again
PublicKey publicMasterKey_ = kmsClient
.retrieveCoverCryptPublicMasterKey(publicMasterKeyUniqueIdentifier);
assertEquals(ObjectType.Public_Key, publicMasterKey_.getObjectType());
assertEquals(KeyFormatType.CoverCryptPublicKey, publicMasterKey_.getKeyBlock().getKeyFormatType());
assertEquals(CryptographicAlgorithm.CoverCrypt,
publicMasterKey_.getKeyBlock().getCryptographicAlgorithm());
// User decryption key
String userDecryptionKeyUniqueIdentifier = kmsClient.createCoverCryptUserDecryptionKey(
accessPolicyProtected(),
privateMasterKeyUniqueIdentifier);
logger.info("Created User Decryption Key with id: " + userDecryptionKeyUniqueIdentifier);
// ... retrieve it
PrivateKey userDecryptionKey = kmsClient
.retrieveCoverCryptUserDecryptionKey(userDecryptionKeyUniqueIdentifier);
assertEquals(KeyFormatType.CoverCryptSecretKey, userDecryptionKey.getKeyBlock().getKeyFormatType());
assertEquals(CryptographicAlgorithm.CoverCrypt,
userDecryptionKey.getKeyBlock().getCryptographicAlgorithm());
KeyValue keyValue = (KeyValue) userDecryptionKey.getKeyBlock().getKeyValue();
VendorAttribute[] vendorAttributes = keyValue.getAttributes().get().getVendorAttributes().get();
// TODO better check on Vendor Attributes
logger.info(() -> Arrays.asList(vendorAttributes).toString());
}
@Test
public void testKmsEncryptDecrypt() throws Exception {
if (!TestUtils.serverAvailable(TestUtils.kmsServerUrl())) {
throw new RuntimeException("No KMS Server available");
}
Policy pg = TestNativeCoverCrypt.policy();
KmsClient kmsClient = new KmsClient(TestUtils.kmsServerUrl(), TestUtils.apiKey());
String[] ids = kmsClient.createCoverCryptMasterKeyPair(pg);
logger.info("Created Master Key: Private Key ID: " + ids[0] + ", Public Key ID: " + ids[1]);
String privateMasterKeyID = ids[0];
PrivateKey privateMasterKey = kmsClient.retrieveCoverCryptPrivateMasterKey(privateMasterKeyID);
assertEquals(KeyFormatType.CoverCryptSecretKey, privateMasterKey.getKeyBlock().getKeyFormatType());
assertEquals(CryptographicAlgorithm.CoverCrypt,
privateMasterKey.getKeyBlock().getCryptographicAlgorithm());
TestUtils.writeResource("cover_crypt/private_master_key.json",
privateMasterKey.toJson().getBytes(StandardCharsets.UTF_8));
String publicMasterKeyUniqueIdentifier = ids[1];
PublicKey publicMasterKey = kmsClient
.retrieveCoverCryptPublicMasterKey(publicMasterKeyUniqueIdentifier);
assertEquals(KeyFormatType.CoverCryptPublicKey, publicMasterKey.getKeyBlock().getKeyFormatType());
assertEquals(CryptographicAlgorithm.CoverCrypt,
publicMasterKey.getKeyBlock().getCryptographicAlgorithm());
TestUtils.writeResource("cover_crypt/public_master_key.json",
publicMasterKey.toJson().getBytes(StandardCharsets.UTF_8));
// encryption
String protected_fin_data = "protected_fin_attributes";
String protected_fin_enc_policy = "Department::FIN && Security Level::Protected";
byte[] protected_fin_ct = kmsClient.coverCryptEncrypt(publicMasterKeyUniqueIdentifier,
protected_fin_data.getBytes(StandardCharsets.UTF_8), protected_fin_enc_policy);
String confidential_fin_data = "confidential_fin_attributes";
String confidential_fin_enc_policy = "Department::FIN && Security Level::Confidential";
byte[] confidential_fin_ct = kmsClient.coverCryptEncrypt(publicMasterKeyUniqueIdentifier,
confidential_fin_data.getBytes(StandardCharsets.UTF_8), confidential_fin_enc_policy);
// User decryption key Protected, FIN, MKG
String fin_mkg_protected_user_key = kmsClient.createCoverCryptUserDecryptionKey(accessPolicyProtected(),
privateMasterKeyID);
PrivateKey userKey_1 = kmsClient.retrieveCoverCryptUserDecryptionKey(fin_mkg_protected_user_key);
TestUtils.writeResource("cover_crypt/fin_mkg_protected_user_key.json",
userKey_1.toJson().getBytes(StandardCharsets.UTF_8));
// User decryption key Confidential, FIN
String fin_confidential_user_key = kmsClient.createCoverCryptUserDecryptionKey(
accessPolicyConfidential(),
privateMasterKeyID);
PrivateKey userKey_2 = kmsClient.retrieveCoverCryptUserDecryptionKey(fin_confidential_user_key);
TestUtils.writeResource("cover_crypt/fin_confidential_user_key.json",
userKey_2.toJson().getBytes(StandardCharsets.UTF_8));
// User decryption key Protected should be able to decrypt protected_fin_ct
String plaintext_1_1 = new String(
kmsClient.coverCryptDecrypt(fin_mkg_protected_user_key, protected_fin_ct)
.getPlaintext(),
StandardCharsets.UTF_8);
assertEquals(protected_fin_data, plaintext_1_1);
// User decryption key Confidential should be able to decrypt protected_fin_ct
String plaintext_1_2 = new String(
kmsClient.coverCryptDecrypt(fin_confidential_user_key, protected_fin_ct)
.getPlaintext(),
StandardCharsets.UTF_8);
assertEquals(protected_fin_data, plaintext_1_2);
// User decryption key Protected should not be able to decrypt
// confidential_fin_ct
try {
new String(kmsClient.coverCryptDecrypt(fin_mkg_protected_user_key, confidential_fin_ct)
.getPlaintext(), StandardCharsets.UTF_8);
throw new RuntimeException(
"User with key Confidential should not be able to decrypt data Confidential");
} catch (CloudproofException e) {
// fine: should not be able to decrypt
}
// User decryption key Confidential should not be able to decrypt
// confidential_fin_ct
String plaintext_2_2 = new String(
kmsClient.coverCryptDecrypt(fin_confidential_user_key, confidential_fin_ct)
.getPlaintext(),
StandardCharsets.UTF_8);
assertEquals(confidential_fin_data, plaintext_2_2);
}
@Test
public void testKmsEncryptDecryptMetaData() throws Exception {
if (!TestUtils.serverAvailable(TestUtils.kmsServerUrl())) {
throw new RuntimeException("No KMS Server available");
}
Policy pg = TestNativeCoverCrypt.policy();
KmsClient kmsClient = new KmsClient(TestUtils.kmsServerUrl(), TestUtils.apiKey());
String[] ids = kmsClient.createCoverCryptMasterKeyPair(pg);
logger.info("Created Master Key: Private Key ID: " + ids[0] + ", Public Key ID: " + ids[1]);
String privateMasterKeyID = ids[0];
String publicMasterKeyUniqueIdentifier = ids[1];
// encryption
byte[] protected_fin_data = "protected_fin_attributes".getBytes(StandardCharsets.UTF_8);
String protected_fin_enc_policy = "Department::FIN && Security Level::Protected";
byte[] authenticationData = "authentication".getBytes(StandardCharsets.UTF_8);
byte[] headerMetaData = "headerMeta".getBytes(StandardCharsets.UTF_8);
byte[] protected_fin_ct = kmsClient.coverCryptEncrypt(publicMasterKeyUniqueIdentifier,
protected_fin_data, protected_fin_enc_policy, authenticationData, headerMetaData);
// User decryption key Protected, FIN, MKG
String fin_mkg_protected_user_key = kmsClient.createCoverCryptUserDecryptionKey(accessPolicyProtected(),
privateMasterKeyID);
PrivateKey userKey_1 = kmsClient.retrieveCoverCryptUserDecryptionKey(fin_mkg_protected_user_key);
TestUtils.writeResource("cover_crypt/fin_mkg_protected_user_key.json",
userKey_1.toJson().getBytes(StandardCharsets.UTF_8));
// User decryption key Protected should be able to decrypt protected_fin_ct
DecryptedData decryptedData = kmsClient.coverCryptDecrypt(fin_mkg_protected_user_key, protected_fin_ct,
authenticationData);
byte[] plaintext_ = decryptedData.getPlaintext();
byte[] headerMetadata_ = decryptedData.getHeaderMetaData();
assertArrayEquals(protected_fin_data, plaintext_);
assertArrayEquals(headerMetaData, headerMetadata_);
}
@Test
public void testLocalEncryptServerDecrypt() throws Exception {
System.out.println("");
System.out.println("---------------------------------------");
System.out.println(" Hybrid Crypto Test Local Encrypt + Server Decrypt");
System.out.println("---------------------------------------");
System.out.println("");
if (!TestUtils.serverAvailable(TestUtils.kmsServerUrl())) {
throw new RuntimeException("No KMS Server available");
}
// The data we want to encrypt/decrypt
byte[] plaintext = "This is a test message".getBytes(StandardCharsets.UTF_8);
// A unique ID associated with this message. The unique id is used to
// authenticate the message in the AES encryption scheme.
// Typically this will be a hash of the content if it is unique, a unique
// filename or a database unique key
byte[] uid = MessageDigest.getInstance("SHA-256").digest(plaintext);
Policy policy = TestNativeCoverCrypt.policy();
KmsClient kmsClient = new KmsClient(TestUtils.kmsServerUrl(), TestUtils.apiKey());
String[] ids = kmsClient.createCoverCryptMasterKeyPair(policy);
String privateMasterKeyId = ids[0];
String publicMasterKeyId = ids[1];
PublicKey publicKey = kmsClient.retrieveCoverCryptPublicMasterKey(publicMasterKeyId);
// User decryption key Confidential, FIN
String userKeyId = kmsClient.createCoverCryptUserDecryptionKey(accessPolicyConfidential(),
privateMasterKeyId);
//
// Local Encryption
//
System.out.println("Local Encryption");
// The encryption policy attributes that will be used to encrypt the content.
// Attributes must exist in the policy associated with the Public Key
String encryptionPolicy = "Department::FIN && Security Level::Confidential";
byte[] ciphertext = CoverCrypt.encrypt(policy, publicKey.bytes(), encryptionPolicy, plaintext, Optional.of(uid),
Optional.empty());
//
// Local Decryption
//
System.out.println("Local Decryption");
PrivateKey userKey = kmsClient.retrieveCoverCryptUserDecryptionKey(userKeyId);
DecryptedData res = CoverCrypt.decrypt(userKey.bytes(), ciphertext, Optional.of(uid));
assertArrayEquals(plaintext, res.getPlaintext());
assertArrayEquals(new byte[] {}, res.getHeaderMetaData());
//
// KMS Decryption
//
System.out.println("KMS Decryption");
byte[] data_kms = kmsClient.coverCryptDecrypt(userKeyId, ciphertext, uid).getPlaintext();
assertArrayEquals(plaintext, data_kms);
}
@Test
public void testServerEncryptLocalDecrypt() throws Exception {
System.out.println("");
System.out.println("------------------------------------------------------");
System.out.println(" Hybrid Crypto Test Server Encrypt + Local Decrypt ");
System.out.println("------------------------------------------------------");
System.out.println("");
if (!TestUtils.serverAvailable(TestUtils.kmsServerUrl())) {
throw new RuntimeException("No KMS Server available");
}
// The data we want to encrypt/decrypt
byte[] plaintext = new byte[] {1, 2, 3, 4, 5, 6};
// A unique ID associated with this message. The unique id is used to
// authenticate the message in the AES encryption scheme.
// Typically this will be a hash of the content if it is unique, a unique
// filename or a database unique key
byte[] uid = MessageDigest.getInstance("SHA-256").digest(plaintext);
Policy policy = TestNativeCoverCrypt.policy();
KmsClient kmsClient = new KmsClient(TestUtils.kmsServerUrl(), TestUtils.apiKey());
String[] ids = kmsClient.createCoverCryptMasterKeyPair(policy);
String privateMasterKeyId = ids[0];
String publicMasterKeyId = ids[1];
// User decryption key Confidential, FIN
String userKeyId = kmsClient.createCoverCryptUserDecryptionKey(accessPolicyConfidential(), privateMasterKeyId);
//
// Server Encryption
//
// The encryption policy attributes that will be used to encrypt the content.
// Attributes must exist in the policy associated with the Public Key
String encryptionPolicy = "Department::FIN && Security Level::Confidential";
byte[] ciphertext = kmsClient.coverCryptEncrypt(publicMasterKeyId, plaintext, encryptionPolicy, uid);
//
// KMS Decryption
//
DecryptedData data_kms = kmsClient.coverCryptDecrypt(userKeyId, ciphertext, uid);
assertArrayEquals(plaintext, data_kms.getPlaintext());
assertArrayEquals(new byte[] {}, data_kms.getHeaderMetaData());
//
// Local Decryption
//
PrivateKey userKey = kmsClient.retrieveCoverCryptUserDecryptionKey(userKeyId);
DecryptedData res = CoverCrypt.decrypt(userKey.bytes(), ciphertext, Optional.of(uid));
assertArrayEquals(plaintext, res.getPlaintext());
assertArrayEquals(new byte[] {}, res.getHeaderMetaData());
}
}