author | vinnie |
Thu, 25 Sep 2014 12:24:19 +0100 | |
changeset 26736 | 5a93000b26cd |
parent 25859 | 3317bb8137f4 |
child 29122 | 052f0685cff4 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
26736
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
2 |
* Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.security.util; |
|
27 |
||
28 |
import java.math.BigInteger; |
|
29 |
import java.util.regex.Pattern; |
|
30 |
import java.util.regex.Matcher; |
|
6122
16fa7ed7ff1b
6867345: Turkish regional options cause NPE in sun.security.x509.AlgorithmId.algOID
xuelei
parents:
5506
diff
changeset
|
31 |
import java.util.Locale; |
2 | 32 |
|
33 |
/** |
|
34 |
* A utility class for debuging. |
|
35 |
* |
|
36 |
* @author Roland Schemers |
|
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 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"); |
|
13796
792402bbac8d
7196593: java.security.debug=help doesn't list certpath option
mullan
parents:
10788
diff
changeset
|
73 |
System.err.println("certpath PKIX CertPathBuilder and"); |
792402bbac8d
7196593: java.security.debug=help doesn't list certpath option
mullan
parents:
10788
diff
changeset
|
74 |
System.err.println(" CertPathValidator debugging"); |
2 | 75 |
System.err.println("combiner SubjectDomainCombiner debugging"); |
76 |
System.err.println("gssloginconfig"); |
|
13796
792402bbac8d
7196593: java.security.debug=help doesn't list certpath option
mullan
parents:
10788
diff
changeset
|
77 |
System.err.println(" GSS LoginConfigImpl debugging"); |
2 | 78 |
System.err.println("configfile JAAS ConfigFile loading"); |
79 |
System.err.println("configparser JAAS ConfigFile parsing"); |
|
80 |
System.err.println("jar jar verification"); |
|
81 |
System.err.println("logincontext login context results"); |
|
13796
792402bbac8d
7196593: java.security.debug=help doesn't list certpath option
mullan
parents:
10788
diff
changeset
|
82 |
System.err.println("jca JCA engine class debugging"); |
2 | 83 |
System.err.println("policy loading and granting"); |
84 |
System.err.println("provider security provider debugging"); |
|
13796
792402bbac8d
7196593: java.security.debug=help doesn't list certpath option
mullan
parents:
10788
diff
changeset
|
85 |
System.err.println("pkcs11 PKCS11 session manager debugging"); |
792402bbac8d
7196593: java.security.debug=help doesn't list certpath option
mullan
parents:
10788
diff
changeset
|
86 |
System.err.println("pkcs11keystore"); |
792402bbac8d
7196593: java.security.debug=help doesn't list certpath option
mullan
parents:
10788
diff
changeset
|
87 |
System.err.println(" PKCS11 KeyStore debugging"); |
792402bbac8d
7196593: java.security.debug=help doesn't list certpath option
mullan
parents:
10788
diff
changeset
|
88 |
System.err.println("sunpkcs11 SunPKCS11 provider debugging"); |
2 | 89 |
System.err.println("scl permissions SecureClassLoader assigns"); |
10788
680a3dbfcaba
7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents:
6122
diff
changeset
|
90 |
System.err.println("ts timestamping"); |
2 | 91 |
System.err.println(); |
92 |
System.err.println("The following can be used with access:"); |
|
93 |
System.err.println(); |
|
94 |
System.err.println("stack include stack trace"); |
|
95 |
System.err.println("domain dump all domains in context"); |
|
96 |
System.err.println("failure before throwing exception, dump stack"); |
|
97 |
System.err.println(" and domain that didn't have permission"); |
|
98 |
System.err.println(); |
|
99 |
System.err.println("The following can be used with stack and domain:"); |
|
100 |
System.err.println(); |
|
101 |
System.err.println("permission=<classname>"); |
|
102 |
System.err.println(" only dump output if specified permission"); |
|
103 |
System.err.println(" is being checked"); |
|
104 |
System.err.println("codebase=<URL>"); |
|
105 |
System.err.println(" only dump output if specified codebase"); |
|
106 |
System.err.println(" is being checked"); |
|
26736
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
107 |
System.err.println(); |
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
108 |
System.err.println("The following can be used with provider:"); |
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
109 |
System.err.println(); |
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
110 |
System.err.println("engine=<engines>"); |
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
111 |
System.err.println(" only dump output for the specified list"); |
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
112 |
System.err.println(" of JCA engines. Supported values:"); |
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
113 |
System.err.println(" Cipher, KeyAgreement, KeyGenerator,"); |
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
114 |
System.err.println(" KeyPairGenerator, KeyStore, Mac,"); |
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
115 |
System.err.println(" MessageDigest, SecureRandom, Signature."); |
2 | 116 |
System.err.println(); |
117 |
System.err.println("Note: Separate multiple options with a comma"); |
|
118 |
System.exit(0); |
|
119 |
} |
|
120 |
||
121 |
||
122 |
/** |
|
123 |
* Get a Debug object corresponding to whether or not the given |
|
124 |
* option is set. Set the prefix to be the same as option. |
|
125 |
*/ |
|
126 |
||
127 |
public static Debug getInstance(String option) |
|
128 |
{ |
|
129 |
return getInstance(option, option); |
|
130 |
} |
|
131 |
||
132 |
/** |
|
133 |
* Get a Debug object corresponding to whether or not the given |
|
134 |
* option is set. Set the prefix to be prefix. |
|
135 |
*/ |
|
136 |
public static Debug getInstance(String option, String prefix) |
|
137 |
{ |
|
138 |
if (isOn(option)) { |
|
139 |
Debug d = new Debug(); |
|
140 |
d.prefix = prefix; |
|
141 |
return d; |
|
142 |
} else { |
|
143 |
return null; |
|
144 |
} |
|
145 |
} |
|
146 |
||
147 |
/** |
|
148 |
* True if the system property "security.debug" contains the |
|
149 |
* string "option". |
|
150 |
*/ |
|
151 |
public static boolean isOn(String option) |
|
152 |
{ |
|
153 |
if (args == null) |
|
154 |
return false; |
|
155 |
else { |
|
156 |
if (args.indexOf("all") != -1) |
|
157 |
return true; |
|
158 |
else |
|
159 |
return (args.indexOf(option) != -1); |
|
160 |
} |
|
161 |
} |
|
162 |
||
163 |
/** |
|
164 |
* print a message to stderr that is prefixed with the prefix |
|
165 |
* created from the call to getInstance. |
|
166 |
*/ |
|
167 |
||
168 |
public void println(String message) |
|
169 |
{ |
|
170 |
System.err.println(prefix + ": "+message); |
|
171 |
} |
|
172 |
||
173 |
/** |
|
174 |
* print a blank line to stderr that is prefixed with the prefix. |
|
175 |
*/ |
|
176 |
||
177 |
public void println() |
|
178 |
{ |
|
179 |
System.err.println(prefix + ":"); |
|
180 |
} |
|
181 |
||
182 |
/** |
|
183 |
* print a message to stderr that is prefixed with the prefix. |
|
184 |
*/ |
|
185 |
||
186 |
public static void println(String prefix, String message) |
|
187 |
{ |
|
188 |
System.err.println(prefix + ": "+message); |
|
189 |
} |
|
190 |
||
191 |
/** |
|
192 |
* return a hexadecimal printed representation of the specified |
|
193 |
* BigInteger object. the value is formatted to fit on lines of |
|
194 |
* at least 75 characters, with embedded newlines. Words are |
|
195 |
* separated for readability, with eight words (32 bytes) per line. |
|
196 |
*/ |
|
197 |
public static String toHexString(BigInteger b) { |
|
198 |
String hexValue = b.toString(16); |
|
24969
afa6934dd8e8
8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents:
14342
diff
changeset
|
199 |
StringBuilder sb = new StringBuilder(hexValue.length()*2); |
2 | 200 |
|
201 |
if (hexValue.startsWith("-")) { |
|
24969
afa6934dd8e8
8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents:
14342
diff
changeset
|
202 |
sb.append(" -"); |
2 | 203 |
hexValue = hexValue.substring(1); |
204 |
} else { |
|
24969
afa6934dd8e8
8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents:
14342
diff
changeset
|
205 |
sb.append(" "); // four spaces |
2 | 206 |
} |
207 |
if ((hexValue.length()%2) != 0) { |
|
208 |
// add back the leading 0 |
|
209 |
hexValue = "0" + hexValue; |
|
210 |
} |
|
211 |
int i=0; |
|
212 |
while (i < hexValue.length()) { |
|
213 |
// one byte at a time |
|
24969
afa6934dd8e8
8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents:
14342
diff
changeset
|
214 |
sb.append(hexValue.substring(i, i + 2)); |
2 | 215 |
i+=2; |
216 |
if (i!= hexValue.length()) { |
|
217 |
if ((i%64) == 0) { |
|
24969
afa6934dd8e8
8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents:
14342
diff
changeset
|
218 |
sb.append("\n "); // line after eight words |
2 | 219 |
} else if (i%8 == 0) { |
24969
afa6934dd8e8
8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents:
14342
diff
changeset
|
220 |
sb.append(" "); // space between words |
2 | 221 |
} |
222 |
} |
|
223 |
} |
|
24969
afa6934dd8e8
8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents:
14342
diff
changeset
|
224 |
return sb.toString(); |
2 | 225 |
} |
226 |
||
227 |
/** |
|
228 |
* change a string into lower case except permission classes and URLs. |
|
229 |
*/ |
|
230 |
private static String marshal(String args) { |
|
231 |
if (args != null) { |
|
24969
afa6934dd8e8
8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents:
14342
diff
changeset
|
232 |
StringBuilder target = new StringBuilder(); |
2 | 233 |
StringBuffer source = new StringBuffer(args); |
234 |
||
235 |
// obtain the "permission=<classname>" options |
|
236 |
// the syntax of classname: IDENTIFIER.IDENTIFIER |
|
237 |
// the regular express to match a class name: |
|
238 |
// "[a-zA-Z_$][a-zA-Z0-9_$]*([.][a-zA-Z_$][a-zA-Z0-9_$]*)*" |
|
239 |
String keyReg = "[Pp][Ee][Rr][Mm][Ii][Ss][Ss][Ii][Oo][Nn]="; |
|
240 |
String keyStr = "permission="; |
|
241 |
String reg = keyReg + |
|
242 |
"[a-zA-Z_$][a-zA-Z0-9_$]*([.][a-zA-Z_$][a-zA-Z0-9_$]*)*"; |
|
243 |
Pattern pattern = Pattern.compile(reg); |
|
244 |
Matcher matcher = pattern.matcher(source); |
|
245 |
StringBuffer left = new StringBuffer(); |
|
246 |
while (matcher.find()) { |
|
247 |
String matched = matcher.group(); |
|
248 |
target.append(matched.replaceFirst(keyReg, keyStr)); |
|
249 |
target.append(" "); |
|
250 |
||
251 |
// delete the matched sequence |
|
252 |
matcher.appendReplacement(left, ""); |
|
253 |
} |
|
254 |
matcher.appendTail(left); |
|
255 |
source = left; |
|
256 |
||
257 |
// obtain the "codebase=<URL>" options |
|
258 |
// the syntax of URL is too flexible, and here assumes that the |
|
259 |
// URL contains no space, comma(','), and semicolon(';'). That |
|
260 |
// also means those characters also could be used as separator |
|
261 |
// after codebase option. |
|
262 |
// However, the assumption is incorrect in some special situation |
|
263 |
// when the URL contains comma or semicolon |
|
264 |
keyReg = "[Cc][Oo][Dd][Ee][Bb][Aa][Ss][Ee]="; |
|
265 |
keyStr = "codebase="; |
|
266 |
reg = keyReg + "[^, ;]*"; |
|
267 |
pattern = Pattern.compile(reg); |
|
268 |
matcher = pattern.matcher(source); |
|
269 |
left = new StringBuffer(); |
|
270 |
while (matcher.find()) { |
|
271 |
String matched = matcher.group(); |
|
272 |
target.append(matched.replaceFirst(keyReg, keyStr)); |
|
273 |
target.append(" "); |
|
274 |
||
275 |
// delete the matched sequence |
|
276 |
matcher.appendReplacement(left, ""); |
|
277 |
} |
|
278 |
matcher.appendTail(left); |
|
279 |
source = left; |
|
280 |
||
281 |
// convert the rest to lower-case characters |
|
6122
16fa7ed7ff1b
6867345: Turkish regional options cause NPE in sun.security.x509.AlgorithmId.algOID
xuelei
parents:
5506
diff
changeset
|
282 |
target.append(source.toString().toLowerCase(Locale.ENGLISH)); |
2 | 283 |
|
284 |
return target.toString(); |
|
285 |
} |
|
286 |
||
287 |
return null; |
|
288 |
} |
|
289 |
||
290 |
private final static char[] hexDigits = "0123456789abcdef".toCharArray(); |
|
291 |
||
292 |
public static String toString(byte[] b) { |
|
293 |
if (b == null) { |
|
294 |
return "(null)"; |
|
295 |
} |
|
296 |
StringBuilder sb = new StringBuilder(b.length * 3); |
|
297 |
for (int i = 0; i < b.length; i++) { |
|
298 |
int k = b[i] & 0xff; |
|
299 |
if (i != 0) { |
|
300 |
sb.append(':'); |
|
301 |
} |
|
302 |
sb.append(hexDigits[k >>> 4]); |
|
303 |
sb.append(hexDigits[k & 0xf]); |
|
304 |
} |
|
305 |
return sb.toString(); |
|
306 |
} |
|
307 |
||
308 |
} |