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 Public Key from CSRDemonstrates how to get the public key from a CSR.
-- 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) -- This requires the Chilkat API to have been previously unlocked. -- See Global Unlock Sample for sample code. DECLARE @pem int -- Use "Chilkat_9_5_0.Pem" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.Pem', @pem OUT IF @hr <> 0 BEGIN PRINT 'Failed to create ActiveX component' RETURN END -- No password is required. Pass an empty password string.. DECLARE @noPassword nvarchar(4000) SELECT @noPassword = '' DECLARE @success int EXEC sp_OAMethod @pem, 'LoadPemFile', @success OUT, 'qa_data/csr/csr2.pem', @noPassword IF @success <> 1 BEGIN EXEC sp_OAGetProperty @pem, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @pem RETURN END DECLARE @strBase64 nvarchar(4000) EXEC sp_OAMethod @pem, 'GetEncodedItem', @strBase64 OUT, 'csr', '', 'base64', 0 DECLARE @asn int -- Use "Chilkat_9_5_0.Asn" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.Asn', @asn OUT EXEC sp_OAMethod @asn, 'LoadEncoded', @success OUT, @strBase64, 'base64' IF @success <> 1 BEGIN EXEC sp_OAGetProperty @asn, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @pem EXEC @hr = sp_OADestroy @asn RETURN END -- Convert the ASN.1 to XML. DECLARE @xml int -- Use "Chilkat_9_5_0.Xml" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.Xml', @xml OUT EXEC sp_OAMethod @asn, 'AsnToXml', @sTmp0 OUT EXEC sp_OAMethod @xml, 'LoadXml', @success OUT, @sTmp0 EXEC sp_OAMethod @xml, 'GetXml', @sTmp0 OUT PRINT @sTmp0 PRINT '----' DECLARE @strModulusHex nvarchar(4000) EXEC sp_OAMethod @xml, 'GetChildContent', @strModulusHex OUT, 'bits' PRINT 'strModulusHex = ' + @strModulusHex PRINT '----' -- We need the modulus as base64. DECLARE @bd int -- Use "Chilkat_9_5_0.BinData" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.BinData', @bd OUT EXEC sp_OAMethod @bd, 'AppendEncoded', @success OUT, @strModulusHex, 'hex' DECLARE @modulus64 nvarchar(4000) EXEC sp_OAMethod @bd, 'GetEncoded', @modulus64 OUT, 'base64' PRINT 'modulus64 = ' + @modulus64 PRINT '----' -- Build the XML for the public key. DECLARE @xmlPubKey int -- Use "Chilkat_9_5_0.Xml" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.Xml', @xmlPubKey OUT EXEC sp_OASetProperty @xmlPubKey, 'Tag', 'RSAPublicKey' EXEC sp_OAMethod @xmlPubKey, 'UpdateChildContent', NULL, 'Modulus', @modulus64 -- The RSA exponent will always be decimal 65537 (base64 = AQAB) EXEC sp_OAMethod @xmlPubKey, 'UpdateChildContent', NULL, 'Exponent', 'AQAB' PRINT 'RSA public key as XML:' EXEC sp_OAMethod @xmlPubKey, 'GetXml', @sTmp0 OUT PRINT @sTmp0 PRINT '----' -- Load the XML into a Chilkat public key object. DECLARE @pubkey int -- Use "Chilkat_9_5_0.PublicKey" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.PublicKey', @pubkey OUT EXEC sp_OAMethod @xmlPubKey, 'GetXml', @sTmp0 OUT EXEC sp_OAMethod @pubkey, 'LoadFromString', @success OUT, @sTmp0 IF @success <> 1 BEGIN EXEC sp_OAGetProperty @pubkey, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @pem EXEC @hr = sp_OADestroy @asn EXEC @hr = sp_OADestroy @xml EXEC @hr = sp_OADestroy @bd EXEC @hr = sp_OADestroy @xmlPubKey EXEC @hr = sp_OADestroy @pubkey RETURN END -- Show the public key as PEM. DECLARE @preferPkcs1 int SELECT @preferPkcs1 = 1 EXEC sp_OAMethod @pubkey, 'GetPem', @sTmp0 OUT, @preferPkcs1 PRINT @sTmp0 EXEC @hr = sp_OADestroy @pem EXEC @hr = sp_OADestroy @asn EXEC @hr = sp_OADestroy @xml EXEC @hr = sp_OADestroy @bd EXEC @hr = sp_OADestroy @xmlPubKey EXEC @hr = sp_OADestroy @pubkey END GO |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.