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