DERUniversalString.java revision c37f4a04ef89e73a39a59f3c5a179af8c8ab5974
1b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampackage org.bouncycastle.asn1; 2b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 3b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport java.io.ByteArrayOutputStream; 4b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport java.io.IOException; 5b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 6b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam/** 7b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * DER UniversalString object. 8b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam */ 9b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampublic class DERUniversalString 10c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom extends ASN1Object 11b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam implements DERString 12b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam{ 13b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam private static final char[] table = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; 14b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam private byte[] string; 15b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 16b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam /** 17b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * return a Universal String from the passed in object. 18b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * 19b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * @exception IllegalArgumentException if the object cannot be converted. 20b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam */ 21b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam public static DERUniversalString getInstance( 22b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam Object obj) 23b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 24b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam if (obj == null || obj instanceof DERUniversalString) 25b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 26b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam return (DERUniversalString)obj; 27b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 28b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 29b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam if (obj instanceof ASN1OctetString) 30b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 31b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam return new DERUniversalString(((ASN1OctetString)obj).getOctets()); 32b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 33b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 34b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName()); 35b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 36b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 37b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam /** 38b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * return a Universal String from a tagged object. 39b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * 40b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * @param obj the tagged object holding the object we want 41b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * @param explicit true if the object is meant to be explicitly 42b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * tagged false otherwise. 43b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * @exception IllegalArgumentException if the tagged object cannot 44b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * be converted. 45b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam */ 46b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam public static DERUniversalString getInstance( 47b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam ASN1TaggedObject obj, 48b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam boolean explicit) 49b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 50b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam return getInstance(obj.getObject()); 51b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 52b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 53b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam /** 54b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * basic constructor - byte encoded string. 55b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam */ 56b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam public DERUniversalString( 57b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam byte[] string) 58b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 59b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam this.string = string; 60b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 61b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 62b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam public String getString() 63b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 64b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam StringBuffer buf = new StringBuffer("#"); 65b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam ByteArrayOutputStream bOut = new ByteArrayOutputStream(); 66b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam ASN1OutputStream aOut = new ASN1OutputStream(bOut); 67b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 68b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam try 69b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 70b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam aOut.writeObject(this); 71b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 72b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam catch (IOException e) 73b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 74b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam throw new RuntimeException("internal error encoding BitString"); 75b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 76b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 77b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam byte[] string = bOut.toByteArray(); 78b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 79b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam for (int i = 0; i != string.length; i++) 80b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 81c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom buf.append(table[(string[i] >>> 4) & 0xf]); 82b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam buf.append(table[string[i] & 0xf]); 83b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 84b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 85b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam return buf.toString(); 86b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 87b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 88c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom public String toString() 89c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom { 90c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom return getString(); 91c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom } 92c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom 93b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam public byte[] getOctets() 94b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 95b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam return string; 96b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 97b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 98b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam void encode( 99b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam DEROutputStream out) 100b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam throws IOException 101b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 102b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam out.writeEncoded(UNIVERSAL_STRING, this.getOctets()); 103b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 104b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 105c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom boolean asn1Equals( 106c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom DERObject o) 107b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 108c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom if (!(o instanceof DERUniversalString)) 109b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 110b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam return false; 111b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 112b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 113b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam return this.getString().equals(((DERUniversalString)o).getString()); 114b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 115b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 116b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam public int hashCode() 117b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 118b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam return this.getString().hashCode(); 119b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 120b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam} 121