Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/mscapi/VeryLongAlias.java
41152 views
1
/*
2
* Copyright (c) 2019, 2020, 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 8223063 8153005
27
* @requires os.family == "windows"
28
* @library /test/lib
29
* @summary Support CNG RSA keys
30
*/
31
32
import jdk.test.lib.SecurityTools;
33
import jdk.test.lib.process.ProcessTools;
34
35
import java.io.File;
36
import java.security.KeyStore;
37
import java.security.MessageDigest;
38
import java.security.PrivateKey;
39
import java.security.PublicKey;
40
import java.security.Signature;
41
import java.security.cert.X509Certificate;
42
import java.util.List;
43
import java.util.Random;
44
45
public class VeryLongAlias {
46
47
static String alias = String.format("%0512d", new Random().nextInt(100000));
48
49
public static void main(String[] args) throws Throwable {
50
51
// Using the old algorithms to make sure the file is recognized
52
// by the certutil command on old versions of Windows.
53
SecurityTools.keytool(
54
"-J-Dkeystore.pkcs12.legacy"
55
+ " -genkeypair -storetype pkcs12 -keystore ks"
56
+ " -storepass changeit -keyalg RSA -dname CN=A -alias "
57
+ alias);
58
String id = ((X509Certificate)KeyStore.getInstance(
59
new File("ks"), "changeit".toCharArray())
60
.getCertificate(alias)).getSerialNumber().toString(16);
61
try {
62
// Importing pkcs12 file. Long alias is only supported by CNG.
63
ProcessTools.executeCommand("certutil", "-v", "-p", "changeit",
64
"-csp", "Microsoft Software Key Storage Provider",
65
"-user", "-importpfx", "MY", "ks", "NoRoot,NoExport")
66
.shouldHaveExitValue(0);
67
test();
68
} finally {
69
ProcessTools.executeCommand("certutil", "-user", "-delstore", "MY",
70
id);
71
}
72
}
73
74
static void test() throws Exception {
75
76
char[] pass = "changeit".toCharArray();
77
78
KeyStore k1 = KeyStore.getInstance("Windows-MY");
79
k1.load(null, null);
80
81
KeyStore k2 = KeyStore.getInstance(new File("ks"), pass);
82
83
PrivateKey p1 = (PrivateKey)k1.getKey(alias, null);
84
PublicKey u1 = k1.getCertificate(alias).getPublicKey();
85
86
PrivateKey p2 = (PrivateKey)k2.getKey(alias, pass);
87
PublicKey u2 = k2.getCertificate(alias).getPublicKey();
88
89
System.out.println(p1.toString());
90
System.out.println(u1.toString());
91
if (!p1.toString().contains("type=CNG")) {
92
throw new Exception("Not a CNG key");
93
}
94
95
testSignature(p1, u1);
96
testSignature(p1, u2);
97
testSignature(p2, u1);
98
testSignature(p2, u2);
99
}
100
101
static void testSignature(PrivateKey p, PublicKey u) throws Exception {
102
byte[] data = "hello".getBytes();
103
for (String alg : List.of(
104
"NONEwithRSA", "SHA1withRSA",
105
"SHA256withRSA", "SHA512withRSA")) {
106
if (alg.contains("NONE")) {
107
data = MessageDigest.getInstance("SHA-256").digest(data);
108
}
109
Signature s1 = Signature.getInstance(alg);
110
Signature s2 = Signature.getInstance(alg);
111
s1.initSign(p);
112
s2.initVerify(u);
113
s1.update(data);
114
s2.update(data);
115
if (!s2.verify(s1.sign())) {
116
throw new Exception("Error");
117
}
118
}
119
}
120
}
121
122