Objective-C Requires Chilkat v11.0.0+
Objective-C
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 Objective-C Downloads
#import <CkoImap.h>
#import <CkoMessageSet.h>
#import <CkoDkim.h>
#import <CkoBinData.h>
#import <CkoJsonObject.h>
BOOL success = NO;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
CkoImap *imap = [[CkoImap alloc] init];
imap.Ssl = YES;
imap.Port = [NSNumber numberWithInt:993];
imap.RequireSslCertVerify = YES;
imap.PeekMode = YES;
imap.AutoDownloadAttachments = YES;
success = [imap Connect: @"imap.example.com"];
if (success != YES) {
NSLog(@"%@",imap.LastErrorText);
return;
}
success = [imap Login: @"IMAP_USERNAME" password: @"IMAP_PASSWORD"];
if (success != YES) {
NSLog(@"%@",imap.LastErrorText);
return;
}
success = [imap SelectMailbox: @"INBOX"];
if (success != YES) {
NSLog(@"%@",imap.LastErrorText);
return;
}
// Select the first ten sequence positions, then fetch by stable UID.
CkoMessageSet *messages = [[CkoMessageSet alloc] init];
success = [imap QueryMbx: @"1:10" bUid: YES msgSet: messages];
if (success != YES) {
NSLog(@"%@",imap.LastErrorText);
return;
}
int i = 0;
CkoDkim *verifier = [[CkoDkim alloc] init];
CkoBinData *message = [[CkoBinData alloc] init];
while ((i < [messages.Count intValue])) {
unsigned long uid = [messages GetId: [NSNumber numberWithInt: i]];
NSLog(@"%@%d",@"Message UID ",uid);
success = [imap FetchSingleBd: uid bUid: YES mimeData: message];
if (success != YES) {
NSLog(@"%@",imap.LastErrorText);
}
else {
int count = [[verifier NumDkimSigs: message] intValue];
if (count == 0) {
NSLog(@"%@",@"No DKIM signatures.");
}
int sig = 0;
while ((sig < count)) {
success = [verifier DkimVerify: [NSNumber numberWithInt: sig] mimeData: message];
// Read diagnostics before the next verification replaces them.
CkoJsonObject *info = [[CkoJsonObject alloc] init];
BOOL haveInfo = [info Load: verifier.VerifyInfo];
if (success == YES) {
NSLog(@"%@%d%@",@"Signature ",sig,@" verified.");
}
else {
if ((haveInfo == YES) && ([info StringOfEquals: @"bodyHashVerified" value: @"no" caseSensitive: YES] == YES)) {
NSLog(@"%@%d%@",@"Signature ",sig,@": the body hash differs.");
}
else {
NSLog(@"%@%d%@",@"Signature ",sig,@" could not be verified.");
}
NSLog(@"%@",verifier.LastErrorText);
}
if ((haveInfo == YES) && ([[info JsonTypeOf: @"domain"] intValue] == 1)) {
NSLog(@"%@%@%@%@",@"Signing domain: ",[info StringOf: @"domain"],@" Selector: ",[info StringOf: @"selector"]);
}
sig = sig + 1;
}
}
i = i + 1;
}
success = [imap Disconnect];