Sample code for 30+ languages & platforms
PureBasic

Get the S/MIME Signer's Certificate

See more MIME Examples

Demonstrates the Chilkat Mime.LastSignerCert method, which loads the signer certificate at a zero-based index from the most recent successful verification (or security-unwrapping) into a Cert. The first argument is the signer index and the second is the receiving Cert.

Note: The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.

Background: Verifying tells you the signature is mathematically valid, but you usually also need to know who signed — the subject, the issuer, the validity dates — to decide whether to trust it. This retrieves each signer's certificate after verification so your code can inspect and apply its own trust rules. A message can have multiple signers, so iterate from 0 to NumSignerCerts - 1.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkCert.pb"
IncludeFile "CkMime.pb"

Procedure ChilkatExample()

    success.i = 0

    mime.i = CkMime::ckCreate()
    If mime.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkMime::ckLoadMimeFile(mime,"qa_data/signed.eml")
    If success = 0
        Debug CkMime::ckLastErrorText(mime)
        CkMime::ckDispose(mime)
        ProcedureReturn
    EndIf

    ;  After a successful verification, load each signer's certificate to inspect it.  The 1st argument
    ;  is the zero-based signer index and the 2nd is a Cert object that receives the certificate.
    success = CkMime::ckVerify(mime)
    If success <> 1
        Debug CkMime::ckLastErrorText(mime)
        CkMime::ckDispose(mime)
        ProcedureReturn
    EndIf

    n.i = CkMime::ckNumSignerCerts(mime)
    cert.i = CkCert::ckCreate()
    If cert.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    i.i
    For i = 0 To n - 1
        success = CkMime::ckLastSignerCert(mime,i,cert)
        If success = 0
            Debug CkMime::ckLastErrorText(mime)
            CkMime::ckDispose(mime)
            CkCert::ckDispose(cert)
            ProcedureReturn
        EndIf

        Debug "Signer " + Str(i) + ": " + CkCert::ckSubjectCN(cert) + " (issued by " + CkCert::ckIssuerCN(cert) + ")"
    Next


    CkMime::ckDispose(mime)
    CkCert::ckDispose(cert)


    ProcedureReturn
EndProcedure