1/*
2 * Copyright (c) 1999, 2010, 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.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package sun.security.ssl;
27
28import java.io.PrintStream;
29import java.security.AccessController;
30import java.util.Locale;
31
32import sun.security.action.GetPropertyAction;
33
34/**
35 * This class has be shamefully lifted from sun.security.util.Debug
36 *
37 * @author Gary Ellison
38 */
39public class Debug {
40
41    private String prefix;
42
43    private static String args;
44
45    static {
46        args = java.security.AccessController.doPrivileged(
47            new GetPropertyAction("javax.net.debug", ""));
48        args = args.toLowerCase(Locale.ENGLISH);
49        if (args.equals("help")) {
50            Help();
51        }
52    }
53
54    public static void Help()
55    {
56        System.err.println();
57        System.err.println("all            turn on all debugging");
58        System.err.println("ssl            turn on ssl debugging");
59        System.err.println();
60        System.err.println("The following can be used with ssl:");
61        System.err.println("\trecord       enable per-record tracing");
62        System.err.println("\thandshake    print each handshake message");
63        System.err.println("\tkeygen       print key generation data");
64        System.err.println("\tsession      print session activity");
65        System.err.println("\tdefaultctx   print default SSL initialization");
66        System.err.println("\tsslctx       print SSLContext tracing");
67        System.err.println("\tsessioncache print session cache tracing");
68        System.err.println("\tkeymanager   print key manager tracing");
69        System.err.println("\ttrustmanager print trust manager tracing");
70        System.err.println("\tpluggability print pluggability tracing");
71        System.err.println();
72        System.err.println("\thandshake debugging can be widened with:");
73        System.err.println("\tdata         hex dump of each handshake message");
74        System.err.println("\tverbose      verbose handshake message printing");
75        System.err.println();
76        System.err.println("\trecord debugging can be widened with:");
77        System.err.println("\tplaintext    hex dump of record plaintext");
78        System.err.println("\tpacket       print raw SSL/TLS packets");
79        System.err.println();
80        System.exit(0);
81    }
82
83    /**
84     * Get a Debug object corresponding to whether or not the given
85     * option is set. Set the prefix to be the same as option.
86     */
87
88    public static Debug getInstance(String option)
89    {
90        return getInstance(option, option);
91    }
92
93    /**
94     * Get a Debug object corresponding to whether or not the given
95     * option is set. Set the prefix to be prefix.
96     */
97    public static Debug getInstance(String option, String prefix)
98    {
99        if (isOn(option)) {
100            Debug d = new Debug();
101            d.prefix = prefix;
102            return d;
103        } else {
104            return null;
105        }
106    }
107
108    /**
109     * True if the property "javax.net.debug" contains the
110     * string "option".
111     */
112    public static boolean isOn(String option)
113    {
114        if (args == null) {
115            return false;
116        } else {
117            int n = 0;
118            option = option.toLowerCase(Locale.ENGLISH);
119
120            if (args.indexOf("all") != -1) {
121                return true;
122            } else if ((n = args.indexOf("ssl")) != -1) {
123                if (args.indexOf("sslctx", n) == -1) {
124                    // don't enable data and plaintext options by default
125                    if (!(option.equals("data")
126                        || option.equals("packet")
127                        || option.equals("plaintext"))) {
128                        return true;
129                    }
130                }
131            }
132            return (args.indexOf(option) != -1);
133        }
134    }
135
136    /**
137     * print a message to stderr that is prefixed with the prefix
138     * created from the call to getInstance.
139     */
140
141    public void println(String message)
142    {
143        System.err.println(prefix + ": "+message);
144    }
145
146    /**
147     * print a blank line to stderr that is prefixed with the prefix.
148     */
149
150    public void println()
151    {
152        System.err.println(prefix + ":");
153    }
154
155    /**
156     * print a message to stderr that is prefixed with the prefix.
157     */
158
159    public static void println(String prefix, String message)
160    {
161        System.err.println(prefix + ": "+message);
162    }
163
164    public static void println(PrintStream s, String name, byte[] data) {
165        s.print(name + ":  { ");
166        if (data == null) {
167            s.print("null");
168        } else {
169            for (int i = 0; i < data.length; i++) {
170                if (i != 0) s.print(", ");
171                s.print(data[i] & 0x0ff);
172            }
173        }
174        s.println(" }");
175    }
176
177    /**
178     * Return the value of the boolean System property propName.
179     *
180     * Note use of doPrivileged(). Do make accessible to applications.
181     */
182    static boolean getBooleanProperty(String propName, boolean defaultValue) {
183        // if set, require value of either true or false
184        String b = AccessController.doPrivileged(
185                new GetPropertyAction(propName));
186        if (b == null) {
187            return defaultValue;
188        } else if (b.equalsIgnoreCase("false")) {
189            return false;
190        } else if (b.equalsIgnoreCase("true")) {
191            return true;
192        } else {
193            throw new RuntimeException("Value of " + propName
194                + " must either be 'true' or 'false'");
195        }
196    }
197
198    static String toString(byte[] b) {
199        return sun.security.util.Debug.toString(b);
200    }
201}
202