18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 /*
25 * @test
26 * @bug 8200219
27 * @summary Negative tests for Key related Test with DiffieHellman, ECDH, XDH.
28 * It Tests,
29 * Use modified encoding while generating Public/Private Keys
30 * Short, long, unsupported keysize
31 * Invalid Algo names including Null
32 * Invalid provider names including Null
33 * Invalid curve names
34 * Invalid spec usage
35 * Arguments order <KeyExchangeAlgorithm> <Provider> <KeyGenAlgorithm>
36 * <keySize> <Curve*>
37 * @library /test/lib
38 * @build jdk.test.lib.Convert
39 * @run main NegativeTest DiffieHellman SunJCE DiffieHellman 1024
40 * @run main NegativeTest ECDH SunEC EC 256
41 * @run main NegativeTest XDH SunEC XDH 255 X25519
42 * @run main NegativeTest XDH SunEC XDH 448 X448
43 */
44 import java.math.BigInteger;
45 import java.security.InvalidAlgorithmParameterException;
46 import java.security.InvalidParameterException;
47 import java.security.KeyFactory;
48 import java.security.KeyPair;
49 import java.security.KeyPairGenerator;
50 import java.security.NoSuchAlgorithmException;
51 import java.security.NoSuchProviderException;
52 import java.security.Security;
53 import java.security.spec.InvalidKeySpecException;
54 import java.security.spec.NamedParameterSpec;
55 import java.security.spec.KeySpec;
56 import java.security.spec.PKCS8EncodedKeySpec;
57 import java.security.spec.X509EncodedKeySpec;
58 import java.security.spec.XECPrivateKeySpec;
59 import java.security.spec.XECPublicKeySpec;
60 import java.util.Arrays;
61 import javax.crypto.KeyAgreement;
62 import jdk.test.lib.Convert;
63
64 public class NegativeTest {
65
66 public static void main(String[] args) throws Exception {
67
68 String kaAlgo = args[0];
69 String provider = args[1];
70 String kpgAlgo = args[2];
71 int keySize = Integer.parseInt(args[3]);
72 String kpgInit = (args.length > 4) ? args[4] : args[2];
73 testModifiedKeyEncodingTest(provider, kpgAlgo, kpgInit);
74 testInvalidKeyLen(provider, kaAlgo, kpgAlgo, kpgInit);
75 testInvalidKpgAlgo(provider, kaAlgo, keySize);
76 testInvalidKaAlgo(provider, kpgAlgo, keySize);
77 testInvalidProvider(kaAlgo, kpgAlgo, keySize);
78 if (!kaAlgo.equals("DiffieHellman")) {
79 testNamedParameter(provider, kpgAlgo);
80 }
81 if (kaAlgo.equals("XDH")) {
82 testInvalidSpec(provider, kpgAlgo, kpgInit);
111 return kpg.generateKeyPair();
112 }
113
114 private static void testModifiedKeyEncodingTest(String provider,
115 String kpgAlgo, String kpgInit) throws Exception {
116
117 KeyFactory kf = KeyFactory.getInstance(kpgAlgo, provider);
118 KeyPair kp = genKeyPair(provider, kpgAlgo, kpgInit);
119 // Test modified PrivateKey encoding
120 byte[] encoded = kp.getPrivate().getEncoded();
121 byte[] modified = modifyEncoded(encoded);
122 PKCS8EncodedKeySpec priSpec = new PKCS8EncodedKeySpec(modified);
123 try {
124 // Generate PrivateKey with modified encoding
125 kf.generatePrivate(priSpec);
126 throw new RuntimeException(
127 "testModifiedKeyTest should fail but passed.");
128 } catch (InvalidKeySpecException e) {
129 System.out.printf("Expected InvalidKeySpecException for invalid "
130 + "PrivateKey %s%n and modified encoding: %s, %s%n",
131 Convert.byteArrayToHexString(encoded),
132 Convert.byteArrayToHexString(modified), e.getMessage());
133 }
134 // Test modified PublicKey encoding
135 encoded = kp.getPublic().getEncoded();
136 modified = modifyEncoded(encoded);
137 X509EncodedKeySpec pubSpec = new X509EncodedKeySpec(modified);
138 try {
139 // Generate PublicKey with modified encoding
140 kf.generatePublic(pubSpec);
141 throw new RuntimeException(
142 "testModifiedKeyTest should fail but passed.");
143 } catch (InvalidKeySpecException e) {
144 System.out.printf("Expected InvalidKeySpecException for invalid "
145 + "PublicKey %s%n and modified encoding: %s, %s%n",
146 Convert.byteArrayToHexString(encoded),
147 Convert.byteArrayToHexString(modified), e.getMessage());
148 }
149 }
150
151 /**
152 * Test with all Invalid key length.
153 */
154 private static void testInvalidKeyLen(String provider, String kaAlgo,
155 String kpgAlgo, String kpgInit) throws Exception {
156
157 for (int keySize : selectInvalidKeylength(kpgInit)) {
158 try {
159 startKeyAgreement(provider, kaAlgo, kpgAlgo, keySize);
160 throw new RuntimeException(
161 "testInvalidKeyLen should fail but passed.");
162 } catch (InvalidParameterException e) {
163 System.out.printf("Expected InvalidParameterException for "
164 + "keyLength: %s, %s%n", keySize, e.getMessage());
165 }
166 }
167 }
|
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 /*
25 * @test
26 * @bug 8200219
27 * @summary Negative tests for Key related Test with DiffieHellman, ECDH, XDH.
28 * It Tests,
29 * Use modified encoding while generating Public/Private Keys
30 * Short, long, unsupported keysize
31 * Invalid Algo names including Null
32 * Invalid provider names including Null
33 * Invalid curve names
34 * Invalid spec usage
35 * Arguments order <KeyExchangeAlgorithm> <Provider> <KeyGenAlgorithm>
36 * <keySize> <Curve*>
37 * @library /test/lib
38 * @run main NegativeTest DiffieHellman SunJCE DiffieHellman 1024
39 * @run main NegativeTest ECDH SunEC EC 256
40 * @run main NegativeTest XDH SunEC XDH 255 X25519
41 * @run main NegativeTest XDH SunEC XDH 448 X448
42 */
43 import java.math.BigInteger;
44 import java.security.InvalidAlgorithmParameterException;
45 import java.security.InvalidParameterException;
46 import java.security.KeyFactory;
47 import java.security.KeyPair;
48 import java.security.KeyPairGenerator;
49 import java.security.NoSuchAlgorithmException;
50 import java.security.NoSuchProviderException;
51 import java.security.Security;
52 import java.security.spec.InvalidKeySpecException;
53 import java.security.spec.NamedParameterSpec;
54 import java.security.spec.KeySpec;
55 import java.security.spec.PKCS8EncodedKeySpec;
56 import java.security.spec.X509EncodedKeySpec;
57 import java.security.spec.XECPrivateKeySpec;
58 import java.security.spec.XECPublicKeySpec;
59 import java.util.Arrays;
60 import java.util.Hex;
61 import javax.crypto.KeyAgreement;
62
63 public class NegativeTest {
64
65 public static void main(String[] args) throws Exception {
66
67 String kaAlgo = args[0];
68 String provider = args[1];
69 String kpgAlgo = args[2];
70 int keySize = Integer.parseInt(args[3]);
71 String kpgInit = (args.length > 4) ? args[4] : args[2];
72 testModifiedKeyEncodingTest(provider, kpgAlgo, kpgInit);
73 testInvalidKeyLen(provider, kaAlgo, kpgAlgo, kpgInit);
74 testInvalidKpgAlgo(provider, kaAlgo, keySize);
75 testInvalidKaAlgo(provider, kpgAlgo, keySize);
76 testInvalidProvider(kaAlgo, kpgAlgo, keySize);
77 if (!kaAlgo.equals("DiffieHellman")) {
78 testNamedParameter(provider, kpgAlgo);
79 }
80 if (kaAlgo.equals("XDH")) {
81 testInvalidSpec(provider, kpgAlgo, kpgInit);
110 return kpg.generateKeyPair();
111 }
112
113 private static void testModifiedKeyEncodingTest(String provider,
114 String kpgAlgo, String kpgInit) throws Exception {
115
116 KeyFactory kf = KeyFactory.getInstance(kpgAlgo, provider);
117 KeyPair kp = genKeyPair(provider, kpgAlgo, kpgInit);
118 // Test modified PrivateKey encoding
119 byte[] encoded = kp.getPrivate().getEncoded();
120 byte[] modified = modifyEncoded(encoded);
121 PKCS8EncodedKeySpec priSpec = new PKCS8EncodedKeySpec(modified);
122 try {
123 // Generate PrivateKey with modified encoding
124 kf.generatePrivate(priSpec);
125 throw new RuntimeException(
126 "testModifiedKeyTest should fail but passed.");
127 } catch (InvalidKeySpecException e) {
128 System.out.printf("Expected InvalidKeySpecException for invalid "
129 + "PrivateKey %s%n and modified encoding: %s, %s%n",
130 Hex.encoder().encode(encoded),
131 Hex.encoder().encode(modified), e.getMessage());
132 }
133 // Test modified PublicKey encoding
134 encoded = kp.getPublic().getEncoded();
135 modified = modifyEncoded(encoded);
136 X509EncodedKeySpec pubSpec = new X509EncodedKeySpec(modified);
137 try {
138 // Generate PublicKey with modified encoding
139 kf.generatePublic(pubSpec);
140 throw new RuntimeException(
141 "testModifiedKeyTest should fail but passed.");
142 } catch (InvalidKeySpecException e) {
143 System.out.printf("Expected InvalidKeySpecException for invalid "
144 + "PublicKey %s%n and modified encoding: %s, %s%n",
145 Hex.encoder().encode(encoded),
146 Hex.encoder().encode(modified), e.getMessage());
147 }
148 }
149
150 /**
151 * Test with all Invalid key length.
152 */
153 private static void testInvalidKeyLen(String provider, String kaAlgo,
154 String kpgAlgo, String kpgInit) throws Exception {
155
156 for (int keySize : selectInvalidKeylength(kpgInit)) {
157 try {
158 startKeyAgreement(provider, kaAlgo, kpgAlgo, keySize);
159 throw new RuntimeException(
160 "testInvalidKeyLen should fail but passed.");
161 } catch (InvalidParameterException e) {
162 System.out.printf("Expected InvalidParameterException for "
163 + "keyLength: %s, %s%n", keySize, e.getMessage());
164 }
165 }
166 }
|