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 * @modules java.base/sun.security.util 30 */ 31 32 import java.io.*; 33 import sun.security.util.*; 34 35 public class StringTypes { 36 37 private static String s = null; 38 private static String fileName = "StringTypes.bin"; 39 40 public static void main(String[] args) throws Exception { 41 42 s = new String("This is just a test!"); 43 44 byte[] asciiBytes = s.getBytes("ASCII"); 45 byte[] utf8Bytes = s.getBytes("UTF8"); 46 byte[] iso8859_1Bytes = s.getBytes("ISO-8859-1"); 47 48 byte[] unicodeBytes = s.getBytes("UnicodeBigUnmarked"); 49 byte[] unicodeBytes2 = getBytes(s); 50 51 // test that unicode encoder is the correct one 52 if (!equalBytes(unicodeBytes, unicodeBytes2)) 53 throw new Exception ("Problem with unicode encoder being used."); 54 55 FileOutputStream fout = new FileOutputStream(fileName); 56 DerOutputStream derOut = new DerOutputStream(); 57 58 System.out.println("Writing Java string out as various DER" + 59 " encoded Strings now..."); 60 derOut.putUTF8String(s); 61 derOut.putPrintableString(s); 62 derOut.putIA5String(s); 63 derOut.putT61String(s); 64 derOut.putBMPString(s); 65 66 derOut.derEncode(fout); 67 fout.close(); 68 69 FileInputStream fis = new FileInputStream(fileName); 70 byte[] data = new byte[fis.available()]; 71 fis.read(data); 72 DerInputStream derIn = new DerInputStream(data); 73 fis.close(); 74 75 System.out.println("\nReading Strings back as DerValue's...\n"); 76 77 DerValue der; 78 79 der = derIn.getDerValue(); 80 verifyDER("UTF8", der, DerValue.tag_UTF8String, utf8Bytes); 81 82 der = derIn.getDerValue(); 83 verifyDER("Printable", der, DerValue.tag_PrintableString, asciiBytes); 84 85 der = derIn.getDerValue(); 86 verifyDER("IA5", der, DerValue.tag_IA5String, asciiBytes); 87 88 der = derIn.getDerValue(); 89 verifyDER("T61", der, DerValue.tag_T61String, iso8859_1Bytes); 90 91 der = derIn.getDerValue(); 92 verifyDER("BMP", der, DerValue.tag_BMPString, unicodeBytes); 93 94 if (derIn.available() > 0) 95 throw new Exception("DerInputStream has extra data!"); 96 97 98 derIn.reset(); 99 100 System.out.println("Reading Strings back as Strings...\n"); 101 102 verifyString("UTF8", derIn.getUTF8String()); 103 verifyString("Printable", derIn.getPrintableString()); 104 verifyString("IA5", derIn.getIA5String()); 105 verifyString("T61", derIn.getT61String()); 106 verifyString("BMP", derIn.getBMPString()); 107 108 } 109 110 /** 111 * Return the unicode bytes in the string as a byte[] 112 */ 113 private static byte[] getBytes(String s) { 114 115 int len = s.length(); 116 byte[] retVal = new byte[len*2]; // each unicode char is two bytes 117 118 for (int i = 0, j = 0; i < len; i++, j+=2) { 119 retVal[j] = (byte) (s.charAt(i)>>8); 120 retVal[j+1] = (byte) (s.charAt(i)); 121 } 122 123 return retVal; 124 } 125 126 /* Compare two byte arrays */ 127 private static boolean equalBytes(byte[] a, byte[] b) { 128 129 int len1 = a.length; 130 int len2 = b.length; 131 132 if (len1 != len2) 133 return false; 134 135 for (int i = 0; i < len1 && i < len2; i++) 136 if (a[i] != b[i]) 137 return false; 138 139 return true; 140 } 141 142 /* Verify that the DER object is correct */ 143 private static void verifyDER(String type, 144 DerValue der, byte tag, 145 byte[] data) throws Exception { 146 if (der.tag != tag) 147 throw new Exception("Problem with tag for " + type); 148 149 if (!equalBytes(der.data.toByteArray(), data)) 150 throw new Exception("Problem with data for " + type); 151 152 System.out.println(type + " checks out OK"); 153 System.out.println("Calling toString on it: " + der.toString() + "\n"); 154 } 155 156 private static void verifyString(String type, String str) 157 throws Exception { 158 159 if (!s.equals(str)) 160 throw new Exception("Problem with string " + type); 161 162 System.out.println(type + "String checks out OK\n"); 163 } 164 165 }