Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/src/java.security.jgss/share/classes/sun/security/jgss/krb5/SubjectComber.java
67766 views
1
/*
2
* Copyright (c) 2002, 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 sun.security.krb5.JavaxSecurityAuthKerberosAccess;
29
import sun.security.krb5.KerberosSecrets;
30
31
import javax.security.auth.kerberos.KerberosTicket;
32
import javax.security.auth.kerberos.KerberosKey;
33
import javax.security.auth.Subject;
34
import javax.security.auth.DestroyFailedException;
35
import java.util.Iterator;
36
import java.util.ArrayList;
37
import java.util.List;
38
import java.util.Set;
39
import javax.security.auth.kerberos.KerberosPrincipal;
40
import javax.security.auth.kerberos.KeyTab;
41
42
/**
43
* This utility looks through the current Subject and retrieves private
44
* credentials for the desired client/server principals.
45
*
46
* @author Ram Marti
47
* @since 1.4.2
48
*/
49
50
class SubjectComber {
51
52
private static final boolean DEBUG = Krb5Util.DEBUG;
53
54
/**
55
* Default constructor
56
*/
57
private SubjectComber() { // Cannot create one of these
58
}
59
60
static <T> T find(Subject subject, String serverPrincipal,
61
String clientPrincipal, Class<T> credClass) {
62
63
// findAux returns T if oneOnly.
64
return credClass.cast(findAux(subject, serverPrincipal,
65
clientPrincipal, credClass, true));
66
}
67
68
@SuppressWarnings("unchecked") // findAux returns List<T> if !oneOnly.
69
static <T> List<T> findMany(Subject subject, String serverPrincipal,
70
String clientPrincipal, Class<T> credClass) {
71
72
return (List<T>)findAux(subject, serverPrincipal, clientPrincipal,
73
credClass, false);
74
}
75
76
/**
77
* Find private credentials for the specified client/server principals
78
* in the subject. Returns null if the subject is null.
79
*
80
* @return the private credentials
81
*/
82
// Returns T if oneOnly and List<T> if !oneOnly.
83
private static <T> Object findAux(Subject subject, String serverPrincipal,
84
String clientPrincipal, Class<T> credClass, boolean oneOnly) {
85
86
if (subject == null) {
87
return null;
88
} else {
89
List<T> answer = (oneOnly ? null : new ArrayList<T>());
90
91
if (credClass == KeyTab.class) {
92
Iterator<KeyTab> iterator =
93
subject.getPrivateCredentials(KeyTab.class).iterator();
94
while (iterator.hasNext()) {
95
KeyTab t = iterator.next();
96
if (serverPrincipal != null && t.isBound()) {
97
KerberosPrincipal name = t.getPrincipal();
98
if (name != null) {
99
if (!serverPrincipal.equals(name.getName())) {
100
continue;
101
}
102
} else {
103
// legacy bound keytab. although we don't know who
104
// the bound principal is, it must be in allPrincs
105
boolean found = false;
106
for (KerberosPrincipal princ:
107
subject.getPrincipals(KerberosPrincipal.class)) {
108
if (princ.getName().equals(serverPrincipal)) {
109
found = true;
110
break;
111
}
112
}
113
if (!found) continue;
114
}
115
}
116
// Check passed, we can add now
117
if (DEBUG) {
118
System.out.println("Found " + credClass.getSimpleName()
119
+ " " + t);
120
}
121
if (oneOnly) {
122
return t;
123
} else {
124
answer.add(credClass.cast(t));
125
}
126
}
127
} else if (credClass == KerberosKey.class) {
128
// We are looking for credentials for the serverPrincipal
129
Iterator<KerberosKey> iterator =
130
subject.getPrivateCredentials(KerberosKey.class).iterator();
131
while (iterator.hasNext()) {
132
KerberosKey t = iterator.next();
133
String name = t.getPrincipal().getName();
134
if (serverPrincipal == null || serverPrincipal.equals(name)) {
135
if (DEBUG) {
136
System.out.println("Found " +
137
credClass.getSimpleName() + " for " + name);
138
}
139
if (oneOnly) {
140
return t;
141
} else {
142
answer.add(credClass.cast(t));
143
}
144
}
145
}
146
} else if (credClass == KerberosTicket.class) {
147
// we are looking for a KerberosTicket credentials
148
// for client-service principal pair
149
Set<Object> pcs = subject.getPrivateCredentials();
150
synchronized (pcs) {
151
Iterator<Object> iterator = pcs.iterator();
152
while (iterator.hasNext()) {
153
Object obj = iterator.next();
154
if (!(obj instanceof KerberosTicket)) {
155
continue;
156
}
157
@SuppressWarnings("unchecked")
158
KerberosTicket ticket = (KerberosTicket)obj;
159
if (DEBUG) {
160
System.out.println("Found ticket for "
161
+ ticket.getClient()
162
+ " to go to "
163
+ ticket.getServer()
164
+ " expiring on "
165
+ ticket.getEndTime());
166
}
167
if (!ticket.isCurrent()) {
168
// let us remove the ticket from the Subject
169
// Note that both TGT and service ticket will be
170
// removed upon expiration
171
if (!subject.isReadOnly()) {
172
iterator.remove();
173
try {
174
ticket.destroy();
175
if (DEBUG) {
176
System.out.println("Removed and destroyed "
177
+ "the expired Ticket \n"
178
+ ticket);
179
180
}
181
} catch (DestroyFailedException dfe) {
182
if (DEBUG) {
183
System.out.println("Expired ticket not" +
184
" destroyed successfully. " + dfe);
185
}
186
}
187
}
188
continue;
189
}
190
String serverMatch = findServerMatch(serverPrincipal, ticket);
191
if (serverMatch != null) {
192
String clientMatch = findClientMatch(clientPrincipal, ticket);
193
if (clientMatch != null) {
194
if (oneOnly) {
195
return ticket;
196
} else {
197
// Record names so that tickets will
198
// all belong to same principals
199
if (clientPrincipal == null) {
200
clientPrincipal = clientMatch;
201
}
202
if (serverPrincipal == null) {
203
serverPrincipal = serverMatch;
204
}
205
answer.add(credClass.cast(ticket));
206
}
207
}
208
}
209
}
210
}
211
}
212
return answer;
213
}
214
}
215
216
private static String findServerMatch(String input, KerberosTicket ticket) {
217
KerberosPrincipal serverAlias = KerberosSecrets
218
.getJavaxSecurityAuthKerberosAccess()
219
.kerberosTicketGetServerAlias(ticket);
220
if (input != null) {
221
return ((serverAlias != null && input.equals(serverAlias.getName())) ||
222
input.equals(ticket.getServer().getName()))
223
? input : null;
224
} else {
225
return serverAlias != null
226
? serverAlias.getName()
227
: ticket.getServer().getName();
228
}
229
}
230
231
private static String findClientMatch(String input, KerberosTicket ticket) {
232
JavaxSecurityAuthKerberosAccess access = KerberosSecrets
233
.getJavaxSecurityAuthKerberosAccess();
234
KerberosPrincipal clientAlias = access.kerberosTicketGetClientAlias(ticket);
235
KerberosTicket proxy = access.kerberosTicketGetProxy(ticket);
236
if (input != null) {
237
return ((clientAlias != null && input.equals(clientAlias.getName())) ||
238
(proxy != null && input.equals(proxy.getClient().getName())) ||
239
(proxy == null && input.equals(ticket.getClient().getName())))
240
? input : null;
241
} else {
242
if (clientAlias != null) {
243
return clientAlias.getName();
244
} else if (proxy != null) {
245
return proxy.getClient().getName();
246
} else {
247
return ticket.getClient().getName();
248
}
249
}
250
}
251
}
252
253