[java-metadata-aggregator] 01/02: Refactor test for reuse
Ian Young
ian at iay.org.uk
Fri Feb 2 20:18:08 UTC 2024
This is an automated email from the git hooks/post-receive script.
iay pushed a commit to branch main
in repository java-metadata-aggregator.
View the commit online:
http://git.shibboleth.net/view/?p=java-metadata-aggregator.git;a=commit;h=caad03bdadfb7b6b017d5c687782f0f9a4311b43
commit caad03bdadfb7b6b017d5c687782f0f9a4311b43
Author: Ian Young <ian at iay.org.uk>
AuthorDate: Fri Feb 2 20:17:06 2024 +0000
Refactor test for reuse
---
.../metadata/validate/x509/MDA183Test.java | 103 +--------------------
.../x509/testing/BaseX509ValidatorTest.java | 103 +++++++++++++++++++++
2 files changed, 108 insertions(+), 98 deletions(-)
diff --git a/mda-framework/src/test/java/net/shibboleth/metadata/validate/x509/MDA183Test.java b/mda-framework/src/test/java/net/shibboleth/metadata/validate/x509/MDA183Test.java
index b451156..8a65476 100644
--- a/mda-framework/src/test/java/net/shibboleth/metadata/validate/x509/MDA183Test.java
+++ b/mda-framework/src/test/java/net/shibboleth/metadata/validate/x509/MDA183Test.java
@@ -1,124 +1,31 @@
package net.shibboleth.metadata.validate.x509;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.math.BigInteger;
import java.security.Key;
import java.security.KeyStore;
-import java.security.MessageDigest;
-import java.security.NoSuchAlgorithmException;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;
import java.security.interfaces.RSAPrivateKey;
-import java.util.Arrays;
import java.util.List;
import javax.annotation.Nonnull;
-import org.apache.commons.codec.binary.Hex;
-import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.testng.Assert;
import org.testng.annotations.Test;
import net.shibboleth.metadata.ErrorStatus;
import net.shibboleth.metadata.Item;
-import net.shibboleth.metadata.pipeline.StageProcessingException;
-import net.shibboleth.metadata.testing.BaseTest;
import net.shibboleth.metadata.testing.MockItem;
import net.shibboleth.metadata.validate.Validator;
+import net.shibboleth.metadata.validate.x509.testing.BaseX509ValidatorTest;
-public class MDA183Test extends BaseTest {
+public class MDA183Test extends BaseX509ValidatorTest {
- /** Sequence of bytes put on the front of the string to be hashed. */
- private final byte[] openSSLprefix = {
- 'M', 'o', 'd', 'u', 'l', 'u', 's', '=',
- };
-
- /**
- * Computes the OpenSSL digest value for the given modulus.
- *
- * <p>
- * Not used in the test, but left here for use in generating new tests.
- * </p>
- *
- * @param modulus RSA public modulus to be digested
- * @return value to be compared against the blacklist
- * @throws StageProcessingException if SHA1 digester can not be acquired, or for internal
- * errors related to {@link ByteArrayOutputStream}
- */
- @SuppressWarnings("unused")
- private @Nonnull String openSSLDigest(@Nonnull final BigInteger modulus) throws StageProcessingException {
- try {
- // Acquire a representation of the modulus
- byte[] modulusBytes = modulus.toByteArray();
- if (modulusBytes[0] == 0) {
- // drop first 00 byte of modulus representation
- modulusBytes = Arrays.copyOfRange(modulusBytes, 1, modulusBytes.length);
- }
-
- // Encode the modulus into upper-case hex characters
- final char[] encodedModulus = Hex.encodeHex(modulusBytes, false);
-
- // Now construct the thing we want to hash
- final ByteArrayOutputStream bb = new ByteArrayOutputStream();
- try {
- bb.write(openSSLprefix);
- for (final char c : encodedModulus) {
- bb.write((byte) c);
- }
- bb.write('\n');
- } catch (final IOException e) {
- throw new StageProcessingException("internal error writing to ByteArrayStream", e);
- }
- //System.out.println("To be digested: " + bb.toString());
-
- // Make the digest
- final MessageDigest digest = MessageDigest.getInstance("SHA1");
- digest.update(bb.toByteArray());
- final byte[] bytes = digest.digest();
-
- // Convert the digest to a lower-case hex string
- final char [] encodedDigest = Hex.encodeHex(bytes, true);
- final String strValue = String.valueOf(encodedDigest);
- final String trimmed = strValue.substring(20);
- assert trimmed != null;
- //System.out.println("Digest: " + strValue + " trimmed " + trimmed);
- return trimmed;
- } catch (final NoSuchAlgorithmException e) {
- throw new StageProcessingException("could not create message digester", e);
- }
- }
-
- protected MDA183Test() {
+ protected MDA183Test() throws Exception{
super(MDA183Test.class);
}
- private Validator<X509Certificate> getValidator(final int keySize) throws Exception {
- // pick up the appropriate keylist resource
- final @Nonnull Resource keylistResource;
- switch (keySize) {
- case 1024:
- keylistResource = new ClassPathResource("net/shibboleth/metadata/keylists/rsa/legacy/compromised-1024.txt");
- break;
- case 2048:
- keylistResource = new ClassPathResource("net/shibboleth/metadata/keylists/rsa/compromised-2048.txt");
- break;
- default:
- throw new IllegalArgumentException();
- }
-
- // create a validator
- final X509RSAOpenSSLBlacklistValidator val = new X509RSAOpenSSLBlacklistValidator();
- val.setId("validator-" + keySize);
- val.setBlacklistResource(keylistResource);
- val.setKeySize(keySize);
- val.initialize();
-
- return val;
- }
-
private void verifyKey(final KeyStore ks, final @Nonnull String alias,
final String password,
final Validator<X509Certificate> val,
@@ -143,8 +50,8 @@ public class MDA183Test extends BaseTest {
@Test
public void testMDA183jetty() throws Exception {
- final Validator<X509Certificate> validator1024 = getValidator(1024);
- final Validator<X509Certificate> validator2048 = getValidator(2048);
+ final Validator<X509Certificate> validator1024 = getCompromisedKeyValidator(1024);
+ final Validator<X509Certificate> validator2048 = getCompromisedKeyValidator(2048);
// grab the keystore
final Resource keystoreResource = getClasspathResource("keystore.jks");
diff --git a/mda-framework/src/test/java/net/shibboleth/metadata/validate/x509/testing/BaseX509ValidatorTest.java b/mda-framework/src/test/java/net/shibboleth/metadata/validate/x509/testing/BaseX509ValidatorTest.java
index 1fd3dff..ee0f50c 100644
--- a/mda-framework/src/test/java/net/shibboleth/metadata/validate/x509/testing/BaseX509ValidatorTest.java
+++ b/mda-framework/src/test/java/net/shibboleth/metadata/validate/x509/testing/BaseX509ValidatorTest.java
@@ -15,21 +15,31 @@
package net.shibboleth.metadata.validate.x509.testing;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.math.BigInteger;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
+import java.util.Arrays;
import java.util.Collection;
import javax.annotation.Nonnull;
+import org.apache.commons.codec.binary.Hex;
+import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.testng.Assert;
import net.shibboleth.metadata.ErrorStatus;
import net.shibboleth.metadata.Item;
import net.shibboleth.metadata.WarningStatus;
+import net.shibboleth.metadata.pipeline.StageProcessingException;
import net.shibboleth.metadata.testing.BaseTest;
import net.shibboleth.metadata.testing.MockItem;
import net.shibboleth.metadata.validate.Validator;
+import net.shibboleth.metadata.validate.x509.X509RSAOpenSSLBlacklistValidator;
public abstract class BaseX509ValidatorTest extends BaseTest {
@@ -74,4 +84,97 @@ public abstract class BaseX509ValidatorTest extends BaseTest {
errorsAndWarnings(item, expectedErrors, expectedWarnings);
}
+ /**
+ * Build a validator for the set of compromised keys with the given {@code keySize}.
+ *
+ * @param keySize size of compromised keys to validate
+ * @return constructed {@link Validator}
+ * @throws Exception if something goes wrong
+ */
+ protected Validator<X509Certificate> getCompromisedKeyValidator(final int keySize) throws Exception {
+ // pick up the appropriate keylist resource
+ final @Nonnull Resource keylistResource;
+ switch (keySize) {
+ case 1024:
+ keylistResource = new ClassPathResource("net/shibboleth/metadata/keylists/rsa/legacy/compromised-1024.txt");
+ break;
+ case 2048:
+ keylistResource = new ClassPathResource("net/shibboleth/metadata/keylists/rsa/compromised-2048.txt");
+ break;
+ default:
+ throw new IllegalArgumentException();
+ }
+
+ // create a validator
+ final X509RSAOpenSSLBlacklistValidator val = new X509RSAOpenSSLBlacklistValidator();
+ val.setId("validator-" + keySize);
+ val.setBlacklistResource(keylistResource);
+ val.setKeySize(keySize);
+ val.initialize();
+
+ return val;
+ }
+
+ /**
+ * Sequence of bytes put on the front of the string to be hashed when
+ * generating an OpenSSL digest for a modulus.
+ */
+ private final byte[] openSSLprefix = {
+ 'M', 'o', 'd', 'u', 'l', 'u', 's', '=',
+ };
+
+ /**
+ * Computes the OpenSSL digest value for the given modulus.
+ *
+ * <p>
+ * Not necessarily used in tests, but left here for use in generating new tests.
+ * </p>
+ *
+ * @param modulus RSA public modulus to be digested
+ * @return value to be compared against the blacklist
+ * @throws StageProcessingException if SHA1 digester can not be acquired, or for internal
+ * errors related to {@link ByteArrayOutputStream}
+ */
+ protected @Nonnull String openSSLDigest(@Nonnull final BigInteger modulus) throws StageProcessingException {
+ try {
+ // Acquire a representation of the modulus
+ byte[] modulusBytes = modulus.toByteArray();
+ if (modulusBytes[0] == 0) {
+ // drop first 00 byte of modulus representation
+ modulusBytes = Arrays.copyOfRange(modulusBytes, 1, modulusBytes.length);
+ }
+
+ // Encode the modulus into upper-case hex characters
+ final char[] encodedModulus = Hex.encodeHex(modulusBytes, false);
+
+ // Now construct the thing we want to hash
+ final ByteArrayOutputStream bb = new ByteArrayOutputStream();
+ try {
+ bb.write(openSSLprefix);
+ for (final char c : encodedModulus) {
+ bb.write((byte) c);
+ }
+ bb.write('\n');
+ } catch (final IOException e) {
+ throw new StageProcessingException("internal error writing to ByteArrayStream", e);
+ }
+ //System.out.println("To be digested: " + bb.toString());
+
+ // Make the digest
+ final MessageDigest digest = MessageDigest.getInstance("SHA1");
+ digest.update(bb.toByteArray());
+ final byte[] bytes = digest.digest();
+
+ // Convert the digest to a lower-case hex string
+ final char [] encodedDigest = Hex.encodeHex(bytes, true);
+ final String strValue = String.valueOf(encodedDigest);
+ final String trimmed = strValue.substring(20);
+ assert trimmed != null;
+ //System.out.println("Digest: " + strValue + " trimmed " + trimmed);
+ return trimmed;
+ } catch (final NoSuchAlgorithmException e) {
+ throw new StageProcessingException("could not create message digester", e);
+ }
+ }
+
}
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.
More information about the commits
mailing list