CertificateHolder Class

Represents a holder of X509Certificate2 instance.
Inheritance Hierarchy
SystemObject
  Aspose.WordsCertificateHolder

Namespace:  Aspose.Words
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.3
Syntax
public class CertificateHolder

The CertificateHolder type exposes the following members.

Properties
  NameDescription
Public propertyCode exampleCertificate
Returns the instance of X509Certificate2 which holds private, public keys and certificate chain.
Methods
  NameDescription
Public methodStatic memberCode exampleCreate(Byte, SecureString)
Creates CertificateHolder object using byte array of PKCS12 store and its password.
Public methodStatic memberCode exampleCreate(Byte, String)
Creates CertificateHolder object using byte array of PKCS12 store and its password.
Public methodStatic memberCode exampleCreate(String, String)
Creates CertificateHolder object using path to PKCS12 store and its password.
Public methodStatic memberCode exampleCreate(String, String, String)
Creates CertificateHolder object using path to PKCS12 store, its password and the alias by using which private key and certificate will be found.
Public methodEquals (Inherited from Object.)
Public methodGetHashCode (Inherited from Object.)
Public methodGetType (Inherited from Object.)
Public methodToString (Inherited from Object.)
Remarks

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.

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

SignOptions signOptions = new SignOptions { Comments = "My comment", SignTime = DateTime.Now };

using (Stream streamIn = new FileStream(MyDir + "Digitally signed.docx", FileMode.Open))
{
    using (Stream streamOut = new FileStream(ArtifactsDir + "DigitalSignatureUtil.SignDocument.docx", FileMode.OpenOrCreate))
    {
        DigitalSignatureUtil.Sign(streamIn, streamOut, certificateHolder, signOptions);
    }
}
Examples
Shows how to sign encrypted document file.
// Create certificate holder from a file
CertificateHolder certificateHolder = CertificateHolder.Create(MyDir + "morzal.pfx", "aw");

SignOptions signOptions = new SignOptions
{
    Comments = "Comment",
    SignTime = DateTime.Now,
    DecryptionPassword = "docPassword"
};

// Digitally sign encrypted with "docPassword" document in the specified path
string inputFileName = MyDir + "Encrypted.docx";
string outputFileName = ArtifactsDir + "DigitalSignatureUtil.DecryptionPassword.docx";

DigitalSignatureUtil.Sign(inputFileName, outputFileName, certificateHolder, signOptions);
Examples
Demonstrates how to add new signature line to the document and sign it with personal signature using SignatureLineId.
[Description("WORDSNET-16868")]
public static void Sign()
{
    string signPersonName = "Ron Williams";
    string srcDocumentPath = MyDir + "Document.docx";
    string dstDocumentPath = ArtifactsDir + "SignDocumentCustom.Sign.docx";
    string certificatePath = MyDir + "morzal.pfx";
    string certificatePassword = "aw";

    // We need to create simple list with test signers for this example
    CreateSignPersonData();
    Console.WriteLine("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 =
        (from c in gSignPersonList where c.Name == signPersonName select c).FirstOrDefault();

    if (signPersonInfo != null)
    {
        SignDocument(srcDocumentPath, dstDocumentPath, signPersonInfo, certificatePath, certificatePassword);
        Console.WriteLine("Document successfully signed!");
    }
    else
    {
        Console.WriteLine("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(string srcDocumentPath, string dstDocumentPath,
    SignPersonTestClass signPersonInfo, string certificatePath, string certificatePassword)
{
    // 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 { Signer = signPersonInfo.Name, SignerTitle = signPersonInfo.Position };

    // Add signature line for responsible person who sign a document
    SignatureLine signatureLine = builder.InsertSignatureLine(signatureLineOptions).SignatureLine;
    signatureLine.Id = signPersonInfo.PersonId;

    // Save a document with line signatures into temporary file for future signing
    builder.Document.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
    {
        SignatureLineId = signPersonInfo.PersonId,
        SignatureLineImage = signPersonInfo.Image
    };

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

#if NETFRAMEWORK
/// <summary>
/// Converting image file to bytes array.
/// </summary>
private static byte[] ImageToByteArray(Image imageIn)
{
    using (MemoryStream ms = new MemoryStream())
    {
        imageIn.Save(ms, ImageFormat.Png);
        return ms.ToArray();
    }
}
#endif

/// <summary>
/// Create test data that contains info about sing persons
/// </summary>
private static void CreateSignPersonData()
{
    gSignPersonList = new List<SignPersonTestClass>
    {
        #if NETFRAMEWORK
        new SignPersonTestClass(Guid.NewGuid(), "Ron Williams", "Chief Executive Officer",
            ImageToByteArray(Image.FromFile(ImageDir + "Logo.jpg"))),
        #else
        new SignPersonTestClass(Guid.NewGuid(), "Ron Williams", "Chief Executive Officer", 
            SkiaSharp.SKBitmap.Decode(ImageDir + "Logo.jpg").Bytes),
        #endif

        #if NETFRAMEWORK
        new SignPersonTestClass(Guid.NewGuid(), "Stephen Morse", "Head of Compliance",
            ImageToByteArray(Image.FromFile(ImageDir + "Logo.jpg")))
        #else
        new SignPersonTestClass(Guid.NewGuid(), "Stephen Morse", "Head of Compliance", 
            SkiaSharp.SKBitmap.Decode(ImageDir + "Logo.jpg").Bytes)
        #endif
    };
}

private static List<SignPersonTestClass> gSignPersonList;
See Also