| 
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.apache.poi.poifs.crypt.dsig.SignatureInfo
public class SignatureInfo
This class is the default entry point for XML signatures and can be used for validating an existing signed office document and signing a office document.
Validating a signed office document
OPCPackage pkg = OPCPackage.open(..., PackageAccess.READ); SignatureConfig sic = new SignatureConfig(); sic.setOpcPackage(pkg); SignatureInfo si = new SignatureInfo(); si.setSignatureConfig(sic); boolean isValid = si.validate(); ...
Signing an office document
 // loading the keystore - pkcs12 is used here, but of course jks & co are also valid
 // the keystore needs to contain a private key and it's certificate having a
 // 'digitalSignature' key usage
 char password[] = "test".toCharArray();
 File file = new File("test.pfx");
 KeyStore keystore = KeyStore.getInstance("PKCS12");
 FileInputStream fis = new FileInputStream(file);
 keystore.load(fis, password);
 fis.close();
 
 // extracting private key and certificate
 String alias = "xyz"; // alias of the keystore entry
 Key key = keystore.getKey(alias, password);
 X509Certificate x509 = (X509Certificate)keystore.getCertificate(alias);
 
 // filling the SignatureConfig entries (minimum fields, more options are available ...)
 SignatureConfig signatureConfig = new SignatureConfig();
 signatureConfig.setKey(keyPair.getPrivate());
 signatureConfig.setSigningCertificateChain(Collections.singletonList(x509));
 OPCPackage pkg = OPCPackage.open(..., PackageAccess.READ_WRITE);
 signatureConfig.setOpcPackage(pkg);
 
 // adding the signature document to the package
 SignatureInfo si = new SignatureInfo();
 si.setSignatureConfig(signatureConfig);
 si.confirmSignature();
 // optionally verify the generated signature
 boolean b = si.verifySignature();
 assert (b);
 // write the changes back to disc
 pkg.close();
 
 
 Implementation notes:
Although there's a XML signature implementation in the Oracle JDKs 6 and higher, compatibility with IBM JDKs is also in focus (... but maybe not thoroughly tested ...). Therefore we are using the Apache Santuario libs (xmlsec) instead of the built-in classes, as the compatibility seems to be provided there.
To use SignatureInfo and its sibling classes, you'll need to have the following libs in the classpath:
| Nested Class Summary | |
|---|---|
 class | 
SignatureInfo.SignaturePart
 | 
| Constructor Summary | |
|---|---|
SignatureInfo()
Constructor initializes xml signature environment, if it hasn't been initialized before  | 
|
| Method Summary | |
|---|---|
 void | 
confirmSignature()
add the xml signature to the document  | 
 SignatureConfig | 
getSignatureConfig()
 | 
 java.lang.Iterable<SignatureInfo.SignaturePart> | 
getSignatureParts()
 | 
protected static void | 
initXmlProvider()
Initialize the xml signing environment and the bouncycastle provider  | 
 void | 
postSign(org.w3c.dom.Document document,
         byte[] signatureValue)
Helper method for adding informations after the signing.  | 
 DigestInfo | 
preSign(org.w3c.dom.Document document,
        java.util.List<DigestInfo> digestInfos)
Helper method for adding informations before the signing.  | 
 void | 
setSignatureConfig(SignatureConfig signatureConfig)
 | 
 byte[] | 
signDigest(byte[] digest)
Sign (encrypt) the digest with the private key.  | 
 boolean | 
verifySignature()
 | 
protected  void | 
writeDocument(org.w3c.dom.Document document)
Write XML signature into the OPC package  | 
| Methods inherited from class java.lang.Object | 
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait | 
| Constructor Detail | 
|---|
public SignatureInfo()
| Method Detail | 
|---|
public SignatureConfig getSignatureConfig()
public void setSignatureConfig(SignatureConfig signatureConfig)
setSignatureConfig in interface SignatureConfig.SignatureConfigurablesignatureConfig - the signature config, needs to be set before a SignatureInfo object is usedpublic boolean verifySignature()
public void confirmSignature()
                      throws javax.xml.crypto.dsig.XMLSignatureException,
                             javax.xml.crypto.MarshalException
javax.xml.crypto.dsig.XMLSignatureException
javax.xml.crypto.MarshalExceptionpublic byte[] signDigest(byte[] digest)
digest - the hashed input
public java.lang.Iterable<SignatureInfo.SignaturePart> getSignatureParts()
protected static void initXmlProvider()
public DigestInfo preSign(org.w3c.dom.Document document,
                          java.util.List<DigestInfo> digestInfos)
                   throws javax.xml.crypto.dsig.XMLSignatureException,
                          javax.xml.crypto.MarshalException
confirmSignature() is sufficient to be used.
javax.xml.crypto.dsig.XMLSignatureException
javax.xml.crypto.MarshalException
public void postSign(org.w3c.dom.Document document,
                     byte[] signatureValue)
              throws javax.xml.crypto.MarshalException
confirmSignature() is sufficient to be used.
javax.xml.crypto.MarshalException
protected void writeDocument(org.w3c.dom.Document document)
                      throws javax.xml.crypto.MarshalException
document - the xml signature document
javax.xml.crypto.MarshalException
  | 
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||