Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/ec/TestKeyFactory.java
38855 views
/*1* Copyright (c) 2006, 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 640553626* @summary Test the P11ECKeyFactory27* @author Andreas Sterbenz28* @library ..29* @run main/othervm TestKeyFactory30* @run main/othervm TestKeyFactory sm31*/3233import java.security.Key;34import java.security.KeyFactory;35import java.security.KeyPair;36import java.security.KeyPairGenerator;37import java.security.PrivateKey;38import java.security.Provider;39import java.security.PublicKey;40import java.security.spec.ECPrivateKeySpec;41import java.security.spec.ECPublicKeySpec;42import java.security.spec.KeySpec;43import java.security.spec.PKCS8EncodedKeySpec;44import java.security.spec.X509EncodedKeySpec;45import java.util.Arrays;4647public class TestKeyFactory extends PKCS11Test {4849/**50* Test that key1 (reference key) and key2 (key to be tested) are51* equivalent52*/53private static void testKey(Key key1, Key key2) throws Exception {54if (key2.getAlgorithm().equals("EC") == false) {55throw new Exception("Algorithm not EC");56}57if (key1 instanceof PublicKey) {58if (key2.getFormat().equals("X.509") == false) {59throw new Exception("Format not X.509");60}61} else if (key1 instanceof PrivateKey) {62if (key2.getFormat().equals("PKCS#8") == false) {63throw new Exception("Format not PKCS#8");64}65}66if (key1.equals(key2) == false) {67System.out.println("key1: " + key1);68System.out.println("key2: " + key2);69System.out.println("enc1: " + toString(key1.getEncoded()));70System.out.println("enc2: " + toString(key2.getEncoded()));71throw new Exception("Keys not equal");72}73if (Arrays.equals(key1.getEncoded(), key2.getEncoded()) == false) {74throw new Exception("Encodings not equal");75}76}7778private static void testPublic(KeyFactory kf, PublicKey key) throws Exception {79System.out.println("Testing public key...");80PublicKey key2 = (PublicKey)kf.translateKey(key);81KeySpec keySpec = kf.getKeySpec(key, ECPublicKeySpec.class);82PublicKey key3 = kf.generatePublic(keySpec);83KeySpec x509Spec = kf.getKeySpec(key, X509EncodedKeySpec.class);84PublicKey key4 = kf.generatePublic(x509Spec);85KeySpec x509Spec2 = new X509EncodedKeySpec(key.getEncoded());86PublicKey key5 = kf.generatePublic(x509Spec2);87testKey(key, key);88testKey(key, key2);89testKey(key, key3);90testKey(key, key4);91testKey(key, key5);92}9394private static void testPrivate(KeyFactory kf, PrivateKey key) throws Exception {95System.out.println("Testing private key...");96PrivateKey key2 = (PrivateKey)kf.translateKey(key);97KeySpec keySpec = kf.getKeySpec(key, ECPrivateKeySpec.class);98PrivateKey key3 = kf.generatePrivate(keySpec);99KeySpec pkcs8Spec = kf.getKeySpec(key, PKCS8EncodedKeySpec.class);100PrivateKey key4 = kf.generatePrivate(pkcs8Spec);101KeySpec pkcs8Spec2 = new PKCS8EncodedKeySpec(key.getEncoded());102PrivateKey key5 = kf.generatePrivate(pkcs8Spec2);103testKey(key, key);104testKey(key, key2);105testKey(key, key3);106testKey(key, key4);107testKey(key, key5);108}109110private static void test(KeyFactory kf, Key key) throws Exception {111if (key.getAlgorithm().equals("EC") == false) {112throw new Exception("Not an EC key");113}114if (key instanceof PublicKey) {115testPublic(kf, (PublicKey)key);116} else if (key instanceof PrivateKey) {117testPrivate(kf, (PrivateKey)key);118}119}120121public static void main(String[] args) throws Exception {122main(new TestKeyFactory(), args);123}124125@Override126public void main(Provider p) throws Exception {127if (p.getService("KeyFactory", "EC") == null) {128System.out.println("Provider does not support EC, skipping");129return;130}131int[] keyLengths = {192, 163, 409, 521};132int len = 0;133if (getNSSECC() == ECCState.Basic) {134System.out.println("NSS Basic ECC only. Skipping 192, 163, & 409");135len = 3;136}137KeyFactory kf = KeyFactory.getInstance("EC", p);138for (; keyLengths.length > len ; len++) {139System.out.println("Length "+keyLengths[len]);140KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC", p);141kpg.initialize(keyLengths[len]);142KeyPair kp = kpg.generateKeyPair();143test(kf, kp.getPrivate());144test(kf, kp.getPublic());145}146}147}148149150