0N/A/*
2362N/A * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/**
0N/A * @test
0N/A * @bug 4853305
0N/A * @summary Test KeyFactory of the new RSA provider
0N/A * @author Andreas Sterbenz
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.util.*;
0N/A
0N/Aimport java.security.*;
0N/Aimport java.security.interfaces.*;
0N/Aimport java.security.spec.*;
0N/A
0N/Apublic class TestKeyFactory {
0N/A
0N/A private final static String BASE = System.getProperty("test.src", ".");
0N/A
0N/A private static final char[] password = "test12".toCharArray();
0N/A
0N/A static KeyStore getKeyStore() throws Exception {
0N/A InputStream in = new FileInputStream(new File(BASE, "rsakeys.ks"));
0N/A KeyStore ks = KeyStore.getInstance("JKS");
0N/A ks.load(in, password);
0N/A in.close();
0N/A return ks;
0N/A }
0N/A
0N/A /**
0N/A * Test that key1 (reference key) and key2 (key to be tested) are
0N/A * equivalent
0N/A */
0N/A private static void testKey(Key key1, Key key2) throws Exception {
0N/A if (key2.getAlgorithm().equals("RSA") == false) {
0N/A throw new Exception("Algorithm not RSA");
0N/A }
0N/A if (key1 instanceof PublicKey) {
0N/A if (key2.getFormat().equals("X.509") == false) {
0N/A throw new Exception("Format not X.509");
0N/A }
0N/A } else if (key1 instanceof PrivateKey) {
0N/A if (key2.getFormat().equals("PKCS#8") == false) {
0N/A throw new Exception("Format not PKCS#8");
0N/A }
0N/A }
0N/A if (key1.equals(key2) == false) {
0N/A throw new Exception("Keys not equal");
0N/A }
0N/A if (Arrays.equals(key1.getEncoded(), key2.getEncoded()) == false) {
0N/A throw new Exception("Encodings not equal");
0N/A }
0N/A }
0N/A
0N/A private static void testPublic(KeyFactory kf, PublicKey key) throws Exception {
0N/A System.out.println("Testing public key...");
0N/A PublicKey key2 = (PublicKey)kf.translateKey(key);
0N/A KeySpec rsaSpec = kf.getKeySpec(key, RSAPublicKeySpec.class);
0N/A PublicKey key3 = kf.generatePublic(rsaSpec);
0N/A KeySpec x509Spec = kf.getKeySpec(key, X509EncodedKeySpec.class);
0N/A PublicKey key4 = kf.generatePublic(x509Spec);
0N/A KeySpec x509Spec2 = new X509EncodedKeySpec(key.getEncoded());
0N/A PublicKey key5 = kf.generatePublic(x509Spec2);
0N/A testKey(key, key);
0N/A testKey(key, key2);
0N/A testKey(key, key3);
0N/A testKey(key, key4);
0N/A testKey(key, key5);
0N/A }
0N/A
0N/A private static void testPrivate(KeyFactory kf, PrivateKey key) throws Exception {
0N/A System.out.println("Testing private key...");
0N/A PrivateKey key2 = (PrivateKey)kf.translateKey(key);
0N/A KeySpec rsaSpec = kf.getKeySpec(key, RSAPrivateCrtKeySpec.class);
0N/A PrivateKey key3 = kf.generatePrivate(rsaSpec);
0N/A KeySpec pkcs8Spec = kf.getKeySpec(key, PKCS8EncodedKeySpec.class);
0N/A PrivateKey key4 = kf.generatePrivate(pkcs8Spec);
0N/A KeySpec pkcs8Spec2 = new PKCS8EncodedKeySpec(key.getEncoded());
0N/A PrivateKey key5 = kf.generatePrivate(pkcs8Spec2);
0N/A testKey(key, key);
0N/A testKey(key, key2);
0N/A testKey(key, key3);
0N/A testKey(key, key4);
0N/A testKey(key, key5);
0N/A
0N/A KeySpec rsaSpec2 = kf.getKeySpec(key, RSAPrivateKeySpec.class);
0N/A PrivateKey key6 = kf.generatePrivate(rsaSpec2);
0N/A RSAPrivateKey rsaKey = (RSAPrivateKey)key;
0N/A KeySpec rsaSpec3 = new RSAPrivateKeySpec(rsaKey.getModulus(), rsaKey.getPrivateExponent());
0N/A PrivateKey key7 = kf.generatePrivate(rsaSpec3);
0N/A testKey(key6, key6);
0N/A testKey(key6, key7);
0N/A }
0N/A
0N/A private static void test(KeyFactory kf, Key key) throws Exception {
0N/A if (key.getAlgorithm().equals("RSA") == false) {
0N/A System.out.println("Not an RSA key, ignoring");
0N/A }
0N/A if (key instanceof PublicKey) {
0N/A testPublic(kf, (PublicKey)key);
0N/A } else if (key instanceof PrivateKey) {
0N/A testPrivate(kf, (PrivateKey)key);
0N/A }
0N/A }
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A long start = System.currentTimeMillis();
0N/A KeyStore ks = getKeyStore();
0N/A KeyFactory kf = KeyFactory.getInstance("RSA", "SunRsaSign");
0N/A for (Enumeration e = ks.aliases(); e.hasMoreElements(); ) {
0N/A String alias = (String)e.nextElement();
0N/A Key key = null;
0N/A if (ks.isKeyEntry(alias)) {
0N/A test(kf, ks.getKey(alias, password));
0N/A test(kf, ks.getCertificate(alias).getPublicKey());
0N/A }
0N/A }
0N/A long stop = System.currentTimeMillis();
0N/A System.out.println("All tests passed (" + (stop - start) + " ms).");
0N/A }
0N/A}