use of java.security.cert.CertPathValidatorException in project jdk8u_jdk by JetBrains.
the class Serial method main.
public static void main(String[] args) throws Exception {
File f = new File(System.getProperty("test.src", "."), "cert_file");
FileInputStream fis = new FileInputStream(f);
CertificateFactory cf = CertificateFactory.getInstance("X.509");
Certificate c = cf.generateCertificate(fis);
fis.close();
CertPath cp = cf.generateCertPath(Collections.singletonList(c));
CertPathValidatorException cpve1 = new CertPathValidatorException("Test", new Exception("Expired"), cp, 0, BasicReason.EXPIRED);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// FileOutputStream fos = new FileOutputStream("jdk7.serial");
ObjectOutputStream oos = new ObjectOutputStream(baos);
// ObjectOutputStream foos = new ObjectOutputStream(fos);
oos.writeObject(cpve1);
// foos.writeObject(cpve1);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);
CertPathValidatorException cpve2 = (CertPathValidatorException) ois.readObject();
check(!cpve1.getMessage().equals(cpve2.getMessage()), "CertPathValidatorException messages not equal");
check(!cpve1.getCause().getMessage().equals(cpve2.getCause().getMessage()), "CertPathValidatorException causes not equal");
check(!cpve1.getCertPath().equals(cpve2.getCertPath()), "CertPathValidatorException certpaths not equal");
check(cpve1.getIndex() != cpve2.getIndex(), "CertPathValidatorException indexes not equal");
check(cpve1.getReason() != cpve2.getReason(), "CertPathValidatorException reasons not equal");
oos.close();
ois.close();
f = new File(System.getProperty("test.src", "."), "jdk6.serial");
fis = new FileInputStream(f);
ois = new ObjectInputStream(fis);
cpve2 = (CertPathValidatorException) ois.readObject();
check(!cpve1.getMessage().equals(cpve2.getMessage()), "CertPathValidatorException messages not equal");
check(!cpve1.getCause().getMessage().equals(cpve2.getCause().getMessage()), "CertPathValidatorException causes not equal");
check(!cpve1.getCertPath().equals(cpve2.getCertPath()), "CertPathValidatorException certpaths not equal");
check(cpve1.getIndex() != cpve2.getIndex(), "CertPathValidatorException indexes not equal");
// System.out.println(cpve2.getReason());
check(cpve2.getReason() != BasicReason.UNSPECIFIED, "CertPathValidatorException reasons not equal");
oos.close();
ois.close();
if (failed) {
throw new Exception("Some tests FAILED");
}
}
use of java.security.cert.CertPathValidatorException in project verify-hub by alphagov.
the class ConfigServiceKeyStoreTest method getVerificationKeyForEntity_shouldThrowExceptionIfCertificateIsInvalid.
@Test
public void getVerificationKeyForEntity_shouldThrowExceptionIfCertificateIsInvalid() throws Exception {
final CertificateDto certOneDto = getX509Certificate(STUB_IDP_ONE);
when(certificatesConfigProxy.getSignatureVerificationCertificates(issuerId)).thenReturn(of(certOneDto));
when(x509CertificateFactory.createCertificate(certOneDto.getCertificate())).thenReturn(x509Certificate);
when(trustStoreForCertificateProvider.getTrustStoreFor(any(FederationEntityType.class))).thenReturn(trustStore);
CertPathValidatorException underlyingException = new CertPathValidatorException("Invalid Certificate");
when(certificateChainValidator.validate(x509Certificate, trustStore)).thenReturn(invalid(underlyingException));
try {
configServiceKeyStore.getVerifyingKeysForEntity(issuerId);
Assert.fail(String.format("Expected [%s]", CertificateChainValidationException.class.getSimpleName()));
} catch (CertificateChainValidationException success) {
assertThat(success.getMessage()).isEqualTo("Certificate is not valid: Unable to get DN");
assertThat(success.getCause()).isEqualTo(underlyingException);
}
}
use of java.security.cert.CertPathValidatorException in project verify-hub by alphagov.
the class ConfigServiceKeyStoreTest method getEncryptionKeyForEntity_shouldThrowExceptionIfCertificateIsInvalid.
@Test
public void getEncryptionKeyForEntity_shouldThrowExceptionIfCertificateIsInvalid() throws Exception {
final CertificateDto certOneDto = getX509Certificate(IDP_ENTITY_ID);
when(certificatesConfigProxy.getEncryptionCertificate(issuerId)).thenReturn(certOneDto);
when(x509CertificateFactory.createCertificate(certOneDto.getCertificate())).thenReturn(x509Certificate);
when(trustStoreForCertificateProvider.getTrustStoreFor(any(FederationEntityType.class))).thenReturn(trustStore);
CertPathValidatorException underlyingException = new CertPathValidatorException("Invalid Certificate");
when(certificateChainValidator.validate(x509Certificate, trustStore)).thenReturn(invalid(underlyingException));
try {
configServiceKeyStore.getEncryptionKeyForEntity(issuerId);
Assert.fail(String.format("Expected [%s]", CertificateChainValidationException.class.getSimpleName()));
} catch (CertificateChainValidationException success) {
assertThat(success.getMessage()).isEqualTo("Certificate is not valid: Unable to get DN");
assertThat(success.getCause()).isEqualTo(underlyingException);
}
}
use of java.security.cert.CertPathValidatorException in project dropbox-sdk-java by dropbox.
the class NetworkIOException method computeMessage.
private static String computeMessage(IOException ex) {
String message = ex.getMessage();
// useful for debugging.
if (ex instanceof SSLHandshakeException) {
Throwable innerCause = ex.getCause();
if (innerCause instanceof CertPathValidatorException) {
CertPathValidatorException cpve = (CertPathValidatorException) innerCause;
message += "[CERT PATH: " + cpve.getCertPath() + "]";
}
}
return message;
}
use of java.security.cert.CertPathValidatorException in project cxf by apache.
the class TrustedAuthorityValidator method isCertificateChainValid.
/**
* Checks if a certificate is signed by a trusted authority.
*
* @param x509Certificate to check
* @return the validity state of the certificate
*/
boolean isCertificateChainValid(List<X509Certificate> certificates) {
X509Certificate targetCert = certificates.get(0);
X509CertSelector selector = new X509CertSelector();
selector.setCertificate(targetCert);
try {
List<X509Certificate> intermediateCerts = certRepo.getCaCerts();
List<X509Certificate> trustedAuthorityCerts = certRepo.getTrustedCaCerts();
Set<TrustAnchor> trustAnchors = asTrustAnchors(trustedAuthorityCerts);
CertStoreParameters intermediateParams = new CollectionCertStoreParameters(intermediateCerts);
CertStoreParameters certificateParams = new CollectionCertStoreParameters(certificates);
PKIXBuilderParameters pkixParams = new PKIXBuilderParameters(trustAnchors, selector);
pkixParams.addCertStore(CertStore.getInstance("Collection", intermediateParams));
pkixParams.addCertStore(CertStore.getInstance("Collection", certificateParams));
pkixParams.setRevocationEnabled(false);
CertPathBuilder builder = CertPathBuilder.getInstance("PKIX");
CertPath certPath = builder.build(pkixParams).getCertPath();
// Now validate the CertPath (including CRL checking)
if (enableRevocation) {
List<X509CRL> crls = certRepo.getCRLs();
if (!crls.isEmpty()) {
pkixParams.setRevocationEnabled(true);
CertStoreParameters crlParams = new CollectionCertStoreParameters(crls);
pkixParams.addCertStore(CertStore.getInstance("Collection", crlParams));
}
}
CertPathValidator validator = CertPathValidator.getInstance("PKIX");
validator.validate(certPath, pkixParams);
} catch (InvalidAlgorithmParameterException e) {
LOG.log(Level.WARNING, "Invalid algorithm parameter by certificate chain validation. " + "It is likely that issuer certificates are not found in XKMS trusted storage. " + e.getMessage(), e);
return false;
} catch (NoSuchAlgorithmException e) {
LOG.log(Level.WARNING, "Unknown algorithm by trust chain validation: " + e.getMessage(), e);
return false;
} catch (CertPathBuilderException e) {
LOG.log(Level.WARNING, "Cannot build certification path: " + e.getMessage(), e);
return false;
} catch (CertPathValidatorException e) {
LOG.log(Level.WARNING, "Cannot vaidate certification path: " + e.getMessage(), e);
return false;
}
return true;
}
Aggregations