Sample code for 30+ languages & platforms
Lianja

SSH Authentication using an SSH Certificate

See more SSH Examples

Demonstrates how to 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())

loSsh = createobject("CkSsh")

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

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

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


release loSbSshCert
release loSbPrivKey
release loKey
release loSsh