Unicode C
Unicode C
Set Cert and Private Key for S/MIME Decryption
See more IMAP Examples
Demonstrates the Chilkat Imap.SetDecryptCert2 method, which specifies a certificate together with a separately supplied private key for decrypting S/MIME messages. The first argument is the Cert and the second is the PrivateKey. This example loads the certificate and key from separate files.
Note: The certificate path is relative to the application's current working directory. Supply the path to your own certificate file.
Background: Sometimes the certificate (the public part) and the private key are stored separately — a
.cer alongside a PEM key file, for instance. This method pairs them explicitly, whereas SetDecryptCert expects a single Cert that already provides its private key. After the pairing, subsequently downloaded S/MIME messages are decrypted automatically.Chilkat Unicode C Downloads
#include <C_CkImapW.h>
#include <C_CkCertW.h>
#include <C_CkPrivateKeyW.h>
void ChilkatSample(void)
{
BOOL success;
HCkImapW imap;
HCkCertW cert;
HCkPrivateKeyW privKey;
success = FALSE;
// Demonstrates the Imap.SetDecryptCert2 method, which specifies a certificate together with a
// separately supplied private key for decrypting S/MIME messages. The 1st argument is the
// Cert and the 2nd is the PrivateKey.
imap = CkImapW_Create();
CkImapW_putSsl(imap,TRUE);
CkImapW_putPort(imap,993);
success = CkImapW_Connect(imap,L"imap.example.com");
if (success == FALSE) {
wprintf(L"%s\n",CkImapW_lastErrorText(imap));
CkImapW_Dispose(imap);
return;
}
success = CkImapW_Login(imap,L"user@example.com",L"myPassword");
if (success == FALSE) {
wprintf(L"%s\n",CkImapW_lastErrorText(imap));
CkImapW_Dispose(imap);
return;
}
// Load the certificate and its private key from separate files.
cert = CkCertW_Create();
success = CkCertW_LoadFromFile(cert,L"qa_data/smime.cer");
if (success == FALSE) {
wprintf(L"%s\n",CkCertW_lastErrorText(cert));
CkImapW_Dispose(imap);
CkCertW_Dispose(cert);
return;
}
privKey = CkPrivateKeyW_Create();
success = CkPrivateKeyW_LoadPemFile(privKey,L"qa_data/smime_privkey.pem");
if (success == FALSE) {
wprintf(L"%s\n",CkPrivateKeyW_lastErrorText(privKey));
CkImapW_Dispose(imap);
CkCertW_Dispose(cert);
CkPrivateKeyW_Dispose(privKey);
return;
}
// Use the cert and key to automatically decrypt subsequently downloaded S/MIME messages.
success = CkImapW_SetDecryptCert2(imap,cert,privKey);
if (success == FALSE) {
wprintf(L"%s\n",CkImapW_lastErrorText(imap));
CkImapW_Dispose(imap);
CkCertW_Dispose(cert);
CkPrivateKeyW_Dispose(privKey);
return;
}
success = CkImapW_Disconnect(imap);
if (success == FALSE) {
wprintf(L"%s\n",CkImapW_lastErrorText(imap));
CkImapW_Dispose(imap);
CkCertW_Dispose(cert);
CkPrivateKeyW_Dispose(privKey);
return;
}
CkImapW_Dispose(imap);
CkCertW_Dispose(cert);
CkPrivateKeyW_Dispose(privKey);
}