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