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/ConfSecurityLayer.java
38867 views
1
/*
2
* Copyright (c) 2004, 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 5014493
27
* @summary SaslServer.wrap throws NullPointerException when security
28
* layer negotiated
29
* @ignore see run-conf-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 ConfSecurityLayer {
43
private static final String MECH = "GSSAPI";
44
private static final String SERVER_FQDN = "machineX.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-conf");
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
byte[] wrappedClnt = clnt.wrap(clntBuf, 0, clntBuf.length);
161
System.out.println("plaintext2: " + bytesToString(clntBuf));
162
System.out.println("wrapped2: " + bytesToString(wrappedClnt));
163
164
byte[] srvBuf = new byte[]{10, 11, 12, 13};
165
byte[] wrappedSrv = srv.wrap(srvBuf, 0, srvBuf.length);
166
System.out.println("plaintext1: " + bytesToString(srvBuf));
167
System.out.println("wrapped1: " + bytesToString(wrappedSrv));
168
169
byte[] unwrapped1 = clnt.unwrap(wrappedSrv, 0, wrappedSrv.length);
170
System.out.println("unwrapped1: " + bytesToString(unwrapped1));
171
172
byte[] unwrapped2 = srv.unwrap(wrappedClnt, 0, wrappedClnt.length);
173
System.out.println("unwrapped2: " + bytesToString(unwrapped2));
174
}
175
176
private static Subject doLogin(String msg) throws LoginException {
177
LoginContext lc = null;
178
if (verbose) {
179
System.out.println(msg);
180
}
181
try {
182
lc = new LoginContext(msg, new TextCallbackHandler());
183
184
// Attempt authentication
185
// You might want to do this in a "for" loop to give
186
// user more than one chance to enter correct username/password
187
lc.login();
188
189
} catch (LoginException le) {
190
throw le;
191
}
192
return lc.getSubject();
193
}
194
195
private static String bytesToString(byte[] digest) {
196
// Get character representation of digest
197
StringBuffer digestString = new StringBuffer();
198
199
for (int i = 0; i < digest.length; i++) {
200
if ((digest[i] & 0x000000ff) < 0x10) {
201
digestString.append("0" +
202
Integer.toHexString(digest[i] & 0x000000ff));
203
} else {
204
digestString.append(
205
Integer.toHexString(digest[i] & 0x000000ff));
206
}
207
}
208
return digestString.toString();
209
}
210
}
211
212