Skip to content

Commit

Permalink
Merge pull request #235 from cconlon/jniDebug
Browse files Browse the repository at this point in the history
Add JNI Java level debugging with System Property: wolfssljni.debug=true
  • Loading branch information
JacobBarthelmeh authored Nov 22, 2024
2 parents fd51ab9 + 2fa0417 commit 6e69cb2
Show file tree
Hide file tree
Showing 35 changed files with 2,261 additions and 489 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ and what each enables.
| System Property | Default | To Enable | Description |
| --- | --- | --- | --- |
| wolfssl.debug | "false" | "true" | Enables native wolfSSL debug logging |
| wolfssljni.debug | "false" | "true" | Enables wolfJNI debug logging |
| wolfjsse.debug | "false" | "true | Enables wolfJSSE debug logging |
| wolfjsse.debugFormat | | "JSON" | Switches debug output format |
| wolfsslengine.debug | "false" | "true" | Enables SSLEngine debug logging |
Expand All @@ -239,6 +240,15 @@ System.setProperty("wolfjsse.debug", "true");
System.setProperty("wolfsslengine.debug", "true);
```

If wolfSSL JNI/JSSE debug System properties are changed at runtime after
the WolfSSLDebug class has already been initialized/used, applications need
to refresh the debug property values inside the WolfSSLDebug class. To do so,
after setting System properties, call:

```
WolfSSLDebug.refreshDebugFlags()
```

JDK debug logging can be enabled using the `-Djavax.net.debug=all` option.

### JSON Log Message Format
Expand Down
1 change: 0 additions & 1 deletion examples/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,6 @@ public void run(String[] args) {

/* init library */
WolfSSL sslLib = new WolfSSL();
sslLib.debuggingON();

/* set logging callback */
if (logCallback == 1) {
Expand Down
1 change: 0 additions & 1 deletion examples/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ public void run(String[] args) {

/* init library */
WolfSSL sslLib = new WolfSSL();
sslLib.debuggingON();

/* set logging callback */
if (logCallback == 1) {
Expand Down
2 changes: 1 addition & 1 deletion examples/provider/ClientJSSE.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.TrustManagerFactory;

import com.wolfssl.provider.jsse.WolfSSLDebug;
import com.wolfssl.WolfSSLDebug;
import com.wolfssl.provider.jsse.WolfSSLProvider;
import com.wolfssl.WolfSSL;
import java.security.PrivateKey;
Expand Down
2 changes: 1 addition & 1 deletion examples/provider/ServerJSSE.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import javax.net.ssl.TrustManagerFactory;

import com.wolfssl.WolfSSLException;
import com.wolfssl.provider.jsse.WolfSSLDebug;
import com.wolfssl.WolfSSLDebug;
import com.wolfssl.provider.jsse.WolfSSLProvider;

public class ServerJSSE {
Expand Down
27 changes: 26 additions & 1 deletion src/java/com/wolfssl/WolfSSL.java
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,12 @@ public enum TLS_VERSION {
* initialize correctly
*/
public WolfSSL() throws WolfSSLException {
int ret = init();
int ret;

WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, "initializing wolfSSL library");

ret = init();
if (ret != SSL_SUCCESS) {
throw new WolfSSLException("Failed to initialize wolfSSL library: "
+ ret);
Expand Down Expand Up @@ -663,6 +668,9 @@ public static void loadLibrary() throws UnsatisfiedLinkError {

int fipsLoaded = 0;

WolfSSLDebug.log(WolfSSL.class, WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, "loading native library: wolfssl");

String osName = System.getProperty("os.name");
if (osName != null && osName.toLowerCase().contains("win")) {
try {
Expand All @@ -680,6 +688,9 @@ public static void loadLibrary() throws UnsatisfiedLinkError {
}
}

WolfSSLDebug.log(WolfSSL.class, WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, "loading native library: wolfssljni");

/* Load wolfssljni library */
System.loadLibrary("wolfssljni");
}
Expand All @@ -695,6 +706,10 @@ public static void loadLibrary() throws UnsatisfiedLinkError {
* @throws UnsatisfiedLinkError if the library is not found.
*/
public static void loadLibrary(String libName) throws UnsatisfiedLinkError {

WolfSSLDebug.log(WolfSSL.class, WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, "loading native library by name: " + libName);

System.loadLibrary(libName);
}

Expand All @@ -716,6 +731,10 @@ public static void loadLibrary(String libName) throws UnsatisfiedLinkError {
*/
public static void loadLibraryAbsolute(String libPath)
throws UnsatisfiedLinkError {

WolfSSLDebug.log(WolfSSL.class, WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, "loading native library by path: " + libPath);

System.load(libPath);
}

Expand Down Expand Up @@ -1489,6 +1508,9 @@ public static String[] getCiphersAvailableIana(TLS_VERSION version) {
*/
public static int cryptoCbRegisterDevice(int devId) {

WolfSSLDebug.log(WolfSSL.class, WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, "registering crypto cb devId: " + devId);

return wc_CryptoCb_RegisterDevice(devId);
}

Expand All @@ -1500,6 +1522,9 @@ public static int cryptoCbRegisterDevice(int devId) {
*/
public static int cryptoCbUnRegisterDevice(int devId) {

WolfSSLDebug.log(WolfSSL.class, WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, "unregistering crypto cb devId: " + devId);

wc_CryptoCb_UnRegisterDevice(devId);

return 0;
Expand Down
31 changes: 31 additions & 0 deletions src/java/com/wolfssl/WolfSSLCertManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;
import java.security.cert.CertificateEncodingException;
import com.wolfssl.WolfSSLDebug;
import com.wolfssl.WolfSSLException;

/**
Expand Down Expand Up @@ -61,11 +62,15 @@ static native int CertManagerVerifyBuffer(long cm, byte[] in, long sz,
* @throws WolfSSLException if unable to create new manager
*/
public WolfSSLCertManager() throws WolfSSLException {

cmPtr = CertManagerNew();
if (cmPtr == 0) {
throw new WolfSSLException("Failed to create WolfSSLCertManager");
}
this.active = true;

WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, cmPtr, "creating new WolfSSLCertManager");
}

/**
Expand Down Expand Up @@ -99,6 +104,10 @@ public synchronized int CertManagerLoadCA(String f, String d)
confirmObjectIsActive();

synchronized (cmLock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.cmPtr, "entered CertManagerLoadCA(" +
f + ", " + d + "");

return CertManagerLoadCA(this.cmPtr, f, d);
}
}
Expand All @@ -121,6 +130,11 @@ public synchronized int CertManagerLoadCABuffer(
confirmObjectIsActive();

synchronized (cmLock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.cmPtr,
"entered CertManagerLoadCABuffer(sz: " + sz +
", format: " + format + "");

return CertManagerLoadCABuffer(this.cmPtr, in, sz, format);
}
}
Expand All @@ -142,6 +156,10 @@ public synchronized int CertManagerLoadCAKeyStore(KeyStore ks)

confirmObjectIsActive();

WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.cmPtr,
"entered CertManagerLoadCAKeyStore(" + ks + ")");

if (ks == null) {
throw new WolfSSLException("Input KeyStore is null");
}
Expand Down Expand Up @@ -194,6 +212,10 @@ public synchronized int CertManagerUnloadCAs()
confirmObjectIsActive();

synchronized (cmLock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.cmPtr,
"entered CertManagerUnloadCAs()");

return CertManagerUnloadCAs(this.cmPtr);
}
}
Expand All @@ -217,6 +239,11 @@ public synchronized int CertManagerVerifyBuffer(
confirmObjectIsActive();

synchronized (cmLock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.cmPtr,
"entered CertManagerVerifyBuffer(sz: " + sz + ", format: " +
format + ")");

return CertManagerVerifyBuffer(this.cmPtr, in, sz, format);
}
}
Expand All @@ -228,12 +255,16 @@ public synchronized int CertManagerVerifyBuffer(
public synchronized void free() throws IllegalStateException {

synchronized (stateLock) {

if (this.active == false) {
/* already freed, just return */
return;
}

synchronized (cmLock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.cmPtr, "entered free()");

/* free native resources */
CertManagerFree(this.cmPtr);

Expand Down
Loading

0 comments on commit 6e69cb2

Please sign in to comment.