Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/pkcs11/Provider/MultipleLogins.java
51710 views
1
/*
2
* Copyright (c) 2003, 2021, 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
import sun.security.pkcs11.SunPKCS11;
25
26
import javax.security.auth.Subject;
27
import javax.security.auth.callback.Callback;
28
import javax.security.auth.callback.CallbackHandler;
29
import javax.security.auth.callback.PasswordCallback;
30
import javax.security.auth.callback.UnsupportedCallbackException;
31
import javax.security.auth.login.LoginException;
32
import java.io.IOException;
33
import java.lang.ref.WeakReference;
34
import java.security.KeyStore;
35
import java.security.Provider;
36
import java.security.Security;
37
import java.util.Iterator;
38
import java.util.ServiceConfigurationError;
39
import java.util.ServiceLoader;
40
41
import jdk.test.lib.util.ForceGC;
42
43
public class MultipleLogins {
44
private static final String KS_TYPE = "PKCS11";
45
private static final int NUM_PROVIDERS = 20;
46
private static final SunPKCS11[] providers = new SunPKCS11[NUM_PROVIDERS];
47
48
49
public static void main(String[] args) throws Exception {
50
for (int i =0; i < NUM_PROVIDERS; i++) {
51
String nssConfig = PKCS11Test.getNssConfig();
52
if (nssConfig == null) {
53
// No test framework support yet. Ignore
54
System.out.println("No NSS config found. Skipping.");
55
return;
56
}
57
providers[i] =
58
(SunPKCS11)PKCS11Test.newPKCS11Provider()
59
.configure(nssConfig);
60
Security.addProvider(providers[i]);
61
test(providers[i]);
62
}
63
64
WeakReference<SunPKCS11>[] weakRef = new WeakReference[NUM_PROVIDERS];
65
for (int i =0; i < NUM_PROVIDERS; i++) {
66
weakRef[i] = new WeakReference<>(providers[i]);
67
providers[i].logout();
68
69
if (i == 0) {
70
// one provider stays for use with clean up thread
71
continue;
72
}
73
74
try {
75
providers[i].login(new Subject(), new PasswordCallbackHandler());
76
throw new RuntimeException("Expected LoginException");
77
} catch (LoginException le) {
78
// expected
79
}
80
81
Security.removeProvider(providers[i].getName());
82
providers[i] = null;
83
84
ForceGC gc = new ForceGC();
85
int finalI = i;
86
gc.await(() -> weakRef[finalI].get() == null);
87
if (!weakRef[i].refersTo(null)) {
88
throw new RuntimeException("Expected SunPKCS11 Provider to be GC'ed..");
89
}
90
}
91
}
92
93
private static void test(SunPKCS11 p) throws Exception {
94
KeyStore ks = KeyStore.getInstance(KS_TYPE, p);
95
96
p.setCallbackHandler(new PasswordCallbackHandler());
97
try {
98
ks.load(null, (char[]) null);
99
} catch (IOException e) {
100
if (!e.getMessage().contains("load failed")) {
101
// we expect the keystore load to fail
102
throw new RuntimeException("unexpected exception", e);
103
}
104
}
105
106
p.logout();
107
108
try {
109
ks.load(null, (char[]) null);
110
} catch (IOException e) {
111
if (e.getCause() instanceof LoginException &&
112
e.getCause().getMessage().contains("No token present")) {
113
// expected
114
} else {
115
throw new RuntimeException("Token was present", e);
116
}
117
}
118
}
119
120
public static class PasswordCallbackHandler implements CallbackHandler {
121
public void handle(Callback[] callbacks)
122
throws IOException, UnsupportedCallbackException {
123
if (!(callbacks[0] instanceof PasswordCallback)) {
124
throw new UnsupportedCallbackException(callbacks[0]);
125
}
126
PasswordCallback pc = (PasswordCallback)callbacks[0];
127
pc.setPassword(null);
128
}
129
}
130
}
131
132