BcX509CertificateFactory implements CertificateFactory { @Override public CertifiedPublicKey decode(byte[] encoded) throws IOException { return convert(new X509CertificateHolder(encoded)); }  @Override CertifiedPublicKey decode(byte[] encoded); CertifiedPublicKey convert(X509CertificateHolder cert);  }
@Test void v1CaCert() throws Exception { CertifiedPublicKey certificate = factory.decode(v1CaCert); assertTrue(certificate.isSignedBy(certificate.getPublicKeyParameters()), "CA should verify itself."); assertThat(certificate, instanceOf(X509CertifiedPublicKey.class)); X509CertifiedPublicKey cert = (X509CertifiedPublicKey) certificate; assertThat(cert.getVersionNumber(), equalTo(1)); assertThat(cert.isRootCA(), equalTo(true)); }
@Test void v1Cert() throws Exception { CertifiedPublicKey caCert = factory.decode(v1CaCert); CertifiedPublicKey certificate = factory.decode(v1Cert); assertTrue(certificate.isSignedBy(caCert.getPublicKeyParameters()), "End certificate should be verified by CA."); assertThat(certificate, instanceOf(X509CertifiedPublicKey.class)); X509CertifiedPublicKey cert = (X509CertifiedPublicKey) certificate; assertThat(cert.getVersionNumber(), equalTo(1)); }
@Test void v3CaCert() throws Exception { CertifiedPublicKey certificate = factory.decode(v3CaCert); assertTrue(certificate.isSignedBy(certificate.getPublicKeyParameters()), "CA should verify itself."); assertThat(certificate, instanceOf(X509CertifiedPublicKey.class)); X509CertifiedPublicKey cert = (X509CertifiedPublicKey) certificate; assertThat(cert.getVersionNumber(), equalTo(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())); assertThat(cert.isRootCA(), equalTo(true)); }
@Test void v3InterCACert() throws Exception { CertifiedPublicKey caCert = factory.decode(v3CaCert); CertifiedPublicKey interCaCert = factory.decode(v3InterCaCert); assertTrue(interCaCert.isSignedBy(caCert.getPublicKeyParameters()), "Intermediate CA certificate should be verified by CA."); assertThat(interCaCert, instanceOf(X509CertifiedPublicKey.class)); X509CertifiedPublicKey cert = (X509CertifiedPublicKey) interCaCert; assertThat(cert.getVersionNumber(), equalTo(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."); assertThat(cert.getExtensions().getBasicConstraintsPathLen(), equalTo(0)); 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(), equalTo(((X509CertifiedPublicKey) caCert).getSubjectKeyIdentifier())); assertThat(cert.isRootCA(), equalTo(false)); }
@Test void v3Cert() throws Exception { CertifiedPublicKey interCaCert = factory.decode(v3InterCaCert); CertifiedPublicKey certificate = factory.decode(v3Cert); assertTrue(certificate.isSignedBy(interCaCert.getPublicKeyParameters()), "End certificate should be verified by CA."); assertThat(certificate, instanceOf(X509CertifiedPublicKey.class)); X509CertifiedPublicKey cert = (X509CertifiedPublicKey) certificate; assertThat(cert.getVersionNumber(), equalTo(3)); assertTrue(cert.getExtensions().isCritical(KeyUsage.OID), "KeyUsage extension should be critical."); assertThat(cert.getExtensions().getKeyUsage(), equalTo(EnumSet.of(KeyUsage.digitalSignature, KeyUsage.dataEncipherment))); assertFalse(cert.getExtensions().isCritical(ExtendedKeyUsages.OID), "ExtendedKeyUsage extension should be non critical."); assertThat(cert.getExtensions().getExtendedKeyUsage().getAll().toArray(new String[0]), equalTo( new String[]{ ExtendedKeyUsages.EMAIL_PROTECTION })); assertTrue(cert.getExtensions().getExtendedKeyUsage().hasUsage(ExtendedKeyUsages.EMAIL_PROTECTION), "Email data protection extended usage should be set."); assertThat(cert.getAuthorityKeyIdentifier(), equalTo(((X509CertifiedPublicKey) interCaCert).getSubjectKeyIdentifier())); assertThat(cert.isRootCA(), equalTo(false)); }