Skip to content

Commit e90a33a

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

1 file changed

Lines changed: 61 additions & 49 deletions

File tree

native/com_wolfssl_WolfSSL.c

Lines changed: 61 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);
@@ -1694,13 +1697,13 @@ void NativeFIPSErrorCallback(const int ok, const int err,
16941697
const char* const hash)
16951698
{
16961699
#ifdef HAVE_FIPS
1697-
JNIEnv* jenv;
1700+
JNIEnv* jenv = NULL;
16981701
jint vmret = 0;
1699-
jclass excClass;
17001702
jclass fipsCbClass;
17011703
jmethodID errorMethod;
17021704
jobjectRefType refcheck;
17031705
jstring hashString;
1706+
int needsDetach = 0;
17041707

17051708
/* get JNIEnv from JavaVM */
17061709
vmret = (int)((*g_vm)->GetEnv(g_vm, (void**) &jenv, JNI_VERSION_1_6));
@@ -1712,75 +1715,77 @@ void NativeFIPSErrorCallback(const int ok, const int err,
17121715
#endif
17131716
if (vmret) {
17141717
printf("Failed to attach JNIEnv to thread\n");
1718+
return;
17151719
}
1720+
needsDetach = 1;
1721+
17161722
} else if (vmret != JNI_OK) {
17171723
printf("Unable to get JNIEnv from JavaVM\n");
1724+
return;
17181725
}
17191726

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);
1727+
/* Just return if stored callback object reference is NULL/invalid */
1728+
if (g_fipsCbIfaceObj == NULL) {
1729+
if (needsDetach) {
1730+
(*g_vm)->DetachCurrentThread(g_vm);
1731+
}
17251732
return;
17261733
}
17271734

1728-
/* check if our stored object reference is valid */
17291735
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;
1736+
if (refcheck != JNIGlobalRefType) {
1737+
if ((*jenv)->ExceptionOccurred(jenv)) {
1738+
(*jenv)->ExceptionDescribe(jenv);
1739+
(*jenv)->ExceptionClear(jenv);
17431740
}
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;
1741+
if (needsDetach) {
1742+
(*g_vm)->DetachCurrentThread(g_vm);
17561743
}
1744+
return;
1745+
}
17571746

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-
1747+
/* lookup WolfSSLFIPSErrorCallback class from global object ref */
1748+
fipsCbClass = (*jenv)->GetObjectClass(jenv, g_fipsCbIfaceObj);
1749+
if (!fipsCbClass) {
17671750
if ((*jenv)->ExceptionOccurred(jenv)) {
17681751
(*jenv)->ExceptionDescribe(jenv);
17691752
(*jenv)->ExceptionClear(jenv);
1770-
1771-
(*jenv)->ThrowNew(jenv, excClass,
1772-
"Error calling FIPS error callback from JNI");
1773-
return;
17741753
}
1754+
if (needsDetach) {
1755+
(*g_vm)->DetachCurrentThread(g_vm);
1756+
}
1757+
return;
1758+
}
17751759

1776-
} else {
1760+
errorMethod = (*jenv)->GetMethodID(jenv, fipsCbClass, "errorCallback",
1761+
"(IILjava/lang/String;)V");
1762+
if (errorMethod == 0) {
17771763
if ((*jenv)->ExceptionOccurred(jenv)) {
17781764
(*jenv)->ExceptionDescribe(jenv);
17791765
(*jenv)->ExceptionClear(jenv);
17801766
}
1767+
if (needsDetach) {
1768+
(*g_vm)->DetachCurrentThread(g_vm);
1769+
}
1770+
return;
1771+
}
1772+
1773+
/* create jstring from char* */
1774+
hashString = (*jenv)->NewStringUTF(jenv, hash);
1775+
1776+
(*jenv)->CallVoidMethod(jenv, g_fipsCbIfaceObj, errorMethod,
1777+
ok, err, hashString);
1778+
1779+
/* release local reference to jstring, since returning to native */
1780+
(*jenv)->DeleteLocalRef(jenv, hashString);
17811781

1782-
(*jenv)->ThrowNew(jenv, excClass,
1783-
"Object reference invalid in NativeFIPSErrorCallback");
1782+
if ((*jenv)->ExceptionOccurred(jenv)) {
1783+
(*jenv)->ExceptionDescribe(jenv);
1784+
(*jenv)->ExceptionClear(jenv);
1785+
}
1786+
1787+
if (needsDetach) {
1788+
(*g_vm)->DetachCurrentThread(g_vm);
17841789
}
17851790
#else
17861791
(void)ok;
@@ -1820,6 +1825,13 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSL_setFIPSCb
18201825
ret = SSL_SUCCESS;
18211826
}
18221827
}
1828+
else {
1829+
/* NULL callback, deregister native FIPS callback */
1830+
ret = wolfCrypt_SetCb_fips(NULL);
1831+
if (ret == 0) {
1832+
ret = SSL_SUCCESS;
1833+
}
1834+
}
18231835
#else
18241836
(void)jenv;
18251837
(void)callback;

0 commit comments

Comments
 (0)