Sample code for 30+ languages & platforms
SQL Server

Use a Custom Set of Trusted Root Certificates

See more Certificates Examples

Demonstrates how to build a set of trusted root certificates to be used globally by all Chilkat classes.

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

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

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

    --  Indicate that we will NOT trust any pre-installed certificates on the system.
    EXEC sp_OASetProperty @trustedRoots, 'TrustSystemCaRoots', 0

    -- Thawte is a certificate authority that provides a .zip download of their
    -- root CA certificates:  https://www.thawte.com/roots/index.html
    -- The direct download link is: https://www.verisign.com/support/thawte-roots.zip
    -- Note: The above URLs are valid at the time of writing this example (29-May-2015).

    -- Assuming the .zip has already been downloaded, open it and load each .pem file into
    -- our trusted roots object.
    DECLARE @zip int
    EXEC @hr = sp_OACreate 'Chilkat.Zip', @zip OUT

    -- Open a .zip containing PEM files, among other things..
    EXEC sp_OAMethod @zip, 'OpenZip', @success OUT, 'qa_data/certs/thawte-roots.zip'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @zip, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @trustedRoots
        EXEC @hr = sp_OADestroy @zip
        RETURN
      END

    DECLARE @entry int
    EXEC @hr = sp_OACreate 'Chilkat.ZipEntry', @entry OUT

    DECLARE @pemStr nvarchar(4000)

    DECLARE @cert int
    EXEC @hr = sp_OACreate 'Chilkat.Cert', @cert OUT

    DECLARE @pattern nvarchar(4000)
    SELECT @pattern = '*.pem'
    DECLARE @bHasMoreEntries int
    EXEC sp_OAMethod @zip, 'EntryMatching', @bHasMoreEntries OUT, @pattern, @entry
    WHILE @bHasMoreEntries = 1
      BEGIN


        EXEC sp_OAGetProperty @entry, 'FileName', @sTmp0 OUT
        PRINT 'Entry: ' + @sTmp0

        -- Get the PEM of the CA cert:
        EXEC sp_OAMethod @entry, 'UnzipToString', @pemStr OUT, 0, 'utf-8'

        -- Load it into a certificate object:
        EXEC sp_OAMethod @cert, 'LoadPem', @success OUT, @pemStr
        IF @success <> 1
          BEGIN
            EXEC sp_OAGetProperty @cert, 'LastErrorText', @sTmp0 OUT
            PRINT @sTmp0
          END

        -- Add it to the trusted roots.
        EXEC sp_OAMethod @trustedRoots, 'AddCert', @success OUT, @cert

        EXEC sp_OAMethod @entry, 'GetNextMatch', @bHasMoreEntries OUT, @pattern
      END

    --  Activate the trusted roots globally for all Chilkat objects.
    --  This call really shouldn't fail, so we're not checking the return value.
    EXEC sp_OAMethod @trustedRoots, 'Activate', @success OUT

    EXEC @hr = sp_OADestroy @trustedRoots
    EXEC @hr = sp_OADestroy @zip
    EXEC @hr = sp_OADestroy @entry
    EXEC @hr = sp_OADestroy @cert


END
GO