Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.security.jgss/share/classes/sun/security/jgss/krb5/Krb5InitCredential.java
41161 views
1
/*
2
* Copyright (c) 2000, 2021, 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 javax.security.auth.kerberos.KerberosTicket;
33
import javax.security.auth.kerberos.KerberosPrincipal;
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
@SuppressWarnings("serial") // Not statically typed as Serializable
57
private Krb5NameElement name;
58
@SuppressWarnings("serial") // Not statically typed as Serializable
59
private Credentials krb5Credentials;
60
public KerberosTicket proxyTicket;
61
62
private Krb5InitCredential(Krb5NameElement name,
63
byte[] asn1Encoding,
64
KerberosPrincipal client,
65
KerberosPrincipal clientAlias,
66
KerberosPrincipal server,
67
KerberosPrincipal serverAlias,
68
byte[] sessionKey,
69
int keyType,
70
boolean[] flags,
71
Date authTime,
72
Date startTime,
73
Date endTime,
74
Date renewTill,
75
InetAddress[] clientAddresses)
76
throws GSSException {
77
super(asn1Encoding,
78
client,
79
server,
80
sessionKey,
81
keyType,
82
flags,
83
authTime,
84
startTime,
85
endTime,
86
renewTill,
87
clientAddresses);
88
KerberosSecrets.getJavaxSecurityAuthKerberosAccess()
89
.kerberosTicketSetClientAlias(this, clientAlias);
90
KerberosSecrets.getJavaxSecurityAuthKerberosAccess()
91
.kerberosTicketSetServerAlias(this, serverAlias);
92
this.name = name;
93
94
try {
95
// Cache this for later use by the sun.security.krb5 package.
96
krb5Credentials = new Credentials(asn1Encoding,
97
client.getName(),
98
(clientAlias != null ?
99
clientAlias.getName() : null),
100
server.getName(),
101
(serverAlias != null ?
102
serverAlias.getName() : null),
103
sessionKey,
104
keyType,
105
flags,
106
authTime,
107
startTime,
108
endTime,
109
renewTill,
110
clientAddresses);
111
} catch (KrbException e) {
112
throw new GSSException(GSSException.NO_CRED, -1,
113
e.getMessage());
114
} catch (IOException e) {
115
throw new GSSException(GSSException.NO_CRED, -1,
116
e.getMessage());
117
}
118
119
}
120
121
private Krb5InitCredential(Krb5NameElement name,
122
Credentials delegatedCred,
123
byte[] asn1Encoding,
124
KerberosPrincipal client,
125
KerberosPrincipal clientAlias,
126
KerberosPrincipal server,
127
KerberosPrincipal serverAlias,
128
byte[] sessionKey,
129
int keyType,
130
boolean[] flags,
131
Date authTime,
132
Date startTime,
133
Date endTime,
134
Date renewTill,
135
InetAddress[] clientAddresses)
136
throws GSSException {
137
super(asn1Encoding,
138
client,
139
server,
140
sessionKey,
141
keyType,
142
flags,
143
authTime,
144
startTime,
145
endTime,
146
renewTill,
147
clientAddresses);
148
KerberosSecrets.getJavaxSecurityAuthKerberosAccess()
149
.kerberosTicketSetClientAlias(this, clientAlias);
150
KerberosSecrets.getJavaxSecurityAuthKerberosAccess()
151
.kerberosTicketSetServerAlias(this, serverAlias);
152
this.name = name;
153
// A delegated cred does not have all fields set. So do not try to
154
// creat new Credentials out of the delegatedCred.
155
this.krb5Credentials = delegatedCred;
156
}
157
158
static Krb5InitCredential getInstance(GSSCaller caller, Krb5NameElement name,
159
int initLifetime)
160
throws GSSException {
161
162
KerberosTicket tgt = getTgt(caller, name, initLifetime);
163
if (tgt == null)
164
throw new GSSException(GSSException.NO_CRED, -1,
165
"Failed to find any Kerberos tgt");
166
167
if (name == null) {
168
String fullName = tgt.getClient().getName();
169
name = Krb5NameElement.getInstance(fullName,
170
Krb5MechFactory.NT_GSS_KRB5_PRINCIPAL);
171
}
172
173
KerberosPrincipal clientAlias = KerberosSecrets
174
.getJavaxSecurityAuthKerberosAccess()
175
.kerberosTicketGetClientAlias(tgt);
176
KerberosPrincipal serverAlias = KerberosSecrets
177
.getJavaxSecurityAuthKerberosAccess()
178
.kerberosTicketGetServerAlias(tgt);
179
Krb5InitCredential result = new Krb5InitCredential(name,
180
tgt.getEncoded(),
181
tgt.getClient(),
182
clientAlias,
183
tgt.getServer(),
184
serverAlias,
185
tgt.getSessionKey().getEncoded(),
186
tgt.getSessionKeyType(),
187
tgt.getFlags(),
188
tgt.getAuthTime(),
189
tgt.getStartTime(),
190
tgt.getEndTime(),
191
tgt.getRenewTill(),
192
tgt.getClientAddresses());
193
result.proxyTicket = KerberosSecrets.getJavaxSecurityAuthKerberosAccess().
194
kerberosTicketGetProxy(tgt);
195
return result;
196
}
197
198
static Krb5InitCredential getInstance(Krb5NameElement name,
199
Credentials delegatedCred)
200
throws GSSException {
201
202
EncryptionKey sessionKey = delegatedCred.getSessionKey();
203
204
/*
205
* all of the following data is optional in a KRB-CRED
206
* messages. This check for each field.
207
*/
208
209
PrincipalName cPrinc = delegatedCred.getClient();
210
PrincipalName cAPrinc = delegatedCred.getClientAlias();
211
PrincipalName sPrinc = delegatedCred.getServer();
212
PrincipalName sAPrinc = delegatedCred.getServerAlias();
213
214
KerberosPrincipal client = null;
215
KerberosPrincipal clientAlias = null;
216
KerberosPrincipal server = null;
217
KerberosPrincipal serverAlias = null;
218
219
Krb5NameElement credName = null;
220
221
if (cPrinc != null) {
222
String fullName = cPrinc.getName();
223
credName = Krb5NameElement.getInstance(fullName,
224
Krb5MechFactory.NT_GSS_KRB5_PRINCIPAL);
225
client = new KerberosPrincipal(fullName);
226
}
227
228
if (cAPrinc != null) {
229
clientAlias = new KerberosPrincipal(cAPrinc.getName());
230
}
231
232
// XXX Compare name to credName
233
234
if (sPrinc != null) {
235
server =
236
new KerberosPrincipal(sPrinc.getName(),
237
KerberosPrincipal.KRB_NT_SRV_INST);
238
}
239
240
if (sAPrinc != null) {
241
serverAlias = new KerberosPrincipal(sAPrinc.getName());
242
}
243
244
return new Krb5InitCredential(credName,
245
delegatedCred,
246
delegatedCred.getEncoded(),
247
client,
248
clientAlias,
249
server,
250
serverAlias,
251
sessionKey.getBytes(),
252
sessionKey.getEType(),
253
delegatedCred.getFlags(),
254
delegatedCred.getAuthTime(),
255
delegatedCred.getStartTime(),
256
delegatedCred.getEndTime(),
257
delegatedCred.getRenewTill(),
258
delegatedCred.getClientAddresses());
259
}
260
261
/**
262
* Returns the principal name for this credential. The name
263
* is in mechanism specific format.
264
*
265
* @return GSSNameSpi representing principal name of this credential
266
* @exception GSSException may be thrown
267
*/
268
public final GSSNameSpi getName() throws GSSException {
269
return name;
270
}
271
272
/**
273
* Returns the init lifetime remaining.
274
*
275
* @return the init lifetime remaining in seconds
276
* @exception GSSException may be thrown
277
*/
278
public int getInitLifetime() throws GSSException {
279
Date d = getEndTime();
280
if (d == null) {
281
return 0;
282
}
283
long retVal = d.getTime() - System.currentTimeMillis();
284
return (int)(retVal/1000);
285
}
286
287
/**
288
* Returns the accept lifetime remaining.
289
*
290
* @return the accept lifetime remaining in seconds
291
* @exception GSSException may be thrown
292
*/
293
public int getAcceptLifetime() throws GSSException {
294
return 0;
295
}
296
297
public boolean isInitiatorCredential() throws GSSException {
298
return true;
299
}
300
301
public boolean isAcceptorCredential() throws GSSException {
302
return false;
303
}
304
305
/**
306
* Returns the oid representing the underlying credential
307
* mechanism oid.
308
*
309
* @return the Oid for this credential mechanism
310
* @exception GSSException may be thrown
311
*/
312
public final Oid getMechanism() {
313
return Krb5MechFactory.GSS_KRB5_MECH_OID;
314
}
315
316
public final java.security.Provider getProvider() {
317
return Krb5MechFactory.PROVIDER;
318
}
319
320
321
/**
322
* Returns a sun.security.krb5.Credentials instance so that it maybe
323
* used in that package for th Kerberos protocol.
324
*/
325
Credentials getKrb5Credentials() {
326
return krb5Credentials;
327
}
328
329
/*
330
* XXX Call to this.refresh() should refresh the locally cached copy
331
* of krb5Credentials also.
332
*/
333
334
/**
335
* Called to invalidate this credential element.
336
*/
337
public void dispose() throws GSSException {
338
try {
339
destroy();
340
} catch (javax.security.auth.DestroyFailedException e) {
341
GSSException gssException =
342
new GSSException(GSSException.FAILURE, -1,
343
"Could not destroy credentials - " + e.getMessage());
344
gssException.initCause(e);
345
}
346
}
347
348
// XXX call to this.destroy() should destroy the locally cached copy
349
// of krb5Credentials and then call super.destroy().
350
351
@SuppressWarnings("removal")
352
private static KerberosTicket getTgt(GSSCaller caller, Krb5NameElement name,
353
int initLifetime)
354
throws GSSException {
355
356
final String clientPrincipal;
357
358
/*
359
* Find the TGT for the realm that the client is in. If the client
360
* name is not available, then use the default realm.
361
*/
362
if (name != null) {
363
clientPrincipal = (name.getKrb5PrincipalName()).getName();
364
} else {
365
clientPrincipal = null;
366
}
367
368
final AccessControlContext acc = AccessController.getContext();
369
370
try {
371
final GSSCaller realCaller = (caller == GSSCaller.CALLER_UNKNOWN)
372
? GSSCaller.CALLER_INITIATE
373
: caller;
374
return AccessController.doPrivileged(
375
new PrivilegedExceptionAction<KerberosTicket>() {
376
public KerberosTicket run() throws Exception {
377
// It's OK to use null as serverPrincipal. TGT is almost
378
// the first ticket for a principal and we use list.
379
return Krb5Util.getInitialTicket(
380
realCaller,
381
clientPrincipal, acc);
382
}});
383
} catch (PrivilegedActionException e) {
384
GSSException ge =
385
new GSSException(GSSException.NO_CRED, -1,
386
"Attempt to obtain new INITIATE credentials failed!" +
387
" (" + e.getMessage() + ")");
388
ge.initCause(e.getException());
389
throw ge;
390
}
391
}
392
393
@Override
394
public GSSCredentialSpi impersonate(GSSNameSpi name) throws GSSException {
395
try {
396
Krb5NameElement kname = (Krb5NameElement)name;
397
Credentials newCred = Credentials.acquireS4U2selfCreds(
398
kname.getKrb5PrincipalName(), krb5Credentials);
399
return new Krb5ProxyCredential(this, kname, newCred.getTicket());
400
} catch (IOException | KrbException ke) {
401
GSSException ge =
402
new GSSException(GSSException.FAILURE, -1,
403
"Attempt to obtain S4U2self credentials failed!");
404
ge.initCause(ke);
405
throw ge;
406
}
407
}
408
}
409
410