Debian Codegraph @

gss/src/main/java/org/globus/gsi/gssapi/GlobusGSSManagerImpl.java

raw
1 /*
2 * Copyright 1999-2010 University of Chicago
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 * distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11 * express or implied.
12 *
13 * See the License for the specific language governing permissions and limitations under the License.
14 */
15 package org.globus.gsi.gssapi;
16
17 import org.globus.gsi.gssapi.jaas.JaasSubject;
18
19 import java.io.InputStream;
20 import java.io.FileInputStream;
21 import java.io.ByteArrayInputStream;
22 import java.io.IOException;
23 import java.security.Provider;
24 import java.util.Set;
25 import java.util.Iterator;
26
27 import javax.security.auth.Subject;
28
29 import org.ietf.jgss.GSSName;
30 import org.ietf.jgss.GSSCredential;
31 import org.ietf.jgss.GSSException;
32 import org.ietf.jgss.GSSContext;
33 import org.ietf.jgss.Oid;
34
35 import org.gridforum.jgss.ExtendedGSSManager;
36 import org.gridforum.jgss.ExtendedGSSCredential;
37
38 import org.globus.gsi.X509Credential;
39 import org.globus.gsi.CredentialException;
40
41 import org.apache.commons.logging.Log;
42 import org.apache.commons.logging.LogFactory;
43
44 /**
45 * An implementation of <code>GlobusGSSManager</code>.
46 */
47 public class GlobusGSSManagerImpl extends ExtendedGSSManager {
48
49 private static Log logger =
50 LogFactory.getLog(GlobusGSSManagerImpl.class.getName());
51
52 static final Oid[] MECHS;
53
54 static {
55 try {
56 MECHS = new Oid[] {GSSConstants.MECH_OID};
57 } catch(Exception e) {
58 throw new RuntimeException(e.getMessage());
59 }
60 }
61
62 private GlobusGSSCredentialImpl defaultCred;
63
64 /**
65 * Acquires GSI GSS credentials.
66 *
67 * @see #createCredential(GSSName, int, Oid, int)
68 */
69 public GSSCredential createCredential (int usage)
70 throws GSSException {
71 return createCredential(null,
72 GSSCredential.DEFAULT_LIFETIME,
73 (Oid)null,
74 usage);
75 }
76
77 /** Acquires GSI GSS credentials. First, it tries to find the credentials
78 * in the private credential set of the current JAAS Subject. If the
79 * Subject is not set or credentials are not found in the Subject, it
80 * tries to get a default user credential (usually an user proxy file)
81 *
82 * @param lifetime Only lifetime set to
83 * {@link GSSCredential#DEFAULT_LIFETIME
84 * GSSCredential.DEFAULT_LIFETIME} is allowed.
85 * @see org.globus.gsi.X509Credential#getDefaultCredential()
86 */
87 public GSSCredential createCredential (GSSName name,
88 int lifetime,
89 Oid mech,
90 int usage)
91 throws GSSException {
92 checkMechanism(mech);
93
94 if (name != null) {
95 if (name.isAnonymous()) {
96 return new GlobusGSSCredentialImpl();
97 } else {
98 throw new GSSException(GSSException.UNAVAILABLE);
99 }
100 }
101
102 X509Credential cred = null;
103
104 Subject subject = JaasSubject.getCurrentSubject();
105 if (subject != null) {
106 logger.debug("Getting credential from context");
107 Set gssCreds =
108 subject.getPrivateCredentials(GlobusGSSCredentialImpl.class);
109 if (gssCreds != null) {
110 Iterator iter = gssCreds.iterator();
111 if (iter.hasNext()) {
112 GlobusGSSCredentialImpl credImpl =
113 (GlobusGSSCredentialImpl)iter.next();
114 cred = credImpl.getX509Credential();
115 }
116 }
117 }
118
119 if (lifetime == GSSCredential.INDEFINITE_LIFETIME ||
120 lifetime > 0) {
121 // lifetime not supported
122 throw new GlobusGSSException(GSSException.FAILURE,
123 GlobusGSSException.BAD_ARGUMENT,
124 "badLifetime01");
125 }
126
127 if (cred == null) {
128 logger.debug("Getting default credential");
129 try {
130 cred = X509Credential.getDefaultCredential();
131 } catch(CredentialException e) {
132 throw new GlobusGSSException(GSSException.DEFECTIVE_CREDENTIAL,
133 e);
134 } catch(Exception e) {
135 throw new GlobusGSSException(GSSException.DEFECTIVE_CREDENTIAL,
136 e);
137 }
138
139 return getDefaultCredential(cred, usage);
140 } else {
141 return new GlobusGSSCredentialImpl(cred, usage);
142 }
143 }
144
145 private synchronized GSSCredential getDefaultCredential(
146 X509Credential cred,
147 int usage)
148 throws GSSException {
149 if (this.defaultCred != null &&
150 this.defaultCred.getUsage() == usage &&
151 this.defaultCred.getX509Credential() == cred) {
152 return this.defaultCred;
153 } else {
154 this.defaultCred = new GlobusGSSCredentialImpl(cred, usage);
155 return this.defaultCred;
156 }
157 }
158
159 /**
160 * Acquires GSI GSS credentials.
161 *
162 * @see #createCredential(GSSName, int, Oid, int)
163 */
164 public GSSCredential createCredential(GSSName name,
165 int lifetime,
166 Oid mechs[],
167 int usage)
168 throws GSSException {
169 if (mechs == null || mechs.length == 0) {
170 return createCredential(name, lifetime, (Oid)null, usage);
171 } else {
172 // XXX: not sure this is correct
173 GSSCredential cred =
174 createCredential(name, lifetime, mechs[0], usage);
175 for (int i = 1; i < mechs.length; i++) {
176 cred.add(name, lifetime, lifetime, mechs[i], usage);
177 }
178 return cred;
179 }
180 }
181
182 /**
183 * Imports a credential.
184 *
185 * @param lifetime Only lifetime set to
186 * {@link GSSCredential#DEFAULT_LIFETIME
187 * GSSCredential.DEFAULT_LIFETIME} is allowed.
188 */
189 public GSSCredential createCredential (byte[] buff,
190 int option,
191 int lifetime,
192 Oid mech,
193 int usage)
194 throws GSSException {
195 checkMechanism(mech);
196
197 if (buff == null || buff.length < 1) {
198 throw new GlobusGSSException(GSSException.FAILURE,
199 GlobusGSSException.BAD_ARGUMENT,
200 "invalidBuf");
201 }
202
203 if (lifetime == GSSCredential.INDEFINITE_LIFETIME ||
204 lifetime > 0) {
205 // lifetime not supported
206 throw new GlobusGSSException(GSSException.FAILURE,
207 GlobusGSSException.BAD_ARGUMENT,
208 "badLifetime01");
209 }
210
211 InputStream input = null;
212
213 switch (option) {
214 case ExtendedGSSCredential.IMPEXP_OPAQUE:
215 input = new ByteArrayInputStream(buff);
216 break;
217 case ExtendedGSSCredential.IMPEXP_MECH_SPECIFIC:
218 String s = new String(buff);
219 int pos = s.indexOf('=');
220 if (pos == -1) {
221 throw new GSSException(GSSException.FAILURE);
222 }
223 String filename = s.substring(pos+1).trim();
224 try {
225 input = new FileInputStream(filename);
226 } catch (IOException e) {
227 throw new GlobusGSSException(GSSException.FAILURE, e);
228 }
229 break;
230 default:
231 throw new GlobusGSSException(GSSException.FAILURE,
232 GlobusGSSException.BAD_ARGUMENT,
233 "unknownOption",
234 new Object[] {new Integer(option)});
235 }
236
237 X509Credential cred = null;
238 try {
239 cred = new X509Credential(input);
240 } catch(CredentialException e) {
241 throw new GlobusGSSException(GSSException.DEFECTIVE_CREDENTIAL, e);
242 } catch (Exception e) {
243 throw new GlobusGSSException(GSSException.DEFECTIVE_CREDENTIAL, e);
244 }
245
246 return new GlobusGSSCredentialImpl(cred, usage);
247 }
248
249 // for initiators
250 public GSSContext createContext(GSSName peer,
251 Oid mech,
252 GSSCredential cred,
253 int lifetime)
254 throws GSSException {
255 checkMechanism(mech);
256
257
258 GlobusGSSCredentialImpl globusCred = null;
259 if (cred == null) {
260 globusCred = (GlobusGSSCredentialImpl)createCredential(GSSCredential.INITIATE_ONLY);
261 } else if (cred instanceof GlobusGSSCredentialImpl) {
262 globusCred = (GlobusGSSCredentialImpl)cred;
263 } else {
264 throw new GSSException(GSSException.NO_CRED);
265 }
266
267 GSSContext ctx = new GlobusGSSContextImpl(peer, globusCred);
268 ctx.requestLifetime(lifetime);
269
270 return ctx;
271 }
272
273 // for acceptors
274 public GSSContext createContext(GSSCredential cred)
275 throws GSSException {
276
277 GlobusGSSCredentialImpl globusCred = null;
278 if (cred == null) {
279 globusCred = (GlobusGSSCredentialImpl)createCredential(GSSCredential.ACCEPT_ONLY);
280 } else if (cred instanceof GlobusGSSCredentialImpl) {
281 globusCred = (GlobusGSSCredentialImpl)cred;
282 } else {
283 throw new GSSException(GSSException.NO_CRED);
284 }
285 // XXX: don't know about the first argument
286 GSSContext ctx = new GlobusGSSContextImpl(null, globusCred);
287 return ctx;
288 }
289
290 public Oid[] getMechs() {
291 return MECHS;
292 }
293
294 public GSSName createName(String nameStr, Oid nameType)
295 throws GSSException {
296 return new GlobusGSSName(nameStr, nameType);
297 }
298
299 /**
300 * Checks if the specified mechanism matches
301 * the mechanism supported by this implementation.
302 *
303 * @param mech mechanism to check
304 * @exception GSSException if mechanism not supported.
305 */
306 public static void checkMechanism(Oid mech)
307 throws GSSException {
308 if (mech != null && !mech.equals(GSSConstants.MECH_OID)) {
309 throw new GSSException(GSSException.BAD_MECH);
310 }
311 }
312
313 // ==================================================================
314 // Not implemented below
315 // ==================================================================
316
317 /**
318 * Currently not implemented.
319 */
320 public GSSContext createContext(byte [] interProcessToken)
321 throws GSSException {
322 throw new GSSException(GSSException.UNAVAILABLE);
323 }
324
325 /**
326 * Currently not implemented.
327 */
328 public Oid[] getNamesForMech(Oid mech)
329 throws GSSException {
330 throw new GSSException(GSSException.UNAVAILABLE);
331 }
332
333 /**
334 * Currently not implemented.
335 */
336 public Oid[] getMechsForName(Oid nameType) {
337 // not implemented, not needed by Globus
338 return null;
339 }
340
341 /**
342 * Currently not implemented.
343 */
344 public GSSName createName(String nameStr,
345 Oid nameType,
346 Oid mech)
347 throws GSSException {
348 throw new GSSException(GSSException.UNAVAILABLE);
349 }
350
351 /**
352 * Currently not implemented.
353 */
354 public GSSName createName(byte name[], Oid nameType)
355 throws GSSException {
356 throw new GSSException(GSSException.UNAVAILABLE);
357 }
358
359 /**
360 * Currently not implemented.
361 */
362 public GSSName createName(byte name[], Oid nameType, Oid mech)
363 throws GSSException {
364 throw new GSSException(GSSException.UNAVAILABLE);
365 }
366
367 /**
368 * Currently not implemented.
369 */
370 public void addProviderAtFront(Provider p, Oid mech)
371 throws GSSException {
372 // this GSSManager implementation does not support an SPI
373 // with a pluggable provider architecture
374 throw new GSSException(GSSException.UNAVAILABLE);
375 }
376
377 /**
378 * Currently not implemented.
379 */
380 public void addProviderAtEnd(Provider p, Oid mech)
381 throws GSSException {
382 // this GSSManager implementation does not support an SPI
383 // with a pluggable provider architecture
384 throw new GSSException(GSSException.UNAVAILABLE);
385 }
386
387
388 }