Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/ec/TestKeyFactory.java
38855 views
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
* @run main/othervm TestKeyFactory
31
* @run main/othervm TestKeyFactory sm
32
*/
33
34
import java.security.Key;
35
import java.security.KeyFactory;
36
import java.security.KeyPair;
37
import java.security.KeyPairGenerator;
38
import java.security.PrivateKey;
39
import java.security.Provider;
40
import java.security.PublicKey;
41
import java.security.spec.ECPrivateKeySpec;
42
import java.security.spec.ECPublicKeySpec;
43
import java.security.spec.KeySpec;
44
import java.security.spec.PKCS8EncodedKeySpec;
45
import java.security.spec.X509EncodedKeySpec;
46
import java.util.Arrays;
47
48
public class TestKeyFactory extends PKCS11Test {
49
50
/**
51
* Test that key1 (reference key) and key2 (key to be tested) are
52
* equivalent
53
*/
54
private static void testKey(Key key1, Key key2) throws Exception {
55
if (key2.getAlgorithm().equals("EC") == false) {
56
throw new Exception("Algorithm not EC");
57
}
58
if (key1 instanceof PublicKey) {
59
if (key2.getFormat().equals("X.509") == false) {
60
throw new Exception("Format not X.509");
61
}
62
} else if (key1 instanceof PrivateKey) {
63
if (key2.getFormat().equals("PKCS#8") == false) {
64
throw new Exception("Format not PKCS#8");
65
}
66
}
67
if (key1.equals(key2) == false) {
68
System.out.println("key1: " + key1);
69
System.out.println("key2: " + key2);
70
System.out.println("enc1: " + toString(key1.getEncoded()));
71
System.out.println("enc2: " + toString(key2.getEncoded()));
72
throw new Exception("Keys not equal");
73
}
74
if (Arrays.equals(key1.getEncoded(), key2.getEncoded()) == false) {
75
throw new Exception("Encodings not equal");
76
}
77
}
78
79
private static void testPublic(KeyFactory kf, PublicKey key) throws Exception {
80
System.out.println("Testing public key...");
81
PublicKey key2 = (PublicKey)kf.translateKey(key);
82
KeySpec keySpec = kf.getKeySpec(key, ECPublicKeySpec.class);
83
PublicKey key3 = kf.generatePublic(keySpec);
84
KeySpec x509Spec = kf.getKeySpec(key, X509EncodedKeySpec.class);
85
PublicKey key4 = kf.generatePublic(x509Spec);
86
KeySpec x509Spec2 = new X509EncodedKeySpec(key.getEncoded());
87
PublicKey key5 = kf.generatePublic(x509Spec2);
88
testKey(key, key);
89
testKey(key, key2);
90
testKey(key, key3);
91
testKey(key, key4);
92
testKey(key, key5);
93
}
94
95
private static void testPrivate(KeyFactory kf, PrivateKey key) throws Exception {
96
System.out.println("Testing private key...");
97
PrivateKey key2 = (PrivateKey)kf.translateKey(key);
98
KeySpec keySpec = kf.getKeySpec(key, ECPrivateKeySpec.class);
99
PrivateKey key3 = kf.generatePrivate(keySpec);
100
KeySpec pkcs8Spec = kf.getKeySpec(key, PKCS8EncodedKeySpec.class);
101
PrivateKey key4 = kf.generatePrivate(pkcs8Spec);
102
KeySpec pkcs8Spec2 = new PKCS8EncodedKeySpec(key.getEncoded());
103
PrivateKey key5 = kf.generatePrivate(pkcs8Spec2);
104
testKey(key, key);
105
testKey(key, key2);
106
testKey(key, key3);
107
testKey(key, key4);
108
testKey(key, key5);
109
}
110
111
private static void test(KeyFactory kf, Key key) throws Exception {
112
if (key.getAlgorithm().equals("EC") == false) {
113
throw new Exception("Not an EC key");
114
}
115
if (key instanceof PublicKey) {
116
testPublic(kf, (PublicKey)key);
117
} else if (key instanceof PrivateKey) {
118
testPrivate(kf, (PrivateKey)key);
119
}
120
}
121
122
public static void main(String[] args) throws Exception {
123
main(new TestKeyFactory(), args);
124
}
125
126
@Override
127
public void main(Provider p) throws Exception {
128
if (p.getService("KeyFactory", "EC") == null) {
129
System.out.println("Provider does not support EC, skipping");
130
return;
131
}
132
int[] keyLengths = {192, 163, 409, 521};
133
int len = 0;
134
if (getNSSECC() == ECCState.Basic) {
135
System.out.println("NSS Basic ECC only. Skipping 192, 163, & 409");
136
len = 3;
137
}
138
KeyFactory kf = KeyFactory.getInstance("EC", p);
139
for (; keyLengths.length > len ; len++) {
140
System.out.println("Length "+keyLengths[len]);
141
KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC", p);
142
kpg.initialize(keyLengths[len]);
143
KeyPair kp = kpg.generateKeyPair();
144
test(kf, kp.getPrivate());
145
test(kf, kp.getPublic());
146
}
147
}
148
}
149
150