Skip to content

Commit 372f6c2

Browse files
authored
Merge pull request #346 from JeremiahM37/fenrir-fixes
Fenrir fixes
2 parents 2c1bdd1 + c4ebc61 commit 372f6c2

4 files changed

Lines changed: 74 additions & 21 deletions

File tree

IDE/Android/app/src/main/cpp/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ if ("${WOLFSSL_PKG_TYPE}" MATCHES "normal")
276276
list(REMOVE_ITEM CRYPTO_SOURCES ${wolfssl_DIR}/wolfcrypt/src/evp.c)
277277
list(REMOVE_ITEM CRYPTO_SOURCES ${wolfssl_DIR}/wolfcrypt/src/evp_pk.c)
278278
list(REMOVE_ITEM CRYPTO_SOURCES ${wolfssl_DIR}/wolfcrypt/src/misc.c)
279+
list(REMOVE_ITEM CRYPTO_SOURCES ${wolfssl_DIR}/wolfcrypt/src/asn_orig.c)
279280

280281
elseif("${WOLFSSL_PKG_TYPE}" MATCHES "fipsready")
281282
# FIPS Ready needs to explicitly order files for in-core integrity check to work properly.

native/com_wolfssl_WolfSSL.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1989,7 +1989,7 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSL_getPkcs8TraditionalOffset
19891989
if (inBuf == NULL) {
19901990
return MEMORY_E;
19911991
}
1992-
XMEMSET(inBuf, 0, DYNAMIC_TYPE_TMP_BUFFER);
1992+
XMEMSET(inBuf, 0, (long)sz);
19931993

19941994
(*jenv)->GetByteArrayRegion(jenv, in, 0, (jsize)sz, (jbyte*)inBuf);
19951995
if ((*jenv)->ExceptionOccurred(jenv)) {

native/com_wolfssl_WolfSSLContext.c

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

native/com_wolfssl_WolfSSLSession.c

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3687,6 +3687,7 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLSession_usePrivateKeyBuffer
36873687

36883688
ret = wolfSSL_use_PrivateKey_buffer(ssl, buff, (long)sz, format);
36893689

3690+
XMEMSET(buff, 0, (long)sz);
36903691
XFREE(buff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
36913692

36923693
return ret;
@@ -3916,6 +3917,7 @@ void NativeMissingCRLCallback(const char* url)
39163917
{
39173918
JNIEnv* jenv;
39183919
jint vmret = 0;
3920+
int needsDetach = 0;
39193921
jclass crlClass = NULL;
39203922
jmethodID crlMethod = NULL;
39213923
jobjectRefType refcheck;
@@ -3931,9 +3933,12 @@ void NativeMissingCRLCallback(const char* url)
39313933
#endif
39323934
if (vmret) {
39333935
printf("Failed to attach JNIEnv to thread\n");
3936+
return;
39343937
}
3938+
needsDetach = 1;
39353939
} else if (vmret != JNI_OK) {
39363940
printf("Unable to get JNIEnv from JavaVM\n");
3941+
return;
39373942
}
39383943

39393944
/* check if our stored object reference is valid */
@@ -3945,6 +3950,8 @@ void NativeMissingCRLCallback(const char* url)
39453950
if (!crlClass) {
39463951
throwWolfSSLException(jenv,
39473952
"Can't get native WolfSSLMissingCRLCallback class reference");
3953+
if (needsDetach)
3954+
(*g_vm)->DetachCurrentThread(g_vm);
39483955
return;
39493956
}
39503957

@@ -3959,6 +3966,8 @@ void NativeMissingCRLCallback(const char* url)
39593966

39603967
throwWolfSSLException(jenv,
39613968
"Error getting missingCRLCallback method from JNI");
3969+
if (needsDetach)
3970+
(*g_vm)->DetachCurrentThread(g_vm);
39623971
return;
39633972
}
39643973

@@ -3970,7 +3979,6 @@ void NativeMissingCRLCallback(const char* url)
39703979
if ((*jenv)->ExceptionOccurred(jenv)) {
39713980
(*jenv)->ExceptionDescribe(jenv);
39723981
(*jenv)->ExceptionClear(jenv);
3973-
return;
39743982
}
39753983

39763984
} else {
@@ -3982,6 +3990,9 @@ void NativeMissingCRLCallback(const char* url)
39823990
throwWolfSSLException(jenv,
39833991
"Object reference invalid in NativeMissingCRLCallback");
39843992
}
3993+
3994+
if (needsDetach)
3995+
(*g_vm)->DetachCurrentThread(g_vm);
39853996
}
39863997

39873998
#endif /* HAVE_CRL */
@@ -5896,34 +5907,39 @@ int NativeALPNSelectCb(WOLFSSL *ssl, const unsigned char **out,
58965907
/* get char* from jstring */
58975908
selectedProtoCharArr = (*jenv)->GetStringUTFChars(jenv,
58985909
selectedProto, 0);
5899-
selectedProtoCharArrSz = (int)XSTRLEN(selectedProtoCharArr);
5900-
5901-
/* see if selected ALPN protocol is in original sent list */
5910+
/* see if selected ALPN protocol is in original sent list.
5911+
* Wire format is length-prefixed: (LEN|PROTO|LEN|PROTO|...) */
59025912
if (selectedProtoCharArr != NULL) {
5903-
for (idx = 0; idx < inlen; idx++) {
5904-
if (idx + selectedProtoCharArrSz > inlen) {
5905-
/* No match found, fatal error. in not long enough for
5906-
* search. Reset ret to error condition, match not set
5907-
* correctly */
5913+
selectedProtoCharArrSz = (int)XSTRLEN(selectedProtoCharArr);
5914+
idx = 0;
5915+
while (idx < inlen) {
5916+
unsigned char protoLen = in[idx];
5917+
if (idx + 1 + protoLen > inlen) {
59085918
ret = SSL_TLSEXT_ERR_ALERT_FATAL;
59095919
break;
59105920
}
5911-
if (XMEMCMP(in + idx, selectedProtoCharArr,
5921+
if (protoLen == selectedProtoCharArrSz &&
5922+
XMEMCMP(in + idx + 1, selectedProtoCharArr,
59125923
selectedProtoCharArrSz) == 0) {
5913-
/* Match found. Format of input array is length byte of
5914-
* ALPN protocol, followed by ALPN protocol,
5915-
* ie (LEN+ALPN|LEN+ALPN|...) We set *out to ALPN selected
5916-
* protocol and *outlen to length of protocol (idx - 1) */
5917-
*out = in + idx;
5918-
*outlen = in[idx - 1];
5924+
*out = in + idx + 1;
5925+
*outlen = protoLen;
59195926
break;
59205927
}
5928+
idx += 1 + protoLen;
5929+
}
5930+
if (idx >= inlen && ret != SSL_TLSEXT_ERR_ALERT_FATAL) {
5931+
ret = SSL_TLSEXT_ERR_ALERT_FATAL;
59215932
}
59225933
}
59235934
else {
59245935
/* Not able to get selected ALPN protocol from Java, fatal error */
59255936
ret = SSL_TLSEXT_ERR_ALERT_FATAL;
59265937
}
5938+
5939+
if (selectedProtoCharArr != NULL) {
5940+
(*jenv)->ReleaseStringUTFChars(jenv, selectedProto,
5941+
selectedProtoCharArr);
5942+
}
59275943
}
59285944

59295945
return ret;

0 commit comments

Comments
 (0)