author | ohair |
Tue, 25 May 2010 15:58:33 -0700 | |
changeset 5506 | 202f599c92aa |
parent 2942 | 37d9baeb7518 |
child 15006 | 10d6aacdd67f |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2000, 2009, 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.*; |
|
29 |
import sun.security.jgss.GSSUtil; |
|
2942
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
2
diff
changeset
|
30 |
import sun.security.jgss.GSSCaller; |
2 | 31 |
import sun.security.jgss.spi.*; |
32 |
import javax.security.auth.kerberos.ServicePermission; |
|
33 |
import java.security.Provider; |
|
34 |
import java.util.Vector; |
|
35 |
||
36 |
/** |
|
37 |
* Krb5 Mechanism plug in for JGSS |
|
38 |
* This is the properties object required by the JGSS framework. |
|
39 |
* All mechanism specific information is defined here. |
|
40 |
* |
|
41 |
* @author Mayank Upadhyay |
|
42 |
*/ |
|
43 |
||
44 |
public final class Krb5MechFactory implements MechanismFactory { |
|
45 |
||
46 |
private static final boolean DEBUG = Krb5Util.DEBUG; |
|
47 |
||
48 |
static final Provider PROVIDER = |
|
49 |
new sun.security.jgss.SunProvider(); |
|
50 |
||
51 |
static final Oid GSS_KRB5_MECH_OID = |
|
52 |
createOid("1.2.840.113554.1.2.2"); |
|
53 |
||
54 |
static final Oid NT_GSS_KRB5_PRINCIPAL = |
|
55 |
createOid("1.2.840.113554.1.2.2.1"); |
|
56 |
||
57 |
private static Oid[] nameTypes = |
|
58 |
new Oid[] { GSSName.NT_USER_NAME, |
|
59 |
GSSName.NT_HOSTBASED_SERVICE, |
|
60 |
GSSName.NT_EXPORT_NAME, |
|
61 |
NT_GSS_KRB5_PRINCIPAL}; |
|
62 |
||
2942
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
2
diff
changeset
|
63 |
final private GSSCaller caller; |
2 | 64 |
|
65 |
private static Krb5CredElement getCredFromSubject(GSSNameSpi name, |
|
66 |
boolean initiate) |
|
67 |
throws GSSException { |
|
68 |
Vector<Krb5CredElement> creds = |
|
69 |
GSSUtil.searchSubject(name, GSS_KRB5_MECH_OID, initiate, |
|
70 |
(initiate ? |
|
71 |
Krb5InitCredential.class : |
|
72 |
Krb5AcceptCredential.class)); |
|
73 |
||
74 |
Krb5CredElement result = ((creds == null || creds.isEmpty()) ? |
|
75 |
null : creds.firstElement()); |
|
76 |
||
77 |
// Force permission check before returning the cred to caller |
|
78 |
if (result != null) { |
|
79 |
if (initiate) { |
|
80 |
checkInitCredPermission((Krb5NameElement) result.getName()); |
|
81 |
} else { |
|
82 |
checkAcceptCredPermission |
|
83 |
((Krb5NameElement) result.getName(), name); |
|
84 |
} |
|
85 |
} |
|
86 |
return result; |
|
87 |
} |
|
88 |
||
2942
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
2
diff
changeset
|
89 |
public Krb5MechFactory(GSSCaller caller) { |
2 | 90 |
this.caller = caller; |
91 |
} |
|
92 |
||
93 |
public GSSNameSpi getNameElement(String nameStr, Oid nameType) |
|
94 |
throws GSSException { |
|
95 |
return Krb5NameElement.getInstance(nameStr, nameType); |
|
96 |
} |
|
97 |
||
98 |
public GSSNameSpi getNameElement(byte[] name, Oid nameType) |
|
99 |
throws GSSException { |
|
100 |
// At this point, even an exported name is stripped down to safe |
|
101 |
// bytes only |
|
102 |
// XXX Use encoding here |
|
103 |
return Krb5NameElement.getInstance(new String(name), nameType); |
|
104 |
} |
|
105 |
||
106 |
public GSSCredentialSpi getCredentialElement(GSSNameSpi name, |
|
107 |
int initLifetime, int acceptLifetime, |
|
108 |
int usage) throws GSSException { |
|
109 |
||
110 |
if (name != null && !(name instanceof Krb5NameElement)) { |
|
111 |
name = Krb5NameElement.getInstance(name.toString(), |
|
112 |
name.getStringNameType()); |
|
113 |
} |
|
114 |
||
115 |
Krb5CredElement credElement = getCredFromSubject |
|
116 |
(name, (usage != GSSCredential.ACCEPT_ONLY)); |
|
117 |
||
118 |
if (credElement == null) { |
|
119 |
if (usage == GSSCredential.INITIATE_ONLY || |
|
120 |
usage == GSSCredential.INITIATE_AND_ACCEPT) { |
|
121 |
credElement = Krb5InitCredential.getInstance |
|
122 |
(caller, (Krb5NameElement) name, initLifetime); |
|
123 |
checkInitCredPermission |
|
124 |
((Krb5NameElement) credElement.getName()); |
|
125 |
} else if (usage == GSSCredential.ACCEPT_ONLY) { |
|
126 |
credElement = |
|
127 |
Krb5AcceptCredential.getInstance(caller, |
|
128 |
(Krb5NameElement) name); |
|
129 |
checkAcceptCredPermission |
|
130 |
((Krb5NameElement) credElement.getName(), name); |
|
131 |
} else |
|
132 |
throw new GSSException(GSSException.FAILURE, -1, |
|
133 |
"Unknown usage mode requested"); |
|
134 |
} |
|
135 |
return credElement; |
|
136 |
} |
|
137 |
||
138 |
public static void checkInitCredPermission(Krb5NameElement name) { |
|
139 |
SecurityManager sm = System.getSecurityManager(); |
|
140 |
if (sm != null) { |
|
141 |
String realm = (name.getKrb5PrincipalName()).getRealmAsString(); |
|
142 |
String tgsPrincipal = |
|
143 |
new String("krbtgt/" + realm + '@' + realm); |
|
144 |
ServicePermission perm = |
|
145 |
new ServicePermission(tgsPrincipal, "initiate"); |
|
146 |
try { |
|
147 |
sm.checkPermission(perm); |
|
148 |
} catch (SecurityException e) { |
|
149 |
if (DEBUG) { |
|
150 |
System.out.println("Permission to initiate" + |
|
151 |
"kerberos init credential" + e.getMessage()); |
|
152 |
} |
|
153 |
throw e; |
|
154 |
} |
|
155 |
} |
|
156 |
} |
|
157 |
||
158 |
public static void checkAcceptCredPermission(Krb5NameElement name, |
|
159 |
GSSNameSpi originalName) { |
|
160 |
SecurityManager sm = System.getSecurityManager(); |
|
161 |
if (sm != null) { |
|
162 |
ServicePermission perm = new ServicePermission |
|
163 |
(name.getKrb5PrincipalName().getName(), "accept"); |
|
164 |
try { |
|
165 |
sm.checkPermission(perm); |
|
166 |
} catch (SecurityException e) { |
|
167 |
if (originalName == null) { |
|
168 |
// Don't disclose the name of the principal |
|
169 |
e = new SecurityException("No permission to acquire " |
|
170 |
+ "Kerberos accept credential"); |
|
171 |
// Don't call e.initCause() with caught exception |
|
172 |
} |
|
173 |
throw e; |
|
174 |
} |
|
175 |
} |
|
176 |
} |
|
177 |
||
178 |
public GSSContextSpi getMechanismContext(GSSNameSpi peer, |
|
179 |
GSSCredentialSpi myInitiatorCred, int lifetime) |
|
180 |
throws GSSException { |
|
181 |
if (peer != null && !(peer instanceof Krb5NameElement)) { |
|
182 |
peer = Krb5NameElement.getInstance(peer.toString(), |
|
183 |
peer.getStringNameType()); |
|
184 |
} |
|
185 |
// XXX Convert myInitiatorCred to Krb5CredElement |
|
186 |
if (myInitiatorCred == null) { |
|
187 |
myInitiatorCred = getCredentialElement(null, lifetime, 0, |
|
188 |
GSSCredential.INITIATE_ONLY); |
|
189 |
} |
|
190 |
return new Krb5Context(caller, (Krb5NameElement)peer, |
|
191 |
(Krb5CredElement)myInitiatorCred, lifetime); |
|
192 |
} |
|
193 |
||
194 |
public GSSContextSpi getMechanismContext(GSSCredentialSpi myAcceptorCred) |
|
195 |
throws GSSException { |
|
196 |
// XXX Convert myAcceptorCred to Krb5CredElement |
|
197 |
if (myAcceptorCred == null) { |
|
198 |
myAcceptorCred = getCredentialElement(null, 0, |
|
199 |
GSSCredential.INDEFINITE_LIFETIME, GSSCredential.ACCEPT_ONLY); |
|
200 |
} |
|
201 |
return new Krb5Context(caller, (Krb5CredElement)myAcceptorCred); |
|
202 |
} |
|
203 |
||
204 |
public GSSContextSpi getMechanismContext(byte[] exportedContext) |
|
205 |
throws GSSException { |
|
206 |
return new Krb5Context(caller, exportedContext); |
|
207 |
} |
|
208 |
||
209 |
||
210 |
public final Oid getMechanismOid() { |
|
211 |
return GSS_KRB5_MECH_OID; |
|
212 |
} |
|
213 |
||
214 |
public Provider getProvider() { |
|
215 |
return PROVIDER; |
|
216 |
} |
|
217 |
||
218 |
public Oid[] getNameTypes() { |
|
219 |
// nameTypes is cloned in GSSManager.getNamesForMech |
|
220 |
return nameTypes; |
|
221 |
} |
|
222 |
||
223 |
private static Oid createOid(String oidStr) { |
|
224 |
Oid retVal = null; |
|
225 |
try { |
|
226 |
retVal = new Oid(oidStr); |
|
227 |
} catch (GSSException e) { |
|
228 |
// Should not happen! |
|
229 |
} |
|
230 |
return retVal; |
|
231 |
} |
|
232 |
} |