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.ietf.jgss;
019:
020: import java.io.IOException;
021: import java.io.InputStream;
022:
023: import org.apache.harmony.auth.internal.nls.Messages;
024: import org.apache.harmony.security.asn1.ASN1Oid;
025: import org.apache.harmony.security.asn1.ObjectIdentifier;
026:
027: public class Oid {
028:
029: // ASN.1 decoder/encoder
030: private static final ASN1Oid ASN1 = ASN1Oid.getInstance();
031:
032: //inner representation of Object Identifier
033: private final ObjectIdentifier oid;
034:
035: //ASN.1 DER encoding
036: private byte[] encoding;
037:
038: Oid(int[] data) {
039: super ();
040: oid = new ObjectIdentifier(data);
041: }
042:
043: public Oid(byte[] data) throws GSSException {
044: super ();
045: try {
046: oid = new ObjectIdentifier((int[]) ASN1.decode(data));
047: } catch (IOException e) {
048: GSSException gsse = new GSSException(GSSException.FAILURE);
049: gsse.initCause(e);
050: throw gsse;
051: }
052: }
053:
054: public Oid(InputStream derOid) throws GSSException {
055: super ();
056: if (derOid == null) {
057: throw new NullPointerException();
058: }
059:
060: try {
061: oid = new ObjectIdentifier((int[]) ASN1.decode(derOid));
062: } catch (IOException e) {
063: GSSException gsse = new GSSException(GSSException.FAILURE);
064: gsse.initCause(e);
065: throw gsse;
066: }
067: }
068:
069: public Oid(String strOid) throws GSSException {
070: super ();
071: try {
072: oid = new ObjectIdentifier(strOid);
073: } catch (IllegalArgumentException e) {
074: GSSException gsse = new GSSException(GSSException.FAILURE);
075: gsse.initCause(e);
076: throw gsse;
077: }
078: }
079:
080: @Override
081: public boolean equals(Object other) {
082: if (this == other) {
083: return true;
084: }
085: if (other == null || !(other instanceof Oid)) {
086: return false;
087: }
088: return oid.equals(((Oid) other).oid);
089: }
090:
091: public boolean containedIn(Oid[] oids) {
092: if (oids == null) {
093: throw new NullPointerException(Messages
094: .getString("auth.0D")); //$NON-NLS-1$
095: }
096: for (Oid element : oids) {
097: if (oid.equals(element.oid)) {
098: return true;
099: }
100: }
101: return false;
102: }
103:
104: public byte[] getDER() throws GSSException {
105: if (encoding == null) {
106: encoding = ASN1.encode(oid.getOid());
107: }
108: byte[] enc = new byte[encoding.length];
109: System.arraycopy(encoding, 0, enc, 0, enc.length);
110: return enc;
111: }
112:
113: @Override
114: public String toString() {
115: return oid.toString();
116: }
117:
118: @Override
119: public int hashCode() {
120: return oid.hashCode();
121: }
122: }
|