From ecc238bea5987fb7e71710230c923ab6a6dba37a Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Thu, 28 Jun 2018 11:07:13 +0200 Subject: [PATCH] Only try to call SSL.setHostnameValidation(...) if needed. (#8074) Motivation: As the used OpenSSL version may not support hostname validation we should only really call SSL.setHostNameValidation(...) if we detect that its needed. Modifications: Only call SSL.setHostNameValidation if it was disabled before and now it needs to be enabled or if it was enabled before and it should be disabled now. Result: Less risk of an exception when using an OpenSSL version that does not support hostname validation. --- .../ssl/ReferenceCountedOpenSslEngine.java | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/handler/src/main/java/io/netty/handler/ssl/ReferenceCountedOpenSslEngine.java b/handler/src/main/java/io/netty/handler/ssl/ReferenceCountedOpenSslEngine.java index 658bb01558..94d45d109b 100644 --- a/handler/src/main/java/io/netty/handler/ssl/ReferenceCountedOpenSslEngine.java +++ b/handler/src/main/java/io/netty/handler/ssl/ReferenceCountedOpenSslEngine.java @@ -1783,10 +1783,20 @@ public class ReferenceCountedOpenSslEngine extends SSLEngine implements Referenc } final String endPointIdentificationAlgorithm = sslParameters.getEndpointIdentificationAlgorithm(); - final boolean endPointVerificationEnabled = endPointIdentificationAlgorithm != null && - !endPointIdentificationAlgorithm.isEmpty(); - SSL.setHostNameValidation(ssl, DEFAULT_HOSTNAME_VALIDATION_FLAGS, - endPointVerificationEnabled ? getPeerHost() : null); + final boolean endPointVerificationEnabled = isEndPointVerificationEnabled(endPointIdentificationAlgorithm); + + final boolean wasEndPointVerificationEnabled = + isEndPointVerificationEnabled(this.endPointIdentificationAlgorithm); + + if (wasEndPointVerificationEnabled && !endPointVerificationEnabled) { + // Passing in null will disable hostname verification again so only do so if it was enabled before. + SSL.setHostNameValidation(ssl, DEFAULT_HOSTNAME_VALIDATION_FLAGS, null); + } else { + String host = endPointVerificationEnabled ? getPeerHost() : null; + if (host != null && !host.isEmpty()) { + SSL.setHostNameValidation(ssl, DEFAULT_HOSTNAME_VALIDATION_FLAGS, host); + } + } // If the user asks for hostname verification we must ensure we verify the peer. // If the user disables hostname verification we leave it up to the user to change the mode manually. if (clientMode && endPointVerificationEnabled) { @@ -1799,6 +1809,10 @@ public class ReferenceCountedOpenSslEngine extends SSLEngine implements Referenc super.setSSLParameters(sslParameters); } + private static boolean isEndPointVerificationEnabled(String endPointIdentificationAlgorithm) { + return endPointIdentificationAlgorithm != null && !endPointIdentificationAlgorithm.isEmpty(); + } + private boolean isDestroyed() { return destroyed != 0; }