mirrored from https://www.bouncycastle.org/repositories/bc-java
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BCJSSE: Improved workaround for InetAddress limitation
- URLConnectionUtil now calls BCSSLSocket.setHost instead of direct SNI config
- Loading branch information
1 parent
65c9832
commit c47f644
Showing
4 changed files
with
99 additions
and
11 deletions.
There are no files selected for viewing
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
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
80 changes: 80 additions & 0 deletions
80
tls/src/main/java/org/bouncycastle/jsse/util/SetHostSocketFactory.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,80 @@ | ||
package org.bouncycastle.jsse.util; | ||
|
||
import java.net.Socket; | ||
import java.net.URL; | ||
import java.util.concurrent.Callable; | ||
import java.util.logging.Logger; | ||
|
||
import javax.net.SocketFactory; | ||
import javax.net.ssl.SSLSocketFactory; | ||
|
||
import org.bouncycastle.jsse.BCSSLSocket; | ||
|
||
public class SetHostSocketFactory extends CustomSSLSocketFactory | ||
{ | ||
private static final Logger LOG = Logger.getLogger(SetHostSocketFactory.class.getName()); | ||
|
||
protected static final ThreadLocal<SetHostSocketFactory> threadLocal = new ThreadLocal<SetHostSocketFactory>(); | ||
|
||
/** | ||
* Signature matches {@link SSLSocketFactory#getDefault()} so that it can be | ||
* used with e.g. the "java.naming.ldap.factory.socket" property or similar. | ||
* | ||
* @see #call(Callable) | ||
*/ | ||
public static SocketFactory getDefault() | ||
{ | ||
SSLSocketFactory sslSocketFactory = threadLocal.get(); | ||
if (null != sslSocketFactory) | ||
{ | ||
return sslSocketFactory; | ||
} | ||
|
||
return SSLSocketFactory.getDefault(); | ||
} | ||
|
||
protected final URL url; | ||
|
||
public SetHostSocketFactory(SSLSocketFactory delegate, URL url) | ||
{ | ||
super(delegate); | ||
|
||
this.url = url; | ||
} | ||
|
||
/** | ||
* Calls a {@link Callable} in a context where this class's static | ||
* {@link #getDefault()} method will return this {@link SetHostSocketFactory}. | ||
*/ | ||
public <V> V call(Callable<V> callable) throws Exception | ||
{ | ||
try | ||
{ | ||
threadLocal.set(this); | ||
|
||
return callable.call(); | ||
} | ||
finally | ||
{ | ||
threadLocal.remove(); | ||
} | ||
} | ||
|
||
@Override | ||
protected Socket configureSocket(Socket s) | ||
{ | ||
if (url != null && s instanceof BCSSLSocket) | ||
{ | ||
BCSSLSocket ssl = (BCSSLSocket)s; | ||
|
||
String host = url.getHost(); | ||
if (host != null) | ||
{ | ||
LOG.fine("Setting host on socket: " + host); | ||
|
||
ssl.setHost(host); | ||
} | ||
} | ||
return s; | ||
} | ||
} |
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