Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/mscapi/InteropWithSunRsaSign.java
41149 views
1
/*
2
* Copyright (c) 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
/**
26
* @test
27
* @bug 8205445
28
* @summary Interop test between SunMSCAPI and SunRsaSign on RSASSA-PSS
29
* @requires os.family == "windows"
30
*/
31
32
import java.security.KeyPair;
33
import java.security.KeyPairGenerator;
34
import java.security.PrivateKey;
35
import java.security.PublicKey;
36
import java.security.SecureRandom;
37
import java.security.Signature;
38
import java.security.spec.MGF1ParameterSpec;
39
import java.security.spec.PSSParameterSpec;
40
import java.util.Random;
41
42
public class InteropWithSunRsaSign {
43
44
private static final SecureRandom NOT_SECURE_RANDOM = new SecureRandom() {
45
Random r = new Random();
46
@Override
47
public void nextBytes(byte[] bytes) {
48
r.nextBytes(bytes);
49
}
50
};
51
52
private static boolean allResult = true;
53
private static byte[] msg = "hello".getBytes();
54
55
public static void main(String[] args) throws Exception {
56
57
matrix(new PSSParameterSpec(
58
"SHA-1",
59
"MGF1",
60
MGF1ParameterSpec.SHA1,
61
20,
62
PSSParameterSpec.TRAILER_FIELD_BC));
63
64
matrix(new PSSParameterSpec(
65
"SHA-256",
66
"MGF1",
67
MGF1ParameterSpec.SHA256,
68
32,
69
PSSParameterSpec.TRAILER_FIELD_BC));
70
71
matrix(new PSSParameterSpec(
72
"SHA-384",
73
"MGF1",
74
MGF1ParameterSpec.SHA384,
75
48,
76
PSSParameterSpec.TRAILER_FIELD_BC));
77
78
matrix(new PSSParameterSpec(
79
"SHA-512",
80
"MGF1",
81
MGF1ParameterSpec.SHA512,
82
64,
83
PSSParameterSpec.TRAILER_FIELD_BC));
84
85
// non-typical salt length
86
matrix(new PSSParameterSpec(
87
"SHA-1",
88
"MGF1",
89
MGF1ParameterSpec.SHA1,
90
17,
91
PSSParameterSpec.TRAILER_FIELD_BC));
92
93
if (!allResult) {
94
throw new Exception("Failed");
95
}
96
}
97
98
static void matrix(PSSParameterSpec pss) throws Exception {
99
100
System.out.printf("\n%10s%20s%20s%20s %s\n", pss.getDigestAlgorithm(),
101
"KeyPairGenerator", "signer", "verifier", "result");
102
System.out.printf("%10s%20s%20s%20s %s\n",
103
"-------", "----------------", "------", "--------", "------");
104
105
// KeyPairGenerator chooses SPI when getInstance() is called.
106
String[] provsForKPG = {"SunRsaSign", "SunMSCAPI"};
107
108
// "-" means no preferred provider. In this case, SPI is chosen
109
// when initSign/initVerify is called. Worth testing.
110
String[] provsForSignature = {"SunRsaSign", "SunMSCAPI", "-"};
111
112
int pos = 0;
113
for (String pg : provsForKPG) {
114
for (String ps : provsForSignature) {
115
for (String pv : provsForSignature) {
116
System.out.printf("%10d%20s%20s%20s ", ++pos, pg, ps, pv);
117
try {
118
boolean result = test(pg, ps, pv, pss);
119
System.out.println(result);
120
if (!result) {
121
allResult = false;
122
}
123
} catch (Exception e) {
124
if (pg.equals("-") || pg.equals(ps)) {
125
// When Signature provider is automatically
126
// chosen or the same with KeyPairGenerator,
127
// this is an error.
128
allResult = false;
129
System.out.println("X " + e.getMessage());
130
} else {
131
// Known restriction: SunRsaSign and SunMSCAPI can't
132
// use each other's private key for signing.
133
System.out.println(e.getMessage());
134
}
135
}
136
}
137
}
138
}
139
}
140
141
static boolean test(String pg, String ps, String pv, PSSParameterSpec pss)
142
throws Exception {
143
144
KeyPairGenerator kpg = pg.length() == 1
145
? KeyPairGenerator.getInstance("RSA")
146
:KeyPairGenerator.getInstance("RSA", pg);
147
kpg.initialize(
148
pss.getDigestAlgorithm().equals("SHA-512") ? 2048: 1024,
149
NOT_SECURE_RANDOM);
150
KeyPair kp = kpg.generateKeyPair();
151
PrivateKey pr = kp.getPrivate();
152
PublicKey pu = kp.getPublic();
153
154
Signature s = ps.length() == 1
155
? Signature.getInstance("RSASSA-PSS")
156
: Signature.getInstance("RSASSA-PSS", ps);
157
s.initSign(pr);
158
s.setParameter(pss);
159
s.update(msg);
160
byte[] sig = s.sign();
161
162
Signature s2 = pv.length() == 1
163
? Signature.getInstance("RSASSA-PSS")
164
: Signature.getInstance("RSASSA-PSS", pv);
165
s2.initVerify(pu);
166
s2.setParameter(pss);
167
s2.update(msg);
168
169
return s2.verify(sig);
170
}
171
}
172
173