pla_util_2.1.2_815e4700/pla/src/power_line_adapters.adb

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
--  SPDX-License-Identifier: GPL-3.0-or-later
------------------------------------------------------------------------
--  pla-util - A power line adapter utility
--  Copyright (C) 2016-2023 John Serock
--
--  This file is part of pla-util.
--
--  pla-util is free software: you can redistribute it and/or modify
--  it under the terms of the GNU General Public License as published by
--  the Free Software Foundation, either version 3 of the License, or
--  (at your option) any later version.
--
--  pla-util is distributed in the hope that it will be useful,
--  but WITHOUT ANY WARRANTY; without even the implied warranty of
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
--  GNU General Public License for more details.
--
--  You should have received a copy of the GNU General Public License
--  along with this program. If not, see <http://www.gnu.org/licenses/>.
------------------------------------------------------------------------
with Ada.Characters.Latin_1;
with GNAT.SHA256;
with Power_Line_Adapters.Network;
with To_Bounded_String;

package body Power_Line_Adapters is

   function "<" (Left  : Adapter_Type;
                 Right : Adapter_Type) return Boolean is

      use type MAC_Addresses.MAC_Address_Type;

   begin

      if Left.Network_Interface = Right.Network_Interface then
         return Left.MAC_Address < Right.MAC_Address;
      else
         return Left.Network_Interface < Right.Network_Interface;
      end if;

   end "<";

   overriding function "=" (Left  : Adapter_Type;
                            Right : Adapter_Type) return Boolean is

      use type MAC_Addresses.MAC_Address_Type;

   begin

      return Left.Network_Interface = Right.Network_Interface and then Left.MAC_Address = Right.MAC_Address;

   end "=";

   function Generate_DAK (Passphrase : String) return Octets.Key_Type is

      Salt : constant Ada.Streams.Stream_Element_Array (1 .. 8) := (16#08#, 16#85#, 16#6d#, 16#af#, 16#7c#, 16#f5#, 16#81#, 16#85#);

   begin

      return Generate_Key (Passphrase => Passphrase,
                           Salt       => Salt);

   end Generate_DAK;

   function Generate_Key (Passphrase : String;
                          Salt       : Ada.Streams.Stream_Element_Array) return Octets.Key_Type is

      use type Ada.Streams.Stream_Element_Offset;

      Key    : Octets.Key_Type;
      J      : Ada.Streams.Stream_Element_Offset;
      Digest : GNAT.SHA256.Binary_Message_Digest;
      PS     : Ada.Streams.Stream_Element_Array (1 .. Passphrase'Length + Salt'Length);

   begin

      J := 1;
      for I in Passphrase'Range loop
         PS (J) := Ada.Streams.Stream_Element (Character'Pos (Passphrase (I)));
         J      := J + 1;
      end loop;

      for I in Salt'Range loop
         PS (J) := Salt (I);
         J      := J + 1;
      end loop;

      Digest := GNAT.SHA256.Digest (A => PS);
      for I in 2 .. 1000 loop
         Digest := GNAT.SHA256.Digest (A => Digest);
      end loop;

      J := 1;
      for I in Octets.Key_Type'Range loop
         Key (I) := Octets.Octet_Type (Digest (J));
         J       := J + 1;
      end loop;

      return Key;

   end Generate_Key;

   function Generate_NMK (Passphrase : String) return Octets.Key_Type is

      Salt : constant Ada.Streams.Stream_Element_Array (1 .. 8) := (16#08#, 16#85#, 16#6d#, 16#af#, 16#7c#, 16#f5#, 16#81#, 16#86#);

   begin

      return Generate_Key (Passphrase => Passphrase,
                           Salt       => Salt);

   end Generate_NMK;

   function Has_MAC_Address (Self        : Adapter_Type;
                             MAC_Address : MAC_Addresses.MAC_Address_Type) return Boolean is

      use type MAC_Addresses.MAC_Address_Type;

   begin

      return Self.MAC_Address = MAC_Address;

   end Has_MAC_Address;

   function Image (Self : Adapter_Type) return String is

   begin

      return Self.MAC_Address.Image & " via " & Self.Network_Interface'Image & " interface, HFID: " & HFID_Strings.To_String (Source => Self.HFID);

   end Image;

   procedure Initialize (Self              : out Adapter_Type;
                         Network_Interface :     Network_Interface_Type;
                         MAC_Address       :     MAC_Addresses.MAC_Address_Type;
                         HFID              :     HFID_Strings.Bounded_String) is

   begin

      Self.Network_Interface := Network_Interface;
      Self.MAC_Address       := MAC_Address;
      Self.HFID              := HFID;

   end Initialize;

   procedure Process (Self                :     Adapter_Type;
                      Request             :     Messages.Message_Type;
                      Confirmation        : out Packets.Payload_Type;
                      Confirmation_Length : out Natural;
                      From_MAC_Address    : out MAC_Addresses.MAC_Address_Type) is

   begin

      Power_Line_Adapters.Network.Send (Message     => Request,
                                        Destination => Self.MAC_Address);

      Power_Line_Adapters.Network.Receive (Confirmation        => Confirmation,
                                           Confirmation_Length => Confirmation_Length,
                                           From_MAC_Address    => From_MAC_Address);

      if Confirmation_Length = 0 then
         raise Adapter_Error with Message_No_Confirmation;
      end if;

   end Process;

   function To_HFID_String (HFID_Octets : Octets.Octets_Type) return HFID_Strings.Bounded_String is

      function To_String is new To_Bounded_String (T      => HFID_Strings.Bounded_String,
                                                   Append => HFID_Strings.Append);

   begin

      return To_String (Data => HFID_Octets);

   end To_HFID_String;

   procedure Validate_DAK_Passphrase (Passphrase       : String;
                                      Check_Min_Length : Boolean := True) is

      Max_Passphrase_Length : constant := 19;
      Min_Passphrase_Length : constant := 19;

   begin

      Validate_Passphrase (Passphrase            => Passphrase,
                           Min_Passphrase_Length => Min_Passphrase_Length,
                           Max_Passphrase_Length => Max_Passphrase_Length,
                           Check_Min_Length      => Check_Min_Length);

   end Validate_DAK_Passphrase;

   procedure Validate_HFID (HFID            : HFID_Strings.Bounded_String;
                            Min_HFID_Length : Positive := 1) is

      Length : constant HFID_Strings.Length_Range := HFID_Strings.Length (HFID);
      C      : Character;

   begin

      if Length < Min_HFID_Length then
         raise Adapter_Error with "HFID has fewer than" & Integer'Image (Min_HFID_Length) & " characters";
      end if;

      for I in 1 .. Length loop

         C := HFID_Strings.Element (Source => HFID,
                                    Index  => I);

         if C < ' ' or else C > Ada.Characters.Latin_1.DEL then
            raise Adapter_Error with "HFID contains one or more illegal characters";
         end if;

      end loop;

   end Validate_HFID;

   procedure Validate_NMK_Passphrase (Passphrase       : String;
                                      Check_Min_Length : Boolean := True) is

      Max_Passphrase_Length : constant := 64;
      Min_Passphrase_Length : constant := 24;

   begin

      Validate_Passphrase (Passphrase            => Passphrase,
                           Min_Passphrase_Length => Min_Passphrase_Length,
                           Max_Passphrase_Length => Max_Passphrase_Length,
                           Check_Min_Length      => Check_Min_Length);

   end Validate_NMK_Passphrase;

   procedure Validate_Passphrase (Passphrase            : String;
                                  Min_Passphrase_Length : Positive;
                                  Max_Passphrase_Length : Positive;
                                  Check_Min_Length      : Boolean := True) is

   begin

      if Passphrase'Length > Max_Passphrase_Length then
         raise Adapter_Error with "Passphrase has more than" & Integer'Image (Max_Passphrase_Length) & " characters";
      end if;

      if Check_Min_Length and then Passphrase'Length < Min_Passphrase_Length then
         raise Adapter_Error with "Passphrase has fewer than" & Integer'Image (Min_Passphrase_Length) & " characters";
      end if;

      for I in Passphrase'Range loop

         if Passphrase (I) < ' ' or else Passphrase (I) > Ada.Characters.Latin_1.DEL then
            raise Adapter_Error with "Passphrase contains one or more illegal characters";
         end if;

      end loop;

   end Validate_Passphrase;

   function Check_DAK (Self       : Adapter_Type;
                       Passphrase : String) return Boolean is separate;

   function Check_NMK (Self       : Adapter_Type;
                       Passphrase : String) return Boolean is separate;

   function Get_Any_Network_Info (Self : Adapter_Type) return Network_Info_List_Type is separate;

   function Get_Capabilities (Self : Adapter_Type) return Capabilities_Type is separate;

   function Get_Discover_List (Self : Adapter_Type) return Discover_List_Type is separate;

   function Get_HFID (Self : Adapter_Type;
                      Kind : HFID_Kind_Type) return HFID_Strings.Bounded_String is separate;

   function Get_Id_Info (Self : Adapter_Type) return Id_Info_Type is separate;

   function Get_Manufacturer_HFID (Self : Adapter_Type) return HFID_Strings.Bounded_String is separate;

   function Get_Member_Network_Info (Self : Adapter_Type) return Network_Info_List_Type is separate;

   function Get_Network_Info (Self  : Adapter_Type;
                              Scope : Network_Scope_Type) return Network_Info_List_Type is separate;

   function Get_Network_Stats (Self : Adapter_Type) return Network_Stats_List_Type is separate;

   function Get_Station_Info (Self : Adapter_Type) return Station_Info_Type is separate;

   function Get_User_HFID (Self : Adapter_Type) return HFID_Strings.Bounded_String is separate;

   procedure Reset (Self : Adapter_Type) is separate;

   procedure Restart (Self : Adapter_Type) is separate;

   procedure Set_HFID (Self : Adapter_Type;
                       HFID : HFID_Strings.Bounded_String) is separate;

   procedure Set_NMK (Self       : Adapter_Type;
                      Passphrase : String) is separate;

end Power_Line_Adapters;