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