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.util.ArrayList;
021: import java.util.HashMap;
022: import java.util.Map.Entry;
023:
024: import org.ietf.jgss.GSSCredential;
025: import org.ietf.jgss.GSSException;
026: import org.ietf.jgss.GSSName;
027: import org.ietf.jgss.Oid;
028:
029: public class GSSCredentialImpl implements GSSCredential {
030:
031: private GSSCredentialElement defaultCredentialElement;
032:
033: private HashMap<GSSCredentialType, GSSCredentialElement> credentials = new HashMap<GSSCredentialType, GSSCredentialElement>();
034:
035: private boolean disposed;
036:
037: private final GSSManagerImpl managerImpl;
038:
039: public GSSCredentialImpl(GSSManagerImpl managerImpl) {
040: this .managerImpl = managerImpl;
041: }
042:
043: public void add(GSSName name, int initLifetime, int acceptLifetime,
044: Oid mech, int usage) throws GSSException {
045: checkDisposed();
046:
047: if (mech == null) {
048: mech = managerImpl.getDefaultMech();
049: }
050:
051: GSSCredentialType credentialType = new GSSCredentialType(mech,
052: usage);
053: if (credentials.containsKey(credentialType)) {
054: throw new GSSException(GSSException.DUPLICATE_ELEMENT,
055: GSSUtils.DEFAULT_GSSEXCEPTION_MINOR_CODE, mech
056: + " " + usage);
057: }
058:
059: GSSCredentialElement credentialElement = managerImpl
060: .createCredentialElement(name, initLifetime,
061: acceptLifetime, mech, usage);
062: defaultCredentialElement = credentialElement;
063: credentials.put(credentialType, credentialElement);
064: }
065:
066: public void dispose() throws GSSException {
067: if (disposed) {
068: return;
069: }
070:
071: for (GSSCredentialElement credential : credentials.values()) {
072: credential.dispose();
073: }
074: disposed = true;
075: }
076:
077: public Oid[] getMechs() throws GSSException {
078: checkDisposed();
079: ArrayList<Oid> mechs = new ArrayList<Oid>();
080: for (GSSCredentialType credentialType : credentials.keySet()) {
081: Oid mech = credentialType.mech;
082: if (!mechs.contains(mech)) {
083: mechs.add(mech);
084: }
085: }
086: return mechs.toArray(new Oid[mechs.size()]);
087: }
088:
089: public GSSName getName() throws GSSException {
090: checkDisposed();
091: return defaultCredentialElement.getName();
092: }
093:
094: public GSSName getName(Oid mech) throws GSSException {
095: checkDisposed();
096: GSSCredentialElement credential = null;
097: for (Entry<GSSCredentialType, GSSCredentialElement> entry : credentials
098: .entrySet()) {
099: if (entry.getKey().mech.equals(mech)) {
100: credential = entry.getValue();
101: break;
102: }
103: }
104: if (null == credential) {
105: throw new GSSException(GSSException.BAD_MECH,
106: GSSUtils.DEFAULT_GSSEXCEPTION_MINOR_CODE,
107: "fail to get name for " + mech);
108: }
109: return credential.getName();
110: }
111:
112: public int getRemainingAcceptLifetime(Oid mech) throws GSSException {
113: checkDisposed();
114: GSSCredentialElement credential = null;
115: int remainingAcceptLifetime = Integer.MIN_VALUE;
116: credential = credentials.get(new GSSCredentialType(mech,
117: GSSCredential.INITIATE_ONLY));
118: if (credential != null) {
119: remainingAcceptLifetime = credential
120: .getRemainingAcceptLifetime();
121: }
122: GSSCredentialElement tempCredential = credentials
123: .get(new GSSCredentialType(mech,
124: GSSCredential.INITIATE_AND_ACCEPT));
125: if (tempCredential != null) {
126: credential = tempCredential;
127: remainingAcceptLifetime = Math.max(remainingAcceptLifetime,
128: credential.getRemainingAcceptLifetime());
129: }
130:
131: if (credential == null) {
132: throw new GSSException(GSSException.BAD_MECH,
133: GSSUtils.DEFAULT_GSSEXCEPTION_MINOR_CODE,
134: "no credential for mech " + mech);
135: }
136: return remainingAcceptLifetime;
137: }
138:
139: public int getRemainingInitLifetime(Oid mech) throws GSSException {
140: checkDisposed();
141: GSSCredentialElement credential = null;
142: int remainingInitLifetime = Integer.MIN_VALUE;
143: credential = credentials.get(new GSSCredentialType(mech,
144: GSSCredential.INITIATE_ONLY));
145: if (credential != null) {
146: remainingInitLifetime = credential
147: .getRemainingInitLifetime();
148: }
149: GSSCredentialElement tempCredential = credentials
150: .get(new GSSCredentialType(mech,
151: GSSCredential.INITIATE_AND_ACCEPT));
152: if (tempCredential != null) {
153: credential = tempCredential;
154: remainingInitLifetime = Math.max(remainingInitLifetime,
155: credential.getRemainingInitLifetime());
156: }
157:
158: if (credential == null) {
159: throw new GSSException(GSSException.BAD_MECH,
160: GSSUtils.DEFAULT_GSSEXCEPTION_MINOR_CODE,
161: "no credential for mech " + mech);
162: }
163: return remainingInitLifetime;
164: }
165:
166: public int getRemainingLifetime() throws GSSException {
167: checkDisposed();
168: int remainingLifeTime = GSSCredential.INDEFINITE_LIFETIME;
169: for (Entry<GSSCredentialType, GSSCredentialElement> credential : credentials
170: .entrySet()) {
171: GSSCredentialType credentialType = credential.getKey();
172: GSSCredentialElement credentialElement = credential
173: .getValue();
174: int credentialRemainingLifeTime;
175: switch (credentialType.usage) {
176: case GSSCredential.INITIATE_ONLY:
177: credentialRemainingLifeTime = credentialElement
178: .getRemainingInitLifetime();
179: break;
180: case GSSCredential.ACCEPT_ONLY:
181: credentialRemainingLifeTime = credentialElement
182: .getRemainingAcceptLifetime();
183: break;
184: default: // INITIATE_AND_ACCEPT
185: credentialRemainingLifeTime = Math.min(
186: credentialElement.getRemainingInitLifetime(),
187: credentialElement.getRemainingAcceptLifetime());
188: break;
189: }
190: remainingLifeTime = Math.min(remainingLifeTime,
191: credentialRemainingLifeTime);
192:
193: }
194: return remainingLifeTime;
195: }
196:
197: public int getUsage() throws GSSException {
198: checkDisposed();
199: boolean isInitiate = false;
200: boolean isAccept = false;
201: for (GSSCredentialType credentialType : credentials.keySet()) {
202: switch (credentialType.usage) {
203: case GSSCredential.INITIATE_ONLY:
204: isInitiate = true;
205: break;
206: case GSSCredential.ACCEPT_ONLY:
207: isAccept = true;
208: break;
209: case GSSCredential.INITIATE_AND_ACCEPT:
210: isInitiate = isAccept = true;
211: }
212: }
213:
214: if (isInitiate) {
215: if (isAccept) {
216: return GSSCredential.INITIATE_AND_ACCEPT;
217: }
218: return GSSCredential.INITIATE_ONLY;
219: }
220: if (isAccept) {
221: return GSSCredential.ACCEPT_ONLY;
222: }
223: throw new GSSException(GSSException.FAILURE,
224: GSSUtils.DEFAULT_GSSEXCEPTION_MINOR_CODE,
225: "no credential element in this credential");
226: }
227:
228: public int getUsage(Oid mech) throws GSSException {
229: checkDisposed();
230: boolean isInitiate = false;
231: boolean isAccept = false;
232: for (GSSCredentialType credentialType : credentials.keySet()) {
233: if (credentialType.mech.equals(mech)) {
234: switch (credentialType.usage) {
235: case GSSCredential.INITIATE_ONLY:
236: isInitiate = true;
237: break;
238: case GSSCredential.ACCEPT_ONLY:
239: isAccept = true;
240: break;
241: case GSSCredential.INITIATE_AND_ACCEPT:
242: isInitiate = isAccept = true;
243: }
244: }
245: }
246:
247: if (isInitiate) {
248: if (isAccept) {
249: return GSSCredential.INITIATE_AND_ACCEPT;
250: }
251: return GSSCredential.INITIATE_ONLY;
252: }
253: if (isAccept) {
254: return GSSCredential.ACCEPT_ONLY;
255: }
256: throw new GSSException(GSSException.BAD_MECH,
257: GSSUtils.DEFAULT_GSSEXCEPTION_MINOR_CODE,
258: "no credential for mech " + mech);
259: }
260:
261: private void checkDisposed() throws GSSException {
262: if (disposed) {
263: throw new GSSException(
264: GSSUtils.DEFAULT_GSSEXCEPTION_MAJOR_CODE,
265: GSSUtils.DEFAULT_GSSEXCEPTION_MINOR_CODE,
266: "credential disposed");
267: }
268: }
269:
270: private static class GSSCredentialType {
271: public final Oid mech;
272:
273: public final int usage;
274:
275: public GSSCredentialType(Oid mech, int usage) {
276: this .mech = mech;
277: this .usage = usage;
278: }
279:
280: @Override
281: public boolean equals(Object other) {
282: if (!(other instanceof GSSCredentialType)) {
283: return false;
284: }
285: GSSCredentialType otherType = (GSSCredentialType) other;
286: return mech.equals(otherType.mech)
287: && usage == otherType.usage;
288: }
289:
290: @Override
291: public int hashCode() {
292: return mech.hashCode() + usage;
293: }
294: }
295: }
|