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/mscapi/VeryLongAlias.java
38840 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
27
* @requires os.family == "windows"
28
* @library /lib/testlibrary
29
* @summary Support CNG RSA keys
30
*/
31
32
import jdk.testlibrary.SecurityTools;
33
import jdk.testlibrary.ProcessTools;
34
35
import java.io.File;
36
import java.io.FileInputStream;
37
import java.security.KeyStore;
38
import java.security.MessageDigest;
39
import java.security.PrivateKey;
40
import java.security.PublicKey;
41
import java.security.Signature;
42
import java.security.cert.X509Certificate;
43
import java.util.List;
44
import java.util.Random;
45
46
public class VeryLongAlias {
47
48
static String alias = String.format("%0512d", new Random().nextInt(100000));
49
50
public static void main(String[] args) throws Throwable {
51
52
SecurityTools.keytool("-genkeypair -storetype pkcs12 -keystore ks"
53
+ " -storepass changeit -keyalg RSA -dname CN=A -alias "
54
+ alias);
55
56
KeyStore ks = KeyStore.getInstance("PKCS12");
57
58
try (FileInputStream fis = new FileInputStream("ks")) {
59
ks.load(fis, "changeit".toCharArray());
60
}
61
62
String id = ((X509Certificate)ks.getCertificate(alias))
63
.getSerialNumber().toString(16);
64
try {
65
// Importing pkcs12 file. Long alias is only supported by CNG.
66
//ProcessTools.executeCommand("certutil", "-v", "-p", "changeit",
67
// "-csp", "Microsoft Software Key Storage Provider",
68
// "-user", "-importpfx", "MY", "ks", "NoRoot,NoExport")
69
// Adapt for Win7 as it only support a subset of above arguments
70
ProcessTools.executeCommand("certutil", "-v", "-p", "changeit",
71
"-user", "-importpfx", "ks", "NoRoot")
72
.shouldHaveExitValue(0);
73
test();
74
} finally {
75
ProcessTools.executeCommand("certutil", "-user", "-delstore", "MY",
76
id);
77
}
78
}
79
80
static void test() throws Exception {
81
82
char[] pass = "changeit".toCharArray();
83
84
KeyStore k1 = KeyStore.getInstance("Windows-MY");
85
k1.load(null, null);
86
87
KeyStore k2 = KeyStore.getInstance("PKCS12");
88
89
try (FileInputStream fis = new FileInputStream("ks")) {
90
k2.load(fis, pass);
91
}
92
93
PrivateKey p1 = (PrivateKey)k1.getKey(alias, null);
94
PublicKey u1 = k1.getCertificate(alias).getPublicKey();
95
96
PrivateKey p2 = (PrivateKey)k2.getKey(alias, pass);
97
PublicKey u2 = k2.getCertificate(alias).getPublicKey();
98
99
System.out.println(p1.toString());
100
System.out.println(u1.toString());
101
if (!p1.toString().contains("type=CNG")) {
102
throw new Exception("Not a CNG key");
103
}
104
105
testSignature(p1, u1);
106
testSignature(p1, u2);
107
testSignature(p2, u1);
108
testSignature(p2, u2);
109
}
110
111
static void testSignature(PrivateKey p, PublicKey u) throws Exception {
112
byte[] data = "hello".getBytes();
113
114
String[] ALGS = {
115
"NONEwithRSA", "SHA1withRSA",
116
"SHA256withRSA", "SHA512withRSA"
117
};
118
119
for (String alg : ALGS) {
120
if (alg.contains("NONE")) {
121
data = MessageDigest.getInstance("SHA-256").digest(data);
122
}
123
Signature s1 = Signature.getInstance(alg);
124
Signature s2 = Signature.getInstance(alg);
125
s1.initSign(p);
126
s2.initVerify(u);
127
s1.update(data);
128
s2.update(data);
129
if (!s2.verify(s1.sign())) {
130
throw new Exception("Error");
131
}
132
}
133
}
134
}
135
136