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/krb5/Credentials.java
41159 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
/*
27
*
28
* (C) Copyright IBM Corp. 1999 All Rights Reserved.
29
* Copyright 1997 The Open Group Research Institute. All rights reserved.
30
*/
31
32
package sun.security.krb5;
33
34
import sun.security.action.GetPropertyAction;
35
import sun.security.krb5.internal.*;
36
import sun.security.krb5.internal.ccache.CredentialsCache;
37
import sun.security.krb5.internal.crypto.EType;
38
import java.io.IOException;
39
import java.util.Date;
40
import java.util.Locale;
41
import java.net.InetAddress;
42
43
/**
44
* This class encapsulates the concept of a Kerberos service
45
* credential. That includes a Kerberos ticket and an associated
46
* session key.
47
*/
48
public class Credentials {
49
50
Ticket ticket;
51
PrincipalName client;
52
PrincipalName clientAlias;
53
PrincipalName server;
54
PrincipalName serverAlias;
55
EncryptionKey key;
56
TicketFlags flags;
57
KerberosTime authTime;
58
KerberosTime startTime;
59
KerberosTime endTime;
60
KerberosTime renewTill;
61
HostAddresses cAddr;
62
AuthorizationData authzData;
63
private static boolean DEBUG = Krb5.DEBUG;
64
private static CredentialsCache cache;
65
static boolean alreadyLoaded = false;
66
private static boolean alreadyTried = false;
67
68
private Credentials proxy = null;
69
70
public Credentials getProxy() {
71
return proxy;
72
}
73
74
public Credentials setProxy(Credentials proxy) {
75
this.proxy = proxy;
76
return this;
77
}
78
79
// Read native ticket with session key type in the given list
80
private static native Credentials acquireDefaultNativeCreds(int[] eTypes);
81
82
public Credentials(Ticket new_ticket,
83
PrincipalName new_client,
84
PrincipalName new_client_alias,
85
PrincipalName new_server,
86
PrincipalName new_server_alias,
87
EncryptionKey new_key,
88
TicketFlags new_flags,
89
KerberosTime authTime,
90
KerberosTime new_startTime,
91
KerberosTime new_endTime,
92
KerberosTime renewTill,
93
HostAddresses cAddr,
94
AuthorizationData authzData) {
95
this(new_ticket, new_client, new_client_alias, new_server,
96
new_server_alias, new_key, new_flags, authTime,
97
new_startTime, new_endTime, renewTill, cAddr);
98
this.authzData = authzData;
99
}
100
101
// Warning: called by NativeCreds.c and nativeccache.c
102
public Credentials(Ticket new_ticket,
103
PrincipalName new_client,
104
PrincipalName new_client_alias,
105
PrincipalName new_server,
106
PrincipalName new_server_alias,
107
EncryptionKey new_key,
108
TicketFlags new_flags,
109
KerberosTime authTime,
110
KerberosTime new_startTime,
111
KerberosTime new_endTime,
112
KerberosTime renewTill,
113
HostAddresses cAddr) {
114
ticket = new_ticket;
115
client = new_client;
116
clientAlias = new_client_alias;
117
server = new_server;
118
serverAlias = new_server_alias;
119
key = new_key;
120
flags = new_flags;
121
this.authTime = authTime;
122
startTime = new_startTime;
123
endTime = new_endTime;
124
this.renewTill = renewTill;
125
this.cAddr = cAddr;
126
}
127
128
public Credentials(byte[] encoding,
129
String client,
130
String clientAlias,
131
String server,
132
String serverAlias,
133
byte[] keyBytes,
134
int keyType,
135
boolean[] flags,
136
Date authTime,
137
Date startTime,
138
Date endTime,
139
Date renewTill,
140
InetAddress[] cAddrs) throws KrbException, IOException {
141
this(new Ticket(encoding),
142
new PrincipalName(client, PrincipalName.KRB_NT_PRINCIPAL),
143
(clientAlias == null? null : new PrincipalName(clientAlias,
144
PrincipalName.KRB_NT_PRINCIPAL)),
145
new PrincipalName(server, PrincipalName.KRB_NT_SRV_INST),
146
(serverAlias == null? null : new PrincipalName(serverAlias,
147
PrincipalName.KRB_NT_SRV_INST)),
148
new EncryptionKey(keyType, keyBytes),
149
(flags == null? null: new TicketFlags(flags)),
150
(authTime == null? null: new KerberosTime(authTime)),
151
(startTime == null? null: new KerberosTime(startTime)),
152
(endTime == null? null: new KerberosTime(endTime)),
153
(renewTill == null? null: new KerberosTime(renewTill)),
154
null); // caddrs are in the encoding at this point
155
}
156
157
/**
158
* Acquires a service ticket for the specified service
159
* principal. If the service ticket is not already available, it
160
* obtains a new one from the KDC.
161
*/
162
/*
163
public Credentials(Credentials tgt, PrincipalName service)
164
throws KrbException {
165
}
166
*/
167
168
public final PrincipalName getClient() {
169
return client;
170
}
171
172
public final PrincipalName getClientAlias() {
173
return clientAlias;
174
}
175
176
public final PrincipalName getServer() {
177
return server;
178
}
179
180
public final PrincipalName getServerAlias() {
181
return serverAlias;
182
}
183
184
public final EncryptionKey getSessionKey() {
185
return key;
186
}
187
188
public final Date getAuthTime() {
189
if (authTime != null) {
190
return authTime.toDate();
191
} else {
192
return null;
193
}
194
}
195
196
public final Date getStartTime() {
197
if (startTime != null)
198
{
199
return startTime.toDate();
200
}
201
return null;
202
}
203
204
public final Date getEndTime() {
205
if (endTime != null)
206
{
207
return endTime.toDate();
208
}
209
return null;
210
}
211
212
public final Date getRenewTill() {
213
if (renewTill != null)
214
{
215
return renewTill.toDate();
216
}
217
return null;
218
}
219
220
public final boolean[] getFlags() {
221
if (flags == null) // Can be in a KRB-CRED
222
return null;
223
return flags.toBooleanArray();
224
}
225
226
public final InetAddress[] getClientAddresses() {
227
228
if (cAddr == null)
229
return null;
230
231
return cAddr.getInetAddresses();
232
}
233
234
public final byte[] getEncoded() {
235
byte[] retVal = null;
236
try {
237
retVal = ticket.asn1Encode();
238
} catch (Asn1Exception e) {
239
if (DEBUG) {
240
System.out.println(e);
241
}
242
} catch (IOException ioe) {
243
if (DEBUG) {
244
System.out.println(ioe);
245
}
246
}
247
return retVal;
248
}
249
250
public boolean isForwardable() {
251
return flags.get(Krb5.TKT_OPTS_FORWARDABLE);
252
}
253
254
public boolean isRenewable() {
255
return flags.get(Krb5.TKT_OPTS_RENEWABLE);
256
}
257
258
public Ticket getTicket() {
259
return ticket;
260
}
261
262
public TicketFlags getTicketFlags() {
263
return flags;
264
}
265
266
public AuthorizationData getAuthzData() {
267
return authzData;
268
}
269
/**
270
* Checks if the service ticket returned by the KDC has the OK-AS-DELEGATE
271
* flag set
272
* @return true if OK-AS_DELEGATE flag is set, otherwise, return false.
273
*/
274
public boolean checkDelegate() {
275
return flags.get(Krb5.TKT_OPTS_DELEGATE);
276
}
277
278
/**
279
* Reset TKT_OPTS_DELEGATE to false, called at credentials acquirement
280
* when one of the cross-realm TGTs does not have the OK-AS-DELEGATE
281
* flag set. This info must be preservable and restorable through
282
* the Krb5Util.credsToTicket/ticketToCreds() methods so that even if
283
* the service ticket is cached it still remembers the cross-realm
284
* authentication result.
285
*/
286
public void resetDelegate() {
287
flags.set(Krb5.TKT_OPTS_DELEGATE, false);
288
}
289
290
public Credentials renew() throws KrbException, IOException {
291
KDCOptions options = new KDCOptions();
292
options.set(KDCOptions.RENEW, true);
293
/*
294
* Added here to pass KrbKdcRep.check:73
295
*/
296
options.set(KDCOptions.RENEWABLE, true);
297
298
return new KrbTgsReq(options,
299
this,
300
server,
301
serverAlias,
302
null, // from
303
null, // till
304
null, // rtime
305
null, // eTypes
306
cAddr,
307
null,
308
null,
309
null).sendAndGetCreds();
310
}
311
312
/**
313
* Returns a TGT for the given client principal from a ticket cache.
314
*
315
* @param princ the client principal. A value of null means that the
316
* default principal name in the credentials cache will be used.
317
* @param ticketCache the path to the tickets file. A value
318
* of null will be accepted to indicate that the default
319
* path should be searched
320
* @return the TGT credentials or null if none were found. If the tgt
321
* expired, it is the responsibility of the caller to determine this.
322
*/
323
public static Credentials acquireTGTFromCache(PrincipalName princ,
324
String ticketCache)
325
throws KrbException, IOException {
326
327
if (ticketCache == null) {
328
// The default ticket cache on Windows and Mac is not a file.
329
String os = GetPropertyAction.privilegedGetProperty("os.name");
330
if (os.toUpperCase(Locale.ENGLISH).startsWith("WINDOWS") ||
331
os.toUpperCase(Locale.ENGLISH).contains("OS X")) {
332
Credentials creds = acquireDefaultCreds();
333
if (creds == null) {
334
if (DEBUG) {
335
System.out.println(">>> Found no TGT's in native ccache");
336
}
337
return null;
338
}
339
if (princ != null) {
340
if (creds.getClient().equals(princ)) {
341
if (DEBUG) {
342
System.out.println(">>> Obtained TGT from native ccache: "
343
+ creds);
344
}
345
return creds;
346
} else {
347
if (DEBUG) {
348
System.out.println(">>> native ccache contains TGT for "
349
+ creds.getClient()
350
+ " not "
351
+ princ);
352
}
353
return null;
354
}
355
} else {
356
if (DEBUG) {
357
System.out.println(">>> Obtained TGT from native ccache: "
358
+ creds);
359
}
360
return creds;
361
}
362
}
363
}
364
365
/*
366
* Returns the appropriate cache. If ticketCache is null, it is the
367
* default cache otherwise it is the cache filename contained in it.
368
*/
369
CredentialsCache ccache =
370
CredentialsCache.getInstance(princ, ticketCache);
371
372
if (ccache == null) {
373
return null;
374
}
375
376
Credentials tgtCred = ccache.getInitialCreds();
377
378
if (tgtCred == null) {
379
return null;
380
}
381
382
if (EType.isSupported(tgtCred.key.getEType())) {
383
return tgtCred;
384
} else {
385
if (DEBUG) {
386
System.out.println(
387
">>> unsupported key type found the default TGT: " +
388
tgtCred.key.getEType());
389
}
390
return null;
391
}
392
}
393
394
/**
395
* Acquires default credentials.
396
* <br>The possible locations for default credentials cache is searched in
397
* the following order:
398
* <ol>
399
* <li> The directory and cache file name specified by "KRB5CCNAME" system.
400
* property.
401
* <li> The directory and cache file name specified by "KRB5CCNAME"
402
* environment variable.
403
* <li> A cache file named krb5cc_{user.name} at {user.home} directory.
404
* </ol>
405
* @return a <code>KrbCreds</code> object if the credential is found,
406
* otherwise return null.
407
*/
408
409
// this method is intentionally changed to not check if the caller's
410
// principal name matches cache file's principal name.
411
// It assumes that the GSS call has
412
// the privilege to access the default cache file.
413
414
// This method is only called on Windows and Mac OS X, the native
415
// acquireDefaultNativeCreds is also available on these platforms.
416
public static synchronized Credentials acquireDefaultCreds() {
417
Credentials result = null;
418
419
if (cache == null) {
420
cache = CredentialsCache.getInstance();
421
}
422
if (cache != null) {
423
Credentials temp = cache.getInitialCreds();
424
if (temp != null) {
425
if (DEBUG) {
426
System.out.println(">>> KrbCreds found the default ticket"
427
+ " granting ticket in credential cache.");
428
}
429
if (EType.isSupported(temp.key.getEType())) {
430
result = temp;
431
} else {
432
if (DEBUG) {
433
System.out.println(
434
">>> unsupported key type found the default TGT: " +
435
temp.key.getEType());
436
}
437
}
438
}
439
}
440
if (result == null) {
441
// Doesn't seem to be a default cache on this system or
442
// TGT has unsupported encryption type
443
444
if (!alreadyTried) {
445
// See if there's any native code to load
446
try {
447
ensureLoaded();
448
} catch (Exception e) {
449
if (DEBUG) {
450
System.out.println("Can not load native ccache library");
451
e.printStackTrace();
452
}
453
alreadyTried = true;
454
}
455
}
456
if (alreadyLoaded) {
457
// There is some native code
458
if (DEBUG) {
459
System.out.println(">> Acquire default native Credentials");
460
}
461
try {
462
result = acquireDefaultNativeCreds(
463
EType.getDefaults("default_tkt_enctypes"));
464
} catch (KrbException ke) {
465
// when there is no default_tkt_enctypes.
466
}
467
}
468
}
469
return result;
470
}
471
472
/**
473
* Acquires credentials for a specified service using initial credential.
474
* When the service has a different realm
475
* from the initial credential, we do cross-realm authentication
476
* - first, we use the current credential to get
477
* a cross-realm credential from the local KDC, then use that
478
* cross-realm credential to request service credential
479
* from the foreigh KDC.
480
*
481
* @param service the name of service principal using format
482
* components@realm
483
* @param ccreds client's initial credential.
484
* @exception IOException if an error occurs in reading the credentials
485
* cache
486
* @exception KrbException if an error occurs specific to Kerberos
487
* @return a <code>Credentials</code> object.
488
*/
489
490
public static Credentials acquireServiceCreds(String service,
491
Credentials ccreds)
492
throws KrbException, IOException {
493
return CredentialsUtil.acquireServiceCreds(service, ccreds);
494
}
495
496
public static Credentials acquireS4U2selfCreds(PrincipalName user,
497
Credentials ccreds) throws KrbException, IOException {
498
return CredentialsUtil.acquireS4U2selfCreds(user, ccreds);
499
}
500
501
public static Credentials acquireS4U2proxyCreds(String service,
502
Ticket second, PrincipalName client, Credentials ccreds)
503
throws KrbException, IOException {
504
return CredentialsUtil.acquireS4U2proxyCreds(
505
service, second, client, ccreds);
506
}
507
508
public CredentialsCache getCache() {
509
return cache;
510
}
511
512
/*
513
* Prints out debug info.
514
*/
515
public static void printDebug(Credentials c) {
516
System.out.println(">>> DEBUG: ----Credentials----");
517
System.out.println("\tclient: " + c.client.toString());
518
if (c.clientAlias != null)
519
System.out.println("\tclient alias: " + c.clientAlias.toString());
520
System.out.println("\tserver: " + c.server.toString());
521
if (c.serverAlias != null)
522
System.out.println("\tserver alias: " + c.serverAlias.toString());
523
System.out.println("\tticket: sname: " + c.ticket.sname.toString());
524
if (c.startTime != null) {
525
System.out.println("\tstartTime: " + c.startTime.getTime());
526
}
527
System.out.println("\tendTime: " + c.endTime.getTime());
528
System.out.println(" ----Credentials end----");
529
}
530
531
532
@SuppressWarnings("removal")
533
static void ensureLoaded() {
534
java.security.AccessController.doPrivileged(
535
new java.security.PrivilegedAction<Void> () {
536
public Void run() {
537
if (System.getProperty("os.name").contains("OS X")) {
538
System.loadLibrary("osxkrb5");
539
} else {
540
System.loadLibrary("w2k_lsa_auth");
541
}
542
return null;
543
}
544
});
545
alreadyLoaded = true;
546
}
547
548
public String toString() {
549
StringBuilder sb = new StringBuilder("Credentials:");
550
sb.append( "\n client=").append(client);
551
if (clientAlias != null)
552
sb.append( "\n clientAlias=").append(clientAlias);
553
sb.append( "\n server=").append(server);
554
if (serverAlias != null)
555
sb.append( "\n serverAlias=").append(serverAlias);
556
if (authTime != null) {
557
sb.append("\n authTime=").append(authTime);
558
}
559
if (startTime != null) {
560
sb.append("\n startTime=").append(startTime);
561
}
562
sb.append( "\n endTime=").append(endTime);
563
sb.append( "\n renewTill=").append(renewTill);
564
sb.append( "\n flags=").append(flags);
565
sb.append( "\nEType (skey)=").append(key.getEType());
566
sb.append( "\n (tkt key)=").append(ticket.encPart.eType);
567
return sb.toString();
568
}
569
570
public sun.security.krb5.internal.ccache.Credentials toCCacheCreds() {
571
return new sun.security.krb5.internal.ccache.Credentials(
572
getClient(), getServer(),
573
getSessionKey(),
574
date2kt(getAuthTime()),
575
date2kt(getStartTime()),
576
date2kt(getEndTime()),
577
date2kt(getRenewTill()),
578
false,
579
flags,
580
new HostAddresses(getClientAddresses()),
581
getAuthzData(),
582
getTicket(),
583
null);
584
}
585
586
private static KerberosTime date2kt(Date d) {
587
return d == null ? null : new KerberosTime(d);
588
}
589
}
590
591