001: package org.bouncycastle.sasn1;
002:
003: import java.io.ByteArrayInputStream;
004: import java.io.ByteArrayOutputStream;
005: import java.io.IOException;
006: import java.io.OutputStream;
007: import java.math.BigInteger;
008:
009: /**
010: * @deprecated use corresponsding classes in org.bouncycastle.asn1.
011: */
012: public class Asn1ObjectIdentifier extends DerObject {
013: private String _oid;
014:
015: Asn1ObjectIdentifier(int baseTag, byte[] data) throws IOException {
016: super (baseTag, BerTag.OBJECT_IDENTIFIER, data);
017:
018: StringBuffer objId = new StringBuffer();
019: long value = 0;
020: boolean first = true;
021: int b = 0;
022: BigInteger bigValue = null;
023: ByteArrayInputStream bIn = new ByteArrayInputStream(data);
024:
025: while ((b = bIn.read()) >= 0) {
026: if (value < 0x80000000000000L) {
027: value = value * 128 + (b & 0x7f);
028: if ((b & 0x80) == 0) // end of number reached
029: {
030: if (first) {
031: switch ((int) value / 40) {
032: case 0:
033: objId.append('0');
034: break;
035: case 1:
036: objId.append('1');
037: value -= 40;
038: break;
039: default:
040: objId.append('2');
041: value -= 80;
042: }
043: first = false;
044: }
045:
046: objId.append('.');
047: objId.append(value);
048: value = 0;
049: }
050: } else {
051: if (bigValue == null) {
052: bigValue = BigInteger.valueOf(value);
053: }
054: bigValue = bigValue.shiftLeft(7);
055: bigValue = bigValue.or(BigInteger.valueOf(b & 0x7f));
056: if ((b & 0x80) == 0) {
057: objId.append('.');
058: objId.append(bigValue);
059: bigValue = null;
060: value = 0;
061: }
062: }
063: }
064:
065: this ._oid = objId.toString();
066: }
067:
068: public Asn1ObjectIdentifier(String oid)
069: throws IllegalArgumentException {
070: super (BerTagClass.UNIVERSAL, BerTag.OBJECT_IDENTIFIER,
071: toByteArray(oid));
072:
073: this ._oid = oid;
074: }
075:
076: public String toString() {
077: return _oid;
078: }
079:
080: public int hashCode() {
081: return _oid.hashCode();
082: }
083:
084: public boolean equals(Object o) {
085: if (!(o instanceof Asn1ObjectIdentifier)) {
086: return false;
087: }
088:
089: return _oid.equals(((Asn1ObjectIdentifier) o)._oid);
090: }
091:
092: private static void writeField(OutputStream out, long fieldValue)
093: throws IOException {
094: if (fieldValue >= (1L << 7)) {
095: if (fieldValue >= (1L << 14)) {
096: if (fieldValue >= (1L << 21)) {
097: if (fieldValue >= (1L << 28)) {
098: if (fieldValue >= (1L << 35)) {
099: if (fieldValue >= (1L << 42)) {
100: if (fieldValue >= (1L << 49)) {
101: if (fieldValue >= (1L << 56)) {
102: out
103: .write((int) (fieldValue >> 56) | 0x80);
104: }
105: out
106: .write((int) (fieldValue >> 49) | 0x80);
107: }
108: out
109: .write((int) (fieldValue >> 42) | 0x80);
110: }
111: out.write((int) (fieldValue >> 35) | 0x80);
112: }
113: out.write((int) (fieldValue >> 28) | 0x80);
114: }
115: out.write((int) (fieldValue >> 21) | 0x80);
116: }
117: out.write((int) (fieldValue >> 14) | 0x80);
118: }
119: out.write((int) (fieldValue >> 7) | 0x80);
120: }
121: out.write((int) fieldValue & 0x7f);
122: }
123:
124: private static void writeField(OutputStream out,
125: BigInteger fieldValue) throws IOException {
126: int byteCount = (fieldValue.bitLength() + 6) / 7;
127: if (byteCount == 0) {
128: out.write(0);
129: } else {
130: BigInteger tmpValue = fieldValue;
131: byte[] tmp = new byte[byteCount];
132: for (int i = byteCount - 1; i >= 0; i--) {
133: tmp[i] = (byte) ((tmpValue.intValue() & 0x7f) | 0x80);
134: tmpValue = tmpValue.shiftRight(7);
135: }
136: tmp[byteCount - 1] &= 0x7f;
137: out.write(tmp);
138: }
139:
140: }
141:
142: private static byte[] toByteArray(String oid)
143: throws IllegalArgumentException {
144: OIDTokenizer tok = new OIDTokenizer(oid);
145: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
146:
147: try {
148: writeField(bOut, Integer.parseInt(tok.nextToken()) * 40
149: + Integer.parseInt(tok.nextToken()));
150:
151: while (tok.hasMoreTokens()) {
152: String token = tok.nextToken();
153: if (token.length() < 18) {
154: writeField(bOut, Long.parseLong(token));
155: } else {
156: writeField(bOut, new BigInteger(token));
157: }
158: }
159: } catch (NumberFormatException e) {
160: throw new IllegalArgumentException(
161: "exception parsing field value: " + e.getMessage());
162: } catch (IOException e) {
163: throw new IllegalArgumentException(
164: "exception converting to bytes: " + e.getMessage());
165: }
166:
167: return bOut.toByteArray();
168: }
169:
170: private static class OIDTokenizer {
171: private String oid;
172: private int index;
173:
174: public OIDTokenizer(String oid) {
175: this .oid = oid;
176: this .index = 0;
177: }
178:
179: public boolean hasMoreTokens() {
180: return (index != -1);
181: }
182:
183: public String nextToken() {
184: if (index == -1) {
185: return null;
186: }
187:
188: String token;
189: int end = oid.indexOf('.', index);
190:
191: if (end == -1) {
192: token = oid.substring(index);
193: index = -1;
194: return token;
195: }
196:
197: token = oid.substring(index, end);
198:
199: index = end + 1;
200: return token;
201: }
202: }
203: }
|