0N/A/*
2362N/A * Copyright (c) 1999, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/* @test
0N/A * @bug 4228833
0N/A * @summary Make sure constructor that takes DerValue argument works
0N/A */
0N/A
0N/Aimport sun.security.util.*;
0N/Aimport sun.security.x509.*;
0N/A
0N/Apublic class DerValueConstructor {
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A String name = "CN=anne test";
0N/A
0N/A DerOutputStream debugDER;
0N/A byte[] ba;
0N/A
0N/A /*
0N/A * X500Name
0N/A */
0N/A
0N/A // encode
0N/A X500Name dn = new X500Name(name);
0N/A System.err.println("DEBUG: dn: " + dn.toString());
0N/A debugDER = new DerOutputStream();
0N/A dn.encode(debugDER);
0N/A ba = debugDER.toByteArray();
0N/A System.err.print("DEBUG: encoded X500Name bytes: ");
0N/A System.out.println(toHexString(ba));
0N/A System.err.println();
0N/A
0N/A // decode
0N/A System.out.println("DEBUG: decoding into X500Name ...");
0N/A X500Name dn1 = new X500Name(new DerValue(ba));
0N/A System.err.println("DEBUG: dn1: " + dn1.toString());
0N/A System.err.println();
0N/A dn1 = new X500Name(ba);
0N/A System.err.println("DEBUG: dn1: " + dn1.toString());
0N/A System.err.println();
0N/A dn1 = new X500Name(new DerInputStream(ba));
0N/A System.err.println("DEBUG: dn1: " + dn1.toString());
0N/A System.err.println();
0N/A
0N/A /*
0N/A * GeneralName
0N/A */
0N/A
0N/A // encode
0N/A GeneralName gn = new GeneralName(dn);
0N/A System.err.println("DEBUG: gn: " + gn.toString());
0N/A debugDER = new DerOutputStream();
0N/A gn.encode(debugDER);
0N/A ba = debugDER.toByteArray();
0N/A System.err.print("DEBUG: encoded GeneralName bytes: ");
0N/A System.out.println(toHexString(ba));
0N/A System.err.println();
0N/A
0N/A // decode
0N/A System.out.println("DEBUG: decoding into GeneralName ...");
0N/A GeneralName gn1 = new GeneralName(new DerValue(ba));
0N/A System.err.println("DEBUG: gn1: " + gn1.toString());
0N/A System.err.println();
0N/A
0N/A /*
0N/A * GeneralSubtree
0N/A */
0N/A
0N/A // encode
0N/A GeneralSubtree subTree = new GeneralSubtree(gn, 0, -1);
0N/A System.err.println("DEBUG: subTree: " + subTree.toString());
0N/A debugDER = new DerOutputStream();
0N/A subTree.encode(debugDER);
0N/A ba = debugDER.toByteArray();
0N/A System.err.print("DEBUG: encoded GeneralSubtree bytes: ");
0N/A System.out.println(toHexString(ba));
0N/A System.err.println();
0N/A
0N/A // decode
0N/A GeneralSubtree debugSubtree = new GeneralSubtree(new DerValue(ba));
0N/A }
0N/A
0N/A /*
0N/A * Converts a byte to hex digit and writes to the supplied buffer
0N/A */
0N/A private static void byte2hex(byte b, StringBuffer buf) {
0N/A char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
0N/A '9', 'A', 'B', 'C', 'D', 'E', 'F' };
0N/A int high = ((b & 0xf0) >> 4);
0N/A int low = (b & 0x0f);
0N/A buf.append(hexChars[high]);
0N/A buf.append(hexChars[low]);
0N/A }
0N/A
0N/A /*
0N/A * Converts a byte array to hex string
0N/A */
0N/A private static String toHexString(byte[] block) {
0N/A StringBuffer buf = new StringBuffer();
0N/A
0N/A int len = block.length;
0N/A
0N/A for (int i = 0; i < len; i++) {
0N/A byte2hex(block[i], buf);
0N/A if (i < len-1) {
0N/A buf.append(":");
0N/A }
0N/A }
0N/A return buf.toString();
0N/A }
0N/A}