From e0fd653a1ef56db64527a5caf74f22e420c1e674 Mon Sep 17 00:00:00 2001 From: yangjie01 Date: Sun, 22 Oct 2023 17:51:50 +0800 Subject: [PATCH 1/6] init fix --- .../apache/commons/crypto/random/CryptoRandomFactory.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/main/java/org/apache/commons/crypto/random/CryptoRandomFactory.java b/src/main/java/org/apache/commons/crypto/random/CryptoRandomFactory.java index 8acdf9c8d..fd6384dd1 100644 --- a/src/main/java/org/apache/commons/crypto/random/CryptoRandomFactory.java +++ b/src/main/java/org/apache/commons/crypto/random/CryptoRandomFactory.java @@ -198,6 +198,14 @@ public static CryptoRandom getCryptoRandom(final Properties props) } catch (final Exception e) { lastException = e; errorMessage.append("CryptoRandom: [" + className + "] failed with " + e.getMessage()); + } catch (final ExceptionInInitializerError initializerError) { + Throwable t = initializerError.getException(); + if (t instanceof Exception) { + lastException = (Exception) t; + errorMessage.append("CryptoRandom: [" + className + "] initialization failed with " + t.getMessage()); + } else { + throw initializerError; + } } } From 5a38163a844908a55b9282cc14f9a2d4afff2c64 Mon Sep 17 00:00:00 2001 From: yangjie01 Date: Sun, 22 Oct 2023 18:33:33 +0800 Subject: [PATCH 2/6] add new test --- .../random/CryptoRandomFactoryTest.java | 10 +++++ .../ExceptionInInitializerErrorRandom.java | 43 +++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 src/test/java/org/apache/commons/crypto/random/ExceptionInInitializerErrorRandom.java diff --git a/src/test/java/org/apache/commons/crypto/random/CryptoRandomFactoryTest.java b/src/test/java/org/apache/commons/crypto/random/CryptoRandomFactoryTest.java index eaaefc922..4d18cade8 100644 --- a/src/test/java/org/apache/commons/crypto/random/CryptoRandomFactoryTest.java +++ b/src/test/java/org/apache/commons/crypto/random/CryptoRandomFactoryTest.java @@ -141,4 +141,14 @@ public void testNull() { assertThrows(NullPointerException.class, () -> CryptoRandomFactory.getCryptoRandom(null)); } + @Test + public void testExceptionInInitializerErrorRandom() throws GeneralSecurityException, IOException { + final Properties properties = new Properties(); + String classes = ExceptionInInitializerErrorRandom.class.getName().concat(",") + .concat(CryptoRandomFactory.RandomProvider.JAVA.getClassName()); + properties.setProperty(CryptoRandomFactory.CLASSES_KEY, classes); + try (final CryptoRandom random = CryptoRandomFactory.getCryptoRandom(properties)) { + assertEquals(JavaCryptoRandom.class.getName(), random.getClass().getName()); + } + } } diff --git a/src/test/java/org/apache/commons/crypto/random/ExceptionInInitializerErrorRandom.java b/src/test/java/org/apache/commons/crypto/random/ExceptionInInitializerErrorRandom.java new file mode 100644 index 000000000..bfda86430 --- /dev/null +++ b/src/test/java/org/apache/commons/crypto/random/ExceptionInInitializerErrorRandom.java @@ -0,0 +1,43 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.crypto.random; + +import java.io.IOException; +import java.security.GeneralSecurityException; + +public class ExceptionInInitializerErrorRandom implements CryptoRandom { + + static { + try { + check(); + } catch (final GeneralSecurityException e) { + throw new IllegalStateException(e); + } + } + + private static void check() throws GeneralSecurityException { + throw new GeneralSecurityException("Native library is not loaded"); + } + + @Override + public void nextBytes(byte[] bytes) { + } + + @Override + public void close() throws IOException { + } +} From 465f5a79687ac57c2cc8eabc6e6be12d2831e36b Mon Sep 17 00:00:00 2001 From: yangjie01 Date: Sun, 22 Oct 2023 18:37:33 +0800 Subject: [PATCH 3/6] change message --- .../crypto/random/ExceptionInInitializerErrorRandom.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/org/apache/commons/crypto/random/ExceptionInInitializerErrorRandom.java b/src/test/java/org/apache/commons/crypto/random/ExceptionInInitializerErrorRandom.java index bfda86430..3179edcf7 100644 --- a/src/test/java/org/apache/commons/crypto/random/ExceptionInInitializerErrorRandom.java +++ b/src/test/java/org/apache/commons/crypto/random/ExceptionInInitializerErrorRandom.java @@ -30,7 +30,7 @@ public class ExceptionInInitializerErrorRandom implements CryptoRandom { } private static void check() throws GeneralSecurityException { - throw new GeneralSecurityException("Native library is not loaded"); + throw new GeneralSecurityException("ExceptionInInitializerErrorRandom init failed"); } @Override From d978f39ea07eae393692a572d70b90bbd84bbafe Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Sun, 22 Oct 2023 07:54:02 -0400 Subject: [PATCH 4/6] Use more modern API (Java 1.4) --- .../org/apache/commons/crypto/random/CryptoRandomFactory.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/apache/commons/crypto/random/CryptoRandomFactory.java b/src/main/java/org/apache/commons/crypto/random/CryptoRandomFactory.java index fd6384dd1..8239ae0a7 100644 --- a/src/main/java/org/apache/commons/crypto/random/CryptoRandomFactory.java +++ b/src/main/java/org/apache/commons/crypto/random/CryptoRandomFactory.java @@ -199,7 +199,7 @@ public static CryptoRandom getCryptoRandom(final Properties props) lastException = e; errorMessage.append("CryptoRandom: [" + className + "] failed with " + e.getMessage()); } catch (final ExceptionInInitializerError initializerError) { - Throwable t = initializerError.getException(); + Throwable t = initializerError.getCause(); if (t instanceof Exception) { lastException = (Exception) t; errorMessage.append("CryptoRandom: [" + className + "] initialization failed with " + t.getMessage()); From 6ba0ea1c1a1e2ae653c7a2058d3e42f33dc63189 Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Sun, 22 Oct 2023 07:57:53 -0400 Subject: [PATCH 5/6] Javadoc --- .../crypto/random/ExceptionInInitializerErrorRandom.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/test/java/org/apache/commons/crypto/random/ExceptionInInitializerErrorRandom.java b/src/test/java/org/apache/commons/crypto/random/ExceptionInInitializerErrorRandom.java index 3179edcf7..94bd03bac 100644 --- a/src/test/java/org/apache/commons/crypto/random/ExceptionInInitializerErrorRandom.java +++ b/src/test/java/org/apache/commons/crypto/random/ExceptionInInitializerErrorRandom.java @@ -19,6 +19,10 @@ import java.io.IOException; import java.security.GeneralSecurityException; +/** + * Simulates scenarios where {@link OpenSslCryptoRandom} fails in the static code block {@code checkNative()} or + * {@code !OpenSslCryptoRandomNative.nextRandBytes(new byte[1])} is false. + */ public class ExceptionInInitializerErrorRandom implements CryptoRandom { static { From 1c206a8a39980488b561b2fe46fa1fb1b6db6f7b Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Sun, 22 Oct 2023 07:59:24 -0400 Subject: [PATCH 6/6] Document empty blocks --- .../crypto/random/ExceptionInInitializerErrorRandom.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/test/java/org/apache/commons/crypto/random/ExceptionInInitializerErrorRandom.java b/src/test/java/org/apache/commons/crypto/random/ExceptionInInitializerErrorRandom.java index 94bd03bac..f44c98b8a 100644 --- a/src/test/java/org/apache/commons/crypto/random/ExceptionInInitializerErrorRandom.java +++ b/src/test/java/org/apache/commons/crypto/random/ExceptionInInitializerErrorRandom.java @@ -39,9 +39,11 @@ private static void check() throws GeneralSecurityException { @Override public void nextBytes(byte[] bytes) { + // empty } @Override public void close() throws IOException { + // empty } }