title: How to verify for counter signed XML document?
body: <p>How to verify the use library <code>Xades4j</code> for counter signed xml document.
  Iam getting the following error when verifying with <code>Xades4j</code> : </p>
  
  <blockquote>
    <p>xades4j.verification.CounterSignatureSigValueRefException:
    Verification failed for property 'CounterSignature': the counter
    signature doesn't reference the SignatureValue element of the
    countersigned signature   at
    xades4j.verification.CounterSignatureVerifier.verify(CounterSignatureVerifier.java:75)
      at
    xades4j.verification.CounterSignatureVerifier.verify(CounterSignatureVerifier.java:37)
      at
    xades4j.verification.GenericDOMDataVerifier.verify(GenericDOMDataVerifier.java:65)
      at
    xades4j.verification.GenericDOMDataVerifier.verify(GenericDOMDataVerifier.java:30)
      at
    xades4j.verification.QualifyingPropertiesVerifierImpl.verifyProperties(QualifyingPropertiesVerifierImpl.java:59)
      at
    xades4j.verification.XadesVerifierImpl.verify(XadesVerifierImpl.java:187)
      at
    com.fit.einvoice.ingcountersigner.service.xades.XadesVerifyOperation.verifySignature(XadesVerifyOperation.java:92)
      at
    com.fit.einvoice.ingcountersigner.service.xades.XadesVerifyOperation.verifySignature(XadesVerifyOperation.java:87)
      at
    com.fit.einvoice.ingcountersigner.service.xades.XadesVerifyOperation.verifySignature(XadesVerifyOperation.java:64)</p>
  </blockquote>
  
  <p>My validation function : </p>
  
  <pre><code>static void checkSigned(File file) {
       InputStream inputStream = null;
       try {
           inputStream = new FileInputStream(file);
           XadesVerifyOperation verifyOperation = new XadesVerifyOperation();
           ArrayList&lt;XadesVerificationResults&gt; results = verifyOperation.verifySignature(inputStream);
              System.out.println("results size: " + results.size());
              for (XadesVerificationResults result : results) {
                  System.out.println(result.SigningCertificate.getIssuerDN());
              }
          } catch (Exception e) {
              e.printStackTrace();
          } finally {
              try {
                  inputStream.close();
              } catch (IOException ex) {
              }
        }
   }
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>My counter signed function : </p>
  
  <pre><code>public void CounterSign() throws TransformerFactoryConfigurationError, Exception {
          Document doc = SignatureServicesBase.getDocument(_inputStream);
          Element sigElem = (Element) doc.getElementsByTagNameNS(Constants.SignatureSpecNS, Constants._TAG_SIGNATURE).item(0);
  
          System.out.println(sigElem.getNodeName());
  
          org.apache.xml.security.Init.init();
          XMLSignature xmlSig = new XMLSignature(sigElem, doc.getBaseURI());
  
           //Create counter signer
          XadesBesSigningProfile signingProfile = new XadesBesSigningProfile(new Pkcs11KeyingDataProvider(_certInfo));
          signingProfile.withAlgorithmsProvider(Sha1AlgProvider.class);
          signingProfile.withBasicSignatureOptionsProvider(new MyBasicSignatureOptionsProvider(true, true, false));
          final XadesSigner counterSigner = signingProfile.newSigner();
  
          //Extend with counter signature
          XadesFormatExtenderProfile extenderProfile = new XadesFormatExtenderProfile();
          XadesSignatureFormatExtender extender = extenderProfile.getFormatExtender();
          List unsignedProps = Arrays.asList(new CounterSignatureProperty(counterSigner));
          extender.enrichSignature(xmlSig, new UnsignedProperties(unsignedProps));
  
          SignatureServicesBase.outputDocument(doc, _outStream);
  
          if (!_isStream) {
              _inputStream.close();
              _outStream.close();
          }
      }
  </code></pre>