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: * @author Alexander V. Esin, Stepan M. Mishura
019: * @version $Revision$
020: */package org.apache.harmony.security.utils;
021:
022: import java.util.Arrays;
023:
024: import org.apache.harmony.security.internal.nls.Messages;
025:
026: /**
027: * Instance of this class represents ObjectIdentifier (OID).
028: *
029: * OID is represented as a sequence of subidentifier.
030: * Each subidentifier is represented as non negative integer value.
031: * There are at least 2 subidentifiers in the sequence.
032: *
033: * Valid values for first subidentifier are 0, 1 and 2.
034: * If the first subidentifier has 0 or 1 value the second
035: * subidentifier MUST be less then 40.
036: *
037: * @see http://asn1.elibel.tm.fr/en/standards/index.htm
038: */
039:
040: public final class ObjectIdentifier {
041:
042: //OID as array of integers
043: private final int[] oid;
044:
045: //hash code
046: private int hash = -1;
047:
048: //OID as string
049: private String soid;
050:
051: // stores the following: "OID." + soid
052: private String sOID;
053:
054: // OID alias name
055: private String name;
056:
057: // OID's group
058: private Object group;
059:
060: /**
061: * Creates ObjectIdentifier(OID) from array of integers.
062: *
063: * @param oid - array of integers
064: * @return - OID object
065: * @throws NullPointerException - if oid is null
066: * @throws IllegalArgumentException - if oid is invalid
067: */
068: public ObjectIdentifier(int[] oid) {
069:
070: validateOid(oid);
071:
072: this .oid = oid;
073: }
074:
075: /**
076: * Creates ObjectIdentifier(OID) from array of integers.
077: *
078: * @param oid - array of integers
079: * @param name - name of OID
080: * @param oidGroup - OID's group. Is used to separate different OID's
081: * @return - OID object
082: * @throws NullPointerException - if oid is null
083: * @throws IllegalArgumentException - if oid is invalid
084: */
085: public ObjectIdentifier(int[] oid, String name, Object oidGroup) {
086: this (oid);
087:
088: if (oidGroup == null) {
089: throw new NullPointerException(Messages
090: .getString("security.172")); //$NON-NLS-1$
091: }
092: this .group = oidGroup;
093:
094: this .name = name;
095: toOIDString(); // init soid & sOID
096: }
097:
098: /**
099: * Gets OID.
100: *
101: * @return oid
102: */
103: public int[] getOid() {
104: return oid;
105: }
106:
107: /**
108: * Gets OID's name.
109: *
110: * @return name
111: */
112: public String getName() {
113: return name;
114: }
115:
116: /**
117: * Gets OID's group.
118: *
119: * @return group
120: */
121: public Object getGroup() {
122: return group;
123: }
124:
125: /**
126: * Compares object with OID for equality.
127: *
128: * @return true if object is ObjectIdentifier and it has the same
129: * representation as array of integers, otherwise false
130: */
131: public boolean equals(Object o) {
132: if (this == o) {
133: return true;
134: }
135: if (o == null || this .getClass() != o.getClass()) {
136: return false;
137: }
138: return Arrays.equals(oid, ((ObjectIdentifier) o).oid);
139: }
140:
141: /**
142: * Add "OID." to the beginning of string representation.
143: *
144: * @return oid as string
145: */
146: public String toOIDString() {
147: if (sOID == null) {
148: sOID = "OID." + toString(); //$NON-NLS-1$
149: }
150: return sOID;
151: }
152:
153: /**
154: * Overrides Object.toString()
155: *
156: * @return oid as string
157: */
158: public String toString() {
159: if (soid == null) {
160: StringBuffer sb = new StringBuffer(4 * oid.length);
161:
162: for (int i = 0; i < oid.length - 1; ++i) {
163: sb.append(oid[i]);
164: sb.append('.');
165: }
166: sb.append(oid[oid.length - 1]);
167: soid = sb.toString();
168: }
169: return soid;
170: }
171:
172: /**
173: * @see java.lang.Object#hashCode()
174: */
175: public int hashCode() {
176: if (hash == -1) {
177: hash = hashIntArray(oid);
178: }
179: return hash;
180: }
181:
182: /**
183: * Validates ObjectIdentifier (OID).
184: *
185: * @param oid - oid as array of integers
186: * @throws NullPointerException - if oid is null
187: * @throws IllegalArgumentException - if oid is invalid
188: */
189: public static void validateOid(int[] oid) {
190:
191: if (oid == null) {
192: throw new NullPointerException(Messages
193: .getString("security.98")); //$NON-NLS-1$
194: }
195:
196: if (oid.length < 2) {
197: throw new IllegalArgumentException(Messages
198: .getString("security.99")); //$NON-NLS-1$
199: }
200:
201: if (oid[0] > 2) {
202: throw new IllegalArgumentException(Messages
203: .getString("security.9A")); //$NON-NLS-1$
204: } else if (oid[0] != 2 && oid[1] > 39) {
205: throw new IllegalArgumentException(Messages
206: .getString("security.9B")); //$NON-NLS-1$
207: }
208: }
209:
210: /**
211: * Returns hash code for array of integers
212: *
213: * @param oid - array of integers
214: */
215: public static int hashIntArray(int[] array) {
216: int intHash = 0;
217: for (int i = 0; i < array.length && i < 4; i++) {
218: intHash += array[i] << (8 * i); //TODO what about to find better one?
219: }
220: return intHash & 0x7FFFFFFF; // only positive
221: }
222: }
|