2727import java .security .KeyStore .PasswordProtection ;
2828import java .security .KeyStoreException ;
2929import java .security .SecureRandom ;
30- import java .security .Security ;
3130import java .security .cert .Certificate ;
3231import java .security .cert .X509Certificate ;
32+ import java .time .Instant ;
33+ import java .time .temporal .ChronoUnit ;
3334import java .util .Date ;
3435
3536import javax .crypto .spec .SecretKeySpec ;
36- import javax .security .auth .x500 .X500Principal ;
3737
38- import org .bouncycastle .asn1 .x509 .X509Name ;
39- import org .bouncycastle .jce .provider .BouncyCastleProvider ;
40- import org .bouncycastle .x509 .X509V3CertificateGenerator ;
4138import org .junit .Before ;
4239import org .junit .Test ;
4340
4744import com .amazonaws .encryptionsdk .exception .CannotUnwrapDataKeyException ;
4845import com .amazonaws .encryptionsdk .multi .MultipleProviderFactory ;
4946
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+
5159public class KeyStoreProviderTest {
5260 private static final SecureRandom RND = new SecureRandom ();
5361 private static final KeyPairGenerator KG ;
@@ -72,7 +80,7 @@ public void setup() throws Exception {
7280 }
7381
7482 @ Test
75- public void singleKeyPkcs1 () throws GeneralSecurityException {
83+ public void singleKeyPkcs1 () throws Exception {
7684 addEntry ("key1" );
7785 final KeyStoreProvider mkp = new KeyStoreProvider (ks , PP , "KeyStore" , "RSA/ECB/PKCS1Padding" , "key1" );
7886 final JceMasterKey mk1 = mkp .getMasterKey ("key1" );
@@ -87,7 +95,7 @@ public void singleKeyPkcs1() throws GeneralSecurityException {
8795 }
8896
8997 @ Test
90- public void singleKeyOaepSha1 () throws GeneralSecurityException {
98+ public void singleKeyOaepSha1 () throws Exception {
9199 addEntry ("key1" );
92100 final KeyStoreProvider mkp = new KeyStoreProvider (ks , PP , "KeyStore" , "RSA/ECB/OAEPWithSHA-1AndMGF1Padding" ,
93101 "key1" );
@@ -103,7 +111,7 @@ public void singleKeyOaepSha1() throws GeneralSecurityException {
103111 }
104112
105113 @ Test
106- public void singleKeyOaepSha256 () throws GeneralSecurityException {
114+ public void singleKeyOaepSha256 () throws Exception {
107115 addEntry ("key1" );
108116 final KeyStoreProvider mkp = new KeyStoreProvider (ks , PP , "KeyStore" , "RSA/ECB/OAEPWithSHA-256AndMGF1Padding" ,
109117 "key1" );
@@ -119,7 +127,7 @@ public void singleKeyOaepSha256() throws GeneralSecurityException {
119127 }
120128
121129 @ Test
122- public void multipleKeys () throws GeneralSecurityException {
130+ public void multipleKeys () throws Exception {
123131 addEntry ("key1" );
124132 addEntry ("key2" );
125133 final KeyStoreProvider mkp = new KeyStoreProvider (ks , PP , "KeyStore" , "RSA/ECB/OAEPWithSHA-256AndMGF1Padding" ,
@@ -146,7 +154,7 @@ public void multipleKeys() throws GeneralSecurityException {
146154 }
147155
148156 @ Test (expected = CannotUnwrapDataKeyException .class )
149- public void encryptOnly () throws GeneralSecurityException {
157+ public void encryptOnly () throws Exception {
150158 addPublicEntry ("key1" );
151159 final KeyStoreProvider mkp = new KeyStoreProvider (ks , PP , "KeyStore" , "RSA/ECB/OAEPWithSHA-256AndMGF1Padding" ,
152160 "key1" );
@@ -157,7 +165,7 @@ public void encryptOnly() throws GeneralSecurityException {
157165 }
158166
159167 @ Test
160- public void escrowAndSymmetric () throws GeneralSecurityException {
168+ public void escrowAndSymmetric () throws Exception {
161169 addPublicEntry ("key1" );
162170 addEntry ("key2" );
163171 final KeyStoreProvider mkp = new KeyStoreProvider (ks , PP , "KeyStore" , "RSA/ECB/OAEPWithSHA-256AndMGF1Padding" ,
@@ -185,7 +193,7 @@ public void escrowAndSymmetric() throws GeneralSecurityException {
185193 }
186194
187195 @ Test
188- public void escrowAndSymmetricSecondProvider () throws GeneralSecurityException {
196+ public void escrowAndSymmetricSecondProvider () throws GeneralSecurityException , IOException {
189197 addPublicEntry ("key1" );
190198 addEntry ("key2" );
191199 final KeyStoreProvider mkp = new KeyStoreProvider (ks , PP , "KeyStore" , "RSA/ECB/OAEPWithSHA-256AndMGF1Padding" ,
@@ -263,40 +271,34 @@ public void keystoreAndRawProvider() throws GeneralSecurityException, IOExceptio
263271 assertArrayEquals (PLAINTEXT , crypto .decryptData (ksp , ct .getResult ()).getResult ());
264272 }
265273
266- private void addEntry (final String alias ) throws GeneralSecurityException {
274+ private void addEntry (final String alias ) throws GeneralSecurityException , IOException {
267275 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 );
282278 }
283279
284- private void addPublicEntry (final String alias ) throws GeneralSecurityException {
280+ private void addPublicEntry (final String alias ) throws GeneralSecurityException , IOException {
285281 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 ;
300302 }
301303
302304 private void copyPublicPart (final KeyStore src , final KeyStore dst , final String alias ) throws KeyStoreException {
0 commit comments