-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathKeyStoreProviderTest.java
311 lines (277 loc) · 13 KB
/
KeyStoreProviderTest.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
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
/*
* Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except
* in compliance with the License. A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.amazonaws.encryptionsdk.jce;
import static com.amazonaws.encryptionsdk.internal.RandomBytesGenerator.generate;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import com.amazonaws.encryptionsdk.AwsCrypto;
import com.amazonaws.encryptionsdk.CryptoResult;
import com.amazonaws.encryptionsdk.MasterKeyProvider;
import com.amazonaws.encryptionsdk.exception.CannotUnwrapDataKeyException;
import com.amazonaws.encryptionsdk.multi.MultipleProviderFactory;
import java.io.IOException;
import java.math.BigInteger;
import java.security.GeneralSecurityException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.KeyStore;
import java.security.KeyStore.PasswordProtection;
import java.security.KeyStoreException;
import java.security.SecureRandom;
import java.security.Security;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import javax.crypto.spec.SecretKeySpec;
import javax.security.auth.x500.X500Principal;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.x509.X509V3CertificateGenerator;
import org.junit.Before;
import org.junit.Test;
/* These internal sun classes are included solely for test purposes as
this test cannot use BouncyCastle cert generation, as there are incompatibilities
between how standard BC and FIPS BC perform cert generation. */
public class KeyStoreProviderTest {
private static final SecureRandom RND = new SecureRandom();
private static final KeyPairGenerator KG;
private static final byte[] PLAINTEXT = generate(1024);
private static final char[] PASSWORD = "Password".toCharArray();
private static final KeyStore.PasswordProtection PP = new PasswordProtection(PASSWORD);
private KeyStore ks;
static {
try {
KG = KeyPairGenerator.getInstance("RSA");
KG.initialize(2048);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
@Before
public void setup() throws Exception {
Security.addProvider(new BouncyCastleProvider());
ks = KeyStore.getInstance(KeyStore.getDefaultType());
ks.load(null, PASSWORD);
}
@Test
public void singleKeyPkcs1() throws Exception {
addEntry("key1");
final KeyStoreProvider mkp =
new KeyStoreProvider(ks, PP, "KeyStore", "RSA/ECB/PKCS1Padding", "key1");
final JceMasterKey mk1 = mkp.getMasterKey("key1");
final AwsCrypto crypto = AwsCrypto.standard();
final CryptoResult<byte[], JceMasterKey> ct = crypto.encryptData(mkp, PLAINTEXT);
assertEquals(1, ct.getMasterKeyIds().size());
final CryptoResult<byte[], JceMasterKey> result = crypto.decryptData(mkp, ct.getResult());
assertArrayEquals(PLAINTEXT, result.getResult());
// Only the first found key should be used
assertEquals(1, result.getMasterKeys().size());
assertEquals(mk1, result.getMasterKeys().get(0));
}
@Test
public void singleKeyOaepSha1() throws Exception {
addEntry("key1");
final KeyStoreProvider mkp =
new KeyStoreProvider(ks, PP, "KeyStore", "RSA/ECB/OAEPWithSHA-1AndMGF1Padding", "key1");
final JceMasterKey mk1 = mkp.getMasterKey("key1");
final AwsCrypto crypto = AwsCrypto.standard();
final CryptoResult<byte[], JceMasterKey> ct = crypto.encryptData(mkp, PLAINTEXT);
assertEquals(1, ct.getMasterKeyIds().size());
final CryptoResult<byte[], JceMasterKey> result = crypto.decryptData(mkp, ct.getResult());
assertArrayEquals(PLAINTEXT, result.getResult());
// Only the first found key should be used
assertEquals(1, result.getMasterKeys().size());
assertEquals(mk1, result.getMasterKeys().get(0));
}
@Test
public void singleKeyOaepSha256() throws Exception {
addEntry("key1");
final KeyStoreProvider mkp =
new KeyStoreProvider(ks, PP, "KeyStore", "RSA/ECB/OAEPWithSHA-256AndMGF1Padding", "key1");
final JceMasterKey mk1 = mkp.getMasterKey("key1");
final AwsCrypto crypto = AwsCrypto.standard();
final CryptoResult<byte[], JceMasterKey> ct = crypto.encryptData(mkp, PLAINTEXT);
assertEquals(1, ct.getMasterKeyIds().size());
final CryptoResult<byte[], JceMasterKey> result = crypto.decryptData(mkp, ct.getResult());
assertArrayEquals(PLAINTEXT, result.getResult());
// Only the first found key should be used
assertEquals(1, result.getMasterKeys().size());
assertEquals(mk1, result.getMasterKeys().get(0));
}
@Test
public void multipleKeys() throws Exception {
addEntry("key1");
addEntry("key2");
final KeyStoreProvider mkp =
new KeyStoreProvider(
ks, PP, "KeyStore", "RSA/ECB/OAEPWithSHA-256AndMGF1Padding", "key1", "key2");
@SuppressWarnings("unused")
final JceMasterKey mk1 = mkp.getMasterKey("key1");
final JceMasterKey mk2 = mkp.getMasterKey("key2");
final AwsCrypto crypto = AwsCrypto.standard();
final CryptoResult<byte[], JceMasterKey> ct = crypto.encryptData(mkp, PLAINTEXT);
assertEquals(2, ct.getMasterKeyIds().size());
CryptoResult<byte[], JceMasterKey> result = crypto.decryptData(mkp, ct.getResult());
assertArrayEquals(PLAINTEXT, result.getResult());
// Order is non-deterministic
assertEquals(1, result.getMasterKeys().size());
// Delete the first key and see if it works
ks.deleteEntry("key1");
result = crypto.decryptData(mkp, ct.getResult());
assertArrayEquals(PLAINTEXT, result.getResult());
// Only the first found key should be used
assertEquals(1, result.getMasterKeys().size());
assertEquals(mk2, result.getMasterKeys().get(0));
}
@Test(expected = CannotUnwrapDataKeyException.class)
public void encryptOnly() throws Exception {
addPublicEntry("key1");
final KeyStoreProvider mkp =
new KeyStoreProvider(ks, PP, "KeyStore", "RSA/ECB/OAEPWithSHA-256AndMGF1Padding", "key1");
final AwsCrypto crypto = AwsCrypto.standard();
final CryptoResult<byte[], JceMasterKey> ct = crypto.encryptData(mkp, PLAINTEXT);
assertEquals(1, ct.getMasterKeyIds().size());
crypto.decryptData(mkp, ct.getResult());
}
@Test
public void escrowAndSymmetric() throws Exception {
addPublicEntry("key1");
addEntry("key2");
final KeyStoreProvider mkp =
new KeyStoreProvider(
ks, PP, "KeyStore", "RSA/ECB/OAEPWithSHA-256AndMGF1Padding", "key1", "key2");
@SuppressWarnings("unused")
final JceMasterKey mk1 = mkp.getMasterKey("key1");
final JceMasterKey mk2 = mkp.getMasterKey("key2");
final AwsCrypto crypto = AwsCrypto.standard();
final CryptoResult<byte[], JceMasterKey> ct = crypto.encryptData(mkp, PLAINTEXT);
assertEquals(2, ct.getMasterKeyIds().size());
CryptoResult<byte[], JceMasterKey> result = crypto.decryptData(mkp, ct.getResult());
assertArrayEquals(PLAINTEXT, result.getResult());
// Only could have decrypted with the keypair
assertEquals(1, result.getMasterKeys().size());
assertEquals(mk2, result.getMasterKeys().get(0));
// Delete the first key and see if it works
ks.deleteEntry("key1");
result = crypto.decryptData(mkp, ct.getResult());
assertArrayEquals(PLAINTEXT, result.getResult());
// Only the first found key should be used
assertEquals(1, result.getMasterKeys().size());
assertEquals(mk2, result.getMasterKeys().get(0));
}
@Test
public void escrowAndSymmetricSecondProvider() throws GeneralSecurityException, IOException {
addPublicEntry("key1");
addEntry("key2");
final KeyStoreProvider mkp =
new KeyStoreProvider(
ks, PP, "KeyStore", "RSA/ECB/OAEPWithSHA-256AndMGF1Padding", "key1", "key2");
@SuppressWarnings("unused")
final JceMasterKey mk1 = mkp.getMasterKey("key1");
final JceMasterKey mk2 = mkp.getMasterKey("key2");
final AwsCrypto crypto = AwsCrypto.standard();
final CryptoResult<byte[], JceMasterKey> ct = crypto.encryptData(mkp, PLAINTEXT);
assertEquals(2, ct.getMasterKeyIds().size());
final KeyStoreProvider mkp2 =
new KeyStoreProvider(ks, PP, "KeyStore", "RSA/ECB/OAEPWithSHA-256AndMGF1Padding", "key1");
CryptoResult<byte[], JceMasterKey> result = crypto.decryptData(mkp2, ct.getResult());
assertArrayEquals(PLAINTEXT, result.getResult());
// Only could have decrypted with the keypair
assertEquals(1, result.getMasterKeys().size());
assertEquals(mk2, result.getMasterKeys().get(0));
}
@Test
public void escrowCase() throws GeneralSecurityException, IOException {
addEntry("escrowKey");
KeyStore ks2 = KeyStore.getInstance(KeyStore.getDefaultType());
ks2.load(null, PASSWORD);
copyPublicPart(ks, ks2, "escrowKey");
final KeyStoreProvider mkp =
new KeyStoreProvider(
ks, PP, "KeyStore", "RSA/ECB/OAEPWithSHA-256AndMGF1Padding", "escrowKey");
final KeyStoreProvider escrowProvider =
new KeyStoreProvider(
ks2, PP, "KeyStore", "RSA/ECB/OAEPWithSHA-256AndMGF1Padding", "escrowKey");
final JceMasterKey mk1 = escrowProvider.getMasterKey("escrowKey");
final AwsCrypto crypto = AwsCrypto.standard();
final CryptoResult<byte[], JceMasterKey> ct = crypto.encryptData(escrowProvider, PLAINTEXT);
assertEquals(1, ct.getMasterKeyIds().size());
try {
crypto.decryptData(escrowProvider, ct.getResult());
fail("Expected CannotUnwrapDataKeyException");
} catch (final CannotUnwrapDataKeyException ex) {
// expected
}
CryptoResult<byte[], JceMasterKey> result = crypto.decryptData(mkp, ct.getResult());
assertArrayEquals(PLAINTEXT, result.getResult());
// Only could have decrypted with the keypair
assertEquals(1, result.getMasterKeys().size());
assertEquals(mk1, result.getMasterKeys().get(0));
}
@Test
public void keystoreAndRawProvider() throws GeneralSecurityException, IOException {
addEntry("key1");
final SecretKeySpec k1 = new SecretKeySpec(generate(32), "AES");
final JceMasterKey jcep = JceMasterKey.getInstance(k1, "jce", "1", "AES/GCM/NoPadding");
final KeyStoreProvider ksp =
new KeyStoreProvider(ks, PP, "KeyStore", "RSA/ECB/OAEPWithSHA-256AndMGF1Padding", "key1");
MasterKeyProvider<JceMasterKey> multiProvider =
MultipleProviderFactory.buildMultiProvider(JceMasterKey.class, jcep, ksp);
assertEquals(jcep, multiProvider.getMasterKey("jce", "1"));
final AwsCrypto crypto = AwsCrypto.standard();
final CryptoResult<byte[], JceMasterKey> ct = crypto.encryptData(multiProvider, PLAINTEXT);
assertEquals(2, ct.getMasterKeyIds().size());
CryptoResult<byte[], JceMasterKey> result = crypto.decryptData(multiProvider, ct.getResult());
assertArrayEquals(PLAINTEXT, result.getResult());
assertEquals(jcep, result.getMasterKeys().get(0));
// Decrypt just using each individually
assertArrayEquals(PLAINTEXT, crypto.decryptData(jcep, ct.getResult()).getResult());
assertArrayEquals(PLAINTEXT, crypto.decryptData(ksp, ct.getResult()).getResult());
}
private void addEntry(final String alias) throws GeneralSecurityException, IOException {
final KeyPair pair = KG.generateKeyPair();
ks.setEntry(
alias,
new KeyStore.PrivateKeyEntry(
pair.getPrivate(), new X509Certificate[] {generateCertificate(pair, alias)}),
PP);
}
private void addPublicEntry(final String alias) throws GeneralSecurityException, IOException {
final KeyPair pair = KG.generateKeyPair();
ks.setEntry(
alias, new KeyStore.TrustedCertificateEntry(generateCertificate(pair, alias)), null);
}
private X509Certificate generateCertificate(final KeyPair pair, final String alias)
throws GeneralSecurityException {
final X509Certificate certificate;
// Generate self-signed certificate
final X509V3CertificateGenerator certGen = new X509V3CertificateGenerator();
final X500Principal dnName = new X500Principal("dc=" + alias);
certGen.setSerialNumber(new BigInteger(256, new SecureRandom()));
certGen.setIssuerDN(dnName);
certGen.setNotBefore(Date.from(Instant.now().minus(1, ChronoUnit.DAYS)));
certGen.setNotAfter(Date.from(Instant.now().plus(730, ChronoUnit.DAYS)));
certGen.setSubjectDN(dnName);
certGen.setPublicKey(pair.getPublic());
certGen.setSignatureAlgorithm("SHA256WithRSA");
certificate = certGen.generate(pair.getPrivate(), "BC");
return certificate;
}
private void copyPublicPart(final KeyStore src, final KeyStore dst, final String alias)
throws KeyStoreException {
Certificate cert = src.getCertificate(alias);
dst.setCertificateEntry(alias, cert);
}
}