Giter Site home page Giter Site logo

pascal-stuff / delphi_schanneltls Goto Github PK

View Code? Open in Web Editor NEW

This project forked from fr0st-brutal/delphi_schanneltls

0.0 0.0 0.0 729 KB

Helper functions and socket classes to perform TLS communication by means of WinAPI (SChannel)

License: MIT License

Pascal 100.00%

delphi_schanneltls's Introduction

Delphi SChannel (TLS with WinAPI)

SChannel is Windows built-in implementation of TLS protocols. This allows supporting secure connections without any external library.

Repo contains:

  • SChannel.Utils.pas - unit with transport-agnostic helper functions for easy implementation of TLS communication by means of Windows SChannel.

  • SChannel.SyncHandshake.pas - sample of transport-agnostic synchronous TLS handshake using callback functions for real communication

  • SChannel.Jwa*.pas - API declarations borrowed from JEDI project

  • SChannel.IcsWSocket.pas - ICS TWSocket descendant that performs TLS communication

  • Demo\ - demo project for performing any textual (mainly HTTPS) requests via secure connection

  • Enable TLS 1.1 and 1.2 for W7.reg - registry patch that enables TLS 1.1 and 1.2 on Windows 7 (these are actual protocol versions that are not enabled by default on W7)

Installation

There are no installable components - you can just download sources, add paths and use units. But if you prefer packages, there are two of them created with RAD XE2. Just open one of them in IDE and it should upgrade the package to current version.

  • SChannel_XE2.dproj contains all units except ICS socket. Use it if you have no ICS installed
  • SChannel_XE2_ICS.dproj contains all units including ICS socket in SChannel.IcsWSocket.pas. Requires ICS installed

Both packages are configured to build units to ..\Bin\$(Config)\$(Platform) to reduce mess and avoid recompiling SChannel units each time a project is built. So you can add SChannel\Bin\Release\$(Platform) to your Search path and SChannel\Source to Browsing path. In this case don't forget to build the package for all platforms you use. After that installation is complete.

Installation via Delphinus

Installation via Delphinus package manager is also possible. However, it's an initial version that has some limitations:

  • Search path entry will point to sources folder
  • Package is not built

Developer note

This project was started because I needed TLS in my Delphi apps and didn't like shipping two OpenSSL libs. Initial version was 1:1 rewrite of SChannel sample found in Internet. Currently it is used in my 24*7 projects but I implemented only those functions which I needed. I'm not familiar with all this cryptostuff so don't expect advanced certificate validations, secure servers and so on. But if you wish to add something missing I'll consider your PR with pleasure :).

Note - SChannel bug

There's SChannel bug that causes functions rarely and randomly return SEC_E_BUFFER_TOO_SMALL or SEC_E_MESSAGE_ALTERED status during handshake. Good description of the issue could be found here (in brief: it only happens on Windows 7 and 8, with TLSv1.2 and TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 and TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 cipher suites). To deal with the issue, several measures were taken:

  1. Increased number of buffers for handshake (what cURL team did) - didn't help

  2. Added function IsWinHandshakeBug to SChannel.Utils.pas that allows to conveniently check for the bug in except section of a DoClientHandshake call. Just a helper to make special processing like

    ...
    try
      DoClientHandshake(FSessionData, FHandShakeData);
    except on E: ESSPIError do
      // Hide Windows handshake bug and restart the process
      if (FHandShakeData.Stage = hssReadSrvHello) and IsWinHandshakeBug(E.SecStatus) then
      begin
        Log(Format('Handshake bug: "%s", retrying', [E.Message]));
        DeleteContext(FHandShakeData.hContext);
        DoHandshakeStart;
        Exit;
      end
      else
        raise E;
    end;
    ...
  3. TSChannelWSocket class from IcsSChannelSocket.pas and PerformClientHandshake function from SChannel.SyncHandshake.pas already implement one-time retrying invisibly to a caller.

IcsSChannelSocket

Socket class descending from ICS TWSocket that does many things for you. Key features:

  • Automatic handshake retry when handshake bug (see above) is encountered
  • TLS channel could be started/finished at any moment by setting Secure property; OnTLSDone and OnTLSShutdown events will signal channel state
  • Session data could be shared between multiple sockets with SessionData.SharedCreds property. When sessions are shared, handshake becomes significantly shorter so it worths it

delphi_schanneltls's People

Contributors

fr0st-brutal avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.