« Xojo Developer Meetin… | Home | Xojo Developer Meetin… »

Sign PDF with DynaPDF and your custom signature

With the MBS Xojo DynaPDF Plugin, you can digitally sign PDF files. This works with 1024bit keys from PKCS#7 certificates. But DynaPDF does currently not support longer keys.

But well, we do have a MBS Xojo Encryption Plugin with an OpenSSLMBS module including signature functions. With the PKeyMBS and X509MBS classes we can read the PKCS#12 files, get certificates and private key and sign the SHA1 hash from the PDF data.

Here is an example to show you how we can do it:

// In this example we load a PFX certificate file const password = "123456" // read from file dim filePath as FolderItem = SpecialFolder.Desktop.Child("test.pfx") dim b as BinaryStream = BinaryStream.Open(filePath) dim certificateData as string = b.Read(b.Length) // load PKCS12 file to get private key dim cert as X509MBS dim certs() as X509MBS dim pkey as PKeyMBS if X509MBS.ReadFromPkcs12(certificateData, password, pkey, cert, certs) then // get private key as text dim PrivateKey as string = pkey.PrivateKeyData // sign something so we know length dim DummySignature as string = OpenSSLMBS.PKCS7SignData(cert, pkey, certs, SHA1MBS.Hash("test"), 0) // sign parameters dim p as new DynaPDFSigParmsMBS p.ContactInfo = "Call MBS for help." p.Reason = "Signed for demo purpose" p.Signer = "TestApp" p.HashType = DynaPDFMBS.khtSHA1 p.PKCS7ObjLen = lenb(DummySignature) // now assemble PDF and leave room for signature if pdf.CloseAndSignFileExt(p) then // sign the Hash dim Signature as string = OpenSSLMBS.PKCS7SignData(cert, pkey, certs, p.Range1, 0) // add hash to file and launch in PDF reader if pdf.FinishSignature(Signature) then f.Launch end if end if end if
The biggest plugin in space...
05 01 18 - 14:37