SQL Server
SQL Server
Enumerate Private Keys in a PFX
See more PFX/P12 Examples
Demonstrates Pfx.PrivateKeyAt, which copies the private key at a zero-based index into a PrivateKey object. The example enumerates all private keys in the PFX.
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. Valid indexes are less than
NumPrivateKeys. Each retrieved key is an independent copy that can be inspected or used separately.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
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'
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
-- Enumerate the private keys in the PFX by zero-based index. Valid indexes are less than
-- NumPrivateKeys.
DECLARE @numKeys int
EXEC sp_OAGetProperty @pfx, 'NumPrivateKeys', @numKeys OUT
DECLARE @privKey int
EXEC @hr = sp_OACreate 'Chilkat.PrivateKey', @privKey OUT
DECLARE @i int
SELECT @i = 0
WHILE @i <= @numKeys - 1
BEGIN
EXEC sp_OAMethod @pfx, 'PrivateKeyAt', @success OUT, @i, @privKey
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @pfx, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @pfx
EXEC @hr = sp_OADestroy @privKey
RETURN
END
EXEC sp_OAGetProperty @privKey, 'KeyType', @sTmp0 OUT
EXEC sp_OAGetProperty @privKey, 'BitLength', @iTmp0 OUT
PRINT 'Private key ' + @i + ': ' + @sTmp0 + ' (' + @iTmp0 + ' bits)'
SELECT @i = @i + 1
END
EXEC @hr = sp_OADestroy @pfx
EXEC @hr = sp_OADestroy @privKey
END
GO