SQL Server
SQL Server
FTPS with Mutual TLS Authentication (TLS Client Certificate)
See more FTP Examples
Demonstrates how to do mutual TLS authentication (using a client certificate from a .pfx/.p12).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
-- 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 @ftp int
EXEC @hr = sp_OACreate 'Chilkat.Ftp2', @ftp OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
EXEC sp_OASetProperty @ftp, 'Hostname', 'ftp.example.com'
EXEC sp_OASetProperty @ftp, 'Port', 21
-- If using implicit TLS, you probably want port 990..
EXEC sp_OASetProperty @ftp, 'Port', 990
-- Set this to 0 for implicit TLS, otherwise set to 1 for explicit TLS (where port is typically 21).
EXEC sp_OASetProperty @ftp, 'AuthTls', 0
-- Set this to 1 for implicit TLS, otherwise set to 0.
EXEC sp_OASetProperty @ftp, 'Ssl', 1
DECLARE @cert int
EXEC @hr = sp_OACreate 'Chilkat.Cert', @cert OUT
EXEC sp_OAMethod @cert, 'LoadPfxFile', @success OUT, 'qa_data/pfx/example.pfx', 'pfx_password'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @cert, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @ftp
EXEC @hr = sp_OADestroy @cert
RETURN
END
-- Use this certificate for our TLS mutually authenticated connection:
EXEC sp_OAMethod @ftp, 'SetSslClientCert', @success OUT, @cert
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @cert, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @ftp
EXEC @hr = sp_OADestroy @cert
RETURN
END
-- Establish the TLS connection with the FTP server.
EXEC sp_OAMethod @ftp, 'ConnectOnly', @success OUT
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @ftp, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @ftp
EXEC @hr = sp_OADestroy @cert
RETURN
END
-- If a login is required, then login with the FTP account login/password.
EXEC sp_OASetProperty @ftp, 'Username', 'myLogin'
EXEC sp_OASetProperty @ftp, 'Password', 'myPassword'
EXEC sp_OAMethod @ftp, 'LoginAfterConnectOnly', @success OUT
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @ftp, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @ftp
EXEC @hr = sp_OADestroy @cert
RETURN
END
-- Do whatever you're doing to do ...
-- upload files, download files, etc...
-- .....
-- .....
EXEC sp_OAMethod @ftp, 'Disconnect', @success OUT
EXEC @hr = sp_OADestroy @ftp
EXEC @hr = sp_OADestroy @cert
END
GO