author | weijun |
Wed, 28 Sep 2011 14:21:10 +0800 | |
changeset 10696 | 3811a12690ce |
parent 9499 | f3115698a012 |
child 14413 | e954df027393 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
9499 | 2 |
* Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.security.jgss.krb5; |
|
27 |
||
28 |
import org.ietf.jgss.*; |
|
2942
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
2
diff
changeset
|
29 |
import sun.security.jgss.GSSCaller; |
2 | 30 |
import sun.security.jgss.spi.*; |
31 |
import sun.security.krb5.*; |
|
32 |
import java.security.PrivilegedActionException; |
|
33 |
import java.security.PrivilegedExceptionAction; |
|
34 |
import java.security.AccessController; |
|
35 |
import java.security.AccessControlContext; |
|
36 |
import javax.security.auth.DestroyFailedException; |
|
37 |
||
38 |
/** |
|
39 |
* Implements the krb5 acceptor credential element. |
|
40 |
* |
|
41 |
* @author Mayank Upadhyay |
|
42 |
* @since 1.4 |
|
43 |
*/ |
|
44 |
public class Krb5AcceptCredential |
|
45 |
implements Krb5CredElement { |
|
46 |
||
47 |
private static final long serialVersionUID = 7714332137352567952L; |
|
48 |
||
49 |
private Krb5NameElement name; |
|
50 |
||
9499 | 51 |
private Krb5Util.ServiceCreds screds; |
2 | 52 |
|
9499 | 53 |
private Krb5AcceptCredential(Krb5NameElement name, Krb5Util.ServiceCreds creds) { |
2 | 54 |
/* |
55 |
* Initialize this instance with the data from the acquired |
|
56 |
* KerberosKey. This class needs to be a KerberosKey too |
|
57 |
* hence we can't just store a reference. |
|
58 |
*/ |
|
59 |
||
60 |
this.name = name; |
|
9499 | 61 |
this.screds = creds; |
2 | 62 |
} |
63 |
||
2942
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
2
diff
changeset
|
64 |
static Krb5AcceptCredential getInstance(final GSSCaller caller, Krb5NameElement name) |
2 | 65 |
throws GSSException { |
66 |
||
67 |
final String serverPrinc = (name == null? null: |
|
68 |
name.getKrb5PrincipalName().getName()); |
|
69 |
final AccessControlContext acc = AccessController.getContext(); |
|
70 |
||
9499 | 71 |
Krb5Util.ServiceCreds creds = null; |
2 | 72 |
try { |
9499 | 73 |
creds = AccessController.doPrivileged( |
74 |
new PrivilegedExceptionAction<Krb5Util.ServiceCreds>() { |
|
75 |
public Krb5Util.ServiceCreds run() throws Exception { |
|
76 |
return Krb5Util.getServiceCreds( |
|
2942
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
2
diff
changeset
|
77 |
caller == GSSCaller.CALLER_UNKNOWN ? GSSCaller.CALLER_ACCEPT: caller, |
2 | 78 |
serverPrinc, acc); |
79 |
}}); |
|
80 |
} catch (PrivilegedActionException e) { |
|
81 |
GSSException ge = |
|
82 |
new GSSException(GSSException.NO_CRED, -1, |
|
83 |
"Attempt to obtain new ACCEPT credentials failed!"); |
|
84 |
ge.initCause(e.getException()); |
|
85 |
throw ge; |
|
86 |
} |
|
87 |
||
9499 | 88 |
if (creds == null) |
2 | 89 |
throw new GSSException(GSSException.NO_CRED, -1, |
9499 | 90 |
"Failed to find any Kerberos credentails"); |
2 | 91 |
|
92 |
if (name == null) { |
|
9499 | 93 |
String fullName = creds.getName(); |
2 | 94 |
name = Krb5NameElement.getInstance(fullName, |
95 |
Krb5MechFactory.NT_GSS_KRB5_PRINCIPAL); |
|
96 |
} |
|
97 |
||
9499 | 98 |
return new Krb5AcceptCredential(name, creds); |
2 | 99 |
} |
100 |
||
101 |
/** |
|
102 |
* Returns the principal name for this credential. The name |
|
103 |
* is in mechanism specific format. |
|
104 |
* |
|
105 |
* @return GSSNameSpi representing principal name of this credential |
|
106 |
* @exception GSSException may be thrown |
|
107 |
*/ |
|
108 |
public final GSSNameSpi getName() throws GSSException { |
|
109 |
return name; |
|
110 |
} |
|
111 |
||
112 |
/** |
|
113 |
* Returns the init lifetime remaining. |
|
114 |
* |
|
115 |
* @return the init lifetime remaining in seconds |
|
116 |
* @exception GSSException may be thrown |
|
117 |
*/ |
|
118 |
public int getInitLifetime() throws GSSException { |
|
119 |
return 0; |
|
120 |
} |
|
121 |
||
122 |
/** |
|
123 |
* Returns the accept lifetime remaining. |
|
124 |
* |
|
125 |
* @return the accept lifetime remaining in seconds |
|
126 |
* @exception GSSException may be thrown |
|
127 |
*/ |
|
128 |
public int getAcceptLifetime() throws GSSException { |
|
129 |
return GSSCredential.INDEFINITE_LIFETIME; |
|
130 |
} |
|
131 |
||
132 |
public boolean isInitiatorCredential() throws GSSException { |
|
133 |
return false; |
|
134 |
} |
|
135 |
||
136 |
public boolean isAcceptorCredential() throws GSSException { |
|
137 |
return true; |
|
138 |
} |
|
139 |
||
140 |
/** |
|
141 |
* Returns the oid representing the underlying credential |
|
142 |
* mechanism oid. |
|
143 |
* |
|
144 |
* @return the Oid for this credential mechanism |
|
145 |
* @exception GSSException may be thrown |
|
146 |
*/ |
|
147 |
public final Oid getMechanism() { |
|
148 |
return Krb5MechFactory.GSS_KRB5_MECH_OID; |
|
149 |
} |
|
150 |
||
151 |
public final java.security.Provider getProvider() { |
|
152 |
return Krb5MechFactory.PROVIDER; |
|
153 |
} |
|
154 |
||
155 |
EncryptionKey[] getKrb5EncryptionKeys() { |
|
9499 | 156 |
return screds.getEKeys(); |
2 | 157 |
} |
158 |
||
159 |
/** |
|
160 |
* Called to invalidate this credential element. |
|
161 |
*/ |
|
162 |
public void dispose() throws GSSException { |
|
163 |
try { |
|
164 |
destroy(); |
|
165 |
} catch (DestroyFailedException e) { |
|
166 |
GSSException gssException = |
|
167 |
new GSSException(GSSException.FAILURE, -1, |
|
168 |
"Could not destroy credentials - " + e.getMessage()); |
|
169 |
gssException.initCause(e); |
|
170 |
} |
|
171 |
} |
|
172 |
||
173 |
/** |
|
174 |
* Destroys the locally cached EncryptionKey value and then calls |
|
175 |
* destroy in the base class. |
|
176 |
*/ |
|
177 |
public void destroy() throws DestroyFailedException { |
|
9499 | 178 |
screds.destroy(); |
2 | 179 |
} |
180 |
} |