J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2003 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /** |
| 25 | * @test |
| 26 | * @bug 4853305 |
| 27 | * @summary Test signing/verifying using all the signature algorithms |
| 28 | * @author Andreas Sterbenz |
| 29 | */ |
| 30 | |
| 31 | import java.io.*; |
| 32 | import java.util.*; |
| 33 | |
| 34 | import java.security.*; |
| 35 | import java.security.interfaces.*; |
| 36 | |
| 37 | public class TestSignatures { |
| 38 | |
| 39 | private final static String BASE = System.getProperty("test.src", "."); |
| 40 | |
| 41 | private static final char[] password = "test12".toCharArray(); |
| 42 | |
| 43 | private static Provider provider; |
| 44 | |
| 45 | private static byte[] data; |
| 46 | |
| 47 | static KeyStore getKeyStore() throws Exception { |
| 48 | InputStream in = new FileInputStream(new File(BASE, "rsakeys.ks")); |
| 49 | KeyStore ks = KeyStore.getInstance("JKS"); |
| 50 | ks.load(in, password); |
| 51 | in.close(); |
| 52 | return ks; |
| 53 | } |
| 54 | |
| 55 | private static void testSignature(String algorithm, PrivateKey privateKey, PublicKey publicKey) throws Exception { |
| 56 | System.out.println("Testing " + algorithm + "..."); |
| 57 | Signature s = Signature.getInstance(algorithm, provider); |
| 58 | s.initSign(privateKey); |
| 59 | s.update(data); |
| 60 | byte[] sig = s.sign(); |
| 61 | s.initVerify(publicKey); |
| 62 | s.update(data); |
| 63 | boolean result; |
| 64 | result = s.verify(sig); |
| 65 | if (result == false) { |
| 66 | throw new Exception("Verification 1 failed"); |
| 67 | } |
| 68 | s.update(data); |
| 69 | result = s.verify(sig); |
| 70 | if (result == false) { |
| 71 | throw new Exception("Verification 2 failed"); |
| 72 | } |
| 73 | result = s.verify(sig); |
| 74 | if (result == true) { |
| 75 | throw new Exception("Verification 3 succeeded"); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | private static void test(PrivateKey privateKey, PublicKey publicKey) throws Exception { |
| 80 | testSignature("MD2withRSA", privateKey, publicKey); |
| 81 | testSignature("MD5withRSA", privateKey, publicKey); |
| 82 | testSignature("SHA1withRSA", privateKey, publicKey); |
| 83 | testSignature("SHA256withRSA", privateKey, publicKey); |
| 84 | RSAPublicKey rsaKey = (RSAPublicKey)publicKey; |
| 85 | if (rsaKey.getModulus().bitLength() > 512) { |
| 86 | // for SHA384 and SHA512 the data is too long for 512 bit keys |
| 87 | testSignature("SHA384withRSA", privateKey, publicKey); |
| 88 | testSignature("SHA512withRSA", privateKey, publicKey); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | public static void main(String[] args) throws Exception { |
| 93 | long start = System.currentTimeMillis(); |
| 94 | provider = Security.getProvider("SunRsaSign"); |
| 95 | data = new byte[2048]; |
| 96 | new Random().nextBytes(data); |
| 97 | KeyStore ks = getKeyStore(); |
| 98 | KeyFactory kf = KeyFactory.getInstance("RSA", provider); |
| 99 | for (Enumeration e = ks.aliases(); e.hasMoreElements(); ) { |
| 100 | String alias = (String)e.nextElement(); |
| 101 | if (ks.isKeyEntry(alias)) { |
| 102 | System.out.println("* Key " + alias + "..."); |
| 103 | PrivateKey privateKey = (PrivateKey)ks.getKey(alias, password); |
| 104 | PublicKey publicKey = ks.getCertificate(alias).getPublicKey(); |
| 105 | privateKey = (PrivateKey)kf.translateKey(privateKey); |
| 106 | publicKey = (PublicKey)kf.translateKey(publicKey); |
| 107 | test(privateKey, publicKey); |
| 108 | } |
| 109 | } |
| 110 | long stop = System.currentTimeMillis(); |
| 111 | System.out.println("All tests passed (" + (stop - start) + " ms)."); |
| 112 | } |
| 113 | } |