Repository: netty/netty
File: handler/src/main/java/io/netty/handler/ssl/util/SelfSignedCertificate.java (Java)

Code before:
            } else if ("RSA".equalsIgnoreCase(algorithm)) {
                this.algorithm = "RSA";
            } else {
                throw new IllegalArgumentException("Algorithm not valid: " + algorithm);
            }
            return this;
        }

        private SecureRandom randomOrDefault() {
            // Bypass entropy collection by using insecure random generator.
            // We just want to generate it without any delay because it's for testing purposes only.
            return random == null ? ThreadLocalInsecureRandom.current() : random;
        }

        private void generateKeyPairLocally() {
            if (keypair != null) {
                return;
            }

            try {
                KeyPairGenerator keyGen = KeyPairGenerator.getInstance(algorithm);
                keyGen.initialize(bits, randomOrDefault());
                keypair = keyGen.generateKeyPair();
            } catch (NoSuchAlgorithmException e) {
                // Should not reach here because every Java implementation must have RSA and EC key pair generator.
                throw new Error(e);
            }
            privateKey = keypair.getPrivate();
        }

        private void addFailure(Throwable t) {
            if (failure != null) {
                t.addSuppressed(failure);
            }
            failure = t;
        }

        boolean generateBc() {
            if (!isBouncyCastleAvailable()) {
                // no need to even try. We can avoid generating the key pair with this check.
                logger.debug("Failed to generate a self-signed X.509 certificate because " +
                        "BouncyCastle PKIX is not available in classpath");
                return false;
            }
            generateKeyPairLocally();
            try {
                // Try Bouncy Castle first as otherwise we will see an IllegalAccessError on more recent JDKs.
                paths = BouncyCastleSelfSignedCertGenerator.generate(
                        fqdn, keypair, randomOrDefault(), notBefore, notAfter, algorithm);
                return true;
            } catch (Throwable t) {

Review comment (suggestion):
```suggestion
                throw new IllegalStateException(e);
```

Code after:
                throw new IllegalArgumentException("Algorithm not valid: " + algorithm);
            }
            return this;
        }

        private SecureRandom randomOrDefault() {
            // Bypass entropy collection by using insecure random generator.
            // We just want to generate it without any delay because it's for testing purposes only.
            return random == null ? ThreadLocalInsecureRandom.current() : random;
        }

        private void generateKeyPairLocally() {
            if (keypair != null) {
                return;
            }

            try {
                KeyPairGenerator keyGen = KeyPairGenerator.getInstance(algorithm);
                keyGen.initialize(bits, randomOrDefault());
                keypair = keyGen.generateKeyPair();
            } catch (NoSuchAlgorithmException e) {
                // Should not reach here because every Java implementation must have RSA and EC key pair generator.
                throw new IllegalStateException(e);
            }
            privateKey = keypair.getPrivate();
        }

        private void addFailure(Throwable t) {
            if (failure != null) {
                t.addSuppressed(failure);
            }
            failure = t;
        }

        boolean generateBc() {
            if (!isBouncyCastleAvailable()) {
                // no need to even try. We can avoid generating the key pair with this check.
                logger.debug("Failed to generate a self-signed X.509 certificate because " +
                        "BouncyCastle PKIX is not available in classpath");
                return false;
            }
            generateKeyPairLocally();
            try {
                // Try Bouncy Castle first as otherwise we will see an IllegalAccessError on more recent JDKs.
                paths = BouncyCastleSelfSignedCertGenerator.generate(
                        fqdn, keypair, randomOrDefault(), notBefore, notAfter, algorithm);
                return true;
            } catch (Throwable t) {
                logger.debug("Failed to generate a self-signed X.509 certificate using Bouncy Castle:", t);
                addFailure(t);
                return false;