Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

Excel Examples

Web API Categories

ASN.1
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Azure Cloud Storage
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
Socket/SSL/TLS
Spider
Stream
Tar Archive
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl

 

 

 

(Excel) Import a PFX/P12 into the Windows Certificate Stores

Demonstrates how to import the certificates contained in a .pfx/.p12 to the Windows certificate stores.

Download Excel Class Modules

Chilkat Excel Class Modules

Dim primaryCert As Chilkat.Cert
Set primaryCert = Chilkat.NewCert

' Load a PFX file into a certificate object.
' The cert object will contain the certificate from the PFX that has a private key.
' The certs in the chain of authentication (if contained in the PFX) are also loaded,
' and can be accessed by getting the certificate chain (see below).
' If the PFX did not include the issuer certs in the chain of authentication, then Chilkat will
' automatically try to construct the issuer chain from the CA and intermedicate CA certs
' already installed on the Windows system.

pfxPassword = "myPfxPassword"

success = primaryCert.LoadPfxFile("qa_data/pfx/somePfx.p12",pfxPassword)
If (success = False) Then
    Debug.Print primaryCert.LastErrorText
    Exit Sub
End If


Set certChain = primaryCert.GetCertChain()
If (primaryCert.LastMethodSuccess = False) Then
    Debug.Print primaryCert.LastErrorText
    Exit Sub
End If

' If the certificate chain reaches the root CA cert, then the last cert in the chain
' is the root CA cert.

chainReachesRoot = certChain.ReachesRoot
If (chainReachesRoot = True) Then
    Debug.Print "The certificate chain reaches the root CA cert."
End If


i = 0

numCerts = certChain.NumCerts
Do While i < numCerts
    Set cert = certChain.GetCert(i)
    Debug.Print "SubjectDN "; i; ": "; cert.SubjectDN
    Debug.Print "IssuerDN "; i; ": "; cert.IssuerDN
    Debug.Print "--"

    i = i + 1
Loop

' The primary cert having the private key will be imported into the Current User "My" certificate store.
' Any intermediate root certificates will be imported into certificate store for intermediate certificate authorities.
' The root CA cert will be imported into the Root CA cert store.

' Let's open each of these 3 certificate stores..
Dim certStoreCU As Chilkat.CertStore
Set certStoreCU = Chilkat.NewCertStore
Dim certStoreCA As Chilkat.CertStore
Set certStoreCA = Chilkat.NewCertStore
Dim certStoreRootCA As Chilkat.CertStore
Set certStoreRootCA = Chilkat.NewCertStore

readOnlyFlag = False

' "CurrentUser" and "My" are the exact keywords to select your user account's certificate store.
success = certStoreCU.OpenWindowsStore("CurrentUser","My",readOnlyFlag)
If (success = False) Then
    Debug.Print "Failed to open the CurrentUser/My certificate store for read/write."

    Exit Sub
End If

' Certificate store for intermediate certification authorities (CAs). 
success = certStoreCA.OpenWindowsStore("CurrentUser","CertificationAuthority",readOnlyFlag)
If (success = False) Then
    Debug.Print "Failed to open the CurrentUser/CertificationAuthority certificate store for read/write."

    Exit Sub
End If

' Certificate store for trusted root certification authorities (CAs). 
success = certStoreRootCA.OpenWindowsStore("CurrentUser","Root",readOnlyFlag)
If (success = False) Then
    Debug.Print "Failed to open the CurrentUser/Root certificate store for read/write."

    Exit Sub
End If

' Iterate over the certs in the chain and import each into the desired certificate store.

allSuccess = True
i = 0
Do While i < numCerts
    Set cert = certChain.GetCert(i)
    If (i = 0) Then
        ' Import the primary certificate into the CurrentUser/My certificate store.
        success = certStoreCU.AddCertificate(cert)
        If (success = False) Then
            Debug.Print certStoreCU.LastErrorText
            allSuccess = False
        End If

    Else
        If ((i = (numCerts - 1)) And (chainReachesRoot = True)) Then
            ' Add the root CA certificate to the CurrentUser/Root certificate store.
            ' (Your application can obviously choose whether this should be done or not.  Perhaps you prompt the user.)

            ' Note: If the root CA cert is already present in the Windows certificate store, Windows will display
            ' a dialog to ask if it should be deleted.  Chilkat does not explicitly display dialogs.
            success = certStoreRootCA.AddCertificate(cert)
            If (success = False) Then
                Debug.Print certStoreRootCA.LastErrorText
                allSuccess = False
            End If

        Else
            ' This is an intermediate CA certificate.
            success = certStoreCA.AddCertificate(cert)
            If (success = False) Then
                Debug.Print certStoreCA.LastErrorText
                allSuccess = False
            End If

        End If

    End If

    If (success = False) Then
        Debug.Print "Failed to import certificate."
    End If

    i = i + 1
Loop

Debug.Print "allSuccess = "; allSuccess

 

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