SQL Server
SQL Server
Configure a Socket for Server-Side TLS
See more Socket/SSL/TLS Examples
Demonstrates Socket.InitSslServer, which configures the object for server-side TLS using a server certificate that has access to its private key, then listens for and accepts a TLS connection.
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. A TLS listener presents the configured server certificate during the handshake. When client certificates are required, acceptable CA distinguished names are added before InitSslServer.
Chilkat SQL Server Downloads
-- 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
-- Configure this object for server-side TLS using the certificate. The certificate must have
-- access to its corresponding private key.
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
-- Listen for connections. Accepted connections will use TLS.
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
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
PRINT 'Accepted a TLS client connection.'
EXEC @hr = sp_OADestroy @socket
EXEC @hr = sp_OADestroy @cert
EXEC @hr = sp_OADestroy @connectedSock
END
GO