1 19 20 package com.maverick.crypto.asn1; 21 22 import java.io.ByteArrayOutputStream ; 23 import java.io.IOException ; 24 import java.util.Enumeration ; 25 import java.util.Vector ; 26 27 public class BERConstructedOctetString 28 extends DEROctetString 29 { 30 33 static private byte[] toBytes( 34 Vector octs) 35 { 36 ByteArrayOutputStream bOut = new ByteArrayOutputStream (); 37 38 for (int i = 0; i != octs.size(); i++) 39 { 40 DEROctetString o = (DEROctetString)octs.elementAt(i); 41 42 try 43 { 44 bOut.write(o.getOctets()); 45 } 46 catch (IOException e) 47 { 48 throw new RuntimeException ("exception converting octets " + e.toString()); 49 } 50 } 51 52 return bOut.toByteArray(); 53 } 54 55 private Vector octs; 56 57 60 public BERConstructedOctetString( 61 byte[] string) 62 { 63 super(string); 64 } 65 66 public BERConstructedOctetString( 67 Vector octs) 68 { 69 super(toBytes(octs)); 70 71 this.octs = octs; 72 } 73 74 public BERConstructedOctetString( 75 DERObject obj) 76 { 77 super(obj); 78 } 79 80 public BERConstructedOctetString( 81 DEREncodable obj) 82 { 83 super(obj.getDERObject()); 84 } 85 86 public byte[] getOctets() 87 { 88 return string; 89 } 90 91 94 public Enumeration getObjects() 95 { 96 if (octs == null) 97 { 98 return generateOcts().elements(); 99 } 100 101 return octs.elements(); 102 } 103 104 private Vector generateOcts() 105 { 106 int start = 0; 107 int end = 0; 108 Vector vec = new Vector (); 109 110 while ((end + 1) < string.length) 111 { 112 if (string[end] == 0 && string[end + 1] == 0) 113 { 114 byte[] nStr = new byte[end - start + 1]; 115 116 System.arraycopy(string, start, nStr, 0, nStr.length); 117 118 vec.addElement(new DEROctetString(nStr)); 119 start = end + 1; 120 } 121 end++; 122 } 123 124 byte[] nStr = new byte[string.length - start]; 125 126 System.arraycopy(string, start, nStr, 0, nStr.length); 127 128 vec.addElement(new DEROctetString(nStr)); 129 130 return vec; 131 } 132 133 public void encode( 134 DEROutputStream out) 135 throws IOException 136 { 137 if (out instanceof ASN1OutputStream || out instanceof BEROutputStream) 138 { 139 out.write(CONSTRUCTED | OCTET_STRING); 140 141 out.write(0x80); 142 143 if (octs != null) 147 { 148 for (int i = 0; i != octs.size(); i++) 149 { 150 out.writeObject(octs.elementAt(i)); 151 } 152 } 153 else 154 { 155 int start = 0; 156 int end = 0; 157 158 while ((end + 1) < string.length) 159 { 160 if (string[end] == 0 && string[end + 1] == 0) 161 { 162 byte[] nStr = new byte[end - start + 1]; 163 164 System.arraycopy(string, start, nStr, 0, nStr.length); 165 166 out.writeObject(new DEROctetString(nStr)); 167 start = end + 1; 168 } 169 end++; 170 } 171 172 byte[] nStr = new byte[string.length - start]; 173 174 System.arraycopy(string, start, nStr, 0, nStr.length); 175 176 out.writeObject(new DEROctetString(nStr)); 177 } 178 179 out.write(0x00); 180 out.write(0x00); 181 } 182 else 183 { 184 super.encode(out); 185 } 186 } 187 } 188 | Popular Tags |