SQL Server
SQL Server
Duplicate openssl pkcs12 –export –in certfile.cer –inkey certfile.key –out certfile.pfx
See more OpenSSL Examples
How to create a PKCS12 (.p12 or .pfx) from a certificate file and private key file: Demonstrates how to duplicate this OpenSSL command:Duplicate openssl pkcs12 –export –in certfile.cer –inkey certfile.key –out certfile.pfx
Chilkat SQL Server Downloads
-- 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
-- Important: Do not use nvarchar(max). See the warning about using nvarchar(max).
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- This example assumes the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
DECLARE @pkey int
EXEC @hr = sp_OACreate 'Chilkat.PrivateKey', @pkey OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
-- Load the private key from the file.
EXEC sp_OAMethod @pkey, 'LoadAnyFormatFile', @success OUT, 'certFile.key', ''
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @pkey, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @pkey
RETURN
END
DECLARE @cert int
EXEC @hr = sp_OACreate 'Chilkat.Cert', @cert OUT
-- The LoadFromFile method auto-recognizes the format...
EXEC sp_OAMethod @cert, 'LoadFromFile', @success OUT, 'certfile.cer'
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @cert, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @pkey
EXEC @hr = sp_OADestroy @cert
RETURN
END
-- We'll need a cert chain object to create the PKCS12, so get it
-- from the cert.
DECLARE @certChain int
EXEC sp_OAMethod @cert, 'GetCertChain', @certChain OUT
EXEC sp_OAGetProperty @cert, 'LastMethodSuccess', @iTmp0 OUT
IF Not @iTmp0
BEGIN
EXEC sp_OAGetProperty @cert, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @pkey
EXEC @hr = sp_OADestroy @cert
RETURN
END
-- Create the PFX object, add the cert and private key, and write to a .pfx file.
DECLARE @pfx int
EXEC @hr = sp_OACreate 'Chilkat.Pfx', @pfx OUT
-- The cert(s) are automatically added in the call to AddPrivateKey
EXEC sp_OAMethod @pfx, 'AddPrivateKey', @success OUT, @pkey, @certChain
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @pfx, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @pkey
EXEC @hr = sp_OADestroy @cert
EXEC @hr = sp_OADestroy @pfx
RETURN
END
-- Write the .pfx to a file.
DECLARE @password nvarchar(4000)
SELECT @password = 'myPassword'
EXEC sp_OAMethod @pfx, 'ToFile', @success OUT, @password, 'certfile.pfx'
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @pfx, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @pkey
EXEC @hr = sp_OADestroy @cert
EXEC @hr = sp_OADestroy @pfx
RETURN
END
PRINT 'Success.'
EXEC @hr = sp_OADestroy @pkey
EXEC @hr = sp_OADestroy @cert
EXEC @hr = sp_OADestroy @pfx
END
GO