diff options
author | Pete Bentley <prb@google.com> | 2023-01-10 12:43:08 +0000 |
---|---|---|
committer | Pete Bentley <prb@google.com> | 2023-01-10 12:43:08 +0000 |
commit | 10942f23a051f9089baa013e2201f41b4d17a6ea (patch) | |
tree | c03d3e6877f195b33c8f03634cbf19b591c10d43 | |
parent | 95b3809836810a0b491baf15c4d966dda2932322 (diff) | |
download | bouncycastle-10942f23a051f9089baa013e2201f41b4d17a6ea.tar.gz |
Don't throw exceptions from BCPrivate provider.
I originally thought this was fine, but it could cause confusion
for developers encountering a PKCS#12 file using an unknown
algorithm. Instead, throw the original NoSuchAlgorithmException
from the BC Provider.
Bug: 230750823
Test: atest CtsLibcoreTestCases:tests.targets.security.KeyStorePkcs7FormatTest
Change-Id: I8a6d44d0e59bf0fb029ced4b8aa47908194bc161
3 files changed, 69 insertions, 21 deletions
diff --git a/bcprov/src/main/java/org/bouncycastle/jcajce/util/BCJcaJceHelper.java b/bcprov/src/main/java/org/bouncycastle/jcajce/util/BCJcaJceHelper.java index 9a41d31c..13205ec2 100644 --- a/bcprov/src/main/java/org/bouncycastle/jcajce/util/BCJcaJceHelper.java +++ b/bcprov/src/main/java/org/bouncycastle/jcajce/util/BCJcaJceHelper.java @@ -48,13 +48,21 @@ public class BCJcaJceHelper // // If code is using a BCJcajceHelper to ensure it gets its implementation from BC, then // also search in the privately provided algorithms if not found in the main set. + // + // If any error occurs while searching the private Provider, typically a + // NoSuchAlgorithmException being thrown, then the original NoSuchAlgorithmException + // from the BC Provider is thrown for consistency. @Override public Cipher createCipher(String algorithm) throws NoSuchAlgorithmException, NoSuchPaddingException { try { return super.createCipher(algorithm); - } catch (NoSuchAlgorithmException e) { - return Cipher.getInstance(algorithm, getPrivateProvider()); + } catch (NoSuchAlgorithmException originalException) { + try { + return Cipher.getInstance(algorithm, getPrivateProvider()); + } catch (Throwable throwable) { + throw originalException; + } } } @@ -63,8 +71,12 @@ public class BCJcaJceHelper throws NoSuchAlgorithmException { try { return super.createSecretKeyFactory(algorithm); - } catch (NoSuchAlgorithmException e) { - return SecretKeyFactory.getInstance(algorithm, getPrivateProvider()); + } catch (NoSuchAlgorithmException originalException) { + try { + return SecretKeyFactory.getInstance(algorithm, getPrivateProvider()); + } catch (Throwable throwable) { + throw originalException; + } } } @@ -72,8 +84,12 @@ public class BCJcaJceHelper public Mac createMac(String algorithm) throws NoSuchAlgorithmException { try { return super.createMac(algorithm); - } catch (NoSuchAlgorithmException e) { - return Mac.getInstance(algorithm, getPrivateProvider()); + } catch (NoSuchAlgorithmException originalException) { + try { + return Mac.getInstance(algorithm, getPrivateProvider()); + } catch (Throwable throwable) { + throw originalException; + } } } @@ -81,7 +97,7 @@ public class BCJcaJceHelper if (provider instanceof BouncyCastleProvider) { return ((BouncyCastleProvider) provider).getPrivateProvider(); } - throw new IllegalStateException(); // XXX + throw new IllegalStateException("Internal error in BCJcaJceHelper"); } // END Android-added: Look up algorithms in private provider if not found in main Provider. } diff --git a/repackaged/bcprov/src/main/java/com/android/org/bouncycastle/jcajce/util/BCJcaJceHelper.java b/repackaged/bcprov/src/main/java/com/android/org/bouncycastle/jcajce/util/BCJcaJceHelper.java index b444878f..69ab946c 100644 --- a/repackaged/bcprov/src/main/java/com/android/org/bouncycastle/jcajce/util/BCJcaJceHelper.java +++ b/repackaged/bcprov/src/main/java/com/android/org/bouncycastle/jcajce/util/BCJcaJceHelper.java @@ -50,13 +50,21 @@ public class BCJcaJceHelper // // If code is using a BCJcajceHelper to ensure it gets its implementation from BC, then // also search in the privately provided algorithms if not found in the main set. + // + // If any error occurs while searching the private Provider, typically a + // NoSuchAlgorithmException being thrown, then the original NoSuchAlgorithmException + // from the BC Provider is thrown for consistency. @Override public Cipher createCipher(String algorithm) throws NoSuchAlgorithmException, NoSuchPaddingException { try { return super.createCipher(algorithm); - } catch (NoSuchAlgorithmException e) { - return Cipher.getInstance(algorithm, getPrivateProvider()); + } catch (NoSuchAlgorithmException originalException) { + try { + return Cipher.getInstance(algorithm, getPrivateProvider()); + } catch (Throwable throwable) { + throw originalException; + } } } @@ -65,8 +73,12 @@ public class BCJcaJceHelper throws NoSuchAlgorithmException { try { return super.createSecretKeyFactory(algorithm); - } catch (NoSuchAlgorithmException e) { - return SecretKeyFactory.getInstance(algorithm, getPrivateProvider()); + } catch (NoSuchAlgorithmException originalException) { + try { + return SecretKeyFactory.getInstance(algorithm, getPrivateProvider()); + } catch (Throwable throwable) { + throw originalException; + } } } @@ -74,8 +86,12 @@ public class BCJcaJceHelper public Mac createMac(String algorithm) throws NoSuchAlgorithmException { try { return super.createMac(algorithm); - } catch (NoSuchAlgorithmException e) { - return Mac.getInstance(algorithm, getPrivateProvider()); + } catch (NoSuchAlgorithmException originalException) { + try { + return Mac.getInstance(algorithm, getPrivateProvider()); + } catch (Throwable throwable) { + throw originalException; + } } } @@ -83,7 +99,7 @@ public class BCJcaJceHelper if (provider instanceof BouncyCastleProvider) { return ((BouncyCastleProvider) provider).getPrivateProvider(); } - throw new IllegalStateException(); // XXX + throw new IllegalStateException("Internal error in BCJcaJceHelper"); } // END Android-added: Look up algorithms in private provider if not found in main Provider. } diff --git a/repackaged_platform/bcprov/src/main/java/com/android/internal/org/bouncycastle/jcajce/util/BCJcaJceHelper.java b/repackaged_platform/bcprov/src/main/java/com/android/internal/org/bouncycastle/jcajce/util/BCJcaJceHelper.java index 7b7c6cb3..507d225c 100644 --- a/repackaged_platform/bcprov/src/main/java/com/android/internal/org/bouncycastle/jcajce/util/BCJcaJceHelper.java +++ b/repackaged_platform/bcprov/src/main/java/com/android/internal/org/bouncycastle/jcajce/util/BCJcaJceHelper.java @@ -50,13 +50,21 @@ public class BCJcaJceHelper // // If code is using a BCJcajceHelper to ensure it gets its implementation from BC, then // also search in the privately provided algorithms if not found in the main set. + // + // If any error occurs while searching the private Provider, typically a + // NoSuchAlgorithmException being thrown, then the original NoSuchAlgorithmException + // from the BC Provider is thrown for consistency. @Override public Cipher createCipher(String algorithm) throws NoSuchAlgorithmException, NoSuchPaddingException { try { return super.createCipher(algorithm); - } catch (NoSuchAlgorithmException e) { - return Cipher.getInstance(algorithm, getPrivateProvider()); + } catch (NoSuchAlgorithmException originalException) { + try { + return Cipher.getInstance(algorithm, getPrivateProvider()); + } catch (Throwable throwable) { + throw originalException; + } } } @@ -65,8 +73,12 @@ public class BCJcaJceHelper throws NoSuchAlgorithmException { try { return super.createSecretKeyFactory(algorithm); - } catch (NoSuchAlgorithmException e) { - return SecretKeyFactory.getInstance(algorithm, getPrivateProvider()); + } catch (NoSuchAlgorithmException originalException) { + try { + return SecretKeyFactory.getInstance(algorithm, getPrivateProvider()); + } catch (Throwable throwable) { + throw originalException; + } } } @@ -74,8 +86,12 @@ public class BCJcaJceHelper public Mac createMac(String algorithm) throws NoSuchAlgorithmException { try { return super.createMac(algorithm); - } catch (NoSuchAlgorithmException e) { - return Mac.getInstance(algorithm, getPrivateProvider()); + } catch (NoSuchAlgorithmException originalException) { + try { + return Mac.getInstance(algorithm, getPrivateProvider()); + } catch (Throwable throwable) { + throw originalException; + } } } @@ -83,7 +99,7 @@ public class BCJcaJceHelper if (provider instanceof BouncyCastleProvider) { return ((BouncyCastleProvider) provider).getPrivateProvider(); } - throw new IllegalStateException(); // XXX + throw new IllegalStateException("Internal error in BCJcaJceHelper"); } // END Android-added: Look up algorithms in private provider if not found in main Provider. } |