Sample code for 30+ languages & platforms
SQL Server

Convert CRL PEM to XML

See more PEM Examples

Loads a CRL (Certificate Revocation List) from the PEM file format and converts to XML to allow for visual examination and parsing.

Note: This example requires Chilkat v9.5.0.77 or greater.

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

    -- This example requires the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    -- This example requires Chilkat v9.5.0.77 or greater.
    DECLARE @pem int
    EXEC @hr = sp_OACreate 'Chilkat.Pem', @pem OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OASetProperty @pem, 'VerboseLogging', 1
    EXEC sp_OAMethod @pem, 'LoadPemFile', @success OUT, 'qa_data/crl/sampleCrl.pem', 'password_not_used'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @pem, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pem
        RETURN
      END

    DECLARE @numCrls int
    EXEC sp_OAGetProperty @pem, 'NumCrls', @numCrls OUT
    DECLARE @i int
    SELECT @i = 0

    DECLARE @asn int
    EXEC @hr = sp_OACreate 'Chilkat.Asn', @asn OUT

    DECLARE @xml int
    EXEC @hr = sp_OACreate 'Chilkat.Xml', @xml OUT

    WHILE @i < @numCrls
      BEGIN

        -- Get the CRL as base64 (multi-line)
        DECLARE @crlBase64 nvarchar(4000)
        EXEC sp_OAMethod @pem, 'GetEncodedItem', @crlBase64 OUT, 'crl', '', 'base64_mime', @i
        EXEC sp_OAGetProperty @pem, 'LastMethodSuccess', @iTmp0 OUT
        IF @iTmp0 <> 1
          BEGIN
            EXEC sp_OAGetProperty @pem, 'LastErrorText', @sTmp0 OUT
            PRINT @sTmp0
            EXEC @hr = sp_OADestroy @pem
            EXEC @hr = sp_OADestroy @asn
            EXEC @hr = sp_OADestroy @xml
            RETURN
          END


        PRINT @crlBase64

        EXEC sp_OAMethod @asn, 'LoadEncoded', @success OUT, @crlBase64, 'base64'
        IF @success <> 1
          BEGIN
            EXEC sp_OAGetProperty @asn, 'LastErrorText', @sTmp0 OUT
            PRINT @sTmp0
            EXEC @hr = sp_OADestroy @pem
            EXEC @hr = sp_OADestroy @asn
            EXEC @hr = sp_OADestroy @xml
            RETURN
          END

        -- Convert ASN.1 to XML and load into xml and re-emit for pretty printing..
        EXEC sp_OAMethod @asn, 'AsnToXml', @sTmp0 OUT
        EXEC sp_OAMethod @xml, 'LoadXml', @success OUT, @sTmp0
        EXEC sp_OAMethod @xml, 'GetXml', @sTmp0 OUT
        PRINT @sTmp0

        -- Use this online tool to generate parsing code from CRL XML: 
        -- Generate Parsing Code from XML


        PRINT '-------------------------------------'
        SELECT @i = @i + 1
      END

    EXEC @hr = sp_OADestroy @pem
    EXEC @hr = sp_OADestroy @asn
    EXEC @hr = sp_OADestroy @xml


END
GO