SQL Server
SQL Server
Convert Java KeyStore to PEM
See more Java KeyStore (JKS) Examples
Loads a Java keystore file and saves the trusted certificate entries to a PEM file.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
-- Important: Do not use nvarchar(max). See the warning about using nvarchar(max).
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- This requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
DECLARE @jks int
EXEC @hr = sp_OACreate 'Chilkat.JavaKeyStore', @jks OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
DECLARE @jksPassword nvarchar(4000)
SELECT @jksPassword = 'myJksPassword'
-- Load the Java keystore from a file. The JKS file password is used
-- to verify the keyed digest that is found at the very end of the keystore.
-- It verifies that the keystore has not been modified.
EXEC sp_OAMethod @jks, 'LoadFile', @success OUT, @jksPassword, '/someDir/keyStore.jks'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @jks, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @jks
RETURN
END
-- Open/create the output PEM file.
-- This example uses Chilkat's file access class for writing the output file.
-- You may replace the file I/O lines of code with whatever is most convenient for you.
DECLARE @fac int
EXEC @hr = sp_OACreate 'Chilkat.FileAccess', @fac OUT
EXEC sp_OAMethod @fac, 'OpenForWrite', @success OUT, '/pemFiles/caCerts.pem'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @fac, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @jks
EXEC @hr = sp_OADestroy @fac
RETURN
END
DECLARE @numCerts int
EXEC sp_OAGetProperty @jks, 'NumTrustedCerts', @numCerts OUT
DECLARE @cert int
EXEC @hr = sp_OACreate 'Chilkat.Cert', @cert OUT
DECLARE @pem nvarchar(4000)
-- Iterate over the trusted certs, get the PEM for each,
-- and append it to the output file.
DECLARE @i int
SELECT @i = 0
WHILE @i < @numCerts
BEGIN
EXEC sp_OAMethod @jks, 'TrustedCertAt', @success OUT, @i, @cert
-- Get the certificate in PEM format.
EXEC sp_OAMethod @cert, 'ExportCertPem', @pem OUT
-- Append the PEM string to the open file.
EXEC sp_OAMethod @fac, 'AppendText', @success OUT, @pem, 'utf-8'
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @fac, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @jks
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @cert
RETURN
END
SELECT @i = @i + 1
END
-- Close the output file.
EXEC sp_OAMethod @fac, 'FileClose', NULL
PRINT 'Trusted certificates saved to PEM.'
EXEC @hr = sp_OADestroy @jks
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @cert
END
GO