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
47
44
import com .amazonaws .encryptionsdk .exception .CannotUnwrapDataKeyException ;
48
45
import com .amazonaws .encryptionsdk .multi .MultipleProviderFactory ;
49
46
50
- @ SuppressWarnings ("deprecation" )
47
+ /* These internal sun classes are included solely for test purposes as
48
+ this test cannot use BouncyCastle cert generation, as there are incompatibilities
49
+ between how standard BC and FIPS BC perform cert generation. */
50
+ import sun .security .x509 .AlgorithmId ;
51
+ import sun .security .x509 .CertificateAlgorithmId ;
52
+ import sun .security .x509 .CertificateSerialNumber ;
53
+ import sun .security .x509 .CertificateValidity ;
54
+ import sun .security .x509 .CertificateX509Key ;
55
+ import sun .security .x509 .X500Name ;
56
+ import sun .security .x509 .X509CertImpl ;
57
+ import sun .security .x509 .X509CertInfo ;
58
+
51
59
public class KeyStoreProviderTest {
52
60
private static final SecureRandom RND = new SecureRandom ();
53
61
private static final KeyPairGenerator KG ;
@@ -72,7 +80,7 @@ public void setup() throws Exception {
72
80
}
73
81
74
82
@ Test
75
- public void singleKeyPkcs1 () throws GeneralSecurityException {
83
+ public void singleKeyPkcs1 () throws Exception {
76
84
addEntry ("key1" );
77
85
final KeyStoreProvider mkp = new KeyStoreProvider (ks , PP , "KeyStore" , "RSA/ECB/PKCS1Padding" , "key1" );
78
86
final JceMasterKey mk1 = mkp .getMasterKey ("key1" );
@@ -87,7 +95,7 @@ public void singleKeyPkcs1() throws GeneralSecurityException {
87
95
}
88
96
89
97
@ Test
90
- public void singleKeyOaepSha1 () throws GeneralSecurityException {
98
+ public void singleKeyOaepSha1 () throws Exception {
91
99
addEntry ("key1" );
92
100
final KeyStoreProvider mkp = new KeyStoreProvider (ks , PP , "KeyStore" , "RSA/ECB/OAEPWithSHA-1AndMGF1Padding" ,
93
101
"key1" );
@@ -103,7 +111,7 @@ public void singleKeyOaepSha1() throws GeneralSecurityException {
103
111
}
104
112
105
113
@ Test
106
- public void singleKeyOaepSha256 () throws GeneralSecurityException {
114
+ public void singleKeyOaepSha256 () throws Exception {
107
115
addEntry ("key1" );
108
116
final KeyStoreProvider mkp = new KeyStoreProvider (ks , PP , "KeyStore" , "RSA/ECB/OAEPWithSHA-256AndMGF1Padding" ,
109
117
"key1" );
@@ -119,7 +127,7 @@ public void singleKeyOaepSha256() throws GeneralSecurityException {
119
127
}
120
128
121
129
@ Test
122
- public void multipleKeys () throws GeneralSecurityException {
130
+ public void multipleKeys () throws Exception {
123
131
addEntry ("key1" );
124
132
addEntry ("key2" );
125
133
final KeyStoreProvider mkp = new KeyStoreProvider (ks , PP , "KeyStore" , "RSA/ECB/OAEPWithSHA-256AndMGF1Padding" ,
@@ -146,7 +154,7 @@ public void multipleKeys() throws GeneralSecurityException {
146
154
}
147
155
148
156
@ Test (expected = CannotUnwrapDataKeyException .class )
149
- public void encryptOnly () throws GeneralSecurityException {
157
+ public void encryptOnly () throws Exception {
150
158
addPublicEntry ("key1" );
151
159
final KeyStoreProvider mkp = new KeyStoreProvider (ks , PP , "KeyStore" , "RSA/ECB/OAEPWithSHA-256AndMGF1Padding" ,
152
160
"key1" );
@@ -157,7 +165,7 @@ public void encryptOnly() throws GeneralSecurityException {
157
165
}
158
166
159
167
@ Test
160
- public void escrowAndSymmetric () throws GeneralSecurityException {
168
+ public void escrowAndSymmetric () throws Exception {
161
169
addPublicEntry ("key1" );
162
170
addEntry ("key2" );
163
171
final KeyStoreProvider mkp = new KeyStoreProvider (ks , PP , "KeyStore" , "RSA/ECB/OAEPWithSHA-256AndMGF1Padding" ,
@@ -185,7 +193,7 @@ public void escrowAndSymmetric() throws GeneralSecurityException {
185
193
}
186
194
187
195
@ Test
188
- public void escrowAndSymmetricSecondProvider () throws GeneralSecurityException {
196
+ public void escrowAndSymmetricSecondProvider () throws GeneralSecurityException , IOException {
189
197
addPublicEntry ("key1" );
190
198
addEntry ("key2" );
191
199
final KeyStoreProvider mkp = new KeyStoreProvider (ks , PP , "KeyStore" , "RSA/ECB/OAEPWithSHA-256AndMGF1Padding" ,
@@ -263,40 +271,34 @@ public void keystoreAndRawProvider() throws GeneralSecurityException, IOExceptio
263
271
assertArrayEquals (PLAINTEXT , crypto .decryptData (ksp , ct .getResult ()).getResult ());
264
272
}
265
273
266
- private void addEntry (final String alias ) throws GeneralSecurityException {
274
+ private void addEntry (final String alias ) throws GeneralSecurityException , IOException {
267
275
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 );
276
+ ks .setEntry (alias , new KeyStore .PrivateKeyEntry (pair .getPrivate (),
277
+ new X509Certificate [] { generateCertificate (pair , alias ) }), PP );
282
278
}
283
279
284
- private void addPublicEntry (final String alias ) throws GeneralSecurityException {
280
+ private void addPublicEntry (final String alias ) throws GeneralSecurityException , IOException {
285
281
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 );
282
+ ks .setEntry (alias , new KeyStore .TrustedCertificateEntry (generateCertificate (pair , alias )), null );
283
+ }
284
+
285
+ private X509Certificate generateCertificate (final KeyPair pair , final String alias ) throws GeneralSecurityException , IOException {
286
+ final X509CertInfo info = new X509CertInfo ();
287
+ final X500Name name = new X500Name ("dc=" + alias );
288
+ info .set (X509CertInfo .SERIAL_NUMBER , new CertificateSerialNumber (new BigInteger (256 , RND )));
289
+ info .set (X509CertInfo .SUBJECT , name );
290
+ info .set (X509CertInfo .ISSUER , name );
291
+ info .set (X509CertInfo .VALIDITY ,
292
+ new CertificateValidity (Date .from (Instant .now ().minus (1 , ChronoUnit .DAYS )),
293
+ Date .from (Instant .now ().plus (730 , ChronoUnit .DAYS ))));
294
+ info .set (X509CertInfo .KEY , new CertificateX509Key (pair .getPublic ()));
295
+ info .set (X509CertInfo .ALGORITHM_ID ,
296
+ new CertificateAlgorithmId (new AlgorithmId (AlgorithmId .sha256WithRSAEncryption_oid )));
297
+
298
+ final X509CertImpl cert = new X509CertImpl (info );
299
+ cert .sign (pair .getPrivate (), AlgorithmId .sha256WithRSAEncryption_oid .toString ());
300
+
301
+ return cert ;
300
302
}
301
303
302
304
private void copyPublicPart (final KeyStore src , final KeyStore dst , final String alias ) throws KeyStoreException {
0 commit comments