jdk/test/sun/security/pkcs11/ec/TestCurves.java
changeset 17491 7a33824ec8c5
parent 12685 8a448b5b9006
child 19067 5271291b7121
--- a/jdk/test/sun/security/pkcs11/ec/TestCurves.java	Tue May 14 20:16:21 2013 +0400
+++ b/jdk/test/sun/security/pkcs11/ec/TestCurves.java	Tue May 14 18:08:18 2013 +0100
@@ -38,9 +38,6 @@
 
 import javax.crypto.*;
 
-// XXX no public API to enumerate supported named curves
-import sun.security.ec.NamedCurve;
-
 public class TestCurves extends PKCS11Test {
 
     public static void main(String[] args) throws Exception {
@@ -57,8 +54,8 @@
         byte[] data = new byte[2048];
         random.nextBytes(data);
 
-        Collection<? extends ECParameterSpec> curves =
-            NamedCurve.knownECParameterSpecs();
+        Vector<ECParameterSpec> curves = getKnownCurves(p);
+
         for (ECParameterSpec params : curves) {
             System.out.println("Testing " + params + "...");
             KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC", p);
@@ -92,6 +89,66 @@
         System.out.println("OK");
     }
 
+    private static Vector<ECParameterSpec>
+            getKnownCurves(Provider p) throws Exception {
+
+        int index;
+        int begin;
+        int end;
+        String curve;
+        Vector<ECParameterSpec> results = new Vector<ECParameterSpec>();
+        String kcProp =
+            p.getProperty("AlgorithmParameters.EC SupportedCurves");
+
+        if (kcProp == null) {
+            throw new RuntimeException(
+            "\"AlgorithmParameters.EC SupportedCurves property\" not found");
+        }
+
+        index = 0;
+        for (;;) {
+            // Each set of curve names is enclosed with brackets.
+            begin = kcProp.indexOf('[', index);
+            end = kcProp.indexOf(']', index);
+            if (begin == -1 || end == -1) {
+                break;
+            }
+
+            /*
+             * Each name is separated by a comma.
+             * Just get the first name in the set.
+             */
+            index = end + 1;
+            begin++;
+            end = kcProp.indexOf(',', begin);
+            if (end == -1) {
+                // Only one name in the set.
+                end = index -1;
+            }
+
+            curve = kcProp.substring(begin, end);
+
+            results.add(getECParameterSpec(p, curve));
+        }
+
+        if (results.size() == 0) {
+            throw new RuntimeException("No supported EC curves found");
+        }
+
+        return results;
+    }
+
+    private static ECParameterSpec getECParameterSpec(Provider p, String name)
+            throws Exception {
+
+        AlgorithmParameters parameters =
+            AlgorithmParameters.getInstance("EC", p);
+
+        parameters.init(new ECGenParameterSpec(name));
+
+        return parameters.getParameterSpec(ECParameterSpec.class);
+    }
+
     private static void testSigning(Provider p, String algorithm,
             byte[] data, KeyPair kp1, KeyPair kp2) throws Exception {
         // System.out.print("  " + algorithm);
@@ -115,6 +172,4 @@
             throw new Exception("Signature should not verify");
         }
     }
-
-
 }