SQL Server
SQL Server
Get the Server Certificate from a TLS Connection
See more Socket/SSL/TLS Examples
Demonstrates Socket.GetServerCert, which copies the certificate presented by the remote TLS server into a Cert object for inspection.
Background. The method returns false when the socket is not connected, is not using TLS, or no server certificate is available. Inspecting the certificate lets an application validate the server's identity.
Chilkat SQL Server Downloads
--
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
-- Connect to the server using TLS.
DECLARE @bTls int
SELECT @bTls = 1
DECLARE @maxWaitMs int
SELECT @maxWaitMs = 5000
EXEC sp_OAMethod @socket, 'Connect', @success OUT, 'example.com', 5000, @bTls, @maxWaitMs
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @socket, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @socket
RETURN
END
-- Copy the certificate presented by the remote TLS server, then inspect it. This can be used to
-- validate the server's identity.
DECLARE @cert int
EXEC @hr = sp_OACreate 'Chilkat.Cert', @cert OUT
EXEC sp_OAMethod @socket, 'GetServerCert', @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_OAGetProperty @cert, 'SubjectCN', @sTmp0 OUT
PRINT 'Server certificate subject: ' + @sTmp0
EXEC sp_OAGetProperty @cert, 'IssuerCN', @sTmp0 OUT
PRINT 'Issued by: ' + @sTmp0
EXEC @hr = sp_OADestroy @socket
EXEC @hr = sp_OADestroy @cert
END
GO