1 19 20 package com.maverick.crypto.asn1; 21 22 import java.io.ByteArrayOutputStream ; 23 import java.io.IOException ; 24 import java.io.OutputStream ; 25 26 public class DERObjectIdentifier 27 extends DERObject 28 { 29 String identifier; 30 31 36 public static DERObjectIdentifier getInstance( 37 Object obj) 38 { 39 if (obj == null || obj instanceof DERObjectIdentifier) 40 { 41 return (DERObjectIdentifier)obj; 42 } 43 44 if (obj instanceof ASN1OctetString) 45 { 46 return new DERObjectIdentifier(((ASN1OctetString)obj).getOctets()); 47 } 48 49 if (obj instanceof ASN1TaggedObject) 50 { 51 return getInstance(((ASN1TaggedObject)obj).getObject()); 52 } 53 54 throw new IllegalArgumentException ("illegal object in getInstance: " + obj.getClass().getName()); 55 } 56 57 66 public static DERObjectIdentifier getInstance( 67 ASN1TaggedObject obj, 68 boolean explicit) 69 { 70 return getInstance(obj.getObject()); 71 } 72 73 74 DERObjectIdentifier( 75 byte[] bytes) 76 { 77 StringBuffer objId = new StringBuffer (); 78 int value = 0; 79 boolean first = true; 80 81 for (int i = 0; i != bytes.length; i++) 82 { 83 int b = bytes[i] & 0xff; 84 85 value = value * 128 + (b & 0x7f); 86 if ((b & 0x80) == 0) { 88 if (first) 89 { 90 switch (value / 40) 91 { 92 case 0: 93 objId.append('0'); 94 break; 95 case 1: 96 objId.append('1'); 97 value -= 40; 98 break; 99 default: 100 objId.append('2'); 101 value -= 80; 102 } 103 first = false; 104 } 105 106 objId.append('.'); 107 objId.append(Integer.toString(value)); 108 value = 0; 109 } 110 } 111 112 this.identifier = objId.toString(); 113 } 114 115 public DERObjectIdentifier( 116 String identifier) 117 { 118 this.identifier = identifier; 119 } 120 121 public String getId() 122 { 123 return identifier; 124 } 125 126 private void writeField( 127 OutputStream out, 128 int fieldValue) 129 throws IOException 130 { 131 if (fieldValue >= (1 << 7)) 132 { 133 if (fieldValue >= (1 << 14)) 134 { 135 if (fieldValue >= (1 << 21)) 136 { 137 if (fieldValue >= (1 << 28)) 138 { 139 out.write((fieldValue >> 28) | 0x80); 140 } 141 out.write((fieldValue >> 21) | 0x80); 142 } 143 out.write((fieldValue >> 14) | 0x80); 144 } 145 out.write((fieldValue >> 7) | 0x80); 146 } 147 out.write(fieldValue & 0x7f); 148 } 149 150 void encode( 151 DEROutputStream out) 152 throws IOException 153 { 154 OIDTokenizer tok = new OIDTokenizer(identifier); 155 ByteArrayOutputStream bOut = new ByteArrayOutputStream (); 156 DEROutputStream dOut = new DEROutputStream(bOut); 157 158 writeField(bOut, 159 Integer.parseInt(tok.nextToken()) * 40 160 + Integer.parseInt(tok.nextToken())); 161 162 while (tok.hasMoreTokens()) 163 { 164 writeField(bOut, Integer.parseInt(tok.nextToken())); 165 } 166 167 dOut.close(); 168 169 byte[] bytes = bOut.toByteArray(); 170 171 out.writeEncoded(OBJECT_IDENTIFIER, bytes); 172 } 173 174 public int hashCode() 175 { 176 return identifier.hashCode(); 177 } 178 179 public boolean equals( 180 Object o) 181 { 182 if ((o == null) || !(o instanceof DERObjectIdentifier)) 183 { 184 return false; 185 } 186 187 return identifier.equals(((DERObjectIdentifier)o).identifier); 188 } 189 } 190 | Popular Tags |