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) Duplicate openssl pkcs12 –export –in certfile.cer –inkey certfile.key –out certfile.pfxHow 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
-- 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 assumes the Chilkat API to have been previously unlocked. -- See Global Unlock Sample for sample code. DECLARE @success int DECLARE @pkey int -- Use "Chilkat_9_5_0.PrivateKey" for versions of Chilkat < 10.0.0 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 -- Use "Chilkat_9_5_0.Cert" for versions of Chilkat < 10.0.0 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 -- Use "Chilkat_9_5_0.Pfx" for versions of Chilkat < 10.0.0 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 |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.