Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/security/Signature/SignatureGetInstance.java
38812 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 8216039
27
* @summary Ensure the BC provider-reselection workaround in Signature class
28
* functions correctly
29
* @run main/othervm SignatureGetInstance
30
*/
31
import java.security.*;
32
import java.security.interfaces.*;
33
import java.security.spec.*;
34
import sun.security.util.SignatureUtil;
35
36
public class SignatureGetInstance {
37
38
private static final String SIGALG = "RSASSA-PSS";
39
40
public static void main(String[] args) throws Exception {
41
Provider testProvider = new TestProvider();
42
// put test provider before SunRsaSign provider
43
Security.insertProviderAt(testProvider, 1);
44
//Security.addProvider(testProvider);
45
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
46
KeyPair kp = kpg.generateKeyPair();
47
48
MyPrivKey testPriv = new MyPrivKey();
49
MyPubKey testPub = new MyPubKey();
50
51
testDblInit(testPriv, testPub, true, "TestProvider");
52
testDblInit(kp.getPrivate(), kp.getPublic(), true, "SunRsaSign");
53
testDblInit(testPriv, kp.getPublic(), false, null);
54
testDblInit(kp.getPrivate(), testPub, false, null);
55
56
testSetAndInit(null, testPriv, true);
57
testSetAndInit(null, testPub, true);
58
testSetAndInit(null, kp.getPrivate(), true);
59
testSetAndInit(null, kp.getPublic(), true);
60
61
String provName = "SunRsaSign";
62
testSetAndInit(provName, testPriv, false);
63
testSetAndInit(provName, testPub, false);
64
testSetAndInit(provName, kp.getPrivate(), true);
65
testSetAndInit(provName, kp.getPublic(), true);
66
67
provName = "TestProvider";
68
testSetAndInit(provName, testPriv, true);
69
testSetAndInit(provName, testPub, true);
70
testSetAndInit(provName, kp.getPrivate(), false);
71
testSetAndInit(provName, kp.getPublic(), false);
72
73
System.out.println("Test Passed");
74
}
75
76
private static void checkName(Signature s, String name) {
77
if (name != null &&
78
!(name.equals(s.getProvider().getName()))) {
79
throw new RuntimeException("Fail: provider name mismatch");
80
}
81
}
82
83
private static void testDblInit(PrivateKey key1, PublicKey key2,
84
boolean shouldPass, String expectedProvName) throws Exception {
85
Signature sig = Signature.getInstance(SIGALG);
86
SignatureUtil.initSignWithParam(sig, key1, PSSParameterSpec.DEFAULT, null);
87
try {
88
sig.initVerify(key2);
89
if (!shouldPass) {
90
throw new RuntimeException("Fail: should throw InvalidKeyException");
91
}
92
checkName(sig, expectedProvName);
93
} catch (InvalidKeyException ike) {
94
if (shouldPass) {
95
System.out.println("Fail: Unexpected InvalidKeyException");
96
throw ike;
97
}
98
}
99
}
100
101
private static void testSetAndInit(String provName, Key key,
102
boolean shouldPass) throws Exception {
103
Signature sig;
104
if (provName == null) {
105
sig = Signature.getInstance(SIGALG);
106
} else {
107
sig = Signature.getInstance(SIGALG, provName);
108
}
109
AlgorithmParameterSpec params = PSSParameterSpec.DEFAULT;
110
boolean doSign = (key instanceof PrivateKey);
111
try {
112
if (doSign) {
113
SignatureUtil.initSignWithParam(sig, (PrivateKey)key, params, null);
114
} else {
115
SignatureUtil.initVerifyWithParam(sig, (PublicKey)key, params);
116
}
117
if (!shouldPass) {
118
throw new RuntimeException("Fail: should throw InvalidKeyException");
119
}
120
checkName(sig, provName);
121
// check that the earlier parameter is still there
122
if (sig.getParameters() == null) {
123
throw new RuntimeException("Fail: parameters not preserved");
124
}
125
} catch (InvalidKeyException ike) {
126
if (shouldPass) {
127
System.out.println("Fail: Unexpected InvalidKeyException");
128
throw ike;
129
}
130
}
131
}
132
133
// Test provider which only accepts its own Key objects
134
// Registered to be more preferred than SunRsaSign provider
135
// for testing deferred provider selection
136
public static class TestProvider extends Provider {
137
TestProvider() {
138
super("TestProvider", 1.0d, "provider for SignatureGetInstance");
139
put("Signature.RSASSA-PSS",
140
"SignatureGetInstance$MySigImpl");
141
}
142
}
143
144
public static class MyPrivKey implements PrivateKey {
145
public String getAlgorithm() { return "RSASSA-PSS"; }
146
public String getFormat() { return "MyOwn"; }
147
public byte[] getEncoded() { return null; }
148
}
149
150
public static class MyPubKey implements PublicKey {
151
public String getAlgorithm() { return "RSASSA-PSS"; }
152
public String getFormat() { return "MyOwn"; }
153
public byte[] getEncoded() { return null; }
154
}
155
156
public static class MySigImpl extends SignatureSpi {
157
// simulate BC behavior of only using params set before init calls
158
AlgorithmParameterSpec initParamSpec = null;
159
AlgorithmParameterSpec paramSpec = null;
160
161
public MySigImpl() {
162
super();
163
}
164
165
@Override
166
protected void engineInitVerify(PublicKey publicKey)
167
throws InvalidKeyException {
168
if (!(publicKey instanceof MyPubKey)) {
169
throw new InvalidKeyException("Must be MyPubKey");
170
}
171
initParamSpec = paramSpec;
172
}
173
174
@Override
175
protected void engineInitSign(PrivateKey privateKey)
176
throws InvalidKeyException {
177
if (!(privateKey instanceof MyPrivKey)) {
178
throw new InvalidKeyException("Must be MyPrivKey");
179
}
180
initParamSpec = paramSpec;
181
}
182
183
@Override
184
protected void engineUpdate(byte b) throws SignatureException {
185
}
186
187
@Override
188
protected void engineUpdate(byte[] b, int off, int len)
189
throws SignatureException {
190
}
191
192
@Override
193
protected byte[] engineSign()
194
throws SignatureException {
195
return new byte[0];
196
}
197
198
@Override
199
protected boolean engineVerify(byte[] sigBytes)
200
throws SignatureException {
201
return false;
202
}
203
204
@Override
205
@Deprecated
206
protected void engineSetParameter(String param, Object value)
207
throws InvalidParameterException {
208
}
209
210
@Override
211
protected void engineSetParameter(AlgorithmParameterSpec params)
212
throws InvalidAlgorithmParameterException {
213
paramSpec = params;
214
}
215
216
@Override
217
@Deprecated
218
protected AlgorithmParameters engineGetParameter(String param)
219
throws InvalidParameterException {
220
return null;
221
}
222
223
@Override
224
protected AlgorithmParameters engineGetParameters() {
225
if (initParamSpec != null) {
226
try {
227
AlgorithmParameters ap =
228
AlgorithmParameters.getInstance("RSASSA-PSS");
229
ap.init(initParamSpec);
230
return ap;
231
} catch (Exception e) {
232
throw new RuntimeException(e);
233
}
234
}
235
return null;
236
}
237
}
238
}
239
240