001: /*
002: * Copyright 2000-2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.security.jgss;
027:
028: import org.ietf.jgss.*;
029: import sun.security.jgss.spi.*;
030: import java.io.*;
031: import java.security.NoSuchProviderException;
032: import java.security.Provider;
033: import java.security.AccessController;
034: import java.security.PrivilegedAction;
035:
036: /**
037: * This class provides the default implementation of the GSSManager
038: * interface.
039: */
040: public class GSSManagerImpl extends GSSManager {
041:
042: // Undocumented property
043: private static final String USE_NATIVE_PROP = "sun.security.jgss.native";
044: private static final Boolean USE_NATIVE;
045:
046: static {
047: USE_NATIVE = AccessController
048: .doPrivileged(new PrivilegedAction<Boolean>() {
049: public Boolean run() {
050: String osname = System.getProperty("os.name");
051: if (osname.startsWith("SunOS")
052: || osname.startsWith("Linux")) {
053: return new Boolean(System
054: .getProperty(USE_NATIVE_PROP));
055: }
056: return Boolean.FALSE;
057: }
058: });
059:
060: }
061:
062: private ProviderList list;
063:
064: // Used by java SPNEGO impl to make sure native is disabled
065: public GSSManagerImpl(int caller, boolean useNative) {
066: list = new ProviderList(caller, useNative);
067: }
068:
069: // Used by HTTP/SPNEGO NegotiatorImpl
070: public GSSManagerImpl(int caller) {
071: list = new ProviderList(caller, USE_NATIVE);
072: }
073:
074: public GSSManagerImpl() {
075: list = new ProviderList(GSSUtil.CALLER_UNKNOWN, USE_NATIVE);
076: }
077:
078: public Oid[] getMechs() {
079: return list.getMechs();
080: }
081:
082: public Oid[] getNamesForMech(Oid mech) throws GSSException {
083: MechanismFactory factory = list.getMechFactory(mech);
084: return (Oid[]) factory.getNameTypes().clone();
085: }
086:
087: public Oid[] getMechsForName(Oid nameType) {
088: Oid[] mechs = list.getMechs();
089: Oid[] retVal = new Oid[mechs.length];
090: int pos = 0;
091:
092: // Iterate thru all mechs in GSS
093: for (int i = 0; i < mechs.length; i++) {
094: // what nametypes does this mech support?
095: Oid mech = mechs[i];
096: try {
097: Oid[] namesForMech = getNamesForMech(mech);
098: // Is the desired Oid present in that list?
099: if (nameType.containedIn(namesForMech)) {
100: retVal[pos++] = mech;
101: }
102: } catch (GSSException e) {
103: // Squelch it and just skip over this mechanism
104: GSSUtil.debug("Skip " + mech
105: + ": error retrieving supported name types");
106: }
107: }
108:
109: // Trim the list if needed
110: if (pos < retVal.length) {
111: Oid[] temp = new Oid[pos];
112: for (int i = 0; i < pos; i++)
113: temp[i] = retVal[i];
114: retVal = temp;
115: }
116:
117: return retVal;
118: }
119:
120: public GSSName createName(String nameStr, Oid nameType)
121: throws GSSException {
122: return new GSSNameImpl(this , nameStr, nameType);
123: }
124:
125: public GSSName createName(byte name[], Oid nameType)
126: throws GSSException {
127: return new GSSNameImpl(this , name, nameType);
128: }
129:
130: public GSSName createName(String nameStr, Oid nameType, Oid mech)
131: throws GSSException {
132: return new GSSNameImpl(this , nameStr, nameType, mech);
133: }
134:
135: public GSSName createName(byte name[], Oid nameType, Oid mech)
136: throws GSSException {
137: return new GSSNameImpl(this , name, nameType, mech);
138: }
139:
140: public GSSCredential createCredential(int usage)
141: throws GSSException {
142: return new GSSCredentialImpl(this , usage);
143: }
144:
145: public GSSCredential createCredential(GSSName aName, int lifetime,
146: Oid mech, int usage) throws GSSException {
147: return new GSSCredentialImpl(this , aName, lifetime, mech, usage);
148: }
149:
150: public GSSCredential createCredential(GSSName aName, int lifetime,
151: Oid mechs[], int usage) throws GSSException {
152: return new GSSCredentialImpl(this , aName, lifetime, mechs,
153: usage);
154: }
155:
156: public GSSContext createContext(GSSName peer, Oid mech,
157: GSSCredential myCred, int lifetime) throws GSSException {
158: return new GSSContextImpl(this , peer, mech, myCred, lifetime);
159: }
160:
161: public GSSContext createContext(GSSCredential myCred)
162: throws GSSException {
163: return new GSSContextImpl(this , myCred);
164: }
165:
166: public GSSContext createContext(byte[] interProcessToken)
167: throws GSSException {
168: return new GSSContextImpl(this , interProcessToken);
169: }
170:
171: public void addProviderAtFront(Provider p, Oid mech)
172: throws GSSException {
173: list.addProviderAtFront(p, mech);
174: }
175:
176: public void addProviderAtEnd(Provider p, Oid mech)
177: throws GSSException {
178: list.addProviderAtEnd(p, mech);
179: }
180:
181: public GSSCredentialSpi getCredentialElement(GSSNameSpi name,
182: int initLifetime, int acceptLifetime, Oid mech, int usage)
183: throws GSSException {
184: MechanismFactory factory = list.getMechFactory(mech);
185: return factory.getCredentialElement(name, initLifetime,
186: acceptLifetime, usage);
187: }
188:
189: // Used by java SPNEGO impl
190: public GSSNameSpi getNameElement(String name, Oid nameType, Oid mech)
191: throws GSSException {
192: // Just use the most preferred MF impl assuming GSSNameSpi
193: // objects are interoperable among providers
194: MechanismFactory factory = list.getMechFactory(mech);
195: return factory.getNameElement(name, nameType);
196: }
197:
198: // Used by java SPNEGO impl
199: public GSSNameSpi getNameElement(byte[] name, Oid nameType, Oid mech)
200: throws GSSException {
201: // Just use the most preferred MF impl assuming GSSNameSpi
202: // objects are interoperable among providers
203: MechanismFactory factory = list.getMechFactory(mech);
204: return factory.getNameElement(name, nameType);
205: }
206:
207: GSSContextSpi getMechanismContext(GSSNameSpi peer,
208: GSSCredentialSpi myInitiatorCred, int lifetime, Oid mech)
209: throws GSSException {
210: Provider p = null;
211: if (myInitiatorCred != null) {
212: p = myInitiatorCred.getProvider();
213: }
214: MechanismFactory factory = list.getMechFactory(mech, p);
215: return factory.getMechanismContext(peer, myInitiatorCred,
216: lifetime);
217: }
218:
219: GSSContextSpi getMechanismContext(GSSCredentialSpi myAcceptorCred,
220: Oid mech) throws GSSException {
221: Provider p = null;
222: if (myAcceptorCred != null) {
223: p = myAcceptorCred.getProvider();
224: }
225: MechanismFactory factory = list.getMechFactory(mech, p);
226: return factory.getMechanismContext(myAcceptorCred);
227: }
228:
229: GSSContextSpi getMechanismContext(byte[] exportedContext)
230: throws GSSException {
231: if ((exportedContext == null) || (exportedContext.length == 0)) {
232: throw new GSSException(GSSException.NO_CONTEXT);
233: }
234: GSSContextSpi result = null;
235:
236: // Only allow context import with native provider since JGSS
237: // still has not defined its own interprocess token format
238: Oid[] mechs = list.getMechs();
239: for (int i = 0; i < mechs.length; i++) {
240: MechanismFactory factory = list.getMechFactory(mechs[i]);
241: if (factory.getProvider().getName().equals("SunNativeGSS")) {
242: result = factory.getMechanismContext(exportedContext);
243: if (result != null)
244: break;
245: }
246: }
247: if (result == null) {
248: throw new GSSException(GSSException.UNAVAILABLE);
249: }
250: return result;
251: }
252: }
|