Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/sun/security/sasl/gsskerb/AuthOnly.java
41154 views
1
/*
2
* Copyright (c) 2003, 2014, 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 4634892
27
* @summary Ensure authentication via GSS-API/Kerberos v5 works.
28
* @run main/manual AuthOnly
29
*/
30
31
/*
32
* Set logging to FINEST to view exchange.
33
* See runwjaas.csh for instructions for how to run this test.
34
*/
35
36
import javax.security.sasl.*;
37
import javax.security.auth.callback.*;
38
import java.security.*;
39
import javax.security.auth.Subject;
40
import javax.security.auth.login.*;
41
import com.sun.security.auth.callback.*;
42
import java.util.HashMap;
43
44
public class AuthOnly {
45
private static final String MECH = "GSSAPI";
46
private static final String SERVER_FQDN = "machineX.imc.org";
47
private static final String PROTOCOL = "sample";
48
49
private static String namesfile, proxyfile;
50
private static final byte[] EMPTY = new byte[0];
51
private static boolean auto;
52
private static boolean verbose = false;
53
54
public static void main(String[] args) throws Exception {
55
if (args.length == 0) {
56
namesfile = null;
57
auto = true;
58
} else {
59
int i = 0;
60
if (args[i].equals("-m")) {
61
i++;
62
auto = false;
63
}
64
if (args.length > i) {
65
namesfile = args[i++];
66
if (args.length > i) {
67
proxyfile = args[i];
68
}
69
} else {
70
namesfile = null;
71
}
72
}
73
74
CallbackHandler clntCbh = null;
75
final CallbackHandler srvCbh = new PropertiesFileCallbackHandler(
76
null, namesfile, proxyfile);
77
78
Subject clntSubj = doLogin("client");
79
Subject srvSubj = doLogin("server");
80
final HashMap clntprops = new HashMap();
81
final HashMap srvprops = new HashMap();
82
83
clntprops.put(Sasl.QOP, "auth");
84
srvprops.put(Sasl.QOP, "auth,auth-int,auth-conf");
85
86
final SaslClient clnt = (SaslClient)
87
Subject.doAs(clntSubj, new PrivilegedExceptionAction() {
88
public Object run() throws Exception {
89
return Sasl.createSaslClient(
90
new String[]{MECH}, null, PROTOCOL, SERVER_FQDN,
91
clntprops, null);
92
}
93
});
94
95
if (verbose) {
96
System.out.println(clntSubj);
97
System.out.println(srvSubj);
98
}
99
final SaslServer srv = (SaslServer)
100
Subject.doAs(srvSubj, new PrivilegedExceptionAction() {
101
public Object run() throws Exception {
102
return Sasl.createSaslServer(MECH, PROTOCOL, SERVER_FQDN,
103
srvprops, srvCbh);
104
}
105
});
106
107
108
if (clnt == null) {
109
throw new IllegalStateException(
110
"Unable to find client impl for " + MECH);
111
}
112
if (srv == null) {
113
throw new IllegalStateException(
114
"Unable to find server impl for " + MECH);
115
}
116
117
byte[] response;
118
byte[] challenge;
119
120
response = (byte[]) Subject.doAs(clntSubj,
121
new PrivilegedExceptionAction() {
122
public Object run() throws Exception {
123
return (clnt.hasInitialResponse()? clnt.evaluateChallenge(EMPTY) : EMPTY);
124
}});
125
126
while (!clnt.isComplete() || !srv.isComplete()) {
127
final byte[] responseCopy = response;
128
challenge = (byte[]) Subject.doAs(srvSubj,
129
new PrivilegedExceptionAction() {
130
public Object run() throws Exception {
131
return srv.evaluateResponse(responseCopy);
132
}});
133
134
if (challenge != null) {
135
final byte[] challengeCopy = challenge;
136
response = (byte[]) Subject.doAs(clntSubj,
137
new PrivilegedExceptionAction() {
138
public Object run() throws Exception {
139
return clnt.evaluateChallenge(challengeCopy);
140
}});
141
}
142
}
143
144
if (clnt.isComplete() && srv.isComplete()) {
145
if (verbose) {
146
System.out.println("SUCCESS");
147
System.out.println("authzid is " + srv.getAuthorizationID());
148
}
149
} else {
150
throw new IllegalStateException("FAILURE: mismatched state:" +
151
" client complete? " + clnt.isComplete() +
152
" server complete? " + srv.isComplete());
153
}
154
}
155
156
private static Subject doLogin(String msg) throws LoginException {
157
LoginContext lc = null;
158
if (verbose) {
159
System.out.println(msg);
160
}
161
try {
162
lc = new LoginContext(msg, new TextCallbackHandler());
163
164
// Attempt authentication
165
// You might want to do this in a "for" loop to give
166
// user more than one chance to enter correct username/password
167
lc.login();
168
169
} catch (LoginException le) {
170
throw le;
171
}
172
return lc.getSubject();
173
}
174
}
175
176