Search in sources :

Example 1 with DigestInfo

use of org.apache.poi.poifs.crypt.dsig.DigestInfo in project poi by apache.

the class TestSignatureInfo method sign.

private void sign(OPCPackage pkgCopy, String alias, String signerDn, int signerCount) throws Exception {
    initKeyPair(alias, signerDn);
    SignatureConfig signatureConfig = new SignatureConfig();
    signatureConfig.setKey(keyPair.getPrivate());
    signatureConfig.setSigningCertificateChain(Collections.singletonList(x509));
    signatureConfig.setExecutionTime(cal.getTime());
    signatureConfig.setDigestAlgo(HashAlgorithm.sha1);
    signatureConfig.setOpcPackage(pkgCopy);
    SignatureInfo si = new SignatureInfo();
    si.setSignatureConfig(signatureConfig);
    Document document = DocumentHelper.createDocument();
    // operate
    DigestInfo digestInfo = si.preSign(document, null);
    // verify
    assertNotNull(digestInfo);
    LOG.log(POILogger.DEBUG, "digest algo: " + digestInfo.hashAlgo);
    LOG.log(POILogger.DEBUG, "digest description: " + digestInfo.description);
    assertEquals("Office OpenXML Document", digestInfo.description);
    assertNotNull(digestInfo.hashAlgo);
    assertNotNull(digestInfo.digestValue);
    // setup: key material, signature value
    byte[] signatureValue = si.signDigest(digestInfo.digestValue);
    // operate: postSign
    si.postSign(document, signatureValue);
    // verify: signature
    si.getSignatureConfig().setOpcPackage(pkgCopy);
    List<X509Certificate> result = new ArrayList<X509Certificate>();
    for (SignaturePart sp : si.getSignatureParts()) {
        if (sp.validate()) {
            result.add(sp.getSigner());
        }
    }
    assertEquals(signerCount, result.size());
}
Also used : SignatureInfo(org.apache.poi.poifs.crypt.dsig.SignatureInfo) DigestInfo(org.apache.poi.poifs.crypt.dsig.DigestInfo) SignatureConfig(org.apache.poi.poifs.crypt.dsig.SignatureConfig) ArrayList(java.util.ArrayList) Document(org.w3c.dom.Document) SignatureDocument(org.w3.x2000.x09.xmldsig.SignatureDocument) SignaturePart(org.apache.poi.poifs.crypt.dsig.SignatureInfo.SignaturePart) X509Certificate(java.security.cert.X509Certificate)

Aggregations

X509Certificate (java.security.cert.X509Certificate)1 ArrayList (java.util.ArrayList)1 DigestInfo (org.apache.poi.poifs.crypt.dsig.DigestInfo)1 SignatureConfig (org.apache.poi.poifs.crypt.dsig.SignatureConfig)1 SignatureInfo (org.apache.poi.poifs.crypt.dsig.SignatureInfo)1 SignaturePart (org.apache.poi.poifs.crypt.dsig.SignatureInfo.SignaturePart)1 SignatureDocument (org.w3.x2000.x09.xmldsig.SignatureDocument)1 Document (org.w3c.dom.Document)1