Skip to content

Commit 815f99d

Browse files
committed
Ada binding: improve comments and arguments in the PSK case
- Add comments for the PSK value in the example. - Add runtime argument for executing the PSK test. - Warn user that their callback implementation can't be in the SPARK subset.
1 parent 11a40a6 commit 815f99d

2 files changed

Lines changed: 23 additions & 9 deletions

File tree

wrapper/Ada/tls_client.adb

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,23 @@ package body Tls_Client with SPARK_Mode is
6363
Key : chars_ptr;
6464
Key_Max_Length : unsigned) return unsigned
6565
with
66-
SPARK_Mode => Off
66+
SPARK_Mode => Off
6767
is
6868
use type Interfaces.C.unsigned;
6969

7070
Hint_String : constant String := Interfaces.C.Strings.Value (Hint);
71+
72+
-- Identity is OpenSSL testing default for openssl s_client, keep same
7173
Identity_String : constant String := "Client_identity";
74+
-- Test key in hex is 0x1a2b3c4d, in decimal 439,041,101
7275
Key_String : constant String :=
7376
Character'Val (26)
7477
& Character'Val (43)
7578
& Character'Val (60)
7679
& Character'Val (77);
80+
-- These values are aligned with test values in wolfssl/wolfssl/test.h
81+
-- and wolfssl-examples/psk/server-psk.c for testing interoperability.
82+
7783
begin
7884

7985
Ada.Text_IO.Put_Line ("Hint: " & Hint_String);
@@ -199,6 +205,7 @@ package body Tls_Client with SPARK_Mode is
199205

200206
Result : WolfSSL.Subprogram_Result;
201207
DTLS : Boolean;
208+
PSK : Boolean;
202209
begin
203210
Result := WolfSSL.Initialize;
204211
if Result /= Success then
@@ -208,13 +215,19 @@ package body Tls_Client with SPARK_Mode is
208215

209216
if Argument_Count < 1
210217
or Argument_Count > 2
211-
or (Argument_Count = 2 and then Argument (2) /= "--dtls")
218+
or (Argument_Count = 2 and then
219+
Argument (2) /= "--dtls" and then
220+
Argument (2) /= "--psk")
212221
then
213-
Put_Line ("usage: tls_client_main <IPv4 address> [--dtls]");
222+
Put_Line ("usage: tls_client_main <IPv4 address> [--dtls | --psk]");
214223
return;
215224
end if;
216225

217-
DTLS := (SPARK_Terminal.Argument_Count = 2);
226+
DTLS := (SPARK_Terminal.Argument_Count = 2 and then
227+
Argument (2) = "--dtls");
228+
229+
PSK := (SPARK_Terminal.Argument_Count = 2 and then
230+
Argument (2) = "--psk");
218231

219232
if DTLS then
220233
SPARK_Sockets.Create_Datagram_Socket (C);
@@ -276,8 +289,7 @@ package body Tls_Client with SPARK_Mode is
276289
(Context => Ctx,
277290
Mode => WolfSSL.Verify_Peer or WolfSSL.Verify_Fail_If_No_Peer_Cert);
278291

279-
if Ada.Directories.Exists (CERT_FILE) and then
280-
Ada.Directories.Exists (KEY_FILE) then
292+
if not PSK then
281293

282294
-- Load client certificate into WOLFSSL_CTX.
283295
Result := WolfSSL.Use_Certificate_File (Context => Ctx,
@@ -335,10 +347,9 @@ package body Tls_Client with SPARK_Mode is
335347
return;
336348
end if;
337349

338-
if not (Ada.Directories.Exists (CERT_FILE) and then
339-
Ada.Directories.Exists (KEY_FILE)) then
350+
if PSK then
340351

341-
-- Use PSK for authentication.
352+
-- Use PSK for authentication.
342353
WolfSSL.Set_PSK_Client_Callback
343354
(Ssl => Ssl,
344355
Callback => PSK_Client_Callback'Access);

wrapper/Ada/wolfssl.ads

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,9 @@ package WolfSSL with SPARK_Mode is
318318
-- Id_Max_Length - Size of the ID buffer.
319319
-- Key - The key will be stored here.
320320
-- Key_Max_Length - The max size of the key.
321+
--
322+
-- The implementation of this callback will need `SPARK_Mode => Off`
323+
-- since it will require the code to use the C memory model.
321324

322325
procedure Set_PSK_Client_Callback
323326
(Ssl : WolfSSL_Type;

0 commit comments

Comments
 (0)