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