Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/sun/security/sasl/gsskerb/NoSecurityLayer.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 4873552
27
* @summary GSS-API/krb5 SASL mechanism should throw IllegalStateException
28
* for auth-only
29
* @run main/manual NoSecurityLayer
30
*/
31
32
/*
33
* Set logging to FINEST to view exchange.
34
* See run-nosec-wjaas.csh for instructions for how to run this test.
35
*/
36
37
import javax.security.sasl.*;
38
import javax.security.auth.callback.*;
39
import java.security.*;
40
import javax.security.auth.Subject;
41
import javax.security.auth.login.*;
42
import com.sun.security.auth.callback.*;
43
import java.util.HashMap;
44
45
public class NoSecurityLayer {
46
private static final String MECH = "GSSAPI";
47
private static final String SERVER_FQDN = "anti.imc.org";
48
private static final String PROTOCOL = "sample";
49
50
private static String namesfile, proxyfile;
51
private static final byte[] EMPTY = new byte[0];
52
private static boolean auto;
53
private static boolean verbose = false;
54
55
public static void main(String[] args) throws Exception {
56
if (args.length == 0) {
57
namesfile = null;
58
auto = true;
59
} else {
60
int i = 0;
61
if (args[i].equals("-m")) {
62
i++;
63
auto = false;
64
}
65
if (args.length > i) {
66
namesfile = args[i++];
67
if (args.length > i) {
68
proxyfile = args[i];
69
}
70
} else {
71
namesfile = null;
72
}
73
}
74
75
CallbackHandler clntCbh = null;
76
final CallbackHandler srvCbh = new PropertiesFileCallbackHandler(
77
null, namesfile, proxyfile);
78
79
Subject clntSubj = doLogin("client");
80
Subject srvSubj = doLogin("server");
81
final HashMap clntprops = new HashMap();
82
final HashMap srvprops = new HashMap();
83
84
clntprops.put(Sasl.QOP, "auth");
85
srvprops.put(Sasl.QOP, "auth,auth-int,auth-conf");
86
87
final SaslClient clnt = (SaslClient)
88
Subject.doAs(clntSubj, new PrivilegedExceptionAction() {
89
public Object run() throws Exception {
90
return Sasl.createSaslClient(
91
new String[]{MECH}, null, PROTOCOL, SERVER_FQDN,
92
clntprops, null);
93
}
94
});
95
96
if (verbose) {
97
System.out.println(clntSubj);
98
System.out.println(srvSubj);
99
}
100
final SaslServer srv = (SaslServer)
101
Subject.doAs(srvSubj, new PrivilegedExceptionAction() {
102
public Object run() throws Exception {
103
return Sasl.createSaslServer(MECH, PROTOCOL, SERVER_FQDN,
104
srvprops, srvCbh);
105
}
106
});
107
108
109
if (clnt == null) {
110
throw new IllegalStateException(
111
"Unable to find client impl for " + MECH);
112
}
113
if (srv == null) {
114
throw new IllegalStateException(
115
"Unable to find server impl for " + MECH);
116
}
117
118
byte[] response;
119
byte[] challenge;
120
121
response = (byte[]) Subject.doAs(clntSubj,
122
new PrivilegedExceptionAction() {
123
public Object run() throws Exception {
124
return (clnt.hasInitialResponse()? clnt.evaluateChallenge(EMPTY) : EMPTY);
125
}});
126
127
while (!clnt.isComplete() || !srv.isComplete()) {
128
final byte[] responseCopy = response;
129
challenge = (byte[]) Subject.doAs(srvSubj,
130
new PrivilegedExceptionAction() {
131
public Object run() throws Exception {
132
return srv.evaluateResponse(responseCopy);
133
}});
134
135
if (challenge != null) {
136
final byte[] challengeCopy = challenge;
137
response = (byte[]) Subject.doAs(clntSubj,
138
new PrivilegedExceptionAction() {
139
public Object run() throws Exception {
140
return clnt.evaluateChallenge(challengeCopy);
141
}});
142
}
143
}
144
145
if (clnt.isComplete() && srv.isComplete()) {
146
if (verbose) {
147
System.out.println("SUCCESS");
148
System.out.println("authzid is " + srv.getAuthorizationID());
149
}
150
} else {
151
throw new IllegalStateException("FAILURE: mismatched state:" +
152
" client complete? " + clnt.isComplete() +
153
" server complete? " + srv.isComplete());
154
}
155
156
if (verbose) {
157
System.out.println(clnt.getNegotiatedProperty(Sasl.QOP));
158
}
159
160
// Now try to use security layer
161
162
byte[] clntBuf = new byte[]{0, 1, 2, 3};
163
try {
164
byte[] wrapped = clnt.wrap(clntBuf, 0, clntBuf.length);
165
throw new Exception(
166
"clnt wrap should not be allowed w/no security layer");
167
} catch (IllegalStateException e) {
168
// expected
169
}
170
171
byte[] srvBuf = new byte[]{10, 11, 12, 13};
172
try {
173
byte[] wrapped = srv.wrap(srvBuf, 0, srvBuf.length);
174
throw new Exception(
175
"srv wrap should not be allowed w/no security layer");
176
} catch (IllegalStateException e) {
177
// expected
178
}
179
180
try {
181
byte[] unwrapped = clnt.unwrap(clntBuf, 0, clntBuf.length);
182
throw new Exception(
183
"clnt wrap should not be allowed w/no security layer");
184
} catch (IllegalStateException e) {
185
// expected
186
}
187
188
try {
189
byte[] unwrapped = srv.unwrap(srvBuf, 0, srvBuf.length);
190
throw new Exception(
191
"srv wrap should not be allowed w/no security layer");
192
} catch (IllegalStateException e) {
193
// expected
194
}
195
}
196
197
private static Subject doLogin(String msg) throws LoginException {
198
LoginContext lc = null;
199
if (verbose) {
200
System.out.println(msg);
201
}
202
try {
203
lc = new LoginContext(msg, new TextCallbackHandler());
204
205
// Attempt authentication
206
// You might want to do this in a "for" loop to give
207
// user more than one chance to enter correct username/password
208
lc.login();
209
210
} catch (LoginException le) {
211
throw le;
212
}
213
return lc.getSubject();
214
}
215
}
216
217