gnoga_2.1.2_5f127c56/deps/simple_components/gnat-sockets-connection_state_machine-asn1.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
--                                                                    --
--  package                         Copyright (c)  Dmitry A. Kazakov  --
--     GNAT.Sockets.Connection_State_Machine.      Luebeck            --
--        ASN1                                     Spring, 2019       --
--  Implementation                                                    --
--                                Last revision :  10:13 29 Nov 2020  --
--                                                                    --
--  This  library  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  2  of  --
--  the License, or (at your option) any later version. This library  --
--  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 library; if not, write to  the  Free  Software  Foundation,  --
--  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.    --
--                                                                    --
--  As a special exception, if other files instantiate generics from  --
--  this unit, or you link this unit with other files to produce  an  --
--  executable, this unit does not by  itself  cause  the  resulting  --
--  executable to be covered by the GNU General Public License. This  --
--  exception  does not however invalidate any other reasons why the  --
--  executable file might be covered by the GNU Public License.       --
--____________________________________________________________________--

with Ada.Exceptions;         use Ada.Exceptions;
with Ada.IO_Exceptions;      use Ada.IO_Exceptions;
with Ada.Tags;               use Ada.Tags;
with Strings_Edit;           use Strings_Edit;
with Strings_Edit.Integers;  use Strings_Edit.Integers;

package body GNAT.Sockets.Connection_State_Machine.ASN1 is

   function "=" (Left, Right : Tag_Type) return Boolean is
   begin
      return Left.Class = Right.Class and then Left.Value = Right.Value;
   end "=";

   function "<" (Left, Right : Tag_Type) return Boolean is
   begin
      return
      (  Left.Class < Right.Class
      or else
         (  Left.Class = Right.Class
         and then
            Left.Value < Right.Value
      )  );
   end "<";

   function Always_Constructed
            (  Item : ASN1_Data_Item
            )  return Boolean is
   begin
      return False;
   end Always_Constructed;

   function Always_Constructed (Tag : ASN1_Type) return Boolean is
   begin
      case Tag is
         when Sequence_Tag | Set_Tag =>
            return True;
         when others =>
            return False;
      end case;
   end Always_Constructed;

   procedure Check
             (  Tag       : Stream_Element;
                Expected  : ASN1_Type_Array;
                Primitive : Boolean := False
             )  is
      This : constant ASN1_Type := ASN1_Type (Tag and 16#1F#);
   begin
      if Primitive and then 0 /= (ASN1_Tag (Tag) and Constructed) then
         Raise_Exception
         (  Data_Error'Identity,
            (  "Object of ASN.1 type "
            &  Image (This)
            &  " must be primitive"
         )  );
      elsif Universal_Class = (ASN1_Tag (Tag) and 2#1100_0000#) then
         for Index in Expected'Range loop
            if This = Expected (Index) then
               return;
            end if;
         end loop;
         Raise_Exception
         (  Data_Error'Identity,
            (  "ASN.1 type "
            &  Image (This)
            &  " does not match expected type "
            &  Image (Expected)
         )  );
      elsif Expected'Length = 1 then
         Raise_Exception
         (  Data_Error'Identity,
            "ASN.1 type " & Image (Expected) & " is expected"
         );
      else
         Raise_Exception
         (  Data_Error'Identity,
            "One of ASN.1 types " & Image (Expected) & " is expected"
         );
      end if;
   end Check;

   procedure Get
             (  Data        : Stream_Element_Array;
                Pointer     : in out Stream_Element_Offset;
                Tag         : out Tag_Type;
                Constructed : out Boolean
             )  is
   begin
      if (  Pointer < Data'First
         or else
            (  Pointer > Data'Last
            and then
               Pointer - Data'Last /= 1
         )  )
      then
         Raise_Exception (Layout_Error'Identity, Out_Of_Bounds);
      elsif Pointer <= Data'Last then
         case ASN1_Tag (Data (Pointer) and 2#1100_0000#) is
            when Application_Class =>
               Tag.Class := Application_Tag;
            when Context_Specific_Class =>
               Tag.Class := Context_Specific_Tag;
            when Private_Class =>
               Tag.Class := Private_Tag;
            when others =>
               Tag.Class := Universal_Tag;
         end case;
         Tag.Optional := False;
         Constructed :=
             0 /= (ASN1_Tag (Data (Pointer)) and ASN1.Constructed);
         Tag.Value := ASN1_Type (Data (Pointer) and 16#1F#);
         if Tag.Value < 31 then
            Pointer := Pointer + 1;
            return;
         end if;
         declare
            Index : Stream_Element_Offset := Pointer + 1;
         begin
            Tag.Value := 0;
            while Index <= Data'Last loop
               begin
                  Tag.Value :=
                     (  Tag.Value * 128
                     +  ASN1_Type (Data (Index) and 16#7F#)
                     );
               exception
                  when Constraint_Error =>
                     Raise_Exception
                     (  Constraint_Error'Identity,
                        Tag_Too_Large
                     );
               end;
               if 0 = (Data (Index) and 16#80#) then
                  Pointer := Index + 1;
                  return;
               end if;
               Index := Index + 1;
            end loop;
         end;
         Raise_Exception
         (  End_Error'Identity,
            "Non-terminated or missing ASN.1 tag"
         );
      end if;
   end Get;

   function Image (Tag : ASN1_Type) return String is
   begin
      case Tag is
         when Boolean_Tag           => return "BOOLEAN";
         when Integer_Tag           => return "INTEGER";
         when Bit_String_Tag        => return "BITSTRING";
         when Octet_String_Tag      => return "OCTET STRING";
         when Null_Tag              => return "NULL";
         when Object_Identifier_Tag => return "OBJECT IDENTIFIER";
         when Object_Descriptor_Tag => return "ObjectDescriptor";
         when Instance_Tag          => return "INSTANCE";
         when Real_Tag              => return "REAL";
         when Enumerated_Tag        => return "ENUMERATED";
         when Embedded_Tag          => return "EMBEDDED PDV";
         when UTF8_String_Tag       => return "UTF8String";
         when Relative_OID_Tag      => return "RELATIVE-OID";
         when Time_Tag              => return "TIME";
         when Sequence_Tag          => return "SEQUENCE";
         when Set_Tag               => return "SET";
         when Numeric_String_Tag    => return "NumericString";
         when Printable_String_Tag  => return "PrintableString";
         when Teletext_String_Tag   => return "TeletexString";
         when Videotext_String_Tag  => return "VideotexString";
         when IA5_String_Tag        => return "IA5String";
         when UTC_Time_Tag          => return "UTCTime";
         when Generalized_Time_Tag  => return "GeneralizedTime";
         when Graphic_String_Tag    => return "GraphicString";
         when ISO646_String_Tag     => return "ISO 646 string";
         when General_String_Tag    => return "GeneralString";
         when Universal_String_Tag  => return "UniversalString";
         when Character_String_Tag  => return "CHARACTER STRING";
         when BMP_String_Tag        => return "BMPString";
         when Date_Tag              => return "DATE";
         when Time_Of_Day_Tag       => return "TIME-OF-DAY";
         when Date_Time_Tag         => return "DATE-TIME";
         when Duration_Tag          => return "DURATION";
         when others                => return Image (Integer (Tag));
      end case;
   end Image;

   function Image (Tags : ASN1_Type_Array) return String is
      Buffer  : String (1..512);
      Pointer : Integer := 1;
   begin
      for Index in Tags'Range loop
         if Index > Tags'First then
            Put (Buffer, Pointer, ", ");
         end if;
         Put (Buffer, Pointer, Image (Tags (Index)));
      end loop;
      return Buffer (1..Pointer - 1);
   end Image;

   function Image (Value : Tag_Type) return String is
      Result  : String (1..60);
      Pointer : Integer := 1;
   begin
      case Value.Class is
         when Application_Tag =>
            Put (Result, Pointer, "[APPLICATION ");
            Put (Result, Pointer, Integer (Value.Value));
            Put (Result, Pointer, "]");
         when Context_Specific_Tag =>
            Put (Result, Pointer, "[");
            Put (Result, Pointer, Integer (Value.Value));
            Put (Result, Pointer, "]");
         when Private_Tag =>
            Put (Result, Pointer, "[PRIVATE ");
            Put (Result, Pointer, Integer (Value.Value));
            Put (Result, Pointer, "]");
         when Universal_Tag =>
            Put (Result, Pointer, "[UNIVERSAL ");
            Put (Result, Pointer, Integer (Value.Value));
            Put (Result, Pointer, "]");
      end case;
      return Result (1..Pointer - 1);
   end Image;

   procedure Put
             (  Data        : in out Stream_Element_Array;
                Pointer     : in out Stream_Element_Offset;
                Tag         : Tag_Type;
                Constructed : Boolean
             )  is
      Header : ASN1_Tag;
   begin
      if (  Pointer < Data'First
         or else
            (  Pointer > Data'Last
            and then
               Pointer - Data'Last /= 1
         )  )  then
         Raise_Exception (Layout_Error'Identity, Out_Of_Bounds);
      end if;
      case Tag.Class is
         when Application_Tag =>
            Header := Application_Class;
         when Context_Specific_Tag =>
            Header := Context_Specific_Class;
         when Private_Tag =>
            Header := Private_Class;
         when Universal_Tag =>
            Header := Universal_Class;
      end case;
      if Constructed then
         Header := Header + ASN1.Constructed;
      end if;
      if Tag.Value < 31 then -- Single octet length
         if Pointer > Data'Last then
            Raise_Exception (End_Error'Identity, No_Room);
         end if;
         Data (Pointer) :=
            Stream_Element (Header) + Stream_Element (Tag.Value);
         Pointer := Pointer + 1;
      else
         declare
            Value  : ASN1_Type := Tag.Value;
            Length : Stream_Element_Count := 1;
         begin
            loop
               Value := Value / 128;
               exit when Value = 0;
               Length := Length + 1;
            end loop;
            if Data'Last - Pointer < Length then
               Raise_Exception (End_Error'Identity, No_Room);
            end if;
            Data (Pointer) := Stream_Element (Header) or 16#1F#;
            Value := Tag.Value;
            Data (Pointer + Length) := Stream_Element (Value mod 128);
            Value := Value / 128;
            for Octet in reverse Pointer + 1..Pointer + Length - 1 loop
               Data (Octet) := Stream_Element (Value mod 128) or 16#80#;
               Value := Value / 128;
            end loop;
            Pointer := Pointer + Length + 1;
         end;
      end if;
   end Put;

   procedure Set_Implicit_Tag
             (  Item   : in out Abstract_ASN1_Data_Item;
                Tag    : Tag_Type;
                Length : Stream_Element_Offset
             )  is
   begin
      if Length < 0 then
         Raise_Exception
         (  Data_Error'Identity,
            (  "A non-constructed object "
            &  Expanded_Name (Abstract_ASN1_Data_Item'Class (Item)'Tag)
            &  " has indefinite length"
         )  );
      elsif Length = 0 then
         Raise_Exception
         (  Data_Error'Identity,
            (  "ASN.1 object "
            &  Expanded_Name (Abstract_ASN1_Data_Item'Class (Item)'Tag)
            &  " has zero length"
         )  );
      end if;
   end Set_Implicit_Tag;

   procedure Set_Untagged (Item : in out Abstract_ASN1_Data_Item) is
   begin
      if Is_Implicit (Abstract_ASN1_Data_Item'Class (Item)) then
         Raise_Exception
         (  Data_Error'Identity,
            (  "ASN.1 indefinite object "
            &  Expanded_Name (Abstract_ASN1_Data_Item'Class (Item)'Tag)
            &  " has no tag"
         )  );
      end if;
   end Set_Untagged;

end GNAT.Sockets.Connection_State_Machine.ASN1;