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;
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 <IPv4 address>");
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;

View File

@ -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;

View File

@ -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;

View File

@ -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;