Objective-C
Objective-C
IMAP Download and Verify Signed MIME
See more IMAP Examples
Downloads the original MIME of a digitally signed email and saves the .p7s signature along with other MIME parts. It then imports the email into a Chilkat email object to unwrap the S/MIME and verify the signature, and subsequently saves the attachments if they haven't been saved already.Chilkat Objective-C Downloads
#import <CkoImap.h>
#import <CkoStringBuilder.h>
#import <CkoMime.h>
#import <CkoStringTable.h>
#import <CkoEmail.h>
#import <CkoCert.h>
BOOL success = NO;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
CkoImap *imap = [[CkoImap alloc] init];
// Connect to an IMAP server.
// Use TLS
imap.Ssl = YES;
imap.Port = [NSNumber numberWithInt:993];
success = [imap Connect: @"imap.example.com"];
if (success == NO) {
NSLog(@"%@",imap.LastErrorText);
return;
}
success = [imap Login: @"myLogin" password: @"myPassword"];
if (success == NO) {
NSLog(@"%@",imap.LastErrorText);
return;
}
// Select an IMAP mailbox
success = [imap SelectMailbox: @"Inbox"];
if (success == NO) {
NSLog(@"%@",imap.LastErrorText);
return;
}
// Download the 1st email (as MIME) in the Inbox by sequence number.
CkoStringBuilder *sbMime = [[CkoStringBuilder alloc] init];
success = [imap FetchSingleAsMimeSb: 1 bUid: NO sbMime: sbMime];
if (success == NO) {
NSLog(@"%@",imap.LastErrorText);
return;
}
// Load it into a MIME object and check to see if it is signed
CkoMime *mime = [[CkoMime alloc] init];
[mime LoadMimeSb: sbMime];
BOOL alreadySavedParts = NO;
if ([mime ContainsSignedParts] == YES) {
// This will save the .p7s and other parts...
CkoStringTable *st = [[CkoStringTable alloc] init];
success = [mime PartsToFiles: @"c:/temp/qa_output" st: st];
if (success == YES) {
int numFiles = [st.Count intValue];
int i = 0;
while (i < numFiles) {
NSLog(@"%@%@",@"Created: ",[st StringAt: [NSNumber numberWithInt: i]]);
i = i + 1;
}
alreadySavedParts = YES;
}
}
// Load the MIME into an Email object. This unwraps the security layers and verifies signatures.
CkoEmail *email = [[CkoEmail alloc] init];
[email SetFromMimeSb: sbMime];
if (email.ReceivedSigned == YES) {
NSLog(@"%@",@"This email was signed.");
// Check to see if the signatures were verified.
if (email.SignaturesValid == YES) {
NSLog(@"%@",@"Digital signature(s) verified.");
NSLog(@"%@%@",@"Signer: ",email.SignedBy);
// The certificate used for signing may be obtained
// by calling email.GetSignedByCert.
CkoCert *cert = [[CkoCert alloc] init];
success = [email LastSignerCert: [NSNumber numberWithInt: i] cert: cert];
if (success == NO) {
NSLog(@"%@",@"Failed to get signing certificate object.");
}
else {
NSLog(@"%@%@",@"Signing cert: ",cert.SubjectCN);
}
}
}
else {
NSLog(@"%@",@"Digital signature verification failed.");
}
if (alreadySavedParts != YES) {
[email SaveAllAttachments: @"c:/temp/qa_output"];
}