Skip to content

Commit e306e53

Browse files
committed
Handle BC-FIPS related exception and propagate exception causes
When testing with BC-FIPS PEMEncodeable was failing in some unexpected ways. in the first if we could not convert due to a PKCSException or InvalidKeySpecException then the cause was logged and a new exception without details was thrown. This means the underlying cause is lost when using any exception in a FormValidation. In the second case when using a key that had too short of salt an org.bouncycastle.crypto.fips.FipsUnapprovedOperationError was thrown which being an error would ripple up and cause an unexpected error. We now catch this error and wrap it in an UnrecoverableKeyException so it can be handled by the caller
1 parent e1089fe commit e306e53

1 file changed

Lines changed: 15 additions & 5 deletions

File tree

src/main/java/jenkins/bouncycastle/api/PEMEncodable.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
import java.util.ArrayList;
5151
import java.util.Arrays;
5252
import java.util.List;
53-
import java.util.logging.Level;
5453
import java.util.logging.Logger;
5554
import java.util.stream.Collectors;
5655
import org.apache.commons.codec.binary.Hex;
@@ -245,13 +244,24 @@ private static final PEMEncodable convertedPemToPemDecodable(Object object, char
245244
+ object.getClass().getName());
246245
}
247246
} catch (PKCSException | InvalidKeySpecException e) {
248-
LOGGER.log(Level.WARNING, "Could not read PEM encrypted information", e);
249-
throw new UnrecoverableKeyException();
247+
UnrecoverableKeyException unrecoverableKeyEx = new UnrecoverableKeyException(e.getMessage());
248+
unrecoverableKeyEx.initCause(e);
249+
throw unrecoverableKeyEx;
250250
} catch (CertificateException e) {
251251
throw new IOException("Could not read certificate", e);
252252
} catch (NoSuchAlgorithmException e) {
253-
throw new AssertionError(
254-
"RSA algorithm support is mandated by Java Language Specification. See https://docs.oracle.com/javase/7/docs/api/java/security/KeyFactory.html");
253+
throw new IOException("Algorithm required for parsing is not implemented", e);
254+
} catch (AssertionError e) {
255+
// when using the FIPS BC variety org.bouncycastle.crypto.fips.FipsUnapprovedOperationError can be throw
256+
// if the encoded object is not FIPS compliant.
257+
// there are no known subclasses so just match on the classname.
258+
if (e.getClass().getName().equals("org.bouncycastle.crypto.fips.FipsUnapprovedOperationError")) {
259+
UnrecoverableKeyException unrecoverableKeyEx =
260+
new UnrecoverableKeyException("Provided Object is not FIPS 140 compliant");
261+
unrecoverableKeyEx.initCause(e);
262+
throw unrecoverableKeyEx;
263+
}
264+
throw e;
255265
}
256266
}
257267

0 commit comments

Comments
 (0)