Sample code for 30+ languages & platforms
PureBasic Requires Chilkat v11.0.0+

Verify a signed message with a trusted public key file

See more DKIM Examples

Use the saved signed message and its matching public PEM file to test a signing configuration before publishing DNS. This verifier caches the key for the exact selector and domain. A different pair can trigger normal DNS lookup, so the example also checks which signature actually verified.

Load the key from a trusted source independent of the received message. A pass with a locally supplied key confirms the cryptographic match. Use the published-key check to test what recipients can retrieve.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkBinData.pb"
IncludeFile "CkDkim.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Assumes Chilkat is unlocked. See Global Unlock Sample.

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

    success = CkBinData::ckLoadFile(message,"signed.eml")
    If success <> 1
        Debug "Could not read signed.eml."
        CkBinData::ckDispose(message)
        ProcedureReturn
    EndIf

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

    domain.s = "example.com"
    selector.s = "mail2026"
    success = CkDkim::ckLoadPublicKeyFile(verifier,selector,domain,"dkim-public.pem")
    If success <> 1
        Debug CkDkim::ckLastErrorText(verifier)
        CkBinData::ckDispose(message)
        CkDkim::ckDispose(verifier)
        ProcedureReturn
    EndIf

    ;  signed.eml from the signing example has our newly added signature at index zero.
    If CkDkim::ckNumDkimSigs(verifier,message) = 0
        Debug "No DKIM signature in signed.eml."
        CkBinData::ckDispose(message)
        CkDkim::ckDispose(verifier)
        ProcedureReturn
    EndIf

    success = CkDkim::ckDkimVerify(verifier,0,message)
    If success <> 1
        Debug CkDkim::ckLastErrorText(verifier)
        Debug CkDkim::ckVerifyInfo(verifier)
        CkBinData::ckDispose(message)
        CkDkim::ckDispose(verifier)
        ProcedureReturn
    EndIf

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

    success = CkJsonObject::ckLoad(info,CkDkim::ckVerifyInfo(verifier))
    If (success <> 1) OR (CkJsonObject::ckStringOfEquals(info,"domain",domain,1) <> 1) OR (CkJsonObject::ckStringOfEquals(info,"selector",selector,1) <> 1)
        Debug "The first signature is not from the selected domain and selector."
        CkBinData::ckDispose(message)
        CkDkim::ckDispose(verifier)
        CkJsonObject::ckDispose(info)
        ProcedureReturn
    EndIf

    Debug "Signature verified with the saved key for " + selector + "._domainkey." + domain


    CkBinData::ckDispose(message)
    CkDkim::ckDispose(verifier)
    CkJsonObject::ckDispose(info)


    ProcedureReturn
EndProcedure