Sample code for 30+ languages & platforms
SQL Server

Load PFX/P12 from a Base64 Encoded PFX File

See more PFX/P12 Examples

Demonstrates how to call LoadPfxEncoded.

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
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

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

    EXEC sp_OAMethod @bd, 'LoadFile', @success OUT, 'qa_data/pfx/cert_test123.pfx'
    IF @success <> 1
      BEGIN

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

    -- Get the bytes contained in the PFX in base64 format:
    DECLARE @strBase64 nvarchar(4000)
    EXEC sp_OAMethod @bd, 'GetEncoded', @strBase64 OUT, 'base64'

    -- The base64 looks like this:  "MIIbEAIBAzCCGswGCSqGSIb3DQEHAaCCGr0Eghq5MIIatTCCBg..."

    PRINT @strBase64

    DECLARE @pfx int
    EXEC @hr = sp_OACreate 'Chilkat.Pfx', @pfx OUT

    -- Load the PFX from the base64 string
    DECLARE @password nvarchar(4000)
    SELECT @password = 'test123'
    EXEC sp_OAMethod @pfx, 'LoadPfxEncoded', @success OUT, @strBase64, 'base64', @password
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @pfx, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @bd
        EXEC @hr = sp_OADestroy @pfx
        RETURN
      END


    PRINT 'success'

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


END
GO