blob: 35ce8d66348692d5087cde85d70d06fba6e9669e [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
Vinnie Ryana9299762015-02-26 16:29:49 +00002 * Copyright (c) 1998, 2015, Oracle and/or its affiliates. All rights reserved.
J. Duke319a3b92007-12-01 00:00:00 +00003 * 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
Kelly O'Hairfe008ae2010-05-25 15:58:33 -07007 * published by the Free Software Foundation. Oracle designates this
J. Duke319a3b92007-12-01 00:00:00 +00008 * particular file as subject to the "Classpath" exception as provided
Kelly O'Hairfe008ae2010-05-25 15:58:33 -07009 * by Oracle in the LICENSE file that accompanied this code.
J. Duke319a3b92007-12-01 00:00:00 +000010 *
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 *
Kelly O'Hairfe008ae2010-05-25 15:58:33 -070021 * 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.
J. Duke319a3b92007-12-01 00:00:00 +000024 */
25
26package sun.security.util;
27
28import java.math.BigInteger;
29import java.util.regex.Pattern;
30import java.util.regex.Matcher;
Xue-Lei Andrew Fanf5561762010-07-24 22:59:41 +080031import java.util.Locale;
J. Duke319a3b92007-12-01 00:00:00 +000032
33/**
34 * A utility class for debuging.
35 *
36 * @author Roland Schemers
37 */
38public class Debug {
39
40 private String prefix;
41
42 private static String args;
43
44 static {
45 args = java.security.AccessController.doPrivileged
46 (new sun.security.action.GetPropertyAction
47 ("java.security.debug"));
48
49 String args2 = java.security.AccessController.doPrivileged
50 (new sun.security.action.GetPropertyAction
51 ("java.security.auth.debug"));
52
53 if (args == null) {
54 args = args2;
55 } else {
56 if (args2 != null)
57 args = args + "," + args2;
58 }
59
60 if (args != null) {
61 args = marshal(args);
62 if (args.equals("help")) {
63 Help();
64 }
65 }
66 }
67
68 public static void Help()
69 {
70 System.err.println();
71 System.err.println("all turn on all debugging");
72 System.err.println("access print all checkPermission results");
Jason Uhb89fe032012-09-12 15:20:54 -040073 System.err.println("certpath PKIX CertPathBuilder and");
74 System.err.println(" CertPathValidator debugging");
J. Duke319a3b92007-12-01 00:00:00 +000075 System.err.println("combiner SubjectDomainCombiner debugging");
76 System.err.println("gssloginconfig");
Jason Uhb89fe032012-09-12 15:20:54 -040077 System.err.println(" GSS LoginConfigImpl debugging");
J. Duke319a3b92007-12-01 00:00:00 +000078 System.err.println("configfile JAAS ConfigFile loading");
79 System.err.println("configparser JAAS ConfigFile parsing");
J. Duke319a3b92007-12-01 00:00:00 +000080 System.err.println("jar jar verification");
81 System.err.println("logincontext login context results");
Jason Uhb89fe032012-09-12 15:20:54 -040082 System.err.println("jca JCA engine class debugging");
Vinnie Ryana9299762015-02-26 16:29:49 +000083 System.err.println("keystore KeyStore debugging");
J. Duke319a3b92007-12-01 00:00:00 +000084 System.err.println("policy loading and granting");
85 System.err.println("provider security provider debugging");
Jason Uhb89fe032012-09-12 15:20:54 -040086 System.err.println("pkcs11 PKCS11 session manager debugging");
87 System.err.println("pkcs11keystore");
88 System.err.println(" PKCS11 KeyStore debugging");
Vinnie Ryana9299762015-02-26 16:29:49 +000089 System.err.println("pkcs12 PKCS12 KeyStore debugging");
Jason Uhb89fe032012-09-12 15:20:54 -040090 System.err.println("sunpkcs11 SunPKCS11 provider debugging");
J. Duke319a3b92007-12-01 00:00:00 +000091 System.err.println("scl permissions SecureClassLoader assigns");
Sean Mullanf1f3aad2011-10-19 10:15:23 -040092 System.err.println("ts timestamping");
J. Duke319a3b92007-12-01 00:00:00 +000093 System.err.println();
94 System.err.println("The following can be used with access:");
95 System.err.println();
96 System.err.println("stack include stack trace");
97 System.err.println("domain dump all domains in context");
98 System.err.println("failure before throwing exception, dump stack");
99 System.err.println(" and domain that didn't have permission");
100 System.err.println();
101 System.err.println("The following can be used with stack and domain:");
102 System.err.println();
103 System.err.println("permission=<classname>");
104 System.err.println(" only dump output if specified permission");
105 System.err.println(" is being checked");
106 System.err.println("codebase=<URL>");
107 System.err.println(" only dump output if specified codebase");
108 System.err.println(" is being checked");
Vinnie Ryanaeecc192014-09-25 12:24:19 +0100109 System.err.println();
110 System.err.println("The following can be used with provider:");
111 System.err.println();
112 System.err.println("engine=<engines>");
113 System.err.println(" only dump output for the specified list");
114 System.err.println(" of JCA engines. Supported values:");
115 System.err.println(" Cipher, KeyAgreement, KeyGenerator,");
116 System.err.println(" KeyPairGenerator, KeyStore, Mac,");
117 System.err.println(" MessageDigest, SecureRandom, Signature.");
J. Duke319a3b92007-12-01 00:00:00 +0000118 System.err.println();
Vinnie Ryana9299762015-02-26 16:29:49 +0000119 System.err.println("The following can be used with certpath:");
120 System.err.println();
121 System.err.println("ocsp dump the OCSP protocol exchanges");
122 System.err.println();
J. Duke319a3b92007-12-01 00:00:00 +0000123 System.err.println("Note: Separate multiple options with a comma");
124 System.exit(0);
125 }
126
127
128 /**
129 * Get a Debug object corresponding to whether or not the given
130 * option is set. Set the prefix to be the same as option.
131 */
132
133 public static Debug getInstance(String option)
134 {
135 return getInstance(option, option);
136 }
137
138 /**
139 * Get a Debug object corresponding to whether or not the given
140 * option is set. Set the prefix to be prefix.
141 */
142 public static Debug getInstance(String option, String prefix)
143 {
144 if (isOn(option)) {
145 Debug d = new Debug();
146 d.prefix = prefix;
147 return d;
148 } else {
149 return null;
150 }
151 }
152
153 /**
154 * True if the system property "security.debug" contains the
155 * string "option".
156 */
157 public static boolean isOn(String option)
158 {
159 if (args == null)
160 return false;
161 else {
162 if (args.indexOf("all") != -1)
163 return true;
164 else
165 return (args.indexOf(option) != -1);
166 }
167 }
168
169 /**
170 * print a message to stderr that is prefixed with the prefix
171 * created from the call to getInstance.
172 */
173
174 public void println(String message)
175 {
176 System.err.println(prefix + ": "+message);
177 }
178
179 /**
180 * print a blank line to stderr that is prefixed with the prefix.
181 */
182
183 public void println()
184 {
185 System.err.println(prefix + ":");
186 }
187
188 /**
189 * print a message to stderr that is prefixed with the prefix.
190 */
191
192 public static void println(String prefix, String message)
193 {
194 System.err.println(prefix + ": "+message);
195 }
196
197 /**
198 * return a hexadecimal printed representation of the specified
199 * BigInteger object. the value is formatted to fit on lines of
200 * at least 75 characters, with embedded newlines. Words are
201 * separated for readability, with eight words (32 bytes) per line.
202 */
203 public static String toHexString(BigInteger b) {
204 String hexValue = b.toString(16);
Otavio Goncalves de Santanaed271b02014-06-16 17:45:26 +0100205 StringBuilder sb = new StringBuilder(hexValue.length()*2);
J. Duke319a3b92007-12-01 00:00:00 +0000206
207 if (hexValue.startsWith("-")) {
Otavio Goncalves de Santanaed271b02014-06-16 17:45:26 +0100208 sb.append(" -");
J. Duke319a3b92007-12-01 00:00:00 +0000209 hexValue = hexValue.substring(1);
210 } else {
Otavio Goncalves de Santanaed271b02014-06-16 17:45:26 +0100211 sb.append(" "); // four spaces
J. Duke319a3b92007-12-01 00:00:00 +0000212 }
213 if ((hexValue.length()%2) != 0) {
214 // add back the leading 0
215 hexValue = "0" + hexValue;
216 }
217 int i=0;
218 while (i < hexValue.length()) {
219 // one byte at a time
Otavio Goncalves de Santanaed271b02014-06-16 17:45:26 +0100220 sb.append(hexValue.substring(i, i + 2));
J. Duke319a3b92007-12-01 00:00:00 +0000221 i+=2;
222 if (i!= hexValue.length()) {
223 if ((i%64) == 0) {
Otavio Goncalves de Santanaed271b02014-06-16 17:45:26 +0100224 sb.append("\n "); // line after eight words
J. Duke319a3b92007-12-01 00:00:00 +0000225 } else if (i%8 == 0) {
Otavio Goncalves de Santanaed271b02014-06-16 17:45:26 +0100226 sb.append(" "); // space between words
J. Duke319a3b92007-12-01 00:00:00 +0000227 }
228 }
229 }
Otavio Goncalves de Santanaed271b02014-06-16 17:45:26 +0100230 return sb.toString();
J. Duke319a3b92007-12-01 00:00:00 +0000231 }
232
233 /**
234 * change a string into lower case except permission classes and URLs.
235 */
236 private static String marshal(String args) {
237 if (args != null) {
Otavio Goncalves de Santanaed271b02014-06-16 17:45:26 +0100238 StringBuilder target = new StringBuilder();
J. Duke319a3b92007-12-01 00:00:00 +0000239 StringBuffer source = new StringBuffer(args);
240
241 // obtain the "permission=<classname>" options
242 // the syntax of classname: IDENTIFIER.IDENTIFIER
243 // the regular express to match a class name:
244 // "[a-zA-Z_$][a-zA-Z0-9_$]*([.][a-zA-Z_$][a-zA-Z0-9_$]*)*"
245 String keyReg = "[Pp][Ee][Rr][Mm][Ii][Ss][Ss][Ii][Oo][Nn]=";
246 String keyStr = "permission=";
247 String reg = keyReg +
248 "[a-zA-Z_$][a-zA-Z0-9_$]*([.][a-zA-Z_$][a-zA-Z0-9_$]*)*";
249 Pattern pattern = Pattern.compile(reg);
250 Matcher matcher = pattern.matcher(source);
251 StringBuffer left = new StringBuffer();
252 while (matcher.find()) {
253 String matched = matcher.group();
254 target.append(matched.replaceFirst(keyReg, keyStr));
255 target.append(" ");
256
257 // delete the matched sequence
258 matcher.appendReplacement(left, "");
259 }
260 matcher.appendTail(left);
261 source = left;
262
263 // obtain the "codebase=<URL>" options
264 // the syntax of URL is too flexible, and here assumes that the
265 // URL contains no space, comma(','), and semicolon(';'). That
266 // also means those characters also could be used as separator
267 // after codebase option.
268 // However, the assumption is incorrect in some special situation
269 // when the URL contains comma or semicolon
270 keyReg = "[Cc][Oo][Dd][Ee][Bb][Aa][Ss][Ee]=";
271 keyStr = "codebase=";
272 reg = keyReg + "[^, ;]*";
273 pattern = Pattern.compile(reg);
274 matcher = pattern.matcher(source);
275 left = new StringBuffer();
276 while (matcher.find()) {
277 String matched = matcher.group();
278 target.append(matched.replaceFirst(keyReg, keyStr));
279 target.append(" ");
280
281 // delete the matched sequence
282 matcher.appendReplacement(left, "");
283 }
284 matcher.appendTail(left);
285 source = left;
286
287 // convert the rest to lower-case characters
Xue-Lei Andrew Fanf5561762010-07-24 22:59:41 +0800288 target.append(source.toString().toLowerCase(Locale.ENGLISH));
J. Duke319a3b92007-12-01 00:00:00 +0000289
290 return target.toString();
291 }
292
293 return null;
294 }
295
Martin Buchholza2f0fe32015-09-15 21:56:04 -0700296 private static final char[] hexDigits = "0123456789abcdef".toCharArray();
J. Duke319a3b92007-12-01 00:00:00 +0000297
298 public static String toString(byte[] b) {
299 if (b == null) {
300 return "(null)";
301 }
302 StringBuilder sb = new StringBuilder(b.length * 3);
303 for (int i = 0; i < b.length; i++) {
304 int k = b[i] & 0xff;
305 if (i != 0) {
306 sb.append(':');
307 }
308 sb.append(hexDigits[k >>> 4]);
309 sb.append(hexDigits[k & 0xf]);
310 }
311 return sb.toString();
312 }
313
314}