Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/ec/xec/XECKeyFormat.java
41155 views
1
/*
2
* Copyright (c) 2018, 2021, 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 8213363
27
* @summary Check for correct formatting of X25519/X448 private keys
28
* @library /test/lib
29
* @modules java.base/sun.security.util
30
* @run main XECKeyFormat
31
*/
32
33
import java.security.*;
34
import java.security.spec.*;
35
import java.security.interfaces.*;
36
import java.io.*;
37
import java.nio.file.*;
38
import java.math.*;
39
import java.util.*;
40
41
import java.util.HexFormat;
42
43
import sun.security.util.*;
44
45
public class XECKeyFormat {
46
47
private interface Test {
48
public void runTest(Provider p) throws Exception;
49
}
50
51
private static void forEachProvider(Test t, String algName)
52
throws Exception {
53
54
int tested = 0;
55
for (Provider p : Security.getProviders()) {
56
Provider.Service s = p.getService("KeyPairGenerator", algName);
57
if (s != null) {
58
t.runTest(p);
59
tested++;
60
}
61
}
62
if (tested == 0) {
63
throw new RuntimeException("no service found for " + algName);
64
}
65
}
66
67
private static Map<String, String> privKeys = Map.of(
68
"X25519",
69
"302e020100300506032b656e0422042010fdf8358b9cda51eb98d2479fb092a80639" +
70
"bf31c5e7c5ba5000387fbf9c6678",
71
"X448",
72
"3046020100300506032b656f043a043880998f387e05852d217c1d715b177c24aa7b" +
73
"f3f4c3a72223f4983597b9ab2ed4793c30d871c24388b380d80bb36d963f5c276219" +
74
"b0677fed"
75
);
76
77
private static List<String> pubKeys = List.of(
78
"302a300506032b656e03210019bf44096984cdfe8541bac167dc3b96c85086aa30b6" +
79
"b6cb0c5c38ad703166e1"
80
);
81
82
public static void main(String[] args) throws Exception {
83
privKeyTest("X25519");
84
privKeyTest("X448");
85
pubKeyTest();
86
}
87
88
private static void pubKeyTest() throws Exception {
89
forEachProvider(XECKeyFormat::pubKeyTest, "XDH");
90
}
91
92
private static void pubKeyTest(Provider p) throws Exception {
93
for (String s : pubKeys) {
94
pubKeyTest(p, s);
95
}
96
}
97
98
private static void pubKeyTest(Provider p, String key) throws Exception {
99
// ensure that a properly-formatted key can be read
100
byte[] encodedKey = HexFormat.of().parseHex(key);
101
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(encodedKey);
102
KeyFactory kf = KeyFactory.getInstance("XDH", p);
103
kf.generatePublic(keySpec);
104
}
105
106
private static void privKeyTest(String algName) throws Exception {
107
108
forEachProvider(p -> privKeyTest(algName, p), algName);
109
}
110
111
private static void privKeyTest(String algName, Provider p)
112
throws Exception {
113
114
System.out.println("Testing " + algName + " in " + p.getName());
115
116
// ensure format produced is correct
117
KeyPairGenerator kpg = KeyPairGenerator.getInstance(algName, p);
118
KeyPair kp = kpg.generateKeyPair();
119
PrivateKey priv = kp.getPrivate();
120
checkPrivKeyFormat(priv.getEncoded());
121
KeyFactory kf = KeyFactory.getInstance(algName, p);
122
PKCS8EncodedKeySpec keySpec =
123
kf.getKeySpec(priv, PKCS8EncodedKeySpec.class);
124
checkPrivKeyFormat(keySpec.getEncoded());
125
126
// ensure that a properly-formatted key can be read
127
byte[] encodedKey = HexFormat.of().parseHex(privKeys.get(algName));
128
keySpec = new PKCS8EncodedKeySpec(encodedKey);
129
kf.generatePrivate(keySpec);
130
}
131
132
private static void checkPrivKeyFormat(byte[] key) throws IOException {
133
// key value should be nested octet strings
134
DerValue val = new DerValue(new ByteArrayInputStream(key));
135
BigInteger version = val.data.getBigInteger();
136
DerValue algId = val.data.getDerValue();
137
byte[] keyValue = val.data.getOctetString();
138
val = new DerValue(new ByteArrayInputStream(keyValue));
139
if (val.tag != DerValue.tag_OctetString) {
140
throw new RuntimeException("incorrect format");
141
}
142
}
143
}
144
145
146
147