Sample code for 30+ languages & platforms
SQL Server

SSH Auth Failure Reason (AuthenticatePwPk)

See more SSH Examples

Demonstrates how to determine why authentication failed when a server requires both a password and a private key. If AuthenticatePwPk fails, GetLastJsonData returns diagnostic JSON whose authResult and authFailReason members reveal which factor was at fault.

Background: With multi-factor SSH, a plain failure is ambiguous — the key could be wrong, the password could be wrong, or both. The diagnostic JSON resolves that: it reports Key is incorrect when the key fails, or a partialAuthResult showing the key succeeded before Password is incorrect. Note the ordering consequence: if the key is rejected, authentication never reaches the password check, so you cannot yet know whether the password is also wrong. Passwords and passphrases are never included in this JSON, so it is safe to log.

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 how to determine why authentication failed when a server requires BOTH a
    --  password and a private key.  Was the private key wrong, or the password?

    --  Load the private key used for authentication.
    DECLARE @key int
    EXEC @hr = sp_OACreate 'Chilkat.SshKey', @key OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    --  The key password is not hard-coded in a real application; obtain it from a secure source.
    EXEC sp_OASetProperty @key, 'Password', 'myKeyPassword'

    DECLARE @privKeyText nvarchar(4000)
    EXEC sp_OAMethod @key, 'LoadText', @privKeyText OUT, 'qa_data/my_private_key_file'
    EXEC sp_OAGetProperty @key, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @key, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @key
        RETURN
      END

    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 @key
        RETURN
      END

    DECLARE @ssh int
    EXEC @hr = sp_OACreate 'Chilkat.Ssh', @ssh OUT

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

    --  Normally you would not hard-code the password in source.  You should instead obtain it
    --  from an interactive prompt, environment variable, or a secrets vault.
    DECLARE @password nvarchar(4000)
    SELECT @password = 'mySshPassword'

    --  Authenticate using both a password and a private key.
    EXEC sp_OAMethod @ssh, 'AuthenticatePwPk', @success OUT, 'mySshLogin', @password, @key
    IF @success
      BEGIN

        PRINT 'Authentication is successful!'
        EXEC @hr = sp_OADestroy @key
        EXEC @hr = sp_OADestroy @ssh
        RETURN
      END

    --  Authentication failed.  The diagnostic JSON explains which factor was at fault.
    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT

    EXEC sp_OAMethod @ssh, 'GetLastJsonData', NULL, @json
    EXEC sp_OASetProperty @json, 'EmitCompact', 0

    --  If the key is correct but the password is wrong:
    --  {
    --    "public_key_type": "rsa",
    --    "partialAuthResult": "publickey success. continue to authenticate with password...",
    --    "authResult": "failed",
    --    "authFailReason": "Password is incorrect"
    --  }
    --  
    --  If the key is incorrect.  Whether the password is also wrong is unknown, because
    --  authentication never got far enough to check it:
    --  {
    --    "public_key_type": "rsa",
    --    "authResult": "failed",
    --    "authFailReason": "Key is incorrect"
    --  }


    EXEC sp_OAMethod @json, 'StringOf', @sTmp0 OUT, 'authResult'
    PRINT 'authResult: ' + @sTmp0

    EXEC sp_OAMethod @json, 'StringOf', @sTmp0 OUT, 'authFailReason'
    PRINT 'authFailReason: ' + @sTmp0

    EXEC @hr = sp_OADestroy @key
    EXEC @hr = sp_OADestroy @ssh
    EXEC @hr = sp_OADestroy @json


END
GO