Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global DLL Handlers implementation #321

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Lib/Protocols/IdSSLOpenSSL.pas
Original file line number Diff line number Diff line change
Expand Up @@ -2365,6 +2365,17 @@ function LoadOpenSSLLibrary: Boolean;
if not Result then begin
Exit;
end;
//
// Zdravko Gabrovski - Check for a global openSSL pointers
//
{$IFNDEF STATICLOAD_OPENSSL}
if ( GlobalIdCrypto <> IdNilHandle ) and ( GlobalIdSSL <> IdNilHandle ) then begin
SSLIsLoaded.Value := True;
Result := True;
Exit;
end;
{$ENDIF}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the 2 global handles have been assigned, exiting here will bypass Indy's initialization of OpenSSL's thread-locking callbacks. Are you sure this is intended behavior?

{$IFDEF OPENSSL_SET_MEMORY_FUNCS}
// has to be done before anything that uses memory
IdSslCryptoMallocInit;
Expand Down Expand Up @@ -2412,6 +2423,17 @@ procedure UnLoadOpenSSLLibrary;
LList: TIdCriticalSectionList;
{$ENDIF}
begin
//
// Zdravko Gabrovski - check for GLOBAL OpenSSL handlers
//
{$IFNDEF STATICLOAD_OPENSSL}
if ( GlobalIdCrypto <> IdNilHandle ) and ( GlobalIdSSL <> IdNilHandle ) then begin
InitializeFuncPointers;
SSLIsLoaded.Value := False;
Exit;
end;
{$ENDIF}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the 2 global handles have been assigned, exiting here will bypass Indy's cleanup of OpenSSL's thread-locking callbacks. Are you sure this is intended behavior?

// ssl was never loaded
if Assigned(CRYPTO_set_locking_callback) then begin
CRYPTO_set_locking_callback(nil);
Expand Down
181 changes: 133 additions & 48 deletions Lib/Protocols/IdSSLOpenSSLHeaders.pas
Original file line number Diff line number Diff line change
Expand Up @@ -14793,7 +14793,7 @@ lhash_of_SSL_SESSION = record

// PASN1_UTCTIME = Pointer;

//GREGOR - spremenjana deklaracija ker se tol�e
//GREGOR - spremenjana deklaracija ker se tol\E8e
// Phostent = Pointer;
{$NODEFINE Phostent2}
Phostent2 = Pointer;
Expand Down Expand Up @@ -18922,6 +18922,26 @@ function RAND_status() : integer;
function RAND_event(iMsg : UINT; wp : wparam; lp : lparam) : integer;
procedure RAND_screen();
{$ENDIF}
//
// Zdravko Gabrovski - Global openSSL handlers implementation
//
procedure InitializeFuncPointers;

{$IFNDEF STATICLOAD_OPENSSL}
function GlobalIdSSL : TIdLibHandle;
function GlobalIdCrypto : TIdLibHandle;
procedure SetGlobalIdSSL ( fGlobalIdSSL : TIdLibHandle );
procedure SetGlobalIdCrypto ( fGlobalIdCrypto : TIdLibHandle );
var
hGlobalIdSSL : TIdLibHandle = IdNilHandle;
hGlobalIdCrypto : TIdLibHandle = IdNilHandle;
csGlobalIDSSL : TIdCriticalSection;

hIdSSL : TIdLibHandle = IdNilHandle;
hIdCrypto : TIdLibHandle = IdNilHandle;

{$ENDIF}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the point of declaring getter/setter functions if you are also making the variables themselves public as well? The variables should not be in the interface, only the implementation.


implementation

Expand All @@ -18937,6 +18957,52 @@ implementation
, DynLibs // needed for FreeLibrary
{$ENDIF}
{$ENDIF};
//
// Zdravko Gabrovski - global OpenSSL Handler implementation
//
{$IFNDEF STATICLOAD_OPENSSL}
function GlobalIdSSL : TIdLibHandle;
begin
csGlobalIDSSL.Acquire;
try
Result := hGlobalIdSSL;
finally
csGlobalIDSSL.Release;
end;
end;

function GlobalIdCrypto : TIdLibHandle;
begin
csGlobalIDSSL.Acquire;
try
Result := hGlobalIdCrypto;
finally
csGlobalIDSSL.Release;
end;
end;

procedure SetGlobalIdSSL ( fGlobalIdSSL : TIdLibHandle );
begin
csGlobalIDSSL.Acquire;
try
hGlobalIdSSL := fGlobalIdSSL;
finally
csGlobalIDSSL.Release;
end;
end;

procedure SetGlobalIdCrypto ( fGlobalIdCrypto : TIdLibHandle );
begin
csGlobalIDSSL.Acquire;
try
hGlobalIdCrypto := fGlobalIdCrypto;
finally
csGlobalIDSSL.Release;
end;
end;
{$ENDIF}



{$IFNDEF OPENSSL_NO_HMAC}
procedure HMAC_Init_ex(ctx : PHMAC_CTX; key : Pointer; len : TIdC_INT;
Expand Down Expand Up @@ -19594,8 +19660,6 @@ class procedure EIdOpenSSLAPISSLError.RaiseExceptionCode(const AErrCode, ARetCod
{$IFDEF STATICLOAD_OPENSSL}
bIsLoaded : Boolean = False;
{$ELSE}
hIdSSL : TIdLibHandle = IdNilHandle;
hIdCrypto : TIdLibHandle = IdNilHandle;
FFailedLoadList : TStringList;
{$ENDIF}

Expand Down Expand Up @@ -19674,10 +19738,10 @@ function GetCryptLibHandle : TIdLibHandle;
{$IFNDEF OPENSSL_NO_FP_API}
{CH fn_NCONF_load_fp = 'NCONF_load_fp'; }{Do not localize}
{$ENDIF}
{CH fn_NCONF_load_bio = 'NCONF_load_bio'; {Do not localize}
{CH fn_NCONF_get_section = 'NCONF_get_section'; {Do not localize}
{CH fn_NCONF_get_string = 'NCONF_get_string'; {Do not localize}
{CH fn_NCONF_get_number_e = 'NCONF_get_number_e'; {Do not localize}
{CH fn_NCONF_load_bio = 'NCONF_load_bio';} {Do not localize}
{CH fn_NCONF_get_section = 'NCONF_get_section';} {Do not localize}
{CH fn_NCONF_get_string = 'NCONF_get_string';} {Do not localize}
{CH fn_NCONF_get_number_e = 'NCONF_get_number_e';} {Do not localize}
{CH fn_NCONF_dump_fp = 'NCONF_dump_fp'; } {Do not localize}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have incorporated this change into the master code. Please resync your PR.

{CH fn_NCONF_dump_bio = 'NCONF_dump_bio'; }{Do not localize}
{CH fn_CONF_modules_load = 'CONF_modules_load'; } {Do not localize}
Expand Down Expand Up @@ -19730,7 +19794,7 @@ function GetCryptLibHandle : TIdLibHandle;
{CH fn_CRYPTO_pop_info = 'CRYPTO_pop_info'; } {Do not localize}
{CH fn_CRYPTO_remove_all_info = 'CRYPTO_remove_all_info'; } {Do not localize}
{CH fn_OpenSSLDie = 'OpenSSLDie'; } {Do not localize}
{CH fn_OPENSSL_ia32cap_loc = 'OPENSSL_ia32cap_loc'; { {Do not localize}
{CH fn_OPENSSL_ia32cap_loc = 'OPENSSL_ia32cap_loc';} {Do not localize}
{CH fn_CRYPTO_get_new_lockid = 'CRYPTO_get_new_lockid'; } {Do not localize}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have incorporated this change into the master code. Please resync your PR.

fn_CRYPTO_num_locks = 'CRYPTO_num_locks'; {Do not localize}
fn_CRYPTO_lock = 'CRYPTO_lock'; {Do not localize}
Expand Down Expand Up @@ -20033,49 +20097,49 @@ function GetCryptLibHandle : TIdLibHandle;
{CH fn_des_read_pw = 'DES_read_pw'; } {Do not localize}
{CH fn_des_cblock_print_file = 'DES_cblock_print_file'; } {Do not localize}
//des_old.h
{CH fn__ossl_old_des_options = '_ossl_old_des_options'; {Do not localize}
{CH fn__ossl_old_des_ecb3_encrypt = '_ossl_old_des_ecb3_encrypt'; {Do not localize}
{CH fn__ossl_old_des_cbc_cksum = '_ossl_old_des_cbc_cksum'; {Do not localize}
{CH fn__ossl_old_des_cbc_encrypt = '_ossl_old_des_cbc_encrypt'; {Do not localize}
{CH fn__ossl_old_des_ncbc_encrypt = '_ossl_old_des_ncbc_encrypt'; {Do not localize}
{CH fn__ossl_old_des_xcbc_encrypt = '_ossl_old_des_xcbc_encrypt'; {Do not localize}
{CH fn__ossl_old_des_cfb_encrypt = '_ossl_old_des_cfb_encrypt'; {Do not localize}
{CH fn__ossl_old_des_options = '_ossl_old_des_options';} {Do not localize}
{CH fn__ossl_old_des_ecb3_encrypt = '_ossl_old_des_ecb3_encrypt';} {Do not localize}
{CH fn__ossl_old_des_cbc_cksum = '_ossl_old_des_cbc_cksum';} {Do not localize}
{CH fn__ossl_old_des_cbc_encrypt = '_ossl_old_des_cbc_encrypt';} {Do not localize}
{CH fn__ossl_old_des_ncbc_encrypt = '_ossl_old_des_ncbc_encrypt';} {Do not localize}
{CH fn__ossl_old_des_xcbc_encrypt = '_ossl_old_des_xcbc_encrypt';} {Do not localize}
{CH fn__ossl_old_des_cfb_encrypt = '_ossl_old_des_cfb_encrypt';} {Do not localize}
fn__ossl_old_des_ecb_encrypt = '_ossl_old_des_ecb_encrypt'; {Do not localize}
{CH fn__ossl_old_des_encrypt = '_ossl_old_des_encrypt'; {Do not localize}
{CH fn__ossl_old_des_encrypt2 = '_ossl_old_des_encrypt2'; {Do not localize}
{CH fn__ossl_old_des_encrypt3 = '_ossl_old_des_encrypt3'; {Do not localize}
{CH fn__ossl_old_des_decrypt3 = '_ossl_old_des_decrypt3'; {Do not localize}
{CH fn__ossl_old_des_ede3_cbc_encrypt = '_ossl_old_des_ede3_cbc_encrypt'; {Do not localize}
{CH fn__ossl_old_des_ede3_cfb64_encrypt = '_ossl_old_des_ede3_cfb64_encrypt'; {Do not localize}
{CH fn__ossl_old_des_ede3_ofb64_encrypt = '_ossl_old_des_ede3_ofb64_encrypt'; {Do not localize}
{CH fn__ossl_old_des_encrypt = '_ossl_old_des_encrypt';} {Do not localize}
{CH fn__ossl_old_des_encrypt2 = '_ossl_old_des_encrypt2';}{Do not localize}
{CH fn__ossl_old_des_encrypt3 = '_ossl_old_des_encrypt3';} {Do not localize}
{CH fn__ossl_old_des_decrypt3 = '_ossl_old_des_decrypt3';} {Do not localize}
{CH fn__ossl_old_des_ede3_cbc_encrypt = '_ossl_old_des_ede3_cbc_encrypt';} {Do not localize}
{CH fn__ossl_old_des_ede3_cfb64_encrypt = '_ossl_old_des_ede3_cfb64_encrypt';} {Do not localize}
{CH fn__ossl_old_des_ede3_ofb64_encrypt = '_ossl_old_des_ede3_ofb64_encrypt';} {Do not localize}
{$IFDEF USE_THIS}
{CH fn__ossl_old_des_xwhite_in2out = '_ossl_old_des_xwhite_in2out'; {Do not localize}
{CH fn__ossl_old_des_xwhite_in2out = '_ossl_old_des_xwhite_in2out';} {Do not localize}
{$ENDIF}
{CH fn__ossl_old_des_enc_read = '_ossl_old_des_enc_read'; {Do not localize}
{CH fn__ossl_old_des_enc_write = '_ossl_old_des_enc_write'; {Do not localize}
{CH fn__ossl_old_des_fcrypt = '_ossl_old_des_fcrypt'; {Do not localize}
{CH fn__ossl_old_des_crypt = '_ossl_old_des_crypt'; {Do not localize}
{CH fn__ossl_old_des_enc_read = '_ossl_old_des_enc_read';} {Do not localize}
{CH fn__ossl_old_des_enc_write = '_ossl_old_des_enc_write';} {Do not localize}
{CH fn__ossl_old_des_fcrypt = '_ossl_old_des_fcrypt';} {Do not localize}
{CH fn__ossl_old_des_crypt = '_ossl_old_des_crypt';} {Do not localize}
{$IFNDEF PERL5}
{$IFNDEF NeXT}
{CH fn__ossl_old_crypt = '_ossl_old_crypt'; {Do not localize}
{CH fn__ossl_old_crypt = '_ossl_old_crypt';} {Do not localize}
{$ENDIF}
{$ENDIF}
{CH fn__ossl_old_des_ofb_encrypt = '_ossl_old_des_ofb_encrypt'; {Do not localize}
{CH fn__ossl_old_des_pcbc_encrypt = '_ossl_old_des_pcbc_encrypt'; {Do not localize}
{CH fn__ossl_old_des_quad_cksum = '_ossl_old_des_quad_cksum'; {Do not localize}
{CH fn__ossl_old_des_random_seed = '_ossl_old_des_random_seed'; {Do not localize}
{CH fn__ossl_old_des_random_key = '_ossl_old_des_random_key'; {Do not localize}
{CH fn__ossl_old_des_read_password = '_ossl_old_des_read_password'; {Do not localize}
{CH fn__ossl_old_des_read_2passwords = '_ossl_old_des_read_2passwords'; {Do not localize}
{CH fn__ossl_old_des_ofb_encrypt = '_ossl_old_des_ofb_encrypt';} {Do not localize}
{CH fn__ossl_old_des_pcbc_encrypt = '_ossl_old_des_pcbc_encrypt';} {Do not localize}
{CH fn__ossl_old_des_quad_cksum = '_ossl_old_des_quad_cksum';} {Do not localize}
{CH fn__ossl_old_des_random_seed = '_ossl_old_des_random_seed';} {Do not localize}
{CH fn__ossl_old_des_random_key = '_ossl_old_des_random_key';} {Do not localize}
{CH fn__ossl_old_des_read_password = '_ossl_old_des_read_password';} {Do not localize}
{CH fn__ossl_old_des_read_2passwords = '_ossl_old_des_read_2passwords';} {Do not localize}
fn__ossl_old_des_set_odd_parity = '_ossl_old_des_set_odd_parity'; {Do not localize}
{CH fn__ossl_old_des_is_weak_key = '_ossl_old_des_is_weak_key'; {Do not localize}
{CH fn__ossl_old_des_is_weak_key = '_ossl_old_des_is_weak_key';} {Do not localize}
fn__ossl_old_des_set_key = '_ossl_old_des_set_key'; {Do not localize}
{CH fn__ossl_old_des_key_sched = '_ossl_old_des_key_sched'; {Do not localize}
{CH fn__ossl_old_des_string_to_key = '_ossl_old_des_string_to_key'; {Do not localize}
{CH fn__ossl_old_des_string_to_2keys = '_ossl_old_des_string_to_2keys'; {Do not localize}
{CH fn__ossl_old_des_cfb64_encrypt = '_ossl_old_des_cfb64_encrypt'; {Do not localize}
{CH fn__ossl_old_des_ofb64_encrypt = '_ossl_old_des_ofb64_encrypt'; {Do not localize}
{CH fn__ossl_096_des_random_seed = '_ossl_096_des_random_seed'; {Do not localize}
{CH fn__ossl_old_des_key_sched = '_ossl_old_des_key_sched';} {Do not localize}
{CH fn__ossl_old_des_string_to_key = '_ossl_old_des_string_to_key';} {Do not localize}
{CH fn__ossl_old_des_string_to_2keys = '_ossl_old_des_string_to_2keys';} {Do not localize}
{CH fn__ossl_old_des_cfb64_encrypt = '_ossl_old_des_cfb64_encrypt';} {Do not localize}
{CH fn__ossl_old_des_ofb64_encrypt = '_ossl_old_des_ofb64_encrypt';} {Do not localize}
{CH fn__ossl_096_des_random_seed = '_ossl_096_des_random_seed';} {Do not localize}
{$ENDIF}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have incorporated these changes into the master code. Please resync your PR.

{$IFNDEF OPENSSL_NO_RC4}
{CH fn_RC4_options = 'RC4_options'; } {Do not localize}
Expand Down Expand Up @@ -20764,7 +20828,7 @@ function GetCryptLibHandle : TIdLibHandle;
{CH fn_ASN1_mbstring_copy = 'ASN1_mbstring_copy'; } {Do not localize}
{CH fn_ASN1_mbstring_ncopy = 'ASN1_mbstring_ncopy'; } {Do not localize}
{CH fn_ASN1_STRING_set_by_NID = 'ASN1_STRING_set_by_NID'; } {Do not localize}
{CH fn_ASN1_STRING_TABLE_get = 'ASN1_STRING_TABLE_get'; {Do not localize}
{CH fn_ASN1_STRING_TABLE_get = 'ASN1_STRING_TABLE_get';} {Do not localize}
{CH fn_ASN1_STRING_TABLE_add = 'ASN1_STRING_TABLE_add'; } {Do not localize}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have incorporated this change into the master code. Please resync your PR.

{CH fn_ASN1_STRING_TABLE_cleanup = 'ASN1_STRING_TABLE_cleanup'; } {Do not localize}
{CH fn_ASN1_item_new = 'ASN1_item_new'; } {Do not localize}
Expand Down Expand Up @@ -22082,7 +22146,7 @@ function GetCryptLibHandle : TIdLibHandle;
{CH fn_SSL_use_RSAPrivateKey_ASN1 = 'SSL_use_RSAPrivateKey_ASN1'; } {Do not localize}
{$ENDIF}
{CH fn_SSL_use_PrivateKey = 'SSL_use_PrivateKey'; } {Do not localize}
{CH fn_SSL_use_PrivateKey_ASN1 = 'SSL_use_PrivateKey_ASN1'; {Do not localize}
{CH fn_SSL_use_PrivateKey_ASN1 = 'SSL_use_PrivateKey_ASN1';} {Do not localize}
{CH fn_SSL_use_certificate = 'SSL_use_certificate'; } {Do not localize}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have incorporated this change into the master code. Please resync your PR.

{CH fn_SSL_use_certificate_ASN1 = 'SSL_use_certificate_ASN1'; } {Do not localize}
{CH fn_SSL_use_RSAPrivateKey_file = 'SSL_use_RSAPrivateKey_file'; } {Do not localize}
Expand Down Expand Up @@ -22437,7 +22501,7 @@ function GetCryptLibHandle : TIdLibHandle;
{CH fn_ENGINE_register_RAND = 'ENGINE_register_RAND'; } {Do not localize}
{CH fn_ENGINE_unregister_RAND = 'ENGINE_unregister_RAND'; } {Do not localize}
{CH fn_ENGINE_register_all_RAND = 'ENGINE_register_all_RAND'; } {Do not localize}
{CH fn_ENGINE_register_STORE = 'ENGINE_register_STORE'; { {Do not localize}
{CH fn_ENGINE_register_STORE = 'ENGINE_register_STORE'; } {Do not localize}
{CH fn_ENGINE_unregister_STORE = 'ENGINE_unregister_STORE'; } {Do not localize}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have incorporated this change into the master code. Please resync your PR.

{CH fn_ENGINE_register_all_STORE = 'ENGINE_register_all_STORE'; } {Do not localize}
{CH fn_ENGINE_register_ciphers = 'ENGINE_register_ciphers'; } {Do not localize}
Expand Down Expand Up @@ -22817,15 +22881,32 @@ function Load: Boolean;
FFailedLoadList.Clear;

if hIdCrypto = IdNilHandle then begin
hIdCrypto := LoadSSLCryptoLibrary;

//
// Zdravko Gabrovski - Global OpenSSL handler implementation
//
if GlobalIdCrypto <> IdNilHandle then
hIdCrypto := GlobalIdCrypto
else begin
hIdCrypto := LoadSSLCryptoLibrary;
end;

if hIdCrypto = IdNilHandle then begin
FFailedLoadList.Add(IndyFormat(RSOSSFailedToLoad, [GIdOpenSSLPath + SSLCLIB_DLL_name {$IFDEF UNIX}+ LIBEXT{$ENDIF}]));
Exit;
end;
end;

if hIdSSL = IdNilHandle then begin
hIdSSL := LoadSSLLibrary;

//
// Zdravko Gabrovski - Global OpenSSL handler implementation
//
if GlobalIdSSL <> IdNilHandle then
hIdSSL := GlobalIdSSL
else
hIdSSL := LoadSSLLibrary;

if hIdSSL = IdNilHandle then begin
FFailedLoadList.Add(IndyFormat(RSOSSFailedToLoad, [GIdOpenSSLPath + SSL_DLL_name {$IFDEF UNIX}+ LIBEXT{$ENDIF}]));
Exit;
Expand Down Expand Up @@ -26790,6 +26871,8 @@ initialization
InitializeFuncPointers;
{$ELSE}
FFailedLoadList := TStringList.Create;
// Z.Gabrovski - Global OpenSSL Handler implementation
csGlobalIDSSL := TIdCriticalSection.Create;
{$ENDIF}
SetFIPSMode := OpenSSLSetFIPSMode;
GetFIPSMode := OpenSSLGetFIPSMode;
Expand Down Expand Up @@ -26830,6 +26913,8 @@ initialization
{$IFNDEF STATICLOAD_OPENSSL}
finalization
FreeAndNil(FFailedLoadList);
// Z.Gabrovski - Global OpenSSL Handler implementation
FreeAndNil(csGlobalIDSSL);
{$ENDIF}

end.
end.