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