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/internal/CredentialsUtil.java
41161 views
1
/*
2
* Copyright (c) 2001, 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.internal;
33
34
import sun.security.krb5.*;
35
import sun.security.util.DerValue;
36
37
import java.io.IOException;
38
import java.util.LinkedList;
39
import java.util.List;
40
41
/**
42
* This class is a utility that contains much of the TGS-Exchange
43
* protocol. It is used by ../Credentials.java for service ticket
44
* acquisition in both the normal and the x-realm case.
45
*/
46
public class CredentialsUtil {
47
48
private static boolean DEBUG = sun.security.krb5.internal.Krb5.DEBUG;
49
50
private static enum S4U2Type {
51
NONE, SELF, PROXY
52
}
53
54
/**
55
* Used by a middle server to acquire credentials on behalf of a
56
* client to itself using the S4U2self extension.
57
* @param client the client to impersonate
58
* @param ccreds the TGT of the middle service
59
* @return the new creds (cname=client, sname=middle)
60
*/
61
public static Credentials acquireS4U2selfCreds(PrincipalName client,
62
Credentials ccreds) throws KrbException, IOException {
63
if (!ccreds.isForwardable()) {
64
throw new KrbException("S4U2self needs a FORWARDABLE ticket");
65
}
66
PrincipalName sname = ccreds.getClient();
67
String uRealm = client.getRealmString();
68
String localRealm = ccreds.getClient().getRealmString();
69
if (!uRealm.equals(localRealm)) {
70
// Referrals will be required because the middle service
71
// and the client impersonated are on different realms.
72
if (Config.DISABLE_REFERRALS) {
73
throw new KrbException("Cross-realm S4U2Self request not" +
74
" possible when referrals are disabled.");
75
}
76
if (ccreds.getClientAlias() != null) {
77
// If the name was canonicalized, the user pick
78
// has preference. This gives the possibility of
79
// using FQDNs that KDCs may use to return referrals.
80
// I.e.: a SVC/[email protected] name
81
// may be used by REALM-1.COM KDC to return a
82
// referral to REALM-2.COM.
83
sname = ccreds.getClientAlias();
84
}
85
sname = new PrincipalName(sname.getNameType(),
86
sname.getNameStrings(), new Realm(uRealm));
87
}
88
Credentials creds = serviceCreds(
89
KDCOptions.with(KDCOptions.FORWARDABLE),
90
ccreds, ccreds.getClient(), sname, null,
91
new PAData[] {
92
new PAData(Krb5.PA_FOR_USER,
93
new PAForUserEnc(client,
94
ccreds.getSessionKey()).asn1Encode()),
95
new PAData(Krb5.PA_PAC_OPTIONS,
96
new PaPacOptions()
97
.setResourceBasedConstrainedDelegation(true)
98
.setClaims(true)
99
.asn1Encode())
100
}, S4U2Type.SELF);
101
if (!creds.getClient().equals(client)) {
102
throw new KrbException("S4U2self request not honored by KDC");
103
}
104
if (!creds.isForwardable()) {
105
throw new KrbException("S4U2self ticket must be FORWARDABLE");
106
}
107
return creds;
108
}
109
110
/**
111
* Used by a middle server to acquire a service ticket to a backend
112
* server using the S4U2proxy extension.
113
* @param backend the name of the backend service
114
* @param second the client's service ticket to the middle server
115
* @param ccreds the TGT of the middle server
116
* @return the creds (cname=client, sname=backend)
117
*/
118
public static Credentials acquireS4U2proxyCreds(
119
String backend, Ticket second,
120
PrincipalName client, Credentials ccreds)
121
throws KrbException, IOException {
122
PrincipalName backendPrincipal = new PrincipalName(backend);
123
String backendRealm = backendPrincipal.getRealmString();
124
String localRealm = ccreds.getClient().getRealmString();
125
if (!backendRealm.equals(localRealm)) {
126
// The middle service and the backend service are on
127
// different realms, so referrals will be required.
128
if (Config.DISABLE_REFERRALS) {
129
throw new KrbException("Cross-realm S4U2Proxy request not" +
130
" possible when referrals are disabled.");
131
}
132
backendPrincipal = new PrincipalName(
133
backendPrincipal.getNameType(),
134
backendPrincipal.getNameStrings(),
135
new Realm(localRealm));
136
}
137
Credentials creds = serviceCreds(KDCOptions.with(
138
KDCOptions.CNAME_IN_ADDL_TKT, KDCOptions.FORWARDABLE),
139
ccreds, ccreds.getClient(), backendPrincipal,
140
new Ticket[] {second}, new PAData[] {
141
new PAData(Krb5.PA_PAC_OPTIONS,
142
new PaPacOptions()
143
.setResourceBasedConstrainedDelegation(true)
144
.setClaims(true)
145
.asn1Encode())
146
}, S4U2Type.PROXY);
147
if (!creds.getClient().equals(client)) {
148
throw new KrbException("S4U2proxy request not honored by KDC");
149
}
150
return creds;
151
}
152
153
/**
154
* Acquires credentials for a specified service using initial
155
* credential. When the service has a different realm from the initial
156
* credential, we do cross-realm authentication - first, we use the
157
* current credential to get a cross-realm credential from the local KDC,
158
* then use that cross-realm credential to request service credential
159
* from the foreign KDC.
160
*
161
* @param service the name of service principal
162
* @param ccreds client's initial credential
163
*/
164
public static Credentials acquireServiceCreds(
165
String service, Credentials ccreds)
166
throws KrbException, IOException {
167
PrincipalName sname = new PrincipalName(service,
168
PrincipalName.KRB_NT_UNKNOWN);
169
return serviceCreds(sname, ccreds);
170
}
171
172
/**
173
* Gets a TGT to another realm
174
* @param localRealm this realm
175
* @param serviceRealm the other realm, cannot equals to localRealm
176
* @param ccreds TGT in this realm
177
* @param okAsDelegate an [out] argument to receive the okAsDelegate
178
* property. True only if all realms allow delegation.
179
* @return the TGT for the other realm, null if cannot find a path
180
* @throws KrbException if something goes wrong
181
*/
182
private static Credentials getTGTforRealm(String localRealm,
183
String serviceRealm, Credentials ccreds, boolean[] okAsDelegate)
184
throws KrbException {
185
186
// Get a list of realms to traverse
187
String[] realms = Realm.getRealmsList(localRealm, serviceRealm);
188
189
int i = 0, k = 0;
190
Credentials cTgt = null, newTgt = null, theTgt = null;
191
PrincipalName tempService = null;
192
String newTgtRealm = null;
193
194
okAsDelegate[0] = true;
195
for (cTgt = ccreds, i = 0; i < realms.length;) {
196
tempService = PrincipalName.tgsService(serviceRealm, realms[i]);
197
198
if (DEBUG) {
199
System.out.println(
200
">>> Credentials acquireServiceCreds: main loop: ["
201
+ i +"] tempService=" + tempService);
202
}
203
204
try {
205
newTgt = serviceCreds(tempService, cTgt);
206
} catch (Exception exc) {
207
newTgt = null;
208
}
209
210
if (newTgt == null) {
211
if (DEBUG) {
212
System.out.println(">>> Credentials acquireServiceCreds: "
213
+ "no tgt; searching thru capath");
214
}
215
216
/*
217
* No tgt found. Let's go thru the realms list one by one.
218
*/
219
for (newTgt = null, k = i+1;
220
newTgt == null && k < realms.length; k++) {
221
tempService = PrincipalName.tgsService(realms[k], realms[i]);
222
if (DEBUG) {
223
System.out.println(
224
">>> Credentials acquireServiceCreds: "
225
+ "inner loop: [" + k
226
+ "] tempService=" + tempService);
227
}
228
try {
229
newTgt = serviceCreds(tempService, cTgt);
230
} catch (Exception exc) {
231
newTgt = null;
232
}
233
}
234
} // Ends 'if (newTgt == null)'
235
236
if (newTgt == null) {
237
if (DEBUG) {
238
System.out.println(">>> Credentials acquireServiceCreds: "
239
+ "no tgt; cannot get creds");
240
}
241
break;
242
}
243
244
/*
245
* We have a tgt. It may or may not be for the target.
246
* If it's for the target realm, we're done looking for a tgt.
247
*/
248
newTgtRealm = newTgt.getServer().getInstanceComponent();
249
if (okAsDelegate[0] && !newTgt.checkDelegate()) {
250
if (DEBUG) {
251
System.out.println(">>> Credentials acquireServiceCreds: " +
252
"global OK-AS-DELEGATE turned off at " +
253
newTgt.getServer());
254
}
255
okAsDelegate[0] = false;
256
}
257
258
if (DEBUG) {
259
System.out.println(">>> Credentials acquireServiceCreds: "
260
+ "got tgt");
261
}
262
263
if (newTgtRealm.equals(serviceRealm)) {
264
/* We got the right tgt */
265
theTgt = newTgt;
266
break;
267
}
268
269
/*
270
* The new tgt is not for the target realm.
271
* See if the realm of the new tgt is in the list of realms
272
* and continue looking from there.
273
*/
274
for (k = i+1; k < realms.length; k++) {
275
if (newTgtRealm.equals(realms[k])) {
276
break;
277
}
278
}
279
280
if (k < realms.length) {
281
/*
282
* (re)set the counter so we start looking
283
* from the realm we just obtained a tgt for.
284
*/
285
i = k;
286
cTgt = newTgt;
287
288
if (DEBUG) {
289
System.out.println(">>> Credentials acquireServiceCreds: "
290
+ "continuing with main loop counter reset to " + i);
291
}
292
continue;
293
}
294
else {
295
/*
296
* The new tgt's realm is not in the hierarchy of realms.
297
* It's probably not safe to get a tgt from
298
* a tgs that is outside the known list of realms.
299
* Give up now.
300
*/
301
break;
302
}
303
} // Ends outermost/main 'for' loop
304
305
return theTgt;
306
}
307
308
/*
309
* This method does the real job to request the service credential.
310
*/
311
private static Credentials serviceCreds(
312
PrincipalName service, Credentials ccreds)
313
throws KrbException, IOException {
314
return serviceCreds(new KDCOptions(), ccreds,
315
ccreds.getClient(), service, null, null,
316
S4U2Type.NONE);
317
}
318
319
/*
320
* Obtains credentials for a service (TGS).
321
* Cross-realm referrals are handled if enabled. A fallback scheme
322
* without cross-realm referrals supports is used in case of server
323
* error to maintain backward compatibility.
324
*/
325
private static Credentials serviceCreds(
326
KDCOptions options, Credentials asCreds,
327
PrincipalName cname, PrincipalName sname,
328
Ticket[] additionalTickets, PAData[] extraPAs,
329
S4U2Type s4u2Type)
330
throws KrbException, IOException {
331
if (!Config.DISABLE_REFERRALS) {
332
try {
333
return serviceCredsReferrals(options, asCreds, cname, sname,
334
s4u2Type, additionalTickets, extraPAs);
335
} catch (KrbException e) {
336
// Server may raise an error if CANONICALIZE is true.
337
// Try CANONICALIZE false.
338
}
339
}
340
return serviceCredsSingle(options, asCreds, cname,
341
asCreds.getClientAlias(), sname, sname, s4u2Type,
342
additionalTickets, extraPAs);
343
}
344
345
/*
346
* Obtains credentials for a service (TGS).
347
* May handle and follow cross-realm referrals as defined by RFC 6806.
348
*/
349
private static Credentials serviceCredsReferrals(
350
KDCOptions options, Credentials asCreds,
351
PrincipalName cname, PrincipalName sname,
352
S4U2Type s4u2Type, Ticket[] additionalTickets,
353
PAData[] extraPAs)
354
throws KrbException, IOException {
355
options = new KDCOptions(options.toBooleanArray());
356
options.set(KDCOptions.CANONICALIZE, true);
357
PrincipalName cSname = sname;
358
PrincipalName refSname = sname; // May change with referrals
359
Credentials creds = null;
360
boolean isReferral = false;
361
List<String> referrals = new LinkedList<>();
362
PrincipalName clientAlias = asCreds.getClientAlias();
363
while (referrals.size() <= Config.MAX_REFERRALS) {
364
ReferralsCache.ReferralCacheEntry ref =
365
ReferralsCache.get(cname, sname, refSname.getRealmString());
366
String toRealm = null;
367
if (ref == null) {
368
creds = serviceCredsSingle(options, asCreds, cname,
369
clientAlias, refSname, cSname, s4u2Type,
370
additionalTickets, extraPAs);
371
PrincipalName server = creds.getServer();
372
if (!refSname.equals(server)) {
373
String[] serverNameStrings = server.getNameStrings();
374
if (serverNameStrings.length == 2 &&
375
serverNameStrings[0].equals(
376
PrincipalName.TGS_DEFAULT_SRV_NAME) &&
377
!refSname.getRealmAsString().equals(
378
serverNameStrings[1])) {
379
// Server Name (sname) has the following format:
380
// krbtgt/[email protected]
381
if (s4u2Type == S4U2Type.NONE) {
382
// Do not store S4U2Self or S4U2Proxy referral
383
// TGTs in the cache. Caching such tickets is not
384
// defined in MS-SFU and may cause unexpected
385
// results when using them in a different context.
386
ReferralsCache.put(cname, sname,
387
server.getRealmString(),
388
serverNameStrings[1], creds);
389
}
390
toRealm = serverNameStrings[1];
391
isReferral = true;
392
}
393
}
394
} else {
395
creds = ref.getCreds();
396
toRealm = ref.getToRealm();
397
isReferral = true;
398
}
399
if (isReferral) {
400
if (s4u2Type == S4U2Type.PROXY) {
401
Credentials[] credsInOut =
402
new Credentials[] {creds, null};
403
toRealm = handleS4U2ProxyReferral(asCreds,
404
credsInOut, sname);
405
creds = credsInOut[0];
406
if (additionalTickets == null ||
407
additionalTickets.length == 0 ||
408
credsInOut[1] == null) {
409
throw new KrbException("Additional tickets expected" +
410
" for S4U2Proxy.");
411
}
412
additionalTickets[0] = credsInOut[1].getTicket();
413
} else if (s4u2Type == S4U2Type.SELF) {
414
handleS4U2SelfReferral(extraPAs, asCreds, creds);
415
}
416
if (referrals.contains(toRealm)) {
417
// Referrals loop detected
418
return null;
419
}
420
asCreds = creds;
421
refSname = new PrincipalName(refSname.getNameString(),
422
refSname.getNameType(), toRealm);
423
referrals.add(toRealm);
424
isReferral = false;
425
continue;
426
}
427
break;
428
}
429
return creds;
430
}
431
432
/*
433
* Obtains credentials for a service (TGS).
434
* If the service realm is different than the one in the TGT, a new TGT for
435
* the service realm is obtained first (see getTGTforRealm call). This is
436
* not expected when following cross-realm referrals because the referral
437
* TGT realm matches the service realm.
438
*/
439
private static Credentials serviceCredsSingle(
440
KDCOptions options, Credentials asCreds,
441
PrincipalName cname, PrincipalName clientAlias,
442
PrincipalName refSname, PrincipalName sname,
443
S4U2Type s4u2Type, Ticket[] additionalTickets,
444
PAData[] extraPAs)
445
throws KrbException, IOException {
446
Credentials theCreds = null;
447
boolean[] okAsDelegate = new boolean[]{true};
448
String[] serverAsCredsNames = asCreds.getServer().getNameStrings();
449
String tgtRealm = serverAsCredsNames[1];
450
String serviceRealm = refSname.getRealmString();
451
if (!serviceRealm.equals(tgtRealm)) {
452
// This is a cross-realm service request
453
if (DEBUG) {
454
System.out.println(">>> serviceCredsSingle:" +
455
" cross-realm authentication");
456
System.out.println(">>> serviceCredsSingle:" +
457
" obtaining credentials from " + tgtRealm +
458
" to " + serviceRealm);
459
}
460
Credentials newTgt = getTGTforRealm(tgtRealm, serviceRealm,
461
asCreds, okAsDelegate);
462
if (newTgt == null) {
463
throw new KrbApErrException(Krb5.KRB_AP_ERR_GEN_CRED,
464
"No service creds");
465
}
466
if (DEBUG) {
467
System.out.println(">>> Cross-realm TGT Credentials" +
468
" serviceCredsSingle: ");
469
Credentials.printDebug(newTgt);
470
}
471
if (s4u2Type == S4U2Type.SELF) {
472
handleS4U2SelfReferral(extraPAs, asCreds, newTgt);
473
}
474
asCreds = newTgt;
475
cname = asCreds.getClient();
476
} else if (DEBUG) {
477
System.out.println(">>> Credentials serviceCredsSingle:" +
478
" same realm");
479
}
480
KrbTgsReq req = new KrbTgsReq(options, asCreds, cname, clientAlias,
481
refSname, sname, additionalTickets, extraPAs);
482
theCreds = req.sendAndGetCreds();
483
if (theCreds != null) {
484
if (DEBUG) {
485
System.out.println(">>> TGS credentials serviceCredsSingle:");
486
Credentials.printDebug(theCreds);
487
}
488
if (!okAsDelegate[0]) {
489
theCreds.resetDelegate();
490
}
491
}
492
return theCreds;
493
}
494
495
/**
496
* PA-FOR-USER may need to be regenerated if credentials
497
* change. This may happen when obtaining a TGT for a
498
* different realm or when using a referral TGT.
499
*/
500
private static void handleS4U2SelfReferral(PAData[] pas,
501
Credentials oldCeds, Credentials newCreds)
502
throws Asn1Exception, KrbException, IOException {
503
if (DEBUG) {
504
System.out.println(">>> Handling S4U2Self referral");
505
}
506
for (int i = 0; i < pas.length; i++) {
507
PAData pa = pas[i];
508
if (pa.getType() == Krb5.PA_FOR_USER) {
509
PAForUserEnc paForUser = new PAForUserEnc(
510
new DerValue(pa.getValue()),
511
oldCeds.getSessionKey());
512
pas[i] = new PAData(Krb5.PA_FOR_USER,
513
new PAForUserEnc(paForUser.getName(),
514
newCreds.getSessionKey()).asn1Encode());
515
break;
516
}
517
}
518
}
519
520
/**
521
* This method is called after receiving the first realm referral for
522
* a S4U2Proxy request. The credentials and tickets needed for the
523
* final S4U2Proxy request (in the referrals chain) are returned.
524
*
525
* Referrals are handled as described by MS-SFU (section 3.1.5.2.2
526
* Receives Referral).
527
*
528
* @param asCreds middle service credentials used for the first S4U2Proxy
529
* request
530
* @param credsInOut (in/out parameter):
531
* * input: first S4U2Proxy referral TGT received, null
532
* * output: referral TGT for final S4U2Proxy service request,
533
* client referral TGT for final S4U2Proxy service request
534
* (to be sent as additional-ticket)
535
* @param sname the backend service name
536
* @param additionalTickets (out parameter): the additional ticket for the
537
* last S4U2Proxy request is returned
538
* @return the backend realm for the last S4U2Proxy request
539
*/
540
private static String handleS4U2ProxyReferral(Credentials asCreds,
541
Credentials[] credsInOut, PrincipalName sname)
542
throws KrbException, IOException {
543
if (DEBUG) {
544
System.out.println(">>> Handling S4U2Proxy referral");
545
}
546
Credentials refTGT = null;
547
// Get a credential for the middle service to the backend so we know
548
// the backend realm, as described in MS-SFU (section 3.1.5.2.2).
549
Credentials middleSvcCredsInBackendRealm =
550
serviceCreds(sname, asCreds);
551
String backendRealm =
552
middleSvcCredsInBackendRealm.getServer().getRealmString();
553
String toRealm = credsInOut[0].getServer().getNameStrings()[1];
554
if (!toRealm.equals(backendRealm)) {
555
// More than 1 hop. Follow the referrals chain and obtain a
556
// TGT for the backend realm.
557
refTGT = getTGTforRealm(toRealm, backendRealm, credsInOut[0],
558
new boolean[1]);
559
} else {
560
// There was only 1 hop. The referral TGT received is already
561
// for the backend realm.
562
refTGT = credsInOut[0];
563
}
564
credsInOut[0] = getTGTforRealm(asCreds.getClient().getRealmString(),
565
backendRealm, asCreds, new boolean[1]);
566
credsInOut[1] = refTGT;
567
return backendRealm;
568
}
569
}
570
571