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