Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/pkcs11/Provider/Login.java
51712 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
/* @test
25
* @bug 4850423
26
* @summary login facilities for hardware tokens
27
* @library /test/lib ..
28
* @run testng/othervm -Djava.security.manager=allow Login
29
*/
30
31
import org.testng.annotations.BeforeClass;
32
import org.testng.annotations.Test;
33
34
import java.io.*;
35
import java.nio.file.Path;
36
import java.security.*;
37
import javax.security.auth.callback.*;
38
39
import javax.security.auth.Subject;
40
import javax.security.auth.login.FailedLoginException;
41
42
public class Login extends PKCS11Test {
43
44
private static final String KS_TYPE = "PKCS11";
45
private static char[] password;
46
47
@BeforeClass
48
public void setUp() throws Exception {
49
copyNssCertKeyToClassesDir();
50
setCommonSystemProps();
51
System.setProperty("CUSTOM_P11_CONFIG",
52
Path.of(BASE).resolve("Login-nss.txt").toString());
53
}
54
55
@Test
56
public void testLogin() throws Exception {
57
String[] args = new String[]{ "sm", "Login.policy"};
58
main(new Login(), args);
59
}
60
61
public void main(Provider p) throws Exception {
62
63
int testnum = 1;
64
65
KeyStore ks = KeyStore.getInstance(KS_TYPE, p);
66
67
// check instance
68
if (ks.getProvider() instanceof AuthProvider ap) {
69
System.out.println("keystore provider instance of AuthProvider");
70
System.out.println("test " + testnum++ + " passed");
71
} else {
72
throw new SecurityException("did not get AuthProvider KeyStore");
73
}
74
75
try {
76
77
// test app-provided callback
78
System.out.println("*** enter [foo] as the password ***");
79
password = new char[] { 'f', 'o', 'o' };
80
81
ap.login(new Subject(), new PasswordCallbackHandler());
82
ap.logout();
83
throw new SecurityException("test failed, expected LoginException");
84
} catch (FailedLoginException fle) {
85
System.out.println("test " + testnum++ + " passed");
86
}
87
88
try {
89
90
// test default callback
91
System.out.println("*** enter [foo] as the password ***");
92
password = new char[] { 'f', 'o', 'o' };
93
94
Security.setProperty("auth.login.defaultCallbackHandler",
95
"Login$PasswordCallbackHandler");
96
ap.login(new Subject(), null);
97
ap.logout();
98
throw new SecurityException("test failed, expected LoginException");
99
} catch (FailedLoginException fle) {
100
System.out.println("test " + testnum++ + " passed");
101
}
102
103
// test provider-set callback
104
System.out.println("*** enter test12 (correct) password ***");
105
password = new char[] { 't', 'e', 's', 't', '1', '2' };
106
107
Security.setProperty("auth.login.defaultCallbackHandler", "");
108
ap.setCallbackHandler(new PasswordCallbackHandler());
109
ap.login(new Subject(), null);
110
System.out.println("test " + testnum++ + " passed");
111
112
// test user already logged in
113
ap.setCallbackHandler(null);
114
ap.login(new Subject(), null);
115
System.out.println("test " + testnum++ + " passed");
116
117
// logout
118
ap.logout();
119
120
// call KeyStore.load with a NULL password, and get prompted for PIN
121
ap.setCallbackHandler(new PasswordCallbackHandler());
122
ks.load(null, (char[])null);
123
System.out.println("test " + testnum++ + " passed");
124
}
125
126
public static class PasswordCallbackHandler implements CallbackHandler {
127
public void handle(Callback[] callbacks)
128
throws IOException, UnsupportedCallbackException {
129
if (!(callbacks[0] instanceof PasswordCallback pc)) {
130
throw new UnsupportedCallbackException(callbacks[0]);
131
}
132
pc.setPassword(Login.password);
133
}
134
}
135
}
136
137