Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3/4TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

DataFlex Examples

ASN.1
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Base64
Bounced Email
Box
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Email Object
Encryption
FTP
Facebook
FileAccess
Firebase
GMail REST API
GeoOp
Google APIs
Google Calendar
Google Cloud Storage
Google Drive
Google Sheets
Gzip
HTML-to-XML/Text
HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)

Java KeyStore (JKS)
Jira
MHT / HTML Email
MIME
Microsoft Graph
NTLM
OAuth1
OAuth2
OneDrive
OpenSSL
Outlook
PEM
PFX/P12
POP3
PRNG
PayPal
Peoplevox
QuickBooks
REST
REST Misc
RSA Encryption
SCP
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
SharePoint
Shopify
Socket/SSL/TLS
Spider
Stream
Stripe
SugarCRM
Tar Archive
Twitter
Upload
VoiceBase
Walmart
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Xero
Zip
curl
eBay

 

 

 

(DataFlex) HTTP Public Key Pinning

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.

What is Pinning?

Pinning is the process of associating a host with their expected X509 certificate or public key. Once a certificate or public key is known or seen for a host, the certificate or public key is associated or 'pinned' to the host. If more than one certificate or public key is acceptable, then the program holds a pinset. In this case, the advertised identity must match one of the elements in the pinset. A host or service's certificate or public key can be added to an application at development time, or it can be added upon first encountering the certificate or public key. The former - adding at development time - is preferred since preloading the certificate or public key out of band usually means the attacker cannot taint the pin. ..

Beginning in Chilkat v9.5.0.55, TLS public key pinning is implemented by (1) making it possible to easily get the SPKI fingerprint of a certificate, and (2) adding the TlsPinSet property to classes that can establish TLS connections.

Get SPKI Fingerprint using OpenSSL

This example shows how Chilkat can be used to obtain the server's public key SPKI fingerprint. OpenSSL can also get the fingerprint with these two commands (shown for the domain ssllabs.com) :

openssl x509 -noout -in ssllabs.com.pem -pubkey | openssl asn1parse -noout -inform pem -out ssllabs.com.key
openssl dgst -sha256 -binary ssllabs.com.key | openssl enc -base64

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

Use ChilkatAx-9.5.0-win32.pkg

Procedure Test
    Handle hoHttpA
    Variant vSslCert
    Handle hoSslCert
    Handle hoHttpB
    String sHtml
    String sTemp1
    Boolean bTemp1

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    Get Create (RefClass(cComChilkatHttp)) To hoHttpA
    If (Not(IsComObjectCreated(hoHttpA))) Begin
        Send CreateComObject of hoHttpA
    End

    // 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
    Get ComGetServerSslCert Of hoHttpA "www.ssllabs.com" 443 To vSslCert
    If (IsComObject(vSslCert)) Begin
        Get Create (RefClass(cComChilkatCert)) To hoSslCert
        Set pvComObject Of hoSslCert To vSslCert
    End
    Get ComLastMethodSuccess Of hoHttpA To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoHttpA To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // The GetSpkiFingerprint method returns the SPKI Fingerprint suitable for use in pinning.
    Get ComGetSpkiFingerprint Of hoSslCert "sha256" "base64" To sTemp1
    Showln "SPKI Fingerprint: " sTemp1
    Send Destroy of hoSslCert

    // ------------------------------------------------------------------------------------

    // At the time of writing this example (on 19-Dec-2015) the sha256/base64 SPKI fingerprint
    // for the ssllabs.com server certificate is: xkWf9Qfs1uZi2NcMV3Gdnrz1UF4FNAslzApMTwynaMU=

    Get Create (RefClass(cComChilkatHttp)) To hoHttpB
    If (Not(IsComObjectCreated(hoHttpB))) Begin
        Send CreateComObject of hoHttpB
    End

    // Set the TlsPinSet.  The format of the TlsPinSet string is:
    //  "hashAlg, encoding, fingerprint1, fingerprint2, ..."
    Set ComTlsPinSet Of hoHttpB To "sha256, base64, xkWf9Qfs1uZi2NcMV3Gdnrz1UF4FNAslzApMTwynaMU="

    // 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).
    Get ComQuickGetStr Of hoHttpB "https://www.ssllabs.com/" To sHtml
    Get ComLastMethodSuccess Of hoHttpB To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoHttpB To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "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.
    Get ComQuickGetStr Of hoHttpB "https://www.owasp.org/index.php/Certificate_and_Public_Key_Pinning" To sHtml
    Get ComLastMethodSuccess Of hoHttpB To bTemp1
    If (bTemp1 = False) Begin
        Showln "Good, this connection was rejected..."
    End
    Else Begin
        Showln "This was not supposed to happen!"
        Procedure_Return
    End



End_Procedure

 

© 2000-2019 Chilkat Software, Inc. All Rights Reserved.