1b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampackage org.bouncycastle.asn1; 2b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 3b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport java.io.IOException; 4b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 5b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam/** 6b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * DER IA5String object - this is an ascii string. 7b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam */ 8b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampublic class DERIA5String 9c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom extends ASN1Object 10b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam implements DERString 11b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam{ 12b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam String string; 13b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 14b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam /** 15b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * return a IA5 string from the passed in object 16b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * 17b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * @exception IllegalArgumentException if the object cannot be converted. 18b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam */ 19b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam public static DERIA5String getInstance( 20b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam Object obj) 21b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 22b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam if (obj == null || obj instanceof DERIA5String) 23b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 24b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam return (DERIA5String)obj; 25b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 26b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 27b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName()); 28b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 29b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 30b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam /** 31b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * return an IA5 String from a tagged object. 32b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * 33b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * @param obj the tagged object holding the object we want 34b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * @param explicit true if the object is meant to be explicitly 35b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * tagged false otherwise. 36b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * @exception IllegalArgumentException if the tagged object cannot 37b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * be converted. 38b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam */ 39b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam public static DERIA5String getInstance( 40b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam ASN1TaggedObject obj, 41b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam boolean explicit) 42b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 436e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom DERObject o = obj.getObject(); 446e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom 456e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom if (explicit || o instanceof DERIA5String) 466e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom { 476e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom return getInstance(o); 486e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom } 496e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom else 506e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom { 516e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom return new DERIA5String(((ASN1OctetString)o).getOctets()); 526e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom } 53b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 54b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 55b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam /** 56b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * basic constructor - with bytes. 57b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam */ 58b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam public DERIA5String( 59b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam byte[] string) 60b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 61b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam char[] cs = new char[string.length]; 62b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 63b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam for (int i = 0; i != cs.length; i++) 64b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 65b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam cs[i] = (char)(string[i] & 0xff); 66b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 67b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 68b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam this.string = new String(cs); 69b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 70b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 71b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam /** 72c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom * basic constructor - without validation. 73b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam */ 74b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam public DERIA5String( 75b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam String string) 76b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 77c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom this(string, false); 78c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom } 79c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom 80c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom /** 81c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom * Constructor with optional validation. 82c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom * 83c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom * @param string the base string to wrap. 84c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom * @param validate whether or not to check the string. 85c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom * @throws IllegalArgumentException if validate is true and the string 86c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom * contains characters that should not be in an IA5String. 87c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom */ 88c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom public DERIA5String( 89c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom String string, 90c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom boolean validate) 91c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom { 92c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom if (string == null) 93c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom { 94c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom throw new NullPointerException("string cannot be null"); 95c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom } 96c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom if (validate && !isIA5String(string)) 97c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom { 98c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom throw new IllegalArgumentException("string contains illegal characters"); 99c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom } 100c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom 101b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam this.string = string; 102b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 103b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 104b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam public String getString() 105b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 106b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam return string; 107b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 108b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 109c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom public String toString() 110c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom { 111c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom return string; 112c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom } 113c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom 114b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam public byte[] getOctets() 115b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 116b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam char[] cs = string.toCharArray(); 117b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam byte[] bs = new byte[cs.length]; 118b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 119b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam for (int i = 0; i != cs.length; i++) 120b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 121b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam bs[i] = (byte)cs[i]; 122b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 123b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 124b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam return bs; 125b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 126b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 127b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam void encode( 128b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam DEROutputStream out) 129b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam throws IOException 130b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 131b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam out.writeEncoded(IA5_STRING, this.getOctets()); 132b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 133b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 134b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam public int hashCode() 135b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 136b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam return this.getString().hashCode(); 137b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 138b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 139c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom boolean asn1Equals( 140c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom DERObject o) 141b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 142b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam if (!(o instanceof DERIA5String)) 143b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 144b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam return false; 145b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 146b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 147b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam DERIA5String s = (DERIA5String)o; 148b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 149b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam return this.getString().equals(s.getString()); 150b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 151c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom 152c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom /** 153c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom * return true if the passed in String can be represented without 154c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom * loss as an IA5String, false otherwise. 155c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom * 156c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom * @return true if in printable set, false otherwise. 157c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom */ 158c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom public static boolean isIA5String( 159c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom String str) 160c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom { 161c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom for (int i = str.length() - 1; i >= 0; i--) 162c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom { 163c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom char ch = str.charAt(i); 164c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom 165c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom if (ch > 0x007f) 166c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom { 167c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom return false; 168c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom } 169c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom } 170c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom 171c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom return true; 172c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom } 173b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam} 174