如何使用XADES4j,证书存储窗口和智能卡+ PIN码签署xml

时间:2017-10-17 16:58:07

标签: java xml xades4j gemalto

我正在尝试使用XADES4j签署一个xml文件。我还使用智能卡认证(将从Windows商店加载)。

是否有一个符合我搜索范围的示例,因为我是XML签名和智能卡的新手。我搜索了几个星期而没有成功。

还有其他一些例子,但不是很清楚:

Example1 Example2

我在https://github.com/luisgoncalves/xades4j/wiki/DefiningKeyingData找到了这个演示,但我不知道如何设置函数PKCS11KeyStoreKeyingDataProvider来应用Windows证书参数及其密码:

    KeyingDataProvider kp = new PKCS11KeyStoreKeyingDataProvider(
              "path/to/native/lib",
               "MS SABRI", // CERTIFICATE NAME
               new FirstCertificateSelector(),
               null, 
               null,false);, 

我的代码:

          try {

     // >>> TEST N°1
     // KeyingDataProvider kp = new DirectKeyingDataProvider((X509Certificate) certExemple, PrivateKEY);

      // >>> TEST N°2
      KeyingDataProvider kp = new PKCS11KeyStoreKeyingDataProvider(
              "path/to/native/lib",
               "name", // CERTIFICATE NAME
               new FirstCertificateSelector(),
               new DirectPasswordProvider("123456"), // PIN CODE
               new DirectPasswordProvider("123456"), // PIN CODE
               false);



      // XADES
      XadesSigningProfile p = new XadesBesSigningProfile(kp);
      XadesSigner signer    = p.newSigner();

      javax.xml.parsers.DocumentBuilderFactory factory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
      factory.setNamespaceAware(true);
      javax.xml.parsers.DocumentBuilder builder = null;
      builder = factory.newDocumentBuilder();


      // XML FILE TO BE SIGNED
      Document doc1 = builder.parse(new File("FileNotSigned.xml"));

      // NODE
      Node parentElement            = doc1.getDocumentElement();
      Node nodeToSign               = doc1.getDocumentElement().getFirstChild();
      Node nodeToAttachSignature    = doc1.getDocumentElement();


      IndividualDataObjsTimeStampProperty dataObjsTimeStamp = new IndividualDataObjsTimeStampProperty();
      AllDataObjsCommitmentTypeProperty globalCommitment    = AllDataObjsCommitmentTypeProperty.proofOfApproval();
      CommitmentTypeProperty commitment                     = CommitmentTypeProperty.proofOfCreation();   

      // XPATH STRING
      String xpathHeader    ="/InvoiceHeader";
      String xpathBody      ="/InvoiceBody";

      // OBJECT
      DataObjectDesc obj1 = new DataObjectReference("");
      obj1.withTransform(XPath2Filter.intersect( xpathHeader ).intersect(xpathBody));
      SignedDataObjects dataObjs = new SignedDataObjects( obj1 );

      // SIGN
      signer.sign(dataObjs, nodeToAttachSignature);

      // TRANSFORMER
      Transformer transformer = TransformerFactory.newInstance().newTransformer();

      // XML SIGNED
      Result output = new StreamResult(new File("FileSigned.xml"));
      Source input  = new DOMSource(doc1);
      transformer.transform(input, output);

1 个答案:

答案 0 :(得分:0)

我不确定“Windows商店+智能卡”是什么意思,因为它们看起来都是独家的。无论如何,如果您想使用智能卡,您的代码几乎可以。

智能卡通常具有安装在主机操作系统上的本机库。在PKCS11KeyStoreKeyingDataProvider的第一个参数上,您应该将路径传递给该库。第二个参数(name)只是注册提供者实例的名称。由于智能卡通常会处理PIN以访问密钥,因此您通常可以为nullkeyStorePasswordProvider参数提供entryPasswordProvider

在图书馆单元测试中,您可以找到example using the Portuguese citizen card

希望这有帮助。