Chilkat Examples

ChilkatHOMEAndroid™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi DLLGoJavaNode.jsObjective-CPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwiftTclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

SQL Server Examples
Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Box
CAdES
CSR
CSV
Cert Store
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
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)
MHT / HTML Email
MIME
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
Secrets
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
X
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(SQL Server) 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.

Note: This example requires Chilkat v11.0.0 or greater.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

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

    DECLARE @httpA int
    EXEC @hr = sp_OACreate 'Chilkat.Http', @httpA OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    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
    DECLARE @sslCert int
    EXEC @hr = sp_OACreate 'Chilkat.Cert', @sslCert OUT

    EXEC sp_OAMethod @httpA, 'GetServerCert', @success OUT, 'www.ssllabs.com', 443, @sslCert
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @httpA, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @httpA
        EXEC @hr = sp_OADestroy @sslCert
        RETURN
      END

    -- The GetSpkiFingerprint method returns the SPKI Fingerprint suitable for use in pinning.

    EXEC sp_OAMethod @sslCert, 'GetSpkiFingerprint', @sTmp0 OUT, 'sha256', 'base64'
    PRINT 'SPKI Fingerprint: ' + @sTmp0

    -- ------------------------------------------------------------------------------------

    DECLARE @httpB int
    EXEC @hr = sp_OACreate 'Chilkat.Http', @httpB OUT

    -- Set the TlsPinSet.  The format of the TlsPinSet string is:
    --  "hashAlg, encoding, fingerprint1, fingerprint2, ..."
    EXEC sp_OASetProperty @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).
    DECLARE @html nvarchar(4000)
    EXEC sp_OAMethod @httpB, 'QuickGetStr', @html OUT, 'https://www.ssllabs.com/'
    EXEC sp_OAGetProperty @httpB, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @httpB, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @httpA
        EXEC @hr = sp_OADestroy @sslCert
        EXEC @hr = sp_OADestroy @httpB
        RETURN
      END


    PRINT '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.
    EXEC sp_OAMethod @httpB, 'QuickGetStr', @html OUT, 'https://www.owasp.org/index.php/Certificate_and_Public_Key_Pinning'
    EXEC sp_OAGetProperty @httpB, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN

        PRINT 'Good, this connection was rejected...'
      END
    ELSE
      BEGIN

        PRINT 'This was not supposed to happen!'
      END

    EXEC @hr = sp_OADestroy @httpA
    EXEC @hr = sp_OADestroy @sslCert
    EXEC @hr = sp_OADestroy @httpB


END
GO

 

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