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 org.apache.harmony.auth.jgss.GSSUtils;
021: import org.ietf.jgss.GSSException;
022: import org.ietf.jgss.GSSName;
023: import org.ietf.jgss.Oid;
024:
025: public abstract class GSSNameImpl implements GSSName {
026: private static final byte EXPORTED_TOKEN_FIRST_BYTE = 0x04;
027:
028: private static final byte EXPORTED_TOKEN_SECOND_BYTE = 0x01;
029:
030: private static final int EXPORTED_TOKEN_LENGTH = 2;
031:
032: private static final int OID_LENGTH_ENCODED_LENGTH = 2;
033:
034: private static final int NAME_LENGTH_ENCODED_LENGTH = 4;
035:
036: private static final int FIX_CONTENT_LENGTH = EXPORTED_TOKEN_LENGTH
037: + OID_LENGTH_ENCODED_LENGTH + NAME_LENGTH_ENCODED_LENGTH;
038:
039: static GSSName importFromString(byte[] encodedGSSName,
040: GSSManagerImpl gssManagerImpl) throws GSSException {
041: byte[] encoded = encodedGSSName;
042: int index = 0;
043:
044: if (encoded[index++] != EXPORTED_TOKEN_FIRST_BYTE
045: || encoded[index++] != EXPORTED_TOKEN_SECOND_BYTE) {
046: throw new GSSException(
047: GSSUtils.DEFAULT_GSSEXCEPTION_MAJOR_CODE,
048: GSSUtils.DEFAULT_GSSEXCEPTION_MINOR_CODE,
049: "Illegal token in importing string to GSSName");
050: }
051:
052: int oidLength = GSSUtils.toInt(encoded, index,
053: OID_LENGTH_ENCODED_LENGTH);
054: index += OID_LENGTH_ENCODED_LENGTH;
055:
056: byte[] encodedMech = new byte[oidLength];
057: System.arraycopy(encoded, index, encodedMech, 0, oidLength);
058: index += oidLength;
059: Oid mech = new Oid(encodedMech);
060: GSSMechSpi gssApi = gssManagerImpl.getSpi(mech);
061:
062: int nameLength = GSSUtils.toInt(encoded, index,
063: NAME_LENGTH_ENCODED_LENGTH);
064: index += NAME_LENGTH_ENCODED_LENGTH;
065:
066: byte[] encodedName = new byte[nameLength];
067: System.arraycopy(encoded, index, encodedName, 0, nameLength);
068: String name = GSSUtils.toString(encodedName);
069: return gssApi.createName(name);
070: }
071:
072: public boolean equals(Object o) {
073: if (o instanceof GSSName) {
074: try {
075: return equals((GSSName) o);
076: } catch (GSSException e) {
077: }
078: }
079: return false;
080: }
081:
082: public byte[] export() throws GSSException {
083: byte[] name = exportMechDependent();
084: byte[] oid = getMech().getDER();
085:
086: byte[] encoded = new byte[FIX_CONTENT_LENGTH + oid.length
087: + name.length];
088: int index = 0;
089: encoded[index++] = EXPORTED_TOKEN_FIRST_BYTE;
090: encoded[index++] = EXPORTED_TOKEN_SECOND_BYTE;
091:
092: byte[] oid_length = GSSUtils.getBytes(oid.length,
093: OID_LENGTH_ENCODED_LENGTH);
094: System.arraycopy(oid_length, 0, encoded, index,
095: OID_LENGTH_ENCODED_LENGTH);
096: index += OID_LENGTH_ENCODED_LENGTH;
097: System.arraycopy(oid, 0, encoded, index, oid.length);
098: index += oid.length;
099:
100: byte[] name_length = GSSUtils.getBytes(name.length,
101: NAME_LENGTH_ENCODED_LENGTH);
102: System.arraycopy(name_length, 0, encoded, index,
103: NAME_LENGTH_ENCODED_LENGTH);
104: index += NAME_LENGTH_ENCODED_LENGTH;
105: System.arraycopy(name, 0, encoded, index, name.length);
106: return encoded;
107: }
108:
109: protected abstract byte[] exportMechDependent() throws GSSException;
110:
111: protected abstract Oid getMech();
112: }
|