@@ -32,21 +32,29 @@ package body Tls_Client with SPARK_Mode is
3232 use type WolfSSL.Mode_Type;
3333 use type WolfSSL.Byte_Index;
3434 use type WolfSSL.Byte_Array;
35+ use type WolfSSL.Subprogram_Result;
3536
36- use all type WolfSSL.Subprogram_Result;
37+ subtype Byte_Index is WolfSSL.Byte_Index;
38+
39+ Success : WolfSSL.Subprogram_Result renames WolfSSL.Success;
3740
3841 subtype Byte_Type is WolfSSL.Byte_Type;
3942
40- package Integer_IO is new Ada.Text_IO.Integer_IO (Integer );
43+ package Natural_IO is new Ada.Text_IO.Integer_IO (Natural );
4144
4245 procedure Put (Text : String) is
4346 begin
4447 Ada.Text_IO.Put (Text);
4548 end Put ;
4649
47- procedure Put (Number : Integer) is
50+ procedure Put (Number : Natural) is
51+ begin
52+ Natural_IO.Put (Item => Number, Width => 0 , Base => 10 );
53+ end Put ;
54+
55+ procedure Put (Number : Byte_Index) is
4856 begin
49- Integer_IO .Put (Item => Number, Width => 0 , Base => 10 );
57+ Natural_IO .Put (Item => Natural ( Number) , Width => 0 , Base => 10 );
5058 end Put ;
5159
5260 procedure Put_Line (Text : String) is
@@ -120,19 +128,18 @@ package body Tls_Client with SPARK_Mode is
120128
121129 Addr : SPARK_Sockets.Optional_Inet_Addr;
122130
123- Bytes_Written : Integer;
124-
125131 Count : WolfSSL.Byte_Index;
126132
127133 Text : String (1 .. 200 );
128- Last : Integer ;
134+ Last : Natural ;
129135
130- Input : WolfSSL.Read_Result;
136+ Input : WolfSSL.Read_Result;
137+ Output : WolfSSL.Write_Result;
131138
132139 Result : WolfSSL.Subprogram_Result;
133140 begin
134141 Result := WolfSSL.Initialize;
135- if Result = Failure then
142+ if Result /= Success then
136143 Put_Line (" ERROR: Failed to initialize the WolfSSL library." );
137144 return ;
138145 end if ;
@@ -162,7 +169,7 @@ package body Tls_Client with SPARK_Mode is
162169
163170 Result := SPARK_Sockets.Connect_Socket (Socket => C.Socket,
164171 Server => A);
165- if Result = Failure then
172+ if Result /= Success then
166173 Put_Line (" ERROR: Failed to connect to server." );
167174 SPARK_Sockets.Close_Socket (C);
168175 Set (Exit_Status_Failure);
@@ -183,7 +190,7 @@ package body Tls_Client with SPARK_Mode is
183190 Result := WolfSSL.Use_Certificate_File (Context => Ctx,
184191 File => CERT_FILE,
185192 Format => WolfSSL.Format_Pem);
186- if Result = Failure then
193+ if Result /= Success then
187194 Put (" ERROR: failed to load " );
188195 Put (CERT_FILE);
189196 Put (" , please check the file." );
@@ -198,7 +205,7 @@ package body Tls_Client with SPARK_Mode is
198205 Result := WolfSSL.Use_Private_Key_File (Context => Ctx,
199206 File => KEY_FILE,
200207 Format => WolfSSL.Format_Pem);
201- if Result = Failure then
208+ if Result /= Success then
202209 Put (" ERROR: failed to load " );
203210 Put (KEY_FILE);
204211 Put (" , please check the file." );
@@ -213,7 +220,7 @@ package body Tls_Client with SPARK_Mode is
213220 Result := WolfSSL.Load_Verify_Locations (Context => Ctx,
214221 File => CA_FILE,
215222 Path => " " );
216- if Result = Failure then
223+ if Result /= Success then
217224 Put (" ERROR: failed to load " );
218225 Put (CA_FILE);
219226 Put (" , please check the file." );
@@ -237,7 +244,7 @@ package body Tls_Client with SPARK_Mode is
237244 -- Attach wolfSSL to the socket.
238245 Result := WolfSSL.Attach (Ssl => Ssl,
239246 Socket => SPARK_Sockets.To_C (C.Socket));
240- if Result = Failure then
247+ if Result /= Success then
241248 Put_Line (" ERROR: Failed to set the file descriptor." );
242249 SPARK_Sockets.Close_Socket (C);
243250 WolfSSL.Free (Ssl);
@@ -247,7 +254,7 @@ package body Tls_Client with SPARK_Mode is
247254 end if ;
248255
249256 Result := WolfSSL.Connect (Ssl);
250- if Result = Failure then
257+ if Result /= Success then
251258 Put_Line (" ERROR: failed to connect to wolfSSL." );
252259 SPARK_Sockets.Close_Socket (C);
253260 WolfSSL.Free (Ssl);
@@ -262,12 +269,21 @@ package body Tls_Client with SPARK_Mode is
262269 SPARK_Sockets.To_C (Item => Text (1 .. Last),
263270 Target => D,
264271 Count => Count);
265- Bytes_Written := WolfSSL.Write (Ssl => Ssl,
266- Data => D (1 .. Count));
267- if Bytes_Written < Last then
272+ Output := WolfSSL.Write (Ssl => Ssl,
273+ Data => D (1 .. Count));
274+ if not Output.Success then
275+ Put (" ERROR: write failure" );
276+ New_Line;
277+ SPARK_Sockets.Close_Socket (C);
278+ WolfSSL.Free (Ssl);
279+ WolfSSL.Free (Context => Ctx);
280+ return ;
281+ end if ;
282+
283+ if Natural (Output.Bytes_Written) < Last then
268284 Put (" ERROR: failed to write entire message" );
269285 New_Line;
270- Put (Bytes_Written);
286+ Put (Output. Bytes_Written);
271287 Put (" bytes of " );
272288 Put (Last);
273289 Put (" bytes were sent" );
@@ -279,7 +295,7 @@ package body Tls_Client with SPARK_Mode is
279295 end if ;
280296
281297 Input := WolfSSL.Read (Ssl);
282- if Input.Result /= Success then
298+ if not Input.Success then
283299 Put_Line (" Read error." );
284300 Set (Exit_Status_Failure);
285301 SPARK_Sockets.Close_Socket (C);
@@ -304,7 +320,7 @@ package body Tls_Client with SPARK_Mode is
304320 WolfSSL.Free (Ssl);
305321 WolfSSL.Free (Context => Ctx);
306322 Result := WolfSSL.Finalize;
307- if Result = Failure then
323+ if Result /= Success then
308324 Put_Line (" ERROR: Failed to finalize the WolfSSL library." );
309325 end if ;
310326 end Run ;
0 commit comments