16

クライアントのjava.security.cert.X509Certificateだけを指定して、OCSPを使用してJavaで証明書失効ステータスを手動で確認するにはどうすればよいですか。私はそれを行うための明確な方法を見ることができません。

または、Tomcatに自動的に実行させることはできますか?また、ソリューションが正しいことをどのように知ることができますか?

4

4 に答える 4

18

私は最も優れた解決策を見つけました:

http://www.docjar.com/html/api/sun/security/provider/certpath/OCSP.java.html

        /**
   54    * This is a class that checks the revocation status of a certificate(s) using
   55    * OCSP. It is not a PKIXCertPathChecker and therefore can be used outside of
   56    * the CertPathValidator framework. It is useful when you want to
   57    * just check the revocation status of a certificate, and you don't want to
   58    * incur the overhead of validating all of the certificates in the
   59    * associated certificate chain.
   60    *
   61    * @author Sean Mullan
   62    */

トリックを行うメソッドチェック(X509Certificate clientCert、X509Certificate issuerCert)があります!

于 2011-03-09T16:53:26.763 に答える
3

ocsp検証を有効にするためのTomcat用のパッチがここにあるようです。

手動で行うことを選択した場合:

Security.setProperty("ocsp.enable", "true")

または、コマンドライン引数を使用して設定します。ここを参照してください

このプロパティの値はtrueまたはfalseのいずれかです。trueの場合、証明書失効チェックを実行するときにOCSPチェックが有効になります。falseまたは設定されていない場合、OCSPチェックは無効になります。

そして、これが私がうまくいくと思ういくつかのコードです:

interface ValidationStrategy {
    boolean validate(X509Certificate certificate, CertPath certPath,
            PKIXParameters parameters) throws GeneralSecurityException;
}


class SunOCSPValidationStrategy implements ValidationStrategy {
    @Override
    public boolean validate(X509Certificate certificate, CertPath certPath,
            PKIXParameters parameters) throws GeneralSecurityException {
        try {
            CertPathValidator cpv = CertPathValidator.getInstance("PKIX");
            PKIXCertPathValidatorResult result = (PKIXCertPathValidatorResult) cpv
                    .validate(certPath, parameters);
            Signature.LOG.debug("Validation result is: " + result);
            return true; // if no exception is thrown
        } catch (CertPathValidatorException cpve) {

            // if the exception is (or is caused by)
            // CertificateRevokedException, return false;
            // otherwise re-throw, because this indicates a failure to perform
            // the validation
            Throwable cause = ExceptionUtils.getRootCause(cpve);
            Class<? extends Throwable> exceptionClass = cause != null ? cause.getClass()
                    : cpve.getClass();
            if (exceptionClass.getSimpleName().equals("CertificateRevokedException")) {
                return false;
            }
            throw cpve;
        }
    }

}
于 2011-03-01T23:08:12.530 に答える
3

これは、servletRequestリクエストから取得した証明書の配列を取得し、OCSPを使用したcertpathAPIを介してそれらを検証するJetty7の関連コードです。

http://grepcode.com/file/repo1.maven.org/maven2/org.eclipse.jetty/jetty-util/7.4.0.v20110414/org/eclipse/jetty/util/security/CertificateValidator.java#189

于 2011-08-24T20:17:02.397 に答える
0
import org.bouncycastle.util.io.pem.PemReader;
import sun.security.provider.certpath.OCSP;
import sun.security.x509.X509CertImpl;
import java.io.IOException;
import java.io.StringReader;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.cert.CertPathValidatorException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Date;

public void test() throws IOException, CertPathValidatorException, java.security.cert.CertificateException {
        X509Certificate userCert = getX509Cert("path_to_user_cert");
        X509Certificate caCert = getX509Cert("path_to_CA_cert");
        OCSP.RevocationStatus ocsp = OCSP.check(userCert, caCert, URI.create("URL to OCSP, but this can be read from USER Cert(AuthorityInfoAccess) As well"), caCert, new Date());
        System.out.println(ocsp);
    }

    private X509CertImpl getX509Cert(final String path) throws CertificateException, IOException {
        return new X509CertImpl(
                new PemReader(
                        new StringReader(
                                new String(
                                        Files.readAllBytes(
                                                Paths.get(path)))))
                        .readPemObject()
                        .getContent());
    }
于 2017-03-17T20:06:47.220 に答える