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