1b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampackage org.bouncycastle.asn1; 2b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 3b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport java.io.ByteArrayOutputStream; 4b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport java.io.IOException; 5b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport java.util.Enumeration; 6b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport java.util.Vector; 7b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 84c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom/** 94c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom * @deprecated use BEROctetString 104c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom */ 11b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampublic class BERConstructedOctetString 124c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom extends BEROctetString 13b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam{ 14c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom private static final int MAX_LENGTH = 1000; 15c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom 16b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam /** 17b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * convert a vector of octet strings into a single byte string 18b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam */ 19b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam static private byte[] toBytes( 20b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam Vector octs) 21b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 22b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam ByteArrayOutputStream bOut = new ByteArrayOutputStream(); 23b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 24b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam for (int i = 0; i != octs.size(); i++) 25b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 26b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam try 27b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 28b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam DEROctetString o = (DEROctetString)octs.elementAt(i); 29b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 30b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam bOut.write(o.getOctets()); 31b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 32b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam catch (ClassCastException e) 33b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 34b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam throw new IllegalArgumentException(octs.elementAt(i).getClass().getName() + " found in input should only contain DEROctetString"); 35b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 36b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam catch (IOException e) 37b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 38b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam throw new IllegalArgumentException("exception converting octets " + e.toString()); 39b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 40b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 41b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 42b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam return bOut.toByteArray(); 43b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 44b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 45b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam private Vector octs; 46b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 47b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam /** 48b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * @param string the octets making up the octet string. 49b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam */ 50b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam public BERConstructedOctetString( 51b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam byte[] string) 52b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 53b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam super(string); 54b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 55b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 56b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam public BERConstructedOctetString( 57b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam Vector octs) 58b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 59b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam super(toBytes(octs)); 60b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 61b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam this.octs = octs; 62b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 63b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 64b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam public BERConstructedOctetString( 654c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom ASN1Primitive obj) 66b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 674c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom super(toByteArray(obj)); 684c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom } 694c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom 704c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom private static byte[] toByteArray(ASN1Primitive obj) 714c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom { 724c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom try 734c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom { 744c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom return obj.getEncoded(); 754c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom } 764c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom catch (IOException e) 774c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom { 784c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom throw new IllegalArgumentException("Unable to encode object"); 794c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom } 80b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 81b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 82b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam public BERConstructedOctetString( 834c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom ASN1Encodable obj) 84b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 854c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom this(obj.toASN1Primitive()); 86b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 87b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 88b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam public byte[] getOctets() 89b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 90b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam return string; 91b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 92b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 93b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam /** 94b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * return the DER octets that make up this string. 95b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam */ 96b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam public Enumeration getObjects() 97b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 98b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam if (octs == null) 99b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 100b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam return generateOcts().elements(); 101b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 102b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 103b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam return octs.elements(); 104b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 105b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 106c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom private Vector generateOcts() 107c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom { 108c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom Vector vec = new Vector(); 109c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom for (int i = 0; i < string.length; i += MAX_LENGTH) 110c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom { 111c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom int end; 112c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom 113c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom if (i + MAX_LENGTH > string.length) 114c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom { 115c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom end = string.length; 116c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom } 117c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom else 118c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom { 119c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom end = i + MAX_LENGTH; 120c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom } 121c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom 122c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom byte[] nStr = new byte[end - i]; 123c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom 124c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom System.arraycopy(string, i, nStr, 0, nStr.length); 125c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom 126c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom vec.addElement(new DEROctetString(nStr)); 127c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom } 128c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom 129c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom return vec; 130b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 131b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 1324c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom public static BEROctetString fromSequence(ASN1Sequence seq) 1336e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom { 1346e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom Vector v = new Vector(); 1356e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom Enumeration e = seq.getObjects(); 1366e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom 1376e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom while (e.hasMoreElements()) 1386e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom { 1396e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom v.addElement(e.nextElement()); 1406e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom } 1416e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom 1426e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom return new BERConstructedOctetString(v); 1436e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom } 144b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam} 145