Skip to content

Commit e27176e

Browse files
authored
Merge pull request #155 from jtnord/better-exception-handling
Handle BC-FIPS related exception and propagate exception causes
2 parents e1089fe + e306e53 commit e27176e

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)