Skip to content

Commit 25723c3

Browse files
committed
JCE: register default FIPS error callback in WolfCryptProvider
1 parent 7d24d30 commit 25723c3

1 file changed

Lines changed: 54 additions & 0 deletions

File tree

src/main/java/com/wolfssl/provider/jce/WolfCryptProvider.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.security.Security;
2626
import com.wolfssl.wolfcrypt.FeatureDetect;
2727
import com.wolfssl.wolfcrypt.Fips;
28+
import com.wolfssl.wolfcrypt.WolfCryptError;
2829
import com.wolfssl.wolfcrypt.WolfSSLX509StoreCtx;
2930

3031
/**
@@ -34,6 +35,51 @@ public final class WolfCryptProvider extends Provider {
3435

3536
private static final long serialVersionUID = 1L;
3637

38+
/**
39+
* Default FIPS error callback for wolfJCE provider.
40+
*
41+
* Logs FIPS errors to aid in debugging module failures. Registered
42+
* automatically when wolfJCE provider is instantiated with FIPS wolfCrypt.
43+
*/
44+
private static class JCEFIPSErrorCallback implements Fips.ErrorCallback {
45+
46+
/* Track last error code to suppress duplicate messages. Native
47+
* wolfCrypt may call the callback with different error codes during
48+
* a failure sequence. We log each unique code once. */
49+
private static volatile int lastErr = 0;
50+
51+
/**
52+
* Called by native wolfCrypt when FIPS error occurs.
53+
*
54+
* @param ok 1 if verification passed, otherwise 0
55+
* @param err wolfCrypt FIPS error code
56+
* @param hash expected verifyCore hash value
57+
*/
58+
@Override
59+
public void errorCallback(int ok, int err, String hash) {
60+
61+
if (err == lastErr) {
62+
return;
63+
}
64+
lastErr = err;
65+
66+
String errStr = WolfCryptError.fromInt(err).getDescription();
67+
68+
System.err.println("wolfJCE FIPS error: ok = " + ok + ", err = " +
69+
err + " (" + errStr + "), hash = " + hash);
70+
71+
if (err == WolfCryptError.IN_CORE_FIPS_E.getCode()) {
72+
System.err.println("wolfJCE FIPS: in core integrity hash " +
73+
"check failure. Copy hash above into verifyCore[] in " +
74+
"fips_test.c and rebuild");
75+
}
76+
77+
WolfCryptDebug.log(JCEFIPSErrorCallback.class, WolfCryptDebug.ERROR,
78+
() -> "FIPS error: ok = " + ok + ", err = " + err + " (" +
79+
errStr + "), hash = " + hash);
80+
}
81+
}
82+
3783
/**
3884
* Create new WolfCryptProvider object
3985
*/
@@ -45,6 +91,14 @@ public WolfCryptProvider() {
4591
WolfCryptDebug.refreshDebugFlags();
4692

4793
registerServices();
94+
95+
/* Register default FIPS error callback if FIPS enabled. */
96+
if (Fips.enabled) {
97+
Fips.wolfCrypt_SetCb_fips(new JCEFIPSErrorCallback());
98+
99+
WolfCryptDebug.log(getClass(), WolfCryptDebug.INFO,
100+
() -> "Registered wolfCrypt FIPS error callback");
101+
}
48102
}
49103

50104
/**

0 commit comments

Comments
 (0)