@Test void generateSelfSignedCertificateVersion3RootCa() throws Exception { CertifiedPublicKey certificate = factory.getInstance(signerFactory.getInstance(true, rsaPrivateKey), new X509CertificateGenerationParameters( builder.addBasicConstraints(true) .addKeyUsage(true, EnumSet.of(KeyUsage.keyCertSign, KeyUsage.cRLSign)) .build())) .generate(new DistinguishedName("CN=Test"), rsaPublicKey, new X509CertificateParameters()); X509CertifiedPublicKey cert = checkSelfSigned(certificate, 3); assertTrue(cert.getExtensions().isCritical(X509Extensions.BASIC_CONSTRAINTS_OID), "Basic constraints should be critical."); assertTrue(cert.getExtensions().hasCertificateAuthorityBasicConstraints(), "Basic constraints should be set to CA."); assertTrue(cert.getExtensions().isCritical(KeyUsage.OID), "KeyUsage extension should be critical."); assertThat(cert.getExtensions().getKeyUsage(), equalTo(EnumSet.of(KeyUsage.keyCertSign, KeyUsage.cRLSign))); assertThat(cert.getAuthorityKeyIdentifier(), notNullValue()); assertThat(cert.getAuthorityKeyIdentifier(), equalTo(cert.getSubjectKeyIdentifier())); }
@Override public CertificateGenerator getInstance(Signer signer, CertificateGenerationParameters parameters) { if (!(parameters instanceof X509CertificateGenerationParameters)) { throw new IllegalArgumentException("Invalid parameters for X.509 certificate: " + parameters.getClass().getName()); } if (!signer.isForSigning()) { throw new IllegalArgumentException("Verifying signer used for signing certificates."); } X509CertificateGenerationParameters params = (X509CertificateGenerationParameters) parameters; switch (params.getX509Version()) { case V1: return new BcX509v1CertificateGenerator(signer, params, this.signerFactory, this.randomProvider.get()); case V3: return new BcX509v3CertificateGenerator(signer, params, this.signerFactory, this.randomProvider.get()); default: throw new IllegalArgumentException("Unknown X.509 certificate version: " + params.getX509Version()); } }
BcX509CertificateGeneratorFactory implements CertificateGeneratorFactory { @Override public CertificateGenerator getInstance(Signer signer, CertificateGenerationParameters parameters) { if (!(parameters instanceof X509CertificateGenerationParameters)) { throw new IllegalArgumentException("Invalid parameters for X.509 certificate: " + parameters.getClass().getName()); } if (!signer.isForSigning()) { throw new IllegalArgumentException("Verifying signer used for signing certificates."); } X509CertificateGenerationParameters params = (X509CertificateGenerationParameters) parameters; switch (params.getX509Version()) { case V1: return new BcX509v1CertificateGenerator(signer, params, this.signerFactory, this.randomProvider.get()); case V3: return new BcX509v3CertificateGenerator(signer, params, this.signerFactory, this.randomProvider.get()); default: throw new IllegalArgumentException("Unknown X.509 certificate version: " + params.getX509Version()); } } }
BcX509CertificateGeneratorFactory implements CertificateGeneratorFactory { @Override public CertificateGenerator getInstance(Signer signer, CertificateGenerationParameters parameters) { if (!(parameters instanceof X509CertificateGenerationParameters)) { throw new IllegalArgumentException("Invalid parameters for X.509 certificate: " + parameters.getClass().getName()); } if (!signer.isForSigning()) { throw new IllegalArgumentException("Verifying signer used for signing certificates."); } X509CertificateGenerationParameters params = (X509CertificateGenerationParameters) parameters; switch (params.getX509Version()) { case V1: return new BcX509v1CertificateGenerator(signer, params, this.signerFactory, this.randomProvider.get()); case V3: return new BcX509v3CertificateGenerator(signer, params, this.signerFactory, this.randomProvider.get()); default: throw new IllegalArgumentException("Unknown X.509 certificate version: " + params.getX509Version()); } }  }
BcX509CertificateGeneratorFactory implements CertificateGeneratorFactory { @Override public CertificateGenerator getInstance(Signer signer, CertificateGenerationParameters parameters) { if (!(parameters instanceof X509CertificateGenerationParameters)) { throw new IllegalArgumentException("Invalid parameters for X.509 certificate: " + parameters.getClass().getName()); } if (!signer.isForSigning()) { throw new IllegalArgumentException("Verifying signer used for signing certificates."); } X509CertificateGenerationParameters params = (X509CertificateGenerationParameters) parameters; switch (params.getX509Version()) { case V1: return new BcX509v1CertificateGenerator(signer, params, this.signerFactory, this.randomProvider.get()); case V3: return new BcX509v3CertificateGenerator(signer, params, this.signerFactory, this.randomProvider.get()); default: throw new IllegalArgumentException("Unknown X.509 certificate version: " + params.getX509Version()); } }  @Override CertificateGenerator getInstance(Signer signer, CertificateGenerationParameters parameters); }
BcX509CertificateGeneratorFactory implements CertificateGeneratorFactory { @Override public CertificateGenerator getInstance(Signer signer, CertificateGenerationParameters parameters) { if (!(parameters instanceof X509CertificateGenerationParameters)) { throw new IllegalArgumentException("Invalid parameters for X.509 certificate: " + parameters.getClass().getName()); } if (!signer.isForSigning()) { throw new IllegalArgumentException("Verifying signer used for signing certificates."); } X509CertificateGenerationParameters params = (X509CertificateGenerationParameters) parameters; switch (params.getX509Version()) { case V1: return new BcX509v1CertificateGenerator(signer, params, this.signerFactory, this.randomProvider.get()); case V3: return new BcX509v3CertificateGenerator(signer, params, this.signerFactory, this.randomProvider.get()); default: throw new IllegalArgumentException("Unknown X.509 certificate version: " + params.getX509Version()); } }  @Override CertificateGenerator getInstance(Signer signer, CertificateGenerationParameters parameters);  }