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