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 KeyFactory of the new RSA provider |
| 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 | import java.security.spec.*; |
| 37 | |
| 38 | public class TestKeyFactory { |
| 39 | |
| 40 | private final static String BASE = System.getProperty("test.src", "."); |
| 41 | |
| 42 | private static final char[] password = "test12".toCharArray(); |
| 43 | |
| 44 | static KeyStore getKeyStore() throws Exception { |
| 45 | InputStream in = new FileInputStream(new File(BASE, "rsakeys.ks")); |
| 46 | KeyStore ks = KeyStore.getInstance("JKS"); |
| 47 | ks.load(in, password); |
| 48 | in.close(); |
| 49 | return ks; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Test that key1 (reference key) and key2 (key to be tested) are |
| 54 | * equivalent |
| 55 | */ |
| 56 | private static void testKey(Key key1, Key key2) throws Exception { |
| 57 | if (key2.getAlgorithm().equals("RSA") == false) { |
| 58 | throw new Exception("Algorithm not RSA"); |
| 59 | } |
| 60 | if (key1 instanceof PublicKey) { |
| 61 | if (key2.getFormat().equals("X.509") == false) { |
| 62 | throw new Exception("Format not X.509"); |
| 63 | } |
| 64 | } else if (key1 instanceof PrivateKey) { |
| 65 | if (key2.getFormat().equals("PKCS#8") == false) { |
| 66 | throw new Exception("Format not PKCS#8"); |
| 67 | } |
| 68 | } |
| 69 | if (key1.equals(key2) == false) { |
| 70 | throw new Exception("Keys not equal"); |
| 71 | } |
| 72 | if (Arrays.equals(key1.getEncoded(), key2.getEncoded()) == false) { |
| 73 | throw new Exception("Encodings not equal"); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | private static void testPublic(KeyFactory kf, PublicKey key) throws Exception { |
| 78 | System.out.println("Testing public key..."); |
| 79 | PublicKey key2 = (PublicKey)kf.translateKey(key); |
| 80 | KeySpec rsaSpec = kf.getKeySpec(key, RSAPublicKeySpec.class); |
| 81 | PublicKey key3 = kf.generatePublic(rsaSpec); |
| 82 | KeySpec x509Spec = kf.getKeySpec(key, X509EncodedKeySpec.class); |
| 83 | PublicKey key4 = kf.generatePublic(x509Spec); |
| 84 | KeySpec x509Spec2 = new X509EncodedKeySpec(key.getEncoded()); |
| 85 | PublicKey key5 = kf.generatePublic(x509Spec2); |
| 86 | testKey(key, key); |
| 87 | testKey(key, key2); |
| 88 | testKey(key, key3); |
| 89 | testKey(key, key4); |
| 90 | testKey(key, key5); |
| 91 | } |
| 92 | |
| 93 | private static void testPrivate(KeyFactory kf, PrivateKey key) throws Exception { |
| 94 | System.out.println("Testing private key..."); |
| 95 | PrivateKey key2 = (PrivateKey)kf.translateKey(key); |
| 96 | KeySpec rsaSpec = kf.getKeySpec(key, RSAPrivateCrtKeySpec.class); |
| 97 | PrivateKey key3 = kf.generatePrivate(rsaSpec); |
| 98 | KeySpec pkcs8Spec = kf.getKeySpec(key, PKCS8EncodedKeySpec.class); |
| 99 | PrivateKey key4 = kf.generatePrivate(pkcs8Spec); |
| 100 | KeySpec pkcs8Spec2 = new PKCS8EncodedKeySpec(key.getEncoded()); |
| 101 | PrivateKey key5 = kf.generatePrivate(pkcs8Spec2); |
| 102 | testKey(key, key); |
| 103 | testKey(key, key2); |
| 104 | testKey(key, key3); |
| 105 | testKey(key, key4); |
| 106 | testKey(key, key5); |
| 107 | |
| 108 | KeySpec rsaSpec2 = kf.getKeySpec(key, RSAPrivateKeySpec.class); |
| 109 | PrivateKey key6 = kf.generatePrivate(rsaSpec2); |
| 110 | RSAPrivateKey rsaKey = (RSAPrivateKey)key; |
| 111 | KeySpec rsaSpec3 = new RSAPrivateKeySpec(rsaKey.getModulus(), rsaKey.getPrivateExponent()); |
| 112 | PrivateKey key7 = kf.generatePrivate(rsaSpec3); |
| 113 | testKey(key6, key6); |
| 114 | testKey(key6, key7); |
| 115 | } |
| 116 | |
| 117 | private static void test(KeyFactory kf, Key key) throws Exception { |
| 118 | if (key.getAlgorithm().equals("RSA") == false) { |
| 119 | System.out.println("Not an RSA key, ignoring"); |
| 120 | } |
| 121 | if (key instanceof PublicKey) { |
| 122 | testPublic(kf, (PublicKey)key); |
| 123 | } else if (key instanceof PrivateKey) { |
| 124 | testPrivate(kf, (PrivateKey)key); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | public static void main(String[] args) throws Exception { |
| 129 | long start = System.currentTimeMillis(); |
| 130 | KeyStore ks = getKeyStore(); |
| 131 | KeyFactory kf = KeyFactory.getInstance("RSA", "SunRsaSign"); |
| 132 | for (Enumeration e = ks.aliases(); e.hasMoreElements(); ) { |
| 133 | String alias = (String)e.nextElement(); |
| 134 | Key key = null; |
| 135 | if (ks.isKeyEntry(alias)) { |
| 136 | test(kf, ks.getKey(alias, password)); |
| 137 | test(kf, ks.getCertificate(alias).getPublicKey()); |
| 138 | } |
| 139 | } |
| 140 | long stop = System.currentTimeMillis(); |
| 141 | System.out.println("All tests passed (" + (stop - start) + " ms)."); |
| 142 | } |
| 143 | } |