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
(PowerBuilder) Import a PFX/P12 into the Windows Certificate StoresDemonstrates how to import the certificates contained in a .pfx/.p12 to the Windows certificate stores.
integer li_rc oleobject loo_PrimaryCert string ls_PfxPassword integer li_Success oleobject loo_CertChain integer li_ChainReachesRoot oleobject loo_Cert integer i integer li_NumCerts oleobject loo_CertStoreCU oleobject loo_CertStoreCA oleobject loo_CertStoreRootCA integer li_ReadOnlyFlag integer li_AllSuccess loo_PrimaryCert = create oleobject // Use "Chilkat_9_5_0.Cert" for versions of Chilkat < 10.0.0 li_rc = loo_PrimaryCert.ConnectToNewObject("Chilkat.Cert") if li_rc < 0 then destroy loo_PrimaryCert MessageBox("Error","Connecting to COM object failed") return end if // 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. ls_PfxPassword = "myPfxPassword" li_Success = loo_PrimaryCert.LoadPfxFile("qa_data/pfx/somePfx.p12",ls_PfxPassword) if li_Success = 0 then Write-Debug loo_PrimaryCert.LastErrorText destroy loo_PrimaryCert return end if loo_CertChain = loo_PrimaryCert.GetCertChain() if loo_PrimaryCert.LastMethodSuccess = 0 then Write-Debug loo_PrimaryCert.LastErrorText destroy loo_PrimaryCert return end if // If the certificate chain reaches the root CA cert, then the last cert in the chain // is the root CA cert. li_ChainReachesRoot = loo_CertChain.ReachesRoot if li_ChainReachesRoot = 1 then Write-Debug "The certificate chain reaches the root CA cert." end if i = 0 li_NumCerts = loo_CertChain.NumCerts do while i < li_NumCerts loo_Cert = loo_CertChain.GetCert(i) Write-Debug "SubjectDN " + string(i) + ": " + loo_Cert.SubjectDN Write-Debug "IssuerDN " + string(i) + ": " + loo_Cert.IssuerDN Write-Debug "--" destroy loo_Cert 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.. loo_CertStoreCU = create oleobject // Use "Chilkat_9_5_0.CertStore" for versions of Chilkat < 10.0.0 li_rc = loo_CertStoreCU.ConnectToNewObject("Chilkat.CertStore") loo_CertStoreCA = create oleobject // Use "Chilkat_9_5_0.CertStore" for versions of Chilkat < 10.0.0 li_rc = loo_CertStoreCA.ConnectToNewObject("Chilkat.CertStore") loo_CertStoreRootCA = create oleobject // Use "Chilkat_9_5_0.CertStore" for versions of Chilkat < 10.0.0 li_rc = loo_CertStoreRootCA.ConnectToNewObject("Chilkat.CertStore") li_ReadOnlyFlag = 0 // "CurrentUser" and "My" are the exact keywords to select your user account's certificate store. li_Success = loo_CertStoreCU.OpenWindowsStore("CurrentUser","My",li_ReadOnlyFlag) if li_Success = 0 then Write-Debug "Failed to open the CurrentUser/My certificate store for read/write." destroy loo_CertChain destroy loo_PrimaryCert destroy loo_CertStoreCU destroy loo_CertStoreCA destroy loo_CertStoreRootCA return end if // Certificate store for intermediate certification authorities (CAs). li_Success = loo_CertStoreCA.OpenWindowsStore("CurrentUser","CertificationAuthority",li_ReadOnlyFlag) if li_Success = 0 then Write-Debug "Failed to open the CurrentUser/CertificationAuthority certificate store for read/write." destroy loo_CertChain destroy loo_PrimaryCert destroy loo_CertStoreCU destroy loo_CertStoreCA destroy loo_CertStoreRootCA return end if // Certificate store for trusted root certification authorities (CAs). li_Success = loo_CertStoreRootCA.OpenWindowsStore("CurrentUser","Root",li_ReadOnlyFlag) if li_Success = 0 then Write-Debug "Failed to open the CurrentUser/Root certificate store for read/write." destroy loo_CertChain destroy loo_PrimaryCert destroy loo_CertStoreCU destroy loo_CertStoreCA destroy loo_CertStoreRootCA return end if // Iterate over the certs in the chain and import each into the desired certificate store. li_AllSuccess = 1 i = 0 do while i < li_NumCerts loo_Cert = loo_CertChain.GetCert(i) if i = 0 then // Import the primary certificate into the CurrentUser/My certificate store. li_Success = loo_CertStoreCU.AddCertificate(loo_Cert) if li_Success = 0 then Write-Debug loo_CertStoreCU.LastErrorText li_AllSuccess = 0 end if else if (i = (li_NumCerts - 1)) AND (li_ChainReachesRoot = 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. li_Success = loo_CertStoreRootCA.AddCertificate(loo_Cert) if li_Success = 0 then Write-Debug loo_CertStoreRootCA.LastErrorText li_AllSuccess = 0 end if else // This is an intermediate CA certificate. li_Success = loo_CertStoreCA.AddCertificate(loo_Cert) if li_Success = 0 then Write-Debug loo_CertStoreCA.LastErrorText li_AllSuccess = 0 end if end if end if if li_Success = 0 then Write-Debug "Failed to import certificate." end if destroy loo_Cert i = i + 1 loop destroy loo_CertChain Write-Debug "allSuccess = " + string(li_AllSuccess) destroy loo_PrimaryCert destroy loo_CertStoreCU destroy loo_CertStoreCA destroy loo_CertStoreRootCA |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.