Added Initialize and Finalize functions to initialize and cleanup resources of the WolfSSL library. Removed definitions of exceptions.

This commit is contained in:
Joakim Strandberg 2023-07-14 22:20:39 +02:00
parent f49ffc0353
commit 71b28caa09
4 changed files with 41 additions and 21 deletions

View File

@ -131,6 +131,12 @@ package body Tls_Client with SPARK_Mode is
Result : WolfSSL.Subprogram_Result; Result : WolfSSL.Subprogram_Result;
begin 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 if Argument_Count < 1 then
Put_Line ("usage: tcl_client <IPv4 address>"); Put_Line ("usage: tcl_client <IPv4 address>");
return; return;
@ -297,7 +303,10 @@ package body Tls_Client with SPARK_Mode is
SPARK_Sockets.Close_Socket (C); SPARK_Sockets.Close_Socket (C);
WolfSSL.Free (Ssl); WolfSSL.Free (Ssl);
WolfSSL.Free (Context => Ctx); 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 Run;
end Tls_Client; end Tls_Client;

View File

@ -111,6 +111,12 @@ package body Tls_Server with SPARK_Mode is
Input : WolfSSL.Read_Result; Input : WolfSSL.Read_Result;
Option : Option_Type; Option : Option_Type;
begin 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); SPARK_Sockets.Create_Socket (Socket => L);
if not L.Exists then if not L.Exists then
Put_Line ("ERROR: Failed to create socket."); Put_Line ("ERROR: Failed to create socket.");
@ -308,7 +314,11 @@ package body Tls_Server with SPARK_Mode is
end loop; end loop;
SPARK_Sockets.Close_Socket (L); SPARK_Sockets.Close_Socket (L);
WolfSSL.Free (Context => Ctx); 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 Run;
end Tls_Server; end Tls_Server;

View File

@ -44,11 +44,23 @@ package body WolfSSL is
External_Name => "wolfSSL_Cleanup", External_Name => "wolfSSL_Cleanup",
Import => True; 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; Result : constant int := Finalize_WolfSSL;
begin begin
if Result /= WOLFSSL_SUCCESS then if Result = WOLFSSL_SUCCESS then
raise Cleanup_Error; return Success;
else
return Failure;
end if; end if;
end Finalize; end Finalize;
@ -728,9 +740,4 @@ package body WolfSSL is
Ssl := null; Ssl := null;
end Free; end Free;
Result : constant int := Initialize_WolfSSL;
begin
if Result /= WOLFSSL_SUCCESS then
raise Initialization_Error;
end if;
end WolfSSL; end WolfSSL;

View File

@ -25,17 +25,13 @@ with Interfaces.C;
-- the API of this package is used correctly. -- the API of this package is used correctly.
package WolfSSL with SPARK_Mode is package WolfSSL with SPARK_Mode is
procedure Finalize; type Subprogram_Result is (Success, Failure);
-- Must be called before application exit.
Initialization_Error : exception; function Initialize return Subprogram_Result;
-- Raised if error was encountered during initialization of the -- Must be called before usage of the WolfSSL library.
-- WolfSSL library. The WolfSSL libray is initialized during
-- elaboration time.
Cleanup_Error : exception; function Finalize return Subprogram_Result;
-- Raised if error was encountered during application shutdown -- Must be called before application exit to cleanup resources.
-- and cleanup of resources allocated by WolfSSL has failed.
subtype char_array is Interfaces.C.char_array; -- Remove? 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_Index is Interfaces.C.size_t range 0 .. 16_000;
subtype Byte_Array is Interfaces.C.char_array; subtype Byte_Array is Interfaces.C.char_array;
type Subprogram_Result is (Success, Failure);
type Context_Type is limited private; type Context_Type is limited private;
function Is_Valid (Context : Context_Type) return Boolean; function Is_Valid (Context : Context_Type) return Boolean;