Return-Path: Delivered-To: apmail-harmony-commits-archive@www.apache.org Received: (qmail 74626 invoked from network); 12 Oct 2007 06:48:54 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 12 Oct 2007 06:48:54 -0000 Received: (qmail 86358 invoked by uid 500); 12 Oct 2007 06:48:41 -0000 Delivered-To: apmail-harmony-commits-archive@harmony.apache.org Received: (qmail 86340 invoked by uid 500); 12 Oct 2007 06:48:41 -0000 Mailing-List: contact commits-help@harmony.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@harmony.apache.org Delivered-To: mailing list commits@harmony.apache.org Received: (qmail 86331 invoked by uid 99); 12 Oct 2007 06:48:41 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 11 Oct 2007 23:48:41 -0700 X-ASF-Spam-Status: No, hits=-100.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 12 Oct 2007 06:48:52 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id EA3531A9832; Thu, 11 Oct 2007 23:48:01 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r584076 - in /harmony/enhanced/classlib/trunk/modules/auth/src: main/java/common/org/apache/harmony/auth/jgss/ main/java/common/org/apache/harmony/auth/jgss/kerberos/ test/java/common/org/apache/harmony/auth/tests/jgss/ test/java/common/org... Date: Fri, 12 Oct 2007 06:48:00 -0000 To: commits@harmony.apache.org From: leoli@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20071012064801.EA3531A9832@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: leoli Date: Thu Oct 11 23:47:58 2007 New Revision: 584076 URL: http://svn.apache.org/viewvc?rev=584076&view=rev Log: Apply patch for HARMONY-4721 ([classlib][auth]Harmony lacks default JGSS provider)(1) JGSS framework. Added: harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/jgss/ harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/jgss/GSSCredentialElement.java (with props) harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/jgss/GSSCredentialImpl.java (with props) harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/jgss/GSSManagerImpl.java (with props) harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/jgss/GSSMechSpi.java (with props) harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/jgss/GSSNameImpl.java (with props) harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/jgss/GSSUtils.java (with props) harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/jgss/kerberos/ harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/jgss/kerberos/KerberosName.java (with props) harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/jgss/kerberos/KerberosProvider.java (with props) harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/jgss/kerberos/KerberosSpiImpl.java (with props) harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/jgss/kerberos/KerberosUtils.java (with props) harmony/enhanced/classlib/trunk/modules/auth/src/test/java/common/org/apache/harmony/auth/tests/jgss/ harmony/enhanced/classlib/trunk/modules/auth/src/test/java/common/org/apache/harmony/auth/tests/jgss/GSSManagerImplTest.java (with props) harmony/enhanced/classlib/trunk/modules/auth/src/test/java/common/org/apache/harmony/auth/tests/jgss/GSSUtilsTest.java (with props) harmony/enhanced/classlib/trunk/modules/auth/src/test/java/common/org/apache/harmony/auth/tests/jgss/kerberos/ harmony/enhanced/classlib/trunk/modules/auth/src/test/java/common/org/apache/harmony/auth/tests/jgss/kerberos/KerberosNameTest.java (with props) Added: harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/jgss/GSSCredentialElement.java URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/jgss/GSSCredentialElement.java?rev=584076&view=auto ============================================================================== --- harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/jgss/GSSCredentialElement.java (added) +++ harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/jgss/GSSCredentialElement.java Thu Oct 11 23:47:58 2007 @@ -0,0 +1,38 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.harmony.auth.jgss; + +import org.ietf.jgss.GSSException; +import org.ietf.jgss.GSSName; +import org.ietf.jgss.Oid; + +public interface GSSCredentialElement { + + public GSSName getName(); + + public Oid getMech(); + + public int getUsage(); + + public int getRemainingAcceptLifetime(); + + public int getRemainingInitLifetime(); + + public void dispose() throws GSSException; + +} Propchange: harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/jgss/GSSCredentialElement.java ------------------------------------------------------------------------------ svn:eol-style = native Added: harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/jgss/GSSCredentialImpl.java URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/jgss/GSSCredentialImpl.java?rev=584076&view=auto ============================================================================== --- harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/jgss/GSSCredentialImpl.java (added) +++ harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/jgss/GSSCredentialImpl.java Thu Oct 11 23:47:58 2007 @@ -0,0 +1,289 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.harmony.auth.jgss; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map.Entry; + +import org.ietf.jgss.GSSCredential; +import org.ietf.jgss.GSSException; +import org.ietf.jgss.GSSName; +import org.ietf.jgss.Oid; + +public class GSSCredentialImpl implements GSSCredential { + + private GSSCredentialElement defaultCredentialElement; + + private HashMap credentials = new HashMap(); + + private boolean disposed; + + private final GSSManagerImpl managerImpl; + + public GSSCredentialImpl(GSSManagerImpl managerImpl) { + this.managerImpl = managerImpl; + } + + public void add(GSSName name, int initLifetime, int acceptLifetime, + Oid mech, int usage) throws GSSException { + checkDisposed(); + + if (mech == null) { + mech = managerImpl.getDefaultMech(); + } + + GSSCredentialType credentialType = new GSSCredentialType(mech, usage); + if (credentials.containsKey(credentialType)) { + throw new GSSException(GSSException.DUPLICATE_ELEMENT, + GSSUtils.DEFAULT_GSSEXCEPTION_MINOR_CODE, mech + " " + + usage); + } + + GSSCredentialElement credentialElement = managerImpl + .createCredentialElement(name, initLifetime, acceptLifetime, + mech, usage); + defaultCredentialElement = credentialElement; + credentials.put(credentialType, credentialElement); + } + + public void dispose() throws GSSException { + if (disposed) { + return; + } + + for (GSSCredentialElement credential : credentials.values()) { + credential.dispose(); + } + disposed = true; + } + + public Oid[] getMechs() throws GSSException { + checkDisposed(); + ArrayList mechs = new ArrayList(); + for (GSSCredentialType credentialType : credentials.keySet()) { + Oid mech = credentialType.mech; + if (!mechs.contains(mech)) { + mechs.add(mech); + } + } + return mechs.toArray(new Oid[mechs.size()]); + } + + public GSSName getName() throws GSSException { + checkDisposed(); + return defaultCredentialElement.getName(); + } + + public GSSName getName(Oid mech) throws GSSException { + checkDisposed(); + GSSCredentialElement credential = null; + for (Entry entry : credentials + .entrySet()) { + if (entry.getKey().mech.equals(mech)) { + credential = entry.getValue(); + break; + } + } + if (null == credential) { + throw new GSSException(GSSException.BAD_MECH, + GSSUtils.DEFAULT_GSSEXCEPTION_MINOR_CODE, + "fail to get name for " + mech); + } + return credential.getName(); + } + + public int getRemainingAcceptLifetime(Oid mech) throws GSSException { + checkDisposed(); + GSSCredentialElement credential = null; + int remainingAcceptLifetime = Integer.MIN_VALUE; + credential = credentials.get(new GSSCredentialType(mech, + GSSCredential.INITIATE_ONLY)); + if (credential != null) { + remainingAcceptLifetime = credential.getRemainingAcceptLifetime(); + } + GSSCredentialElement tempCredential = credentials + .get(new GSSCredentialType(mech, + GSSCredential.INITIATE_AND_ACCEPT)); + if (tempCredential != null) { + credential = tempCredential; + remainingAcceptLifetime = Math.max(remainingAcceptLifetime, + credential.getRemainingAcceptLifetime()); + } + + if (credential == null) { + throw new GSSException(GSSException.BAD_MECH, + GSSUtils.DEFAULT_GSSEXCEPTION_MINOR_CODE, + "no credential for mech " + mech); + } + return remainingAcceptLifetime; + } + + public int getRemainingInitLifetime(Oid mech) throws GSSException { + checkDisposed(); + GSSCredentialElement credential = null; + int remainingInitLifetime = Integer.MIN_VALUE; + credential = credentials.get(new GSSCredentialType(mech, + GSSCredential.INITIATE_ONLY)); + if (credential != null) { + remainingInitLifetime = credential.getRemainingInitLifetime(); + } + GSSCredentialElement tempCredential = credentials + .get(new GSSCredentialType(mech, + GSSCredential.INITIATE_AND_ACCEPT)); + if (tempCredential != null) { + credential = tempCredential; + remainingInitLifetime = Math.max(remainingInitLifetime, credential + .getRemainingInitLifetime()); + } + + if (credential == null) { + throw new GSSException(GSSException.BAD_MECH, + GSSUtils.DEFAULT_GSSEXCEPTION_MINOR_CODE, + "no credential for mech " + mech); + } + return remainingInitLifetime; + } + + public int getRemainingLifetime() throws GSSException { + checkDisposed(); + int remainingLifeTime = GSSCredential.INDEFINITE_LIFETIME; + for (Entry credential : credentials + .entrySet()) { + GSSCredentialType credentialType = credential.getKey(); + GSSCredentialElement credentialElement = credential.getValue(); + int credentialRemainingLifeTime; + switch (credentialType.usage) { + case GSSCredential.INITIATE_ONLY: + credentialRemainingLifeTime = credentialElement + .getRemainingInitLifetime(); + break; + case GSSCredential.ACCEPT_ONLY: + credentialRemainingLifeTime = credentialElement + .getRemainingAcceptLifetime(); + break; + default: // INITIATE_AND_ACCEPT + credentialRemainingLifeTime = Math.min(credentialElement + .getRemainingInitLifetime(), credentialElement + .getRemainingAcceptLifetime()); + break; + } + remainingLifeTime = Math.min(remainingLifeTime, + credentialRemainingLifeTime); + + } + return remainingLifeTime; + } + + public int getUsage() throws GSSException { + checkDisposed(); + boolean isInitiate = false; + boolean isAccept = false; + for (GSSCredentialType credentialType : credentials.keySet()) { + switch (credentialType.usage) { + case GSSCredential.INITIATE_ONLY: + isInitiate = true; + break; + case GSSCredential.ACCEPT_ONLY: + isAccept = true; + break; + case GSSCredential.INITIATE_AND_ACCEPT: + isInitiate = isAccept = true; + } + } + + if (isInitiate) { + if (isAccept) { + return GSSCredential.INITIATE_AND_ACCEPT; + } + return GSSCredential.INITIATE_ONLY; + } + if (isAccept) { + return GSSCredential.ACCEPT_ONLY; + } + throw new GSSException(GSSException.FAILURE, + GSSUtils.DEFAULT_GSSEXCEPTION_MINOR_CODE, + "no credential element in this credential"); + } + + public int getUsage(Oid mech) throws GSSException { + checkDisposed(); + boolean isInitiate = false; + boolean isAccept = false; + for (GSSCredentialType credentialType : credentials.keySet()) { + if (credentialType.mech.equals(mech)) { + switch (credentialType.usage) { + case GSSCredential.INITIATE_ONLY: + isInitiate = true; + break; + case GSSCredential.ACCEPT_ONLY: + isAccept = true; + break; + case GSSCredential.INITIATE_AND_ACCEPT: + isInitiate = isAccept = true; + } + } + } + + if (isInitiate) { + if (isAccept) { + return GSSCredential.INITIATE_AND_ACCEPT; + } + return GSSCredential.INITIATE_ONLY; + } + if (isAccept) { + return GSSCredential.ACCEPT_ONLY; + } + throw new GSSException(GSSException.BAD_MECH, + GSSUtils.DEFAULT_GSSEXCEPTION_MINOR_CODE, + "no credential for mech " + mech); + } + + private void checkDisposed() throws GSSException { + if (disposed) { + throw new GSSException(GSSUtils.DEFAULT_GSSEXCEPTION_MAJOR_CODE, + GSSUtils.DEFAULT_GSSEXCEPTION_MINOR_CODE, + "credential disposed"); + } + } + + private static class GSSCredentialType { + public final Oid mech; + + public final int usage; + + public GSSCredentialType(Oid mech, int usage) { + this.mech = mech; + this.usage = usage; + } + + @Override + public boolean equals(Object other) { + if (!(other instanceof GSSCredentialType)) { + return false; + } + GSSCredentialType otherType = (GSSCredentialType) other; + return mech.equals(otherType.mech) && usage == otherType.usage; + } + + @Override + public int hashCode() { + return mech.hashCode() + usage; + } + } +} Propchange: harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/jgss/GSSCredentialImpl.java ------------------------------------------------------------------------------ svn:eol-style = native Added: harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/jgss/GSSManagerImpl.java URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/jgss/GSSManagerImpl.java?rev=584076&view=auto ============================================================================== --- harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/jgss/GSSManagerImpl.java (added) +++ harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/jgss/GSSManagerImpl.java Thu Oct 11 23:47:58 2007 @@ -0,0 +1,226 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.harmony.auth.jgss; + +import java.security.Provider; +import java.util.ArrayList; +import java.util.Hashtable; +import java.util.Set; +import java.util.Map.Entry; + +import org.apache.harmony.auth.jgss.kerberos.KerberosSpiImpl; +import org.apache.harmony.auth.jgss.kerberos.KerberosProvider; +import org.ietf.jgss.GSSContext; +import org.ietf.jgss.GSSCredential; +import org.ietf.jgss.GSSException; +import org.ietf.jgss.GSSManager; +import org.ietf.jgss.GSSName; +import org.ietf.jgss.Oid; + +public class GSSManagerImpl extends GSSManager { + + private static Oid DEFAULT_MECH; + + private static Provider DEFAULT_PROVIDER = new KerberosProvider("kerberos provider", 0, ""); + + private static GSSMechSpi DEFAULT_API = new KerberosSpiImpl(); + + + static { + try { + DEFAULT_MECH = new Oid("1.2.840.113554.1.2.2"); + } catch (GSSException e) { + + } + } + + public GSSManagerImpl() throws GSSException{ + addProviderAtFront(DEFAULT_PROVIDER, null); + } + + private Hashtable spis = new Hashtable(); + + private static final String JGSSAPI = "GssApiMechanism."; + + private void enumApisFromProvider(Provider p, Oid mech, boolean override) { + for (Entry entry : p.entrySet()) { + String key = (String) entry.getKey(); + + String value = (String) entry.getValue(); + + if (!key.startsWith(JGSSAPI)) { + continue; + } + + String currentMechName = key.substring(JGSSAPI.length()).trim(); + Oid currentMech; + try { + currentMech = new Oid(currentMechName); + } catch (GSSException e) { + continue; + } + + if (mech != null && !mech.equals(currentMech)) { + continue; + } + + if (!override && spis.get(currentMech) != null) { + continue; + } + + GSSMechSpi gssApi; + try { + gssApi = (GSSMechSpi) Class.forName(value).newInstance(); + } catch (Exception e) { + continue; + } + spis.put(currentMech, gssApi); + } + } + + @Override + public void addProviderAtEnd(Provider p, Oid mech) throws GSSException { + enumApisFromProvider(p, mech, false); + } + + @Override + public void addProviderAtFront(Provider p, Oid mech) throws GSSException { + enumApisFromProvider(p, mech, true); + } + + @Override + public GSSContext createContext(GSSName peer, Oid mech, + GSSCredential myCred, int lifetime) throws GSSException { + + return null; + } + + @Override + public GSSContext createContext(GSSCredential myCred) throws GSSException { + // TODO Auto-generated method stub + return null; + } + + @Override + public GSSContext createContext(byte[] interProcessToken) + throws GSSException { + // TODO Auto-generated method stub + return null; + } + + @Override + public GSSCredential createCredential(int usage) throws GSSException { + // TODO Auto-generated method stub + return null; + } + + @Override + public GSSCredential createCredential(GSSName name, int lifetime, Oid mech, + int usage) throws GSSException { + // TODO Auto-generated method stub + return null; + } + + @Override + public GSSCredential createCredential(GSSName name, int lifetime, + Oid[] mechs, int usage) throws GSSException { + // TODO Auto-generated method stub + return null; + } + + @Override + public GSSName createName(String nameStr, Oid nameType) throws GSSException { + if(nameType != null && nameType.equals(GSSName.NT_EXPORT_NAME)){ + return GSSNameImpl.importFromString(GSSUtils.getBytes(nameStr), this); + } + return DEFAULT_API.createName(nameStr, nameType); + } + + @Override + public GSSName createName(byte[] name, Oid nameType) throws GSSException { + if (nameType != null && nameType.equals(GSSName.NT_EXPORT_NAME)) { + return GSSNameImpl.importFromString(name, this); + } + return DEFAULT_API.createName(GSSUtils.toString(name), nameType); + } + + @Override + public GSSName createName(String nameStr, Oid nameType, Oid mech) + throws GSSException { + return createName(nameStr, nameType).canonicalize(mech); + } + + @Override + public GSSName createName(byte[] name, Oid nameType, Oid mech) + throws GSSException { + return createName(GSSUtils.toString(name), nameType, mech); + } + + @Override + public Oid[] getMechs() { + Set oids = spis.keySet(); + Oid[] mechs = new Oid[oids.size()]; + int i = 0; + for (Oid oid : oids) { + mechs[i++] = oid; + } + return mechs; + } + + @Override + public Oid[] getMechsForName(Oid nameType) { + ArrayList mechs = new ArrayList(); + Oid[] oids = getMechs(); + for (Oid oid : oids) { + GSSMechSpi api = spis.get(oid); + Oid[] mechNames = api.getNameMechs(); + boolean support = false; + for (Oid mechName : mechNames) { + if (mechName.equals(nameType)) { + support = true; + break; + } + } + if (support) { + mechs.add(oid); + } + } + return mechs.toArray(new Oid[mechs.size()]); + } + + @Override + public Oid[] getNamesForMech(Oid mech) throws GSSException { + GSSMechSpi api = getSpi(mech); + return api.getNameMechs(); + } + + GSSMechSpi getSpi(Oid mech){ + return spis.get(mech); + } + + Oid getDefaultMech(){ + return DEFAULT_MECH; + } + + + + GSSCredentialElement createCredentialElement(GSSName name, int initLifetime, int acceptLifetime, Oid mech, int usage) { + // TODO Auto-generated method stub + return null; + } +} Propchange: harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/jgss/GSSManagerImpl.java ------------------------------------------------------------------------------ svn:eol-style = native Added: harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/jgss/GSSMechSpi.java URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/jgss/GSSMechSpi.java?rev=584076&view=auto ============================================================================== --- harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/jgss/GSSMechSpi.java (added) +++ harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/jgss/GSSMechSpi.java Thu Oct 11 23:47:58 2007 @@ -0,0 +1,32 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.harmony.auth.jgss; + +import org.ietf.jgss.GSSException; +import org.ietf.jgss.GSSName; +import org.ietf.jgss.Oid; + +public interface GSSMechSpi { + + public Oid[] getNameMechs(); + + public GSSName createName(String name, Oid nameType) throws GSSException; + + public GSSName createName(String name) throws GSSException; + +} Propchange: harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/jgss/GSSMechSpi.java ------------------------------------------------------------------------------ svn:eol-style = native Added: harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/jgss/GSSNameImpl.java URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/jgss/GSSNameImpl.java?rev=584076&view=auto ============================================================================== --- harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/jgss/GSSNameImpl.java (added) +++ harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/jgss/GSSNameImpl.java Thu Oct 11 23:47:58 2007 @@ -0,0 +1,107 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.harmony.auth.jgss; + +import org.apache.harmony.auth.jgss.GSSUtils; +import org.ietf.jgss.GSSException; +import org.ietf.jgss.GSSName; +import org.ietf.jgss.Oid; + +public abstract class GSSNameImpl implements GSSName { +private static final byte EXPORTED_TOKEN_FIRST_BYTE = 0x04; + + private static final byte EXPORTED_TOKEN_SECOND_BYTE = 0x01; + + private static final int EXPORTED_TOKEN_LENGTH = 2; + + private static final int OID_LENGTH_ENCODED_LENGTH = 2; + + private static final int NAME_LENGTH_ENCODED_LENGTH = 4; + + private static final int FIX_CONTENT_LENGTH = EXPORTED_TOKEN_LENGTH + OID_LENGTH_ENCODED_LENGTH + NAME_LENGTH_ENCODED_LENGTH; + + + + static GSSName importFromString(byte[] encodedGSSName, + GSSManagerImpl gssManagerImpl) throws GSSException { + byte[] encoded = encodedGSSName; + int index = 0; + + if (encoded[index++] != EXPORTED_TOKEN_FIRST_BYTE + || encoded[index++] != EXPORTED_TOKEN_SECOND_BYTE) { + throw new GSSException(GSSUtils.DEFAULT_GSSEXCEPTION_MAJOR_CODE, + GSSUtils.DEFAULT_GSSEXCEPTION_MINOR_CODE, + "Illegal token in importing string to GSSName"); + } + + int oidLength = GSSUtils.toInt(encoded, index, + OID_LENGTH_ENCODED_LENGTH); + index += OID_LENGTH_ENCODED_LENGTH; + + byte[] encodedMech = new byte[oidLength]; + System.arraycopy(encoded, index, encodedMech, 0, oidLength); + index += oidLength; + Oid mech = new Oid(encodedMech); + GSSMechSpi gssApi = gssManagerImpl.getSpi(mech); + + int nameLength = GSSUtils.toInt(encoded, index, + NAME_LENGTH_ENCODED_LENGTH); + index += NAME_LENGTH_ENCODED_LENGTH; + + byte[] encodedName = new byte[nameLength]; + System.arraycopy(encoded, index, encodedName, 0, nameLength); + String name = GSSUtils.toString(encodedName); + return gssApi.createName(name); + } + + public boolean equals(Object o){ + if( o instanceof GSSName){ + try { + return equals((GSSName) o); + } catch (GSSException e) { + } + } + return false; + } + + public byte[] export() throws GSSException { + byte[] name = exportMechDependent(); + byte[] oid = getMech().getDER(); + + byte[] encoded = new byte[FIX_CONTENT_LENGTH + oid.length + name.length]; + int index = 0; + encoded[index++] = EXPORTED_TOKEN_FIRST_BYTE; + encoded[index++] = EXPORTED_TOKEN_SECOND_BYTE; + + byte[] oid_length = GSSUtils.getBytes(oid.length, OID_LENGTH_ENCODED_LENGTH); + System.arraycopy(oid_length, 0, encoded, index, OID_LENGTH_ENCODED_LENGTH); + index += OID_LENGTH_ENCODED_LENGTH; + System.arraycopy(oid, 0, encoded, index, oid.length); + index += oid.length; + + + byte[] name_length = GSSUtils.getBytes(name.length, NAME_LENGTH_ENCODED_LENGTH); + System.arraycopy(name_length, 0, encoded, index, NAME_LENGTH_ENCODED_LENGTH); + index += NAME_LENGTH_ENCODED_LENGTH; + System.arraycopy(name, 0, encoded, index, name.length); + return encoded; + } + + protected abstract byte[] exportMechDependent() throws GSSException; + protected abstract Oid getMech(); +} Propchange: harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/jgss/GSSNameImpl.java ------------------------------------------------------------------------------ svn:eol-style = native Added: harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/jgss/GSSUtils.java URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/jgss/GSSUtils.java?rev=584076&view=auto ============================================================================== --- harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/jgss/GSSUtils.java (added) +++ harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/jgss/GSSUtils.java Thu Oct 11 23:47:58 2007 @@ -0,0 +1,85 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.harmony.auth.jgss; + +import java.io.UnsupportedEncodingException; + +import org.ietf.jgss.GSSException; + +public class GSSUtils { + + public static final String DEFAULT_CHARSET_NAME = "UTF-8"; + + public static final int DEFAULT_GSSEXCEPTION_MAJOR_CODE = 3; + + public static final int DEFAULT_GSSEXCEPTION_MINOR_CODE = 0; + + public static String toString(byte[] bytes) throws GSSException { + try { + return new String(bytes, DEFAULT_CHARSET_NAME); + } catch (UnsupportedEncodingException e) { + throw new GSSException(DEFAULT_GSSEXCEPTION_MAJOR_CODE, + DEFAULT_GSSEXCEPTION_MINOR_CODE, e.getMessage()); + } + } + + public static byte[] getBytes(String s) throws GSSException { + try { + return s.getBytes(DEFAULT_CHARSET_NAME); + } catch (UnsupportedEncodingException e) { + throw new GSSException(DEFAULT_GSSEXCEPTION_MAJOR_CODE, + DEFAULT_GSSEXCEPTION_MINOR_CODE, e.getMessage()); + } + } + + public static byte[] getBytes(int source, int length) { + if (source < 0) { + throw new Error( + "org.apache.harmony.auth.jgss.GSSUtils.getBytes(int i, int length) does not support negative integer"); + } + if (length <= 0 || length > 4) { + throw new Error( + "org.apache.harmony.auth.jgss.GSSUtils.getBytes(int i, int length) must have 0>> shift); + shift -=8; + } + return target; + } + + public static int toInt(byte[] source, int offset, int length) { + if (length == 0 || length > 4) { + throw new Error( + "org.apache.harmony.auth.jgss.GSSUtils.toInt(byte[] source) must have 0