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