@@ -87,6 +87,9 @@ public class WolfSSLEngineHelper {
8787 /* Has setUseClientMode() been called on this object */
8888 private boolean modeSet = false ;
8989
90+ /* wolfSSL verification mode, set inside setLocalAuth() */
91+ private int verifyMask = WolfSSL .SSL_VERIFY_PEER ;
92+
9093 /* Internal Java verify callback, used when user/app is not using
9194 * com.wolfssl.provider.jsse.WolfSSLTrustX509 and instead using their
9295 * own TrustManager to perform verification via checkClientTrusted()
@@ -805,7 +808,7 @@ private void setLocalAuth(SSLSocket socket, SSLEngine engine) {
805808 * Algorithm has been set. To get this callback to be called,
806809 * native wolfSSL should be compiled with the following define:
807810 * WOLFSSL_ALWAYS_VERIFY_CB */
808- this .ssl . setVerify ( mask , wicb ) ;
811+ this .verifyMask = mask ;
809812
810813 } else {
811814 /* not our own TrustManager, set up callback so JSSE can use
@@ -814,8 +817,10 @@ private void setLocalAuth(SSLSocket socket, SSLEngine engine) {
814817 "X509TrustManager is not of type WolfSSLTrustX509" );
815818 WolfSSLDebug .log (getClass (), WolfSSLDebug .INFO ,
816819 "Using checkClientTrusted/ServerTrusted() for verification" );
817- this .ssl . setVerify ( WolfSSL .SSL_VERIFY_PEER , wicb ) ;
820+ this .verifyMask = WolfSSL .SSL_VERIFY_PEER ;
818821 }
822+
823+ this .ssl .setVerify (this .verifyMask , wicb );
819824 }
820825
821826
@@ -1330,6 +1335,35 @@ else if (peerAddr != null) {
13301335 return ret ;
13311336 }
13321337
1338+ /**
1339+ * Unset the native verify callback and reset internal verify
1340+ * callback state.
1341+ *
1342+ * This helper method is called by SSLEngine to reset the native
1343+ * wolfSSL verify callback back to null. Since a pointer to that verify
1344+ * callback is stored as a global JNI variable, it can prevent garbage
1345+ * collection from being done. This helper can be called when an SSLEngine
1346+ * or SSLSocket is closed/done to reset the verify callback.
1347+ *
1348+ * The verify callback will be set again if needed when
1349+ * initHandshake() is called.
1350+ */
1351+ protected synchronized void unsetVerifyCallback () {
1352+ /* Set native callback to null, releases JNI global and allows for
1353+ * garbage collection if needed */
1354+ if (this .ssl != null ) {
1355+ this .ssl .setVerify (this .verifyMask , null );
1356+ }
1357+
1358+ /* Reset internal state of WolfSSLInternalVerifyCallback, removes
1359+ * references to SSLSocket/SSLEngine to allow garbage collection if
1360+ * needed */
1361+ if (this .wicb != null ) {
1362+ this .wicb .clearInternalVars ();
1363+ this .wicb = null ;
1364+ }
1365+ }
1366+
13331367 /**
13341368 * Saves session on connection close for resumption
13351369 *
@@ -1357,6 +1391,7 @@ protected synchronized void finalize() throws Throwable {
13571391 * may be used by wrapper object to WolfSSLEngineHelper and should
13581392 * be freed there */
13591393 this .ssl = null ;
1394+ this .wicb = null ;
13601395
13611396 this .session = null ;
13621397 this .params = null ;
0 commit comments