Skip to content

Commit f6af049

Browse files
committed
Review comments
* Fix formatting * Add descriptive keyUsage, extendedKeyUsage msgs * Mock cert's keyUsage, extendedKeyUsage * Mock session's cipherSuite, protocol
1 parent 88ecbd0 commit f6af049

File tree

2 files changed

+96
-51
lines changed

2 files changed

+96
-51
lines changed

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

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.util.Optional;
3535
import java.util.stream.Collectors;
3636
import java.util.Arrays;
37+
import java.util.stream.IntStream;
3738

3839
public class SslDiagnostics {
3940

@@ -152,6 +153,30 @@ boolean isSameCertificate() {
152153
}
153154
}
154155

156+
public enum ExtendedKeyUsage {
157+
serverAuth ("1.3.6.1.5.5.7.3.1"),
158+
clientAuth ("1.3.6.1.5.5.7.3.2"),
159+
codeSigning ("1.3.6.1.5.5.7.3.3"),
160+
emailProtection ("1.3.6.1.5.5.7.3.4"),
161+
timeStamping ("1.3.6.1.5.5.7.3.8"),
162+
ocspSigning ("1.3.6.1.5.5.7.3.9");
163+
164+
private String oid;
165+
166+
private ExtendedKeyUsage(String oid) {
167+
this.oid = oid;
168+
}
169+
170+
public static String decodeOid(String oid) {
171+
for (ExtendedKeyUsage e : values()) {
172+
if (e.oid != null && e.oid.equals(oid)) {
173+
return e.name();
174+
}
175+
}
176+
return oid;
177+
}
178+
}
179+
155180
/**
156181
* @param contextName The descriptive name of this SSL context (e.g. "xpack.security.transport.ssl")
157182
* @param trustedIssuers A Map of DN to Certificate, for the issuers that were trusted in the context in which this failure occurred
@@ -178,9 +203,9 @@ public static String getTrustDiagnosticFailure(X509Certificate[] chain, PeerType
178203
.append(peerType.name().toLowerCase(Locale.ROOT))
179204
.append(" provided a certificate with subject name [")
180205
.append(peerCert.getSubjectX500Principal().getName())
181-
.append("] and ")
206+
.append("], ")
182207
.append(fingerprintDescription(peerCert))
183-
.append(" and ")
208+
.append(", ")
184209
.append(keyUsageDescription(peerCert))
185210
.append(" and ")
186211
.append(extendedKeyUsageDescription(peerCert));
@@ -415,23 +440,40 @@ private static boolean isSelfIssued(X509Certificate certificate) {
415440
}
416441

417442
private static String keyUsageDescription(X509Certificate certificate) {
418-
return Optional.ofNullable(certificate.getKeyUsage())
419-
.map(keyUsage -> "keyUsage [" + Arrays.toString(keyUsage) + "]")
443+
boolean[] keyUsage = certificate.getKeyUsage();
444+
if (keyUsage == null || keyUsage.length == 0) {
445+
return "no keyUsage";
446+
}
447+
448+
final String[] keyUsageGlossary = {"digitalSignature", "nonRepudiation", "keyEncipherment",
449+
"dataEncipherment", "keyAgreement", "keyCertSign", "cRLSign", "encipherOnly",
450+
"decipherOnly"};
451+
452+
List<String> keyUsageDescription = new ArrayList<>();
453+
IntStream.range(0, keyUsage.length).forEach(i -> {
454+
if (keyUsage[i]) {
455+
keyUsageDescription.add(keyUsageGlossary[i]);
456+
}
457+
});
458+
return keyUsageDescription.stream()
459+
.reduce((a, b) -> a + ", " + b)
460+
.map(str -> "keyUsage [" + str + "]")
420461
.orElse("no keyUsage");
421462
}
422463

423464
private static String extendedKeyUsageDescription(X509Certificate certificate) {
424465
try {
425466
return Optional.ofNullable(certificate.getExtendedKeyUsage())
426-
.map(list -> generateExtendedKeyUsageDescription(list))
467+
.map(keyUsage -> generateExtendedKeyUsageDescription(keyUsage))
427468
.orElse("no extendedKeyUsage");
428469
} catch (CertificateParsingException e) {
429470
return "invalid extendedKeyUsage [" + e.toString() + "]";
430471
}
431472
}
432473

433-
private static String generateExtendedKeyUsageDescription(List<String> list) {
434-
return list.stream()
474+
private static String generateExtendedKeyUsageDescription(List<String> oids) {
475+
return oids.stream()
476+
.map(ExtendedKeyUsage::decodeOid)
435477
.reduce((x, y) -> x + ", " + y)
436478
.map(str -> "extendedKeyUsage [" + str + "]")
437479
.orElse("no extendedKeyUsage");
@@ -444,10 +486,9 @@ private static void addSessionDescription(SSLSession session, StringBuilder mess
444486
String protocol = Optional.ofNullable(session)
445487
.map(SSLSession::getProtocol)
446488
.orElse("<unknown protocol>");
447-
message.append("; the session supports the cipher suite [")
489+
message.append("; the session supports cipher suite [")
448490
.append(cipherSuite)
449-
.append("] and ")
450-
.append("the protocol [")
491+
.append("] and protocol [")
451492
.append(protocol)
452493
.append("]");
453494
}

0 commit comments

Comments
 (0)