Skip to content

Commit bf979d6

Browse files
committed
Improved diagnostics for TLS trust failures
- Improves HTTP client hostname verification failure messages - Adds "DiagnosticTrustManager" which logs certificate information when trust cannot be established (hostname failure, CA path failure, etc) These diagnostic messages are designed so that many common TLS problems can be diagnosed based solely (or primarily) on the elasticsearch logs. These diagnostics can be disabled by setting xpack.security.ssl.diagnose.trust: false Backport of: elastic#48911
1 parent 9cb1ace commit bf979d6

File tree

31 files changed

+1571
-80
lines changed

31 files changed

+1571
-80
lines changed

docs/reference/settings/security-settings.asciidoc

+10
Original file line numberDiff line numberDiff line change
@@ -1496,6 +1496,16 @@ through the list of URLs will continue until a successful connection is made.
14961496

14971497
[float]
14981498
[[ssl-tls-settings]]
1499+
==== General TLS settings
1500+
`xpack.security.ssl.diagnose.trust`::
1501+
Controls whether to output diagnostic messages for SSL/TLS trust failures.
1502+
If this is `true` (the default), a message will be printed to the Elasticsearch
1503+
log whenever an SSL connection (incoming or outgoing) is rejected due to a failure
1504+
to establish trust.
1505+
This diagnostic message contains information that can be used to determine the
1506+
cause of the failure and assist with resolving the problem.
1507+
Set to `false` to disable these messages.
1508+
14991509
==== Default values for TLS/SSL settings
15001510
In general, the values below represent the default values for the various TLS
15011511
settings.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.common.ssl;
21+
22+
import javax.net.ssl.SSLEngine;
23+
import javax.net.ssl.SSLSession;
24+
import javax.net.ssl.SSLSocket;
25+
import javax.net.ssl.X509ExtendedTrustManager;
26+
import java.net.Socket;
27+
import java.security.GeneralSecurityException;
28+
import java.security.cert.CertificateException;
29+
import java.security.cert.X509Certificate;
30+
import java.util.ArrayList;
31+
import java.util.List;
32+
import java.util.Map;
33+
import java.util.function.Supplier;
34+
import java.util.stream.Collectors;
35+
import java.util.stream.Stream;
36+
37+
import static org.elasticsearch.common.ssl.SslDiagnostics.getTrustDiagnosticFailure;
38+
39+
public final class DiagnosticTrustManager extends X509ExtendedTrustManager {
40+
41+
42+
/**
43+
* This interface exists because the ssl-config library does not depend on log4j, however the whole purpose of this class is to log
44+
* diagnostic messages, so it must be provided with a function by which it can do that.
45+
*/
46+
@FunctionalInterface
47+
public interface DiagnosticLogger {
48+
void warning(String message, GeneralSecurityException cause);
49+
}
50+
51+
52+
private final X509ExtendedTrustManager delegate;
53+
private final Supplier<String> contextName;
54+
private final DiagnosticLogger logger;
55+
private final Map<String, List<X509Certificate>> issuers;
56+
57+
/**
58+
* @param contextName The descriptive name of the context that this trust manager is operating in (e.g "xpack.security.http.ssl")
59+
* @param logger For uses that depend on log4j, it is recommended that this parameter be equivalent to
60+
* {@code LogManager.getLogger(DiagnosticTrustManager.class)::warn}
61+
*/
62+
public DiagnosticTrustManager(X509ExtendedTrustManager delegate, Supplier<String> contextName, DiagnosticLogger logger) {
63+
this.delegate = delegate;
64+
this.contextName = contextName;
65+
this.logger = logger;
66+
this.issuers = Stream.of(delegate.getAcceptedIssuers())
67+
.collect(Collectors.toMap(cert -> cert.getSubjectX500Principal().getName(), List::of,
68+
(List<X509Certificate> a, List<X509Certificate> b) -> {
69+
final ArrayList<X509Certificate> list = new ArrayList<>(a.size() + b.size());
70+
list.addAll(a);
71+
list.addAll(b);
72+
return list;
73+
}));
74+
}
75+
76+
@Override
77+
public void checkClientTrusted(X509Certificate[] chain, String authType, Socket socket) throws CertificateException {
78+
try {
79+
delegate.checkClientTrusted(chain, authType, socket);
80+
} catch (CertificateException e) {
81+
diagnose(e, chain, SslDiagnostics.PeerType.CLIENT, session(socket));
82+
throw e;
83+
}
84+
}
85+
86+
@Override
87+
public void checkServerTrusted(X509Certificate[] chain, String authType, Socket socket) throws CertificateException {
88+
try {
89+
delegate.checkServerTrusted(chain, authType, socket);
90+
} catch (CertificateException e) {
91+
diagnose(e, chain, SslDiagnostics.PeerType.SERVER, session(socket));
92+
throw e;
93+
}
94+
}
95+
96+
@Override
97+
public void checkClientTrusted(X509Certificate[] chain, String authType, SSLEngine engine) throws CertificateException {
98+
try {
99+
delegate.checkClientTrusted(chain, authType, engine);
100+
} catch (CertificateException e) {
101+
diagnose(e, chain, SslDiagnostics.PeerType.CLIENT, session(engine));
102+
throw e;
103+
}
104+
}
105+
106+
@Override
107+
public void checkServerTrusted(X509Certificate[] chain, String authType, SSLEngine engine) throws CertificateException {
108+
try {
109+
delegate.checkServerTrusted(chain, authType, engine);
110+
} catch (CertificateException e) {
111+
diagnose(e, chain, SslDiagnostics.PeerType.SERVER, session(engine));
112+
throw e;
113+
}
114+
}
115+
116+
@Override
117+
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
118+
try {
119+
delegate.checkClientTrusted(chain, authType);
120+
} catch (CertificateException e) {
121+
diagnose(e, chain, SslDiagnostics.PeerType.CLIENT, null);
122+
throw e;
123+
}
124+
}
125+
126+
@Override
127+
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
128+
try {
129+
delegate.checkServerTrusted(chain, authType);
130+
} catch (CertificateException e) {
131+
diagnose(e, chain, SslDiagnostics.PeerType.SERVER, null);
132+
throw e;
133+
}
134+
}
135+
136+
@Override
137+
public X509Certificate[] getAcceptedIssuers() {
138+
return delegate.getAcceptedIssuers();
139+
}
140+
141+
private void diagnose(CertificateException cause, X509Certificate[] chain, SslDiagnostics.PeerType peerType, SSLSession session) {
142+
final String diagnostic = getTrustDiagnosticFailure(chain, peerType, session, this.contextName.get(), this.issuers);
143+
logger.warning(diagnostic, cause);
144+
}
145+
146+
private SSLSession session(Socket socket) {
147+
if (socket instanceof SSLSocket) {
148+
final SSLSocket ssl = (SSLSocket) socket;
149+
final SSLSession handshakeSession = ssl.getHandshakeSession();
150+
if (handshakeSession == null) {
151+
return ssl.getSession();
152+
} else {
153+
return handshakeSession;
154+
}
155+
} else {
156+
return null;
157+
}
158+
}
159+
160+
private SSLSession session(SSLEngine engine) {
161+
return engine.getHandshakeSession();
162+
}
163+
}

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

+1-9
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
import java.security.KeyFactory;
4242
import java.security.KeyPairGenerator;
4343
import java.security.MessageDigest;
44-
import java.security.NoSuchAlgorithmException;
4544
import java.security.PrivateKey;
4645
import java.security.cert.Certificate;
4746
import java.security.cert.CertificateException;
@@ -452,7 +451,7 @@ private static Cipher getCipherFromParameters(String dekHeaderValue, char[] pass
452451
*/
453452
private static byte[] generateOpenSslKey(char[] password, byte[] salt, int keyLength) {
454453
byte[] passwordBytes = CharArrays.toUtf8Bytes(password);
455-
MessageDigest md5 = messageDigest("md5");
454+
MessageDigest md5 = SslUtil.messageDigest("md5");
456455
byte[] key = new byte[keyLength];
457456
int copied = 0;
458457
int remaining;
@@ -603,11 +602,4 @@ static List<Certificate> readCertificates(Collection<Path> certPaths) throws Cer
603602
return certificates;
604603
}
605604

606-
private static MessageDigest messageDigest(String digestAlgorithm) {
607-
try {
608-
return MessageDigest.getInstance(digestAlgorithm);
609-
} catch (NoSuchAlgorithmException e) {
610-
throw new SslConfigException("unexpected exception creating MessageDigest instance for [" + digestAlgorithm + "]", e);
611-
}
612-
}
613605
}

0 commit comments

Comments
 (0)