src/java.base/share/classes/sun/security/ssl/Debug.java
changeset 47216 71c04702a3d5
parent 41820 3d8c88d00c9f
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     1 /*
       
     2  * Copyright (c) 1999, 2016, 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 
       
    26 package sun.security.ssl;
       
    27 
       
    28 import java.io.PrintStream;
       
    29 import java.util.Locale;
       
    30 
       
    31 import sun.security.util.HexDumpEncoder;
       
    32 import java.nio.ByteBuffer;
       
    33 
       
    34 import sun.security.action.GetPropertyAction;
       
    35 
       
    36 /**
       
    37  * This class has be shamefully lifted from sun.security.util.Debug
       
    38  *
       
    39  * @author Gary Ellison
       
    40  */
       
    41 public class Debug {
       
    42 
       
    43     private String prefix;
       
    44 
       
    45     private static String args;
       
    46 
       
    47     static {
       
    48         args = GetPropertyAction.privilegedGetProperty("javax.net.debug", "");
       
    49         args = args.toLowerCase(Locale.ENGLISH);
       
    50         if (args.equals("help")) {
       
    51             Help();
       
    52         }
       
    53     }
       
    54 
       
    55     public static void Help()
       
    56     {
       
    57         System.err.println();
       
    58         System.err.println("all            turn on all debugging");
       
    59         System.err.println("ssl            turn on ssl debugging");
       
    60         System.err.println();
       
    61         System.err.println("The following can be used with ssl:");
       
    62         System.err.println("\trecord       enable per-record tracing");
       
    63         System.err.println("\thandshake    print each handshake message");
       
    64         System.err.println("\tkeygen       print key generation data");
       
    65         System.err.println("\tsession      print session activity");
       
    66         System.err.println("\tdefaultctx   print default SSL initialization");
       
    67         System.err.println("\tsslctx       print SSLContext tracing");
       
    68         System.err.println("\tsessioncache print session cache tracing");
       
    69         System.err.println("\tkeymanager   print key manager tracing");
       
    70         System.err.println("\ttrustmanager print trust manager tracing");
       
    71         System.err.println("\tpluggability print pluggability tracing");
       
    72         System.err.println();
       
    73         System.err.println("\thandshake debugging can be widened with:");
       
    74         System.err.println("\tdata         hex dump of each handshake message");
       
    75         System.err.println("\tverbose      verbose handshake message printing");
       
    76         System.err.println();
       
    77         System.err.println("\trecord debugging can be widened with:");
       
    78         System.err.println("\tplaintext    hex dump of record plaintext");
       
    79         System.err.println("\tpacket       print raw SSL/TLS packets");
       
    80         System.err.println();
       
    81         System.exit(0);
       
    82     }
       
    83 
       
    84     /**
       
    85      * Get a Debug object corresponding to whether or not the given
       
    86      * option is set. Set the prefix to be the same as option.
       
    87      */
       
    88 
       
    89     public static Debug getInstance(String option)
       
    90     {
       
    91         return getInstance(option, option);
       
    92     }
       
    93 
       
    94     /**
       
    95      * Get a Debug object corresponding to whether or not the given
       
    96      * option is set. Set the prefix to be prefix.
       
    97      */
       
    98     public static Debug getInstance(String option, String prefix)
       
    99     {
       
   100         if (isOn(option)) {
       
   101             Debug d = new Debug();
       
   102             d.prefix = prefix;
       
   103             return d;
       
   104         } else {
       
   105             return null;
       
   106         }
       
   107     }
       
   108 
       
   109     /**
       
   110      * True if the property "javax.net.debug" contains the
       
   111      * string "option".
       
   112      */
       
   113     public static boolean isOn(String option)
       
   114     {
       
   115         if (args == null) {
       
   116             return false;
       
   117         } else {
       
   118             int n = 0;
       
   119             option = option.toLowerCase(Locale.ENGLISH);
       
   120 
       
   121             if (args.indexOf("all") != -1) {
       
   122                 return true;
       
   123             } else if ((n = args.indexOf("ssl")) != -1) {
       
   124                 if (args.indexOf("sslctx", n) == -1) {
       
   125                     // don't enable data and plaintext options by default
       
   126                     if (!(option.equals("data")
       
   127                         || option.equals("packet")
       
   128                         || option.equals("plaintext"))) {
       
   129                         return true;
       
   130                     }
       
   131                 }
       
   132             }
       
   133             return (args.indexOf(option) != -1);
       
   134         }
       
   135     }
       
   136 
       
   137     /**
       
   138      * print a message to stderr that is prefixed with the prefix
       
   139      * created from the call to getInstance.
       
   140      */
       
   141 
       
   142     public void println(String message)
       
   143     {
       
   144         System.err.println(prefix + ": "+message);
       
   145     }
       
   146 
       
   147     /**
       
   148      * Print a message to stdout.
       
   149      */
       
   150     static void log(String message) {
       
   151         System.out.println(Thread.currentThread().getName() + ": " + message);
       
   152     }
       
   153 
       
   154     /**
       
   155      * print a blank line to stderr that is prefixed with the prefix.
       
   156      */
       
   157 
       
   158     public void println()
       
   159     {
       
   160         System.err.println(prefix + ":");
       
   161     }
       
   162 
       
   163     /**
       
   164      * print a message to stderr that is prefixed with the prefix.
       
   165      */
       
   166     public static void println(String prefix, String message)
       
   167     {
       
   168         System.err.println(prefix + ": "+message);
       
   169     }
       
   170 
       
   171     public static void println(PrintStream s, String name, byte[] data) {
       
   172         s.print(name + ":  { ");
       
   173         if (data == null) {
       
   174             s.print("null");
       
   175         } else {
       
   176             for (int i = 0; i < data.length; i++) {
       
   177                 if (i != 0) s.print(", ");
       
   178                 s.print(data[i] & 0x0ff);
       
   179             }
       
   180         }
       
   181         s.println(" }");
       
   182     }
       
   183 
       
   184     /**
       
   185      * Return the value of the boolean System property propName.
       
   186      *
       
   187      * Note use of privileged action. Do NOT make accessible to applications.
       
   188      */
       
   189     static boolean getBooleanProperty(String propName, boolean defaultValue) {
       
   190         // if set, require value of either true or false
       
   191         String b = GetPropertyAction.privilegedGetProperty(propName);
       
   192         if (b == null) {
       
   193             return defaultValue;
       
   194         } else if (b.equalsIgnoreCase("false")) {
       
   195             return false;
       
   196         } else if (b.equalsIgnoreCase("true")) {
       
   197             return true;
       
   198         } else {
       
   199             throw new RuntimeException("Value of " + propName
       
   200                 + " must either be 'true' or 'false'");
       
   201         }
       
   202     }
       
   203 
       
   204     static String toString(byte[] b) {
       
   205         return sun.security.util.Debug.toString(b);
       
   206     }
       
   207 
       
   208     static void printHex(String prefix, byte[] bytes) {
       
   209         HexDumpEncoder dump = new HexDumpEncoder();
       
   210 
       
   211         synchronized (System.out) {
       
   212             System.out.println(prefix);
       
   213             try {
       
   214                 dump.encodeBuffer(bytes, System.out);
       
   215             } catch (Exception e) {
       
   216                 // ignore
       
   217             }
       
   218             System.out.flush();
       
   219         }
       
   220     }
       
   221 
       
   222     static void printHex(String prefix, ByteBuffer bb) {
       
   223         HexDumpEncoder dump = new HexDumpEncoder();
       
   224 
       
   225         synchronized (System.out) {
       
   226             System.out.println(prefix);
       
   227             try {
       
   228                 dump.encodeBuffer(bb.slice(), System.out);
       
   229             } catch (Exception e) {
       
   230                 // ignore
       
   231             }
       
   232             System.out.flush();
       
   233         }
       
   234     }
       
   235 
       
   236     static void printHex(String prefix, byte[] bytes, int offset, int length) {
       
   237         HexDumpEncoder dump = new HexDumpEncoder();
       
   238 
       
   239         synchronized (System.out) {
       
   240             System.out.println(prefix);
       
   241             try {
       
   242                 ByteBuffer bb = ByteBuffer.wrap(bytes, offset, length);
       
   243                 dump.encodeBuffer(bb, System.out);
       
   244             } catch (Exception e) {
       
   245                 // ignore
       
   246             }
       
   247             System.out.flush();
       
   248         }
       
   249     }
       
   250 }