SQL Server
SQL Server
Get Public Key from CSR
See more CSR Examples
Demonstrates how to get the public key from a CSR.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 @pem int
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 = ''
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
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
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
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
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
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