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

Verify DKIM using a public key supplied as text

See more DKIM Examples

Applications that keep public keys in configuration can pass a complete PEM string or the base64 value of the DNS p= tag to LoadPublicKey. This example reads a trusted PEM as text so it can be run alongside the file-based example. Supply just the key material when passing a base64 value. Reuse the same verifier for signatures with that exact domain and selector.

Chilkat Objective-C Downloads

Objective-C
#import <CkoBinData.h>
#import <CkoDkim.h>
#import <NSString.h>
#import <CkoStringBuilder.h>
#import <CkoJsonObject.h>

BOOL success = NO;

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

CkoBinData *message = [[CkoBinData alloc] init];
success = [message LoadFile: @"signed.eml"];
if (success != YES) {
    NSLog(@"%@",@"Could not read signed.eml.");
    return;
}

CkoDkim *verifier = [[CkoDkim alloc] init];
NSString *domain = @"example.com";
NSString *selector = @"mail2026";
CkoStringBuilder *keyText = [[CkoStringBuilder alloc] init];
success = [keyText LoadFile: @"dkim-public.pem" charset: @"utf-8"];
if (success != YES) {
    NSLog(@"%@",@"Could not read the trusted public PEM.");
    return;
}

success = [verifier LoadPublicKey: selector domain: domain publicKey: [keyText GetAsString]];
if (success != YES) {
    NSLog(@"%@",verifier.LastErrorText);
    return;
}

//  signed.eml from the signing example has our newly added signature at index zero.
if ([[verifier NumDkimSigs: message] intValue] == 0) {
    NSLog(@"%@",@"No DKIM signature in signed.eml.");
    return;
}

success = [verifier DkimVerify: [NSNumber numberWithInt: 0] mimeData: message];
if (success != YES) {
    NSLog(@"%@",verifier.LastErrorText);
    NSLog(@"%@",verifier.VerifyInfo);
    return;
}

CkoJsonObject *info = [[CkoJsonObject alloc] init];
success = [info Load: verifier.VerifyInfo];
if ((success != YES) || ([info StringOfEquals: @"domain" value: domain caseSensitive: YES] != YES) || ([info StringOfEquals: @"selector" value: selector caseSensitive: YES] != YES)) {
    NSLog(@"%@",@"The first signature is not from the selected domain and selector.");
    return;
}

NSLog(@"%@",@"The configured public key verifies this message.");