@@ -1777,6 +1777,7 @@ void NativeCtxMissingCRLCallback(const char* url)
17771777{
17781778 JNIEnv * jenv ;
17791779 jint vmret = 0 ;
1780+ int needsDetach = 0 ;
17801781 jclass excClass ;
17811782 jclass crlClass = NULL ;
17821783 jmethodID crlMethod ;
@@ -1793,16 +1794,21 @@ void NativeCtxMissingCRLCallback(const char* url)
17931794#endif
17941795 if (vmret ) {
17951796 printf ("Failed to attach JNIEnv to thread\n" );
1797+ return ;
17961798 }
1799+ needsDetach = 1 ;
17971800 } else if (vmret != JNI_OK ) {
17981801 printf ("Unable to get JNIEnv from JavaVM\n" );
1802+ return ;
17991803 }
18001804
18011805 /* find exception class */
18021806 excClass = (* jenv )-> FindClass (jenv , "com/wolfssl/WolfSSLJNIException" );
18031807 if ((* jenv )-> ExceptionOccurred (jenv )) {
18041808 (* jenv )-> ExceptionDescribe (jenv );
18051809 (* jenv )-> ExceptionClear (jenv );
1810+ if (needsDetach )
1811+ (* g_vm )-> DetachCurrentThread (g_vm );
18061812 return ;
18071813 }
18081814
@@ -1815,6 +1821,8 @@ void NativeCtxMissingCRLCallback(const char* url)
18151821 if (!crlClass ) {
18161822 (* jenv )-> ThrowNew (jenv , excClass ,
18171823 "Can't get native WolfSSLMissingCRLCallback class reference" );
1824+ if (needsDetach )
1825+ (* g_vm )-> DetachCurrentThread (g_vm );
18181826 return ;
18191827 }
18201828
@@ -1829,6 +1837,8 @@ void NativeCtxMissingCRLCallback(const char* url)
18291837
18301838 (* jenv )-> ThrowNew (jenv , excClass ,
18311839 "Error getting missingCRLCallback method from JNI" );
1840+ if (needsDetach )
1841+ (* g_vm )-> DetachCurrentThread (g_vm );
18321842 return ;
18331843 }
18341844
@@ -1841,7 +1851,6 @@ void NativeCtxMissingCRLCallback(const char* url)
18411851 if ((* jenv )-> ExceptionOccurred (jenv )) {
18421852 (* jenv )-> ExceptionDescribe (jenv );
18431853 (* jenv )-> ExceptionClear (jenv );
1844- return ;
18451854 }
18461855
18471856 } else {
@@ -1853,6 +1862,9 @@ void NativeCtxMissingCRLCallback(const char* url)
18531862 (* jenv )-> ThrowNew (jenv , excClass ,
18541863 "Object reference invalid in NativeMissingCRLCallback" );
18551864 }
1865+
1866+ if (needsDetach )
1867+ (* g_vm )-> DetachCurrentThread (g_vm );
18561868}
18571869
18581870#endif /* HAVE_CRL */
@@ -6171,7 +6183,7 @@ unsigned int NativePskClientCb(WOLFSSL* ssl, const char* hint, char* identity,
61716183 }
61726184 }
61736185
6174- if (retval > 0 ) {
6186+ if (retval > 0 && retval <= ( jlong ) max_key_len ) {
61756187
61766188 /* copy jbyteArray into char key array */
61776189 (* jenv )-> GetByteArrayRegion (jenv , keyArray , 0 , retval , (jbyte * )key );
@@ -6239,10 +6251,27 @@ unsigned int NativePskClientCb(WOLFSSL* ssl, const char* hint, char* identity,
62396251 }
62406252 return 0 ;
62416253 }
6242- strcpy (identity , tmpString );
6254+ if (XSTRLEN (tmpString ) >= id_max_len ) {
6255+ (* jenv )-> ReleaseStringUTFChars (jenv , bufString ,
6256+ tmpString );
6257+ (* jenv )-> DeleteLocalRef (jenv , ctxRef );
6258+ (* jenv )-> DeleteLocalRef (jenv , hintString );
6259+ (* jenv )-> DeleteLocalRef (jenv , strBufObj );
6260+ (* jenv )-> DeleteLocalRef (jenv , keyArray );
6261+ (* jenv )-> DeleteLocalRef (jenv , bufString );
6262+ if (needsDetach ) {
6263+ (* g_vm )-> DetachCurrentThread (g_vm );
6264+ }
6265+ return 0 ;
6266+ }
6267+ XMEMCPY (identity , tmpString , XSTRLEN (tmpString ));
6268+ identity [XSTRLEN (tmpString )] = '\0' ;
62436269 (* jenv )-> ReleaseStringUTFChars (jenv , bufString , tmpString );
62446270 (* jenv )-> DeleteLocalRef (jenv , bufString );
62456271 }
6272+ else {
6273+ retval = 0 ;
6274+ }
62466275
62476276 /* delete local obj refs, detach JNIEnv from thread */
62486277 (* jenv )-> DeleteLocalRef (jenv , ctxRef );
@@ -6546,7 +6575,7 @@ unsigned int NativePskServerCb(WOLFSSL* ssl, const char* identity,
65466575 }
65476576 }
65486577
6549- if (retval > 0 ) {
6578+ if (retval > 0 && retval <= ( jlong ) max_key_len ) {
65506579
65516580 /* copy jbyteArray into char key array */
65526581 (* jenv )-> GetByteArrayRegion (jenv , keyArray , 0 , retval , (jbyte * )key );
@@ -6562,6 +6591,9 @@ unsigned int NativePskServerCb(WOLFSSL* ssl, const char* identity,
65626591 return 0 ;
65636592 }
65646593 }
6594+ else {
6595+ retval = 0 ;
6596+ }
65656597
65666598 /* delete local obj refs, detach JNIEnv from thread */
65676599 (* jenv )-> DeleteLocalRef (jenv , ctxRef );
@@ -6648,6 +6680,10 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLContext_setGroups
66486680
66496681 if (groupsSz == 0 || groupsSz > WOLFSSL_MAX_GROUP_COUNT ||
66506682 jniGroups == NULL ) {
6683+ if (jniGroups != NULL ) {
6684+ (* jenv )-> ReleaseIntArrayElements (jenv , groups ,
6685+ jniGroups , JNI_ABORT );
6686+ }
66516687 return (jint )BAD_FUNC_ARG ;
66526688 }
66536689
0 commit comments