1 /* 2 * Copyright (c) 2006, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 6405536 27 * @summary Test the P11ECKeyFactory 28 * @author Andreas Sterbenz 29 * @library .. 30 * @modules jdk.crypto.token 31 * @run main/othervm TestKeyFactory 32 * @run main/othervm TestKeyFactory sm 33 */ 34 35 import java.security.Key; 36 import java.security.KeyFactory; 37 import java.security.KeyPair; 38 import java.security.KeyPairGenerator; 39 import java.security.PrivateKey; 40 import java.security.Provider; 41 import java.security.PublicKey; 42 import java.security.spec.ECPrivateKeySpec; 43 import java.security.spec.ECPublicKeySpec; 44 import java.security.spec.KeySpec; 45 import java.security.spec.PKCS8EncodedKeySpec; 46 import java.security.spec.X509EncodedKeySpec; 47 import java.util.Arrays; 48 49 public class TestKeyFactory extends PKCS11Test { 50 51 /** 52 * Test that key1 (reference key) and key2 (key to be tested) are 53 * equivalent 54 */ 55 private static void testKey(Key key1, Key key2) throws Exception { 56 if (key2.getAlgorithm().equals("EC") == false) { 57 throw new Exception("Algorithm not EC"); 58 } 59 if (key1 instanceof PublicKey) { 60 if (key2.getFormat().equals("X.509") == false) { 61 throw new Exception("Format not X.509"); 62 } 63 } else if (key1 instanceof PrivateKey) { 64 if (key2.getFormat().equals("PKCS#8") == false) { 65 throw new Exception("Format not PKCS#8"); 66 } 67 } 68 if (key1.equals(key2) == false) { 69 System.out.println("key1: " + key1); 70 System.out.println("key2: " + key2); 71 System.out.println("enc1: " + toString(key1.getEncoded())); 72 System.out.println("enc2: " + toString(key2.getEncoded())); 73 throw new Exception("Keys not equal"); 74 } 75 if (Arrays.equals(key1.getEncoded(), key2.getEncoded()) == false) { 76 throw new Exception("Encodings not equal"); 77 } 78 } 79 80 private static void testPublic(KeyFactory kf, PublicKey key) throws Exception { 81 System.out.println("Testing public key..."); 82 PublicKey key2 = (PublicKey)kf.translateKey(key); 83 KeySpec keySpec = kf.getKeySpec(key, ECPublicKeySpec.class); 84 PublicKey key3 = kf.generatePublic(keySpec); 85 KeySpec x509Spec = kf.getKeySpec(key, X509EncodedKeySpec.class); 86 PublicKey key4 = kf.generatePublic(x509Spec); 87 KeySpec x509Spec2 = new X509EncodedKeySpec(key.getEncoded()); 88 PublicKey key5 = kf.generatePublic(x509Spec2); 89 testKey(key, key); 90 testKey(key, key2); 91 testKey(key, key3); 92 testKey(key, key4); 93 testKey(key, key5); 94 } 95 96 private static void testPrivate(KeyFactory kf, PrivateKey key) throws Exception { 97 System.out.println("Testing private key..."); 98 PrivateKey key2 = (PrivateKey)kf.translateKey(key); 99 KeySpec keySpec = kf.getKeySpec(key, ECPrivateKeySpec.class); 100 PrivateKey key3 = kf.generatePrivate(keySpec); 101 KeySpec pkcs8Spec = kf.getKeySpec(key, PKCS8EncodedKeySpec.class); 102 PrivateKey key4 = kf.generatePrivate(pkcs8Spec); 103 KeySpec pkcs8Spec2 = new PKCS8EncodedKeySpec(key.getEncoded()); 104 PrivateKey key5 = kf.generatePrivate(pkcs8Spec2); 105 testKey(key, key); 106 testKey(key, key2); 107 testKey(key, key3); 108 testKey(key, key4); 109 testKey(key, key5); 110 } 111 112 private static void test(KeyFactory kf, Key key) throws Exception { 113 if (key.getAlgorithm().equals("EC") == false) { 114 throw new Exception("Not an EC key"); 115 } 116 if (key instanceof PublicKey) { 117 testPublic(kf, (PublicKey)key); 118 } else if (key instanceof PrivateKey) { 119 testPrivate(kf, (PrivateKey)key); 120 } 121 } 122 123 public static void main(String[] args) throws Exception { 124 main(new TestKeyFactory(), args); 125 } 126 127 @Override 128 public void main(Provider p) throws Exception { 129 if (p.getService("KeyFactory", "EC") == null) { 130 System.out.println("Provider does not support EC, skipping"); 131 return; 132 } 133 int[] keyLengths = {192, 163, 409, 521}; 134 int len = 0; 135 if (getNSSECC() == ECCState.Basic) { 136 System.out.println("NSS Basic ECC only. Skipping 192, 163, & 409"); 137 len = 3; 138 } 139 KeyFactory kf = KeyFactory.getInstance("EC", p); 140 for (; keyLengths.length > len ; len++) { 141 System.out.println("Length "+keyLengths[len]); 142 KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC", p); 143 kpg.initialize(keyLengths[len]); 144 KeyPair kp = kpg.generateKeyPair(); 145 test(kf, kp.getPrivate()); 146 test(kf, kp.getPublic()); 147 } 148 } 149 }