2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
|
2
|
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 |
*
|
5506
|
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.
|
2
|
22 |
*/
|
|
23 |
|
|
24 |
/**
|
|
25 |
* @test
|
|
26 |
* @bug 6405536
|
|
27 |
* @summary Test the P11ECKeyFactory
|
|
28 |
* @author Andreas Sterbenz
|
|
29 |
* @library ..
|
|
30 |
*/
|
|
31 |
|
|
32 |
import java.io.*;
|
|
33 |
import java.util.*;
|
|
34 |
|
|
35 |
import java.security.*;
|
|
36 |
import java.security.interfaces.*;
|
|
37 |
import java.security.spec.*;
|
|
38 |
|
|
39 |
public class TestKeyFactory extends PKCS11Test {
|
|
40 |
|
|
41 |
/**
|
|
42 |
* Test that key1 (reference key) and key2 (key to be tested) are
|
|
43 |
* equivalent
|
|
44 |
*/
|
|
45 |
private static void testKey(Key key1, Key key2) throws Exception {
|
|
46 |
if (key2.getAlgorithm().equals("EC") == false) {
|
|
47 |
throw new Exception("Algorithm not EC");
|
|
48 |
}
|
|
49 |
if (key1 instanceof PublicKey) {
|
|
50 |
if (key2.getFormat().equals("X.509") == false) {
|
|
51 |
throw new Exception("Format not X.509");
|
|
52 |
}
|
|
53 |
} else if (key1 instanceof PrivateKey) {
|
|
54 |
if (key2.getFormat().equals("PKCS#8") == false) {
|
|
55 |
throw new Exception("Format not PKCS#8");
|
|
56 |
}
|
|
57 |
}
|
|
58 |
if (key1.equals(key2) == false) {
|
|
59 |
System.out.println("key1: " + key1);
|
|
60 |
System.out.println("key2: " + key2);
|
|
61 |
System.out.println("enc1: " + toString(key1.getEncoded()));
|
|
62 |
System.out.println("enc2: " + toString(key2.getEncoded()));
|
|
63 |
throw new Exception("Keys not equal");
|
|
64 |
}
|
|
65 |
if (Arrays.equals(key1.getEncoded(), key2.getEncoded()) == false) {
|
|
66 |
throw new Exception("Encodings not equal");
|
|
67 |
}
|
|
68 |
}
|
|
69 |
|
|
70 |
private static void testPublic(KeyFactory kf, PublicKey key) throws Exception {
|
|
71 |
System.out.println("Testing public key...");
|
|
72 |
PublicKey key2 = (PublicKey)kf.translateKey(key);
|
|
73 |
KeySpec keySpec = kf.getKeySpec(key, ECPublicKeySpec.class);
|
|
74 |
PublicKey key3 = kf.generatePublic(keySpec);
|
|
75 |
KeySpec x509Spec = kf.getKeySpec(key, X509EncodedKeySpec.class);
|
|
76 |
PublicKey key4 = kf.generatePublic(x509Spec);
|
|
77 |
KeySpec x509Spec2 = new X509EncodedKeySpec(key.getEncoded());
|
|
78 |
PublicKey key5 = kf.generatePublic(x509Spec2);
|
|
79 |
testKey(key, key);
|
|
80 |
testKey(key, key2);
|
|
81 |
testKey(key, key3);
|
|
82 |
testKey(key, key4);
|
|
83 |
testKey(key, key5);
|
|
84 |
}
|
|
85 |
|
|
86 |
private static void testPrivate(KeyFactory kf, PrivateKey key) throws Exception {
|
|
87 |
System.out.println("Testing private key...");
|
|
88 |
PrivateKey key2 = (PrivateKey)kf.translateKey(key);
|
|
89 |
KeySpec keySpec = kf.getKeySpec(key, ECPrivateKeySpec.class);
|
|
90 |
PrivateKey key3 = kf.generatePrivate(keySpec);
|
|
91 |
KeySpec pkcs8Spec = kf.getKeySpec(key, PKCS8EncodedKeySpec.class);
|
|
92 |
PrivateKey key4 = kf.generatePrivate(pkcs8Spec);
|
|
93 |
KeySpec pkcs8Spec2 = new PKCS8EncodedKeySpec(key.getEncoded());
|
|
94 |
PrivateKey key5 = kf.generatePrivate(pkcs8Spec2);
|
|
95 |
testKey(key, key);
|
|
96 |
testKey(key, key2);
|
|
97 |
testKey(key, key3);
|
|
98 |
testKey(key, key4);
|
|
99 |
testKey(key, key5);
|
|
100 |
}
|
|
101 |
|
|
102 |
private static void test(KeyFactory kf, Key key) throws Exception {
|
|
103 |
if (key.getAlgorithm().equals("EC") == false) {
|
|
104 |
throw new Exception("Not an EC key");
|
|
105 |
}
|
|
106 |
if (key instanceof PublicKey) {
|
|
107 |
testPublic(kf, (PublicKey)key);
|
|
108 |
} else if (key instanceof PrivateKey) {
|
|
109 |
testPrivate(kf, (PrivateKey)key);
|
|
110 |
}
|
|
111 |
}
|
|
112 |
|
|
113 |
public static void main(String[] args) throws Exception {
|
|
114 |
main(new TestKeyFactory());
|
|
115 |
}
|
|
116 |
|
|
117 |
public void main(Provider p) throws Exception {
|
|
118 |
if (p.getService("KeyFactory", "EC") == null) {
|
|
119 |
System.out.println("Provider does not support EC, skipping");
|
|
120 |
return;
|
|
121 |
}
|
|
122 |
int[] keyLengths = {192, 163, 521, 409};
|
|
123 |
KeyFactory kf = KeyFactory.getInstance("EC", p);
|
|
124 |
for (int len : keyLengths) {
|
|
125 |
KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC", p);
|
|
126 |
kpg.initialize(len);
|
|
127 |
KeyPair kp = kpg.generateKeyPair();
|
|
128 |
test(kf, kp.getPrivate());
|
|
129 |
test(kf, kp.getPublic());
|
|
130 |
}
|
|
131 |
}
|
|
132 |
}
|