Imports certificate(s) and its associated private key from a PFX file.
Syntax:
- Set <oCertificate> = ediSecurities.ImportCertificatePfx(<sImportFile>, <sPassword>, [<sServiceProvider>], [<lProviderType>])
Parameters:
- sImportFile – The PFX file to import.
- sPassword – The password that was used to secure the PFX file.
- sServiceProvider – Optional. Cryptographic Service Provider (CSP) whose database the associated private key will be stored. If none is specified, the default CSP is used.
- lProviderType – Optional. CSP type. If none is specified then the default provider type is used. If the sServiceProvider name has been specified then the provider type of that service provider is used. For the list of possible provider types please see Cryptographic Service Provider Type Constants.
Returns:
Returns an object of type FREDI.ediSecurityCertificate if imported successfully; otherwise NULL or an empty object is returned if the operation fails. For PFX files containing multiple certificates, the object returned is the object of the last certificate successfully imported.
Remarks:
The PFX file may contain multiple certificates with their associated private keys (Note. A certificate does not have to have an associated private key). Each certificate will be imported and stored into the default certificate store, and a new key container with a unique GUID name is created in the CSP database for each associated private key. If an error is generated while importing multiple certificates, all certificates already imported successfully until the time the error was generated will remain in the certificate store -- that is, the error does not revert the operation back.
The sServiceProvider and lProviderType specify the database where the private key container will be stored. If sServiceProvider and/or lProviderType are not specified, the default CSP database is used, which is specified by DefaultProviderName andServiceProviderType. The type of database, whether machine key set or user key set, is specified using ediDocument.Option by the constant OptDocument_MachineKeySet.
The default certificate store is specified by the properties DefaultCertSystemStoreName and DefaultCertSystemStoreLocation.
Example:
Dim oEdiDoc As Fredi.ediDocument
Dim oSecurities As Fredi.ediSecurities
Dim oCertificate As Fredi.ediSecurityCertificate
' Create instance of Framework EDI.
Set oEdiDoc = New Fredi.ediDocument
' Get securities object.
Set oSecurities = oEdiDoc.GetSecurities
' Set the default certificate store
oSecurities.DefaultCertSystemStoreName = "My" ' Case sensitive
' Set the default certificate store location
oSecurities.DefaultCertSystemStoreLocation = "CurrentUser"
' Import PFX file.
' Password of PFX file is PASS123.
Set oCertificate = oSecurities.ImportCertificatePfx(App.Path & "\MyExportedTestCert.PFX", "PASS123")
If Not oCertificate Is Nothing Then
MsgBox "Successfully imported PFX file MyExportedTestCert.PFX."Else
' Get the name of the key container that was created to store the import private key.
If Len(oCertificate.CspKeyContainer) > 0 Then
MsgBox "Key container of Private Key is " & oCertificate.CspKeyContainerEnd If
MsgBox "Failed to create certificate"End If
Sample