Sample code for 30+ languages & platforms
SQL Server

Load a PFX from a File

See more PFX/P12 Examples

Demonstrates Pfx.LoadPfxFile, which loads a PFX / PKCS#12 container from a file path, using the supplied password to decrypt it.

The file path is relative to the application's current working directory. An absolute path may also be used. Supply the path appropriate to your own environment.

Background. A PFX (PKCS#12) container bundles one or more certificates together with their private keys. After loading, the certificates and keys can be enumerated and used.

Chilkat SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @iTmp1 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    DECLARE @pfx int
    EXEC @hr = sp_OACreate 'Chilkat.Pfx', @pfx OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    --  The file path is relative to the application's current working directory.  An absolute path
    --  may also be used.  Supply the path appropriate to your own environment.
    --  The PFX password should come from a secure source rather than being hard-coded.
    DECLARE @password nvarchar(4000)
    SELECT @password = 'myPfxPassword'

    --  Load a PFX / PKCS#12 container from a file.
    EXEC sp_OAMethod @pfx, 'LoadPfxFile', @success OUT, 'qa_data/identity.pfx', @password
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @pfx, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pfx
        RETURN
      END

    EXEC sp_OAGetProperty @pfx, 'NumCerts', @iTmp0 OUT

    EXEC sp_OAGetProperty @pfx, 'NumPrivateKeys', @iTmp1 OUT

    PRINT 'Loaded ' + @iTmp0 + ' certificate(s) and ' + @iTmp1 + ' private key(s).'

    EXEC @hr = sp_OADestroy @pfx


END
GO