C
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 C Downloads
#include <C_CkImap.h>
#include <C_CkStringBuilder.h>
#include <C_CkMime.h>
#include <C_CkStringTable.h>
#include <C_CkEmail.h>
#include <C_CkCert.h>
void ChilkatSample(void)
{
BOOL success;
HCkImap imap;
HCkStringBuilder sbMime;
HCkMime mime;
BOOL alreadySavedParts;
HCkStringTable st;
int numFiles;
int i;
HCkEmail email;
HCkCert cert;
success = FALSE;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
imap = CkImap_Create();
// Connect to an IMAP server.
// Use TLS
CkImap_putSsl(imap,TRUE);
CkImap_putPort(imap,993);
success = CkImap_Connect(imap,"imap.example.com");
if (success == FALSE) {
printf("%s\n",CkImap_lastErrorText(imap));
CkImap_Dispose(imap);
return;
}
success = CkImap_Login(imap,"myLogin","myPassword");
if (success == FALSE) {
printf("%s\n",CkImap_lastErrorText(imap));
CkImap_Dispose(imap);
return;
}
// Select an IMAP mailbox
success = CkImap_SelectMailbox(imap,"Inbox");
if (success == FALSE) {
printf("%s\n",CkImap_lastErrorText(imap));
CkImap_Dispose(imap);
return;
}
// Download the 1st email (as MIME) in the Inbox by sequence number.
sbMime = CkStringBuilder_Create();
success = CkImap_FetchSingleAsMimeSb(imap,1,FALSE,sbMime);
if (success == FALSE) {
printf("%s\n",CkImap_lastErrorText(imap));
CkImap_Dispose(imap);
CkStringBuilder_Dispose(sbMime);
return;
}
// Load it into a MIME object and check to see if it is signed
mime = CkMime_Create();
CkMime_LoadMimeSb(mime,sbMime);
alreadySavedParts = FALSE;
if (CkMime_ContainsSignedParts(mime) == TRUE) {
// This will save the .p7s and other parts...
st = CkStringTable_Create();
success = CkMime_PartsToFiles(mime,"c:/temp/qa_output",st);
if (success == TRUE) {
numFiles = CkStringTable_getCount(st);
i = 0;
while (i < numFiles) {
printf("Created: %s\n",CkStringTable_stringAt(st,i));
i = i + 1;
}
alreadySavedParts = TRUE;
}
}
// Load the MIME into an Email object. This unwraps the security layers and verifies signatures.
email = CkEmail_Create();
CkEmail_SetFromMimeSb(email,sbMime);
if (CkEmail_getReceivedSigned(email) == TRUE) {
printf("This email was signed.\n");
// Check to see if the signatures were verified.
if (CkEmail_getSignaturesValid(email) == TRUE) {
printf("Digital signature(s) verified.\n");
printf("Signer: %s\n",CkEmail_signedBy(email));
// The certificate used for signing may be obtained
// by calling email.GetSignedByCert.
cert = CkCert_Create();
success = CkEmail_LastSignerCert(email,i,cert);
if (success == FALSE) {
printf("Failed to get signing certificate object.\n");
}
else {
printf("Signing cert: %s\n",CkCert_subjectCN(cert));
}
}
}
else {
printf("Digital signature verification failed.\n");
}
if (alreadySavedParts != TRUE) {
CkEmail_SaveAllAttachments(email,"c:/temp/qa_output");
}
CkImap_Dispose(imap);
CkStringBuilder_Dispose(sbMime);
CkMime_Dispose(mime);
CkStringTable_Dispose(st);
CkEmail_Dispose(email);
CkCert_Dispose(cert);
}