Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/jdk/sun/security/pkcs11/Provider/MultipleLogins.java
66646 views
1
/*
2
* Copyright (c) 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.*;
35
import java.util.Iterator;
36
import java.util.PropertyPermission;
37
import java.util.ServiceConfigurationError;
38
import java.util.ServiceLoader;
39
40
import jdk.test.lib.util.ForceGC;
41
42
public class MultipleLogins {
43
private static final String KS_TYPE = "PKCS11";
44
private static final int NUM_PROVIDERS = 20;
45
private static final SunPKCS11[] providers = new SunPKCS11[NUM_PROVIDERS];
46
static final Policy DEFAULT_POLICY = Policy.getPolicy();
47
48
public static void main(String[] args) throws Exception {
49
String nssConfig = PKCS11Test.getNssConfig();
50
if (nssConfig == null) {
51
// No test framework support yet. Ignore
52
System.out.println("No NSS config found. Skipping.");
53
return;
54
}
55
56
for (int i =0; i < NUM_PROVIDERS; i++) {
57
// loop to set up test without security manger
58
providers[i] = (SunPKCS11)PKCS11Test.newPKCS11Provider();
59
}
60
61
if (args.length > 0) {
62
Policy.setPolicy(new SimplePolicy());
63
System.setSecurityManager(new SecurityManager());
64
}
65
66
for (int i =0; i < NUM_PROVIDERS; i++) {
67
providers[i] = (SunPKCS11)providers[i].configure(nssConfig);
68
Security.addProvider(providers[i]);
69
test(providers[i]);
70
}
71
72
WeakReference<SunPKCS11>[] weakRef = new WeakReference[NUM_PROVIDERS];
73
for (int i =0; i < NUM_PROVIDERS; i++) {
74
weakRef[i] = new WeakReference<>(providers[i]);
75
providers[i].logout();
76
77
if (i == 0) {
78
// one provider stays for use with clean up thread
79
continue;
80
}
81
82
try {
83
providers[i].login(new Subject(), new PasswordCallbackHandler());
84
throw new RuntimeException("Expected LoginException");
85
} catch (LoginException le) {
86
// expected
87
}
88
89
Security.removeProvider(providers[i].getName());
90
providers[i] = null;
91
92
ForceGC gc = new ForceGC();
93
int finalI = i;
94
gc.await(() -> weakRef[finalI].get() == null);
95
if (!weakRef[i].refersTo(null)) {
96
throw new RuntimeException("Expected SunPKCS11 Provider to be GC'ed..");
97
}
98
}
99
}
100
101
private static void test(SunPKCS11 p) throws Exception {
102
KeyStore ks = KeyStore.getInstance(KS_TYPE, p);
103
p.setCallbackHandler(new PasswordCallbackHandler());
104
try {
105
ks.load(null, (char[]) null);
106
} catch (IOException e) {
107
if (!e.getMessage().contains("load failed")) {
108
// we expect the keystore load to fail
109
throw new RuntimeException("unexpected exception", e);
110
}
111
}
112
113
p.logout();
114
115
try {
116
ks.load(null, (char[]) null);
117
} catch (IOException e) {
118
if (e.getCause() instanceof LoginException &&
119
e.getCause().getMessage().contains("No token present")) {
120
// expected
121
} else {
122
throw new RuntimeException("Token was present", e);
123
}
124
}
125
}
126
127
static final class SimplePolicy extends Policy {
128
129
final Permissions perms = new Permissions();
130
SimplePolicy() {
131
perms.add(new PropertyPermission("*", "read, write"));
132
perms.add(new SecurityPermission("authProvider.*"));
133
perms.add(new SecurityPermission("insertProvider.*"));
134
perms.add(new SecurityPermission("removeProvider.*"));
135
}
136
137
@Override
138
public boolean implies(ProtectionDomain domain, Permission permission) {
139
return perms.implies(permission) ||
140
DEFAULT_POLICY.implies(domain, permission);
141
}
142
}
143
144
public static class PasswordCallbackHandler implements CallbackHandler {
145
public void handle(Callback[] callbacks)
146
throws IOException, UnsupportedCallbackException {
147
if (!(callbacks[0] instanceof PasswordCallback)) {
148
throw new UnsupportedCallbackException(callbacks[0]);
149
}
150
PasswordCallback pc = (PasswordCallback)callbacks[0];
151
pc.setPassword(null);
152
}
153
}
154
}
155
156