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