From 71b28caa096864cd3e52b47b27f3d1aa7b2d444f Mon Sep 17 00:00:00 2001 From: Joakim Strandberg Date: Fri, 14 Jul 2023 22:20:39 +0200 Subject: [PATCH] Added Initialize and Finalize functions to initialize and cleanup resources of the WolfSSL library. Removed definitions of exceptions. --- wrapper/Ada/tls_client.adb | 11 ++++++++++- wrapper/Ada/tls_server.adb | 12 +++++++++++- wrapper/Ada/wolfssl.adb | 23 +++++++++++++++-------- wrapper/Ada/wolfssl.ads | 16 +++++----------- 4 files changed, 41 insertions(+), 21 deletions(-) diff --git a/wrapper/Ada/tls_client.adb b/wrapper/Ada/tls_client.adb index a00303353..9ebbb43b4 100644 --- a/wrapper/Ada/tls_client.adb +++ b/wrapper/Ada/tls_client.adb @@ -131,6 +131,12 @@ package body Tls_Client with SPARK_Mode is Result : WolfSSL.Subprogram_Result; begin + Result := WolfSSL.Initialize; + if Result = Failure then + Put_Line ("ERROR: Failed to initialize the WolfSSL library."); + return; + end if; + if Argument_Count < 1 then Put_Line ("usage: tcl_client "); return; @@ -297,7 +303,10 @@ package body Tls_Client with SPARK_Mode is SPARK_Sockets.Close_Socket (C); WolfSSL.Free (Ssl); WolfSSL.Free (Context => Ctx); - WolfSSL.Finalize; + Result := WolfSSL.Finalize; + if Result = Failure then + Put_Line ("ERROR: Failed to finalize the WolfSSL library."); + end if; end Run; end Tls_Client; diff --git a/wrapper/Ada/tls_server.adb b/wrapper/Ada/tls_server.adb index 1eaca1349..377db03a0 100644 --- a/wrapper/Ada/tls_server.adb +++ b/wrapper/Ada/tls_server.adb @@ -111,6 +111,12 @@ package body Tls_Server with SPARK_Mode is Input : WolfSSL.Read_Result; Option : Option_Type; begin + Result := WolfSSL.Initialize; + if Result = Failure then + Put_Line ("ERROR: Failed to initialize the WolfSSL library."); + return; + end if; + SPARK_Sockets.Create_Socket (Socket => L); if not L.Exists then Put_Line ("ERROR: Failed to create socket."); @@ -308,7 +314,11 @@ package body Tls_Server with SPARK_Mode is end loop; SPARK_Sockets.Close_Socket (L); WolfSSL.Free (Context => Ctx); - WolfSSL.Finalize; + Result := WolfSSL.Finalize; + if Result = Failure then + Put_Line ("ERROR: Failed to finalize the WolfSSL library."); + return; + end if; end Run; end Tls_Server; diff --git a/wrapper/Ada/wolfssl.adb b/wrapper/Ada/wolfssl.adb index 85c69d279..8ce762540 100644 --- a/wrapper/Ada/wolfssl.adb +++ b/wrapper/Ada/wolfssl.adb @@ -44,11 +44,23 @@ package body WolfSSL is External_Name => "wolfSSL_Cleanup", Import => True; - procedure Finalize is + function Initialize return Subprogram_Result is + Result : constant int := Initialize_WolfSSL; + begin + if Result = WOLFSSL_SUCCESS then + return Success; + else + return Failure; + end if; + end Initialize; + + function Finalize return Subprogram_Result is Result : constant int := Finalize_WolfSSL; begin - if Result /= WOLFSSL_SUCCESS then - raise Cleanup_Error; + if Result = WOLFSSL_SUCCESS then + return Success; + else + return Failure; end if; end Finalize; @@ -728,9 +740,4 @@ package body WolfSSL is Ssl := null; end Free; - Result : constant int := Initialize_WolfSSL; -begin - if Result /= WOLFSSL_SUCCESS then - raise Initialization_Error; - end if; end WolfSSL; diff --git a/wrapper/Ada/wolfssl.ads b/wrapper/Ada/wolfssl.ads index 02c04ef33..4fa778396 100644 --- a/wrapper/Ada/wolfssl.ads +++ b/wrapper/Ada/wolfssl.ads @@ -25,17 +25,13 @@ with Interfaces.C; -- the API of this package is used correctly. package WolfSSL with SPARK_Mode is - procedure Finalize; - -- Must be called before application exit. + type Subprogram_Result is (Success, Failure); - Initialization_Error : exception; - -- Raised if error was encountered during initialization of the - -- WolfSSL library. The WolfSSL libray is initialized during - -- elaboration time. + function Initialize return Subprogram_Result; + -- Must be called before usage of the WolfSSL library. - Cleanup_Error : exception; - -- Raised if error was encountered during application shutdown - -- and cleanup of resources allocated by WolfSSL has failed. + function Finalize return Subprogram_Result; + -- Must be called before application exit to cleanup resources. subtype char_array is Interfaces.C.char_array; -- Remove? @@ -43,8 +39,6 @@ package WolfSSL with SPARK_Mode is subtype Byte_Index is Interfaces.C.size_t range 0 .. 16_000; subtype Byte_Array is Interfaces.C.char_array; - type Subprogram_Result is (Success, Failure); - type Context_Type is limited private; function Is_Valid (Context : Context_Type) return Boolean;