Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Visual FoxPro) Import a PFX/P12 into the Windows Certificate StoresDemonstrates how to import the certificates contained in a .pfx/.p12 to the Windows certificate stores.
LOCAL loPrimaryCert LOCAL lcPfxPassword LOCAL lnSuccess LOCAL loCertChain LOCAL lnChainReachesRoot LOCAL loCert LOCAL i LOCAL lnNumCerts LOCAL loCertStoreCU LOCAL loCertStoreCA LOCAL loCertStoreRootCA LOCAL lnReadOnlyFlag LOCAL lnAllSuccess * For versions of Chilkat < 10.0.0, use CreateObject('Chilkat_9_5_0.Cert') loPrimaryCert = CreateObject('Chilkat.Cert') * 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. lcPfxPassword = "myPfxPassword" lnSuccess = loPrimaryCert.LoadPfxFile("qa_data/pfx/somePfx.p12",lcPfxPassword) IF (lnSuccess = 0) THEN ? loPrimaryCert.LastErrorText RELEASE loPrimaryCert CANCEL ENDIF loCertChain = loPrimaryCert.GetCertChain() IF (loPrimaryCert.LastMethodSuccess = 0) THEN ? loPrimaryCert.LastErrorText RELEASE loPrimaryCert CANCEL ENDIF * If the certificate chain reaches the root CA cert, then the last cert in the chain * is the root CA cert. lnChainReachesRoot = loCertChain.ReachesRoot IF (lnChainReachesRoot = 1) THEN ? "The certificate chain reaches the root CA cert." ENDIF i = 0 lnNumCerts = loCertChain.NumCerts DO WHILE i < lnNumCerts loCert = loCertChain.GetCert(i) ? "SubjectDN " + STR(i) + ": " + loCert.SubjectDN ? "IssuerDN " + STR(i) + ": " + loCert.IssuerDN ? "--" RELEASE loCert i = i + 1 ENDDO * 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.. * For versions of Chilkat < 10.0.0, use CreateObject('Chilkat_9_5_0.CertStore') loCertStoreCU = CreateObject('Chilkat.CertStore') * For versions of Chilkat < 10.0.0, use CreateObject('Chilkat_9_5_0.CertStore') loCertStoreCA = CreateObject('Chilkat.CertStore') * For versions of Chilkat < 10.0.0, use CreateObject('Chilkat_9_5_0.CertStore') loCertStoreRootCA = CreateObject('Chilkat.CertStore') lnReadOnlyFlag = 0 * "CurrentUser" and "My" are the exact keywords to select your user account's certificate store. lnSuccess = loCertStoreCU.OpenWindowsStore("CurrentUser","My",lnReadOnlyFlag) IF (lnSuccess = 0) THEN ? "Failed to open the CurrentUser/My certificate store for read/write." RELEASE loCertChain RELEASE loPrimaryCert RELEASE loCertStoreCU RELEASE loCertStoreCA RELEASE loCertStoreRootCA CANCEL ENDIF * Certificate store for intermediate certification authorities (CAs). lnSuccess = loCertStoreCA.OpenWindowsStore("CurrentUser","CertificationAuthority",lnReadOnlyFlag) IF (lnSuccess = 0) THEN ? "Failed to open the CurrentUser/CertificationAuthority certificate store for read/write." RELEASE loCertChain RELEASE loPrimaryCert RELEASE loCertStoreCU RELEASE loCertStoreCA RELEASE loCertStoreRootCA CANCEL ENDIF * Certificate store for trusted root certification authorities (CAs). lnSuccess = loCertStoreRootCA.OpenWindowsStore("CurrentUser","Root",lnReadOnlyFlag) IF (lnSuccess = 0) THEN ? "Failed to open the CurrentUser/Root certificate store for read/write." RELEASE loCertChain RELEASE loPrimaryCert RELEASE loCertStoreCU RELEASE loCertStoreCA RELEASE loCertStoreRootCA CANCEL ENDIF * Iterate over the certs in the chain and import each into the desired certificate store. lnAllSuccess = 1 i = 0 DO WHILE i < lnNumCerts loCert = loCertChain.GetCert(i) IF (i = 0) THEN * Import the primary certificate into the CurrentUser/My certificate store. lnSuccess = loCertStoreCU.AddCertificate(loCert) IF (lnSuccess = 0) THEN ? loCertStoreCU.LastErrorText lnAllSuccess = 0 ENDIF ELSE IF ((i = (lnNumCerts - 1)) AND (lnChainReachesRoot = 1)) 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. lnSuccess = loCertStoreRootCA.AddCertificate(loCert) IF (lnSuccess = 0) THEN ? loCertStoreRootCA.LastErrorText lnAllSuccess = 0 ENDIF ELSE * This is an intermediate CA certificate. lnSuccess = loCertStoreCA.AddCertificate(loCert) IF (lnSuccess = 0) THEN ? loCertStoreCA.LastErrorText lnAllSuccess = 0 ENDIF ENDIF ENDIF IF (lnSuccess = 0) THEN ? "Failed to import certificate." ENDIF RELEASE loCert i = i + 1 ENDDO RELEASE loCertChain ? "allSuccess = " + STR(lnAllSuccess) RELEASE loPrimaryCert RELEASE loCertStoreCU RELEASE loCertStoreCA RELEASE loCertStoreRootCA |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.