Java SSL Certificate Revocation Checking

前端 未结 4 1405
旧巷少年郎
旧巷少年郎 2020-12-15 12:53

I\'m currently writing a network TCP server using SSL. In production, we\'ll finally require clients to authenticate with a certificate.

In order to revoke certifica

4条回答
  •  萌比男神i
    2020-12-15 13:24

    I figured how to enable CRL checking within a SSLContext without implementing a custom validator, as suggested in the comments.

    It is mainly about properly initializing the SSLContext's TrustManagers with a revocation checker, only a few lines, no custom check logic and the CRL is now checked automatically as well as the verification path.

    Here's a snippet...

    KeyStore ts = KeyStore.getInstance("JKS");
    FileInputStream tfis = new FileInputStream(trustStorePath);
    ts.load(tfis, trustStorePass.toCharArray());
    
    KeyManagerFactory kmf =  KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    
    // initialize certification path checking for the offered certificates and revocation checks against CLRs
    CertPathBuilder cpb = CertPathBuilder.getInstance("PKIX");
    PKIXRevocationChecker rc = (PKIXRevocationChecker)cpb.getRevocationChecker();
    rc.setOptions(EnumSet.of(
        PKIXRevocationChecker.Option.PREFER_CRLS, // prefer CLR over OCSP
        PKIXRevocationChecker.Option.ONLY_END_ENTITY, 
    PKIXRevocationChecker.Option.NO_FALLBACK)); // don't fall back to OCSP checking
    
    PKIXBuilderParameters pkixParams = new PKIXBuilderParameters(ts, new X509CertSelector());
    pkixParams.addCertPathChecker(rc);
    
    tmf.init( new CertPathTrustManagerParameters(pkixParams) );
    // init KeyManagerFactory
    kmf.init(...)
    
    SSLContext ctx = SSLContext.getInstance("TLS");
    ctx.init(kmf.getKeyManagers), tmf.getTrustManagers(), null);
    

    That essentially did what I needed in my application, checking whether a certificate issued to a client is revoked in our CRL. Only checking the end entity and allowing the CRL check to fail is accepted because its all our infrastructure.

提交回复
热议问题