Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/mscapi/SignUsingSHA2withRSA.java
41152 views
1
/*
2
* Copyright (c) 2011, 2018, 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 6753664 8180570
27
* @summary Support SHA256 (and higher) in SunMSCAPI
28
* @requires os.family == "windows"
29
* @modules java.base/sun.security.tools.keytool
30
* java.base/sun.security.x509
31
*/
32
33
import sun.security.tools.keytool.CertAndKeyGen;
34
import sun.security.x509.X500Name;
35
36
import java.security.*;
37
import java.security.cert.Certificate;
38
import java.util.*;
39
40
public class SignUsingSHA2withRSA {
41
42
private static final byte[] toBeSigned = new byte[] {
43
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10
44
};
45
46
private static List<byte[]> generatedSignatures = new ArrayList<>();
47
48
public static void main(String[] args) throws Exception {
49
KeyStore ks = KeyStore.getInstance("Windows-MY");
50
ks.load(null, null);
51
if (ks.containsAlias("6753664")) {
52
ks.deleteEntry("6753664");
53
}
54
55
CertAndKeyGen gen = new CertAndKeyGen("RSA", "SHA256withRSA");
56
gen.generate(2048);
57
58
ks.setKeyEntry("6753664", gen.getPrivateKey(), null,
59
new Certificate[] {
60
gen.getSelfCertificate(new X500Name("cn=localhost,c=US"), 100)
61
});
62
63
try {
64
run();
65
} finally {
66
ks.deleteEntry("6753664");
67
ks.store(null, null);
68
}
69
}
70
71
static void run() throws Exception {
72
73
Provider[] providers = Security.getProviders("Signature.SHA256withRSA");
74
if (providers == null) {
75
System.out.println("No JCE providers support the " +
76
"'Signature.SHA256withRSA' algorithm");
77
System.out.println("Skipping this test...");
78
return;
79
80
} else {
81
System.out.println("The following JCE providers support the " +
82
"'Signature.SHA256withRSA' algorithm: ");
83
for (Provider provider : providers) {
84
System.out.println(" " + provider.getName());
85
}
86
}
87
System.out.println("-------------------------------------------------");
88
89
KeyStore ks = KeyStore.getInstance("Windows-MY", "SunMSCAPI");
90
ks.load(null, null);
91
System.out.println("Loaded keystore: Windows-MY");
92
93
Enumeration<String> e = ks.aliases();
94
PrivateKey privateKey = null;
95
PublicKey publicKey = null;
96
97
while (e.hasMoreElements()) {
98
String alias = e.nextElement();
99
if (alias.equals("6753664")) {
100
System.out.println("Loaded entry: " + alias);
101
privateKey = (PrivateKey) ks.getKey(alias, null);
102
publicKey = (PublicKey) ks.getCertificate(alias).getPublicKey();
103
}
104
}
105
if (privateKey == null || publicKey == null) {
106
throw new Exception("Cannot load the keys need to run this test");
107
}
108
System.out.println("-------------------------------------------------");
109
110
generatedSignatures.add(signUsing("SHA256withRSA", privateKey));
111
generatedSignatures.add(signUsing("SHA384withRSA", privateKey));
112
generatedSignatures.add(signUsing("SHA512withRSA", privateKey));
113
114
System.out.println("-------------------------------------------------");
115
116
verifyUsing("SHA256withRSA", publicKey, generatedSignatures.get(0));
117
verifyUsing("SHA384withRSA", publicKey, generatedSignatures.get(1));
118
verifyUsing("SHA512withRSA", publicKey, generatedSignatures.get(2));
119
120
System.out.println("-------------------------------------------------");
121
}
122
123
private static byte[] signUsing(String signAlgorithm,
124
PrivateKey privateKey) throws Exception {
125
126
// Must explicitly specify the SunMSCAPI JCE provider
127
// (otherwise SunJCE is chosen because it appears earlier in the list)
128
Signature sig1 = Signature.getInstance(signAlgorithm, "SunMSCAPI");
129
if (sig1 == null) {
130
throw new Exception("'" + signAlgorithm + "' is not supported");
131
}
132
System.out.println("Using " + signAlgorithm + " signer from the " +
133
sig1.getProvider().getName() + " JCE provider");
134
135
System.out.println("Using key: " + privateKey);
136
sig1.initSign(privateKey);
137
sig1.update(toBeSigned);
138
byte [] sigBytes = null;
139
140
try {
141
sigBytes = sig1.sign();
142
System.out.println("Generated RSA signature over a " +
143
toBeSigned.length + "-byte data (signature length: " +
144
sigBytes.length * 8 + " bits)");
145
System.out.println(String.format("0x%0" +
146
(sigBytes.length * 2) + "x",
147
new java.math.BigInteger(1, sigBytes)));
148
149
} catch (SignatureException se) {
150
System.out.println("Error generating RSA signature: " + se);
151
}
152
153
return sigBytes;
154
}
155
156
private static void verifyUsing(String signAlgorithm, PublicKey publicKey,
157
byte[] signature) throws Exception {
158
159
// Must explicitly specify the SunMSCAPI JCE provider
160
// (otherwise SunJCE is chosen because it appears earlier in the list)
161
Signature sig1 = Signature.getInstance(signAlgorithm, "SunMSCAPI");
162
if (sig1 == null) {
163
throw new Exception("'" + signAlgorithm + "' is not supported");
164
}
165
System.out.println("Using " + signAlgorithm + " verifier from the "
166
+ sig1.getProvider().getName() + " JCE provider");
167
168
System.out.println("Using key: " + publicKey);
169
170
System.out.println("\nVerifying RSA Signature over a " +
171
toBeSigned.length + "-byte data (signature length: " +
172
signature.length * 8 + " bits)");
173
System.out.println(String.format("0x%0" + (signature.length * 2) +
174
"x", new java.math.BigInteger(1, signature)));
175
176
sig1.initVerify(publicKey);
177
sig1.update(toBeSigned);
178
179
if (sig1.verify(signature)) {
180
System.out.println("Verify PASSED\n");
181
} else {
182
throw new Exception("Verify FAILED");
183
}
184
}
185
}
186
187