aboutsummarylogtreecommitdiffstats
path: root/ssl_openssl_capi.pas
blob: e6eb6c0845a749653eb987732eeb2f7c123d6de4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
{==============================================================================|
| Project : Ararat Synapse                                       | 001.003.000 |
|==============================================================================|
| Content: SSL support by OpenSSL and the CAPI engine                          |
|==============================================================================|
| Copyright (c)2018, Pepak                                                     |
| All rights reserved.                                                         |
|                                                                              |
| Redistribution and use in source and binary forms, with or without           |
| modification, are permitted provided that the following conditions are met:  |
|                                                                              |
| Redistributions of source code must retain the above copyright notice, this  |
| list of conditions and the following disclaimer.                             |
|                                                                              |
| Redistributions in binary form must reproduce the above copyright notice,    |
| this list of conditions and the following disclaimer in the documentation    |
| and/or other materials provided with the distribution.                       |
|                                                                              |
| Neither the name of Lukas Gebauer nor the names of its contributors may      |
| be used to endorse or promote products derived from this software without    |
| specific prior written permission.                                           |
|                                                                              |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"  |
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE    |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE   |
| ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR  |
| ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL       |
| DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR   |
| SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER   |
| CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT           |
| LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY    |
| OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH  |
| DAMAGE.                                                                      |
|==============================================================================|
| The Initial Developer of the Original Code is Pepak (Czech Republic).        |
| Portions created by Pepak are Copyright (c)2018.                             |
| All Rights Reserved.                                                         |
|==============================================================================|
| Contributor(s):                                                              |
|==============================================================================|
| History: see HISTORY.HTM from distribution package                           |
|          (Found at URL: http://www.ararat.cz/synapse/)                       |
|==============================================================================}

//requires OpenSSL libraries, including the CAPI engine (capi.dll)!
//recommended source: Stunnel (https://www.stunnel.org)

{:@abstract(SSL plugin for OpenSSL and the CAPI engine)

Compatibility with OpenSSL versions:

1.0.2 works fine.

1.1.x does not work properly out of the box. I was never able to get CAPI
to work with pre-built binaries or binaries that I built myself, even in
third party applications such as STunnel. The only config which works for
me involves custom-building OpenSSL with engines statically compiled into
libcrypto:

   1) Install PERL (e.g. C:\PERL). Make sure the BIN subdirectory is in
      the PATH (SET PATH=%PATH%;C:\PERL\BIN).

   2) Download DMAKE ( https://metacpan.org/release/dmake ) and unpack it
      into the Perl directory (you will get C:\PERL\DMAKE\DMAKE.EXE and
      other files). Add the DMAKE directory to PATH as well.

   3) Start Visual Studio Development Prompt, either 32 or 64bit. All the
      following commands should be run in this prompt.

   4) Install the Text::Template module by running:
        cpan -i Text::Template

   5) Download and unpack the OpenSSL sources into e.g. C:\SOURCE\OPENSSL.

   6) Download and unpack the Zlib sources into e.g. C:\SOURCE\ZLIB.

   7) Go to the ZLIB directory and run:
        nmake -f win32/Makefile.msc

   8) Go to the OpenSSL directory and run:
        32bit:
          perl Configure shared enable-static-engine enable-zlib --with-zlib-include=C:\SOURCE\ZLIB --with-zlib-lib=C:\SOURCE\ZLIB\zlib.lib VC-WIN32
        64bit:
          perl Configure shared enable-static-engine enable-zlib --with-zlib-include=C:\SOURCE\ZLIB --with-zlib-lib=C:\SOURCE\ZLIB\zlib.lib VC-WIN64A
        Make sure to replace both instances of C:\SOURCE\ZLIB with the actual
        path to the Zlib library.

   9) If you want to build the OpenSSL DLLs without external dependencies
      (e.g. on the Visual Studio Runtime), edit the generated makefile:

      - Change the "/MD" flag in CNF_FLAGS to "/MT".
      - Add "/NODEFAULTLIB:MSVCRT" to CNF_LDFLAGS.

  10) In the OpenSSL directory, run:
        nmake

  11) When all is done, copy LIBCRYPTO-1_1*.DLL and LIBSSH-1_1*.DLL to
      your application's binary directory.


OpenSSL libraries are loaded dynamically - you do not need the librares even if
you compile your application with this unit. SSL just won't work if you don't
have the OpenSSL libraries.

The plugin is built on the standard OpenSSL plugin, giving it all the features
of it. In fact, if you do not have the CAPI engine, the plugin will behave in
exactly the same way as the original plugin - the CAPI engine is completely
optional, the plugin will work without it - obviously without the support for
Windows Certificate Stores.

The windows certificate stores are supported through the following properties:

@link(TSSLOpenSSLCapi.SigningCertificate) - expects pointer to the certificate
context of the signing certificate (PCCERT_CONTENT). @br

Note that due to the limitations of OpenSSL, it is not possible to switch
between different engines (e.g. CAPI and default) on the fly - the engine is
a global setting for the whole of OpenSSL. For that reason, once the engine
is enabled (either explicitly or by using a Windows certificate for a connection),
it will stay enabled and there is no method for disabling it.

}

{$INCLUDE 'jedi.inc'}
{$H+}

{$DEFINE USE_ENGINE_POOL}

unit ssl_openssl_capi;

interface

uses
  Windows, Crypt32, SysUtils, Classes, SyncObjs,
  blcksock, ssl_openssl, ssl_openssl_lib;

type
  PENGINE = Pointer;

type
  TWindowsCertStoreLocation = (
      wcslCurrentUser
    , wcslCurrentUserGroupPolicy
    , wcslUsers
    , wcslCurrentService
    , wcslServices
    , wcslLocalMachine
    , wcslLocalMachineGroupPolicy
    , wcslLocalMachineEnterprise
  );

type
  {:@abstract(class extending the OpenSSL SSL plugin with CAPI support.)
   Instance of this class will be created for each @link(TTCPBlockSocket).
   You not need to create instance of this class, all is done by Synapse itself!}
  TSSLOpenSSLCapi = class(TSSLOpenSSL)
  private
    FEngine: PENGINE;
    FEngineInitialized: boolean;
    FSigningCertificateLocation: TWindowsCertStoreLocation;
    FSigningCertificateStore: string;
    FSigningCertificateID: string;
    function GetEngine: PENGINE;
  protected
    {:Loads a certificate context into the CAPI engine for signing/decryption.}
    function LoadSigningCertificate: boolean;
    {:See @inherited}
    function SetSslKeys: boolean; override;
    {:See @inherited}
    function NeedSigningCertificate: boolean; override;
    {:Returns true if the signing certificate should be used.}
    function SigningCertificateSpecified: boolean;
    {:Provides a cryptographic engine for OpenSSL}
    property Engine: PENGINE read GetEngine;
  public
    {:See @inherited}
    constructor Create(const Value: TTCPBlockSocket); override;
    {:See @inherited}
    destructor Destroy; override;
    {:See @inherited}
    procedure Assign(const Value: TCustomSSL); override;
    {:Use this function to load the CAPI engine and/or verify that the engine
     is available. The plugin will load CAPI itself when it is needed, so you
     may skip this function completely, but it may be useful to perform a manual
     CAPI load early during the application startup to make sure all connection
     use the same cryptographic engine (and, as a result, behave the same way).}
    class function InitEngine: boolean;
    {:Location of the certificate store used for the communication.}
    property SigningCertificateLocation: TWindowsCertStoreLocation read FSigningCertificateLocation write FSigningCertificateLocation;
    {:Certificate store used for the communication. The most common is "MY",
     or the user's private certificates.}
    property SigningCertificateStore: string read FSigningCertificateStore write FSigningCertificateStore;
    {:ID of the certificate to use. For standard CAPI, this is the friendly name
     of the certificate. For the client-side SSL it is not really necessary, as
     long as it is non-empty (which signifies that the CAPI engine should be
     used). For the server side, it must be a substring of the SubjectName of
     the certificate. The first matching certificate will be used.}
    property SigningCertificateID: string read FSigningCertificateID write FSigningCertificateID;
  end;

implementation

{$IFDEF SUPPORTS_REGION}{$REGION 'Support and compatibility functions'}{$ENDIF}
{==============================================================================}
{Support and compatibility functions                                           }
{------------------------------------------------------------------------------}

function GetModuleFileNamePAS(Handle: THandle; out FileName: string): boolean;
var
  FN: string;
  n: integer;
begin
  Result := False;
  if Handle = 0 then
    Exit;
  SetLength(FN, MAX_PATH);
  n := GetModuleFileName(Handle, @FN[1], Length(FN));
  if (n > 0) and (GetLastError = ERROR_INSUFFICIENT_BUFFER) then
  begin
    SetLength(FN, n);
    n := GetModuleFileName(Handle, @FN[1], Length(FN));
  end;
  if (n > 0) and (GetLastError = ERROR_SUCCESS) then
  begin
    SetLength(FN, n);
    FileName := FN;
    Result := True;
  end;
end;

{$IFNDEF UNICODE}
type
  PPointer = ^Pointer;

procedure RaiseLastOSError;
begin
  RaiseLastWin32Error;
end;
{$ENDIF}

{$IFDEF SUPPORTS_REGION}{$ENDREGION}{$ENDIF}

{$IFDEF SUPPORTS_REGION}{$REGION 'Imported functions'}{$ENDIF}
{==============================================================================}
{Imported functions                                                            }
{------------------------------------------------------------------------------}

const
  CapiEngineID = 'capi';
  DLLCapiName = CapiEngineID + '.dll';

const
  SSL_CTRL_OPTIONS = 32;
  SSL_OP_NO_TLSv1_2 = $08000000;

const
  ENGINE_METHOD_ALL = $ffff;

type
  PPX509 = ^PX509;

var
  FEngineCS: TCriticalSection = nil;
  FEngineNeedsSHA2Workaround: boolean = False;

var
  FEngineInterfaceInitialized: boolean = False;
  FENGINE_cleanup: procedure; cdecl = nil;
  FENGINE_load_builtin_engines: procedure; cdecl = nil;
  FENGINE_by_id: function(id: PAnsiChar): PENGINE; cdecl = nil;
  FENGINE_ctrl_cmd_string: function(e: PENGINE; cmd_name, arg: PAnsiChar; cmd_optional: integer): integer; cdecl = nil;
  FENGINE_init: function(e: PENGINE): integer; cdecl = nil;
  FENGINE_finish: function(e: PENGINE): integer; cdecl = nil;
  FENGINE_free: function(e: PENGINE): integer; cdecl = nil;
  FENGINE_set_default: function(e: PENGINE; flags: DWORD): integer; cdecl = nil;
  FENGINE_load_private_key: function(e: PENGINE; key_id: PAnsiChar; ui_method: Pointer; callback_data: Pointer): EVP_PKEY; cdecl = nil;
  FSSL_CTX_set_client_cert_engine: function(ctx: PSSL_CTX; e: PENGINE): integer; cdecl = nil;
  Fd2i_X509: function(px: PPX509; data: PPointer; len: integer): PX509; cdecl = nil;

function InitEngineInterface: boolean;
var
  OpenSSLFileName: string;
  VerInfoSize: DWORD;
  VerInfo: Pointer;
  VerHandle: DWORD;
  SpecVerInfo: PVsFixedFileInfo;
begin
  if FEngineInterfaceInitialized then
  begin
    Result := True;
    Exit;
  end;
  FEngineCS.Enter;
  try
    if FEngineInterfaceInitialized then
    begin
      Result := True;
      Exit;
    end;
    Result := False;
    if not InitSSLInterface then
      Exit;
    if SSLUtilHandle = 0 then
      Exit;
    if SSLLibHandle = 0 then
      Exit;
    FENGINE_cleanup := GetProcAddress(SSLUtilHandle, 'ENGINE_cleanup');
    FENGINE_load_builtin_engines := GetProcAddress(SSLUtilHandle, 'ENGINE_load_builtin_engines');
    FENGINE_by_id := GetProcAddress(SSLUtilHandle, 'ENGINE_by_id');
    FENGINE_ctrl_cmd_string := GetProcAddress(SSLUtilHandle, 'ENGINE_ctrl_cmd_string');
    FENGINE_init := GetProcAddress(SSLUtilHandle, 'ENGINE_init');
    FENGINE_finish := GetProcAddress(SSLUtilHandle, 'ENGINE_finish');
    FENGINE_free := GetProcAddress(SSLUtilHandle, 'ENGINE_free');
    FENGINE_set_default := GetProcAddress(SSLUtilHandle, 'ENGINE_set_default');
    FENGINE_load_private_key := GetProcAddress(SSLUtilHandle, 'ENGINE_load_private_key');
    FSSL_CTX_set_client_cert_engine := GetProcAddress(SSLLibHandle, 'SSL_CTX_set_client_cert_engine');
    Fd2i_X509 := GetProcAddress(SSLUtilHandle, 'd2i_X509');
    FEngineInterfaceInitialized := True;
    //---- Workaround for a CAPI engine bug ------------------------------------
    // https://www.stunnel.org/pipermail/stunnel-users/2017-February/005720.html
    //
    // The capi ENGINE in OpenSSL 1.0.2 and earlier uses the CSP attached
    // to the key for cryptographic operations. Unfortunately this means that
    // SHA2 algorithms are not supported for client authentication.
    //
    // OpenSSL 1.1.0 adds a workaround for this issue. If you disable TLS 1.2
    // in earlier versions of OpenSSL it will not use SHA2 for client auth so
    // that will also work.
    begin
      FEngineNeedsSHA2Workaround := False;
      if GetModuleFileNamePAS(SSLUtilHandle, OpenSSLFileName) then
      begin
        VerInfoSize := GetFileVersionInfoSize(PChar(OpenSSLFileName), VerHandle);
        if VerInfoSize > 0 then
        begin
          GetMem(VerInfo, VerInfoSize);
          try
            if GetFileVersionInfo(PChar(OpenSSLFileName), VerHandle, VerInfoSize, VerInfo) then
              if VerQueryValue(VerInfo, '\', Pointer(SpecVerInfo), VerInfoSize) then
              begin
                if SpecVerInfo^.dwFileVersionMS < (65536*1 + 1) then
                  FEngineNeedsSHA2Workaround := True;
              end;
          finally
            FreeMem(VerInfo);
          end;
        end;
      end;
    end;
    //---- Workaround end ------------------------------------------------------
    Result := True;
  finally
    FEngineCS.Leave;
  end;
end;

procedure DestroyEngineInterface;
begin
  FEngineCS.Enter;
  try
    if Assigned(FENGINE_cleanup) then
      FENGINE_cleanup;
    FENGINE_cleanup := nil;
    FENGINE_load_builtin_engines := nil;
    FENGINE_by_id := nil;
    FENGINE_ctrl_cmd_string := nil;
    FENGINE_init := nil;
    FENGINE_finish := nil;
    FENGINE_free := nil;
    FENGINE_set_default := nil;
    FENGINE_load_private_key := nil;
    FSSL_CTX_set_client_cert_engine := nil;
    Fd2i_X509 := nil;
    FEngineInterfaceInitialized := False;
  finally
    FEngineCS.Leave;
  end;
end;

procedure ENGINE_load_builtin_engines;
begin
  if InitEngineInterface and Assigned(FENGINE_load_builtin_engines) then
    FENGINE_load_builtin_engines;
end;

function ENGINE_by_id(id: PAnsiChar): PENGINE;
begin
  if InitEngineInterface and Assigned(FENGINE_by_id) then
    Result := FENGINE_by_id(id)
  else
    Result := nil;
end;

function ENGINE_ctrl_cmd_string(e: PENGINE; cmd_name, arg: PAnsiChar; cmd_optional: integer): integer;
begin
  if InitEngineInterface and Assigned(FENGINE_ctrl_cmd_string) then
    Result := FENGINE_ctrl_cmd_string(e, cmd_name, arg, cmd_optional)
  else
    Result := 0;
end;

function ENGINE_init(e: PENGINE): integer;
begin
  if InitEngineInterface and Assigned(FENGINE_init) then
    Result := FENGINE_init(e)
  else
    Result := 0;
end;

function ENGINE_finish(e: PENGINE): integer;
begin
  if InitEngineInterface and Assigned(FENGINE_finish) then
    Result := FENGINE_finish(e)
  else
    Result := 0;
end;

function ENGINE_free(e: PENGINE): integer;
begin
  if InitEngineInterface and Assigned(FENGINE_free) then
    Result := FENGINE_free(e)
  else
    Result := 0;
end;

function ENGINE_set_default(e: PENGINE; flags: DWORD): integer;
begin
  if InitEngineInterface and Assigned(FENGINE_set_default) then
    Result := FENGINE_set_default(e, flags)
  else
    Result := 0;
end;

function ENGINE_load_private_key(e: PENGINE; key_id: PAnsiChar; ui_method: Pointer; callback_data: Pointer): EVP_PKEY;
begin
  if InitEngineInterface and Assigned(FENGINE_load_private_key) then
    Result := FENGINE_load_private_key(e, key_id, ui_method, callback_data)
  else
    Result := nil;
end;

function SSL_CTX_set_client_cert_engine(ctx: PSSL_CTX; e: PENGINE): integer;
begin
  if InitEngineInterface and Assigned(FSSL_CTX_set_client_cert_engine) then
    Result := FSSL_CTX_set_client_cert_engine(ctx, e)
  else
    Result := 0;
end;

function d2i_X509(px: PPX509; data: PPointer; len: integer): PX509;
begin
  if InitEngineInterface and Assigned(Fd2i_X509) then
    Result := Fd2i_X509(px, data, len)
  else
    Result := nil;
end;

{$IFDEF SUPPORTS_REGION}{$ENDREGION}{$ENDIF}

{$IFDEF SUPPORTS_REGION}{$REGION 'CAPI engine support'}{$ENDIF}
{==============================================================================}
{CAPI engine support                                                           }
{------------------------------------------------------------------------------}

var
  FGlobalEngineInitialized: boolean = False;
  FGlobalEngine: PENGINE = nil;

function PrepareCapiEngine(out Engine: PENGINE): boolean;

  function LoadCapiEngine(Engine: PENGINE; const FileName: string): boolean;
  begin
    Result := False;
    if ENGINE_ctrl_cmd_string(Engine, 'SO_PATH', PAnsiChar(AnsiString(FileName)), 0) <> 0 then
      if ENGINE_ctrl_cmd_string(Engine, 'LOAD', nil, 0) <> 0 then
        Result := True;
  end;

  function LoadCapiEngineDynamic(out Engine: PENGINE): boolean;
  var
    OpenSSLFileName: string;
    TempEngine: PENGINE;
  begin
    Result := False;
    if not GetModuleFileNamePAS(SSLUtilHandle, OpenSSLFileName) then
      Exit;
    TempEngine := ENGINE_by_id('dynamic');
    try
      if TempEngine <> nil then
      begin
        if LoadCapiEngine(TempEngine, ExtractFilePath(OpenSSLFileName) + DLLCapiName) then // need a version match! Same dir suggests the versions could be the same
          if ENGINE_init(TempEngine) <> 0 then
          begin
            Engine := TempEngine;
            TempEngine := nil;
            Result := True;
          end;
      end;
    finally
      if TempEngine <> nil then
      begin
        ENGINE_free(TempEngine);
        //TempEngine := nil; // triggers a hint
      end;
    end;
  end;

  function LoadCapiEngineStatic(out Engine: PENGINE): boolean;
  var
    TempEngine: PENGINE;
  begin
    Result := False;
    TempEngine := ENGINE_by_id(CapiEngineID);
    try
      if TempEngine <> nil then
      begin
        if ENGINE_init(TempEngine) <> 0 then
        begin
          Engine := TempEngine;
          TempEngine := nil;
          Result := True;
        end;
      end;
    finally
      if TempEngine <> nil then
      begin
        ENGINE_free(TempEngine);
        //TempEngine := nil; // triggers a hint
      end;
    end;
  end;

begin
  Result := LoadCapiEngineStatic(Engine) or LoadCapiEngineDynamic(Engine);
end;

function InitCapiEngine: boolean;
var
  E: PENGINE;
begin
  Result := FGlobalEngine <> nil;
  if FGlobalEngineInitialized then
    Exit;
  FEngineCS.Enter;
  try
    if FGlobalEngineInitialized then
      Exit;
    ENGINE_load_builtin_engines();
    if PrepareCapiEngine(E) then
    begin
      if not Assigned(FSSL_CTX_set_client_cert_engine) then
      begin
        if ENGINE_set_default(E, ENGINE_METHOD_ALL) = 0 then
        begin
          ENGINE_finish(E);
          ENGINE_free(E);
          E := nil;
        end;
      end;
      FGlobalEngine := E;
    end;
    FGlobalEngineInitialized := True;
    Result := FGlobalEngine <> nil;
  finally
    FEngineCS.Leave;
  end;
end;

{$IFDEF SUPPORTS_REGION}{$ENDREGION}{$ENDIF}

{$IFDEF SUPPORTS_REGION}{$REGION 'Pool of engines'}{$ENDIF}
{==============================================================================}
{Pool of engines, to reduce the time to get a working connection               }
{------------------------------------------------------------------------------}

{$IFDEF USE_ENGINE_POOL}

type
  TEnginePool = class
  private
    fLock: TCriticalSection;
    fAvailableList: TList;
  protected
    procedure Lock;
    procedure Unlock;
  public
    constructor Create;
    destructor Destroy; override;
    function Acquire(out Engine: PENGINE): boolean;
    procedure Release(var Engine: PENGINE);
    procedure Clear;
  end;

var
  FEnginePool: TEnginePool  = nil;

{ TEnginePool }

function TEnginePool.Acquire(out Engine: PENGINE): boolean;
var
  n: integer;
begin
  if fAvailableList.Count > 0 then
  begin
    Lock;
    try
      for n := Pred(fAvailableList.Count) downto 0 do
      begin
        Engine := fAvailableList[n];
        if Engine <> nil then
        begin
          fAvailableList.Delete(n);
          Result := True;
          Exit;
        end;
      end;
    finally
      Unlock;
    end;
  end;
  Result := InitCapiEngine and PrepareCapiEngine(Engine);
end;

procedure TEnginePool.Clear;
var
  i: integer;
  E: PENGINE;
begin
  Lock;
  try
    for i := 0 to Pred(fAvailableList.Count) do
    begin
      E := fAvailableList[i];
      fAvailableList[i] := nil;
      if E <> nil then
      begin
        ENGINE_finish(E);
        ENGINE_free(E);
      end;
    end;
    fAvailableList.Clear;
  finally
    Unlock;
  end;
end;

constructor TEnginePool.Create;
begin
  inherited Create;
  fLock := TCriticalSection.Create;
  fAvailableList := TList.Create;
end;

destructor TEnginePool.Destroy;
begin
  Clear;
  FreeAndNil(fAvailableList);
  FreeAndNil(fLock);
  inherited;
end;

procedure TEnginePool.Lock;
begin
  fLock.Enter;
end;

procedure TEnginePool.Release(var Engine: PENGINE);
begin
  if Engine = nil then
    Exit;
  Lock;
  try
    fAvailableList.Add(Engine);
    Engine := nil;
  finally
    Unlock;
  end;
end;

procedure TEnginePool.Unlock;
begin
  fLock.Leave;
end;

{$ENDIF}

{$IFDEF SUPPORTS_REGION}{$ENDREGION}{$ENDIF}

{$IFDEF SUPPORTS_REGION}{$REGION 'The plugin'}{$ENDIF}
{==============================================================================}
{The plugin                                                                    }
{------------------------------------------------------------------------------}

{ TSSLOpenSSLCapi }

class function TSSLOpenSSLCapi.InitEngine: boolean;
begin
  Result := InitCapiEngine;
end;

procedure TSSLOpenSSLCapi.Assign(const Value: TCustomSSL);
var
  CAPIValue: TSSLOpenSSLCapi;
begin
  inherited;
  if (Value <> nil) and (Value is TSSLOpenSSLCapi) then
  begin
    CAPIValue := TSSLOpenSSLCapi(Value);
    Self.FSigningCertificateLocation := CAPIValue.FSigningCertificateLocation;
    Self.FSigningCertificateStore := CAPIValue.FSigningCertificateStore;
    Self.FSigningCertificateID := CAPIValue.FSigningCertificateID;
  end;
end;

constructor TSSLOpenSSLCapi.Create(const Value: TTCPBlockSocket);
begin
  inherited;
  FEngine := nil;
  FEngineInitialized := False;
  FSigningCertificateLocation := wcslCurrentUser;
  FSigningCertificateStore := 'MY';
  FSigningCertificateID := '';
end;

destructor TSSLOpenSSLCapi.Destroy;
begin
  if FEngine <> nil then
  begin
    {$IFDEF USE_ENGINE_POOL}
    FEnginePool.Release(FEngine);
    {$ELSE}
    ENGINE_finish(FEngine);
    ENGINE_free(FEngine);
    {$ENDIF}
    FEngineInitialized := False;
  end;
  inherited;
end;

function TSSLOpenSSLCapi.GetEngine: PENGINE;
begin
  if not FEngineInitialized then
  begin
    {$IFDEF USE_ENGINE_POOL}
    if not FEnginePool.Acquire(FEngine) then
      FEngine := nil;
    {$ELSE}
    if (not InitEngine) or (not PrepareCapiEngine(FEngine)) then
      FEngine := nil;
    {$ENDIF}
    FEngineInitialized := True;
  end;
  Result := FEngine;
end;

function TSSLOpenSSLCapi.LoadSigningCertificate: boolean;
var
  pkey: EVP_PKEY;
  pdata: Pointer;
  cert: PX509;
  store: HCERTSTORE;
  certctx: PCCERT_CONTEXT;
  flags: DWORD;
begin
  Result := False;
  if not SigningCertificateSpecified then
    Exit;
  if not InitEngine then
    Exit;
  if Engine = nil then
    Exit;
  if not Assigned(FSSL_CTX_set_client_cert_engine) then
    Exit;
  if SSL_CTX_set_client_cert_engine(Fctx, Engine) = 0 then
    Exit;
  if ENGINE_ctrl_cmd_string(Engine, 'store_name', PAnsiChar( {$IFDEF UNICODE} AnsiString {$ENDIF} (SigningCertificateStore)), 0) = 0 then
    Exit;
  if ENGINE_ctrl_cmd_string(Engine, 'lookup_method', '1', 0) = 0 then
    Exit;
  case SigningCertificateLocation of
    wcslCurrentUser:
      if ENGINE_ctrl_cmd_string(Engine, 'store_flags', '0', 0) = 0 then
        Exit;
    wcslLocalMachine:
      if ENGINE_ctrl_cmd_string(Engine, 'store_flags', '1', 0) = 0 then
        Exit;
    else
      Exit; // other store flags are not supported by the CAPI engine
  end;
  if Server then
  begin
    cert := nil;
    pkey := nil;
    try
      // Need to find the context and the store for the certificate. Unfortunately,
      // due to the CAPI engine limitations (see capi_load_privkey), I can only use
      // a very limited set of criteria for finding the certificate
      flags := 0;
      case SigningCertificateLocation of
        wcslCurrentUser:
          flags := flags or CERT_SYSTEM_STORE_CURRENT_USER;
        wcslLocalMachine:
          flags := flags or CERT_SYSTEM_STORE_LOCAL_MACHINE;
        else
          Exit; // other store flags are not supported by the CAPI engine
      end;
      store := CertOpenStore(CERT_STORE_PROV_SYSTEM_W, 0, 0, flags, PWideChar(WideString(SigningCertificateStore)));
      if store <> 0 then
      begin
        try
          certctx := CertFindCertificateInStore(store, X509_ASN_ENCODING, 0, CERT_FIND_SUBJECT_STR_A, PAnsiChar( {$IFDEF UNICODE} AnsiString {$ENDIF} (SigningCertificateID)), nil);
          if certctx = nil then
            Exit;
          pkey := ENGINE_load_private_key(Engine, PAnsiChar( {$IFDEF UNICODE} AnsiString {$ENDIF} (SigningCertificateID)), nil, nil);
          if pkey = nil then
            Exit;
          pdata := certctx.pbCertEncoded;
          cert := d2i_X509(nil, @pdata, certctx.cbCertEncoded);
          if cert = nil then
            Exit;
          if SSLCTXusecertificate(Fctx, cert) <= 0 then
            Exit;
          if SSLCTXusePrivateKey(Fctx, pkey) <= 0 then
            Exit;
          Result := True;
        finally
          CertCloseStore(store, 0);
        end;
      end;
    finally
      if pkey <> nil then
        EvpPkeyFree(pkey);
      if cert <> nil then
        X509free(cert);
    end;
  end
  else
  begin
    Result := True;
  end;
  if Result then
    if FEngineNeedsSHA2Workaround then
      SslCtxCtrl(Fctx, SSL_CTRL_OPTIONS, SslCtxCtrl(Fctx, SSL_CTRL_OPTIONS, 0, nil) or SSL_OP_NO_TLSv1_2, nil);
end;

function TSSLOpenSSLCapi.NeedSigningCertificate: boolean;
begin
  Result := SigningCertificateSpecified and inherited NeedSigningCertificate;
end;

function TSSLOpenSSLCapi.SetSslKeys: boolean;
begin
  Result := False;
  if not assigned(FCtx) then
    Exit;
  try
    if SigningCertificateSpecified and InitEngine then
    begin
      if not LoadSigningCertificate then
        Exit;
      Result := True;
    end;
    if inherited SetSslKeys then
      Result := True;
  finally
    SSLCheck;
  end;
end;

function TSSLOpenSSLCapi.SigningCertificateSpecified: boolean;
begin
  Result := (SigningCertificateID <> '');
end;

{$IFDEF SUPPORTS_REGION}{$ENDREGION}{$ENDIF}

{$IFDEF SUPPORTS_REGION}{$REGION 'Initialization and finalization'}{$ENDIF}
{==============================================================================}
{Initialization and finalization                                               }
{------------------------------------------------------------------------------}

initialization
begin
  FEngineCS := TCriticalSection.Create;
  if InitSSLInterface and ((SSLImplementation = TSSLNone) or (SSLImplementation = TSSLOpenSSL)) then
    SSLImplementation := TSSLOpenSSLCapi;
  {$IFDEF USE_ENGINE_POOL}
  FEnginePool := TEnginePool.Create;
  {$ENDIF}
end;

finalization
begin
  DestroyEngineInterface;
  {$IFDEF USE_ENGINE_POOL}
  FreeAndNil(FEnginePool);
  {$ENDIF}
  FreeAndNil(FEngineCS);
end;

{$IFDEF SUPPORTS_REGION}{$ENDREGION}{$ENDIF}

end.