Sample code for 30+ languages & platforms
SQL Server

SSH Authentication using X.509 Certificates

See more SSH Examples

Demonstrates authenticating with an SSH/SFTP server using the private key of an X.509 certificate. The certificate and key are loaded from a .pfx, the private key is exported as PEM, and that key is used for public-key authentication. See X.509v3 Certificates for SSH Authentication for more information.

Note: The file paths are relative to the application's current working directory. Supply the paths to your own files.

Background: This is not X.509 certificate authentication in the SSH-protocol sense — the server still performs ordinary public-key authentication. The certificate is simply a convenient container for a key pair your organization already manages through its PKI, so the same credential issued for other purposes can be reused for SSH. What the server needs installed is the corresponding public key, exactly as with any other key. Chilkat can also load such certificates from smart cards and USB tokens rather than a file.

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 @iTmp0 int
    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.

    --  Demonstrates authenticating with an SSH/SFTP server using the private key of an X.509
    --  certificate.

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

    DECLARE @hostname nvarchar(4000)
    SELECT @hostname = 'ssh.example.com'
    DECLARE @port int
    SELECT @port = 22
    EXEC sp_OAMethod @ssh, 'Connect', @success OUT, @hostname, @port
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @ssh
        RETURN
      END

    --  Load the certificate and its private key from a .pfx file.  The PFX password should come
    --  from a secure source rather than being hard-coded.
    --  Note: Chilkat can load certificates and private keys from many sources, including smart
    --  cards and USB tokens (HSMs).
    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/pfx/example.pfx', @pfxPassword
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @cert, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @ssh
        EXEC @hr = sp_OADestroy @cert
        RETURN
      END

    --  Get the certificate's private key as PEM, to be used for SSH authentication.  The
    --  corresponding public key is installed on the server.
    DECLARE @privKeyPem nvarchar(4000)
    EXEC sp_OAMethod @cert, 'GetPrivateKeyPem', @privKeyPem OUT
    EXEC sp_OAGetProperty @cert, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @cert, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @ssh
        EXEC @hr = sp_OADestroy @cert
        RETURN
      END

    DECLARE @key int
    EXEC @hr = sp_OACreate 'Chilkat.SshKey', @key OUT

    EXEC sp_OAMethod @key, 'FromOpenSshPrivateKey', @success OUT, @privKeyPem
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @key, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @ssh
        EXEC @hr = sp_OADestroy @cert
        EXEC @hr = sp_OADestroy @key
        RETURN
      END

    EXEC sp_OAMethod @ssh, 'AuthenticatePk', @success OUT, 'mySshLogin', @key
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @ssh
        EXEC @hr = sp_OADestroy @cert
        EXEC @hr = sp_OADestroy @key
        RETURN
      END


    PRINT 'Public-key authentication successful.'

    EXEC sp_OAMethod @ssh, 'Disconnect', NULL

    EXEC @hr = sp_OADestroy @ssh
    EXEC @hr = sp_OADestroy @cert
    EXEC @hr = sp_OADestroy @key


END
GO