Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Node.js) 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. 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 OpenSSLThis 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
var os = require('os'); if (os.platform() == 'win32') { if (os.arch() == 'ia32') { var chilkat = require('@chilkat/ck-node21-win-ia32'); } else { var chilkat = require('@chilkat/ck-node21-win64'); } } else if (os.platform() == 'linux') { if (os.arch() == 'arm') { var chilkat = require('@chilkat/ck-node21-arm'); } else if (os.arch() == 'x86') { var chilkat = require('@chilkat/ck-node21-linux32'); } else { var chilkat = require('@chilkat/ck-node21-linux64'); } } else if (os.platform() == 'darwin') { if (os.arch() == 'arm64') { var chilkat = require('@chilkat/ck-node21-mac-m1'); } else { var chilkat = require('@chilkat/ck-node21-macosx'); } } function chilkatExample() { // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. var httpA = new 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 // sslCert: Cert var sslCert = httpA.GetServerSslCert("www.ssllabs.com",443); if (httpA.LastMethodSuccess == false) { console.log(httpA.LastErrorText); return; } // The GetSpkiFingerprint method returns the SPKI Fingerprint suitable for use in pinning. console.log("SPKI Fingerprint: " + sslCert.GetSpkiFingerprint("sha256","base64")); // ------------------------------------------------------------------------------------ // At the time of writing this example (on 19-Dec-2015) the sha256/base64 SPKI fingerprint // for the ssllabs.com server certificate is: xkWf9Qfs1uZi2NcMV3Gdnrz1UF4FNAslzApMTwynaMU= var httpB = new chilkat.Http(); // Set the TlsPinSet. The format of the TlsPinSet string is: // "hashAlg, encoding, fingerprint1, fingerprint2, ..." httpB.TlsPinSet = "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). var html = httpB.QuickGetStr("https://www.ssllabs.com/"); if (httpB.LastMethodSuccess == false) { console.log(httpB.LastErrorText); return; } console.log("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. html = httpB.QuickGetStr("https://www.owasp.org/index.php/Certificate_and_Public_Key_Pinning"); if (httpB.LastMethodSuccess == false) { console.log("Good, this connection was rejected..."); } else { console.log("This was not supposed to happen!"); return; } } chilkatExample(); |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.