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