Skip to content

Commit 7b6f178

Browse files
committed
Initial implementation of secure server communication
Work in progress (only first request works). - Found and fixed problem with `WolfSSL.Get_Error` - PR: wolfSSL/wolfssl#9083
1 parent 6de734b commit 7b6f178

5 files changed

Lines changed: 115 additions & 34 deletions

File tree

TODO.org

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@
2525

2626
* Server
2727
- [X] Consider implementing server state-machine and example
28+
- [ ] Implement secure communications for the server
2829
- [ ] Implement server logic for CRUD
2930
- [ ] Add tests for the server
31+
- [ ] Make the validation profile work for the server
32+
either with the Workarounds package or making SPARKLib 15 to work.
3033

3134
* Upgrades
3235
- [x] (discarded) Upgrade to GNATProve 15 and GNAT 15

libs/wolfssl

src/coap_spark-channel.adb

Lines changed: 84 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
with Ada.Streams;
22
with Ada.Unchecked_Conversion;
33

4+
with CoAP_SPARK.Log;
45

56
with Interfaces.C;
67

@@ -111,6 +112,7 @@ is
111112
begin
112113

113114
Socket.Attached_Socket := (Exists => False);
115+
Socket.Is_Server := Server;
114116

115117
SPARK_Sockets.Create_Datagram_Socket (Socket => Socket.Attached_Socket);
116118

@@ -133,7 +135,10 @@ is
133135

134136
end if;
135137

136-
if Socket.Is_Secure then
138+
if not Socket.Is_Secure then
139+
Socket.Client_Address :=
140+
(Sock_Addr => (Family => GNAT.Sockets.Family_Unspec));
141+
else
137142
Socket.Result := WolfSSL.Initialize;
138143

139144
if Socket.Result /= WolfSSL.Success then
@@ -142,16 +147,31 @@ is
142147

143148
-- Create and initialize WOLFSSL_CTX.
144149
WolfSSL.Create_Context
145-
(Method => WolfSSL.DTLSv1_2_Client_Method, Context => Socket.Ctx);
150+
(Method => (if Server then WolfSSL.DTLSv1_2_Server_Method
151+
else WolfSSL.DTLSv1_2_Client_Method),
152+
Context => Socket.Ctx);
146153

147154
if WolfSSL.Is_Valid (Socket.Ctx) then
148155

156+
WolfSSL.Set_Context_PSK_Server_Callback
157+
(Context => Socket.Ctx, Callback => PSK_Server_Callback);
158+
149159
WolfSSL.Create_WolfSSL (Context => Socket.Ctx, Ssl => Socket.Ssl);
150160

151161
if WolfSSL.Is_Valid (Socket.Ssl) then
152162
if Server then
153-
WolfSSL.Set_PSK_Server_Callback
154-
(Ssl => Socket.Ssl, Callback => PSK_Server_Callback);
163+
-- Attach WolfSSL to the socket.
164+
Socket.Result :=
165+
WolfSSL.Attach
166+
(Ssl => Socket.Ssl,
167+
Socket =>
168+
SPARK_Sockets.To_C (Socket.Attached_Socket.Socket));
169+
170+
if Socket.Result /= WolfSSL.Success then
171+
Finalize (Socket);
172+
return;
173+
end if;
174+
155175
else
156176
WolfSSL.Set_PSK_Client_Callback
157177
(Ssl => Socket.Ssl, Callback => PSK_Client_Callback);
@@ -161,6 +181,47 @@ is
161181
end if;
162182
end Initialize;
163183

184+
procedure Accept_Connection (Socket : in out Socket_Type) is
185+
begin
186+
if Socket.Is_Secure then
187+
Socket.Result := WolfSSL.Accept_Connection (Socket.Ssl);
188+
-- TODO factorize
189+
if Socket.Result /= WolfSSL.Success then
190+
declare
191+
Error_Message : constant WolfSSL.Error_Message :=
192+
WolfSSL.Error
193+
(WolfSSL.Get_Error
194+
(Ssl => Socket.Ssl, Result => Socket.Result));
195+
begin
196+
CoAP_SPARK.Log.Put
197+
("Error shutting-down: ", CoAP_SPARK.Log.Error);
198+
CoAP_SPARK.Log.Put_Line
199+
(Error_Message.Text (1 .. Error_Message.Last),
200+
CoAP_SPARK.Log.Error);
201+
end;
202+
Finalize (Socket);
203+
return;
204+
end if;
205+
Socket.Result := WolfSSL.Accept_Connection (Socket.Ssl);
206+
if Socket.Result /= WolfSSL.Success then
207+
declare
208+
Error_Message : constant WolfSSL.Error_Message :=
209+
WolfSSL.Error
210+
(WolfSSL.Get_Error
211+
(Ssl => Socket.Ssl, Result => Socket.Result));
212+
begin
213+
CoAP_SPARK.Log.Put
214+
("Error accepting connection: ", CoAP_SPARK.Log.Error);
215+
CoAP_SPARK.Log.Put_Line
216+
(Error_Message.Text (1 .. Error_Message.Last),
217+
CoAP_SPARK.Log.Error);
218+
end;
219+
Finalize (Socket);
220+
return;
221+
end if;
222+
end if;
223+
end Accept_Connection;
224+
164225
use type SPARK_Sockets.Family_Type;
165226

166227
function Inet_Address (Host_Name : String) return SPARK_Sockets.Optional_Inet_Addr;
@@ -283,9 +344,16 @@ is
283344
constant Ada.Streams.Stream_Element_Array :=
284345
To_Ada_Stream (Buffer);
285346
begin
286-
Send_Socket
287-
(Socket => Socket,
288-
Item => Data);
347+
if Socket.Is_Server then
348+
Send_Socket
349+
(Socket => Socket,
350+
Item => Data,
351+
To => Socket.Client_Address);
352+
else
353+
Send_Socket
354+
(Socket => Socket,
355+
Item => Data);
356+
end if;
289357
end;
290358
end if;
291359
end Send;
@@ -381,8 +449,15 @@ is
381449
Last : Ada.Streams.Stream_Element_Offset;
382450
begin
383451

384-
Receive_Socket
385-
(Socket => Socket, Item => Data, Last => Last);
452+
if Socket.Is_Server then
453+
Receive_Socket
454+
(Socket => Socket,
455+
Item => Data,
456+
Last => Last,
457+
From => Socket.Client_Address);
458+
else
459+
Receive_Socket (Socket => Socket, Item => Data, Last => Last);
460+
end if;
386461

387462
pragma Assert (Data'Length = Buffer'Length);
388463
Buffer (Buffer'First .. RFLX.RFLX_Builtin_Types.Index'Base (Last)) :=

src/coap_spark-channel.ads

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ is
3535
Relaxed_Initialization => Socket,
3636
Global => null;
3737

38+
procedure Accept_Connection
39+
(Socket : in out Socket_Type)
40+
with
41+
Pre => Is_Valid (Socket),
42+
Global =>
43+
null;
44+
3845
function Is_Valid (Socket : Socket_Type) return Boolean with
3946
Global =>
4047
null;
@@ -59,7 +66,6 @@ is
5966
To : Address_Type) with
6067
Pre => Is_Valid (Socket) and then
6168
Buffer'First = 1 and then
62-
not Socket.Is_Secure and then
6369
Is_Valid (To),
6470
Global =>
6571
null;
@@ -82,7 +88,7 @@ is
8288
Length : out RFLX.RFLX_Builtin_Types.Length;
8389
From : out Address_Type) with
8490
Relaxed_Initialization => (Buffer),
85-
Pre => Is_Valid (Socket) and then Buffer'First = 1 and then not Socket.Is_Secure,
91+
Pre => Is_Valid (Socket) and then Buffer'First = 1,
8692
Post =>
8793
Length <= Buffer'Length and then
8894
Buffer (Buffer'First .. RFLX.RFLX_Builtin_Types.Index'Base (Length))'Initialized and then
@@ -99,14 +105,15 @@ is
99105
private
100106

101107
type Socket_Type (Is_Secure : Boolean) is record
108+
Is_Server : Boolean := False;
102109
Attached_Socket : SPARK_Sockets.Optional_Socket;
103110
case Is_Secure is
104111
when True =>
105112
Ssl : WolfSSL.WolfSSL_Type;
106113
Ctx : WolfSSL.Context_Type;
107114
Result : WolfSSL.Subprogram_Result;
108115
when False =>
109-
null;
116+
Client_Address : Address_Type;
110117
end case;
111118
end record;
112119

src/coap_spark-server_session.adb

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ is
1313
use type Types.Index;
1414

1515
procedure Read (Ctx : FSM.Context;
16-
Skt : in out CoAP_SPARK.Channel.Socket_Type;
17-
To : CoAP_SPARK.Channel.Address_Type) with
16+
Skt : in out CoAP_SPARK.Channel.Socket_Type) with
1817
Pre =>
1918
FSM.Initialized (Ctx)
2019
and then FSM.Has_Data (Ctx, FSM.C_Transport)
@@ -40,15 +39,13 @@ is
4039
(Ctx,
4140
FSM.C_Transport,
4241
Buffer (Buffer'First .. Buffer'First - 2 + Types.Index (Size + 1)));
43-
Channel.Send_To
42+
Channel.Send
4443
(Skt,
45-
Buffer (Buffer'First .. Buffer'First - 2 + Types.Index (Size + 1)),
46-
To);
44+
Buffer (Buffer'First .. Buffer'First - 2 + Types.Index (Size + 1)));
4745
end Read;
4846

4947
procedure Write (Ctx : in out FSM.Context;
50-
Skt : in out CoAP_SPARK.Channel.Socket_Type;
51-
From : out CoAP_SPARK.Channel.Address_Type) with
48+
Skt : in out CoAP_SPARK.Channel.Socket_Type) with
5249
Pre =>
5350
FSM.Initialized (Ctx)
5451
and then FSM.Needs_Data (Ctx, FSM.C_Transport)
@@ -61,7 +58,7 @@ is
6158
with Relaxed_Initialization;
6259
Length : RFLX.RFLX_Builtin_Types.Length;
6360
begin
64-
Channel.Receive (Skt, Buffer, Length, From);
61+
Channel.Receive (Skt, Buffer, Length);
6562
if
6663
Length > 0
6764
and then Length <= FSM.Write_Buffer_Size (Ctx, FSM.C_Transport)
@@ -77,29 +74,28 @@ is
7774
(Ctx : in out FSM.Context;
7875
Skt : in out CoAP_SPARK.Channel.Socket_Type)
7976
is
80-
Client_Address : CoAP_SPARK.Channel.Address_Type;
77+
use type FSM.State;
8178
begin
8279

83-
if Skt.Is_Secure then
84-
-- Not implemented yet.
85-
CoAP_SPARK.Log.Put_Line ("Secure server not implemented yet.", CoAP_SPARK.Log.Error);
86-
Ctx.E.Current_Status := CoAP_SPARK.Unexpected_Case;
87-
return;
88-
end if;
89-
9080
while FSM.Active (Ctx) loop
9181
pragma Loop_Invariant (FSM.Initialized (Ctx));
9282
pragma Loop_Invariant (CoAP_SPARK.Channel.Is_Valid (Skt));
83+
84+
CoAP_SPARK.Log.Put_Line (FSM.Next_State (Ctx)'Image, CoAP_SPARK.Log.Info);
85+
if FSM.Next_State (Ctx) = FSM.S_Receive_Request then
86+
CoAP_SPARK.Channel.Accept_Connection (Skt);
87+
end if;
88+
9389
for C in FSM.Channel'Range loop
9490
pragma Loop_Invariant (FSM.Initialized (Ctx));
95-
exit when not CoAP_SPARK.Channel.Is_Valid (Skt);
96-
if CoAP_SPARK.Channel.Is_Valid (Client_Address) and then FSM.Has_Data (Ctx, C) then
97-
Read (Ctx, Skt, To => Client_Address);
91+
if FSM.Needs_Data (Ctx, C) then
92+
Write (Ctx, Skt);
9893
end if;
9994
exit when not CoAP_SPARK.Channel.Is_Valid (Skt);
100-
if FSM.Needs_Data (Ctx, C) then
101-
Write (Ctx, Skt, From => Client_Address);
95+
if FSM.Has_Data (Ctx, C) then
96+
Read (Ctx, Skt);
10297
end if;
98+
exit when not CoAP_SPARK.Channel.Is_Valid (Skt);
10399
end loop;
104100
exit when not CoAP_SPARK.Channel.Is_Valid (Skt);
105101
FSM.Run (Ctx);

0 commit comments

Comments
 (0)