Unicode C
Unicode C
S/MIME Decrypt using Certificate and Private Key in Apple Keychain
See more Apple Keychain Examples
Decrypts S/MIME using a certificate with private key found in the Apple Keychain.Chilkat Unicode C Downloads
#include <C_CkCertW.h>
#include <C_CkMimeW.h>
void ChilkatSample(void)
{
BOOL success;
HCkCertW cert;
HCkMimeW mime;
success = FALSE;
cert = CkCertW_Create();
// Load the certificate to be used for decryption by the Subject Common Name
// On MacOS and iOS, Chilkat will search the Keychain(s) for the matching certificate.
// Note: The private key must be present to decrypt.
success = CkCertW_LoadByCommonName(cert,L"My Cert");
if (success == FALSE) {
wprintf(L"%s\n",CkCertW_lastErrorText(cert));
CkCertW_Dispose(cert);
return;
}
wprintf(L"Cert has private key: %d\n",CkCertW_HasPrivateKey(cert));
// Load the MIME to be decrypted.
// We encrypted using this example: S/MIME Encrypt using Certificate in Apple Keychain
mime = CkMimeW_Create();
success = CkMimeW_LoadMimeFile(mime,L"./encrypted.mime");
if (success == FALSE) {
wprintf(L"%s\n",CkMimeW_lastErrorText(mime));
CkCertW_Dispose(cert);
CkMimeW_Dispose(mime);
return;
}
wprintf(L"%s\n",CkMimeW_getMime(mime));
wprintf(L"----\n");
// Here's the encrypted MIME:
// Subject: test
// SomeHeader: 123
// Content-Type: application/x-pkcs7-mime; name="smime.p7m"; smime-type="enveloped-data"
// Content-Disposition: attachment; filename="smime.p7m"
// Content-Transfer-Encoding: base64
//
// MIICSwYJKoZIhvcNAQcDoIICPDCCAjgCAQAxggGzMIIBrwIBADCBljCBgTELMAkGA1UEBhMCSVQx
// EDAOBgNVBAgMB0JlcmdhbW8xGTAXBgNVBAcMEFBvbnRlIFNhbiBQaWV0cm8xFzAVBgNVBAoMDkFj
// dGFsaXMgUy5wLkEuMSwwKgYDVQQDDCNBY3RhbGlzIENsaWVudCBBdXRoZW50aWNhdGlvbiBDQSBH
// MwIQPCWvkSv8oQ7xRmEHJ6TzEDANBgkqhkiG9w0BAQEFAASCAQB3VZvHRE5EWxug7Sckpcz1ucDZ
// YiTKiqmyPt75MhzNRQLtKFx/jWwlemUwnPMzeu6yutCkZ74Bdn7MBsfDqV3bUz43wAu+fRBteGvF
// mTc00MfY8L7o8dkpj4AqAOCj4hKQzbSE99GvSzyXcPE2Gm5NrOPtKxqfFqbBRTCb4fBZP84LaL+x
// rnYfrM4qXTppixyN8iFYCd4maEbMu/GA5o+j0BkDDnx42pILDoAGV/ERyx55Y3Nc2Mhm/cITBMNn
// g7uS9KPrlYizNaqVu09Hi9jg4gdZaRiTjUqg05tSOk/YqIQxTgfscwSPY92/ewpI6e1EHtLt8Q33
// gWCbERptSntUMHwGCSqGSIb3DQEHATAdBglghkgBZQMEAQIEENm1AxeXlEMx7p6McjHIj5CAUEQj
// 0GuJ5LnTqiqIjOiwmwNidl1N1TRluxX5vAQvwBuYE6bQK4+i04yn2Av3cucW4kvxgP2Nmni+XgQt
// aPPKlasaVceEeZ15IYjw77/m3YYn
// -------------------------------------
// Decrypt the MIME.
success = CkMimeW_DecryptUsingCert(mime,cert);
if (success == FALSE) {
wprintf(L"%s\n",CkMimeW_lastErrorText(mime));
CkCertW_Dispose(cert);
CkMimeW_Dispose(mime);
return;
}
// -------------------------------------
// Note: A password dialog may be shown, possibly twice, when accessing the private key.
// See the following blog post explaining how to suppress the dialog:
// Suppress Password Dialog when Accessing Private Key in Apple Keychain
// Show the unencrypted MIME
wprintf(L"%s\n",CkMimeW_getMime(mime));
// Here's the decrypted MIME
// Subject: test
// SomeHeader: 123
// Content-Type: text/plain
//
// This is the body.
CkCertW_Dispose(cert);
CkMimeW_Dispose(mime);
}