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

Check a published DKIM selector against a signed message

See more DKIM Examples

After publishing a selector, run this check with a message signed by its matching private key. Start with a fresh verifier, fetch the public key from DNS, and then verify the message with that same object. DNS lookup alone shows only that a usable key was returned. Signature verification also checks that it matches what the signer used.

Set the signing domain and selector exactly as they appear in the signature. Applications processing many messages can reuse the verifier for that selector. After a key change, use a new verifier and allow for the DNS resolver’s TTL.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Message
oleobject loo_Verifier
string ls_Domain
string ls_Selector
oleobject loo_Info

li_Success = 0

//  Assumes Chilkat is unlocked. See Global Unlock Sample.

loo_Message = create oleobject
li_rc = loo_Message.ConnectToNewObject("Chilkat.BinData")
if li_rc < 0 then
    destroy loo_Message
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_Success = loo_Message.LoadFile("signed.eml")
if li_Success <> 1 then
    Write-Debug "Could not read signed.eml."
    destroy loo_Message
    return
end if

loo_Verifier = create oleobject
li_rc = loo_Verifier.ConnectToNewObject("Chilkat.Dkim")

ls_Domain = "example.com"
ls_Selector = "mail2026"
li_Success = loo_Verifier.PrefetchPublicKey(ls_Selector,ls_Domain)
if li_Success <> 1 then
    Write-Debug loo_Verifier.LastErrorText
    destroy loo_Message
    destroy loo_Verifier
    return
end if

//  signed.eml from the signing example has our newly added signature at index zero.
if loo_Verifier.NumDkimSigs(loo_Message) = 0 then
    Write-Debug "No DKIM signature in signed.eml."
    destroy loo_Message
    destroy loo_Verifier
    return
end if

li_Success = loo_Verifier.DkimVerify(0,loo_Message)
if li_Success <> 1 then
    Write-Debug loo_Verifier.LastErrorText
    Write-Debug loo_Verifier.VerifyInfo
    destroy loo_Message
    destroy loo_Verifier
    return
end if

loo_Info = create oleobject
li_rc = loo_Info.ConnectToNewObject("Chilkat.JsonObject")

li_Success = loo_Info.Load(loo_Verifier.VerifyInfo)
if (li_Success <> 1) OR (loo_Info.StringOfEquals("domain",ls_Domain,1) <> 1) OR (loo_Info.StringOfEquals("selector",ls_Selector,1) <> 1) then
    Write-Debug "The first signature is not from the selected domain and selector."
    destroy loo_Message
    destroy loo_Verifier
    destroy loo_Info
    return
end if

Write-Debug "The published key verifies this message for " + ls_Selector + "._domainkey." + ls_Domain


destroy loo_Message
destroy loo_Verifier
destroy loo_Info