1 /* 2 * Copyright (c) 1999, 2001, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 4178326 27 * @summary Make sure reading/writing of different DER encoded string 28 * types works correctly. 29 */ 30 31 import java.io.*; 32 import sun.security.util.*; 33 34 public class StringTypes { 35 36 private static String s = null; 37 private static String fileName = "StringTypes.bin"; 38 39 public static void main(String[] args) throws Exception { 40 41 s = new String("This is just a test!"); 42 43 byte[] asciiBytes = s.getBytes("ASCII"); 44 byte[] utf8Bytes = s.getBytes("UTF8"); 45 byte[] iso8859_1Bytes = s.getBytes("ISO-8859-1"); 46 47 byte[] unicodeBytes = s.getBytes("UnicodeBigUnmarked"); 48 byte[] unicodeBytes2 = getBytes(s); 49 50 // test that unicode encoder is the correct one 51 if (!equalBytes(unicodeBytes, unicodeBytes2)) 52 throw new Exception ("Problem with unicode encoder being used."); 53 54 FileOutputStream fout = new FileOutputStream(fileName); 55 DerOutputStream derOut = new DerOutputStream(); 56 57 System.out.println("Writing Java string out as various DER" + 58 " encoded Strings now..."); 59 derOut.putUTF8String(s); 60 derOut.putPrintableString(s); 61 derOut.putIA5String(s); 62 derOut.putT61String(s); 63 derOut.putBMPString(s); 64 65 derOut.derEncode(fout); 66 fout.close(); 67 68 FileInputStream fis = new FileInputStream(fileName); 69 byte[] data = new byte[fis.available()]; 70 fis.read(data); 71 DerInputStream derIn = new DerInputStream(data); 72 fis.close(); 73 74 System.out.println("\nReading Strings back as DerValue's...\n"); 75 76 DerValue der; 77 78 der = derIn.getDerValue(); 79 verifyDER("UTF8", der, DerValue.tag_UTF8String, utf8Bytes); 80 81 der = derIn.getDerValue(); 82 verifyDER("Printable", der, DerValue.tag_PrintableString, asciiBytes); 83 84 der = derIn.getDerValue(); 85 verifyDER("IA5", der, DerValue.tag_IA5String, asciiBytes); 86 87 der = derIn.getDerValue(); 88 verifyDER("T61", der, DerValue.tag_T61String, iso8859_1Bytes); 89 90 der = derIn.getDerValue(); 91 verifyDER("BMP", der, DerValue.tag_BMPString, unicodeBytes); 92 93 if (derIn.available() > 0) 94 throw new Exception("DerInputStream has extra data!"); 95 96 97 derIn.reset(); 98 99 System.out.println("Reading Strings back as Strings...\n"); 100 101 verifyString("UTF8", derIn.getUTF8String()); 102 verifyString("Printable", derIn.getPrintableString()); 103 verifyString("IA5", derIn.getIA5String()); 104 verifyString("T61", derIn.getT61String()); 105 verifyString("BMP", derIn.getBMPString()); 106 107 } 108 109 /** 110 * Return the unicode bytes in the string as a byte[] 111 */ 112 private static byte[] getBytes(String s) { 113 114 int len = s.length(); 115 byte[] retVal = new byte[len*2]; // each unicode char is two bytes 116 117 for (int i = 0, j = 0; i < len; i++, j+=2) { 118 retVal[j] = (byte) (s.charAt(i)>>8); 119 retVal[j+1] = (byte) (s.charAt(i)); 120 } 121 122 return retVal; 123 } 124 125 /* Compare two byte arrays */ 126 private static boolean equalBytes(byte[] a, byte[] b) { 127 128 int len1 = a.length; 129 int len2 = b.length; 130 131 if (len1 != len2) 132 return false; 133 134 for (int i = 0; i < len1 && i < len2; i++) 135 if (a[i] != b[i]) 136 return false; 137 138 return true; 139 } 140 141 /* Verify that the DER object is correct */ 142 private static void verifyDER(String type, 143 DerValue der, byte tag, 144 byte[] data) throws Exception { 145 if (der.tag != tag) 146 throw new Exception("Problem with tag for " + type); 147 148 if (!equalBytes(der.data.toByteArray(), data)) 149 throw new Exception("Problem with data for " + type); 150 151 System.out.println(type + " checks out OK"); 152 System.out.println("Calling toString on it: " + der.toString() + "\n"); 153 } 154 155 private static void verifyString(String type, String str) 156 throws Exception { 157 158 if (!s.equals(str)) 159 throw new Exception("Problem with string " + type); 160 161 System.out.println(type + "String checks out OK\n"); 162 } 163 164 }