Sample code for 30+ languages & platforms
SQL Server

Load Base64-encoded X.509 Certificate (.cer)

See more Certificates Examples

Loads a digital certificate from a base64 encoded DER X.509 Certificate (.cer, .crt) and fetches information about the cert.

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

    -- LoadFromFile will load virtually any certificate format file.
    -- It will auto-recognize the format and load appropiately.
    -- A base64-encoded DER X.509 certificate file
    -- contains the binary DER in base64 encoded form.
    -- Therefore, it is a text file that will begin like this:
    -- -----BEGIN CERTIFICATE-----
    -- MIIFbjCCBFagAwIBAgIGdDPNv/L0MA0GCSqGSIb3DQEBBQUAMIHKMQswCQYDVQQG
    -- EwJVUzEQMA4GA1UECBMHQXJpem9uYTETMBEGA1UEBxMKU2NvdHRzZGFsZTEaMBgG
    -- ...

    EXEC sp_OAMethod @cert, 'LoadFromFile', @success OUT, '/Users/chilkat/testData/cer/chilkat_base64.cer'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @cert, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @cert
        RETURN
      END

    -- DN = "Distinguished Name"

    EXEC sp_OAGetProperty @cert, 'SubjectDN', @sTmp0 OUT
    PRINT 'SubjectDN:' + @sTmp0


    EXEC sp_OAGetProperty @cert, 'SubjectCN', @sTmp0 OUT
    PRINT 'Common Name:' + @sTmp0

    EXEC sp_OAGetProperty @cert, 'IssuerCN', @sTmp0 OUT
    PRINT 'Issuer Common Name:' + @sTmp0


    EXEC sp_OAGetProperty @cert, 'SerialNumber', @sTmp0 OUT
    PRINT 'Serial Number:' + @sTmp0

    EXEC @hr = sp_OADestroy @cert


END
GO