jdk/test/sun/security/x509/X500Name/DerValueConstructor.java
author lana
Thu, 04 Nov 2010 15:39:43 -0700
changeset 7205 e6560614a608
parent 5506 202f599c92aa
child 30820 0d4717a011d3
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1999, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @bug 4228833
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary Make sure constructor that takes DerValue argument works
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import sun.security.x509.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
public class DerValueConstructor {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
        String name = "CN=anne test";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
        DerOutputStream debugDER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
        byte[] ba;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
         * X500Name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        // encode
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        X500Name dn = new X500Name(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        System.err.println("DEBUG: dn: " + dn.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        debugDER = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        dn.encode(debugDER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        ba = debugDER.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        System.err.print("DEBUG: encoded X500Name bytes: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        System.out.println(toHexString(ba));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        // decode
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        System.out.println("DEBUG: decoding into X500Name ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        X500Name dn1 = new X500Name(new DerValue(ba));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        System.err.println("DEBUG: dn1: " + dn1.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        dn1 = new X500Name(ba);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        System.err.println("DEBUG: dn1: " + dn1.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        dn1 = new X500Name(new DerInputStream(ba));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        System.err.println("DEBUG: dn1: " + dn1.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
         * GeneralName
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        // encode
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        GeneralName gn = new GeneralName(dn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        System.err.println("DEBUG: gn: " + gn.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        debugDER = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        gn.encode(debugDER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        ba = debugDER.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        System.err.print("DEBUG: encoded GeneralName bytes: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        System.out.println(toHexString(ba));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        // decode
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        System.out.println("DEBUG: decoding into GeneralName ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        GeneralName gn1 = new GeneralName(new DerValue(ba));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        System.err.println("DEBUG: gn1: " + gn1.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
         * GeneralSubtree
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        // encode
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        GeneralSubtree subTree = new GeneralSubtree(gn, 0, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        System.err.println("DEBUG: subTree: " + subTree.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        debugDER = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        subTree.encode(debugDER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        ba = debugDER.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        System.err.print("DEBUG: encoded GeneralSubtree bytes: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        System.out.println(toHexString(ba));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        // decode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        GeneralSubtree debugSubtree = new GeneralSubtree(new DerValue(ba));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Converts a byte to hex digit and writes to the supplied buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    private static void byte2hex(byte b, StringBuffer buf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                            '9', 'A', 'B', 'C', 'D', 'E', 'F' };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        int high = ((b & 0xf0) >> 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        int low = (b & 0x0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        buf.append(hexChars[high]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        buf.append(hexChars[low]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * Converts a byte array to hex string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    private static String toHexString(byte[] block) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        StringBuffer buf = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        int len = block.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            byte2hex(block[i], buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            if (i < len-1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                buf.append(":");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        return buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
}