Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/rsa/TestKeyFactory.java
38838 views
/*1* Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 485330526* @summary Test KeyFactory of the new RSA provider27* @author Andreas Sterbenz28*/2930import java.io.*;31import java.util.*;3233import java.security.*;34import java.security.interfaces.*;35import java.security.spec.*;3637public class TestKeyFactory {3839private final static String BASE = System.getProperty("test.src", ".");4041private static final char[] password = "test12".toCharArray();4243static KeyStore getKeyStore() throws Exception {44InputStream in = new FileInputStream(new File(BASE, "rsakeys.ks"));45KeyStore ks = KeyStore.getInstance("JKS");46ks.load(in, password);47in.close();48return ks;49}5051/**52* Test that key1 (reference key) and key2 (key to be tested) are53* equivalent54*/55private static void testKey(Key key1, Key key2) throws Exception {56if (key2.getAlgorithm().equals("RSA") == false) {57throw new Exception("Algorithm not RSA");58}59if (key1 instanceof PublicKey) {60if (key2.getFormat().equals("X.509") == false) {61throw new Exception("Format not X.509");62}63} else if (key1 instanceof PrivateKey) {64if (key2.getFormat().equals("PKCS#8") == false) {65throw new Exception("Format not PKCS#8");66}67}68if (key1.equals(key2) == false) {69throw new Exception("Keys not equal");70}71if (Arrays.equals(key1.getEncoded(), key2.getEncoded()) == false) {72throw new Exception("Encodings not equal");73}74}7576private static void testPublic(KeyFactory kf, PublicKey key) throws Exception {77System.out.println("Testing public key...");78PublicKey key2 = (PublicKey)kf.translateKey(key);79KeySpec rsaSpec = kf.getKeySpec(key, RSAPublicKeySpec.class);80PublicKey key3 = kf.generatePublic(rsaSpec);81KeySpec x509Spec = kf.getKeySpec(key, X509EncodedKeySpec.class);82PublicKey key4 = kf.generatePublic(x509Spec);83KeySpec x509Spec2 = new X509EncodedKeySpec(key.getEncoded());84PublicKey key5 = kf.generatePublic(x509Spec2);85testKey(key, key);86testKey(key, key2);87testKey(key, key3);88testKey(key, key4);89testKey(key, key5);90}9192private static void testPrivate(KeyFactory kf, PrivateKey key) throws Exception {93System.out.println("Testing private key...");94PrivateKey key2 = (PrivateKey)kf.translateKey(key);95KeySpec rsaSpec = kf.getKeySpec(key, RSAPrivateCrtKeySpec.class);96PrivateKey key3 = kf.generatePrivate(rsaSpec);97KeySpec pkcs8Spec = kf.getKeySpec(key, PKCS8EncodedKeySpec.class);98PrivateKey key4 = kf.generatePrivate(pkcs8Spec);99KeySpec pkcs8Spec2 = new PKCS8EncodedKeySpec(key.getEncoded());100PrivateKey key5 = kf.generatePrivate(pkcs8Spec2);101testKey(key, key);102testKey(key, key2);103testKey(key, key3);104testKey(key, key4);105testKey(key, key5);106107KeySpec rsaSpec2 = kf.getKeySpec(key, RSAPrivateKeySpec.class);108PrivateKey key6 = kf.generatePrivate(rsaSpec2);109RSAPrivateKey rsaKey = (RSAPrivateKey)key;110KeySpec rsaSpec3 = new RSAPrivateKeySpec(rsaKey.getModulus(), rsaKey.getPrivateExponent());111PrivateKey key7 = kf.generatePrivate(rsaSpec3);112testKey(key6, key6);113testKey(key6, key7);114}115116private static void test(KeyFactory kf, Key key) throws Exception {117if (key.getAlgorithm().equals("RSA") == false) {118System.out.println("Not an RSA key, ignoring");119}120if (key instanceof PublicKey) {121testPublic(kf, (PublicKey)key);122} else if (key instanceof PrivateKey) {123testPrivate(kf, (PrivateKey)key);124}125}126127public static void main(String[] args) throws Exception {128long start = System.currentTimeMillis();129KeyStore ks = getKeyStore();130KeyFactory kf = KeyFactory.getInstance("RSA", "SunRsaSign");131for (Enumeration e = ks.aliases(); e.hasMoreElements(); ) {132String alias = (String)e.nextElement();133Key key = null;134if (ks.isKeyEntry(alias)) {135test(kf, ks.getKey(alias, password));136test(kf, ks.getCertificate(alias).getPublicKey());137}138}139long stop = System.currentTimeMillis();140System.out.println("All tests passed (" + (stop - start) + " ms).");141}142}143144145