Search in sources :

Example 1 with OCSPVerifier

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

the class RevocationVerificationManager method verifyRevocationStatus.

/**
 * This method first tries to verify the given certificate chain using OCSP since OCSP verification is
 * faster. If that fails it tries to do the verification using CRL.
 *
 * @param peerCertificates javax.security.cert.X509Certificate[] array of peer certificate chain from peer/client.
 * @throws CertificateVerificationException Occurs when certificate fails to be validated from both OCSP and CRL.
 * @return true If the process of certificate revocation becomes successful.
 */
public boolean verifyRevocationStatus(javax.security.cert.X509Certificate[] peerCertificates) throws CertificateVerificationException {
    X509Certificate[] convertedCertificates = convert(peerCertificates);
    long start = System.currentTimeMillis();
    // If not set by the user, default cache size will be 50 and default cache delay will be 15 mins.
    OCSPCache ocspCache = OCSPCache.getCache();
    ocspCache.init(cacheSize, cacheDelayMins);
    CRLCache crlCache = CRLCache.getCache();
    crlCache.init(cacheSize, cacheDelayMins);
    RevocationVerifier[] verifiers = { new OCSPVerifier(ocspCache), new CRLVerifier(crlCache) };
    for (RevocationVerifier verifier : verifiers) {
        try {
            CertificatePathValidator pathValidator = new CertificatePathValidator(convertedCertificates, verifier);
            pathValidator.validatePath();
            if (LOG.isInfoEnabled()) {
                LOG.info("Path verification is successful. Took {} ms.", System.currentTimeMillis() - start);
            }
            return true;
        } catch (Exception e) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("{} failed.", verifier.getClass().getSimpleName());
                LOG.debug("Certificate verification with {} failed. ", verifier.getClass().getSimpleName(), e);
            }
        }
    }
    throw new CertificateVerificationException("Path verification failed for both OCSP and CRL");
}
Also used : CertificatePathValidator(io.ballerina.stdlib.http.transport.contractimpl.common.certificatevalidation.pathvalidation.CertificatePathValidator) OCSPCache(io.ballerina.stdlib.http.transport.contractimpl.common.certificatevalidation.ocsp.OCSPCache) X509Certificate(java.security.cert.X509Certificate) CRLCache(io.ballerina.stdlib.http.transport.contractimpl.common.certificatevalidation.crl.CRLCache) OCSPVerifier(io.ballerina.stdlib.http.transport.contractimpl.common.certificatevalidation.ocsp.OCSPVerifier) CertificateEncodingException(javax.security.cert.CertificateEncodingException) CertificateException(java.security.cert.CertificateException) CRLVerifier(io.ballerina.stdlib.http.transport.contractimpl.common.certificatevalidation.crl.CRLVerifier)

Example 2 with OCSPVerifier

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

the class OCSPVerifierTest method testOCSPVerifier.

/**
 * A fake certificate signed by a fake CA is made as the revoked certificate. The created OCSP response to the
 * OCSP request will say that that the fake peer certificate is revoked. the SingleResp derived from the OCSP
 * response will be put into the cache against the serial number of the fake peer certificate. Since the SingleResp
 * which corresponds to the revokedSerialNumber is in the cache, there will NOT be a call to a remote OCSP server.
 * Note that the serviceUrl passed to cache.setCacheValue(..) is null since it is not needed.
 *
 * @throws Exception
 */
@Test
public void testOCSPVerifier() throws Exception {
    // Add BouncyCastle as Security Provider.
    Security.addProvider(new BouncyCastleProvider());
    Utils utils = new Utils();
    KeyPair caKeyPair = utils.generateRSAKeyPair();
    X509Certificate caCert = utils.generateFakeRootCert(caKeyPair);
    KeyPair peerKeyPair = utils.generateRSAKeyPair();
    BigInteger revokedSerialNumber = BigInteger.valueOf(111);
    X509Certificate revokedCertificate = utils.generateFakeCertificate(caCert, peerKeyPair.getPublic(), revokedSerialNumber, caKeyPair);
    OCSPReq request = getOCSPRequest(caCert, revokedSerialNumber);
    byte[] issuerCertEnc = caCert.getEncoded();
    X509CertificateHolder certificateHolder = new X509CertificateHolder(issuerCertEnc);
    DigestCalculatorProvider digCalcProv = new JcaDigestCalculatorProviderBuilder().setProvider(Constants.BOUNCY_CASTLE_PROVIDER).build();
    // CertID structure is used to uniquely identify certificates that are the subject of
    // an OCSP request or response and has an ASN.1 definition. CertID structure is defined in RFC 2560.
    CertificateID revokedID = new CertificateID(digCalcProv.get(CertificateID.HASH_SHA1), certificateHolder, revokedSerialNumber);
    OCSPResp response = generateOCSPResponse(request, certificateHolder, caKeyPair.getPrivate(), revokedID);
    SingleResp singleResp = ((BasicOCSPResp) response.getResponseObject()).getResponses()[0];
    OCSPCache cache = OCSPCache.getCache();
    cache.init(5, 5);
    cache.setCacheValue(response, revokedSerialNumber, singleResp, request, null);
    OCSPVerifier ocspVerifier = new OCSPVerifier(cache);
    RevocationStatus status = ocspVerifier.checkRevocationStatus(revokedCertificate, caCert);
    // the cache will have the SingleResponse derived from the create OCSP response and it will be checked to see
    // if the fake certificate is revoked. So the status should be REVOKED.
    assertTrue(status == RevocationStatus.REVOKED);
}
Also used : KeyPair(java.security.KeyPair) OCSPCache(io.ballerina.stdlib.http.transport.contractimpl.common.certificatevalidation.ocsp.OCSPCache) CertificateID(org.bouncycastle.cert.ocsp.CertificateID) X509Certificate(java.security.cert.X509Certificate) OCSPResp(org.bouncycastle.cert.ocsp.OCSPResp) BasicOCSPResp(org.bouncycastle.cert.ocsp.BasicOCSPResp) DigestCalculatorProvider(org.bouncycastle.operator.DigestCalculatorProvider) OCSPReq(org.bouncycastle.cert.ocsp.OCSPReq) X509CertificateHolder(org.bouncycastle.cert.X509CertificateHolder) BigInteger(java.math.BigInteger) RevocationStatus(io.ballerina.stdlib.http.transport.contractimpl.common.certificatevalidation.RevocationStatus) JcaDigestCalculatorProviderBuilder(org.bouncycastle.operator.jcajce.JcaDigestCalculatorProviderBuilder) SingleResp(org.bouncycastle.cert.ocsp.SingleResp) OCSPVerifier(io.ballerina.stdlib.http.transport.contractimpl.common.certificatevalidation.ocsp.OCSPVerifier) BouncyCastleProvider(org.bouncycastle.jce.provider.BouncyCastleProvider) Test(org.testng.annotations.Test)

Example 3 with OCSPVerifier

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

the class OCSPVerifierTest method getOCSPRequest.

/**
 * An OCSP request is made to be given to the fake CA.
 *
 * @param caCert Fake CA certificate.
 * @param revokedSerialNumber Serial number of the certificate which needs to be checked if revoked.
 * @return Created OCSP request.
 * @throws Exception
 */
private OCSPReq getOCSPRequest(X509Certificate caCert, BigInteger revokedSerialNumber) throws Exception {
    OCSPVerifier ocspVerifier = new OCSPVerifier(null);
    Class ocspVerifierClass = ocspVerifier.getClass();
    Method generateOCSPRequest = ocspVerifierClass.getDeclaredMethod("generateOCSPRequest", X509Certificate.class, BigInteger.class);
    generateOCSPRequest.setAccessible(true);
    return (OCSPReq) generateOCSPRequest.invoke(ocspVerifier, caCert, revokedSerialNumber);
}
Also used : OCSPReq(org.bouncycastle.cert.ocsp.OCSPReq) Method(java.lang.reflect.Method) OCSPVerifier(io.ballerina.stdlib.http.transport.contractimpl.common.certificatevalidation.ocsp.OCSPVerifier)

Example 4 with OCSPVerifier

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

the class RevocationVerificationTest method ocspPathValidation.

private void ocspPathValidation(X509Certificate[] certChain) throws Exception {
    OCSPCache ocspCache = OCSPCache.getCache();
    ocspCache.init(5, 5);
    RevocationVerifier verifier = new OCSPVerifier(ocspCache);
    CertificatePathValidator pathValidator = new CertificatePathValidator(certChain, verifier);
    pathValidator.validatePath();
}
Also used : CertificatePathValidator(io.ballerina.stdlib.http.transport.contractimpl.common.certificatevalidation.pathvalidation.CertificatePathValidator) OCSPCache(io.ballerina.stdlib.http.transport.contractimpl.common.certificatevalidation.ocsp.OCSPCache) RevocationVerifier(io.ballerina.stdlib.http.transport.contractimpl.common.certificatevalidation.RevocationVerifier) OCSPVerifier(io.ballerina.stdlib.http.transport.contractimpl.common.certificatevalidation.ocsp.OCSPVerifier)

Example 5 with OCSPVerifier

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

the class Utils method getOCSPRequest.

/**
 * An OCSP request is created to be given to the fake CA. The certificate serial numbers are similar to the
 * ballerina client's serial number.
 *
 * @param caCert Fake CA certificate.
 * @param serialNumber Serial number of the certificate which needs to be checked if revoked.
 * @return Created OCSP request.
 * @throws Exception If any error occurs.
 */
static OCSPReq getOCSPRequest(X509Certificate caCert, BigInteger serialNumber) throws Exception {
    OCSPVerifier ocspVerifier = new OCSPVerifier(null);
    Class ocspVerifierClass = ocspVerifier.getClass();
    Method generateOCSPRequest = ocspVerifierClass.getDeclaredMethod("generateOCSPRequest", X509Certificate.class, BigInteger.class);
    generateOCSPRequest.setAccessible(true);
    return (OCSPReq) generateOCSPRequest.invoke(ocspVerifier, caCert, serialNumber);
}
Also used : OCSPReq(org.bouncycastle.cert.ocsp.OCSPReq) Method(java.lang.reflect.Method) OCSPVerifier(io.ballerina.stdlib.http.transport.contractimpl.common.certificatevalidation.ocsp.OCSPVerifier)

Aggregations

OCSPVerifier (io.ballerina.stdlib.http.transport.contractimpl.common.certificatevalidation.ocsp.OCSPVerifier)5 OCSPCache (io.ballerina.stdlib.http.transport.contractimpl.common.certificatevalidation.ocsp.OCSPCache)3 OCSPReq (org.bouncycastle.cert.ocsp.OCSPReq)3 CertificatePathValidator (io.ballerina.stdlib.http.transport.contractimpl.common.certificatevalidation.pathvalidation.CertificatePathValidator)2 Method (java.lang.reflect.Method)2 X509Certificate (java.security.cert.X509Certificate)2 RevocationStatus (io.ballerina.stdlib.http.transport.contractimpl.common.certificatevalidation.RevocationStatus)1 RevocationVerifier (io.ballerina.stdlib.http.transport.contractimpl.common.certificatevalidation.RevocationVerifier)1 CRLCache (io.ballerina.stdlib.http.transport.contractimpl.common.certificatevalidation.crl.CRLCache)1 CRLVerifier (io.ballerina.stdlib.http.transport.contractimpl.common.certificatevalidation.crl.CRLVerifier)1 BigInteger (java.math.BigInteger)1 KeyPair (java.security.KeyPair)1 CertificateException (java.security.cert.CertificateException)1 CertificateEncodingException (javax.security.cert.CertificateEncodingException)1 X509CertificateHolder (org.bouncycastle.cert.X509CertificateHolder)1 BasicOCSPResp (org.bouncycastle.cert.ocsp.BasicOCSPResp)1 CertificateID (org.bouncycastle.cert.ocsp.CertificateID)1 OCSPResp (org.bouncycastle.cert.ocsp.OCSPResp)1 SingleResp (org.bouncycastle.cert.ocsp.SingleResp)1 BouncyCastleProvider (org.bouncycastle.jce.provider.BouncyCastleProvider)1