Skip to content

Commit 7d24d30

Browse files
committed
JCE: include FIPS module status in FIPS_NOT_ALLOWED_E exceptions
1 parent 8fb4d37 commit 7d24d30

1 file changed

Lines changed: 34 additions & 1 deletion

File tree

src/main/java/com/wolfssl/wolfcrypt/WolfCryptException.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,45 @@ public WolfCryptException(String reason) {
4747
* @param code wolfCrypt error code
4848
*/
4949
public WolfCryptException(int code) {
50-
super(WolfCryptError.fromInt(code).getDescription());
50+
super(getErrorMessage(code));
5151

5252
this.error = WolfCryptError.fromInt(code);
5353
this.code = code;
5454
}
5555

56+
/**
57+
* Build exception message from error code. For FIPS not allowed errors
58+
* (-197), queries and appends the current FIPS module status to help
59+
* diagnose the root cause.
60+
*
61+
* @param code wolfCrypt error code
62+
* @return descriptive error message string
63+
*/
64+
private static String getErrorMessage(int code) {
65+
66+
String msg = WolfCryptError.fromInt(code).getDescription();
67+
68+
/* Get module status for root cause of FIPS not allowed failure */
69+
if (code == WolfCryptError.FIPS_NOT_ALLOWED_E.getCode()) {
70+
try {
71+
if (Fips.enabled) {
72+
int status = Fips.wolfCrypt_GetStatus_fips();
73+
if (status != 0) {
74+
String statusDesc =
75+
WolfCryptError.fromInt(status).getDescription();
76+
msg += " [FIPS module status: " + status + " (" +
77+
statusDesc + ")]";
78+
}
79+
}
80+
}
81+
catch (Exception e) {
82+
/* FIPS status query not available */
83+
}
84+
}
85+
86+
return msg;
87+
}
88+
5689
/**
5790
* Create new WolfCryptException from reason and cause
5891
*

0 commit comments

Comments
 (0)