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
(DataFlex) Import a PFX/P12 into the Windows Certificate StoresDemonstrates how to import the certificates contained in a .pfx/.p12 to the Windows certificate stores.
Use ChilkatAx-win32.pkg Procedure Test Handle hoPrimaryCert String sPfxPassword Boolean iSuccess Variant vCertChain Handle hoCertChain Boolean iChainReachesRoot Variant vCert Handle hoCert Integer i Integer iNumCerts Handle hoCertStoreCU Handle hoCertStoreCA Handle hoCertStoreRootCA Boolean iReadOnlyFlag Boolean iAllSuccess String sTemp1 Boolean bTemp1 Get Create (RefClass(cComChilkatCert)) To hoPrimaryCert If (Not(IsComObjectCreated(hoPrimaryCert))) Begin Send CreateComObject of hoPrimaryCert End // 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. Move "myPfxPassword" To sPfxPassword Get ComLoadPfxFile Of hoPrimaryCert "qa_data/pfx/somePfx.p12" sPfxPassword To iSuccess If (iSuccess = False) Begin Get ComLastErrorText Of hoPrimaryCert To sTemp1 Showln sTemp1 Procedure_Return End Get ComGetCertChain Of hoPrimaryCert To vCertChain If (IsComObject(vCertChain)) Begin Get Create (RefClass(cComChilkatCertChain)) To hoCertChain Set pvComObject Of hoCertChain To vCertChain End Get ComLastMethodSuccess Of hoPrimaryCert To bTemp1 If (bTemp1 = False) Begin Get ComLastErrorText Of hoPrimaryCert To sTemp1 Showln sTemp1 Procedure_Return End // If the certificate chain reaches the root CA cert, then the last cert in the chain // is the root CA cert. Get ComReachesRoot Of hoCertChain To iChainReachesRoot If (iChainReachesRoot = True) Begin Showln "The certificate chain reaches the root CA cert." End Move 0 To i Get ComNumCerts Of hoCertChain To iNumCerts While (i < iNumCerts) Get ComGetCert Of hoCertChain i To vCert If (IsComObject(vCert)) Begin Get Create (RefClass(cComChilkatCert)) To hoCert Set pvComObject Of hoCert To vCert End Get ComSubjectDN Of hoCert To sTemp1 Showln "SubjectDN " i ": " sTemp1 Get ComIssuerDN Of hoCert To sTemp1 Showln "IssuerDN " i ": " sTemp1 Showln "--" Send Destroy of hoCert Move (i + 1) To i 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.. Get Create (RefClass(cComChilkatCertStore)) To hoCertStoreCU If (Not(IsComObjectCreated(hoCertStoreCU))) Begin Send CreateComObject of hoCertStoreCU End Get Create (RefClass(cComChilkatCertStore)) To hoCertStoreCA If (Not(IsComObjectCreated(hoCertStoreCA))) Begin Send CreateComObject of hoCertStoreCA End Get Create (RefClass(cComChilkatCertStore)) To hoCertStoreRootCA If (Not(IsComObjectCreated(hoCertStoreRootCA))) Begin Send CreateComObject of hoCertStoreRootCA End Move False To iReadOnlyFlag // "CurrentUser" and "My" are the exact keywords to select your user account's certificate store. Get ComOpenWindowsStore Of hoCertStoreCU "CurrentUser" "My" iReadOnlyFlag To iSuccess If (iSuccess = False) Begin Showln "Failed to open the CurrentUser/My certificate store for read/write." Send Destroy of hoCertChain Procedure_Return End // Certificate store for intermediate certification authorities (CAs). Get ComOpenWindowsStore Of hoCertStoreCA "CurrentUser" "CertificationAuthority" iReadOnlyFlag To iSuccess If (iSuccess = False) Begin Showln "Failed to open the CurrentUser/CertificationAuthority certificate store for read/write." Send Destroy of hoCertChain Procedure_Return End // Certificate store for trusted root certification authorities (CAs). Get ComOpenWindowsStore Of hoCertStoreRootCA "CurrentUser" "Root" iReadOnlyFlag To iSuccess If (iSuccess = False) Begin Showln "Failed to open the CurrentUser/Root certificate store for read/write." Send Destroy of hoCertChain Procedure_Return End // Iterate over the certs in the chain and import each into the desired certificate store. Move True To iAllSuccess Move 0 To i While (i < iNumCerts) Get ComGetCert Of hoCertChain i To vCert If (IsComObject(vCert)) Begin Get Create (RefClass(cComChilkatCert)) To hoCert Set pvComObject Of hoCert To vCert End If (i = 0) Begin // Import the primary certificate into the CurrentUser/My certificate store. Get ComAddCertificate Of hoCertStoreCU vCert To iSuccess If (iSuccess = False) Begin Get ComLastErrorText Of hoCertStoreCU To sTemp1 Showln sTemp1 Move False To iAllSuccess End End Else Begin If ((i = (iNumCerts - 1)) And (iChainReachesRoot = True)) Begin // 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. Get ComAddCertificate Of hoCertStoreRootCA vCert To iSuccess If (iSuccess = False) Begin Get ComLastErrorText Of hoCertStoreRootCA To sTemp1 Showln sTemp1 Move False To iAllSuccess End End Else Begin // This is an intermediate CA certificate. Get ComAddCertificate Of hoCertStoreCA vCert To iSuccess If (iSuccess = False) Begin Get ComLastErrorText Of hoCertStoreCA To sTemp1 Showln sTemp1 Move False To iAllSuccess End End End If (iSuccess = False) Begin Showln "Failed to import certificate." End Send Destroy of hoCert Move (i + 1) To i Loop Send Destroy of hoCertChain Showln "allSuccess = " iAllSuccess End_Procedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.