CertificateHolder Class

Represents a holder of X509Certificate2 instance.

ExpandedInheritance Hierarchy

SystemObject
  Aspose.WordsCertificateHolder

Namespace:  Aspose.Words
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.10.0

ExpandedSyntax

public class CertificateHolder

The CertificateHolder type exposes the following members.

ExpandedProperties

  NameDescription
Public propertyCode exampleCertificate
Returns the instance of X509Certificate2 which holds private, public keys and certificate chain.

ExpandedMethods

  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.)

ExpandedRemarks

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.

ExpandedExamples

Shows how to sign encrypted document file.
// Create an X.509 certificate from a PKCS#12 store, which should contain a private key.
CertificateHolder certificateHolder = CertificateHolder.Create(MyDir + "morzal.pfx", "aw");

// Create a comment, date, and decryption password which will be applied with our new digital signature.
SignOptions signOptions = new SignOptions
{
    Comments = "Comment",
    SignTime = DateTime.Now,
    DecryptionPassword = "docPassword"
};

// Set a local system filename for the unsigned input document, and an output filename for its new digitally signed copy.
string inputFileName = MyDir + "Encrypted.docx";
string outputFileName = ArtifactsDir + "DigitalSignatureUtil.DecryptionPassword.docx";

DigitalSignatureUtil.Sign(inputFileName, outputFileName, certificateHolder, signOptions);
Shows how to digitally sign documents.
// Create an X.509 certificate from a PKCS#12 store, which should contain a private key.
CertificateHolder certificateHolder = CertificateHolder.Create(MyDir + "morzal.pfx", "aw");

// Create a comment and date which will be applied with our new digital signature.
SignOptions signOptions = new SignOptions
{
    Comments = "My comment", 
    SignTime = DateTime.Now
};

// Take an unsigned document from the local file system via a file stream,
// then create a signed copy of it determined by the filename of the output file stream.
using (Stream streamIn = new FileStream(MyDir + "Document.docx", FileMode.Open))
{
    using (Stream streamOut = new FileStream(ArtifactsDir + "DigitalSignatureUtil.SignDocument.docx", FileMode.OpenOrCreate))
    {
        DigitalSignatureUtil.Sign(streamIn, streamOut, certificateHolder, signOptions);
    }
}
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 NET462 || JAVA
/// <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 NET462 || JAVA
        new SignPersonTestClass(Guid.NewGuid(), "Ron Williams", "Chief Executive Officer",
            ImageToByteArray(Image.FromFile(ImageDir + "Logo.jpg"))),
        #elif NETCOREAPP2_1 || __MOBILE__
        new SignPersonTestClass(Guid.NewGuid(), "Ron Williams", "Chief Executive Officer", 
            SkiaSharp.SKBitmap.Decode(ImageDir + "Logo.jpg").Bytes),
        #endif

        #if NET462 || JAVA
        new SignPersonTestClass(Guid.NewGuid(), "Stephen Morse", "Head of Compliance",
            ImageToByteArray(Image.FromFile(ImageDir + "Logo.jpg")))
        #elif NETCOREAPP2_1 || __MOBILE__
        new SignPersonTestClass(Guid.NewGuid(), "Stephen Morse", "Head of Compliance", 
            SkiaSharp.SKBitmap.Decode(ImageDir + "Logo.jpg").Bytes)
        #endif
    };
}

private static List<SignPersonTestClass> gSignPersonList;

ExpandedSee Also