Sample code for 30+ languages & platforms
SQL Server

Get a Received Client Certificate (Server Side)

See more Socket/SSL/TLS Examples

Demonstrates Socket.GetRcvdClientCert, which copies a client certificate presented during the TLS handshake of an accepted server-side connection into a Cert object.

The file path is relative to the application's current working directory. An absolute path may also be used. Supply the path appropriate to your own environment.

Background. Use NumReceivedClientCerts to determine the valid index range. Client certificates are supplied only when the server requests them for mutual TLS.

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 @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

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

    --  The file path is relative to the application's current working directory.  An absolute path
    --  may also be used.  Supply the path appropriate to your own environment.
    --  The PFX password should come from a secure source rather than being hard-coded.
    DECLARE @pfxPassword nvarchar(4000)
    SELECT @pfxPassword = 'myPfxPassword'
    DECLARE @cert int
    EXEC @hr = sp_OACreate 'Chilkat.Cert', @cert OUT

    EXEC sp_OAMethod @cert, 'LoadPfxFile', @success OUT, 'qa_data/server.pfx', @pfxPassword
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @cert, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @socket
        EXEC @hr = sp_OADestroy @cert
        RETURN
      END

    EXEC sp_OAMethod @socket, 'InitSslServer', @success OUT, @cert
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @socket, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @socket
        EXEC @hr = sp_OADestroy @cert
        RETURN
      END
    EXEC sp_OAMethod @socket, 'BindAndListen', @success OUT, 5000, 25
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @socket, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @socket
        EXEC @hr = sp_OADestroy @cert
        RETURN
      END

    --  Accept a client connection.  The accepted connection carries any client certificate presented
    --  during the TLS handshake.
    DECLARE @connectedSock int
    EXEC @hr = sp_OACreate 'Chilkat.Socket', @connectedSock OUT

    EXEC sp_OAMethod @socket, 'AcceptNext', @success OUT, 20000, @connectedSock
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @socket, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @socket
        EXEC @hr = sp_OADestroy @cert
        EXEC @hr = sp_OADestroy @connectedSock
        RETURN
      END

    --  Copy each received client certificate and inspect it.
    DECLARE @numClientCerts int
    EXEC sp_OAGetProperty @connectedSock, 'NumReceivedClientCerts', @numClientCerts OUT
    DECLARE @clientCert int
    EXEC @hr = sp_OACreate 'Chilkat.Cert', @clientCert OUT

    DECLARE @i int

    SELECT @i = 0
    WHILE @i <= @numClientCerts - 1
      BEGIN
        EXEC sp_OAMethod @connectedSock, 'GetRcvdClientCert', @success OUT, @i, @clientCert
        IF @success = 0
          BEGIN
            EXEC sp_OAGetProperty @connectedSock, 'LastErrorText', @sTmp0 OUT
            PRINT @sTmp0
            EXEC @hr = sp_OADestroy @socket
            EXEC @hr = sp_OADestroy @cert
            EXEC @hr = sp_OADestroy @connectedSock
            EXEC @hr = sp_OADestroy @clientCert
            RETURN
          END

        EXEC sp_OAGetProperty @clientCert, 'SubjectCN', @sTmp0 OUT
        PRINT 'Client certificate subject: ' + @sTmp0
        SELECT @i = @i + 1
      END

    EXEC @hr = sp_OADestroy @socket
    EXEC @hr = sp_OADestroy @cert
    EXEC @hr = sp_OADestroy @connectedSock
    EXEC @hr = sp_OADestroy @clientCert


END
GO