author | wetmore |
Fri, 11 May 2018 15:53:12 -0700 | |
branch | JDK-8145252-TLS13-branch |
changeset 56542 | 56aaa6cb3693 |
parent 47216 | src/java.base/share/classes/sun/security/ssl/Debug.java@71c04702a3d5 |
child 56717 | e4fe7c97b1de |
permissions | -rwxr-xr-x |
2 | 1 |
/* |
56542 | 2 |
* Copyright (c) 2018, 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.ssl; |
|
27 |
||
56542 | 28 |
import java.io.ByteArrayInputStream; |
29 |
import java.io.ByteArrayOutputStream; |
|
30 |
import java.io.IOException; |
|
2 | 31 |
import java.io.PrintStream; |
56542 | 32 |
import java.lang.System.Logger; |
33 |
import java.lang.System.Logger.Level; |
|
34 |
import java.nio.ByteBuffer; |
|
35 |
import java.security.cert.Certificate; |
|
36 |
import java.security.cert.Extension; |
|
37 |
import java.security.cert.X509Certificate; |
|
38 |
import java.text.MessageFormat; |
|
39 |
import java.text.SimpleDateFormat; |
|
40 |
import java.util.Date; |
|
7039 | 41 |
import java.util.Locale; |
56542 | 42 |
import java.util.Map; |
43 |
import java.util.ResourceBundle; |
|
44 |
import sun.security.action.GetPropertyAction; |
|
34687
d302ed125dc9
8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents:
30904
diff
changeset
|
45 |
import sun.security.util.HexDumpEncoder; |
56542 | 46 |
import sun.security.x509.*; |
2 | 47 |
|
48 |
/** |
|
56542 | 49 |
* Implementation of SSL logger. |
2 | 50 |
* |
56542 | 51 |
* If the system property "javax.net.debug" is not defined, the debug logging |
52 |
* is turned off. If the system property "javax.net.debug" is defined as |
|
53 |
* empty, the debug logger is specified by System.getLogger("javax.net.ssl"), |
|
54 |
* and applications can customize and configure the logger or use external |
|
55 |
* logging mechanisms. If the system property "javax.net.debug" is defined |
|
56 |
* and non-empty, a private debug logger implemented in this class is used. |
|
2 | 57 |
*/ |
56542 | 58 |
public final class SSLLogger { |
59 |
private static final System.Logger logger; |
|
60 |
private static final String property; |
|
61 |
public static final boolean isOn; |
|
2 | 62 |
|
63 |
static { |
|
56542 | 64 |
String p = GetPropertyAction.privilegedGetProperty("javax.net.debug"); |
65 |
if (p != null) { |
|
66 |
if (p.isEmpty()) { |
|
67 |
property = ""; |
|
68 |
logger = System.getLogger("javax.net.ssl"); |
|
69 |
} else { |
|
70 |
property = p.toLowerCase(Locale.ENGLISH); |
|
71 |
if (property.equals("help")) { |
|
72 |
help(); |
|
73 |
} |
|
74 |
||
75 |
logger = new SSLConsoleLogger("javax.net.ssl", p); |
|
76 |
} |
|
77 |
isOn = true; |
|
78 |
} else { |
|
79 |
property = null; |
|
80 |
logger = null; |
|
81 |
isOn = false; |
|
2 | 82 |
} |
83 |
} |
|
84 |
||
56542 | 85 |
private static void help() { |
86 |
System.err.println(); |
|
87 |
System.err.println("help print the help messages"); |
|
88 |
System.err.println("expand expand debugging information"); |
|
2 | 89 |
System.err.println(); |
90 |
System.err.println("all turn on all debugging"); |
|
91 |
System.err.println("ssl turn on ssl debugging"); |
|
92 |
System.err.println(); |
|
93 |
System.err.println("The following can be used with ssl:"); |
|
94 |
System.err.println("\trecord enable per-record tracing"); |
|
95 |
System.err.println("\thandshake print each handshake message"); |
|
96 |
System.err.println("\tkeygen print key generation data"); |
|
97 |
System.err.println("\tsession print session activity"); |
|
98 |
System.err.println("\tdefaultctx print default SSL initialization"); |
|
99 |
System.err.println("\tsslctx print SSLContext tracing"); |
|
100 |
System.err.println("\tsessioncache print session cache tracing"); |
|
101 |
System.err.println("\tkeymanager print key manager tracing"); |
|
102 |
System.err.println("\ttrustmanager print trust manager tracing"); |
|
103 |
System.err.println("\tpluggability print pluggability tracing"); |
|
104 |
System.err.println(); |
|
105 |
System.err.println("\thandshake debugging can be widened with:"); |
|
106 |
System.err.println("\tdata hex dump of each handshake message"); |
|
107 |
System.err.println("\tverbose verbose handshake message printing"); |
|
108 |
System.err.println(); |
|
109 |
System.err.println("\trecord debugging can be widened with:"); |
|
110 |
System.err.println("\tplaintext hex dump of record plaintext"); |
|
111 |
System.err.println("\tpacket print raw SSL/TLS packets"); |
|
112 |
System.err.println(); |
|
113 |
System.exit(0); |
|
114 |
} |
|
115 |
||
116 |
/** |
|
56542 | 117 |
* Return true if the "javax.net.debug" property contains the |
118 |
* debug check points, or System.Logger is used. |
|
2 | 119 |
*/ |
56542 | 120 |
public static boolean isOn(String checkPoints) { |
121 |
if (property == null) { // debugging is turned off |
|
122 |
return false; |
|
123 |
} else if (property.isEmpty()) { // use System.Logger |
|
124 |
return true; |
|
125 |
} // use provider logger |
|
2 | 126 |
|
56542 | 127 |
String[] options = checkPoints.split(","); |
128 |
for (String option : options) { |
|
129 |
option = option.trim(); |
|
130 |
if (!SSLLogger.hasOption(option)) { |
|
131 |
return false; |
|
132 |
} |
|
133 |
} |
|
134 |
||
135 |
return true; |
|
2 | 136 |
} |
137 |
||
56542 | 138 |
private static boolean hasOption(String option) { |
139 |
option = option.toLowerCase(Locale.ENGLISH); |
|
140 |
if (property.contains("all")) { |
|
141 |
return true; |
|
2 | 142 |
} else { |
56542 | 143 |
int offset = property.indexOf("ssl"); |
144 |
if (offset != -1 && property.indexOf("sslctx", offset) != -1) { |
|
145 |
// don't enable data and plaintext options by default |
|
146 |
if (!(option.equals("data") |
|
147 |
|| option.equals("packet") |
|
148 |
|| option.equals("plaintext"))) { |
|
149 |
return true; |
|
150 |
} |
|
151 |
} |
|
152 |
} |
|
153 |
||
154 |
return property.contains(option); |
|
155 |
} |
|
156 |
||
157 |
public static void severe(String msg, Object... params) { |
|
158 |
SSLLogger.log(Level.ERROR, msg, params); |
|
159 |
} |
|
160 |
||
161 |
public static void warning(String msg, Object... params) { |
|
162 |
SSLLogger.log(Level.WARNING, msg, params); |
|
163 |
} |
|
164 |
||
165 |
public static void info(String msg, Object... params) { |
|
166 |
SSLLogger.log(Level.INFO, msg, params); |
|
167 |
} |
|
168 |
||
169 |
public static void fine(String msg, Object... params) { |
|
170 |
SSLLogger.log(Level.DEBUG, msg, params); |
|
171 |
} |
|
172 |
||
173 |
public static void finer(String msg, Object... params) { |
|
174 |
SSLLogger.log(Level.TRACE, msg, params); |
|
175 |
} |
|
176 |
||
177 |
public static void finest(String msg, Object... params) { |
|
178 |
SSLLogger.log(Level.ALL, msg, params); |
|
179 |
} |
|
180 |
||
181 |
private static void log(Level level, String msg, Object... params) { |
|
182 |
if (logger.isLoggable(level)) { |
|
183 |
if (params == null || params.length == 0) { |
|
184 |
logger.log(level, msg); |
|
185 |
} else { |
|
186 |
try { |
|
187 |
String formatted = |
|
188 |
SSLSimpleFormatter.formatParameters(params); |
|
189 |
logger.log(level, msg, formatted); |
|
190 |
} catch (Exception exp) { |
|
191 |
// ignore it, just for debugging. |
|
192 |
} |
|
193 |
} |
|
2 | 194 |
} |
195 |
} |
|
196 |
||
56542 | 197 |
static String toString(Object... params) { |
198 |
try { |
|
199 |
return SSLSimpleFormatter.formatParameters(params); |
|
200 |
} catch (Exception exp) { |
|
201 |
return "unexpected exception thrown: " + exp.getMessage(); |
|
202 |
} |
|
203 |
} |
|
204 |
||
205 |
private static class SSLConsoleLogger implements Logger { |
|
206 |
private final String loggerName; |
|
207 |
private final boolean useCompactFormat; |
|
208 |
||
209 |
SSLConsoleLogger(String loggerName, String options) { |
|
210 |
this.loggerName = loggerName; |
|
211 |
options = options.toLowerCase(Locale.ENGLISH); |
|
212 |
this.useCompactFormat = !options.contains("expand"); |
|
213 |
} |
|
2 | 214 |
|
56542 | 215 |
@Override |
216 |
public String getName() { |
|
217 |
return loggerName; |
|
218 |
} |
|
219 |
||
220 |
@Override |
|
221 |
public boolean isLoggable(Level level) { |
|
222 |
return (level != Level.OFF); |
|
223 |
} |
|
224 |
||
225 |
@Override |
|
226 |
public void log(Level level, |
|
227 |
ResourceBundle rb, String message, Throwable thrwbl) { |
|
228 |
if (isLoggable(level)) { |
|
229 |
try { |
|
230 |
String formatted = |
|
231 |
SSLSimpleFormatter.format(this, level, message, thrwbl); |
|
232 |
System.err.write(formatted.getBytes("UTF-8")); |
|
233 |
} catch (Exception exp) { |
|
234 |
// ignore it, just for debugging. |
|
2 | 235 |
} |
236 |
} |
|
56542 | 237 |
} |
238 |
||
239 |
@Override |
|
240 |
public void log(Level level, |
|
241 |
ResourceBundle rb, String message, Object... params) { |
|
242 |
if (isLoggable(level)) { |
|
243 |
try { |
|
244 |
String formatted = |
|
245 |
SSLSimpleFormatter.format(this, level, message, params); |
|
246 |
System.err.write(formatted.getBytes("UTF-8")); |
|
247 |
} catch (Exception exp) { |
|
248 |
// ignore it, just for debugging. |
|
249 |
} |
|
250 |
} |
|
2 | 251 |
} |
252 |
} |
|
253 |
||
56542 | 254 |
private static class SSLSimpleFormatter { |
255 |
private static final ThreadLocal<SimpleDateFormat> dateFormat = |
|
256 |
new ThreadLocal<SimpleDateFormat>() { |
|
257 |
@Override protected SimpleDateFormat initialValue() { |
|
258 |
return new SimpleDateFormat( |
|
259 |
"yyyy-MM-dd kk:mm:ss.SSS z", Locale.ENGLISH); |
|
260 |
} |
|
261 |
}; |
|
2 | 262 |
|
56542 | 263 |
private static final MessageFormat basicCertFormat = new MessageFormat( |
264 |
"\"version\" : \"v{0}\",\n" + |
|
265 |
"\"serial number\" : \"{1}\",\n" + |
|
266 |
"\"signature algorithm\": \"{2}\",\n" + |
|
267 |
"\"issuer\" : \"{3}\",\n" + |
|
268 |
"\"not before\" : \"{4}\",\n" + |
|
269 |
"\"not after\" : \"{5}\",\n" + |
|
270 |
"\"subject\" : \"{6}\",\n" + |
|
271 |
"\"subject public key\" : \"{7}\"\n", |
|
272 |
Locale.ENGLISH); |
|
2 | 273 |
|
56542 | 274 |
private static final MessageFormat extendedCertFormart = |
275 |
new MessageFormat( |
|
276 |
"\"version\" : \"v{0}\",\n" + |
|
277 |
"\"serial number\" : \"{1}\",\n" + |
|
278 |
"\"signature algorithm\": \"{2}\",\n" + |
|
279 |
"\"issuer\" : \"{3}\",\n" + |
|
280 |
"\"not before\" : \"{4}\",\n" + |
|
281 |
"\"not after\" : \"{5}\",\n" + |
|
282 |
"\"subject\" : \"{6}\",\n" + |
|
283 |
"\"subject public key\" : \"{7}\",\n" + |
|
284 |
"\"extensions\" : [\n" + |
|
285 |
"{8}\n" + |
|
286 |
"]\n", |
|
287 |
Locale.ENGLISH); |
|
41820 | 288 |
|
56542 | 289 |
// |
290 |
// private static MessageFormat certExtFormat = new MessageFormat( |
|
291 |
// "{0} [{1}] '{'\n" + |
|
292 |
// " critical: {2}\n" + |
|
293 |
// " value: {3}\n" + |
|
294 |
// "'}'", |
|
295 |
// Locale.ENGLISH); |
|
296 |
// |
|
297 |
||
298 |
private static final MessageFormat messageFormatNoParas = |
|
299 |
new MessageFormat( |
|
300 |
"'{'\n" + |
|
301 |
" \"logger\" : \"{0}\",\n" + |
|
302 |
" \"level\" : \"{1}\",\n" + |
|
303 |
" \"thread id\" : \"{2}\",\n" + |
|
304 |
" \"thread name\" : \"{3}\",\n" + |
|
305 |
" \"time\" : \"{4}\",\n" + |
|
306 |
" \"caller\" : \"{5}\",\n" + |
|
307 |
" \"message\" : \"{6}\"\n" + |
|
308 |
"'}'\n", |
|
309 |
Locale.ENGLISH); |
|
310 |
||
311 |
private static final MessageFormat messageCompactFormatNoParas = |
|
312 |
new MessageFormat( |
|
313 |
"{0}|{1}|{2}|{3}|{4}|{5}|{6}\n", |
|
314 |
Locale.ENGLISH); |
|
2 | 315 |
|
56542 | 316 |
private static final MessageFormat messageFormatWithParas = |
317 |
new MessageFormat( |
|
318 |
"'{'\n" + |
|
319 |
" \"logger\" : \"{0}\",\n" + |
|
320 |
" \"level\" : \"{1}\",\n" + |
|
321 |
" \"thread id\" : \"{2}\",\n" + |
|
322 |
" \"thread name\" : \"{3}\",\n" + |
|
323 |
" \"time\" : \"{4}\",\n" + |
|
324 |
" \"caller\" : \"{5}\",\n" + |
|
325 |
" \"message\" : \"{6}\",\n" + |
|
326 |
" \"specifics\" : [\n" + |
|
327 |
"{7}\n" + |
|
328 |
" ]\n" + |
|
329 |
"'}'\n", |
|
330 |
Locale.ENGLISH); |
|
331 |
||
332 |
private static final MessageFormat messageCompactFormatWithParas = |
|
333 |
new MessageFormat( |
|
334 |
"{0}|{1}|{2}|{3}|{4}|{5}|{6} (\n" + |
|
335 |
"{7}\n" + |
|
336 |
")\n", |
|
337 |
Locale.ENGLISH); |
|
338 |
||
339 |
private static final MessageFormat keyObjectFormat = new MessageFormat( |
|
340 |
"\"{0}\" : '{'\n" + |
|
341 |
"{1}" + |
|
342 |
"'}'\n", |
|
343 |
Locale.ENGLISH); |
|
344 |
||
345 |
// INFO: [TH: 123450] 2011-08-20 23:12:32.3225 PDT |
|
346 |
// log message |
|
347 |
// log message |
|
348 |
// ... |
|
349 |
private static String format(SSLConsoleLogger logger, Level level, |
|
350 |
String message, Object ... parameters) { |
|
2 | 351 |
|
56542 | 352 |
if (parameters == null || parameters.length == 0) { |
353 |
Object[] messageFields = { |
|
354 |
logger.loggerName, |
|
355 |
level.getName(), |
|
356 |
Utilities.toHexString(Thread.currentThread().getId()), |
|
357 |
Thread.currentThread().getName(), |
|
358 |
dateFormat.get().format(new Date(System.currentTimeMillis())), |
|
359 |
formatCaller(), |
|
360 |
message |
|
361 |
}; |
|
362 |
||
363 |
if (logger.useCompactFormat) { |
|
364 |
return messageCompactFormatNoParas.format(messageFields); |
|
365 |
} else { |
|
366 |
return messageFormatNoParas.format(messageFields); |
|
367 |
} |
|
368 |
} |
|
2 | 369 |
|
56542 | 370 |
Object[] messageFields = { |
371 |
logger.loggerName, |
|
372 |
level.getName(), |
|
373 |
Utilities.toHexString(Thread.currentThread().getId()), |
|
374 |
Thread.currentThread().getName(), |
|
375 |
dateFormat.get().format(new Date(System.currentTimeMillis())), |
|
376 |
formatCaller(), |
|
377 |
message, |
|
378 |
(logger.useCompactFormat ? |
|
379 |
formatParameters(parameters) : |
|
380 |
Utilities.indent(formatParameters(parameters))) |
|
381 |
}; |
|
382 |
||
383 |
if (logger.useCompactFormat) { |
|
384 |
return messageCompactFormatWithParas.format(messageFields); |
|
385 |
} else { |
|
386 |
return messageFormatWithParas.format(messageFields); |
|
2 | 387 |
} |
388 |
} |
|
56542 | 389 |
|
390 |
private static String formatCaller() { |
|
391 |
boolean isSSLLogger = false; |
|
392 |
for (StackTraceElement ste: (new Throwable()).getStackTrace()) { |
|
393 |
String cn = ste.getClassName(); |
|
394 |
if (cn.equals("sun.security.ssl.SSLLogger")) { |
|
395 |
// May be SSLLogger.log() or SSLLogger.fine(), etc. |
|
396 |
isSSLLogger = true; |
|
397 |
} else if (isSSLLogger) { |
|
398 |
// Here is the caller to SSLLogger.fine(), etc. |
|
399 |
return ste.getFileName() + ":" + ste.getLineNumber(); |
|
400 |
} |
|
401 |
} |
|
402 |
||
403 |
return "unknown caller"; |
|
404 |
} |
|
405 |
||
406 |
private static boolean isLoggerImpl(StackTraceElement ste) { |
|
407 |
String cn = ste.getClassName(); |
|
408 |
System.out.println("class name: " + cn); |
|
409 |
return cn.equals("sun.security.ssl.SSLLogger"); |
|
410 |
} |
|
411 |
||
412 |
private static String formatParameters(Object ... parameters) { |
|
413 |
StringBuilder builder = new StringBuilder(512); |
|
414 |
boolean isFirst = true; |
|
415 |
for (Object parameter : parameters) { |
|
416 |
if (isFirst) { |
|
417 |
isFirst = false; |
|
418 |
} else { |
|
419 |
builder.append(",\n"); |
|
420 |
} |
|
2 | 421 |
|
56542 | 422 |
if (parameter instanceof Throwable) { |
423 |
builder.append(formatThrowable((Throwable)parameter)); |
|
424 |
} else if (parameter instanceof Certificate) { |
|
425 |
builder.append(formatCertificate((Certificate)parameter)); |
|
426 |
} else if (parameter instanceof ByteArrayInputStream) { |
|
427 |
builder.append(formatByteArrayInputStream( |
|
428 |
(ByteArrayInputStream)parameter)); |
|
429 |
} else if (parameter instanceof ByteBuffer) { |
|
430 |
builder.append(formatByteBuffer((ByteBuffer)parameter)); |
|
431 |
} else if (parameter instanceof byte[]) { |
|
432 |
builder.append(formatByteArrayInputStream( |
|
433 |
new ByteArrayInputStream((byte[])parameter))); |
|
434 |
} else if (parameter instanceof Map.Entry) { |
|
435 |
@SuppressWarnings("unchecked") |
|
436 |
Map.Entry<String, ?> mapParameter = |
|
437 |
(Map.Entry<String, ?>)parameter; |
|
438 |
builder.append(formatMapEntry(mapParameter)); |
|
439 |
} else { |
|
440 |
builder.append(formatObject(parameter)); |
|
441 |
} |
|
442 |
} |
|
443 |
||
444 |
return builder.toString(); |
|
2 | 445 |
} |
30904 | 446 |
|
56542 | 447 |
// "throwable": { |
448 |
// ... |
|
449 |
// } |
|
450 |
private static String formatThrowable(Throwable throwable) { |
|
451 |
StringBuilder builder = new StringBuilder(512); |
|
452 |
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); |
|
453 |
try (PrintStream out = new PrintStream(bytesOut)) { |
|
454 |
throwable.printStackTrace(out); |
|
455 |
builder.append(Utilities.indent(bytesOut.toString())); |
|
456 |
} |
|
457 |
Object[] fields = { |
|
458 |
"throwable", |
|
459 |
builder.toString() |
|
460 |
}; |
|
30904 | 461 |
|
56542 | 462 |
return keyObjectFormat.format(fields); |
463 |
} |
|
464 |
||
465 |
// "certificate": { |
|
466 |
// ... |
|
467 |
// } |
|
468 |
private static String formatCertificate(Certificate certificate) { |
|
469 |
||
470 |
if (!(certificate instanceof X509Certificate)) { |
|
471 |
return Utilities.indent(certificate.toString()); |
|
472 |
} |
|
473 |
||
474 |
StringBuilder builder = new StringBuilder(512); |
|
30904 | 475 |
try { |
56542 | 476 |
X509CertImpl x509 = |
477 |
X509CertImpl.toImpl((X509Certificate)certificate); |
|
478 |
X509CertInfo certInfo = |
|
479 |
(X509CertInfo)x509.get(X509CertImpl.NAME + "." + |
|
480 |
X509CertImpl.INFO); |
|
481 |
CertificateExtensions certExts = (CertificateExtensions) |
|
482 |
certInfo.get(X509CertInfo.EXTENSIONS); |
|
483 |
if (certExts == null) { |
|
484 |
Object[] certFields = { |
|
485 |
x509.getVersion(), |
|
486 |
Utilities.toHexString( |
|
487 |
x509.getSerialNumber().toByteArray()), |
|
488 |
x509.getSigAlgName(), |
|
489 |
x509.getIssuerX500Principal().toString(), |
|
490 |
dateFormat.get().format(x509.getNotBefore()), |
|
491 |
dateFormat.get().format(x509.getNotAfter()), |
|
492 |
x509.getSubjectX500Principal().toString(), |
|
493 |
x509.getPublicKey().getAlgorithm() |
|
494 |
}; |
|
495 |
builder.append(Utilities.indent( |
|
496 |
basicCertFormat.format(certFields))); |
|
497 |
} else { |
|
498 |
StringBuilder extBuilder = new StringBuilder(512); |
|
499 |
boolean isFirst = true; |
|
500 |
for (Extension certExt : certExts.getAllExtensions()) { |
|
501 |
if (isFirst) { |
|
502 |
isFirst = false; |
|
503 |
} else { |
|
504 |
extBuilder.append(",\n"); |
|
505 |
} |
|
506 |
extBuilder.append("{\n" + |
|
507 |
Utilities.indent(certExt.toString()) + "\n}"); |
|
508 |
} |
|
509 |
Object[] certFields = { |
|
510 |
x509.getVersion(), |
|
511 |
Utilities.toHexString( |
|
512 |
x509.getSerialNumber().toByteArray()), |
|
513 |
x509.getSigAlgName(), |
|
514 |
x509.getIssuerX500Principal().toString(), |
|
515 |
dateFormat.get().format(x509.getNotBefore()), |
|
516 |
dateFormat.get().format(x509.getNotAfter()), |
|
517 |
x509.getSubjectX500Principal().toString(), |
|
518 |
x509.getPublicKey().getAlgorithm(), |
|
519 |
Utilities.indent(extBuilder.toString()) |
|
520 |
}; |
|
521 |
builder.append(Utilities.indent( |
|
522 |
extendedCertFormart.format(certFields))); |
|
523 |
} |
|
524 |
} catch (Exception ce) { |
|
525 |
// ignore the exception |
|
30904 | 526 |
} |
56542 | 527 |
|
528 |
Object[] fields = { |
|
529 |
"certificate", |
|
530 |
builder.toString() |
|
531 |
}; |
|
532 |
||
533 |
return Utilities.indent(keyObjectFormat.format(fields)); |
|
30904 | 534 |
} |
535 |
||
56542 | 536 |
private static String formatByteArrayInputStream( |
537 |
ByteArrayInputStream bytes) { |
|
538 |
StringBuilder builder = new StringBuilder(512); |
|
30904 | 539 |
|
56542 | 540 |
try (ByteArrayOutputStream bytesOut = new ByteArrayOutputStream()) { |
541 |
HexDumpEncoder hexEncoder = new HexDumpEncoder(); |
|
542 |
hexEncoder.encodeBuffer(bytes, bytesOut); |
|
543 |
||
544 |
builder.append(Utilities.indent(bytesOut.toString())); |
|
545 |
} catch (IOException ioe) { |
|
546 |
// ignore it, just for debugging. |
|
30904 | 547 |
} |
56542 | 548 |
|
549 |
return builder.toString(); |
|
30904 | 550 |
} |
56542 | 551 |
|
552 |
private static String formatByteBuffer(ByteBuffer byteBuffer) { |
|
553 |
StringBuilder builder = new StringBuilder(512); |
|
554 |
try (ByteArrayOutputStream bytesOut = new ByteArrayOutputStream()) { |
|
555 |
HexDumpEncoder hexEncoder = new HexDumpEncoder(); |
|
556 |
hexEncoder.encodeBuffer(byteBuffer.duplicate(), bytesOut); |
|
557 |
builder.append(Utilities.indent(bytesOut.toString())); |
|
558 |
} catch (IOException ioe) { |
|
559 |
// ignore it, just for debugging. |
|
560 |
} |
|
561 |
||
562 |
return builder.toString(); |
|
563 |
} |
|
564 |
||
565 |
private static String formatMapEntry(Map.Entry<String, ?> entry) { |
|
566 |
String key = entry.getKey(); |
|
567 |
Object value = entry.getValue(); |
|
30904 | 568 |
|
56542 | 569 |
String formatted; |
570 |
if (value instanceof String) { |
|
571 |
// "key": "value" |
|
572 |
formatted = "\"" + key + "\": \"" + (String)value + "\""; |
|
573 |
} else if (value instanceof String[]) { |
|
574 |
// "key": [ "string a", |
|
575 |
// "string b", |
|
576 |
// "string c" |
|
577 |
// ] |
|
578 |
StringBuilder builder = new StringBuilder(512); |
|
579 |
String[] strings = (String[])value; |
|
580 |
builder.append("\"" + key + "\": [\n"); |
|
581 |
for (String string : strings) { |
|
582 |
builder.append(" \"" + string + "\""); |
|
583 |
if (string != strings[strings.length - 1]) { |
|
584 |
builder.append(","); |
|
585 |
} |
|
586 |
builder.append("\n"); |
|
587 |
} |
|
588 |
builder.append(" ]"); |
|
30904 | 589 |
|
56542 | 590 |
formatted = builder.toString(); |
591 |
} else if (value instanceof byte[]) { |
|
592 |
formatted = "\"" + key + "\": \"" + |
|
593 |
Utilities.toHexString((byte[])value) + "\""; |
|
594 |
} else if (value instanceof Byte) { |
|
595 |
formatted = "\"" + key + "\": \"" + |
|
596 |
Utilities.toHexString((byte)value) + "\""; |
|
597 |
} else { |
|
598 |
formatted = "\"" + key + "\": " + |
|
599 |
"\"" + value.toString() + "\""; |
|
30904 | 600 |
} |
56542 | 601 |
|
602 |
return Utilities.indent(formatted); |
|
603 |
} |
|
604 |
||
605 |
private static String formatObject(Object obj) { |
|
606 |
return obj.toString(); |
|
30904 | 607 |
} |
608 |
} |
|
2 | 609 |
} |