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