Xbase++ Requires Chilkat v11.0.0+
Xbase++
SFTP use Cert's Private Key from PFX (.pfx/.p12)
See more SFTP Examples
Demonstrates how to use the private key associated with a certificate from a .pfx/.p12 file.Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oCert
LOCAL cPfxFilepath
LOCAL cPfxPassword
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")
cPfxFilepath := "qa_data/pfx/my.pfx"
cPfxPassword := "secret"
// A PFX typically contains certificates in the chain of authentication.
// The Chilkat cert object will choose the certificate w/
// private key farthest from the root authority cert.
// To access all the certificates in a PFX, use the
// Chilkat certificate store object instead.
nSuccess := oCert:LoadPfxFile(cPfxFilepath, cPfxPassword)
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()