Skip to content

Commit 1a2f881

Browse files
committed
JNI/JSSE: deregister native FIPS callback in cleanup() and setFIPSCb(NULL), guard NativeFIPSErrorCallback against invalid object refs
1 parent 13cb353 commit 1a2f881

1 file changed

Lines changed: 39 additions & 49 deletions

File tree

native/com_wolfssl_WolfSSL.c

Lines changed: 39 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,6 +1524,9 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSL_cleanup
15241524
}
15251525

15261526
#ifdef HAVE_FIPS
1527+
/* Deregister native FIPS callback from wolfCrypt before releasing */
1528+
wolfCrypt_SetCb_fips(NULL);
1529+
15271530
/* release existing FIPS callback object if set */
15281531
if (g_fipsCbIfaceObj != NULL) {
15291532
(*jenv)->DeleteGlobalRef(jenv, g_fipsCbIfaceObj);
@@ -1696,7 +1699,6 @@ void NativeFIPSErrorCallback(const int ok, const int err,
16961699
#ifdef HAVE_FIPS
16971700
JNIEnv* jenv;
16981701
jint vmret = 0;
1699-
jclass excClass;
17001702
jclass fipsCbClass;
17011703
jmethodID errorMethod;
17021704
jobjectRefType refcheck;
@@ -1717,70 +1719,53 @@ void NativeFIPSErrorCallback(const int ok, const int err,
17171719
printf("Unable to get JNIEnv from JavaVM\n");
17181720
}
17191721

1720-
/* find exception class */
1721-
excClass = (*jenv)->FindClass(jenv, "java/lang/Exception");
1722-
if ((*jenv)->ExceptionOccurred(jenv)) {
1723-
(*jenv)->ExceptionDescribe(jenv);
1724-
(*jenv)->ExceptionClear(jenv);
1722+
/* Just return if stored callback object reference is NULL/invalid */
1723+
if (g_fipsCbIfaceObj == NULL) {
17251724
return;
17261725
}
17271726

1728-
/* check if our stored object reference is valid */
17291727
refcheck = (*jenv)->GetObjectRefType(jenv, g_fipsCbIfaceObj);
1730-
if (refcheck == JNIGlobalRefType) {
1731-
1732-
/* lookup WolfSSLLoggingCallback class from global object ref */
1733-
fipsCbClass = (*jenv)->GetObjectClass(jenv, g_fipsCbIfaceObj);
1734-
if (!fipsCbClass) {
1735-
if ((*jenv)->ExceptionOccurred(jenv)) {
1736-
(*jenv)->ExceptionDescribe(jenv);
1737-
(*jenv)->ExceptionClear(jenv);
1738-
}
1739-
1740-
(*jenv)->ThrowNew(jenv, excClass,
1741-
"Can't get native WolfSSLFIPSErrorCallback class reference");
1742-
return;
1743-
}
1744-
1745-
errorMethod = (*jenv)->GetMethodID(jenv, fipsCbClass,
1746-
"errorCallback",
1747-
"(IILjava/lang/String;)V");
1748-
if (errorMethod == 0) {
1749-
if ((*jenv)->ExceptionOccurred(jenv)) {
1750-
(*jenv)->ExceptionDescribe(jenv);
1751-
(*jenv)->ExceptionClear(jenv);
1752-
}
1753-
(*jenv)->ThrowNew(jenv, excClass,
1754-
"Error getting errorCallback method from JNI");
1755-
return;
1728+
if (refcheck != JNIGlobalRefType) {
1729+
if ((*jenv)->ExceptionOccurred(jenv)) {
1730+
(*jenv)->ExceptionDescribe(jenv);
1731+
(*jenv)->ExceptionClear(jenv);
17561732
}
1733+
return;
1734+
}
17571735

1758-
/* create jstring from char* */
1759-
hashString = (*jenv)->NewStringUTF(jenv, hash);
1760-
1761-
(*jenv)->CallVoidMethod(jenv, g_fipsCbIfaceObj, errorMethod,
1762-
ok, err, hashString);
1763-
1764-
/* release local reference to jstring, since returning to native */
1765-
(*jenv)->DeleteLocalRef(jenv, hashString);
1766-
1736+
/* lookup WolfSSLFIPSErrorCallback class from global object ref */
1737+
fipsCbClass = (*jenv)->GetObjectClass(jenv, g_fipsCbIfaceObj);
1738+
if (!fipsCbClass) {
17671739
if ((*jenv)->ExceptionOccurred(jenv)) {
17681740
(*jenv)->ExceptionDescribe(jenv);
17691741
(*jenv)->ExceptionClear(jenv);
1770-
1771-
(*jenv)->ThrowNew(jenv, excClass,
1772-
"Error calling FIPS error callback from JNI");
1773-
return;
17741742
}
1743+
return;
1744+
}
17751745

1776-
} else {
1746+
errorMethod = (*jenv)->GetMethodID(jenv, fipsCbClass,
1747+
"errorCallback",
1748+
"(IILjava/lang/String;)V");
1749+
if (errorMethod == 0) {
17771750
if ((*jenv)->ExceptionOccurred(jenv)) {
17781751
(*jenv)->ExceptionDescribe(jenv);
17791752
(*jenv)->ExceptionClear(jenv);
17801753
}
1754+
return;
1755+
}
1756+
1757+
/* create jstring from char* */
1758+
hashString = (*jenv)->NewStringUTF(jenv, hash);
1759+
1760+
(*jenv)->CallVoidMethod(jenv, g_fipsCbIfaceObj, errorMethod,
1761+
ok, err, hashString);
1762+
1763+
/* release local reference to jstring, since returning to native */
1764+
(*jenv)->DeleteLocalRef(jenv, hashString);
17811765

1782-
(*jenv)->ThrowNew(jenv, excClass,
1783-
"Object reference invalid in NativeFIPSErrorCallback");
1766+
if ((*jenv)->ExceptionOccurred(jenv)) {
1767+
(*jenv)->ExceptionDescribe(jenv);
1768+
(*jenv)->ExceptionClear(jenv);
17841769
}
17851770
#else
17861771
(void)ok;
@@ -1820,6 +1805,11 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSL_setFIPSCb
18201805
ret = SSL_SUCCESS;
18211806
}
18221807
}
1808+
else {
1809+
/* NULL callback, deregister native FIPS callback */
1810+
wolfCrypt_SetCb_fips(NULL);
1811+
ret = SSL_SUCCESS;
1812+
}
18231813
#else
18241814
(void)jenv;
18251815
(void)callback;

0 commit comments

Comments
 (0)