Java 嵌入x509证书的xades4j验证问题

Java 嵌入x509证书的xades4j验证问题,java,xml-signature,xades4j,Java,Xml Signature,Xades4j,我正在使用智能卡进行签名,并使用SunMSCAPI提供程序和windows密钥存储。 使用xades4j 1.3.2版本,我正在唱一个xml文件,如下所示,它工作正常,证书也嵌入到KeyInfo的xml文件中 private Document signXMLData(Document doc) { try { XadesSigningProfile p; p = new XadesBesSigningProfile(keyingDa

我正在使用智能卡进行签名,并使用SunMSCAPI提供程序和windows密钥存储。 使用xades4j 1.3.2版本,我正在唱一个xml文件,如下所示,它工作正常,证书也嵌入到KeyInfo的xml文件中

private Document signXMLData(Document doc) {
        try {
            XadesSigningProfile p;
            p = new XadesBesSigningProfile(keyingDataProvider).withSignaturePropertiesProvider(new SignaturePropertiesProvider() {
                @Override
                public void provideProperties(SignaturePropertiesCollector signedPropsCol) {
                    signedPropsCol.setSignerRole(new SignerRoleProperty(SignerRole));
                    signedPropsCol.setSignatureProductionPlace(new SignatureProductionPlaceProperty(City, State, PostalCode, Country));
                    signedPropsCol.setSigningTime(new SigningTimeProperty());
                }
            });
            XadesSigner signer = p.newSigner();
            Element elemToSign = doc.getDocumentElement();
            new Enveloped(signer).sign(elemToSign);
            Log.LogOperation("XML signing completed successfully.");
        } catch (Exception ex) {
            Log.LogException(ex);
            doc = null;
        }
        return doc;
    }
但我的问题是,在使用以下代码进行验证时,我得到了以下错误

代码

错误

我尝试了中建议的方法,但也出现了同样的错误


请说明我做错了什么。

从错误中,它似乎与混淆安全提供程序有关,可能是因为您从“SunMSCAPI”创建了
KeyStore
,然后
X509Certificate
是由XML签名上使用的提供程序创建的,而XML签名似乎是弹性城堡

无论如何,由于签名证书包含在签名中,因此无需解析XML并查找它。xades4j的目的是让你远离那些东西。xades4j将收集
KeyInfo
中的所有证书,并在验证签名证书时使用它们构建一个链(在这种情况下,可能只有一个)

如果验证您的证书所需的所有中间证书和根证书都位于Windows根目录上或
KeyInfo
中,则只需创建受信任的根密钥库
KeyStore
,并按原样将其传递给
PKIXCertificateValidationProvider
。如果需要包含其他证书,可以使用
PKIXCertificateValidationProvider
构造函数的
CertStore
参数

总之,您需要的所有代码都是第一部分和最后一部分。比如:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
Document doc = dbf.newDocumentBuilder().parse(isIn);
DOMHelper.useIdAsXmlId(doc.getDocumentElement());

KeyStore ks = KeyStore.getInstance("Windows-ROOT", "SunMSCAPI");
ks.load(null, null);
CertificateValidationProvider validationProviderMySigs = new PKIXCertificateValidationProvider(ks, false, null);
XadesVerificationProfile instance = new XadesVerificationProfile(validationProviderMySigs);
XadesVerifier verifier = instance.newVerifier();
XAdESVerificationResult r = verifier.verify(getSigElement(doc), null);

谢谢你的宝贵意见。我尝试了以上所有的可能性。X509证书由BC创建。我尝试了密钥库值BKS、JKS和使用问题中的代码导入的证书。但在所有情况下,相同的错误都会出现在XadesVerifier verifier=instance.newVerifier();。我也尝试了上面的代码,但也抛出了同样的错误。还有其他解决方法吗?如果在
KeyStore.getInstance()
调用中不提供提供提供程序怎么办?如果您的意思是使用KeyStore.DefaultProvider(),它会给出相同的错误。getinstance需要一些值。我是说,它应该返回第一个支持所提供类型的提供程序。如果这不起作用,我就没有主意了……哦,对不起,这样也会得到相同的结果,KeyStore ks=KeyStore.getInstance(“Windows根”);
Log Date Time:- 2017/06/08 14:38:01
xades4j.utils.XadesProfileResolutionException: com.google.inject.internal.ComputationException: java.lang.SecurityException: class "org.bouncycastle.cms.jcajce.JcaX509CertSelectorConverter"'s signer information does not match signer information of other classes in the same package
    at xades4j.utils.XadesProfileCore.getInstance(XadesProfileCore.java:223)
    at xades4j.verification.XadesVerificationProfile.newVerifier(XadesVerificationProfile.java:147)
    at slr.DigitalVerification.VerifyXMLSign(DigitalVerification.java:163)
    at slr.Handlers$OpenEVerifierHandler.handle(Handlers.java:186)
    at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:77)
    at sun.net.httpserver.AuthFilter.doFilter(AuthFilter.java:83)
    at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:80)
    at sun.net.httpserver.ServerImpl$Exchange$LinkHandler.handle(ServerImpl.java:677)
    at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:77)
    at sun.net.httpserver.ServerImpl$Exchange.run(ServerImpl.java:647)
    at sun.net.httpserver.ServerImpl$DefaultExecutor.execute(ServerImpl.java:158)
    at sun.net.httpserver.ServerImpl$Dispatcher.handle(ServerImpl.java:433)
    at sun.net.httpserver.ServerImpl$Dispatcher.run(ServerImpl.java:398)
    at java.lang.Thread.run(Thread.java:745)
Caused by: com.google.inject.internal.ComputationException: ..........
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
Document doc = dbf.newDocumentBuilder().parse(isIn);
DOMHelper.useIdAsXmlId(doc.getDocumentElement());

KeyStore ks = KeyStore.getInstance("Windows-ROOT", "SunMSCAPI");
ks.load(null, null);
CertificateValidationProvider validationProviderMySigs = new PKIXCertificateValidationProvider(ks, false, null);
XadesVerificationProfile instance = new XadesVerificationProfile(validationProviderMySigs);
XadesVerifier verifier = instance.newVerifier();
XAdESVerificationResult r = verifier.verify(getSigElement(doc), null);