1 7 8 package org.enhydra.oyster.der; 9 10 import org.enhydra.oyster.exception.SMIMEException; 11 import org.enhydra.oyster.exception.ErrorStorage; 12 13 30 public class DERObject { 31 32 35 private int identifierOctet; 36 37 40 private String lengthOctets = ""; 41 42 45 private String contentOctets = ""; 46 47 51 private int tagTypeNumber = 0; 52 53 57 private int tagClassType = 0; 58 59 63 private int tagComplexity = 0; 64 65 68 private int totalLength = 0; 69 70 75 public DERObject (int identifierOctet0) throws SMIMEException 76 { 77 if (identifierOctet0 < 0) 78 throw new SMIMEException(this, 1000); 79 else if (identifierOctet0 > 255) 80 throw new SMIMEException(this, 1001); 81 else if ((identifierOctet0 & 31) == 31) 82 throw new SMIMEException(this, 1002); 83 identifierOctet = identifierOctet0; 84 tagClassType = identifierOctet0 & 192; 85 tagComplexity = identifierOctet0 & 32; 86 tagTypeNumber = identifierOctet0 & 31; 87 totalLength = 1; } 89 90 98 public DERObject (int identifierOctet0, byte[] content0) throws SMIMEException 99 { 100 this(identifierOctet0); 101 this.addContent(content0); 102 } 103 104 111 private String lengthDERPart (int len0) throws SMIMEException{ 112 String returnString = null; 113 if (len0 < 128) { 114 byte[] lenByte = new byte[1]; 115 lenByte[0] = (byte)len0; 116 try { 117 returnString = new String (lenByte, "ISO-8859-1"); 118 } 119 catch(Exception e) { 120 throw SMIMEException.getInstance(this, e, "lengthDERPart" ); 121 } 122 return returnString; 123 } 124 else { 125 int i = 1, a = 1; for (; (a*2) <= len0; i++) 127 a = a*2; 128 i = (int)Math.ceil((double)i/8); byte[] lenByte = new byte[i + 1]; lenByte[0] = (byte)(i); lenByte[0] = (byte)((int)lenByte[0] | 128); for (; i > 0; i--) { 133 a = 255 << ((lenByte.length - i - 1)*8); 134 lenByte[i] = (byte)((len0 & a) >> ((lenByte.length - i - 1)*8)); 135 } 136 try { 137 returnString = new String (lenByte, "ISO-8859-1"); 138 } 139 catch(Exception e) { 140 throw SMIMEException.getInstance(this, e, "lengthDERPart" ); 141 } 142 return returnString; 143 } 144 } 145 146 154 void addContent (byte[] content0) throws SMIMEException { 155 if (identifierOctet == 5) 156 throw new SMIMEException(this, 1003); 157 try { 158 contentOctets = contentOctets.concat(new String (content0, "ISO-8859-1")); 159 lengthOctets = lengthDERPart(contentOctets.length()); 160 totalLength = 1 + contentOctets.length() + lengthOctets.length(); 161 } 162 catch(Exception e) { 163 throw SMIMEException.getInstance(this, e, "addContent" ); 164 } 165 } 166 167 173 public byte[] getDEREncoded () throws SMIMEException { 174 if (totalLength == 1 && identifierOctet != 5) 175 throw new SMIMEException(this, 1004); 176 byte[] returnByteArray = null; try { 178 byte[] temp = { 179 (byte)identifierOctet 180 }; String derOctet = new String (temp, "ISO-8859-1"); if (identifierOctet == 5) { 184 temp[0] = (byte)0x00; 185 contentOctets = contentOctets.concat(new String (temp, "ISO-8859-1")); 186 derOctet = derOctet.concat(contentOctets); 187 totalLength = 2; 188 returnByteArray = derOctet.getBytes("ISO-8859-1"); } 190 else { 191 derOctet = derOctet.concat(lengthOctets).concat(contentOctets); 192 totalLength = 1 + contentOctets.length() + lengthOctets.length(); 193 returnByteArray = derOctet.getBytes("ISO-8859-1"); 194 } 195 } 196 catch(Exception e) { 197 throw SMIMEException.getInstance(this, e, "getDEREncoded" ); 198 } 199 return returnByteArray; 200 } 201 202 206 public int getIdentifierOctet () { 207 return identifierOctet; 208 } 209 210 214 public int getTagTypeNumber () { 215 return tagTypeNumber; 216 } 217 218 222 public int getTagClassType () { 223 return tagClassType; 224 } 225 226 230 public int getTagComplexity () { 231 return tagComplexity; 232 } 233 234 238 public int getContentPartSize () { 239 return contentOctets.length(); 240 } 241 242 246 public int getLengthPartSize () { 247 return lengthOctets.length(); 248 } 249 250 256 public byte[] getContentOctets() throws SMIMEException { 257 byte[] returnByteArray = null; 258 try { 259 returnByteArray = contentOctets.getBytes("ISO-8859-1"); 260 } 261 catch(Exception e) { 262 throw SMIMEException.getInstance(this, e, "getContentOctets" ); 263 } 264 return returnByteArray; 265 } 266 267 273 public byte[] getLengthOctets() throws SMIMEException { 274 byte[] returnByteArray = null; 275 try { 276 returnByteArray = lengthOctets.getBytes("ISO-8859-1"); 277 } 278 catch(Exception e) { 279 throw SMIMEException.getInstance(this, e, "getLengthOctets" ); 280 } 281 return returnByteArray; 282 } 283 284 289 public int getTotalSize () { 290 if (tagTypeNumber == 5) 291 return totalLength + 1; 292 return totalLength; 293 } 294 } 295 296 297 298 | Popular Tags |