Sample code for 30+ languages & platforms
Xbase++

CoSign PKCS7/CMS Signed Data

See more Digital Signatures Examples

Demonstrates how to add a 2nd signature to a CMS SignedData. This is to add an additional signature, SignerInfo and certificate(s) to an existing CMS signed data.

In this example, we cosign an existing pdf.p7s

Note: The CoSign method is added in Chilkat v9.5.0.89.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oCrypt
LOCAL oCert
LOCAL oBd
LOCAL oBd2

nSuccess := 0

//  This example assumes the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

oCrypt := CreateObject("Chilkat.Crypt2")
oCert := CreateObject("Chilkat.Cert")

//  If loading from a smart card, set the smartcard PIN.
oCert:SmartCardPin := "0000"

//  Load a certificate in some way, such as from a smart card.
//  Chilkat provides other methods to load from a .pfx, .pem, or from the Windows certificate stores..
nSuccess := oCert:LoadFromSmartcard("")
IF (nSuccess == 0)
    ? oCert:LastErrorText
    oCrypt:destroy()
    oCert:destroy()
    RETURN
ENDIF

nSuccess := oCrypt:SetSigningCert(oCert)
IF (nSuccess == 0)
    ? oCrypt:LastErrorText
    oCrypt:destroy()
    oCert:destroy()
    RETURN
ENDIF

//  Load the file to be co-signed.
//  NOTE:  This is to cosign a PDF file contained within a CMS (PKCS7) message.
//  (In other words, the PDF is contained within the CMS SignedData, rather than the other way around
//  where a CMS signature is contained within a PDF.  Use Chilkat's PDF class to sign a PDF, which is
//  to embed a CMS signature within the PDF.)
oBd := CreateObject("Chilkat.BinData")
nSuccess := oBd:LoadFile("qa_data/p7s/cosign/sample.pdf.p7s")
IF (nSuccess == 0)
    ? "Failed to load pdf.p7s input file."
    oCrypt:destroy()
    oCert:destroy()
    oBd:destroy()
    RETURN
ENDIF

//  bd2 will contain the co-signed result.
oBd2 := CreateObject("Chilkat.BinData")
nSuccess := oCrypt:CoSign(oBd, oCert, oBd2)
IF (nSuccess == 0)
    ? oCrypt:LastErrorText
    oCrypt:destroy()
    oCert:destroy()
    oBd:destroy()
    oBd2:destroy()
    RETURN
ENDIF

nSuccess := oBd:WriteFile("qa_output/cosigned.pdf.p7s")
IF (nSuccess == 0)
    ? "Failed to save pdf.p7s output file."
    oCrypt:destroy()
    oCert:destroy()
    oBd:destroy()
    oBd2:destroy()
    RETURN
ENDIF

? "Success!"

oCrypt:destroy()
oCert:destroy()
oBd:destroy()
oBd2:destroy()