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/Krb5AcceptCredential.java
41161 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
package sun.security.jgss.krb5;
27
28
import java.io.IOException;
29
import org.ietf.jgss.*;
30
import sun.security.jgss.GSSCaller;
31
import sun.security.jgss.spi.*;
32
import sun.security.krb5.*;
33
import java.security.PrivilegedActionException;
34
import java.security.PrivilegedExceptionAction;
35
import java.security.AccessController;
36
import java.security.AccessControlContext;
37
import javax.security.auth.DestroyFailedException;
38
39
/**
40
* Implements the krb5 acceptor credential element.
41
*
42
* @author Mayank Upadhyay
43
* @since 1.4
44
*/
45
public class Krb5AcceptCredential
46
implements Krb5CredElement {
47
48
private final Krb5NameElement name;
49
private final ServiceCreds screds;
50
51
private Krb5AcceptCredential(Krb5NameElement name, ServiceCreds creds) {
52
/*
53
* Initialize this instance with the data from the acquired
54
* KerberosKey. This class needs to be a KerberosKey too
55
* hence we can't just store a reference.
56
*/
57
58
this.name = name;
59
this.screds = creds;
60
}
61
62
@SuppressWarnings("removal")
63
static Krb5AcceptCredential getInstance(final GSSCaller caller, Krb5NameElement name)
64
throws GSSException {
65
66
final String serverPrinc = (name == null? null:
67
name.getKrb5PrincipalName().getName());
68
final AccessControlContext acc = AccessController.getContext();
69
70
ServiceCreds creds = null;
71
try {
72
creds = AccessController.doPrivileged(
73
new PrivilegedExceptionAction<ServiceCreds>() {
74
public ServiceCreds run() throws Exception {
75
return Krb5Util.getServiceCreds(
76
caller == GSSCaller.CALLER_UNKNOWN ? GSSCaller.CALLER_ACCEPT: caller,
77
serverPrinc, acc);
78
}});
79
} catch (PrivilegedActionException e) {
80
GSSException ge =
81
new GSSException(GSSException.NO_CRED, -1,
82
"Attempt to obtain new ACCEPT credentials failed!");
83
ge.initCause(e.getException());
84
throw ge;
85
}
86
87
if (creds == null)
88
throw new GSSException(GSSException.NO_CRED, -1,
89
"Failed to find any Kerberos credentials");
90
91
if (name == null) {
92
String fullName = creds.getName();
93
if (fullName != null) {
94
name = Krb5NameElement.getInstance(fullName,
95
Krb5MechFactory.NT_GSS_KRB5_PRINCIPAL);
96
}
97
}
98
99
return new Krb5AcceptCredential(name, creds);
100
}
101
102
/**
103
* Returns the principal name for this credential. The name
104
* is in mechanism specific format.
105
*
106
* @return GSSNameSpi representing principal name of this credential
107
* @exception GSSException may be thrown
108
*/
109
public final GSSNameSpi getName() throws GSSException {
110
return name;
111
}
112
113
/**
114
* Returns the init lifetime remaining.
115
*
116
* @return the init lifetime remaining in seconds
117
* @exception GSSException may be thrown
118
*/
119
public int getInitLifetime() throws GSSException {
120
return 0;
121
}
122
123
/**
124
* Returns the accept lifetime remaining.
125
*
126
* @return the accept lifetime remaining in seconds
127
* @exception GSSException may be thrown
128
*/
129
public int getAcceptLifetime() throws GSSException {
130
return GSSCredential.INDEFINITE_LIFETIME;
131
}
132
133
public boolean isInitiatorCredential() throws GSSException {
134
return false;
135
}
136
137
public boolean isAcceptorCredential() throws GSSException {
138
return true;
139
}
140
141
/**
142
* Returns the oid representing the underlying credential
143
* mechanism oid.
144
*
145
* @return the Oid for this credential mechanism
146
* @exception GSSException may be thrown
147
*/
148
public final Oid getMechanism() {
149
return Krb5MechFactory.GSS_KRB5_MECH_OID;
150
}
151
152
public final java.security.Provider getProvider() {
153
return Krb5MechFactory.PROVIDER;
154
}
155
156
public EncryptionKey[] getKrb5EncryptionKeys(PrincipalName princ) {
157
return screds.getEKeys(princ);
158
}
159
160
/**
161
* Called to invalidate this credential element.
162
*/
163
public void dispose() throws GSSException {
164
try {
165
destroy();
166
} catch (DestroyFailedException e) {
167
GSSException gssException =
168
new GSSException(GSSException.FAILURE, -1,
169
"Could not destroy credentials - " + e.getMessage());
170
gssException.initCause(e);
171
}
172
}
173
174
/**
175
* Destroys the locally cached EncryptionKey value and then calls
176
* destroy in the base class.
177
*/
178
public void destroy() throws DestroyFailedException {
179
screds.destroy();
180
}
181
182
/**
183
* Impersonation is only available on the initiator side. The
184
* service must starts as an initiator to get an initial TGT to complete
185
* the S4U2self protocol.
186
*/
187
@Override
188
public GSSCredentialSpi impersonate(GSSNameSpi name) throws GSSException {
189
Credentials cred = screds.getInitCred();
190
if (cred != null) {
191
return Krb5InitCredential.getInstance(this.name, cred)
192
.impersonate(name);
193
} else {
194
throw new GSSException(GSSException.FAILURE, -1,
195
"Only an initiate credentials can impersonate");
196
}
197
}
198
}
199
200