SQL Server
SQL Server
Get the Certificate with Private Key from a Java KeyStore
See more Java KeyStore (JKS) Examples
Load a Chilkat certificate object from a Java KeyStore.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 requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
-- Most of the time a .jks contains one certificate with it's associated private key.
-- (Similar to how a .pfx/.p12 usually contains a particular certificate with private key.)
-- This example demonstrates how to get the certificate with private key such that it can be used
-- by other Chilkat classes wherever a cert w/ private key is needed.
DECLARE @jks int
EXEC @hr = sp_OACreate 'Chilkat.JavaKeyStore', @jks OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
DECLARE @password nvarchar(4000)
SELECT @password = 'secret'
EXEC sp_OAMethod @jks, 'LoadFile', @success OUT, @password, 'qa_data/jks/test_secret.jks'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @jks, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @jks
RETURN
END
-- Make sure we have a private key.
EXEC sp_OAGetProperty @jks, 'NumPrivateKeys', @iTmp0 OUT
IF @iTmp0 < 1
BEGIN
PRINT 'No private key available.'
EXEC @hr = sp_OADestroy @jks
RETURN
END
-- -------------------------------------------------------------------------
-- Get the certificate chain associated with the 1st (and probably only) private key in the JKS.
DECLARE @chain int
EXEC @hr = sp_OACreate 'Chilkat.CertChain', @chain OUT
EXEC sp_OAMethod @jks, 'CertChainAt', @success OUT, 0, @chain
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @jks, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @jks
EXEC @hr = sp_OADestroy @chain
RETURN
END
DECLARE @cert int
EXEC @hr = sp_OACreate 'Chilkat.Cert', @cert OUT
EXEC sp_OAMethod @chain, 'CertAt', @success OUT, 0, @cert
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @chain, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @jks
EXEC @hr = sp_OADestroy @chain
EXEC @hr = sp_OADestroy @cert
RETURN
END
-- Verify again that this cert has a private key.
EXEC sp_OAMethod @cert, 'HasPrivateKey', @iTmp0 OUT
IF @iTmp0 <> 1
BEGIN
PRINT 'Certificate has no associated private key.'
EXEC @hr = sp_OADestroy @jks
EXEC @hr = sp_OADestroy @chain
EXEC @hr = sp_OADestroy @cert
RETURN
END
-- We now have the cert object with it's associated private key, and it can be used in other Chilkat classes where needed.
-- For example..
DECLARE @crypt int
EXEC @hr = sp_OACreate 'Chilkat.Crypt2', @crypt OUT
EXEC sp_OAMethod @crypt, 'SetSigningCert', @success OUT, @cert
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @crypt, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @jks
EXEC @hr = sp_OADestroy @chain
EXEC @hr = sp_OADestroy @cert
EXEC @hr = sp_OADestroy @crypt
RETURN
END
-- ...
-- ...
EXEC @hr = sp_OADestroy @jks
EXEC @hr = sp_OADestroy @chain
EXEC @hr = sp_OADestroy @cert
EXEC @hr = sp_OADestroy @crypt
END
GO