Xbase++ Requires Chilkat v11.0.0+
Xbase++
SFTP use Cert's Private Key for Authentication (Windows)
See more SFTP Examples
Demonstrates how to use the private key of a pre-installed certificate (on Windows) for SFTP authentication. The certificate's private key must be marked as "exportable" when originally installed.Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oCert
LOCAL oPrivKey
LOCAL cPrivKeyPem
LOCAL oSshKey
LOCAL oSftp
nSuccess := 0
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
oCert := CreateObject("Chilkat.Cert")
// Load the certificate from the Windows certificate store
nSuccess := oCert:LoadByCommonName("my_cert_common_name")
IF (nSuccess == 0)
? oCert:LastErrorText
oCert:destroy()
RETURN
ENDIF
// Get the private key.
oPrivKey := CreateObject("Chilkat.PrivateKey")
nSuccess := oCert:GetPrivateKey(oPrivKey)
IF (nSuccess == 0)
? oCert:LastErrorText
oCert:destroy()
oPrivKey:destroy()
RETURN
ENDIF
cPrivKeyPem := oPrivKey:GetPkcs8Pem()
IF (oPrivKey:LastMethodSuccess == 0)
? oPrivKey:LastErrorText
oCert:destroy()
oPrivKey:destroy()
RETURN
ENDIF
oSshKey := CreateObject("Chilkat.SshKey")
nSuccess := oSshKey:FromOpenSshPrivateKey(cPrivKeyPem)
IF (nSuccess == 0)
? oSshKey:LastErrorText
oCert:destroy()
oPrivKey:destroy()
oSshKey:destroy()
RETURN
ENDIF
// Connect to an SSH/SFTP server
oSftp := CreateObject("Chilkat.SFtp")
nSuccess := oSftp:Connect("sftp.example.com", 22)
IF (nSuccess == 0)
? oSftp:LastErrorText
oCert:destroy()
oPrivKey:destroy()
oSshKey:destroy()
oSftp:destroy()
RETURN
ENDIF
// Authenticate with the SSH server using a username + private key.
// (The private key serves as the password. The username identifies
// the SSH user account on the server.)
nSuccess := oSftp:AuthenticatePk("mySshLogin", oSshKey)
IF (nSuccess == 0)
? oSftp:LastErrorText
oCert:destroy()
oPrivKey:destroy()
oSshKey:destroy()
oSftp:destroy()
RETURN
ENDIF
? "OK, the connection and authentication with the SSH server is completed."
// This example is only to show the connection + authentication using a private key associated with a certificate in the Windows certificate store...
oCert:destroy()
oPrivKey:destroy()
oSshKey:destroy()
oSftp:destroy()