Programming Examples

ChilkatHOMEAndroid™ASPVisual BasicVB.NETC#iOS (IPhone)Objective-CC++CMFCDelphiFoxProJavaPerl
PHP ExtensionPHP ActiveXPythonPowerShellRubySQL ServerVBScript

C Examples

Bounced Mail
Bz2
Certificates/Keys
Charset
CSV
DKIM / DomainKey
Diffie-Hellman
DSA
Email Object
Encryption
FileAccess
FTP
HTML Conversion
HTTP
IMAP
MHT / HTML Email
MIME
NTLM
POP3
RSA
SMTP
Socket
Spider
SSH Key
SSH
SSH Tunnel
SFTP
Tar
Upload
XML
Zip
Amazon S3

 

 

 

 

 

 

 

 

Export Certificate and Private Key to PFX

Demonstrates how to export a digital certificate, it's private key, and potentially all certificate's in the chain of authentication to a PFX file.

Downloads:

MS Windows Visual C/C++ Libraries
Linux/CentOS C/C++ Libraries
MAC OS X C/C++ Libraries
Solaris C/C++ Libraries
C++ Builder Libraries
#include <C_CkCreateCS.h>
#include <C_CkCertStore.h>
#include <C_CkCert.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkCreateCS ccs;
    HCkCertStore certStore;
    HCkCert cert;
    BOOL bIncludeChain;



    //  This object is used to create a certificate store object.
    ccs = CkCreateCS_Create();

    //  Open the local machine certificate store read-only.
    CkCreateCS_putReadOnly(ccs,TRUE);
    certStore = CkCreateCS_OpenLocalSystemStore(ccs);

    //  Can we find a certificate by email address?

    cert = CkCertStore_FindCertBySubjectE(certStore,"admin@chilkatsoft.com");
    if (cert == 0 ) {
        //  Open the current-user certificate store and check it instead.
        CkCertStore_Dispose(certStore);
        printf("Checking current-user certificate store...\n");

        certStore = CkCreateCS_OpenCurrentUserStore(ccs);

        cert = CkCertStore_FindCertBySubjectE(certStore,"admin@chilkatsoft.com");
        if (cert == 0 ) {
            printf("Failed to find certificate!\n");
            return;
        }

    }

    CkCertStore_Dispose(certStore);

    //  Does this certificate have a private key accessible
    //  to the calling process?  Private keys are *not* stored
    //  within the certificate store.  Private keys are stored
    //  in a key container in a Windows protected store.  It
    //  can be one of two protected stores: the protected store for
    //  the current logged-in user account, or the "machine-key"
    //  protected store.  The private key must both exist in a
    //  protected store, and the process must have permission to
    //  access it...

    //  You can only export to a PFX if the private key exists
    //  and is accessible.

    if (CkCert_HasPrivateKey(cert) == TRUE) {

        //  Export to a PFX.
        //  Provide a password that will be required whenever the PFX is opened.
        //  Also, include all certs in the chain of authentication.

        bIncludeChain = TRUE;
        success = CkCert_ExportToPfxFile(cert,"myCert.pfx","myPassword",bIncludeChain);
        if (success != TRUE) {
            printf("%s\n",CkCert_lastErrorText(cert));
        }
        else {
            printf("Exported to PFX!\n");
        }

    }
    else {
        printf("Certificate does not have a private key available\n");
    }

    CkCert_Dispose(cert);

    //  The Chilkat Certificate, Certificate Store, Private Key,
    //  Public Key, and Key Container classes / objects are freeware.

    //  They are used by and included with the Chilkat Email,
    //  Crypt, S/MIME, and other commercial Chilkat components.


    CkCreateCS_Dispose(ccs);

    }

Need a specific example? Send a request to support@chilkatsoft.com

© 2000-2010 Chilkat Software, Inc. All Rights Reserved.