Some test text!

Hamburger Icon

iOS / Guides

Custom Signing on iOS

The Apryse custom signing API is a set of APIs related to cryptographic digital signatures which allows users to customize the process of signing documents. Among other things, this includes the capability to allow for easy integration of PDF-specific signing-related operations with access to Hardware Security Module (HSM) tokens/devices, access to cloud keystores, access to system keystores, etc. The intent behind this API is to remove the old, tricky, and complicated requirement for users with specific needs to create custom SignatureHandler functor objects.

What follows is a simple code guide to the use of the custom signing API. Please note: any of the steps can be replaced with your own code that provides some custom functionality.

PTPDFDoc* doc = [[PTPDFDoc alloc] initWithFilepath: in_docpath];

PTPage* page1 = [doc GetPage: 1];

// Create a digital signature field and associated widget.
PTDigitalSignatureField* digsig_field = [doc CreateDigitalSignatureField: in_sig_field_name];
PTSignatureWidget* widgetAnnot = [PTSignatureWidget CreateWithDigitalSignatureField: doc pos: [[PTPDFRect alloc] initWithX1: 143 y1: 287 x2: 219 y2: 306] field: digsig_field];
[page1 AnnotPushBack: widgetAnnot];

// Create a digital signature dictionary inside the digital signature field, in preparation for signing.
// For security reasons, set the contents size to a value greater than but as close as possible to the size you expect your final signature to be, in bytes.
// ... or, if you want to apply a certification signature, use createSigDictForCustomCertification instead.
[digsig_field CreateSigDictForCustomSigning: @"Adobe.PPKLite" in_subfilter_type: e_ptadbe_pkcs7_detached in_contents_size_to_reserve: 7500];

// (OPTIONAL) Set the signing time in the signature dictionary, if no secure embedded timestamping support is available from your signing provider.
PTDate* current_date = [[PTDate alloc] init];
[current_date SetCurrentTime];
[digsig_field SetSigDictTimeOfSigning];

// Save the document incrementally to avoid invalidating any previous signatures.
[doc SaveToFile: in_outpath flags: e_ptincremental];

// Digest the relevant bytes of the document in accordance with ByteRanges surrounding the signature.
NSData* buf = pdf_digest = [digsig_field CalculateDigest: e_ptsha256];

PTX509Certificate* signer_cert = [[X509Certificate alloc] init: in_cert_path];

// Optionally, you can add a custom signed attribute at this point, such as one of the PAdES ESS attributes.
// The function we provide takes care of generating the correct PAdES ESS attribute depending on your digest algorithm.
NSDSata* pades_versioned_ess_signing_cert_attribute = [PTDigitalSignatureField GenerateESSSigningCertPAdESAttribute: signer_cert in_digest_algorithm_type: e_ptsha256];

// Generate the signedAttrs component of CMS, passing any optional custom signedAttrs (e.g. PAdES ESS). The signedAttrs are certain attributes that become protected by their inclusion in the signature.
NSData* signedAttrs = [PTDigitalSignatureField GenerateCMSSignedAttributes: pdf_digest in_custom_signedattributes_buf: pades_versioned_ess_signing_cert_attribute];

// Calculate the digest of the signedAttrs (i.e. not the PDF digest, this time).
NSData* signedAttrs_digest = [PTDigestAlgorithm CalculateDigest: e_ptsha256 in_message_buf: signedAttrs];

At this point, use your signing provider (e.g. HSM device, cloud keystore) to sign the digest of signedAttrs. Your input should be the variable signedAttrs_digest. In the following code, we assume the output is in a variable named signature_value.

// Then, load all your chain certificates into a container of X509Certificate.
NSData* chain_certs; // details omitted

// Then, create ObjectIdentifiers for the algorithms you have used.
PTObjectIdentifier* digest_algorithm_oid = [[PTObjectIdentifier alloc] init: e_SHA256];
PTObjectIdentifier* signature_algorithm_oid = [[PTObjectIdentifier alloc] init: e_ptRSA_encryption_PKCS1];

// Then, put the CMS signature components together.
NSData* cms_signature = [PTDigitalSignatureField GenerateCMSSignature: signer_cert in_chain_certs_list: chain_certs in_digest_algorithm_oid: digest_algorithm_oid in_signature_algorithm_oid: signature_algorithm_oid  in_signature_value_buf: signature_value in_signedattributes_buf: signedAttrs];

// Write the signature to the document.
[doc SaveCustomSignature: cms_signature in_field: digsig_field in_path: in_outpath];

Get the answers you need: Chat with us