Skip to content
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

feat: enable hostnameVerification flag in SSL sockets #281

Merged
merged 1 commit into from
Nov 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
enable hostnameVerification flag in SSL sockets
ported from logback

qos-ch/logback@9b67089

fix #250
  • Loading branch information
tony19 committed Nov 6, 2022
commit 96402e98c679e79dfa1d6c9e6367b5242daacfa5
1 change: 1 addition & 0 deletions logback-android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ dependencies {
testImplementation(group: 'org.subethamail', name: 'subethasmtp', version: '3.1.7') {
exclude(module: 'slf4j-api')
}
testImplementation project(path: ':logback-android')

compileOnly "org.slf4j:slf4j-api:${slf4jVersion}"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ private void connectSocketAndDispatchEvents() {
ObjectWriter objectWriter = createObjectWriterForSocket();
addInfo(peerId + "connection established");
dispatchEvents(objectWriter);
} catch (javax.net.ssl.SSLHandshakeException she) {
// FIXME
Thread.sleep(DEFAULT_RECONNECTION_DELAY);
} catch (IOException ex) {
addInfo(peerId + "connection failed: " + ex);
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* to facilitate unit testing.
*
* @author Carl Harris
* @author Bruno Harbulot
*/
public interface SSLConfigurable {

Expand Down Expand Up @@ -84,4 +85,5 @@ public interface SSLConfigurable {
*/
void setWantClientAuth(boolean state);

void setHostnameVerification(boolean verifyHostname);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
package ch.qos.logback.core.net.ssl;

import android.annotation.TargetApi;

import javax.net.ssl.SSLParameters;
import javax.net.ssl.SSLServerSocket;

/**
Expand Down Expand Up @@ -62,4 +65,14 @@ public void setWantClientAuth(boolean state) {
delegate.setWantClientAuth(state);
}

@TargetApi(24)
@Override
public void setHostnameVerification(boolean hostnameVerification) {
if (!hostnameVerification) {
return;
}
SSLParameters sslParameters = delegate.getSSLParameters();
sslParameters.setEndpointIdentificationAlgorithm("HTTPS");
delegate.setSSLParameters(sslParameters);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@
*/
package ch.qos.logback.core.net.ssl;

import android.annotation.TargetApi;

import javax.net.ssl.SSLParameters;
import javax.net.ssl.SSLSocket;

/**
* An {@link SSLConfigurable} wrapper for an {@link SSLSocket}.
*
* @author Carl Harris
* @author Bruno Harbulot
*/
public class SSLConfigurableSocket implements SSLConfigurable {

Expand Down Expand Up @@ -62,4 +66,14 @@ public void setWantClientAuth(boolean state) {
delegate.setWantClientAuth(state);
}

@TargetApi(24)
@Override
public void setHostnameVerification(boolean hostnameVerification) {
if (!hostnameVerification) {
return;
}
SSLParameters sslParameters = delegate.getSSLParameters();
sslParameters.setEndpointIdentificationAlgorithm("HTTPS");
delegate.setSSLParameters(sslParameters);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class SSLParametersConfiguration extends ContextAwareBase {
private Boolean wantClientAuth;
private String[] enabledProtocols;
private String[] enabledCipherSuites;
private Boolean hostnameVerification;

/**
* Configures SSL parameters on an {@link SSLConfigurable}.
Expand All @@ -57,6 +58,20 @@ public void configure(SSLConfigurable socket) {
if (isWantClientAuth() != null) {
socket.setWantClientAuth(isWantClientAuth());
}
if (hostnameVerification != null) {
addInfo("hostnameVerification="+hostnameVerification);
socket.setHostnameVerification(hostnameVerification);
}
}

public boolean getHostnameVerification() {
if(hostnameVerification == null)
return false;
return hostnameVerification;
}

public void setHostnameVerification(boolean hostnameVerification) {
this.hostnameVerification = hostnameVerification;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import ch.qos.logback.core.net.SocketConnector.ExceptionHandler;
Expand All @@ -47,10 +46,9 @@
*
* @author Carl Harris
*/
@Ignore
public class DefaultSocketConnectorTest {

private static final int DELAY = 1000;
private static final int DELAY = 2000;
private static final int SHORT_DELAY = 10;
private static final int RETRY_DELAY = 10;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,7 @@ public void setWantClientAuth(boolean wantClientAuth) {
this.wantClientAuth = wantClientAuth;
}

@Override
public void setHostnameVerification(boolean verifyHostname) {
}
}