Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/jgss/krb5/Krb5InitCredential.java
38922 views
1
/*
2
* Copyright (c) 2000, 2019, 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. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package sun.security.jgss.krb5;
27
28
import org.ietf.jgss.*;
29
import sun.security.jgss.GSSCaller;
30
import sun.security.jgss.spi.*;
31
import sun.security.krb5.*;
32
import sun.security.krb5.Config;
33
import javax.security.auth.kerberos.*;
34
import java.net.InetAddress;
35
import java.io.IOException;
36
import java.util.Date;
37
import java.security.AccessController;
38
import java.security.AccessControlContext;
39
import java.security.PrivilegedExceptionAction;
40
import java.security.PrivilegedActionException;
41
42
/**
43
* Implements the krb5 initiator credential element.
44
*
45
* @author Mayank Upadhyay
46
* @author Ram Marti
47
* @since 1.4
48
*/
49
50
public class Krb5InitCredential
51
extends KerberosTicket
52
implements Krb5CredElement {
53
54
private static final long serialVersionUID = 7723415700837898232L;
55
56
private Krb5NameElement name;
57
private Credentials krb5Credentials;
58
public KerberosTicket proxyTicket;
59
60
private Krb5InitCredential(Krb5NameElement name,
61
byte[] asn1Encoding,
62
KerberosPrincipal client,
63
KerberosPrincipal clientAlias,
64
KerberosPrincipal server,
65
KerberosPrincipal serverAlias,
66
byte[] sessionKey,
67
int keyType,
68
boolean[] flags,
69
Date authTime,
70
Date startTime,
71
Date endTime,
72
Date renewTill,
73
InetAddress[] clientAddresses)
74
throws GSSException {
75
super(asn1Encoding,
76
client,
77
server,
78
sessionKey,
79
keyType,
80
flags,
81
authTime,
82
startTime,
83
endTime,
84
renewTill,
85
clientAddresses);
86
KerberosSecrets.getJavaxSecurityAuthKerberosAccess()
87
.kerberosTicketSetClientAlias(this, clientAlias);
88
KerberosSecrets.getJavaxSecurityAuthKerberosAccess()
89
.kerberosTicketSetServerAlias(this, serverAlias);
90
this.name = name;
91
92
try {
93
// Cache this for later use by the sun.security.krb5 package.
94
krb5Credentials = new Credentials(asn1Encoding,
95
client.getName(),
96
(clientAlias != null ?
97
clientAlias.getName() : null),
98
server.getName(),
99
(serverAlias != null ?
100
serverAlias.getName() : null),
101
sessionKey,
102
keyType,
103
flags,
104
authTime,
105
startTime,
106
endTime,
107
renewTill,
108
clientAddresses);
109
} catch (KrbException e) {
110
throw new GSSException(GSSException.NO_CRED, -1,
111
e.getMessage());
112
} catch (IOException e) {
113
throw new GSSException(GSSException.NO_CRED, -1,
114
e.getMessage());
115
}
116
117
}
118
119
private Krb5InitCredential(Krb5NameElement name,
120
Credentials delegatedCred,
121
byte[] asn1Encoding,
122
KerberosPrincipal client,
123
KerberosPrincipal clientAlias,
124
KerberosPrincipal server,
125
KerberosPrincipal serverAlias,
126
byte[] sessionKey,
127
int keyType,
128
boolean[] flags,
129
Date authTime,
130
Date startTime,
131
Date endTime,
132
Date renewTill,
133
InetAddress[] clientAddresses)
134
throws GSSException {
135
super(asn1Encoding,
136
client,
137
server,
138
sessionKey,
139
keyType,
140
flags,
141
authTime,
142
startTime,
143
endTime,
144
renewTill,
145
clientAddresses);
146
KerberosSecrets.getJavaxSecurityAuthKerberosAccess()
147
.kerberosTicketSetClientAlias(this, clientAlias);
148
KerberosSecrets.getJavaxSecurityAuthKerberosAccess()
149
.kerberosTicketSetServerAlias(this, serverAlias);
150
this.name = name;
151
// A delegated cred does not have all fields set. So do not try to
152
// creat new Credentials out of the delegatedCred.
153
this.krb5Credentials = delegatedCred;
154
}
155
156
static Krb5InitCredential getInstance(GSSCaller caller, Krb5NameElement name,
157
int initLifetime)
158
throws GSSException {
159
160
KerberosTicket tgt = getTgt(caller, name, initLifetime);
161
if (tgt == null)
162
throw new GSSException(GSSException.NO_CRED, -1,
163
"Failed to find any Kerberos tgt");
164
165
if (name == null) {
166
String fullName = tgt.getClient().getName();
167
name = Krb5NameElement.getInstance(fullName,
168
Krb5MechFactory.NT_GSS_KRB5_PRINCIPAL);
169
}
170
171
KerberosPrincipal clientAlias = KerberosSecrets
172
.getJavaxSecurityAuthKerberosAccess()
173
.kerberosTicketGetClientAlias(tgt);
174
KerberosPrincipal serverAlias = KerberosSecrets
175
.getJavaxSecurityAuthKerberosAccess()
176
.kerberosTicketGetServerAlias(tgt);
177
Krb5InitCredential result = new Krb5InitCredential(name,
178
tgt.getEncoded(),
179
tgt.getClient(),
180
clientAlias,
181
tgt.getServer(),
182
serverAlias,
183
tgt.getSessionKey().getEncoded(),
184
tgt.getSessionKeyType(),
185
tgt.getFlags(),
186
tgt.getAuthTime(),
187
tgt.getStartTime(),
188
tgt.getEndTime(),
189
tgt.getRenewTill(),
190
tgt.getClientAddresses());
191
result.proxyTicket = KerberosSecrets.getJavaxSecurityAuthKerberosAccess().
192
kerberosTicketGetProxy(tgt);
193
return result;
194
}
195
196
static Krb5InitCredential getInstance(Krb5NameElement name,
197
Credentials delegatedCred)
198
throws GSSException {
199
200
EncryptionKey sessionKey = delegatedCred.getSessionKey();
201
202
/*
203
* all of the following data is optional in a KRB-CRED
204
* messages. This check for each field.
205
*/
206
207
PrincipalName cPrinc = delegatedCred.getClient();
208
PrincipalName cAPrinc = delegatedCred.getClientAlias();
209
PrincipalName sPrinc = delegatedCred.getServer();
210
PrincipalName sAPrinc = delegatedCred.getServerAlias();
211
212
KerberosPrincipal client = null;
213
KerberosPrincipal clientAlias = null;
214
KerberosPrincipal server = null;
215
KerberosPrincipal serverAlias = null;
216
217
Krb5NameElement credName = null;
218
219
if (cPrinc != null) {
220
String fullName = cPrinc.getName();
221
credName = Krb5NameElement.getInstance(fullName,
222
Krb5MechFactory.NT_GSS_KRB5_PRINCIPAL);
223
client = new KerberosPrincipal(fullName);
224
}
225
226
if (cAPrinc != null) {
227
clientAlias = new KerberosPrincipal(cAPrinc.getName());
228
}
229
230
// XXX Compare name to credName
231
232
if (sPrinc != null) {
233
server =
234
new KerberosPrincipal(sPrinc.getName(),
235
KerberosPrincipal.KRB_NT_SRV_INST);
236
}
237
238
if (sAPrinc != null) {
239
serverAlias = new KerberosPrincipal(sAPrinc.getName());
240
}
241
242
return new Krb5InitCredential(credName,
243
delegatedCred,
244
delegatedCred.getEncoded(),
245
client,
246
clientAlias,
247
server,
248
serverAlias,
249
sessionKey.getBytes(),
250
sessionKey.getEType(),
251
delegatedCred.getFlags(),
252
delegatedCred.getAuthTime(),
253
delegatedCred.getStartTime(),
254
delegatedCred.getEndTime(),
255
delegatedCred.getRenewTill(),
256
delegatedCred.getClientAddresses());
257
}
258
259
/**
260
* Returns the principal name for this credential. The name
261
* is in mechanism specific format.
262
*
263
* @return GSSNameSpi representing principal name of this credential
264
* @exception GSSException may be thrown
265
*/
266
public final GSSNameSpi getName() throws GSSException {
267
return name;
268
}
269
270
/**
271
* Returns the init lifetime remaining.
272
*
273
* @return the init lifetime remaining in seconds
274
* @exception GSSException may be thrown
275
*/
276
public int getInitLifetime() throws GSSException {
277
Date d = getEndTime();
278
if (d == null) {
279
return 0;
280
}
281
282
long retVal = d.getTime() - System.currentTimeMillis();
283
return (int)(retVal/1000);
284
}
285
286
/**
287
* Returns the accept lifetime remaining.
288
*
289
* @return the accept lifetime remaining in seconds
290
* @exception GSSException may be thrown
291
*/
292
public int getAcceptLifetime() throws GSSException {
293
return 0;
294
}
295
296
public boolean isInitiatorCredential() throws GSSException {
297
return true;
298
}
299
300
public boolean isAcceptorCredential() throws GSSException {
301
return false;
302
}
303
304
/**
305
* Returns the oid representing the underlying credential
306
* mechanism oid.
307
*
308
* @return the Oid for this credential mechanism
309
* @exception GSSException may be thrown
310
*/
311
public final Oid getMechanism() {
312
return Krb5MechFactory.GSS_KRB5_MECH_OID;
313
}
314
315
public final java.security.Provider getProvider() {
316
return Krb5MechFactory.PROVIDER;
317
}
318
319
320
/**
321
* Returns a sun.security.krb5.Credentials instance so that it maybe
322
* used in that package for th Kerberos protocol.
323
*/
324
Credentials getKrb5Credentials() {
325
return krb5Credentials;
326
}
327
328
/*
329
* XXX Call to this.refresh() should refresh the locally cached copy
330
* of krb5Credentials also.
331
*/
332
333
/**
334
* Called to invalidate this credential element.
335
*/
336
public void dispose() throws GSSException {
337
try {
338
destroy();
339
} catch (javax.security.auth.DestroyFailedException e) {
340
GSSException gssException =
341
new GSSException(GSSException.FAILURE, -1,
342
"Could not destroy credentials - " + e.getMessage());
343
gssException.initCause(e);
344
}
345
}
346
347
// XXX call to this.destroy() should destroy the locally cached copy
348
// of krb5Credentials and then call super.destroy().
349
350
private static KerberosTicket getTgt(GSSCaller caller, Krb5NameElement name,
351
int initLifetime)
352
throws GSSException {
353
354
final String clientPrincipal;
355
356
/*
357
* Find the TGT for the realm that the client is in. If the client
358
* name is not available, then use the default realm.
359
*/
360
if (name != null) {
361
clientPrincipal = (name.getKrb5PrincipalName()).getName();
362
} else {
363
clientPrincipal = null;
364
}
365
366
final AccessControlContext acc = AccessController.getContext();
367
368
try {
369
final GSSCaller realCaller = (caller == GSSCaller.CALLER_UNKNOWN)
370
? GSSCaller.CALLER_INITIATE
371
: caller;
372
return AccessController.doPrivileged(
373
new PrivilegedExceptionAction<KerberosTicket>() {
374
public KerberosTicket run() throws Exception {
375
// It's OK to use null as serverPrincipal. TGT is almost
376
// the first ticket for a principal and we use list.
377
return Krb5Util.getInitialTicket(
378
realCaller,
379
clientPrincipal, acc);
380
}});
381
} catch (PrivilegedActionException e) {
382
GSSException ge =
383
new GSSException(GSSException.NO_CRED, -1,
384
"Attempt to obtain new INITIATE credentials failed!" +
385
" (" + e.getMessage() + ")");
386
ge.initCause(e.getException());
387
throw ge;
388
}
389
}
390
391
@Override
392
public GSSCredentialSpi impersonate(GSSNameSpi name) throws GSSException {
393
try {
394
Krb5NameElement kname = (Krb5NameElement)name;
395
Credentials newCred = Credentials.acquireS4U2selfCreds(
396
kname.getKrb5PrincipalName(), krb5Credentials);
397
return new Krb5ProxyCredential(this, kname, newCred.getTicket());
398
} catch (IOException | KrbException ke) {
399
GSSException ge =
400
new GSSException(GSSException.FAILURE, -1,
401
"Attempt to obtain S4U2self credentials failed!");
402
ge.initCause(ke);
403
throw ge;
404
}
405
}
406
}
407
408