com.aspose.words

Class CertificateHolder

  • java.lang.Object
    • com.aspose.words.CertificateHolder
public class CertificateHolder 
extends java.lang.Object

Represents a holder of X509Certificate2 instance.

CertificateHolder can be created by static factory methods only. It contains an instance of X509Certificate2 which is used to introduce private, public keys and certificate chains into the system. This class is applied in DigitalSignatureUtil and PdfDigitalSignatureDetails instead of obsolete methods with X509Certificate2 as parameters.

Example:

Shows how to sign documents using certificate holder and sign options.
CertificateHolder certificateHolder = CertificateHolder.create(getMyDir() + "morzal.pfx", "aw");

// By string:
Document doc = new Document(getMyDir() + "Document.DigitalSignature.docx");
String outputFileName = getArtifactsDir() + "Document.DigitalSignature.docx";

SignOptions signOptions = new SignOptions();
signOptions.setComments("My comment");
signOptions.setSignTime(new Date());

DigitalSignatureUtil.sign(doc.getOriginalFileName(), outputFileName, certificateHolder, signOptions);

// By stream:
InputStream streamIn = new FileInputStream(getMyDir() + "Document.DigitalSignature.docx");
OutputStream streamOut = new FileOutputStream(getArtifactsDir() + "Document.DigitalSignature.docx");

DigitalSignatureUtil.sign(streamIn, streamOut, certificateHolder, signOptions);

Example:

Shows how to sign encrypted document opened from a file.
// Create certificate holder from a file.
CertificateHolder certificateHolder = CertificateHolder.create(getMyDir() + "morzal.pfx", "aw");

SignOptions signOptions = new SignOptions();
signOptions.setComments("Comment");
signOptions.setSignTime(new Date());
signOptions.setDecryptionPassword("docPassword");

// Digitally sign encrypted with "docPassword" document in the specified path.
String inputFileName = getMyDir() + "Document.Encrypted.docx";
String outputFileName = getArtifactsDir() + "Document.Encrypted.docx";

DigitalSignatureUtil.sign(inputFileName, outputFileName, certificateHolder, signOptions);

// Open encrypted document from a file.
LoadOptions loadOptions = new LoadOptions("docPassword");
Assert.assertEquals(loadOptions.getPassword(), signOptions.getDecryptionPassword());

Document signedDoc = new Document(outputFileName, loadOptions);

Example:

Demonstrates how to add new signature line to the document and sign it with personal signature using SignatureLineId.
public static void signSignatureLineUsingSignatureLineId() throws Exception {
    String signPersonName = "Ron Williams";
    String srcDocumentPath = getMyDir() + "Document.docx";
    String dstDocumentPath = getArtifactsDir() + "Document.Signed.docx";
    String certificatePath = getMyDir() + "morzal.pfx";
    String certificatePassword = "aw";

    // We need to create simple list with test signers for this example.
    createSignPersonData();
    System.out.println("Test data successfully added!");

    // Get sign person object by name of the person who must sign a document.
    // This an example, in real use case you would return an object from a database.
    SignPersonTestClass signPersonInfo = gSignPersonList.stream().filter(x -> x.getName() == signPersonName).findFirst().get();

    if (signPersonInfo != null) {
        signDocument(srcDocumentPath, dstDocumentPath, signPersonInfo, certificatePath, certificatePassword);
        System.out.println("Document successfully signed!");
    } else {
        System.out.println("Sign person does not exist, please check your parameters.");
    }

    // Now do something with a signed document, for example, save it to your database.
    // Use 'new Document(dstDocumentPath)' for loading a signed document.
}

/// <summary>
/// Signs the document obtained at the source location and saves it to the specified destination.
/// </summary>
private static void signDocument(final String srcDocumentPath, final String dstDocumentPath,
                                 final SignPersonTestClass signPersonInfo, final String certificatePath,
                                 final String certificatePassword) throws Exception {
    // Create new document instance based on a test file that we need to sign.
    Document document = new Document(srcDocumentPath);
    DocumentBuilder builder = new DocumentBuilder(document);

    // Add info about responsible person who sign a document.
    SignatureLineOptions signatureLineOptions = new SignatureLineOptions();
    signatureLineOptions.setSigner(signPersonInfo.getName());
    signatureLineOptions.setSignerTitle(signPersonInfo.getPosition());

    // Add signature line for responsible person who sign a document.
    SignatureLine signatureLine = builder.insertSignatureLine(signatureLineOptions).getSignatureLine();
    signatureLine.setId(signPersonInfo.getPersonId());

    // Save a document with line signatures into temporary file for future signing.
    builder.getDocument().save(dstDocumentPath);

    // Create holder of certificate instance based on your personal certificate.
    // This is the test certificate generated for this example.
    CertificateHolder certificateHolder = CertificateHolder.create(certificatePath, certificatePassword);

    // Link our signature line with personal signature.
    SignOptions signOptions = new SignOptions();
    signOptions.setSignatureLineId(signPersonInfo.getPersonId());
    signOptions.setSignatureLineImage(signPersonInfo.getImage());

    // Sign a document which contains signature line with personal certificate.
    DigitalSignatureUtil.sign(dstDocumentPath, dstDocumentPath, certificateHolder, signOptions);
}

/// <summary>
/// Create test data that contains info about sing persons
/// </summary>
private static void createSignPersonData() throws IOException {
    InputStream inputStream = new FileInputStream(getImageDir() + "LogoSmall.png");

    gSignPersonList = new ArrayList<>();
    gSignPersonList.add(new SignPersonTestClass(UUID.randomUUID(), "Ron Williams", "Chief Executive Officer",
            DocumentHelper.getBytesFromStream(inputStream)));
    gSignPersonList.add(new SignPersonTestClass(UUID.randomUUID(), "Stephen Morse", "Head of Compliance",
            DocumentHelper.getBytesFromStream(inputStream)));
}

private static ArrayList<SignPersonTestClass> gSignPersonList;

Property Getters/Setters Summary
System.Security.Cryptography.X509Certificates.X509Certificate2getCertificate()
Returns the instance of X509Certificate2Wrapper that holds X509Certificate2 which holds private, public keys and certificate chain.
 
Method Summary
static CertificateHoldercreate(byte[] certBytes, java.lang.String password)
Creates CertificateHolder object using byte array of PKCS12 store and its password.
static CertificateHoldercreate(java.lang.String fileName, java.lang.String password)
Creates CertificateHolder object using path to PKCS12 store and its password.
static CertificateHoldercreate(java.lang.String fileName, java.lang.String password, java.lang.String alias)
Creates CertificateHolder object using path to PKCS12 store, its password and the alias by using which private key and certificate will be found.
 

    • Property Getters/Setters Detail

      • getCertificate

        public System.Security.Cryptography.X509Certificates.X509Certificate2 getCertificate()
        
        Returns the instance of X509Certificate2Wrapper that holds X509Certificate2 which holds private, public keys and certificate chain.

        Example:

        Shows how to validate each signature in a document and display basic information about the signature.
        // Load the document which contains signature.
        Document doc = new Document(getMyDir() + "Document.DigitalSignature.docx");
        
        for (DigitalSignature signature : doc.getDigitalSignatures()) {
            System.out.println("*** Signature Found ***");
            System.out.println("Is valid: " + signature.isValid());
            System.out.println("Reason for signing: " + signature.getComments()); // This property is available in MS Word documents only.
            System.out.println("Signature type: " + DigitalSignatureType.toString(signature.getSignatureType()));
            System.out.println("Time of signing: " + signature.getSignTime());
            System.out.println("Subject name: " + signature.getSubjectName());
            System.out.println("Issuer name: " + signature.getIssuerName());
            System.out.println("Certificate type: " + signature.getCertificateHolder().getCertificate().getClass().getName());
            System.out.println();
        }
        Returns:
        com.aspose.words.X509Certificate2Wrapper instance
    • Method Detail

      • create

        public static CertificateHolder create(byte[] certBytes, java.lang.String password)
                                            throws java.lang.Exception
        Creates CertificateHolder object using byte array of PKCS12 store and its password.
        Parameters:
        certBytes - A byte array that contains data from an X.509 certificate.
        password - The password required to access the X.509 certificate data.
        Returns:
        An instance of CertificateHolder
      • create

        public static CertificateHolder create(java.lang.String fileName, java.lang.String password)
                                            throws java.lang.Exception
        Creates CertificateHolder object using path to PKCS12 store and its password.
        Parameters:
        fileName - The name of a certificate file.
        password - The password required to access the X.509 certificate data.
        Returns:
        An instance of CertificateHolder

        Example:

        Shows how to sign documents using certificate holder and sign options.
        CertificateHolder certificateHolder = CertificateHolder.create(getMyDir() + "morzal.pfx", "aw");
        
        // By string:
        Document doc = new Document(getMyDir() + "Document.DigitalSignature.docx");
        String outputFileName = getArtifactsDir() + "Document.DigitalSignature.docx";
        
        SignOptions signOptions = new SignOptions();
        signOptions.setComments("My comment");
        signOptions.setSignTime(new Date());
        
        DigitalSignatureUtil.sign(doc.getOriginalFileName(), outputFileName, certificateHolder, signOptions);
        
        // By stream:
        InputStream streamIn = new FileInputStream(getMyDir() + "Document.DigitalSignature.docx");
        OutputStream streamOut = new FileOutputStream(getArtifactsDir() + "Document.DigitalSignature.docx");
        
        DigitalSignatureUtil.sign(streamIn, streamOut, certificateHolder, signOptions);
      • create

        public static CertificateHolder create(java.lang.String fileName, java.lang.String password, java.lang.String alias)
                                            throws java.lang.Exception
        Creates CertificateHolder object using path to PKCS12 store, its password and the alias by using which private key and certificate will be found.
        Parameters:
        fileName - The name of a certificate file.
        password - The password required to access the X.509 certificate data.
        alias - The associated alias for a certificate and its private key
        Returns:
        An instance of CertificateHolder