仅给定客户端的 java.security.cert.X509Certificate,如何使用 OCSP 在 java 中手动检查证书吊销状态?我看不到一个明确的方法来做到这一点。
或者,我可以让tomcat自动为我做,你怎么知道你的解决方案是真的?
仅给定客户端的 java.security.cert.X509Certificate,如何使用 OCSP 在 java 中手动检查证书吊销状态?我看不到一个明确的方法来做到这一点。
或者,我可以让tomcat自动为我做,你怎么知道你的解决方案是真的?
我找到了一个最优秀的解决方案:
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 */
它有一个方法 check(X509Certificate clientCert, X509Certificate issuerCert) 可以解决问题!
这是来自 Jetty 7 的相关代码,它采用从 servletRequest 请求中提取的证书数组,并通过 certpath API 和 OCSP 验证它们。
似乎这里有一个Tomcat 补丁来启用 ocsp 验证。
如果您选择手动操作:
Security.setProperty("ocsp.enable", "true")
或者通过命令行参数设置它。见这里:
该属性的值为真或假。如果为 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;
}
}
}
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());
}