![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Visual FoxPro) HTTP Public Key PinningDemonstrates 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. Note: This example requires Chilkat v11.0.0 or greater.
LOCAL lnSuccess LOCAL loHttpA LOCAL loSslCert LOCAL loHttpB LOCAL lcHtml lnSuccess = 0 * This example requires the Chilkat API to have been previously unlocked. * See Global Unlock Sample for sample code. loHttpA = CreateObject('Chilkat.Http') * 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 loSslCert = CreateObject('Chilkat.Cert') lnSuccess = loHttpA.GetServerCert("www.ssllabs.com",443,loSslCert) IF (lnSuccess = 0) THEN ? loHttpA.LastErrorText RELEASE loHttpA RELEASE loSslCert CANCEL ENDIF * The GetSpkiFingerprint method returns the SPKI Fingerprint suitable for use in pinning. ? "SPKI Fingerprint: " + loSslCert.GetSpkiFingerprint("sha256","base64") * ------------------------------------------------------------------------------------ loHttpB = CreateObject('Chilkat.Http') * Set the TlsPinSet. The format of the TlsPinSet string is: * "hashAlg, encoding, fingerprint1, fingerprint2, ..." loHttpB.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). lcHtml = loHttpB.QuickGetStr("https://www.ssllabs.com/") IF (loHttpB.LastMethodSuccess = 0) THEN ? loHttpB.LastErrorText RELEASE loHttpA RELEASE loSslCert RELEASE loHttpB CANCEL ENDIF ? "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. lcHtml = loHttpB.QuickGetStr("https://www.owasp.org/index.php/Certificate_and_Public_Key_Pinning") IF (loHttpB.LastMethodSuccess = 0) THEN ? "Good, this connection was rejected..." ELSE ? "This was not supposed to happen!" ENDIF RELEASE loHttpA RELEASE loSslCert RELEASE loHttpB |
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.