SQL Server
SQL Server
SSH Authentication using an SSH Certificate
See more SSH Examples
Demonstrates authenticating with an SSH server using an SSH certificate. The private key is imported into an SshKey object, UseSshCertificate attaches the certificate, and AuthenticatePk performs the authentication. See Understanding SSH Certificates for background.
Note: The file paths are relative to the application's current working directory. Supply the paths to your own files.
Background: An SSH certificate is a public key signed by a certificate authority, and it solves the scaling problem of ordinary public-key authentication: instead of copying every user's public key into
authorized_keys on every server, each server simply trusts the CA. Certificates also carry an expiration, so access lapses automatically rather than lingering until someone remembers to remove a key. Note this is distinct from X.509 certificates — the SSH certificate format is its own thing.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
-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
-- Demonstrates authenticating with an SSH server using an SSH certificate. An SSH certificate
-- is a signed public key: the server trusts the certificate authority that signed it, rather
-- than holding a copy of each user's public key.
DECLARE @sbSshCert int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbSshCert OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
EXEC sp_OAMethod @sbSshCert, 'LoadFile', @success OUT, 'qa_data/sshCert/user_ecdsa_key-cert.pub', 'utf-8'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @sbSshCert, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @sbSshCert
RETURN
END
DECLARE @sbPrivKey int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbPrivKey OUT
EXEC sp_OAMethod @sbPrivKey, 'LoadFile', @success OUT, 'qa_data/sshKeys/user_ecdsa_key', 'utf-8'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @sbPrivKey, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @sbSshCert
EXEC @hr = sp_OADestroy @sbPrivKey
RETURN
END
DECLARE @key int
EXEC @hr = sp_OACreate 'Chilkat.SshKey', @key OUT
-- Set the password if the private key file is stored encrypted. This should come from a
-- secure source rather than being hard-coded.
EXEC sp_OASetProperty @key, 'Password', 'myKeyPassword'
DECLARE @privKeyText nvarchar(4000)
EXEC sp_OAMethod @sbPrivKey, 'GetAsString', @privKeyText OUT
EXEC sp_OAMethod @key, 'FromOpenSshPrivateKey', @success OUT, @privKeyText
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @key, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @sbSshCert
EXEC @hr = sp_OADestroy @sbPrivKey
EXEC @hr = sp_OADestroy @key
RETURN
END
-- Indicate that the SSH certificate is to be used for authentication.
DECLARE @sshCertText nvarchar(4000)
EXEC sp_OAMethod @sbSshCert, 'GetAsString', @sshCertText OUT
EXEC sp_OAMethod @key, 'UseSshCertificate', @success OUT, @sshCertText
DECLARE @ssh int
EXEC @hr = sp_OACreate 'Chilkat.Ssh', @ssh OUT
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 @sbSshCert
EXEC @hr = sp_OADestroy @sbPrivKey
EXEC @hr = sp_OADestroy @key
EXEC @hr = sp_OADestroy @ssh
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 @sbSshCert
EXEC @hr = sp_OADestroy @sbPrivKey
EXEC @hr = sp_OADestroy @key
EXEC @hr = sp_OADestroy @ssh
RETURN
END
PRINT 'Public-key authentication using an SSH certificate was successful.'
EXEC sp_OAMethod @ssh, 'Disconnect', NULL
EXEC @hr = sp_OADestroy @sbSshCert
EXEC @hr = sp_OADestroy @sbPrivKey
EXEC @hr = sp_OADestroy @key
EXEC @hr = sp_OADestroy @ssh
END
GO