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.internal.kerberos.v5;
019:
020: import java.io.IOException;
021: import java.math.BigInteger;
022: import java.util.Arrays;
023: import java.util.List;
024: import java.util.StringTokenizer;
025:
026: import org.apache.harmony.security.asn1.ASN1Explicit;
027: import org.apache.harmony.security.asn1.ASN1Integer;
028: import org.apache.harmony.security.asn1.ASN1Sequence;
029: import org.apache.harmony.security.asn1.ASN1SequenceOf;
030: import org.apache.harmony.security.asn1.ASN1StringType;
031: import org.apache.harmony.security.asn1.ASN1Type;
032: import org.apache.harmony.security.asn1.BerInputStream;
033:
034: /**
035: * Kerberos PrincipalName type.
036: *
037: * @see http://www.ietf.org/rfc/rfc4120.txt
038: */
039: public class PrincipalName {
040:
041: public static final int NT_UNKNOWN = 0;
042:
043: public static final int NT_PRINCIPAL = 1;
044:
045: public static final int NT_SRV_INST = 2;
046:
047: public static final int NT_SRV_HST = 3;
048:
049: public static final int NT_SRV_XHST = 4;
050:
051: public static final int NT_UID = 5;
052:
053: public static final int NT_X500_PRINCIPAL = 6;
054:
055: public static final int NT_SMTP_NAME = 7;
056:
057: public static final int NT_ENTERPRISE = 10;
058:
059: private final int type;
060:
061: private final String name[];
062:
063: public PrincipalName(int type, String[] name) {
064: this .type = type;
065: this .name = name;
066: }
067:
068: public PrincipalName(int type, String str) {
069: this .type = type;
070:
071: // FIXME: ignores escaped '/','@' chars
072: if (str.indexOf('/') == -1) {
073: //there is only one component in principal name
074: name = new String[] { str };
075: } else {
076: StringTokenizer strTknzr = new StringTokenizer(str, "/"); //$NON-NLS-1$
077: name = new String[strTknzr.countTokens()];
078: for (int i = 0; i < name.length; i++) {
079: name[i] = strTknzr.nextToken();
080: }
081: }
082: }
083:
084: public int getType() {
085: return type;
086: }
087:
088: public String[] getName() {
089: return name;
090: }
091:
092: public String getCanonicalName() {
093: StringBuilder buf = new StringBuilder();
094: for (int i = 0; i < (name.length - 1); i++) {
095: buf.append(name[i]);
096: buf.append('/');
097: }
098: // append last name element
099: buf.append(name[name.length - 1]);
100:
101: return buf.toString();
102: }
103:
104: public byte[] getEncoded() {
105: return ASN1.encode(this );
106: }
107:
108: @Override
109: public boolean equals(Object obj) {
110: if (this == obj) {
111: return true;
112: }
113:
114: if (!(obj instanceof PrincipalName)) {
115: return false;
116: }
117:
118: PrincipalName that = (PrincipalName) obj;
119:
120: return type == that.type && Arrays.equals(that.name, name);
121: }
122:
123: public static PrincipalName instanceOf(byte[] enc)
124: throws IOException {
125: return (PrincipalName) ASN1.decode(enc);
126: }
127:
128: @Override
129: public int hashCode() {
130: return type + Arrays.hashCode(name);
131: }
132:
133: @Override
134: public String toString() {
135: StringBuilder buf = new StringBuilder("Name: "); //$NON-NLS-1$
136:
137: for (int i = 0; i < (name.length - 1); i++) {
138: buf.append(name[i]);
139: buf.append('/');
140: }
141: buf.append(name[name.length - 1]);
142: buf.append(", type: "); //$NON-NLS-1$
143: buf.append(type);
144:
145: return buf.toString();
146: }
147:
148: // PrincipalName ::= SEQUENCE {
149: // name-type [0] Int32,
150: // name-string [1] SEQUENCE OF KerberosString
151: // }
152: static final ASN1Sequence ASN1 = new ASN1Sequence(new ASN1Type[] {
153: new ASN1Explicit(0, ASN1Integer.getInstance()),
154: new ASN1Explicit(1, new ASN1SequenceOf(
155: ASN1StringType.GENERALSTRING)), }) {
156:
157: @Override
158: protected Object getDecodedObject(BerInputStream in)
159: throws IOException {
160:
161: Object[] values = (Object[]) in.content;
162:
163: int type = ASN1Integer.toIntValue(values[0]);
164:
165: // TODO: list to array conversion should be done by framework
166: List<?> list = (List<?>) values[1];
167: String[] name = list.toArray(new String[list.size()]);
168: return new PrincipalName(type, name);
169: }
170:
171: @Override
172: protected void getValues(Object object, Object[] values) {
173:
174: PrincipalName name = (PrincipalName) object;
175:
176: values[0] = BigInteger.valueOf(name.getType())
177: .toByteArray();
178:
179: values[1] = Arrays.asList(name.getName());
180: }
181: };
182: }
|