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

Verify each DKIM signature in email downloaded over IMAP

See more DKIM Examples

Download the first ten messages (or all messages in a smaller inbox) as complete MIME, including attachments, while preserving their unread flags. The verifier looks up the selector’s TXT record when it needs a key. Inspect VerifyInfo immediately after each attempt for the signing domain, selector, and any body-hash mismatch.

Verify each signature independently, in top-to-bottom order. A passing signature authenticates the signing domain’s responsibility for the signed content. Identifying the author and checking DMARC alignment with the visible From address are separate steps. A missing DNS key or unavailable resolver can also prevent verification. DKIM specification.

Chilkat C Downloads

C
#include <C_CkImap.h>
#include <C_CkMessageSet.h>
#include <C_CkDkim.h>
#include <C_CkBinData.h>
#include <C_CkJsonObject.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkImap imap;
    HCkMessageSet messages;
    int i;
    HCkDkim verifier;
    HCkBinData message;
    unsigned long uid;
    int count;
    int sig;
    HCkJsonObject info;
    BOOL haveInfo;

    success = FALSE;

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

    imap = CkImap_Create();
    CkImap_putSsl(imap,TRUE);
    CkImap_putPort(imap,993);
    CkImap_putRequireSslCertVerify(imap,TRUE);
    CkImap_putPeekMode(imap,TRUE);
    CkImap_putAutoDownloadAttachments(imap,TRUE);
    success = CkImap_Connect(imap,"imap.example.com");
    if (success != TRUE) {
        printf("%s\n",CkImap_lastErrorText(imap));
        CkImap_Dispose(imap);
        return;
    }

    success = CkImap_Login(imap,"IMAP_USERNAME","IMAP_PASSWORD");
    if (success != TRUE) {
        printf("%s\n",CkImap_lastErrorText(imap));
        CkImap_Dispose(imap);
        return;
    }

    success = CkImap_SelectMailbox(imap,"INBOX");
    if (success != TRUE) {
        printf("%s\n",CkImap_lastErrorText(imap));
        CkImap_Dispose(imap);
        return;
    }

    //  Select the first ten sequence positions, then fetch by stable UID.
    messages = CkMessageSet_Create();
    success = CkImap_QueryMbx(imap,"1:10",TRUE,messages);
    if (success != TRUE) {
        printf("%s\n",CkImap_lastErrorText(imap));
        CkImap_Dispose(imap);
        CkMessageSet_Dispose(messages);
        return;
    }

    i = 0;
    verifier = CkDkim_Create();
    message = CkBinData_Create();
    while ((i < CkMessageSet_getCount(messages))) {
        uid = CkMessageSet_GetId(messages,i);
        printf("Message UID %u\n",uid);
        success = CkImap_FetchSingleBd(imap,uid,TRUE,message);
        if (success != TRUE) {
            printf("%s\n",CkImap_lastErrorText(imap));
        }
        else {
            count = CkDkim_NumDkimSigs(verifier,message);
            if (count == 0) {
                printf("No DKIM signatures.\n");
            }

            sig = 0;
            while ((sig < count)) {
                success = CkDkim_DkimVerify(verifier,sig,message);
                //  Read diagnostics before the next verification replaces them.
                info = CkJsonObject_Create();
                haveInfo = CkJsonObject_Load(info,CkDkim_verifyInfo(verifier));
                if (success == TRUE) {
                    printf("Signature %d verified.\n",sig);
                }
                else {
                    if ((haveInfo == TRUE) && (CkJsonObject_StringOfEquals(info,"bodyHashVerified","no",TRUE) == TRUE)) {
                        printf("Signature %d: the body hash differs.\n",sig);
                    }
                    else {
                        printf("Signature %d could not be verified.\n",sig);
                    }

                    printf("%s\n",CkDkim_lastErrorText(verifier));
                }

                if ((haveInfo == TRUE) && (CkJsonObject_JsonTypeOf(info,"domain") == 1)) {
                    printf("Signing domain: %s  Selector: %s\n",CkJsonObject_stringOf(info,"domain"),CkJsonObject_stringOf(info,"selector"));
                }

                sig = sig + 1;
            }

        }

        i = i + 1;
    }

    success = CkImap_Disconnect(imap);


    CkImap_Dispose(imap);
    CkMessageSet_Dispose(messages);
    CkDkim_Dispose(verifier);
    CkBinData_Dispose(message);
    CkJsonObject_Dispose(info);

    }