Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/rsa/TestKeyFactory.java
38855 views
/*1* Copyright (c) 2003, 2016, 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 485696626* @summary Test KeyFactory of the new RSA provider27* @author Andreas Sterbenz28* @library ..29* @run main/othervm TestKeyFactory30* @run main/othervm TestKeyFactory sm rsakeys.ks.policy31*/3233import java.io.*;34import java.util.*;3536import java.security.*;37import java.security.spec.*;3839public class TestKeyFactory extends PKCS11Test {4041private static final char[] password = "test12".toCharArray();4243static KeyStore getKeyStore() throws Exception {44KeyStore ks;45try (InputStream in = new FileInputStream(new File(BASE, "rsakeys.ks"))) {46ks = KeyStore.getInstance("JKS");47ks.load(in, password);48}49return ks;50}5152/**53* Test that key1 (reference key) and key2 (key to be tested) are54* equivalent55*/56private static void testKey(Key key1, Key key2) throws Exception {57if (key2.getAlgorithm().equals("RSA") == false) {58throw new Exception("Algorithm not RSA");59}60if (key1 instanceof PublicKey) {61if (key2.getFormat().equals("X.509") == false) {62throw new Exception("Format not X.509");63}64} else if (key1 instanceof PrivateKey) {65if (key2.getFormat().equals("PKCS#8") == false) {66throw new Exception("Format not PKCS#8");67}68}69if (key1.equals(key2) == false) {70throw new Exception("Keys not equal");71}72if (Arrays.equals(key1.getEncoded(), key2.getEncoded()) == false) {73throw new Exception("Encodings not equal");74}75}7677private static void testPublic(KeyFactory kf, PublicKey key) throws Exception {78System.out.println("Testing public key...");79PublicKey key2 = (PublicKey)kf.translateKey(key);80KeySpec rsaSpec = kf.getKeySpec(key, RSAPublicKeySpec.class);81PublicKey key3 = kf.generatePublic(rsaSpec);82KeySpec x509Spec = kf.getKeySpec(key, X509EncodedKeySpec.class);83PublicKey key4 = kf.generatePublic(x509Spec);84KeySpec x509Spec2 = new X509EncodedKeySpec(key.getEncoded());85PublicKey key5 = kf.generatePublic(x509Spec2);86testKey(key, key);87testKey(key, key2);88testKey(key, key3);89testKey(key, key4);90testKey(key, key5);91}9293private static void testPrivate(KeyFactory kf, PrivateKey key) throws Exception {94System.out.println("Testing private key...");95PrivateKey key2 = (PrivateKey)kf.translateKey(key);96KeySpec rsaSpec = kf.getKeySpec(key, RSAPrivateCrtKeySpec.class);97PrivateKey key3 = kf.generatePrivate(rsaSpec);98KeySpec pkcs8Spec = kf.getKeySpec(key, PKCS8EncodedKeySpec.class);99PrivateKey key4 = kf.generatePrivate(pkcs8Spec);100KeySpec pkcs8Spec2 = new PKCS8EncodedKeySpec(key.getEncoded());101PrivateKey key5 = kf.generatePrivate(pkcs8Spec2);102testKey(key, key);103testKey(key, key2);104testKey(key, key3);105testKey(key, key4);106testKey(key, key5);107108// XXX PKCS#11 providers may not support non-CRT keys (e.g. NSS)109// KeySpec rsaSpec2 = kf.getKeySpec(key, RSAPrivateKeySpec.class);110// PrivateKey key6 = kf.generatePrivate(rsaSpec2);111// RSAPrivateKey rsaKey = (RSAPrivateKey)key;112// KeySpec rsaSpec3 = new RSAPrivateKeySpec(rsaKey.getModulus(), rsaKey.getPrivateExponent());113// PrivateKey key7 = kf.generatePrivate(rsaSpec3);114// testKey(key6, key6);115// testKey(key6, key7);116}117118private static void test(KeyFactory kf, Key key) throws Exception {119if (key.getAlgorithm().equals("RSA") == false) {120System.out.println("Not an RSA key, ignoring");121}122if (key instanceof PublicKey) {123testPublic(kf, (PublicKey)key);124} else if (key instanceof PrivateKey) {125testPrivate(kf, (PrivateKey)key);126}127}128129public static void main(String[] args) throws Exception {130main(new TestKeyFactory(), args);131}132133@Override134public void main(Provider p) throws Exception {135long start = System.currentTimeMillis();136KeyStore ks = getKeyStore();137KeyFactory kf = KeyFactory.getInstance("RSA", p);138for (Enumeration e = ks.aliases(); e.hasMoreElements(); ) {139String alias = (String)e.nextElement();140Key key = null;141if (ks.isKeyEntry(alias)) {142test(kf, ks.getKey(alias, password));143test(kf, ks.getCertificate(alias).getPublicKey());144}145}146long stop = System.currentTimeMillis();147System.out.println("All tests passed (" + (stop - start) + " ms).");148}149}150151152