001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.auth.jgss;
019:
020: import java.security.Provider;
021: import java.util.ArrayList;
022: import java.util.Hashtable;
023: import java.util.Set;
024: import java.util.Map.Entry;
025:
026: import org.apache.harmony.auth.jgss.kerberos.KerberosSpiImpl;
027: import org.apache.harmony.auth.jgss.kerberos.KerberosProvider;
028: import org.ietf.jgss.GSSContext;
029: import org.ietf.jgss.GSSCredential;
030: import org.ietf.jgss.GSSException;
031: import org.ietf.jgss.GSSManager;
032: import org.ietf.jgss.GSSName;
033: import org.ietf.jgss.Oid;
034:
035: public class GSSManagerImpl extends GSSManager {
036:
037: private static Oid DEFAULT_MECH;
038:
039: private static Provider DEFAULT_PROVIDER = new KerberosProvider(
040: "kerberos provider", 0, "");
041:
042: private static GSSMechSpi DEFAULT_API = new KerberosSpiImpl();
043:
044: static {
045: try {
046: DEFAULT_MECH = new Oid("1.2.840.113554.1.2.2");
047: } catch (GSSException e) {
048:
049: }
050: }
051:
052: public GSSManagerImpl() throws GSSException {
053: addProviderAtFront(DEFAULT_PROVIDER, null);
054: }
055:
056: private Hashtable<Oid, GSSMechSpi> spis = new Hashtable<Oid, GSSMechSpi>();
057:
058: private static final String JGSSAPI = "GssApiMechanism.";
059:
060: private void enumApisFromProvider(Provider p, Oid mech,
061: boolean override) {
062: for (Entry entry : p.entrySet()) {
063: String key = (String) entry.getKey();
064:
065: String value = (String) entry.getValue();
066:
067: if (!key.startsWith(JGSSAPI)) {
068: continue;
069: }
070:
071: String currentMechName = key.substring(JGSSAPI.length())
072: .trim();
073: Oid currentMech;
074: try {
075: currentMech = new Oid(currentMechName);
076: } catch (GSSException e) {
077: continue;
078: }
079:
080: if (mech != null && !mech.equals(currentMech)) {
081: continue;
082: }
083:
084: if (!override && spis.get(currentMech) != null) {
085: continue;
086: }
087:
088: GSSMechSpi gssApi;
089: try {
090: gssApi = (GSSMechSpi) Class.forName(value)
091: .newInstance();
092: } catch (Exception e) {
093: continue;
094: }
095: spis.put(currentMech, gssApi);
096: }
097: }
098:
099: @Override
100: public void addProviderAtEnd(Provider p, Oid mech)
101: throws GSSException {
102: enumApisFromProvider(p, mech, false);
103: }
104:
105: @Override
106: public void addProviderAtFront(Provider p, Oid mech)
107: throws GSSException {
108: enumApisFromProvider(p, mech, true);
109: }
110:
111: @Override
112: public GSSContext createContext(GSSName peer, Oid mech,
113: GSSCredential myCred, int lifetime) throws GSSException {
114:
115: return null;
116: }
117:
118: @Override
119: public GSSContext createContext(GSSCredential myCred)
120: throws GSSException {
121: // TODO Auto-generated method stub
122: return null;
123: }
124:
125: @Override
126: public GSSContext createContext(byte[] interProcessToken)
127: throws GSSException {
128: // TODO Auto-generated method stub
129: return null;
130: }
131:
132: @Override
133: public GSSCredential createCredential(int usage)
134: throws GSSException {
135: // TODO Auto-generated method stub
136: return null;
137: }
138:
139: @Override
140: public GSSCredential createCredential(GSSName name, int lifetime,
141: Oid mech, int usage) throws GSSException {
142: // TODO Auto-generated method stub
143: return null;
144: }
145:
146: @Override
147: public GSSCredential createCredential(GSSName name, int lifetime,
148: Oid[] mechs, int usage) throws GSSException {
149: // TODO Auto-generated method stub
150: return null;
151: }
152:
153: @Override
154: public GSSName createName(String nameStr, Oid nameType)
155: throws GSSException {
156: if (nameType != null && nameType.equals(GSSName.NT_EXPORT_NAME)) {
157: return GSSNameImpl.importFromString(GSSUtils
158: .getBytes(nameStr), this );
159: }
160: return DEFAULT_API.createName(nameStr, nameType);
161: }
162:
163: @Override
164: public GSSName createName(byte[] name, Oid nameType)
165: throws GSSException {
166: if (nameType != null && nameType.equals(GSSName.NT_EXPORT_NAME)) {
167: return GSSNameImpl.importFromString(name, this );
168: }
169: return DEFAULT_API
170: .createName(GSSUtils.toString(name), nameType);
171: }
172:
173: @Override
174: public GSSName createName(String nameStr, Oid nameType, Oid mech)
175: throws GSSException {
176: return createName(nameStr, nameType).canonicalize(mech);
177: }
178:
179: @Override
180: public GSSName createName(byte[] name, Oid nameType, Oid mech)
181: throws GSSException {
182: return createName(GSSUtils.toString(name), nameType, mech);
183: }
184:
185: @Override
186: public Oid[] getMechs() {
187: Set<Oid> oids = spis.keySet();
188: Oid[] mechs = new Oid[oids.size()];
189: int i = 0;
190: for (Oid oid : oids) {
191: mechs[i++] = oid;
192: }
193: return mechs;
194: }
195:
196: @Override
197: public Oid[] getMechsForName(Oid nameType) {
198: ArrayList<Oid> mechs = new ArrayList<Oid>();
199: Oid[] oids = getMechs();
200: for (Oid oid : oids) {
201: GSSMechSpi api = spis.get(oid);
202: Oid[] mechNames = api.getNameMechs();
203: boolean support = false;
204: for (Oid mechName : mechNames) {
205: if (mechName.equals(nameType)) {
206: support = true;
207: break;
208: }
209: }
210: if (support) {
211: mechs.add(oid);
212: }
213: }
214: return mechs.toArray(new Oid[mechs.size()]);
215: }
216:
217: @Override
218: public Oid[] getNamesForMech(Oid mech) throws GSSException {
219: GSSMechSpi api = getSpi(mech);
220: return api.getNameMechs();
221: }
222:
223: GSSMechSpi getSpi(Oid mech) {
224: return spis.get(mech);
225: }
226:
227: Oid getDefaultMech() {
228: return DEFAULT_MECH;
229: }
230:
231: GSSCredentialElement createCredentialElement(GSSName name,
232: int initLifetime, int acceptLifetime, Oid mech, int usage) {
233: // TODO Auto-generated method stub
234: return null;
235: }
236: }
|