Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CRYPTO-169] Add protection for the ExceptionInInitializerError scenario to the CryptoRandomFactory#getCryptoRandom(Properties) method #258

Merged
merged 6 commits into from
Oct 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.getCause();
if (t instanceof Exception) {
lastException = (Exception) t;
errorMessage.append("CryptoRandom: [" + className + "] initialization failed with " + t.getMessage());
} else {
throw initializerError;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,14 @@ public void testNull() {
assertThrows(NullPointerException.class, () -> CryptoRandomFactory.getCryptoRandom(null));
}

@Test
public void testExceptionInInitializerErrorRandom() throws GeneralSecurityException, IOException {
Copy link
Contributor Author

@LuciferYang LuciferYang Oct 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there are no chagnes to CryptoRandomFactory.java, the new test case will fail:

java.lang.ExceptionInInitializerError
	at java.base/java.lang.Class.forName0(Native Method)
	at java.base/java.lang.Class.forName(Class.java:534)
	at java.base/java.lang.Class.forName(Class.java:513)
	at org.apache.commons.crypto.utils.ReflectionUtils.getClassByNameOrNull(ReflectionUtils.java:93)
	at org.apache.commons.crypto.utils.ReflectionUtils.getClassByName(ReflectionUtils.java:64)
	at org.apache.commons.crypto.random.CryptoRandomFactory.getCryptoRandom(CryptoRandomFactory.java:189)
	at org.apache.commons.crypto.random.CryptoRandomFactoryTest.testExceptionInInitializerErrorRandom(CryptoRandomFactoryTest.java:150)
	at java.base/java.lang.reflect.Method.invoke(Method.java:580)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
Caused by: java.lang.IllegalStateException: java.security.GeneralSecurityException: ExceptionInInitializerErrorRandom init failed
	at org.apache.commons.crypto.random.ExceptionInInitializerErrorRandom.<clinit>(ExceptionInInitializerErrorRandom.java:28)
	... 10 more
Caused by: java.security.GeneralSecurityException: ExceptionInInitializerErrorRandom init failed
	at org.apache.commons.crypto.random.ExceptionInInitializerErrorRandom.check(ExceptionInInitializerErrorRandom.java:33)
	at org.apache.commons.crypto.random.ExceptionInInitializerErrorRandom.<clinit>(ExceptionInInitializerErrorRandom.java:26)
	... 10 more

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());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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;

/**
* 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 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class is used to simulate scenarios where OpenSslCryptoRandom fails in the static code block checkNative() or !OpenSslCryptoRandomNative.nextRandBytes(new byte[1]) is false.


static {
try {
check();
} catch (final GeneralSecurityException e) {
throw new IllegalStateException(e);
}
}

private static void check() throws GeneralSecurityException {
throw new GeneralSecurityException("ExceptionInInitializerErrorRandom init failed");
}

@Override
public void nextBytes(byte[] bytes) {
// empty
}

@Override
public void close() throws IOException {
// empty
}
}