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