Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(SQL Server) Get the Certificate with Private Key from a Java KeyStoreSee more Java KeyStore (JKS) ExamplesLoad a Chilkat certificate object from a Java KeyStore.
-- 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) -- 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 -- Use "Chilkat_9_5_0.JavaKeyStore" for versions of Chilkat < 10.0.0 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' DECLARE @success int EXEC sp_OAMethod @jks, 'LoadFile', @success OUT, @password, 'qa_data/jks/test_secret.jks' IF @success <> 1 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 sp_OAMethod @jks, 'GetCertChain', @chain OUT, 0 EXEC sp_OAGetProperty @jks, 'LastMethodSuccess', @iTmp0 OUT IF @iTmp0 <> 1 BEGIN EXEC sp_OAGetProperty @jks, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @jks RETURN END DECLARE @cert int EXEC sp_OAMethod @chain, 'GetCert', @cert OUT, 0 EXEC sp_OAGetProperty @chain, 'LastMethodSuccess', @iTmp0 OUT IF @iTmp0 <> 1 BEGIN EXEC sp_OAGetProperty @chain, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @chain EXEC @hr = sp_OADestroy @jks RETURN END EXEC @hr = sp_OADestroy @chain -- 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 @cert EXEC @hr = sp_OADestroy @jks 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 -- Use "Chilkat_9_5_0.Crypt2" for versions of Chilkat < 10.0.0 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 @crypt RETURN END -- ... -- ... EXEC @hr = sp_OADestroy @cert EXEC @hr = sp_OADestroy @jks EXEC @hr = sp_OADestroy @crypt END GO |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.