Skip to content

Commit 6e91223

Browse files
Merge pull request #104 from cconlon/noFilesystemFixes
Native JNI fixes for NO_FILESYSTEM
2 parents d978cca + ec5f91d commit 6e91223

10 files changed

Lines changed: 173 additions & 36 deletions

native/com_wolfssl_WolfSSL.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,19 @@ JNIEXPORT jboolean JNICALL Java_com_wolfssl_WolfSSL_RsaEnabled
362362
#endif
363363
}
364364

365+
JNIEXPORT jboolean JNICALL Java_com_wolfssl_WolfSSL_FileSystemEnabled
366+
(JNIEnv* jenv, jclass jcl)
367+
{
368+
(void)jenv;
369+
(void)jcl;
370+
371+
#ifdef NO_FILESYSTEM
372+
return JNI_FALSE;
373+
#else
374+
return JNI_TRUE;
375+
#endif
376+
}
377+
365378
JNIEXPORT jlong JNICALL Java_com_wolfssl_WolfSSL_SSLv3_1ServerMethod
366379
(JNIEnv* jenv, jclass jcl)
367380
{

native/com_wolfssl_WolfSSL.h

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

native/com_wolfssl_WolfSSLCertManager.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ JNIEXPORT void JNICALL Java_com_wolfssl_WolfSSLCertManager_CertManagerFree
5050
JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLCertManager_CertManagerLoadCA
5151
(JNIEnv* jenv, jclass jcl, jlong cmPtr, jstring f, jstring d)
5252
{
53+
#ifndef NO_FILESYSTEM
5354
int ret;
5455
const char* certFile = NULL;
5556
const char* certPath = NULL;
@@ -69,6 +70,14 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLCertManager_CertManagerLoadCA
6970
(*jenv)->ReleaseStringUTFChars(jenv, d, certPath);
7071

7172
return (jint)ret;
73+
#else
74+
(void)jenv;
75+
(void)jcl;
76+
(void)cmPtr;
77+
(void)f;
78+
(void)d;
79+
return NOT_COMPILED_IN;
80+
#endif
7281
}
7382

7483
JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLCertManager_CertManagerLoadCABuffer

native/com_wolfssl_WolfSSLCertificate.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ JNIEXPORT jlong JNICALL Java_com_wolfssl_WolfSSLCertificate_X509_1load_1certific
6363
JNIEXPORT jlong JNICALL Java_com_wolfssl_WolfSSLCertificate_X509_1load_1certificate_1file
6464
(JNIEnv* jenv, jclass jcl, jstring filename, jint format)
6565
{
66+
#ifndef NO_FILESYSTEM
6667
WOLFSSL_X509* x509 = NULL;
6768
const char* path = NULL;
6869
(void)jcl;
@@ -80,6 +81,13 @@ JNIEXPORT jlong JNICALL Java_com_wolfssl_WolfSSLCertificate_X509_1load_1certific
8081
(*jenv)->ReleaseStringUTFChars(jenv, filename, path);
8182

8283
return (jlong)(uintptr_t)x509;
84+
#else
85+
(void)jenv;
86+
(void)jcl;
87+
(void)filename;
88+
(void)format;
89+
return 0;
90+
#endif
8391
}
8492

8593
JNIEXPORT jbyteArray JNICALL Java_com_wolfssl_WolfSSLCertificate_X509_1get_1der

native/com_wolfssl_WolfSSLContext.c

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ JNIEXPORT jlong JNICALL Java_com_wolfssl_WolfSSLContext_newContext(JNIEnv* jenv,
162162
JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLContext_useCertificateFile
163163
(JNIEnv* jenv, jobject jcl, jlong ctxPtr, jstring file, jint format)
164164
{
165+
#ifndef NO_FILESYSTEM
165166
jint ret = 0;
166167
jclass excClass;
167168
const char* certFile;
@@ -192,11 +193,20 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLContext_useCertificateFile
192193
(*jenv)->ReleaseStringUTFChars(jenv, file, certFile);
193194

194195
return ret;
196+
#else
197+
(void)jenv;
198+
(void)jcl;
199+
(void)ctxPtr;
200+
(void)file;
201+
(void)format;
202+
return NOT_COMPILED_IN;
203+
#endif
195204
}
196205

197206
JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLContext_usePrivateKeyFile
198207
(JNIEnv* jenv, jobject jcl, jlong ctxPtr, jstring file, jint format)
199208
{
209+
#ifndef NO_FILESYSTEM
200210
jint ret = 0;
201211
jclass excClass;
202212
const char* keyFile;
@@ -227,11 +237,20 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLContext_usePrivateKeyFile
227237
(*jenv)->ReleaseStringUTFChars(jenv, file, keyFile);
228238

229239
return ret;
240+
#else
241+
(void)jenv;
242+
(void)jcl;
243+
(void)ctxPtr;
244+
(void)file;
245+
(void)format;
246+
return NOT_COMPILED_IN;
247+
#endif
230248
}
231249

232250
JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLContext_loadVerifyLocations
233251
(JNIEnv* jenv, jobject jcl, jlong ctxPtr, jstring file, jstring path)
234252
{
253+
#ifndef NO_FILESYSTEM
235254
jint ret = 0;
236255
jclass excClass;
237256
const char* caFile;
@@ -276,11 +295,20 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLContext_loadVerifyLocations
276295
(*jenv)->ReleaseStringUTFChars(jenv, path, caPath);
277296

278297
return ret;
298+
#else
299+
(void)jenv;
300+
(void)jcl;
301+
(void)ctxPtr;
302+
(void)file;
303+
(void)path;
304+
return NOT_COMPILED_IN;
305+
#endif
279306
}
280307

281308
JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLContext_useCertificateChainFile
282309
(JNIEnv* jenv, jobject jcl, jlong ctxPtr, jstring file)
283310
{
311+
#ifndef NO_FILESYSTEM
284312
jint ret = 0;
285313
jclass excClass;
286314
const char* chainFile;
@@ -312,6 +340,13 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLContext_useCertificateChainFile
312340
(*jenv)->ReleaseStringUTFChars(jenv, file, chainFile);
313341

314342
return ret;
343+
#else
344+
(void)jenv;
345+
(void)jcl;
346+
(void)ctxPtr;
347+
(void)file;
348+
return NOT_COMPILED_IN;
349+
#endif
315350
}
316351

317352
JNIEXPORT void JNICALL Java_com_wolfssl_WolfSSLContext_freeContext
@@ -1503,7 +1538,7 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLContext_loadCRL
15031538
(JNIEnv* jenv, jobject jcl, jlong ctxPtr, jstring path, jint type,
15041539
jint monitor)
15051540
{
1506-
#ifdef HAVE_CRL
1541+
#if defined(HAVE_CRL) && !defined(NO_FILESYSTEM)
15071542
int ret;
15081543
const char* crlPath;
15091544
WOLFSSL_CTX* ctx = (WOLFSSL_CTX*)(uintptr_t)ctxPtr;

native/com_wolfssl_WolfSSLSession.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLSession_setFd(JNIEnv* jenv,
377377
JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLSession_useCertificateFile
378378
(JNIEnv* jenv, jobject jcl, jlong sslPtr, jstring file, jint format)
379379
{
380-
#ifdef OPENSSL_EXTRA
380+
#if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM)
381381
jint ret = 0;
382382
const char* certFile;
383383
WOLFSSL* ssl = (WOLFSSL*)(uintptr_t)sslPtr;
@@ -411,7 +411,7 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLSession_useCertificateFile
411411
JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLSession_usePrivateKeyFile
412412
(JNIEnv* jenv, jobject jcl, jlong sslPtr, jstring file, jint format)
413413
{
414-
#ifdef OPENSSL_EXTRA
414+
#if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM)
415415
jint ret = 0;
416416
const char* keyFile;
417417
WOLFSSL* ssl = (WOLFSSL*)(uintptr_t)sslPtr;
@@ -445,7 +445,7 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLSession_usePrivateKeyFile
445445
JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLSession_useCertificateChainFile
446446
(JNIEnv* jenv, jobject jcl, jlong sslPtr, jstring file)
447447
{
448-
#ifdef OPENSSL_EXTRA
448+
#if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM)
449449
jint ret = 0;
450450
const char* chainFile;
451451
WOLFSSL* ssl = (WOLFSSL*)(uintptr_t)sslPtr;
@@ -1855,7 +1855,7 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLSession_setTmpDH
18551855
JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLSession_setTmpDHFile
18561856
(JNIEnv* jenv, jobject jcl, jlong sslPtr, jstring file, jint format)
18571857
{
1858-
#ifndef NO_DH
1858+
#if !defined(NO_DH) && !defined(NO_FILESYSTEM)
18591859
int ret;
18601860
const char* fname;
18611861
jclass excClass;
@@ -2098,7 +2098,7 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLSession_loadCRL
20982098
(JNIEnv* jenv, jobject jcl, jlong sslPtr, jstring path, jint type,
20992099
jint monitor)
21002100
{
2101-
#ifdef HAVE_CRL
2101+
#if defined(HAVE_CRL) && !defined(NO_FILESYSTEM)
21022102
int ret;
21032103
const char* crlPath;
21042104
jclass excClass;

src/java/com/wolfssl/WolfSSL.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,13 @@ public static void loadLibraryAbsolute(String libPath)
507507
*/
508508
public static native boolean RsaEnabled();
509509

510+
/**
511+
* Tests if filesystem support has been compiled into the wolfSSL library.
512+
*
513+
* @return 1 if enabled, otherwise 0 if NO_FILESYSTEM has been defined.
514+
*/
515+
public static native boolean FileSystemEnabled();
516+
510517
/* ---------------- native SSL/TLS version functions ---------------- */
511518

512519
/**

src/test/com/wolfssl/provider/jsse/test/WolfSSLX509Test.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import org.junit.BeforeClass;
5555
import org.junit.Test;
5656

57+
import com.wolfssl.WolfSSL;
5758
import com.wolfssl.WolfSSLException;
5859
import com.wolfssl.provider.jsse.WolfSSLProvider;
5960
import com.wolfssl.provider.jsse.WolfSSLX509;
@@ -145,6 +146,12 @@ public void testExtensions() {
145146

146147
System.out.print("\tTesting x509 ext");
147148

149+
/* skip if wolfSSL compiled with NO_FILESYSTEM */
150+
if (WolfSSL.FileSystemEnabled() == false) {
151+
pass("\t\t... skipped");
152+
return;
153+
}
154+
148155
try {
149156
x509 = new WolfSSLX509(tf.googleCACert);
150157

@@ -220,6 +227,13 @@ public void testX509XValidity() {
220227
WolfSSLX509X x509;
221228

222229
System.out.print("\tTesting X509X validity");
230+
231+
/* skip if wolfSSL compiled with NO_FILESYSTEM */
232+
if (WolfSSL.FileSystemEnabled() == false) {
233+
pass("\t\t... skipped");
234+
return;
235+
}
236+
223237
try {
224238
x509 = new WolfSSLX509X(tf.googleCACert);
225239
x509.checkValidity();
@@ -239,6 +253,13 @@ public void testTBS() {
239253
WolfSSLX509 x509;
240254

241255
System.out.print("\tTesting TBS");
256+
257+
/* skip if wolfSSL compiled with NO_FILESYSTEM */
258+
if (WolfSSL.FileSystemEnabled() == false) {
259+
pass("\t\t\t... skipped");
260+
return;
261+
}
262+
242263
try {
243264
x509 = new WolfSSLX509(tf.googleCACert);
244265
tbs = x509.getTBSCertificate();
@@ -575,6 +596,12 @@ public void testSubjectAlternativeNames() {
575596

576597
System.out.print("\tTesting getting alt names");
577598

599+
/* skip if wolfSSL compiled with NO_FILESYSTEM */
600+
if (WolfSSL.FileSystemEnabled() == false) {
601+
pass("\t... skipped");
602+
return;
603+
}
604+
578605
/* populate known alt name list for example.com cert, for comparison */
579606
List<String> expected = new ArrayList<>();
580607
expected.add("www.example.org");

src/test/com/wolfssl/test/WolfSSLCertificateTest.java

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,15 @@ public void testWolfSSLCertificate() throws WolfSSLException {
6868
test_WolfSSLCertificate_new_pemArray();
6969
test_runCertTestsAfterConstructor();
7070

71-
/* WolfSSLCertificate(byte[] pem) */
72-
test_WolfSSLCertificate_new_derFile();
73-
test_runCertTestsAfterConstructor();
74-
75-
/* WolfSSLCertificate(String pem) */
76-
test_WolfSSLCertificate_new_pemFile();
77-
test_runCertTestsAfterConstructor();
71+
if (WolfSSL.FileSystemEnabled() == true) {
72+
/* WolfSSLCertificate(byte[] pem) */
73+
test_WolfSSLCertificate_new_derFile();
74+
test_runCertTestsAfterConstructor();
75+
76+
/* WolfSSLCertificate(String pem) */
77+
test_WolfSSLCertificate_new_pemFile();
78+
test_runCertTestsAfterConstructor();
79+
}
7880
}
7981

8082

@@ -191,6 +193,19 @@ public void test_WolfSSLCertificate_new_pemFile() {
191193
System.out.println("\t... passed");
192194
}
193195

196+
private byte[] fileToByteArray(String filePath)
197+
throws IOException {
198+
File f = new File(filePath);
199+
byte[] fBytes = null;
200+
201+
InputStream stream = new FileInputStream(f);
202+
fBytes = new byte[(int) f.length()];
203+
stream.read(fBytes, 0, fBytes.length);
204+
stream.close();
205+
206+
return fBytes;
207+
}
208+
194209

195210
public void test_getSerial() {
196211
byte[] expected = new byte[]{
@@ -501,7 +516,13 @@ public void test_getKeyUsage() {
501516
int i;
502517
boolean[] kuse;
503518

504-
ext = new WolfSSLCertificate(this.external);
519+
if (WolfSSL.FileSystemEnabled() == true) {
520+
ext = new WolfSSLCertificate(this.external);
521+
} else {
522+
ext = new WolfSSLCertificate(fileToByteArray(this.external),
523+
WolfSSL.SSL_FILETYPE_ASN1);
524+
}
525+
505526
kuse = ext.getKeyUsage();
506527
if (kuse == null) {
507528
System.out.println("\t\t... failed");
@@ -516,8 +537,9 @@ public void test_getKeyUsage() {
516537
}
517538
}
518539
ext.free();
519-
} catch (WolfSSLException ex) {
520-
Logger.getLogger(WolfSSLCertificateTest.class.getName()).log(Level.SEVERE, null, ex);
540+
} catch (Exception ex) {
541+
Logger.getLogger(WolfSSLCertificateTest.class.getName()).log(
542+
Level.SEVERE, null, ex);
521543
System.out.println("\t\t... failed");
522544
fail("Error loading external certificate");
523545
}

0 commit comments

Comments
 (0)