Skip to content

Commit 5da69b4

Browse files
authored
Merge pull request #337 from cconlon/fipsCallbackGC
Deregister native FIPS error callback on library cleanup or reset
2 parents f25cc9c + e90a33a commit 5da69b4

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
@@ -1537,6 +1537,9 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSL_cleanup
15371537
}
15381538

15391539
#ifdef HAVE_FIPS
1540+
/* Deregister native FIPS callback from wolfCrypt before releasing */
1541+
wolfCrypt_SetCb_fips(NULL);
1542+
15401543
/* release existing FIPS callback object if set */
15411544
if (g_fipsCbIfaceObj != NULL) {
15421545
(*jenv)->DeleteGlobalRef(jenv, g_fipsCbIfaceObj);
@@ -1707,13 +1710,13 @@ void NativeFIPSErrorCallback(const int ok, const int err,
17071710
const char* const hash)
17081711
{
17091712
#ifdef HAVE_FIPS
1710-
JNIEnv* jenv;
1713+
JNIEnv* jenv = NULL;
17111714
jint vmret = 0;
1712-
jclass excClass;
17131715
jclass fipsCbClass;
17141716
jmethodID errorMethod;
17151717
jobjectRefType refcheck;
17161718
jstring hashString;
1719+
int needsDetach = 0;
17171720

17181721
/* get JNIEnv from JavaVM */
17191722
vmret = (int)((*g_vm)->GetEnv(g_vm, (void**) &jenv, JNI_VERSION_1_6));
@@ -1725,75 +1728,77 @@ void NativeFIPSErrorCallback(const int ok, const int err,
17251728
#endif
17261729
if (vmret) {
17271730
printf("Failed to attach JNIEnv to thread\n");
1731+
return;
17281732
}
1733+
needsDetach = 1;
1734+
17291735
} else if (vmret != JNI_OK) {
17301736
printf("Unable to get JNIEnv from JavaVM\n");
1737+
return;
17311738
}
17321739

1733-
/* find exception class */
1734-
excClass = (*jenv)->FindClass(jenv, "java/lang/Exception");
1735-
if ((*jenv)->ExceptionOccurred(jenv)) {
1736-
(*jenv)->ExceptionDescribe(jenv);
1737-
(*jenv)->ExceptionClear(jenv);
1740+
/* Just return if stored callback object reference is NULL/invalid */
1741+
if (g_fipsCbIfaceObj == NULL) {
1742+
if (needsDetach) {
1743+
(*g_vm)->DetachCurrentThread(g_vm);
1744+
}
17381745
return;
17391746
}
17401747

1741-
/* check if our stored object reference is valid */
17421748
refcheck = (*jenv)->GetObjectRefType(jenv, g_fipsCbIfaceObj);
1743-
if (refcheck == JNIGlobalRefType) {
1744-
1745-
/* lookup WolfSSLLoggingCallback class from global object ref */
1746-
fipsCbClass = (*jenv)->GetObjectClass(jenv, g_fipsCbIfaceObj);
1747-
if (!fipsCbClass) {
1748-
if ((*jenv)->ExceptionOccurred(jenv)) {
1749-
(*jenv)->ExceptionDescribe(jenv);
1750-
(*jenv)->ExceptionClear(jenv);
1751-
}
1752-
1753-
(*jenv)->ThrowNew(jenv, excClass,
1754-
"Can't get native WolfSSLFIPSErrorCallback class reference");
1755-
return;
1749+
if (refcheck != JNIGlobalRefType) {
1750+
if ((*jenv)->ExceptionOccurred(jenv)) {
1751+
(*jenv)->ExceptionDescribe(jenv);
1752+
(*jenv)->ExceptionClear(jenv);
17561753
}
1757-
1758-
errorMethod = (*jenv)->GetMethodID(jenv, fipsCbClass,
1759-
"errorCallback",
1760-
"(IILjava/lang/String;)V");
1761-
if (errorMethod == 0) {
1762-
if ((*jenv)->ExceptionOccurred(jenv)) {
1763-
(*jenv)->ExceptionDescribe(jenv);
1764-
(*jenv)->ExceptionClear(jenv);
1765-
}
1766-
(*jenv)->ThrowNew(jenv, excClass,
1767-
"Error getting errorCallback method from JNI");
1768-
return;
1754+
if (needsDetach) {
1755+
(*g_vm)->DetachCurrentThread(g_vm);
17691756
}
1757+
return;
1758+
}
17701759

1771-
/* create jstring from char* */
1772-
hashString = (*jenv)->NewStringUTF(jenv, hash);
1773-
1774-
(*jenv)->CallVoidMethod(jenv, g_fipsCbIfaceObj, errorMethod,
1775-
ok, err, hashString);
1776-
1777-
/* release local reference to jstring, since returning to native */
1778-
(*jenv)->DeleteLocalRef(jenv, hashString);
1779-
1760+
/* lookup WolfSSLFIPSErrorCallback class from global object ref */
1761+
fipsCbClass = (*jenv)->GetObjectClass(jenv, g_fipsCbIfaceObj);
1762+
if (!fipsCbClass) {
17801763
if ((*jenv)->ExceptionOccurred(jenv)) {
17811764
(*jenv)->ExceptionDescribe(jenv);
17821765
(*jenv)->ExceptionClear(jenv);
1783-
1784-
(*jenv)->ThrowNew(jenv, excClass,
1785-
"Error calling FIPS error callback from JNI");
1786-
return;
17871766
}
1767+
if (needsDetach) {
1768+
(*g_vm)->DetachCurrentThread(g_vm);
1769+
}
1770+
return;
1771+
}
17881772

1789-
} else {
1773+
errorMethod = (*jenv)->GetMethodID(jenv, fipsCbClass, "errorCallback",
1774+
"(IILjava/lang/String;)V");
1775+
if (errorMethod == 0) {
17901776
if ((*jenv)->ExceptionOccurred(jenv)) {
17911777
(*jenv)->ExceptionDescribe(jenv);
17921778
(*jenv)->ExceptionClear(jenv);
17931779
}
1780+
if (needsDetach) {
1781+
(*g_vm)->DetachCurrentThread(g_vm);
1782+
}
1783+
return;
1784+
}
1785+
1786+
/* create jstring from char* */
1787+
hashString = (*jenv)->NewStringUTF(jenv, hash);
1788+
1789+
(*jenv)->CallVoidMethod(jenv, g_fipsCbIfaceObj, errorMethod,
1790+
ok, err, hashString);
1791+
1792+
/* release local reference to jstring, since returning to native */
1793+
(*jenv)->DeleteLocalRef(jenv, hashString);
17941794

1795-
(*jenv)->ThrowNew(jenv, excClass,
1796-
"Object reference invalid in NativeFIPSErrorCallback");
1795+
if ((*jenv)->ExceptionOccurred(jenv)) {
1796+
(*jenv)->ExceptionDescribe(jenv);
1797+
(*jenv)->ExceptionClear(jenv);
1798+
}
1799+
1800+
if (needsDetach) {
1801+
(*g_vm)->DetachCurrentThread(g_vm);
17971802
}
17981803
#else
17991804
(void)ok;
@@ -1833,6 +1838,13 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSL_setFIPSCb
18331838
ret = SSL_SUCCESS;
18341839
}
18351840
}
1841+
else {
1842+
/* NULL callback, deregister native FIPS callback */
1843+
ret = wolfCrypt_SetCb_fips(NULL);
1844+
if (ret == 0) {
1845+
ret = SSL_SUCCESS;
1846+
}
1847+
}
18361848
#else
18371849
(void)jenv;
18381850
(void)callback;

0 commit comments

Comments
 (0)