Skip to content

Instantly share code, notes, and snippets.

@mshenoy83
Last active September 21, 2017 03:21
Show Gist options
  • Save mshenoy83/9763e06d947db860c3305f6f59a62f3b to your computer and use it in GitHub Desktop.
Save mshenoy83/9763e06d947db860c3305f6f59a62f3b to your computer and use it in GitHub Desktop.
CustomTlsSSLSocketFactory
public class CustomTlsSSLSocketFactory : SSLSocketFactory
{
public CustomTlsSSLSocketFactory()
{
SSLContext context = SSLContext.GetInstance("TLS");
context.Init(null, null, null);
_factory = context.SocketFactory;
}
private readonly SSLSocketFactory _factory;
public override string[] GetDefaultCipherSuites()
{
return _factory.GetDefaultCipherSuites();
}
public override string[] GetSupportedCipherSuites()
{
return _factory.GetSupportedCipherSuites();
}
public override Java.Net.Socket CreateSocket(Java.Net.InetAddress address, int port, Java.Net.InetAddress localAddress, int localPort)
{
SSLSocket socket = (SSLSocket)_factory.CreateSocket(address, port, localAddress, localPort);
socket.SetEnabledProtocols(socket.GetSupportedProtocols());
socket.SetEnabledCipherSuites(_factory.GetSupportedCipherSuites());
return socket;
}
public override Java.Net.Socket CreateSocket(Java.Net.InetAddress host, int port)
{
SSLSocket socket = (SSLSocket)_factory.CreateSocket(host, port);
socket.SetEnabledProtocols(socket.GetSupportedProtocols());
socket.SetEnabledCipherSuites(_factory.GetSupportedCipherSuites());
return socket;
}
public override Java.Net.Socket CreateSocket(string host, int port, Java.Net.InetAddress localHost, int localPort)
{
SSLSocket socket = (SSLSocket)_factory.CreateSocket(host, port, localHost, localPort);
socket.SetEnabledProtocols(socket.GetSupportedProtocols());
socket.SetEnabledCipherSuites(_factory.GetSupportedCipherSuites());
return socket;
}
public override Java.Net.Socket CreateSocket(string host, int port)
{
SSLSocket socket = (SSLSocket)_factory.CreateSocket(host, port);
socket.SetEnabledProtocols(socket.GetSupportedProtocols());
socket.SetEnabledCipherSuites(_factory.GetSupportedCipherSuites());
return socket;
}
public override Java.Net.Socket CreateSocket(Java.Net.Socket s, string host, int port, bool autoClose)
{
SSLSocket socket = (SSLSocket)_factory.CreateSocket(s, host, port, autoClose);
socket.SetEnabledProtocols(socket.GetSupportedProtocols());
socket.SetEnabledCipherSuites(_factory.GetSupportedCipherSuites());
return socket;
}
protected override void Dispose(bool disposing)
{
_factory.Dispose();
base.Dispose(disposing);
}
public override Java.Net.Socket CreateSocket()
{
SSLSocket socket = (SSLSocket)_factory.CreateSocket();
socket.SetEnabledProtocols(socket.GetSupportedProtocols());
socket.SetEnabledCipherSuites(_factory.GetSupportedCipherSuites());
return socket;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment