35 |
38 |
36 import java.security.*; |
39 import java.security.*; |
37 import java.security.interfaces.*; |
40 import java.security.interfaces.*; |
38 import java.security.spec.*; |
41 import java.security.spec.*; |
39 |
42 |
|
43 import jdk.test.lib.SigTestUtil; |
|
44 import static jdk.test.lib.SigTestUtil.SignatureType; |
|
45 |
40 public class TestKeyPairGenerator { |
46 public class TestKeyPairGenerator { |
41 |
47 |
42 private static Provider provider; |
48 private static Provider provider; |
43 |
49 |
44 private static byte[] data; |
50 private static byte[] data; |
45 |
51 |
46 private static void testSignature(String algorithm, PrivateKey privateKey, PublicKey publicKey) throws Exception { |
52 private static void testSignature(SignatureType type, String mdAlg, |
47 System.out.println("Testing " + algorithm + "..."); |
53 PrivateKey privateKey, PublicKey publicKey) throws |
48 Signature s = Signature.getInstance(algorithm, provider); |
54 NoSuchAlgorithmException, InvalidKeyException, SignatureException { |
|
55 System.out.println("Testing against " + mdAlg + "..."); |
|
56 String sigAlg = SigTestUtil.generateSigAlg(type, mdAlg); |
|
57 Signature s = Signature.getInstance(sigAlg, provider); |
49 s.initSign(privateKey); |
58 s.initSign(privateKey); |
50 s.update(data); |
59 s.update(data); |
51 byte[] sig = s.sign(); |
60 byte[] sig = s.sign(); |
52 s.initVerify(publicKey); |
61 s.initVerify(publicKey); |
53 s.update(data); |
62 s.update(data); |
54 boolean result = s.verify(sig); |
63 boolean result = s.verify(sig); |
55 if (result == false) { |
64 if (result == false) { |
56 throw new Exception("Verification failed"); |
65 throw new RuntimeException("Verification failed"); |
57 } |
66 } |
58 } |
67 } |
59 |
68 |
60 private static void test(PrivateKey privateKey, PublicKey publicKey) throws Exception { |
69 private static void test(PrivateKey privateKey, PublicKey publicKey) throws Exception { |
61 testSignature("MD2withRSA", privateKey, publicKey); |
70 |
62 testSignature("MD5withRSA", privateKey, publicKey); |
71 int testSize = ((RSAPublicKey)publicKey).getModulus().bitLength(); |
63 testSignature("SHA1withRSA", privateKey, publicKey); |
72 System.out.println("modulus size = " + testSize); |
64 testSignature("SHA224withRSA", privateKey, publicKey); |
73 |
65 testSignature("SHA256withRSA", privateKey, publicKey); |
74 Iterable<String> md_alg_pkcs15 = |
66 RSAPublicKey rsaKey = (RSAPublicKey)publicKey; |
75 SigTestUtil.getDigestAlgorithms(SignatureType.RSA, testSize); |
67 if (rsaKey.getModulus().bitLength() > 512) { |
76 md_alg_pkcs15.forEach(mdAlg -> { |
68 // for SHA384 and SHA512 the data is too long for 512 bit keys |
77 try { |
69 testSignature("SHA384withRSA", privateKey, publicKey); |
78 testSignature(SignatureType.RSA, mdAlg, privateKey, publicKey); |
70 testSignature("SHA512withRSA", privateKey, publicKey); |
79 } catch (NoSuchAlgorithmException | InvalidKeyException | |
|
80 SignatureException ex) { |
|
81 throw new RuntimeException(ex); |
|
82 } |
71 } |
83 } |
|
84 ); |
72 } |
85 } |
73 |
86 |
74 // regression test for 4865198 |
87 // regression test for 4865198 |
75 private static void testInvalidSignature(KeyPair kp1, KeyPair kp2) throws Exception { |
88 private static void testInvalidSignature(KeyPair kp1, KeyPair kp2) throws Exception { |
76 System.out.println("Testing signature with incorrect key..."); |
89 System.out.println("Testing signature with incorrect key..."); |