Search in sources :

Example 6 with CertificateVerificationException

use of io.ballerina.stdlib.http.transport.contractimpl.common.certificatevalidation.CertificateVerificationException in project module-ballerina-http by ballerina-platform.

the class RevocationVerificationTest method testCRLPathValidationWithFakeCerts.

/**
 * Tests CRL path validation with fake certificates. The path validation should fail since they are fake and do not
 * contain proper information.
 *
 * @throws Exception
 */
@Test
public void testCRLPathValidationWithFakeCerts() throws Exception {
    // Add BouncyCastle as Security Provider.
    Security.addProvider(new BouncyCastleProvider());
    Utils utils = new Utils();
    X509Certificate[] fakeCertificates = utils.getFakeCertificateChain();
    Throwable throwable = null;
    try {
        crlPathValidation(fakeCertificates);
    } catch (CertificateVerificationException e) {
        // Path Verification Should fail. So this catch block should be called.
        throwable = e;
    }
    assertNotNull(throwable);
}
Also used : CertificateVerificationException(io.ballerina.stdlib.http.transport.contractimpl.common.certificatevalidation.CertificateVerificationException) X509Certificate(java.security.cert.X509Certificate) BouncyCastleProvider(org.bouncycastle.jce.provider.BouncyCastleProvider) Test(org.testng.annotations.Test)

Example 7 with CertificateVerificationException

use of io.ballerina.stdlib.http.transport.contractimpl.common.certificatevalidation.CertificateVerificationException in project module-ballerina-http by ballerina-platform.

the class RevocationVerificationTest method testCRLPathValidation.

/**
 * Tests CRL Path Validation with the use of a real certificate chain. The verification process will make
 * HTTP calls to remote CRL server URLs extracted from the certificates in the chain. Usually these certificates
 * will not be revoked. So the path validation must be successful to pass the test. In case they are revoked
 * or expired, new certificates should be added to the resources directory and Constants should be modified
 * accordingly. See the interface Constants for expiry dates of the certificates.
 *
 * @throws Exception
 */
@Test
public void testCRLPathValidation() throws Exception {
    // Add BouncyCastle as Security Provider.
    Security.addProvider(new BouncyCastleProvider());
    Utils utils = new Utils();
    X509Certificate[] certificates = utils.getRealCertificateChain();
    Throwable throwable = null;
    try {
        crlPathValidation(certificates);
    } catch (CertificateVerificationException e) {
        // Path Verification Should Pass. This catch block should not be called.
        throwable = e;
    }
    assertNull(throwable);
}
Also used : CertificateVerificationException(io.ballerina.stdlib.http.transport.contractimpl.common.certificatevalidation.CertificateVerificationException) X509Certificate(java.security.cert.X509Certificate) BouncyCastleProvider(org.bouncycastle.jce.provider.BouncyCastleProvider) Test(org.testng.annotations.Test)

Example 8 with CertificateVerificationException

use of io.ballerina.stdlib.http.transport.contractimpl.common.certificatevalidation.CertificateVerificationException in project module-ballerina-http by ballerina-platform.

the class RevocationVerificationTest method testOCSPPathValidationWithFakeCerts.

/**
 * Tests OCSP path validation with a chain of fake certificates. In order to pass the test, the path validation
 * should fail since the certificates are fake and do not contain right information.
 *
 * @throws Exception
 */
@Test
public void testOCSPPathValidationWithFakeCerts() throws Exception {
    Security.addProvider(new BouncyCastleProvider());
    Utils utils = new Utils();
    X509Certificate[] fackeCertificates = utils.getFakeCertificateChain();
    Throwable throwable = null;
    try {
        ocspPathValidation(fackeCertificates);
    } catch (CertificateVerificationException e) {
        // Path Verification Should fail. So this catch block should be called.
        throwable = e;
    }
    assertNotNull(throwable);
}
Also used : CertificateVerificationException(io.ballerina.stdlib.http.transport.contractimpl.common.certificatevalidation.CertificateVerificationException) X509Certificate(java.security.cert.X509Certificate) BouncyCastleProvider(org.bouncycastle.jce.provider.BouncyCastleProvider) Test(org.testng.annotations.Test)

Example 9 with CertificateVerificationException

use of io.ballerina.stdlib.http.transport.contractimpl.common.certificatevalidation.CertificateVerificationException in project module-ballerina-http by ballerina-platform.

the class OCSPResponseBuilder method getOCSPResponse.

/**
 * Get OCSP response from cache.
 *
 * @param locations       CA locations
 * @param request         OCSP request
 * @param userCertificate User's certificate
 * @param ocspCache       cache to store ocsp responses
 * @return Ocsp response
 * @throws CertificateVerificationException If an error occurs while getting the response.
 */
public static OCSPResp getOCSPResponse(List<String> locations, OCSPReq request, X509Certificate userCertificate, OCSPCache ocspCache) throws CertificateVerificationException {
    SingleResp[] responses;
    BasicOCSPResp basicResponse;
    CertificateStatus certificateStatus;
    for (String serviceUrl : locations) {
        OCSPResp response;
        try {
            response = OCSPVerifier.getOCSPResponce(serviceUrl, request);
            if (OCSPResponseStatus.SUCCESSFUL != response.getStatus()) {
                // Server didn't give the correct response.
                continue;
            }
            basicResponse = (BasicOCSPResp) response.getResponseObject();
            responses = (basicResponse == null) ? null : basicResponse.getResponses();
        } catch (OCSPException | CertificateVerificationException e) {
            LOG.debug("OCSP response failed for url{}. Hence trying the next url", serviceUrl);
            continue;
        }
        if (responses != null && responses.length == 1) {
            SingleResp singleResponse = responses[0];
            certificateStatus = singleResponse.getCertStatus();
            if (certificateStatus != null) {
                throw new IllegalStateException("certificate-status=" + certificateStatus);
            }
            // User certificates serial number and response coming from CA needs to be same.
            if (!userCertificate.getSerialNumber().equals(singleResponse.getCertID().getSerialNumber())) {
                throw new IllegalStateException("Bad Serials=" + userCertificate.getSerialNumber() + " vs. " + singleResponse.getCertID().getSerialNumber());
            }
            // If the response state is successful we cache the response.
            ocspCache.setCacheValue(response, userCertificate.getSerialNumber(), singleResponse, request, serviceUrl);
            return response;
        }
    }
    throw new CertificateVerificationException("Could not get revocation status from OCSP.");
}
Also used : CertificateVerificationException(io.ballerina.stdlib.http.transport.contractimpl.common.certificatevalidation.CertificateVerificationException) OCSPException(org.bouncycastle.cert.ocsp.OCSPException) BasicOCSPResp(org.bouncycastle.cert.ocsp.BasicOCSPResp) CertificateStatus(org.bouncycastle.cert.ocsp.CertificateStatus) SingleResp(org.bouncycastle.cert.ocsp.SingleResp) OCSPResp(org.bouncycastle.cert.ocsp.OCSPResp) BasicOCSPResp(org.bouncycastle.cert.ocsp.BasicOCSPResp)

Example 10 with CertificateVerificationException

use of io.ballerina.stdlib.http.transport.contractimpl.common.certificatevalidation.CertificateVerificationException in project module-ballerina-http by ballerina-platform.

the class OCSPVerifier method getOCSPResponce.

/**
 * Gets an ASN.1 encoded OCSP response (as defined in RFC 2560) from the given service URL. Currently supports
 * only HTTP.
 *
 * @param serviceUrl URL of the OCSP endpoint.
 * @param request An OCSP request object.
 * @return OCSP response encoded in ASN.1 structure.
 * @throws CertificateVerificationException if any error occurs while trying to get a response from the CA.
 */
public static OCSPResp getOCSPResponce(String serviceUrl, OCSPReq request) throws CertificateVerificationException {
    try {
        byte[] array = request.getEncoded();
        if (serviceUrl.startsWith("http")) {
            HttpURLConnection connection;
            URL url = new URL(serviceUrl);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestProperty("Content-Type", "application/ocsp-request");
            connection.setRequestProperty("Accept", "application/ocsp-response");
            connection.setDoOutput(true);
            try (OutputStream out = connection.getOutputStream();
                DataOutputStream dataOut = new DataOutputStream(new BufferedOutputStream(out))) {
                dataOut.write(array);
                dataOut.flush();
                // Check errors in response:
                if (connection.getResponseCode() / 100 != 2) {
                    throw new CertificateVerificationException("Error getting ocsp response." + "Response code is " + connection.getResponseCode());
                }
                // Get Response.
                InputStream in = (InputStream) connection.getContent();
                return new OCSPResp(in);
            }
        } else {
            throw new CertificateVerificationException("Only http is supported for OCSP calls");
        }
    } catch (IOException e) {
        throw new CertificateVerificationException("Cannot get OCSP Response from url: " + serviceUrl, e);
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) CertificateVerificationException(io.ballerina.stdlib.http.transport.contractimpl.common.certificatevalidation.CertificateVerificationException) DataOutputStream(java.io.DataOutputStream) ASN1InputStream(org.bouncycastle.asn1.ASN1InputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) BufferedOutputStream(java.io.BufferedOutputStream) DataOutputStream(java.io.DataOutputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream) URL(java.net.URL) OCSPResp(org.bouncycastle.cert.ocsp.OCSPResp) BasicOCSPResp(org.bouncycastle.cert.ocsp.BasicOCSPResp)

Aggregations

CertificateVerificationException (io.ballerina.stdlib.http.transport.contractimpl.common.certificatevalidation.CertificateVerificationException)12 IOException (java.io.IOException)5 BouncyCastleProvider (org.bouncycastle.jce.provider.BouncyCastleProvider)5 DEROctetString (org.bouncycastle.asn1.DEROctetString)4 Test (org.testng.annotations.Test)4 X509Certificate (java.security.cert.X509Certificate)3 ASN1InputStream (org.bouncycastle.asn1.ASN1InputStream)3 DERIA5String (org.bouncycastle.asn1.DERIA5String)3 BasicOCSPResp (org.bouncycastle.cert.ocsp.BasicOCSPResp)3 OCSPException (org.bouncycastle.cert.ocsp.OCSPException)3 OCSPResp (org.bouncycastle.cert.ocsp.OCSPResp)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 CertificateEncodingException (java.security.cert.CertificateEncodingException)2 ArrayList (java.util.ArrayList)2 GeneralName (org.bouncycastle.asn1.x509.GeneralName)2 OCSPReq (org.bouncycastle.cert.ocsp.OCSPReq)2 SingleResp (org.bouncycastle.cert.ocsp.SingleResp)2 OperatorCreationException (org.bouncycastle.operator.OperatorCreationException)2 RevocationStatus (io.ballerina.stdlib.http.transport.contractimpl.common.certificatevalidation.RevocationStatus)1 OCSPCache (io.ballerina.stdlib.http.transport.contractimpl.common.certificatevalidation.ocsp.OCSPCache)1