|
33 | 33 | import java.util.Map; |
34 | 34 | import java.util.Optional; |
35 | 35 | import java.util.stream.Collectors; |
| 36 | +import java.util.Arrays; |
36 | 37 |
|
37 | 38 | public class SslDiagnostics { |
38 | 39 |
|
@@ -178,7 +179,13 @@ public static String getTrustDiagnosticFailure(X509Certificate[] chain, PeerType |
178 | 179 | .append(" provided a certificate with subject name [") |
179 | 180 | .append(peerCert.getSubjectX500Principal().getName()) |
180 | 181 | .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); |
182 | 189 |
|
183 | 190 | if (peerType == PeerType.SERVER) { |
184 | 191 | try { |
@@ -406,4 +413,42 @@ private static boolean checkIssuer(X509Certificate certificate, X509Certificate |
406 | 413 | private static boolean isSelfIssued(X509Certificate certificate) { |
407 | 414 | return certificate.getIssuerX500Principal().equals(certificate.getSubjectX500Principal()); |
408 | 415 | } |
| 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 | + } |
409 | 454 | } |
0 commit comments