Use Debug Wrapper Library in tls Connects

This commit is contained in:
René Linder 2023-03-27 16:38:49 +02:00
parent 743e5132ad
commit c786464580

View file

@ -10,7 +10,7 @@
;* ;*
XIncludeFile "lhs_net_socket.pbi" XIncludeFile "lhs_net_socket.pbi"
XIncludeFile "../SYS/lhs_sys_debug_wrapper.pbi"
DeclareModule tls DeclareModule tls
Structure s_tls_server Structure s_tls_server
CA.s CA.s
@ -19,6 +19,11 @@ DeclareModule tls
Password.s Password.s
EndStructure EndStructure
Global LoggerUUID.s = ""
Global Log_Level_Info = 0
Global Log_Level_Debug = 0
Global Log_Level_Error = 2
Declare InitSimpleTLS(CA.s, Cert.s, Key.s, Password.s, TLSID.i = 0) Declare InitSimpleTLS(CA.s, Cert.s, Key.s, Password.s, TLSID.i = 0)
Declare InitTLS(*Settings.s_tls_server, TLSID.i = 0) Declare InitTLS(*Settings.s_tls_server, TLSID.i = 0)
Declare WaitTLSSocket(ServerSocket.i, TLSID.i = 0) Declare WaitTLSSocket(ServerSocket.i, TLSID.i = 0)
@ -111,15 +116,15 @@ Module tls
EndIf EndIf
Wend Wend
If IsLibrary(libressl_tls) If IsLibrary(libressl_tls)
Debug "Initialized with:" + LibraryDir + LibraryName ldl::Logging("Initialized with:" + LibraryDir + LibraryName, LoggerUUID, Log_Level_Info)
Else Else
Debug "Library not found: " + LibraryDir + LibraryName + " Or " + LibraryDir + LibraryName + ".*" ldl::Logging("Library not found: " + LibraryDir + LibraryName + " Or " + LibraryDir + LibraryName + ".*", LoggerUUID, Log_Level_Info)
End End
EndIf EndIf
FinishDirectory(0) FinishDirectory(0)
EndIf EndIf
Else Else
Debug "Initialized with:" + LibraryDir + LibraryName ldl::Logging("Initialized with:" + LibraryDir + LibraryName, LoggerUUID, Log_Level_Info)
EndIf EndIf
Define Call_tls_init = GetFunction(libressl_tls, "tls_init") Define Call_tls_init = GetFunction(libressl_tls, "tls_init")
@ -178,32 +183,32 @@ Module tls
Protected mem_ptr.i, length.i, returns.i Protected mem_ptr.i, length.i, returns.i
If tls_init() <> 0 : ProcedureReturn -1 : EndIf If tls_init() <> 0 : ProcedureReturn -1 : EndIf
tls_cfg = tls_config_new() tls_cfg = tls_config_new()
Debug "TLS cfg obj:"+Str(tls_cfg) ldl::Logging( "TLS cfg obj:"+Str(tls_cfg), LoggerUUID, Log_Level_Info)
Debug "CA:"+*Settings\CA ldl::Logging( "CA:"+*Settings\CA, LoggerUUID, Log_Level_Info)
mem_ptr = tls_load_file(*Settings\CA, @length) mem_ptr = tls_load_file(*Settings\CA, @length)
If Not mem_ptr : tls_config_free(tls_cfg) : ProcedureReturn -2 : EndIf If Not mem_ptr : tls_config_free(tls_cfg) : ProcedureReturn -2 : EndIf
tls_config_set_ca_mem(tls_cfg, mem_ptr, length) tls_config_set_ca_mem(tls_cfg, mem_ptr, length)
Debug "Cert:"+*Settings\Cert ldl::Logging( "Cert:"+*Settings\Cert, LoggerUUID, Log_Level_Info)
mem_ptr = tls_load_file(*Settings\Cert, @length) mem_ptr = tls_load_file(*Settings\Cert, @length)
If Not mem_ptr : tls_config_free(tls_cfg) : ProcedureReturn -3 : EndIf If Not mem_ptr : tls_config_free(tls_cfg) : ProcedureReturn -3 : EndIf
tls_config_set_cert_mem(tls_cfg, mem_ptr, length) tls_config_set_cert_mem(tls_cfg, mem_ptr, length)
If Len(*Settings\Password) > 0 If Len(*Settings\Password) > 0
Debug "Key:"+*Settings\Key + " Password:"+*Settings\Password ldl::Logging( "Key:"+*Settings\Key + " Password:"+*Settings\Password, LoggerUUID, Log_Level_Info)
mem_ptr = tls_load_file_pw(*Settings\Key, @length, *Settings\Password) mem_ptr = tls_load_file_pw(*Settings\Key, @length, *Settings\Password)
If Not mem_ptr : tls_config_free(tls_cfg) : ProcedureReturn -4 : EndIf If Not mem_ptr : tls_config_free(tls_cfg) : ProcedureReturn -4 : EndIf
tls_config_set_key_mem(tls_cfg, mem_ptr, length) tls_config_set_key_mem(tls_cfg, mem_ptr, length)
Else Else
Debug "Key:"+*Settings\Key ldl::Logging( "Key:"+*Settings\Key, LoggerUUID, Log_Level_Info)
mem_ptr = tls_load_file(*Settings\Key, @length) mem_ptr = tls_load_file(*Settings\Key, @length)
If Not mem_ptr : tls_config_free(tls_cfg) : ProcedureReturn -5 : EndIf If Not mem_ptr : tls_config_free(tls_cfg) : ProcedureReturn -5 : EndIf
tls_config_set_key_mem(tls_cfg, mem_ptr, length) tls_config_set_key_mem(tls_cfg, mem_ptr, length)
EndIf EndIf
tls_ctx = tls_server() tls_ctx = tls_server()
Debug "TLS Server obj:"+Str(tls_ctx) ldl::Logging( "TLS Server obj:"+Str(tls_ctx), LoggerUUID, Log_Level_Info)
If Not tls_ctx : tls_config_free(tls_cfg) : ProcedureReturn -6 : EndIf If Not tls_ctx : tls_config_free(tls_cfg) : ProcedureReturn -6 : EndIf
returns = tls_configure(tls_ctx, tls_cfg) returns = tls_configure(tls_ctx, tls_cfg)
Debug "Configure Returns:"+Str(returns) ldl::Logging( "Configure Returns:"+Str(returns), LoggerUUID, Log_Level_Info)
If returns <> 0 :Debug ErrorTLSSrv(): tls_free(tls_ctx) : tls_config_free(tls_cfg) : ProcedureReturn -7 : EndIf If returns <> 0 :ldl::Logging( ErrorTLSSrv(), LoggerUUID, Log_Level_Info): tls_free(tls_ctx) : tls_config_free(tls_cfg) : ProcedureReturn -7 : EndIf
ProcedureReturn 1 ProcedureReturn 1
EndProcedure EndProcedure