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: */package org.apache.geronimo.crypto.asn1;
017:
018: import java.io.ByteArrayOutputStream;
019: import java.io.IOException;
020: import java.io.OutputStream;
021:
022: public class DERObjectIdentifier extends DERObject {
023: String identifier;
024:
025: /**
026: * return an OID from the passed in object
027: *
028: * @exception IllegalArgumentException if the object cannot be converted.
029: */
030: public static DERObjectIdentifier getInstance(Object obj) {
031: if (obj == null || obj instanceof DERObjectIdentifier) {
032: return (DERObjectIdentifier) obj;
033: }
034:
035: if (obj instanceof ASN1OctetString) {
036: return new DERObjectIdentifier(((ASN1OctetString) obj)
037: .getOctets());
038: }
039:
040: if (obj instanceof ASN1TaggedObject) {
041: return getInstance(((ASN1TaggedObject) obj).getObject());
042: }
043:
044: throw new IllegalArgumentException(
045: "illegal object in getInstance: "
046: + obj.getClass().getName());
047: }
048:
049: /**
050: * return an Object Identifier from a tagged object.
051: *
052: * @param obj the tagged object holding the object we want
053: * @param explicit true if the object is meant to be explicitly
054: * tagged false otherwise.
055: * @exception IllegalArgumentException if the tagged object cannot
056: * be converted.
057: */
058: public static DERObjectIdentifier getInstance(ASN1TaggedObject obj,
059: boolean explicit) {
060: return getInstance(obj.getObject());
061: }
062:
063: DERObjectIdentifier(byte[] bytes) {
064: StringBuffer objId = new StringBuffer();
065: long value = 0;
066: boolean first = true;
067:
068: for (int i = 0; i != bytes.length; i++) {
069: int b = bytes[i] & 0xff;
070:
071: value = value * 128 + (b & 0x7f);
072: if ((b & 0x80) == 0) // end of number reached
073: {
074: if (first) {
075: switch ((int) value / 40) {
076: case 0:
077: objId.append('0');
078: break;
079: case 1:
080: objId.append('1');
081: value -= 40;
082: break;
083: default:
084: objId.append('2');
085: value -= 80;
086: }
087: first = false;
088: }
089:
090: objId.append('.');
091: objId.append(Long.toString(value));
092: value = 0;
093: }
094: }
095:
096: this .identifier = objId.toString();
097: }
098:
099: public DERObjectIdentifier(String identifier) {
100: for (int i = identifier.length() - 1; i >= 0; i--) {
101: char ch = identifier.charAt(i);
102:
103: if ('0' <= ch && ch <= '9') {
104: continue;
105: }
106:
107: if (ch == '.') {
108: continue;
109: }
110:
111: throw new IllegalArgumentException("string " + identifier
112: + " not an OID");
113: }
114:
115: this .identifier = identifier;
116: }
117:
118: public String getId() {
119: return identifier;
120: }
121:
122: private void writeField(OutputStream out, long fieldValue)
123: throws IOException {
124: if (fieldValue >= (1 << 7)) {
125: if (fieldValue >= (1 << 14)) {
126: if (fieldValue >= (1 << 21)) {
127: if (fieldValue >= (1 << 28)) {
128: if (fieldValue >= (1 << 35)) {
129: if (fieldValue >= (1 << 42)) {
130: if (fieldValue >= (1 << 49)) {
131: if (fieldValue >= (1 << 56)) {
132: out
133: .write((int) (fieldValue >> 56) | 0x80);
134: }
135: out
136: .write((int) (fieldValue >> 49) | 0x80);
137: }
138: out
139: .write((int) (fieldValue >> 42) | 0x80);
140: }
141: out.write((int) (fieldValue >> 35) | 0x80);
142: }
143: out.write((int) (fieldValue >> 28) | 0x80);
144: }
145: out.write((int) (fieldValue >> 21) | 0x80);
146: }
147: out.write((int) (fieldValue >> 14) | 0x80);
148: }
149: out.write((int) (fieldValue >> 7) | 0x80);
150: }
151: out.write((int) fieldValue & 0x7f);
152: }
153:
154: void encode(DEROutputStream out) throws IOException {
155: OIDTokenizer tok = new OIDTokenizer(identifier);
156: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
157: DEROutputStream dOut = new DEROutputStream(bOut);
158:
159: writeField(bOut, Integer.parseInt(tok.nextToken()) * 40
160: + Integer.parseInt(tok.nextToken()));
161:
162: while (tok.hasMoreTokens()) {
163: writeField(bOut, Long.parseLong(tok.nextToken()));
164: }
165:
166: dOut.close();
167:
168: byte[] bytes = bOut.toByteArray();
169:
170: out.writeEncoded(OBJECT_IDENTIFIER, bytes);
171: }
172:
173: public int hashCode() {
174: return identifier.hashCode();
175: }
176:
177: public boolean equals(Object o) {
178: if ((o == null) || !(o instanceof DERObjectIdentifier)) {
179: return false;
180: }
181:
182: return identifier.equals(((DERObjectIdentifier) o).identifier);
183: }
184:
185: public String toString() {
186: return getId();
187: }
188: }
|