Sample code for 30+ languages & platforms
Xbase++ Requires Chilkat v11.0.0+

SFTP Authentication using an SSH Certificate

See more SFTP Examples

Demonstrates how to SFTP authenticate using an SSH certificate.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oSbSshCert
LOCAL oSbPrivKey
LOCAL oKey
LOCAL oSftp
LOCAL cHostname
LOCAL nPort

nSuccess := 0

//  This example assumes the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

oSbSshCert := CreateObject("Chilkat.StringBuilder")
nSuccess := oSbSshCert:LoadFile("qa_data/sshCert/user_ecdsa_key-cert.pub", "utf-8")
IF (nSuccess == 0)
    ? "Failed to load user_ecdsa_key-cert.pub"
    oSbSshCert:destroy()
    RETURN
ENDIF

oSbPrivKey := CreateObject("Chilkat.StringBuilder")
nSuccess := oSbPrivKey:LoadFile("qa_data/sshKeys/user_ecdsa_key", "utf-8")
IF (nSuccess == 0)
    ? "Failed to load user_ecdsa_key"
    oSbSshCert:destroy()
    oSbPrivKey:destroy()
    RETURN
ENDIF

oKey := CreateObject("Chilkat.SshKey")
//  Provide the password if the user_ecdsa_key is stored in an encrypted format.
oKey:Password := "secret"
nSuccess := oKey:FromOpenSshPrivateKey(oSbPrivKey:GetAsString())
IF (nSuccess == 0)
    ? oKey:LastErrorText
    oSbSshCert:destroy()
    oSbPrivKey:destroy()
    oKey:destroy()
    RETURN
ENDIF

//  Indicate that the SSH certificate is to be used for authentication.
//  The UseSshCertificate method was added in Chilkat v11.0.0
oKey:UseSshCertificate(oSbSshCert:GetAsString())

oSftp := CreateObject("Chilkat.SFtp")

cHostname := "sftp.example.com"
nPort := 22
nSuccess := oSftp:Connect(cHostname, nPort)
IF (nSuccess != 1)
    ? oSftp:LastErrorText
    oSbSshCert:destroy()
    oSbPrivKey:destroy()
    oKey:destroy()
    oSftp:destroy()
    RETURN
ENDIF

nSuccess := oSftp:AuthenticatePk("myLogin", oKey)
IF (nSuccess != 1)
    ? oSftp:LastErrorText
    oSbSshCert:destroy()
    oSbPrivKey:destroy()
    oKey:destroy()
    oSftp:destroy()
    RETURN
ENDIF

? "Public-Key Authentication using an SSH Certificate was Successful!"

oSbSshCert:destroy()
oSbPrivKey:destroy()
oKey:destroy()
oSftp:destroy()