PowerBuilder
PowerBuilder
HTTP Public Key Pinning
See more HTTP Examples
Demonstrates how to specify a TLS pinset that lists the pre-known valid and accepted TLS server certificate public keys. When a TLS pinset is specified, the Chilkat TLS client software will reject TLS connections (inside the TLS handshake) when the server provides a certificate having a public key not listed in the pinset. This makes it possible to reject the connection at the earliest possible time, before any information (such as the HTTP request) has been sent to the server.Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_HttpA
oleobject loo_SslCert
oleobject loo_HttpB
string ls_Html
li_Success = 0
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
loo_HttpA = create oleobject
li_rc = loo_HttpA.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
destroy loo_HttpA
MessageBox("Error","Connecting to COM object failed")
return
end if
// To do public key pinning, the SPKI fingerprint would be obtained beforehand -- perhaps
// at design time, or possibly at the time of the 1st connection (where the SPKI fingerprint
// is persisted for future use). Note: "If the certificate or public key is added upon first
// encounter, you will be using key continuity. Key continuity can fail if the attacker has a
// privileged position during the first first encounter."
// See https://www.owasp.org/index.php/Certificate_and_Public_Key_Pinning
loo_SslCert = create oleobject
li_rc = loo_SslCert.ConnectToNewObject("Chilkat.Cert")
li_Success = loo_HttpA.GetServerCert("www.ssllabs.com",443,loo_SslCert)
if li_Success = 0 then
Write-Debug loo_HttpA.LastErrorText
destroy loo_HttpA
destroy loo_SslCert
return
end if
// The GetSpkiFingerprint method returns the SPKI Fingerprint suitable for use in pinning.
Write-Debug "SPKI Fingerprint: " + loo_SslCert.GetSpkiFingerprint("sha256","base64")
// ------------------------------------------------------------------------------------
loo_HttpB = create oleobject
li_rc = loo_HttpB.ConnectToNewObject("Chilkat.Http")
// Set the TlsPinSet. The format of the TlsPinSet string is:
// "hashAlg, encoding, fingerprint1, fingerprint2, ..."
loo_HttpB.TlsPinSet = "sha256, base64, zVYucUTcGEk/8/HHt9ifInCRXVAf+hbxTgRTYnCjYk8="
// Our object will refuse to communicate with any TLS server where the server's public key
// does not match a pin in the pinset.
// This should be OK (assuming the ssllabs.com server certificate has not changed since
// the time of writing this example).
ls_Html = loo_HttpB.QuickGetStr("https://www.ssllabs.com/")
if loo_HttpB.LastMethodSuccess = 0 then
Write-Debug loo_HttpB.LastErrorText
destroy loo_HttpA
destroy loo_SslCert
destroy loo_HttpB
return
end if
Write-Debug "Success. The HTTP GET worked because the server's certificate had a matching public key."
// This should NOT be OK because owasp.org's server certificate will not have a matching public key.
ls_Html = loo_HttpB.QuickGetStr("https://www.owasp.org/index.php/Certificate_and_Public_Key_Pinning")
if loo_HttpB.LastMethodSuccess = 0 then
Write-Debug "Good, this connection was rejected..."
else
Write-Debug "This was not supposed to happen!"
end if
destroy loo_HttpA
destroy loo_SslCert
destroy loo_HttpB