实例化类的正确方法,使I'我能够验证证书中的路径链



我有3d方库(针对Kalkan提供者的sdk)。我相信这个库的一部分会检查证书路径。问题是,我应该传递两个参数来正确地实例化类,以便使用我需要的方法之一。

这里的代码:

final PKIXCertPathReviewer checker = new PKIXCertPathReviewer(cp, params);
boolean test = checker.isValidCertPath();

这是构造函数的一部分:

public PKIXCertPathReviewer(CertPath certPath, PKIXParameters params)

关于任务有点。我已通过客户证书签署了文件。我想验证客户端证书中的路径。所以我有客户端的X509Certificate证书实例,一个中间证书和一个根证书。最后两个是磁盘上cer格式的文件。据我所知,我应该把这三个证书合并在一起。如果有人向我展示如何创建cpparams,那将非常有用。提前谢谢。

所以我设法解决了我的问题。如果有人和我有同样的问题,这是代码。

CertificateFactory cf = CertificateFactory.getInstance("X.509", KalkanProvider.PROVIDER_NAME);
java.security.cert.Certificate rootCertificate = /*root certificate*/;
java.security.cert.Certificate clientCertificate = /*client certificate*/;
List mylist = new ArrayList();
mylist.add(clientCertificate);
CertPath cp = cf.generateCertPath(mylist);
TrustAnchor rootAnchor = new TrustAnchor((X509Certificate) rootCertificate, null);
Set<TrustAnchor> trustStore = new HashSet<>();
trustStore.add(rootAnchor);
if (null != /*chain contains middle cert besides root*/) {
java.security.cert.Certificate middleCertificate = /*middle certificate*/;
TrustAnchor middleAnchor = new TrustAnchor((X509Certificate) middleCertificate, null);
trustStore.add(middleAnchor);
}
PKIXParameters params = new PKIXParameters(trustStore);
params.setRevocationEnabled(false);//true - if you need ocsp validation
final PKIXCertPathReviewer checker = new PKIXCertPathReviewer(cp, params);
errors = checker.getErrors();
boolean result = checker.isValidCertPath();

这里有更好的解决方案:

Provider p = ProviderUtil.loadKalkanProvider();
X509Certificate ca = X509Util.loadX509Certificate("/tmp/ca/root_ca_rsa.cer", p);
X509Certificate nca = X509Util.loadX509Certificate("/tmp/ca/root_nca_rsa.cer", p);
X509Certificate cert = X509Util.loadX509Certificate("/tmp/cert/user.cer", p);
X509CRL crl = X509Util.loadX509CRL("/tmp/ca/rsa.crl", p);
X509CRL rcrl = X509Util.loadX509CRL("/tmp/ca/root_rsa.crl", p);
ArrayList<X509Extension> list = new ArrayList<X509Extension>();
list.add(nca);
list.add(cert);
list.add(crl);
list.add(rcrl);
CollectionCertStoreParameters certStoreParameters = new CollectionCertStoreParameters(list);
CertStore certStore = CertStore.getInstance("Collection", certStoreParameters, p);
CertPathBuilder builder = CertPathBuilder.getInstance("PKIX", p);
// проверка до корневого CA, можно сократить до промежуточного NCA
TrustAnchor anchor = new TrustAnchor(ca, null);
Set<TrustAnchor> anchors = new HashSet<TrustAnchor>();
anchors.add(anchor);
X509CertSelector selector = new X509CertSelector();
// задаем параметры для селектора конечного сертификата или можно указать полное соответствие сертификата
//selector.setSerialNumber(cert.getSerialNumber());
//selector.setIssuer(cert.getIssuerX500Principal());
selector.setCertificate(cert);
PKIXBuilderParameters builderParameters = new PKIXBuilderParameters(anchors, selector);
// если не добавляли список CRL, то надо отключить проверку
//builderParameters.setRevocationEnabled(false);
builderParameters.addCertStore(certStore);
builderParameters.setSigProvider(p.getName());
PKIXCertPathBuilderResult builderResult = (PKIXCertPathBuilderResult) builder.build(builderParameters);
System.out.println(builderResult);
// можем закодировать и сохранить полученный путь в форматах PkiPath/PKCS7/PEM
//builderResult.getCertPath().getEncoded("PkiPath");

相关内容

  • 没有找到相关文章

最新更新