PureBasic
PureBasic
Verify and Pin the FTPS Server Certificate
See more FTP Examples
Demonstrates the certificate-verification properties RequireSslCertVerify, TlsPinSet, and the read-only SslServerCertVerified.
Background: By default a TLS connection is not rejected merely because certificate-chain verification fails, so
RequireSslCertVerify = true is the important hardening step — it makes an expired, unsigned, or untrusted certificate abort the connection. TlsPinSet adds public-key pinning on top: the handshake fails unless the server's SPKI fingerprint matches one you configured, defending against a mis-issued certificate that would otherwise validate. Pinning supplements verification rather than replacing it. SslServerCertVerified reports the outcome.Chilkat PureBasic Downloads
IncludeFile "CkFtp2.pb"
Procedure ChilkatExample()
success.i = 0
ftp.i = CkFtp2::ckCreate()
If ftp.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkFtp2::setCkHostname(ftp, "ftp.example.com")
CkFtp2::setCkUsername(ftp, "myFtpLogin")
CkFtp2::setCkAuthTls(ftp, 1)
; Normally you would not hard-code the password in source. You should instead obtain it
; from an interactive prompt, environment variable, or a secrets vault.
CkFtp2::setCkPassword(ftp, "myPassword")
; Reject the connection if the server certificate cannot be verified (expired, bad signature,
; untrusted chain, etc.). The default is 0, which does not reject on verification
; failure -- set 1 for security.
CkFtp2::setCkRequireSslCertVerify(ftp, 1)
; Optionally pin the server's public key. If none of the configured SPKI fingerprints matches,
; the TLS handshake fails. Pinning supplements normal verification; it does not replace it.
CkFtp2::setCkTlsPinSet(ftp, "sha256//YLh1dUR9y6Kja30RrAn7JKnbQG/uEtLMkBgFF2Fuihg=")
success = CkFtp2::ckConnect(ftp)
If success = 0
Debug CkFtp2::ckLastErrorText(ftp)
CkFtp2::ckDispose(ftp)
ProcedureReturn
EndIf
; SslServerCertVerified reports whether the certificate chain was successfully verified.
If CkFtp2::ckSslServerCertVerified(ftp)
Debug "The server certificate was verified."
EndIf
success = CkFtp2::ckDisconnect(ftp)
If success = 0
Debug CkFtp2::ckLastErrorText(ftp)
CkFtp2::ckDispose(ftp)
ProcedureReturn
EndIf
CkFtp2::ckDispose(ftp)
ProcedureReturn
EndProcedure