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.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 blank line to stderr that is prefixed with the prefix.
 149      */
 150 
 151     public void println()
 152     {
 153         System.err.println(prefix + ":");
 154     }
 155 
 156     /**
 157      * print a message to stderr that is prefixed with the prefix.
 158      */
 159 
 160     public static void println(String prefix, String message)
 161     {
 162         System.err.println(prefix + ": "+message);
 163     }
 164 
 165     public static void println(PrintStream s, String name, byte[] data) {
 166         s.print(name + ":  { ");
 167         if (data == null) {
 168             s.print("null");
 169         } else {
 170             for (int i = 0; i < data.length; i++) {
 171                 if (i != 0) s.print(", ");
 172                 s.print(data[i] & 0x0ff);
 173             }
 174         }
 175         s.println(" }");
 176     }
 177 
 178     /**
 179      * Return the value of the boolean System property propName.
 180      *
 181      * Note use of privileged action. Do NOT make accessible to applications.
 182      */
 183     static boolean getBooleanProperty(String propName, boolean defaultValue) {
 184         // if set, require value of either true or false
 185         String b = GetPropertyAction.privilegedGetProperty(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     static void printHex(String prefix, byte[] bytes) {
 203         HexDumpEncoder dump = new HexDumpEncoder();
 204 
 205         synchronized (System.out) {
 206             System.out.println(prefix);
 207             try {
 208                 dump.encodeBuffer(bytes, System.out);
 209             } catch (Exception e) {
 210                 // ignore
 211             }
 212             System.out.flush();
 213         }
 214     }
 215 
 216     static void printHex(String prefix, ByteBuffer bb) {
 217         HexDumpEncoder dump = new HexDumpEncoder();
 218 
 219         synchronized (System.out) {
 220             System.out.println(prefix);
 221             try {
 222                 dump.encodeBuffer(bb.slice(), System.out);
 223             } catch (Exception e) {
 224                 // ignore
 225             }
 226             System.out.flush();
 227         }
 228     }
 229 
 230     static void printHex(String prefix, byte[] bytes, int offset, int length) {
 231         HexDumpEncoder dump = new HexDumpEncoder();
 232 
 233         synchronized (System.out) {
 234             System.out.println(prefix);
 235             try {
 236                 ByteBuffer bb = ByteBuffer.wrap(bytes, offset, length);
 237                 dump.encodeBuffer(bb, System.out);
 238             } catch (Exception e) {
 239                 // ignore
 240             }
 241             System.out.flush();
 242         }
 243     }
 244 }