Sample code for 30+ languages & platforms
C

Create a DKIM-Signature for a MIME Message

See more DKIM / DomainKey Examples

Demonstrates the Chilkat Dkim.DkimSign method, which creates a DKIM-Signature header and prepends it to a complete MIME message held in a BinData, modifying it in place. The DkimAlg, DkimCanon, DkimDomain, DkimSelector, DkimHeaders, and DkimBodyLengthCount properties configure the generated signature.

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: The signature is computed over a hash of the body plus a chosen set of headers, so the message must be completely built — every header, encoding, and boundary — before signing; any later change invalidates it. The d= (domain) and s= (selector) tags tell a verifier where to find the public key: at selector._domainkey.domain in DNS. relaxed/relaxed canonicalization tolerates the minor whitespace changes mail systems make in transit, which is why it is the common choice.

Chilkat C Downloads

C
#include <C_CkDkim.h>
#include <C_CkPrivateKey.h>
#include <C_CkBinData.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkDkim dkim;
    HCkPrivateKey privKey;
    HCkBinData mimeData;

    success = FALSE;

    //  Demonstrates the Dkim.DkimSign method, which creates a DKIM-Signature header and prepends it to
    //  a complete MIME message.  The only argument is a BinData holding the MIME, which is modified in
    //  place.
    //  
    //  The DkimAlg, DkimCanon, DkimDomain, DkimSelector, DkimHeaders, and DkimBodyLengthCount
    //  properties configure the signature that is generated.

    dkim = CkDkim_Create();

    //  Configure the DKIM signature.
    CkDkim_putDkimDomain(dkim,"example.com");
    CkDkim_putDkimSelector(dkim,"myselector");

    //  Signing algorithm written to the a= tag.
    CkDkim_putDkimAlg(dkim,"rsa-sha256");

    //  Canonicalization for headers and body, written to the c= tag.
    CkDkim_putDkimCanon(dkim,"relaxed/relaxed");

    //  Colon-separated list of header fields to sign, written to the h= tag.
    CkDkim_putDkimHeaders(dkim,"From:To:Subject:Date:Message-ID");

    //  Maximum number of canonicalized body bytes to hash.  0 means the entire body.
    CkDkim_putDkimBodyLengthCount(dkim,0);

    //  Load the RSA private key and give it to the Dkim object.
    privKey = CkPrivateKey_Create();
    success = CkPrivateKey_LoadPemFile(privKey,"qa_data/dkim_private.pem");
    if (success == FALSE) {
        printf("%s\n",CkPrivateKey_lastErrorText(privKey));
        CkDkim_Dispose(dkim);
        CkPrivateKey_Dispose(privKey);
        return;
    }

    success = CkDkim_SetDkimPrivateKey(dkim,privKey);
    if (success == FALSE) {
        printf("%s\n",CkDkim_lastErrorText(dkim));
        CkDkim_Dispose(dkim);
        CkPrivateKey_Dispose(privKey);
        return;
    }

    //  Load the complete MIME message to be signed.  It must already contain all headers, transfer
    //  encodings, MIME boundaries, and body bytes.
    mimeData = CkBinData_Create();
    success = CkBinData_LoadFile(mimeData,"qa_data/message.eml");
    if (success == FALSE) {
        printf("%s\n",CkBinData_lastErrorText(mimeData));
        CkDkim_Dispose(dkim);
        CkPrivateKey_Dispose(privKey);
        CkBinData_Dispose(mimeData);
        return;
    }

    //  Sign.  The DKIM-Signature header is prepended to the MIME in place.
    success = CkDkim_DkimSign(dkim,mimeData);
    if (success == FALSE) {
        printf("%s\n",CkDkim_lastErrorText(dkim));
        CkDkim_Dispose(dkim);
        CkPrivateKey_Dispose(privKey);
        CkBinData_Dispose(mimeData);
        return;
    }

    //  Save the signed message.
    success = CkBinData_WriteFile(mimeData,"qa_output/signed.eml");
    if (success == FALSE) {
        printf("%s\n",CkBinData_lastErrorText(mimeData));
        CkDkim_Dispose(dkim);
        CkPrivateKey_Dispose(privKey);
        CkBinData_Dispose(mimeData);
        return;
    }

    printf("Message signed.\n");


    CkDkim_Dispose(dkim);
    CkPrivateKey_Dispose(privKey);
    CkBinData_Dispose(mimeData);

    }