Sample code for 30+ languages & platforms
Lianja

SFTP Authentication using an SSH Certificate

See more SFTP Examples

Demonstrates how to SFTP authenticate using an SSH certificate.

Chilkat Lianja Downloads

Lianja
llSuccess = .F.

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

loSbSshCert = createobject("CkStringBuilder")
llSuccess = loSbSshCert.LoadFile("qa_data/sshCert/user_ecdsa_key-cert.pub","utf-8")
if (llSuccess = .F.) then
    ? "Failed to load user_ecdsa_key-cert.pub"
    release loSbSshCert
    return
endif

loSbPrivKey = createobject("CkStringBuilder")
llSuccess = loSbPrivKey.LoadFile("qa_data/sshKeys/user_ecdsa_key","utf-8")
if (llSuccess = .F.) then
    ? "Failed to load user_ecdsa_key"
    release loSbSshCert
    release loSbPrivKey
    return
endif

loKey = createobject("CkSshKey")
// Provide the password if the user_ecdsa_key is stored in an encrypted format.
loKey.Password = "secret"
llSuccess = loKey.FromOpenSshPrivateKey(loSbPrivKey.GetAsString())
if (llSuccess = .F.) then
    ? loKey.LastErrorText
    release loSbSshCert
    release loSbPrivKey
    release loKey
    return
endif

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

loSftp = createobject("CkSFtp")

lcHostname = "sftp.example.com"
lnPort = 22
llSuccess = loSftp.Connect(lcHostname,lnPort)
if (llSuccess <> .T.) then
    ? loSftp.LastErrorText
    release loSbSshCert
    release loSbPrivKey
    release loKey
    release loSftp
    return
endif

llSuccess = loSftp.AuthenticatePk("myLogin",loKey)
if (llSuccess <> .T.) then
    ? loSftp.LastErrorText
    release loSbSshCert
    release loSbPrivKey
    release loKey
    release loSftp
    return
endif

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


release loSbSshCert
release loSbPrivKey
release loKey
release loSftp