3434import javax .net .ssl .X509TrustManager ;
3535import javax .net .ssl .X509ExtendedTrustManager ;
3636import java .io .IOException ;
37+ import java .lang .ref .WeakReference ;
3738
3839/**
3940 * Internal verify callback.
@@ -47,10 +48,14 @@ public class WolfSSLInternalVerifyCb implements WolfSSLVerifyCallback {
4748
4849 private X509TrustManager tm = null ;
4950 private boolean clientMode ;
50- private SSLSocket callingSocket = null ;
51- private SSLEngine callingEngine = null ;
5251 private WolfSSLParameters params = null ;
5352
53+ /* Use WeakReference for SSLSocket and SSLEngine to avoid
54+ * holding back garbage collection of WolfSSLSocket/WolfSSLEngine
55+ * objects */
56+ private WeakReference <SSLSocket > callingSocket = null ;
57+ private WeakReference <SSLEngine > callingEngine = null ;
58+
5459 /**
5560 * Create new WolfSSLInternalVerifyCb
5661 *
@@ -64,9 +69,19 @@ public WolfSSLInternalVerifyCb(X509TrustManager xtm, boolean client,
6469 SSLSocket socket , SSLEngine engine , WolfSSLParameters params ) {
6570 this .tm = xtm ;
6671 this .clientMode = client ;
67- this .callingSocket = socket ;
68- this .callingEngine = engine ;
6972 this .params = params ;
73+
74+ if (socket != null ) {
75+ this .callingSocket = new WeakReference <>(socket );
76+ } else {
77+ this .callingSocket = null ;
78+ }
79+
80+ if (engine != null ) {
81+ this .callingEngine = new WeakReference <>(engine );
82+ } else {
83+ this .callingEngine = null ;
84+ }
7085 }
7186
7287 /**
@@ -99,19 +114,25 @@ private int verifyHostnameOnly(X509Certificate peer) {
99114 WolfSSLTrustX509 wolfTM = (WolfSSLTrustX509 )tm ;
100115
101116 try {
102- if (this .callingSocket != null ) {
117+ if (this .callingSocket != null &&
118+ this .callingSocket .get () != null ) {
119+
103120 WolfSSLDebug .log (getClass (), WolfSSLDebug .INFO ,
104121 () -> "checking hostname verification using SSLSocket" );
122+
105123 /* Throws CertificateException when verify fails */
106- wolfTM .verifyHostname (peer , this .callingSocket ,
124+ wolfTM .verifyHostname (peer , this .callingSocket . get () ,
107125 null , clientMode );
108126 }
109- else if (this .callingEngine != null ) {
127+ else if (this .callingEngine != null &&
128+ this .callingEngine .get () != null ) {
129+
110130 WolfSSLDebug .log (getClass (), WolfSSLDebug .INFO ,
111131 () -> "checking hostname verification using SSLEngine" );
132+
112133 /* Throws CertificateException when verify fails */
113134 wolfTM .verifyHostname (peer , null ,
114- this .callingEngine , clientMode );
135+ this .callingEngine . get () , clientMode );
115136 }
116137 else {
117138 throw new CertificateException (
@@ -159,19 +180,26 @@ private boolean VerifyCertChainWithTrustManager(X509Certificate[] certs,
159180 if (this .tm instanceof X509ExtendedTrustManager ) {
160181 X509ExtendedTrustManager xtm =
161182 (X509ExtendedTrustManager )this .tm ;
162- if (this .callingSocket != null ) {
183+
184+ if (this .callingSocket != null &&
185+ this .callingSocket .get () != null ) {
186+
163187 WolfSSLDebug .log (getClass (), WolfSSLDebug .INFO ,
164188 () -> "Calling TrustManager.checkServerTrusted(" +
165189 "SSLSocket)" );
190+
166191 xtm .checkServerTrusted (certs , authType ,
167- this .callingSocket );
192+ this .callingSocket . get () );
168193 }
169- else if (this .callingEngine != null ) {
194+ else if (this .callingEngine != null &&
195+ this .callingEngine .get () != null ) {
196+
170197 WolfSSLDebug .log (getClass (), WolfSSLDebug .INFO ,
171198 () -> "Calling TrustManager.checkServerTrusted(" +
172199 "SSLEngine)" );
200+
173201 xtm .checkServerTrusted (certs , authType ,
174- this .callingEngine );
202+ this .callingEngine . get () );
175203 }
176204 else {
177205 /* If we do have access to X509ExtendedTrustManager,
@@ -194,19 +222,26 @@ else if (this.callingEngine != null) {
194222 if (this .tm instanceof X509ExtendedTrustManager ) {
195223 X509ExtendedTrustManager xtm =
196224 (X509ExtendedTrustManager )this .tm ;
197- if (this .callingSocket != null ) {
225+
226+ if (this .callingSocket != null &&
227+ this .callingSocket .get () != null ) {
228+
198229 WolfSSLDebug .log (getClass (), WolfSSLDebug .INFO ,
199230 () -> "Calling TrustManager.checkClientTrusted(" +
200231 "SSLSocket)" );
232+
201233 xtm .checkClientTrusted (certs , authType ,
202- this .callingSocket );
234+ this .callingSocket . get () );
203235 }
204- else if (this .callingEngine != null ) {
236+ else if (this .callingEngine != null &&
237+ this .callingEngine .get () != null ) {
238+
205239 WolfSSLDebug .log (getClass (), WolfSSLDebug .INFO ,
206240 () -> "Calling TrustManager.checkClientTrusted(" +
207241 "SSLEngine)" );
242+
208243 xtm .checkClientTrusted (certs , authType ,
209- this .callingEngine );
244+ this .callingEngine . get () );
210245 }
211246 else {
212247 /* If we do have access to X509ExtendedTrustManager,
0 commit comments