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

Decrypt an S/MIME email and verify its signature

See more Email Object Examples

Register the recipient’s certificate and private key (from a PEM file) before opening signed-encrypted.eml. This workflow expects both encryption and a signature, so it checks both layers before reading the message. Cryptographic signature validity is one input to a signer-trust decision.

Chilkat Objective-C Downloads

Objective-C
#import <CkoEmail.h>
#import <CkoPem.h>
#import <CkoCert.h>

BOOL success = NO;

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

CkoEmail *email = [[CkoEmail alloc] init];
//  The recipient's certificate and private key are in a PEM file.  The 2nd argument of LoadPemFile
//  is the password of the private key.
CkoPem *pem = [[CkoPem alloc] init];
success = [pem LoadPemFile: @"recipient.pem" password: @"RECIPIENT_PASSWORD"];
if (success != YES) {
    NSLog(@"%@",pem.LastErrorText);
    return;
}

success = [email AddPemSource: pem];
if (success != YES) {
    NSLog(@"%@",email.LastErrorText);
    return;
}

success = [email LoadEml: @"signed-encrypted.eml"];
if (success != YES) {
    NSLog(@"%@",email.LastErrorText);
    return;
}

if ((email.ReceivedEncrypted != YES) || (email.Decrypted != YES)) {
    NSLog(@"%@",@"Expected a successfully decrypted S/MIME envelope.");
    return;
}

if ((email.ReceivedSigned != YES) || (email.SignaturesValid != YES)) {
    NSLog(@"%@",@"Expected a valid signature inside the envelope.");
    return;
}

//  This message has one signer. Examine its certificate after verification.
CkoCert *signer = [[CkoCert alloc] init];
success = [email LastSignerCert: [NSNumber numberWithInt: 0] cert: signer];
if (success != YES) {
    NSLog(@"%@",email.LastErrorText);
    return;
}

NSLog(@"%@%@",@"Signer certificate: ",signer.SubjectDN);
NSLog(@"%@%@",@"Recovered subject: ",email.Subject);
NSLog(@"%@%d",@"Recovered attachments: ",[email.NumAttachments intValue]);