Sample code for 30+ languages & platforms
SQL Server

Load a PFX from BinData

See more PFX/P12 Examples

Demonstrates Pfx.LoadPfxBd, which loads a PFX / PKCS#12 container from the bytes held in a BinData object.

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. This is the in-memory equivalent of LoadPfxFile, for when the PKCS#12 data is already available as bytes rather than in a file.

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 @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.
    --  Load the PFX bytes into a BinData.
    DECLARE @bd int
    EXEC @hr = sp_OACreate 'Chilkat.BinData', @bd OUT

    DECLARE @bLoaded int
    EXEC sp_OAMethod @bd, 'LoadFile', @bLoaded OUT, 'qa_data/identity.pfx'
    IF @bLoaded <> 1
      BEGIN

        PRINT 'Failed to load the PFX file.'
        EXEC @hr = sp_OADestroy @pfx
        EXEC @hr = sp_OADestroy @bd
        RETURN
      END

    --  The PFX password should come from a secure source rather than being hard-coded.
    DECLARE @password nvarchar(4000)
    SELECT @password = 'myPfxPassword'

    --  Load the PFX / PKCS#12 container from the bytes held in the BinData.
    EXEC sp_OAMethod @pfx, 'LoadPfxBd', @success OUT, @bd, @password
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @pfx, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pfx
        EXEC @hr = sp_OADestroy @bd
        RETURN
      END

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

    PRINT 'Loaded ' + @iTmp0 + ' certificate(s).'

    EXEC @hr = sp_OADestroy @pfx
    EXEC @hr = sp_OADestroy @bd


END
GO