8343395: SSLLogger doesn't work for formatted messages

Reviewed-by: weijun
This commit is contained in:
Sean Coffey 2025-09-22 18:41:07 +00:00
parent bdfe05b595
commit 47efe3c794
3 changed files with 28 additions and 22 deletions

View File

@ -46,6 +46,7 @@ import sun.security.util.Debug;
import sun.security.x509.*;
import static java.nio.charset.StandardCharsets.UTF_8;
import static sun.security.ssl.Utilities.LINE_SEP;
/**
* Implementation of SSL logger.
@ -62,6 +63,7 @@ public final class SSLLogger {
private static final String property;
public static final boolean isOn;
static {
String p = System.getProperty("javax.net.debug");
if (p != null) {
@ -190,7 +192,12 @@ public final class SSLLogger {
try {
String formatted =
SSLSimpleFormatter.formatParameters(params);
logger.log(level, msg, formatted);
// use the customized log method for SSLConsoleLogger
if (logger instanceof SSLConsoleLogger) {
logger.log(level, msg, formatted);
} else {
logger.log(level, msg + ":" + LINE_SEP + formatted);
}
} catch (Exception exp) {
// ignore it, just for debugging.
}
@ -282,7 +289,7 @@ public final class SSLLogger {
""",
Locale.ENGLISH);
private static final MessageFormat extendedCertFormart =
private static final MessageFormat extendedCertFormat =
new MessageFormat(
"""
"version" : "v{0}",
@ -299,15 +306,6 @@ public final class SSLLogger {
""",
Locale.ENGLISH);
//
// private static MessageFormat certExtFormat = new MessageFormat(
// "{0} [{1}] '{'\n" +
// " critical: {2}\n" +
// " value: {3}\n" +
// "'}'",
// Locale.ENGLISH);
//
private static final MessageFormat messageFormatNoParas =
new MessageFormat(
"""
@ -325,7 +323,7 @@ public final class SSLLogger {
private static final MessageFormat messageCompactFormatNoParas =
new MessageFormat(
"{0}|{1}|{2}|{3}|{4}|{5}|{6}\n",
"{0}|{1}|{2}|{3}|{4}|{5}|{6}" + LINE_SEP,
Locale.ENGLISH);
private static final MessageFormat messageFormatWithParas =
@ -423,7 +421,7 @@ public final class SSLLogger {
if (isFirst) {
isFirst = false;
} else {
builder.append(",\n");
builder.append("," + LINE_SEP);
}
if (parameter instanceof Throwable) {
@ -504,10 +502,10 @@ public final class SSLLogger {
if (isFirst) {
isFirst = false;
} else {
extBuilder.append(",\n");
extBuilder.append("," + LINE_SEP);
}
extBuilder.append("{\n" +
Utilities.indent(certExt.toString()) + "\n}");
extBuilder.append("{" + LINE_SEP +
Utilities.indent(certExt.toString()) + LINE_SEP +"}");
}
Object[] certFields = {
x509.getVersion(),
@ -521,7 +519,7 @@ public final class SSLLogger {
Utilities.indent(extBuilder.toString())
};
builder.append(Utilities.indent(
extendedCertFormart.format(certFields)));
extendedCertFormat.format(certFields)));
}
} catch (Exception ce) {
// ignore the exception
@ -578,7 +576,7 @@ public final class SSLLogger {
// "string c"
// ]
StringBuilder builder = new StringBuilder(512);
builder.append("\"" + key + "\": [\n");
builder.append("\"" + key + "\": [" + LINE_SEP);
int len = strings.length;
for (int i = 0; i < len; i++) {
String string = strings[i];
@ -586,7 +584,7 @@ public final class SSLLogger {
if (i != len - 1) {
builder.append(",");
}
builder.append("\n");
builder.append(LINE_SEP);
}
builder.append(" ]");

View File

@ -40,6 +40,7 @@ final class Utilities {
Pattern.compile("\\r\\n|\\n|\\r");
private static final HexFormat HEX_FORMATTER =
HexFormat.of().withUpperCase();
static final String LINE_SEP = System.lineSeparator();
/**
* Puts {@code hostname} into the {@code serverNames} list.
@ -150,7 +151,7 @@ final class Utilities {
static String indent(String source, String prefix) {
StringBuilder builder = new StringBuilder();
if (source == null) {
builder.append("\n").append(prefix).append("<blank message>");
builder.append(LINE_SEP).append(prefix).append("<blank message>");
} else {
String[] lines = lineBreakPatern.split(source);
boolean isFirst = true;
@ -158,7 +159,7 @@ final class Utilities {
if (isFirst) {
isFirst = false;
} else {
builder.append("\n");
builder.append(LINE_SEP);
}
builder.append(prefix).append(line);
}

View File

@ -70,9 +70,11 @@ public class DebugPropertyValuesTest extends SSLSocketTemplate {
debugMessages.put("verbose", List.of("Ignore unsupported cipher suite:"));
debugMessages.put("handshake-expand",
List.of("\"logger\".*: \"javax.net.ssl\",",
"\"specifics\" : \\[",
"\"message\".*: \"Produced ClientHello handshake message"));
debugMessages.put("record-expand",
List.of("\"logger\".*: \"javax.net.ssl\",",
"\"specifics\" : \\[",
"\"message\".*: \"READ: TLSv1.2 application_data"));
debugMessages.put("help",
List.of("print the help messages",
@ -83,7 +85,12 @@ public class DebugPropertyValuesTest extends SSLSocketTemplate {
// "ALL" shouldn't be seen as a valid Level
debugMessages.put("javax.net.debug.logger.ALL", List.of("ALL:"));
debugMessages.put("javax.net.debug.logger",
List.of("FINE: adding as trusted certificates",
List.of("FINE: adding as trusted certificates:"
+ System.lineSeparator() +
" \"certificate\" : \\{",
"FINE: Produced ClientHello handshake message:" +
System.lineSeparator() +
"\"ClientHello\": \\{",
"FINE: WRITE: TLSv1.3 application_data"));
}