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