27
27
import java .security .KeyStore .PasswordProtection ;
28
28
import java .security .KeyStoreException ;
29
29
import java .security .SecureRandom ;
30
- import java .security .Security ;
31
30
import java .security .cert .Certificate ;
32
31
import java .security .cert .X509Certificate ;
32
+ import java .time .Instant ;
33
+ import java .time .temporal .ChronoUnit ;
33
34
import java .util .Date ;
34
35
35
36
import javax .crypto .spec .SecretKeySpec ;
36
- import javax .security .auth .x500 .X500Principal ;
37
37
38
- import org .bouncycastle .asn1 .x509 .X509Name ;
39
- import org .bouncycastle .jce .provider .BouncyCastleProvider ;
40
- import org .bouncycastle .x509 .X509V3CertificateGenerator ;
41
38
import org .junit .Before ;
42
39
import org .junit .Test ;
43
40
46
43
import com .amazonaws .encryptionsdk .MasterKeyProvider ;
47
44
import com .amazonaws .encryptionsdk .exception .CannotUnwrapDataKeyException ;
48
45
import com .amazonaws .encryptionsdk .multi .MultipleProviderFactory ;
46
+ import sun .security .x509 .AlgorithmId ;
47
+ import sun .security .x509 .CertificateAlgorithmId ;
48
+ import sun .security .x509 .CertificateSerialNumber ;
49
+ import sun .security .x509 .CertificateValidity ;
50
+ import sun .security .x509 .CertificateX509Key ;
51
+ import sun .security .x509 .X500Name ;
52
+ import sun .security .x509 .X509CertImpl ;
53
+ import sun .security .x509 .X509CertInfo ;
49
54
50
- @ SuppressWarnings ("deprecation" )
51
55
public class KeyStoreProviderTest {
52
56
private static final SecureRandom RND = new SecureRandom ();
53
57
private static final KeyPairGenerator KG ;
@@ -72,7 +76,7 @@ public void setup() throws Exception {
72
76
}
73
77
74
78
@ Test
75
- public void singleKeyPkcs1 () throws GeneralSecurityException {
79
+ public void singleKeyPkcs1 () throws Exception {
76
80
addEntry ("key1" );
77
81
final KeyStoreProvider mkp = new KeyStoreProvider (ks , PP , "KeyStore" , "RSA/ECB/PKCS1Padding" , "key1" );
78
82
final JceMasterKey mk1 = mkp .getMasterKey ("key1" );
@@ -87,7 +91,7 @@ public void singleKeyPkcs1() throws GeneralSecurityException {
87
91
}
88
92
89
93
@ Test
90
- public void singleKeyOaepSha1 () throws GeneralSecurityException {
94
+ public void singleKeyOaepSha1 () throws Exception {
91
95
addEntry ("key1" );
92
96
final KeyStoreProvider mkp = new KeyStoreProvider (ks , PP , "KeyStore" , "RSA/ECB/OAEPWithSHA-1AndMGF1Padding" ,
93
97
"key1" );
@@ -103,7 +107,7 @@ public void singleKeyOaepSha1() throws GeneralSecurityException {
103
107
}
104
108
105
109
@ Test
106
- public void singleKeyOaepSha256 () throws GeneralSecurityException {
110
+ public void singleKeyOaepSha256 () throws Exception {
107
111
addEntry ("key1" );
108
112
final KeyStoreProvider mkp = new KeyStoreProvider (ks , PP , "KeyStore" , "RSA/ECB/OAEPWithSHA-256AndMGF1Padding" ,
109
113
"key1" );
@@ -119,7 +123,7 @@ public void singleKeyOaepSha256() throws GeneralSecurityException {
119
123
}
120
124
121
125
@ Test
122
- public void multipleKeys () throws GeneralSecurityException {
126
+ public void multipleKeys () throws Exception {
123
127
addEntry ("key1" );
124
128
addEntry ("key2" );
125
129
final KeyStoreProvider mkp = new KeyStoreProvider (ks , PP , "KeyStore" , "RSA/ECB/OAEPWithSHA-256AndMGF1Padding" ,
@@ -146,7 +150,7 @@ public void multipleKeys() throws GeneralSecurityException {
146
150
}
147
151
148
152
@ Test (expected = CannotUnwrapDataKeyException .class )
149
- public void encryptOnly () throws GeneralSecurityException {
153
+ public void encryptOnly () throws Exception {
150
154
addPublicEntry ("key1" );
151
155
final KeyStoreProvider mkp = new KeyStoreProvider (ks , PP , "KeyStore" , "RSA/ECB/OAEPWithSHA-256AndMGF1Padding" ,
152
156
"key1" );
@@ -157,7 +161,7 @@ public void encryptOnly() throws GeneralSecurityException {
157
161
}
158
162
159
163
@ Test
160
- public void escrowAndSymmetric () throws GeneralSecurityException {
164
+ public void escrowAndSymmetric () throws Exception {
161
165
addPublicEntry ("key1" );
162
166
addEntry ("key2" );
163
167
final KeyStoreProvider mkp = new KeyStoreProvider (ks , PP , "KeyStore" , "RSA/ECB/OAEPWithSHA-256AndMGF1Padding" ,
@@ -185,7 +189,7 @@ public void escrowAndSymmetric() throws GeneralSecurityException {
185
189
}
186
190
187
191
@ Test
188
- public void escrowAndSymmetricSecondProvider () throws GeneralSecurityException {
192
+ public void escrowAndSymmetricSecondProvider () throws GeneralSecurityException , IOException {
189
193
addPublicEntry ("key1" );
190
194
addEntry ("key2" );
191
195
final KeyStoreProvider mkp = new KeyStoreProvider (ks , PP , "KeyStore" , "RSA/ECB/OAEPWithSHA-256AndMGF1Padding" ,
@@ -263,40 +267,34 @@ public void keystoreAndRawProvider() throws GeneralSecurityException, IOExceptio
263
267
assertArrayEquals (PLAINTEXT , crypto .decryptData (ksp , ct .getResult ()).getResult ());
264
268
}
265
269
266
- private void addEntry (final String alias ) throws GeneralSecurityException {
270
+ private void addEntry (final String alias ) throws GeneralSecurityException , IOException {
267
271
final KeyPair pair = KG .generateKeyPair ();
268
- // build a certificate generator
269
- final X509V3CertificateGenerator certGen = new X509V3CertificateGenerator ();
270
- final X500Principal dnName = new X500Principal ("cn=" + alias );
271
-
272
- certGen .setSerialNumber (new BigInteger (256 , RND ));
273
- certGen .setSubjectDN (new X509Name ("dc=" + alias ));
274
- certGen .setIssuerDN (dnName ); // use the same
275
- certGen .setNotBefore (new Date (System .currentTimeMillis () - 24 * 60 * 60 * 1000 ));
276
- certGen .setNotAfter (new Date (System .currentTimeMillis () + 2 * 365 * 24 * 60 * 60 * 1000 ));
277
- certGen .setPublicKey (pair .getPublic ());
278
- certGen .setSignatureAlgorithm ("SHA256WithRSA" );
279
- final X509Certificate cert = certGen .generate (pair .getPrivate (), "BC" );
280
-
281
- ks .setEntry (alias , new KeyStore .PrivateKeyEntry (pair .getPrivate (), new X509Certificate [] { cert }), PP );
272
+ ks .setEntry (alias , new KeyStore .PrivateKeyEntry (pair .getPrivate (),
273
+ new X509Certificate [] { generateCertificate (pair , alias ) }), PP );
282
274
}
283
275
284
- private void addPublicEntry (final String alias ) throws GeneralSecurityException {
276
+ private void addPublicEntry (final String alias ) throws GeneralSecurityException , IOException {
285
277
final KeyPair pair = KG .generateKeyPair ();
286
- // build a certificate generator
287
- final X509V3CertificateGenerator certGen = new X509V3CertificateGenerator ();
288
- final X500Principal dnName = new X500Principal ("cn=" + alias );
289
-
290
- certGen .setSerialNumber (new BigInteger (256 , RND ));
291
- certGen .setSubjectDN (new X509Name ("dc=" + alias ));
292
- certGen .setIssuerDN (dnName ); // use the same
293
- certGen .setNotBefore (new Date (System .currentTimeMillis () - 24 * 60 * 60 * 1000 ));
294
- certGen .setNotAfter (new Date (System .currentTimeMillis () + 2 * 365 * 24 * 60 * 60 * 1000 ));
295
- certGen .setPublicKey (pair .getPublic ());
296
- certGen .setSignatureAlgorithm ("SHA256WithRSA" );
297
- final X509Certificate cert = certGen .generate (pair .getPrivate (), "BC" );
298
-
299
- ks .setEntry (alias , new KeyStore .TrustedCertificateEntry (cert ), null );
278
+ ks .setEntry (alias , new KeyStore .TrustedCertificateEntry (generateCertificate (pair , alias )), null );
279
+ }
280
+
281
+ private X509Certificate generateCertificate (final KeyPair pair , final String alias ) throws GeneralSecurityException , IOException {
282
+ final X509CertInfo info = new X509CertInfo ();
283
+ final X500Name name = new X500Name ("dc=" + alias );
284
+ info .set (X509CertInfo .SERIAL_NUMBER , new CertificateSerialNumber (new BigInteger (256 , RND )));
285
+ info .set (X509CertInfo .SUBJECT , name );
286
+ info .set (X509CertInfo .ISSUER , name );
287
+ info .set (X509CertInfo .VALIDITY ,
288
+ new CertificateValidity (Date .from (Instant .now ().minus (1 , ChronoUnit .DAYS )),
289
+ Date .from (Instant .now ().plus (730 , ChronoUnit .DAYS ))));
290
+ info .set (X509CertInfo .KEY , new CertificateX509Key (pair .getPublic ()));
291
+ info .set (X509CertInfo .ALGORITHM_ID ,
292
+ new CertificateAlgorithmId (new AlgorithmId (AlgorithmId .sha256WithRSAEncryption_oid )));
293
+
294
+ final X509CertImpl cert = new X509CertImpl (info );
295
+ cert .sign (pair .getPrivate (), AlgorithmId .sha256WithRSAEncryption_oid .toString ());
296
+
297
+ return cert ;
300
298
}
301
299
302
300
private void copyPublicPart (final KeyStore src , final KeyStore dst , final String alias ) throws KeyStoreException {
0 commit comments