Path: blob/master/test/jdk/sun/security/pkcs11/ec/TestKeyFactory.java
41153 views
/*1* Copyright (c) 2006, 2020, 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 /test/lib ..29* @modules jdk.crypto.cryptoki30* @run main/othervm TestKeyFactory31* @run main/othervm -Djava.security.manager=allow TestKeyFactory sm32*/3334import java.security.Key;35import java.security.KeyFactory;36import java.security.KeyPair;37import java.security.KeyPairGenerator;38import java.security.PrivateKey;39import java.security.Provider;40import java.security.PublicKey;41import java.security.spec.ECPrivateKeySpec;42import java.security.spec.ECPublicKeySpec;43import java.security.spec.KeySpec;44import java.security.spec.PKCS8EncodedKeySpec;45import java.security.spec.X509EncodedKeySpec;46import java.util.Arrays;4748public class TestKeyFactory extends PKCS11Test {4950/**51* Test that key1 (reference key) and key2 (key to be tested) are52* equivalent53*/54private static void testKey(Key key1, Key key2) throws Exception {55if (key2.getAlgorithm().equals("EC") == false) {56throw new Exception("Algorithm not EC");57}58if (key1 instanceof PublicKey) {59if (key2.getFormat().equals("X.509") == false) {60throw new Exception("Format not X.509");61}62} else if (key1 instanceof PrivateKey) {63if (key2.getFormat().equals("PKCS#8") == false) {64throw new Exception("Format not PKCS#8");65}66}67if (key1.equals(key2) == false) {68System.out.println("key1: " + key1);69System.out.println("key2: " + key2);70System.out.println("enc1: " + toString(key1.getEncoded()));71System.out.println("enc2: " + toString(key2.getEncoded()));72throw new Exception("Keys not equal");73}74if (Arrays.equals(key1.getEncoded(), key2.getEncoded()) == false) {75throw new Exception("Encodings not equal");76}77}7879private static void testPublic(KeyFactory kf, PublicKey key) throws Exception {80System.out.println("Testing public key...");81PublicKey key2 = (PublicKey)kf.translateKey(key);82KeySpec keySpec = kf.getKeySpec(key, ECPublicKeySpec.class);83PublicKey key3 = kf.generatePublic(keySpec);84KeySpec x509Spec = kf.getKeySpec(key, X509EncodedKeySpec.class);85PublicKey key4 = kf.generatePublic(x509Spec);86KeySpec x509Spec2 = new X509EncodedKeySpec(key.getEncoded());87PublicKey key5 = kf.generatePublic(x509Spec2);88testKey(key, key);89testKey(key, key2);90testKey(key, key3);91testKey(key, key4);92testKey(key, key5);93}9495private static void testPrivate(KeyFactory kf, PrivateKey key) throws Exception {96System.out.println("Testing private key...");97PrivateKey key2 = (PrivateKey)kf.translateKey(key);98KeySpec keySpec = kf.getKeySpec(key, ECPrivateKeySpec.class);99PrivateKey key3 = kf.generatePrivate(keySpec);100KeySpec pkcs8Spec = kf.getKeySpec(key, PKCS8EncodedKeySpec.class);101PrivateKey key4 = kf.generatePrivate(pkcs8Spec);102KeySpec pkcs8Spec2 = new PKCS8EncodedKeySpec(key.getEncoded());103PrivateKey key5 = kf.generatePrivate(pkcs8Spec2);104testKey(key, key);105testKey(key, key2);106testKey(key, key3);107testKey(key, key4);108testKey(key, key5);109}110111private static void test(KeyFactory kf, Key key) throws Exception {112if (key.getAlgorithm().equals("EC") == false) {113throw new Exception("Not an EC key");114}115if (key instanceof PublicKey) {116testPublic(kf, (PublicKey)key);117} else if (key instanceof PrivateKey) {118testPrivate(kf, (PrivateKey)key);119}120}121122public static void main(String[] args) throws Exception {123main(new TestKeyFactory(), args);124}125126@Override127public void main(Provider p) throws Exception {128if (p.getService("KeyFactory", "EC") == null) {129System.out.println("Provider does not support EC, skipping");130return;131}132int[] keyLengths = {256, 521};133KeyFactory kf = KeyFactory.getInstance("EC", p);134for (int len : keyLengths) {135System.out.println("Length " + len);136KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC", p);137kpg.initialize(len);138KeyPair kp = kpg.generateKeyPair();139test(kf, kp.getPrivate());140test(kf, kp.getPublic());141}142}143}144145146