0N/A/*
2362N/A * Copyright (c) 2006, 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 6405536
0N/A * @summary Test the P11ECKeyFactory
0N/A * @author Andreas Sterbenz
0N/A * @library ..
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 extends PKCS11Test {
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("EC") == false) {
0N/A throw new Exception("Algorithm not EC");
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 System.out.println("key1: " + key1);
0N/A System.out.println("key2: " + key2);
0N/A System.out.println("enc1: " + toString(key1.getEncoded()));
0N/A System.out.println("enc2: " + toString(key2.getEncoded()));
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 keySpec = kf.getKeySpec(key, ECPublicKeySpec.class);
0N/A PublicKey key3 = kf.generatePublic(keySpec);
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 keySpec = kf.getKeySpec(key, ECPrivateKeySpec.class);
0N/A PrivateKey key3 = kf.generatePrivate(keySpec);
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
0N/A private static void test(KeyFactory kf, Key key) throws Exception {
0N/A if (key.getAlgorithm().equals("EC") == false) {
0N/A throw new Exception("Not an EC key");
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 main(new TestKeyFactory());
0N/A }
0N/A
0N/A public void main(Provider p) throws Exception {
0N/A if (p.getService("KeyFactory", "EC") == null) {
0N/A System.out.println("Provider does not support EC, skipping");
0N/A return;
0N/A }
0N/A int[] keyLengths = {192, 163, 521, 409};
0N/A KeyFactory kf = KeyFactory.getInstance("EC", p);
0N/A for (int len : keyLengths) {
0N/A KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC", p);
0N/A kpg.initialize(len);
0N/A KeyPair kp = kpg.generateKeyPair();
0N/A test(kf, kp.getPrivate());
0N/A test(kf, kp.getPublic());
0N/A }
0N/A }
0N/A}