豆豆友情提示:这是一个非官方 GitHub 代理镜像,主要用于网络测试或访问加速。请勿在此进行登录、注册或处理任何敏感信息。进行这些操作请务必访问官方网站 github.com。 Raw 内容也通过此代理提供。
Skip to content

Commit c47f644

Browse files
committed
BCJSSE: Improved workaround for InetAddress limitation
- URLConnectionUtil now calls BCSSLSocket.setHost instead of direct SNI config
1 parent 65c9832 commit c47f644

File tree

4 files changed

+99
-11
lines changed

4 files changed

+99
-11
lines changed

tls/src/main/java/org/bouncycastle/jsse/provider/ProvSSLSocketDirect.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,6 @@ public synchronized void setEnableSessionCreation(boolean flag)
345345
public synchronized void setHost(String host)
346346
{
347347
this.peerHost = host;
348-
this.peerHostSNI = host;
349348
}
350349

351350
@Override
@@ -531,15 +530,16 @@ synchronized void notifyConnected()
531530
InetAddress peerAddress = getInetAddress();
532531
if (null == peerAddress)
533532
{
533+
this.peerHostSNI = null;
534534
return;
535535
}
536536

537537
/*
538538
* TODO[jsse] If we could somehow access the 'originalHostName' of peerAddress, it would be
539539
* usable as a default SNI host_name.
540540
*/
541-
// String originalHostName = null;
542-
// if (null != originalHostName)
541+
// String originalHostName = peerAddress.holder().getOriginalHostName();
542+
// if (JsseUtils.isNameSpecified(originalHostName))
543543
// {
544544
// this.peerHost = originalHostName;
545545
// this.peerHostSNI = originalHostName;
@@ -555,13 +555,17 @@ synchronized void notifyConnected()
555555
return;
556556
}
557557

558-
if (useClientMode && provJdkTlsTrustNameService)
558+
if (!useClientMode)
559+
{
560+
this.peerHost = peerAddress.getHostAddress();
561+
}
562+
else if (provJdkTlsTrustNameService)
559563
{
560564
this.peerHost = peerAddress.getHostName();
561565
}
562566
else
563567
{
564-
this.peerHost = peerAddress.getHostAddress();
568+
this.peerHost = null;
565569
}
566570

567571
this.peerHostSNI = null;

tls/src/main/java/org/bouncycastle/jsse/provider/ProvSSLSocketWrap.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,6 @@ public synchronized void setEnableSessionCreation(boolean flag)
470470
public synchronized void setHost(String host)
471471
{
472472
this.peerHost = host;
473-
this.peerHostSNI = host;
474473
}
475474

476475
@Override
@@ -720,15 +719,16 @@ synchronized void notifyConnected()
720719
InetAddress peerAddress = getInetAddress();
721720
if (null == peerAddress)
722721
{
722+
this.peerHostSNI = null;
723723
return;
724724
}
725725

726726
/*
727727
* TODO[jsse] If we could somehow access the 'originalHostName' of peerAddress, it would be
728728
* usable as a default SNI host_name.
729729
*/
730-
// String originalHostName = null;
731-
// if (null != originalHostName)
730+
// String originalHostName = peerAddress.holder().getOriginalHostName();
731+
// if (JsseUtils.isNameSpecified(originalHostName))
732732
// {
733733
// this.peerHost = originalHostName;
734734
// this.peerHostSNI = originalHostName;
@@ -744,13 +744,17 @@ synchronized void notifyConnected()
744744
return;
745745
}
746746

747-
if (useClientMode && provJdkTlsTrustNameService)
747+
if (!useClientMode)
748+
{
749+
this.peerHost = peerAddress.getHostAddress();
750+
}
751+
else if (provJdkTlsTrustNameService)
748752
{
749753
this.peerHost = peerAddress.getHostName();
750754
}
751755
else
752756
{
753-
this.peerHost = peerAddress.getHostAddress();
757+
this.peerHost = null;
754758
}
755759

756760
this.peerHostSNI = null;
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package org.bouncycastle.jsse.util;
2+
3+
import java.net.Socket;
4+
import java.net.URL;
5+
import java.util.concurrent.Callable;
6+
import java.util.logging.Logger;
7+
8+
import javax.net.SocketFactory;
9+
import javax.net.ssl.SSLSocketFactory;
10+
11+
import org.bouncycastle.jsse.BCSSLSocket;
12+
13+
public class SetHostSocketFactory extends CustomSSLSocketFactory
14+
{
15+
private static final Logger LOG = Logger.getLogger(SetHostSocketFactory.class.getName());
16+
17+
protected static final ThreadLocal<SetHostSocketFactory> threadLocal = new ThreadLocal<SetHostSocketFactory>();
18+
19+
/**
20+
* Signature matches {@link SSLSocketFactory#getDefault()} so that it can be
21+
* used with e.g. the "java.naming.ldap.factory.socket" property or similar.
22+
*
23+
* @see #call(Callable)
24+
*/
25+
public static SocketFactory getDefault()
26+
{
27+
SSLSocketFactory sslSocketFactory = threadLocal.get();
28+
if (null != sslSocketFactory)
29+
{
30+
return sslSocketFactory;
31+
}
32+
33+
return SSLSocketFactory.getDefault();
34+
}
35+
36+
protected final URL url;
37+
38+
public SetHostSocketFactory(SSLSocketFactory delegate, URL url)
39+
{
40+
super(delegate);
41+
42+
this.url = url;
43+
}
44+
45+
/**
46+
* Calls a {@link Callable} in a context where this class's static
47+
* {@link #getDefault()} method will return this {@link SetHostSocketFactory}.
48+
*/
49+
public <V> V call(Callable<V> callable) throws Exception
50+
{
51+
try
52+
{
53+
threadLocal.set(this);
54+
55+
return callable.call();
56+
}
57+
finally
58+
{
59+
threadLocal.remove();
60+
}
61+
}
62+
63+
@Override
64+
protected Socket configureSocket(Socket s)
65+
{
66+
if (url != null && s instanceof BCSSLSocket)
67+
{
68+
BCSSLSocket ssl = (BCSSLSocket)s;
69+
70+
String host = url.getHost();
71+
if (host != null)
72+
{
73+
LOG.fine("Setting host on socket: " + host);
74+
75+
ssl.setHost(host);
76+
}
77+
}
78+
return s;
79+
}
80+
}

tls/src/main/java/org/bouncycastle/jsse/util/URLConnectionUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,6 @@ protected URLConnection configureConnection(URL url, URLConnection connection)
6666

6767
protected SSLSocketFactory createSSLSocketFactory(SSLSocketFactory delegate, URL url)
6868
{
69-
return new SNISocketFactory(delegate, url);
69+
return new SetHostSocketFactory(delegate, url);
7070
}
7171
}

0 commit comments

Comments
 (0)