1 /* 2 * Copyright (c) 2003, 2021, 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 4853305 8254717 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 getKeyStore()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 */ testKey(Key key1, Key key2)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 testPublic(KeyFactory kf, PublicKey key)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 testPrivate(KeyFactory kf, PrivateKey key)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 testKey(key6, key6); 111 if (key instanceof RSAPrivateCrtKey) { 112 RSAPrivateCrtKey rsaKey = (RSAPrivateCrtKey)key; 113 KeySpec rsaSpec3 = new RSAPrivateCrtKeySpec(rsaKey.getModulus(), 114 rsaKey.getPublicExponent(), rsaKey.getPrivateExponent(), rsaKey.getPrimeP(), rsaKey.getPrimeQ(), 115 rsaKey.getPrimeExponentP(), rsaKey.getPrimeExponentQ(), rsaKey.getCrtCoefficient(), rsaKey.getParams()); 116 PrivateKey key7 = kf.generatePrivate(rsaSpec3); 117 testKey(key6, key7); 118 } 119 } 120 test(KeyFactory kf, Key key)121 private static void test(KeyFactory kf, Key key) throws Exception { 122 if (key.getAlgorithm().equals("RSA") == false) { 123 System.out.println("Not an RSA key, ignoring"); 124 } 125 if (key instanceof PublicKey) { 126 testPublic(kf, (PublicKey)key); 127 } else if (key instanceof PrivateKey) { 128 testPrivate(kf, (PrivateKey)key); 129 } 130 } 131 main(String[] args)132 public static void main(String[] args) throws Exception { 133 long start = System.currentTimeMillis(); 134 KeyStore ks = getKeyStore(); 135 KeyFactory kf = KeyFactory.getInstance("RSA", "SunRsaSign"); 136 for (Enumeration e = ks.aliases(); e.hasMoreElements(); ) { 137 String alias = (String)e.nextElement(); 138 Key key = null; 139 if (ks.isKeyEntry(alias)) { 140 test(kf, ks.getKey(alias, password)); 141 test(kf, ks.getCertificate(alias).getPublicKey()); 142 } 143 } 144 long stop = System.currentTimeMillis(); 145 System.out.println("All tests passed (" + (stop - start) + " ms)."); 146 } 147 } 148