Skip to content

Commit 88ecbd0

Browse files
committed
Add KeyUsage, ExtendedKeyUsage, CipherSuite & Protocol to SSL diagnostics
1 parent 9d55cbd commit 88ecbd0

File tree

2 files changed

+84
-20
lines changed

2 files changed

+84
-20
lines changed

libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/SslDiagnostics.java

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.Map;
3434
import java.util.Optional;
3535
import java.util.stream.Collectors;
36+
import java.util.Arrays;
3637

3738
public class SslDiagnostics {
3839

@@ -178,7 +179,13 @@ public static String getTrustDiagnosticFailure(X509Certificate[] chain, PeerType
178179
.append(" provided a certificate with subject name [")
179180
.append(peerCert.getSubjectX500Principal().getName())
180181
.append("] and ")
181-
.append(fingerprintDescription(peerCert));
182+
.append(fingerprintDescription(peerCert))
183+
.append(" and ")
184+
.append(keyUsageDescription(peerCert))
185+
.append(" and ")
186+
.append(extendedKeyUsageDescription(peerCert));
187+
188+
addSessionDescription(session, message);
182189

183190
if (peerType == PeerType.SERVER) {
184191
try {
@@ -406,4 +413,42 @@ private static boolean checkIssuer(X509Certificate certificate, X509Certificate
406413
private static boolean isSelfIssued(X509Certificate certificate) {
407414
return certificate.getIssuerX500Principal().equals(certificate.getSubjectX500Principal());
408415
}
416+
417+
private static String keyUsageDescription(X509Certificate certificate) {
418+
return Optional.ofNullable(certificate.getKeyUsage())
419+
.map(keyUsage -> "keyUsage [" + Arrays.toString(keyUsage) + "]")
420+
.orElse("no keyUsage");
421+
}
422+
423+
private static String extendedKeyUsageDescription(X509Certificate certificate) {
424+
try {
425+
return Optional.ofNullable(certificate.getExtendedKeyUsage())
426+
.map(list -> generateExtendedKeyUsageDescription(list))
427+
.orElse("no extendedKeyUsage");
428+
} catch (CertificateParsingException e) {
429+
return "invalid extendedKeyUsage [" + e.toString() + "]";
430+
}
431+
}
432+
433+
private static String generateExtendedKeyUsageDescription(List<String> list) {
434+
return list.stream()
435+
.reduce((x, y) -> x + ", " + y)
436+
.map(str -> "extendedKeyUsage [" + str + "]")
437+
.orElse("no extendedKeyUsage");
438+
}
439+
440+
private static void addSessionDescription(SSLSession session, StringBuilder message) {
441+
String cipherSuite = Optional.ofNullable(session)
442+
.map(SSLSession::getCipherSuite)
443+
.orElse("<unknown cipherSuite>");
444+
String protocol = Optional.ofNullable(session)
445+
.map(SSLSession::getProtocol)
446+
.orElse("<unknown protocol>");
447+
message.append("; the session supports the cipher suite [")
448+
.append(cipherSuite)
449+
.append("] and ")
450+
.append("the protocol [")
451+
.append(protocol)
452+
.append("]");
453+
}
409454
}

0 commit comments

Comments
 (0)