1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
/* see bouncycastle_license.txt */
package com.lowagie.bc.asn1;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class DERObjectIdentifier
extends DERObject
{
String identifier;
/**
* return an OID from the passed in object
*
* @exception IllegalArgumentException if the object cannot be converted.
*/
public static DERObjectIdentifier getInstance(
Object obj)
{
if (obj == null || obj instanceof DERObjectIdentifier)
{
return (DERObjectIdentifier)obj;
}
if (obj instanceof ASN1OctetString)
{
return new DERObjectIdentifier(((ASN1OctetString)obj).getOctets());
}
if (obj instanceof ASN1TaggedObject)
{
return getInstance(((ASN1TaggedObject)obj).getObject());
}
throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
}
/**
* return an Object Identifier from a tagged object.
*
* @param obj the tagged object holding the object we want
* @param explicit true if the object is meant to be explicitly
* tagged false otherwise.
* @exception IllegalArgumentException if the tagged object cannot
* be converted.
*/
public static DERObjectIdentifier getInstance(
ASN1TaggedObject obj,
boolean explicit)
{
return getInstance(obj.getObject());
}
DERObjectIdentifier(
byte[] bytes)
{
StringBuffer objId = new StringBuffer();
int value = 0;
boolean first = true;
for (int i = 0; i != bytes.length; i++)
{
int b = bytes[i] & 0xff;
value = value * 128 + (b & 0x7f);
if ((b & 0x80) == 0) // end of number reached
{
if (first)
{
switch (value / 40)
{
case 0:
objId.append('0');
break;
case 1:
objId.append('1');
value -= 40;
break;
default:
objId.append('2');
value -= 80;
}
first = false;
}
objId.append('.');
objId.append(Integer.toString(value));
value = 0;
}
}
this.identifier = objId.toString();
}
public DERObjectIdentifier(
String identifier)
{
this.identifier = identifier;
}
public String getId()
{
return identifier;
}
private void writeField(
OutputStream out,
int fieldValue)
throws IOException
{
if (fieldValue >= (1 << 7))
{
if (fieldValue >= (1 << 14))
{
if (fieldValue >= (1 << 21))
{
if (fieldValue >= (1 << 28))
{
out.write((fieldValue >> 28) | 0x80);
}
out.write((fieldValue >> 21) | 0x80);
}
out.write((fieldValue >> 14) | 0x80);
}
out.write((fieldValue >> 7) | 0x80);
}
out.write(fieldValue & 0x7f);
}
void encode(
DEROutputStream out)
throws IOException
{
OIDTokenizer tok = new OIDTokenizer(identifier);
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
DEROutputStream dOut = new DEROutputStream(bOut);
writeField(bOut,
Integer.parseInt(tok.nextToken()) * 40
+ Integer.parseInt(tok.nextToken()));
while (tok.hasMoreTokens())
{
writeField(bOut, Integer.parseInt(tok.nextToken()));
}
dOut.close();
byte[] bytes = bOut.toByteArray();
out.writeEncoded(OBJECT_IDENTIFIER, bytes);
}
public int hashCode()
{
return identifier.hashCode();
}
public boolean equals(
Object o)
{
if ((o == null) || !(o instanceof DERObjectIdentifier))
{
return false;
}
return identifier.equals(((DERObjectIdentifier)o).identifier);
}
}
|