jdk/test/sun/security/pkcs11/rsa/TestSignatures.java
changeset 2 90ce3da70b43
child 5506 202f599c92aa
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/sun/security/pkcs11/rsa/TestSignatures.java	Sat Dec 01 00:00:00 2007 +0000
@@ -0,0 +1,118 @@
+/*
+ * Copyright 2003 Sun Microsystems, Inc.  All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/**
+ * @test
+ * @bug 4856966
+ * @summary Test signing/verifying using all the signature algorithms
+ * @author Andreas Sterbenz
+ * @library ..
+ */
+
+import java.io.*;
+import java.util.*;
+
+import java.security.*;
+import java.security.interfaces.*;
+
+public class TestSignatures extends PKCS11Test {
+
+    private final static String BASE = System.getProperty("test.src", ".");
+
+    private static final char[] password = "test12".toCharArray();
+
+    private static Provider provider;
+
+    private static byte[] data;
+
+    static KeyStore getKeyStore() throws Exception {
+        InputStream in = new FileInputStream(new File(BASE, "rsakeys.ks"));
+        KeyStore ks = KeyStore.getInstance("JKS");
+        ks.load(in, password);
+        in.close();
+        return ks;
+    }
+
+    private static void testSignature(String algorithm, PrivateKey privateKey, PublicKey publicKey) throws Exception {
+        System.out.println("Testing " + algorithm + "...");
+        Signature s = Signature.getInstance(algorithm, provider);
+        s.initSign(privateKey);
+        s.update(data);
+        byte[] sig = s.sign();
+        s.initVerify(publicKey);
+        s.update(data);
+        boolean result;
+        result = s.verify(sig);
+        if (result == false) {
+            throw new Exception("Verification 1 failed");
+        }
+        s.update(data);
+        result = s.verify(sig);
+        if (result == false) {
+            throw new Exception("Verification 2 failed");
+        }
+        result = s.verify(sig);
+        if (result == true) {
+            throw new Exception("Verification 3 succeeded");
+        }
+    }
+
+    private static void test(PrivateKey privateKey, PublicKey publicKey) throws Exception {
+        testSignature("MD2withRSA", privateKey, publicKey);
+        testSignature("MD5withRSA", privateKey, publicKey);
+        testSignature("SHA1withRSA", privateKey, publicKey);
+        testSignature("SHA256withRSA", privateKey, publicKey);
+        RSAPublicKey rsaKey = (RSAPublicKey)publicKey;
+        if (rsaKey.getModulus().bitLength() > 512) {
+            // for SHA384 and SHA512 the data is too long for 512 bit keys
+            testSignature("SHA384withRSA", privateKey, publicKey);
+            testSignature("SHA512withRSA", privateKey, publicKey);
+        }
+    }
+
+    public static void main(String[] args) throws Exception {
+        main(new TestSignatures());
+    }
+
+    public void main(Provider p) throws Exception {
+        long start = System.currentTimeMillis();
+        provider = p;
+        data = new byte[2048];
+        new Random().nextBytes(data);
+        KeyStore ks = getKeyStore();
+        KeyFactory kf = KeyFactory.getInstance("RSA", provider);
+        for (Enumeration e = ks.aliases(); e.hasMoreElements(); ) {
+            String alias = (String)e.nextElement();
+            if (ks.isKeyEntry(alias)) {
+                System.out.println("* Key " + alias + "...");
+                PrivateKey privateKey = (PrivateKey)ks.getKey(alias, password);
+                PublicKey publicKey = ks.getCertificate(alias).getPublicKey();
+                privateKey = (PrivateKey)kf.translateKey(privateKey);
+                publicKey = (PublicKey)kf.translateKey(publicKey);
+                test(privateKey, publicKey);
+            }
+        }
+        long stop = System.currentTimeMillis();
+        System.out.println("All tests passed (" + (stop - start) + " ms).");
+    }
+}