-
Notifications
You must be signed in to change notification settings - Fork 25k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improved diagnostics for TLS trust failures #48911
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9095e29
Improved diagnostics for TLS trust failures
tvernum 20a23ec
Merge branch 'master' into tls-incorrect-hostname
tvernum 55ddc9d
Address feedback
tvernum b56ab65
Rename setting and add test
tvernum e369c87
Remove empty line
tvernum 3ffb896
Fix javadoc typo
tvernum 8944899
Document new diagnose.trust setting
tvernum File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
163 changes: 163 additions & 0 deletions
163
libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/DiagnosticTrustManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.common.ssl; | ||
|
||
import javax.net.ssl.SSLEngine; | ||
import javax.net.ssl.SSLSession; | ||
import javax.net.ssl.SSLSocket; | ||
import javax.net.ssl.X509ExtendedTrustManager; | ||
import java.net.Socket; | ||
import java.security.GeneralSecurityException; | ||
import java.security.cert.CertificateException; | ||
import java.security.cert.X509Certificate; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.function.Supplier; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
import static org.elasticsearch.common.ssl.SslDiagnostics.getTrustDiagnosticFailure; | ||
|
||
public final class DiagnosticTrustManager extends X509ExtendedTrustManager { | ||
|
||
|
||
/** | ||
* This interface exists because the ssl-config library does not depend on log4j, however the whole purpose of this class is to log | ||
* diagnostic messages, so it must be provided with a function by which it can do that. | ||
*/ | ||
@FunctionalInterface | ||
public interface DiagnosticLogger { | ||
void warning(String message, GeneralSecurityException cause); | ||
} | ||
|
||
|
||
private final X509ExtendedTrustManager delegate; | ||
private final Supplier<String> contextName; | ||
private final DiagnosticLogger logger; | ||
private final Map<String, List<X509Certificate>> issuers; | ||
|
||
/** | ||
* @param contextName The descriptive name of the context that this trust manager is operating in (e.g "xpack.security.http.ssl") | ||
* @param logger For uses that depend on log4j, it is recommended that this parameter be equivalent to | ||
* {@code LogManager.getLogger(DiagnosticTrustManager.class)::warn} | ||
*/ | ||
public DiagnosticTrustManager(X509ExtendedTrustManager delegate, Supplier<String> contextName, DiagnosticLogger logger) { | ||
this.delegate = delegate; | ||
this.contextName = contextName; | ||
this.logger = logger; | ||
this.issuers = Stream.of(delegate.getAcceptedIssuers()) | ||
.collect(Collectors.toMap(cert -> cert.getSubjectX500Principal().getName(), List::of, | ||
(List<X509Certificate> a, List<X509Certificate> b) -> { | ||
final ArrayList<X509Certificate> list = new ArrayList<>(a.size() + b.size()); | ||
list.addAll(a); | ||
list.addAll(b); | ||
return list; | ||
})); | ||
} | ||
|
||
@Override | ||
public void checkClientTrusted(X509Certificate[] chain, String authType, Socket socket) throws CertificateException { | ||
try { | ||
delegate.checkClientTrusted(chain, authType, socket); | ||
} catch (CertificateException e) { | ||
diagnose(e, chain, SslDiagnostics.PeerType.CLIENT, session(socket)); | ||
throw e; | ||
} | ||
} | ||
|
||
@Override | ||
public void checkServerTrusted(X509Certificate[] chain, String authType, Socket socket) throws CertificateException { | ||
try { | ||
delegate.checkServerTrusted(chain, authType, socket); | ||
} catch (CertificateException e) { | ||
diagnose(e, chain, SslDiagnostics.PeerType.SERVER, session(socket)); | ||
throw e; | ||
} | ||
} | ||
|
||
@Override | ||
public void checkClientTrusted(X509Certificate[] chain, String authType, SSLEngine engine) throws CertificateException { | ||
try { | ||
delegate.checkClientTrusted(chain, authType, engine); | ||
} catch (CertificateException e) { | ||
diagnose(e, chain, SslDiagnostics.PeerType.CLIENT, session(engine)); | ||
throw e; | ||
} | ||
} | ||
|
||
@Override | ||
public void checkServerTrusted(X509Certificate[] chain, String authType, SSLEngine engine) throws CertificateException { | ||
try { | ||
delegate.checkServerTrusted(chain, authType, engine); | ||
} catch (CertificateException e) { | ||
diagnose(e, chain, SslDiagnostics.PeerType.SERVER, session(engine)); | ||
throw e; | ||
} | ||
} | ||
|
||
@Override | ||
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { | ||
try { | ||
delegate.checkClientTrusted(chain, authType); | ||
} catch (CertificateException e) { | ||
diagnose(e, chain, SslDiagnostics.PeerType.CLIENT, null); | ||
throw e; | ||
} | ||
} | ||
|
||
@Override | ||
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { | ||
try { | ||
delegate.checkServerTrusted(chain, authType); | ||
} catch (CertificateException e) { | ||
diagnose(e, chain, SslDiagnostics.PeerType.SERVER, null); | ||
throw e; | ||
} | ||
} | ||
|
||
@Override | ||
public X509Certificate[] getAcceptedIssuers() { | ||
return delegate.getAcceptedIssuers(); | ||
} | ||
|
||
private void diagnose(CertificateException cause, X509Certificate[] chain, SslDiagnostics.PeerType peerType, SSLSession session) { | ||
final String diagnostic = getTrustDiagnosticFailure(chain, peerType, session, this.contextName.get(), this.issuers); | ||
logger.warning(diagnostic, cause); | ||
} | ||
|
||
private SSLSession session(Socket socket) { | ||
if (socket instanceof SSLSocket) { | ||
final SSLSocket ssl = (SSLSocket) socket; | ||
final SSLSession handshakeSession = ssl.getHandshakeSession(); | ||
if (handshakeSession == null) { | ||
return ssl.getSession(); | ||
} else { | ||
return handshakeSession; | ||
} | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
private SSLSession session(SSLEngine engine) { | ||
return engine.getHandshakeSession(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Slightly unrelated but how do we plan to handle the divergence between this and
x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/PemUtils.java
? I remember having this question in another PR but can't remember if I raised it and if we discussed itThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure. I think my next round of SSL work will be to make x-pack depend on ssl-config for more things.