1 /* 2 * Copyright (c) 1999, 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 /* @test 25 * @bug 4228833 26 * @summary Make sure constructor that takes DerValue argument works 27 */ 28 29 import sun.security.util.*; 30 import sun.security.x509.*; 31 32 public class DerValueConstructor { 33 main(String[] args)34 public static void main(String[] args) throws Exception { 35 String name = "CN=anne test"; 36 37 DerOutputStream debugDER; 38 byte[] ba; 39 40 /* 41 * X500Name 42 */ 43 44 // encode 45 X500Name dn = new X500Name(name); 46 System.err.println("DEBUG: dn: " + dn.toString()); 47 debugDER = new DerOutputStream(); 48 dn.encode(debugDER); 49 ba = debugDER.toByteArray(); 50 System.err.print("DEBUG: encoded X500Name bytes: "); 51 System.out.println(toHexString(ba)); 52 System.err.println(); 53 54 // decode 55 System.out.println("DEBUG: decoding into X500Name ..."); 56 X500Name dn1 = new X500Name(new DerValue(ba)); 57 System.err.println("DEBUG: dn1: " + dn1.toString()); 58 System.err.println(); 59 dn1 = new X500Name(ba); 60 System.err.println("DEBUG: dn1: " + dn1.toString()); 61 System.err.println(); 62 dn1 = new X500Name(new DerInputStream(ba)); 63 System.err.println("DEBUG: dn1: " + dn1.toString()); 64 System.err.println(); 65 66 /* 67 * GeneralName 68 */ 69 70 // encode 71 GeneralName gn = new GeneralName(dn); 72 System.err.println("DEBUG: gn: " + gn.toString()); 73 debugDER = new DerOutputStream(); 74 gn.encode(debugDER); 75 ba = debugDER.toByteArray(); 76 System.err.print("DEBUG: encoded GeneralName bytes: "); 77 System.out.println(toHexString(ba)); 78 System.err.println(); 79 80 // decode 81 System.out.println("DEBUG: decoding into GeneralName ..."); 82 GeneralName gn1 = new GeneralName(new DerValue(ba)); 83 System.err.println("DEBUG: gn1: " + gn1.toString()); 84 System.err.println(); 85 86 /* 87 * GeneralSubtree 88 */ 89 90 // encode 91 GeneralSubtree subTree = new GeneralSubtree(gn, 0, -1); 92 System.err.println("DEBUG: subTree: " + subTree.toString()); 93 debugDER = new DerOutputStream(); 94 subTree.encode(debugDER); 95 ba = debugDER.toByteArray(); 96 System.err.print("DEBUG: encoded GeneralSubtree bytes: "); 97 System.out.println(toHexString(ba)); 98 System.err.println(); 99 100 // decode 101 GeneralSubtree debugSubtree = new GeneralSubtree(new DerValue(ba)); 102 } 103 104 /* 105 * Converts a byte to hex digit and writes to the supplied buffer 106 */ byte2hex(byte b, StringBuffer buf)107 private static void byte2hex(byte b, StringBuffer buf) { 108 char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', 109 '9', 'A', 'B', 'C', 'D', 'E', 'F' }; 110 int high = ((b & 0xf0) >> 4); 111 int low = (b & 0x0f); 112 buf.append(hexChars[high]); 113 buf.append(hexChars[low]); 114 } 115 116 /* 117 * Converts a byte array to hex string 118 */ toHexString(byte[] block)119 private static String toHexString(byte[] block) { 120 StringBuffer buf = new StringBuffer(); 121 122 int len = block.length; 123 124 for (int i = 0; i < len; i++) { 125 byte2hex(block[i], buf); 126 if (i < len-1) { 127 buf.append(":"); 128 } 129 } 130 return buf.toString(); 131 } 132 } 133