Sample code for 30+ languages & platforms
SQL Server

Binary Certificate Extension Data

See more Certificates Examples

Demonstrates how to binary certificate extension data by OID.

The GetExtensionBd method is added in Chilkat v9.5.0.96.

Chilkat SQL Server Downloads

SQL Server
-- 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

    DECLARE @cert int
    EXEC @hr = sp_OACreate 'Chilkat.Cert', @cert OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OAMethod @cert, 'LoadFromFile', @success OUT, 'qa_data/certs/testCert.cer'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @cert, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @cert
        RETURN
      END

    DECLARE @bd int
    EXEC @hr = sp_OACreate 'Chilkat.BinData', @bd OUT

    -- The particular certificate in this test contains the following extensions

    DECLARE @oid nvarchar(4000)
    SELECT @oid = '1.2.250.1.71.1.2.5'
    EXEC sp_OAMethod @cert, 'GetExtensionBd', @success OUT, @oid, @bd
    IF @success = 1
      BEGIN

        EXEC sp_OAMethod @bd, 'GetEncoded', @sTmp0 OUT, 'hex_lower'
        PRINT @oid + ': ' + @sTmp0
      END

    SELECT @oid = '1.2.250.1.71.1.2.2'
    EXEC sp_OAMethod @cert, 'GetExtensionBd', @success OUT, @oid, @bd
    IF @success = 1
      BEGIN

        EXEC sp_OAMethod @bd, 'GetEncoded', @sTmp0 OUT, 'hex_lower'
        PRINT @oid + ': ' + @sTmp0
      END

    SELECT @oid = '1.2.250.1.71.1.2.3'
    EXEC sp_OAMethod @cert, 'GetExtensionBd', @success OUT, @oid, @bd
    IF @success = 1
      BEGIN

        EXEC sp_OAMethod @bd, 'GetEncoded', @sTmp0 OUT, 'hex_lower'
        PRINT @oid + ': ' + @sTmp0
      END

    SELECT @oid = '1.2.250.1.71.1.2.7'
    EXEC sp_OAMethod @cert, 'GetExtensionBd', @success OUT, @oid, @bd
    IF @success = 1
      BEGIN

        EXEC sp_OAMethod @bd, 'GetEncoded', @sTmp0 OUT, 'hex_lower'
        PRINT @oid + ': ' + @sTmp0
      END

    SELECT @oid = '1.2.250.1.71.4.2.5'
    EXEC sp_OAMethod @cert, 'GetExtensionBd', @success OUT, @oid, @bd
    IF @success = 1
      BEGIN

        EXEC sp_OAMethod @bd, 'GetEncoded', @sTmp0 OUT, 'hex_lower'
        PRINT @oid + ': ' + @sTmp0
      END

    -- Sample output:
    -- 1.2.250.1.71.1.2.5: 040180
    -- 1.2.250.1.71.1.2.2: 020100
    -- 1.2.250.1.71.1.2.3: 1315383032353030303030312f32393030303539313432
    -- 1.2.250.1.71.1.2.7: 02010a
    -- 1.2.250.1.71.4.2.5: 30060c04534d3236

    -- The above binary values are actually ASN.1
    -- You can get the ASN.1 decoed by calling GetExtensionAsXml to get it in XML format,
    -- and then you extract the values from the XML.

    SELECT @oid = '1.2.250.1.71.1.2.5'
    DECLARE @strXml nvarchar(4000)
    EXEC sp_OAMethod @cert, 'GetExtensionAsXml', @strXml OUT, @oid
    EXEC sp_OAGetProperty @cert, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 1
      BEGIN


        PRINT @oid + ': ' + @strXml
      END

    SELECT @oid = '1.2.250.1.71.1.2.2'
    EXEC sp_OAMethod @cert, 'GetExtensionAsXml', @strXml OUT, @oid
    EXEC sp_OAGetProperty @cert, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 1
      BEGIN


        PRINT @oid + ': ' + @strXml
      END

    SELECT @oid = '1.2.250.1.71.1.2.3'
    EXEC sp_OAMethod @cert, 'GetExtensionAsXml', @strXml OUT, @oid
    EXEC sp_OAGetProperty @cert, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 1
      BEGIN


        PRINT @oid + ': ' + @strXml
      END

    SELECT @oid = '1.2.250.1.71.1.2.7'
    EXEC sp_OAMethod @cert, 'GetExtensionAsXml', @strXml OUT, @oid
    EXEC sp_OAGetProperty @cert, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 1
      BEGIN


        PRINT @oid + ': ' + @strXml
      END

    SELECT @oid = '1.2.250.1.71.4.2.5'
    EXEC sp_OAMethod @cert, 'GetExtensionAsXml', @strXml OUT, @oid
    EXEC sp_OAGetProperty @cert, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 1
      BEGIN


        PRINT @oid + ': ' + @strXml
      END

    -- Sample output:
    -- 1.2.250.1.71.1.2.5: <octets>gA==</octets>
    -- 1.2.250.1.71.1.2.2: <int>00</int>
    -- 1.2.250.1.71.1.2.3: <printable>8025000001/2900059142</printable>
    -- 1.2.250.1.71.1.2.7: <int>0A</int>
    -- 1.2.250.1.71.4.2.5: <sequence><utf8>SM26</utf8></sequence>

    -- "gA==" is the base64 encoded byte values
    -- "0A" is hex for decimal 1

    EXEC @hr = sp_OADestroy @cert
    EXEC @hr = sp_OADestroy @bd


END
GO