matreshka_league_21.0.0_0c8f4d47/source/web/wsse/web_services-soap-security-password_digest_utilities.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
------------------------------------------------------------------------------
--                                                                          --
--                            Matreshka Project                             --
--                                                                          --
--                               Web Framework                              --
--                                                                          --
--                        Runtime Library Component                         --
--                                                                          --
------------------------------------------------------------------------------
--                                                                          --
-- Copyright © 2012, Vadim Godunko <vgodunko@gmail.com>                     --
-- All rights reserved.                                                     --
--                                                                          --
-- Redistribution and use in source and binary forms, with or without       --
-- modification, are permitted provided that the following conditions       --
-- are met:                                                                 --
--                                                                          --
--  * Redistributions of source code must retain the above copyright        --
--    notice, this list of conditions and the following disclaimer.         --
--                                                                          --
--  * Redistributions in binary form must reproduce the above copyright     --
--    notice, this list of conditions and the following disclaimer in the   --
--    documentation and/or other materials provided with the distribution.  --
--                                                                          --
--  * Neither the name of the Vadim Godunko, IE nor the names of its        --
--    contributors may be used to endorse or promote products derived from  --
--    this software without specific prior written permission.              --
--                                                                          --
-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS      --
-- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT        --
-- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR    --
-- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT     --
-- HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,   --
-- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED --
-- TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR   --
-- PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF   --
-- LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     --
-- NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS       --
-- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.             --
--                                                                          --
------------------------------------------------------------------------------
--  $Revision: 3493 $ $Date: 2012-11-26 13:36:47 +0400 (Пн, 26 ноя 2012) $
------------------------------------------------------------------------------
with Ada.Numerics.Discrete_Random;
with Ada.Streams;
with GNAT.SHA1;
with Interfaces;

with League.Calendars.ISO_8601;
with League.Stream_Element_Vectors.Internals;
with League.Text_Codecs;
with Matreshka.Internals.Stream_Element_Vectors;

package body Web_Services.SOAP.Security.Password_Digest_Utilities is

   package Unsigned_64_Random is
     new Ada.Numerics.Discrete_Random (Interfaces.Unsigned_64);

   Format : constant League.Strings.Universal_String :=
     League.Strings.To_Universal_String ("yyyy-MM-ddTHH:mm:ss");
   --  Format of Created field in header, except 'Z' at the end, due to 'Z' is
   --  pattern symbol

   UTF8_Codec : constant League.Text_Codecs.Text_Codec
     := League.Text_Codecs.Codec
         (League.Strings.To_Universal_String ("utf-8"));
   --  Text codec to convert strings into UTF8 representation.

   Generator : Unsigned_64_Random.Generator;

   function To_Binary_Message_Digest
    (Item : String) return Ada.Streams.Stream_Element_Array;
   --  Converts string into array of stream elements. Note: this subprogram is
   --  needed for GNAT GPL 2012 only, because it doesn't provide
   --  GNAT.SHA1.Digest subprogram which returns Binary_Message_Digest.

   --------------------
   -- Compute_Digest --
   --------------------

   function Compute_Digest
    (Password : League.Strings.Universal_String;
     Nonce    : League.Stream_Element_Vectors.Stream_Element_Vector;
     Created  : League.Strings.Universal_String)
       return League.Stream_Element_Vectors.Stream_Element_Vector
   is
      Data : League.Stream_Element_Vectors.Stream_Element_Vector;

   begin
      Data.Append (Nonce);
      Data.Append (UTF8_Codec.Encode (Created));
      Data.Append (UTF8_Codec.Encode (Password));

      return
        League.Stream_Element_Vectors.To_Stream_Element_Vector
         (To_Binary_Message_Digest
           (GNAT.SHA1.Digest (Data.To_Stream_Element_Array)));
   end Compute_Digest;

   ----------------------
   -- Generate_Created --
   ----------------------

   function Generate_Created return League.Strings.Universal_String is
   begin
      return Result : League.Strings.Universal_String do
         Result :=
           League.Calendars.ISO_8601.Image
            (Format, League.Calendars.Clock);
         Result.Append ('Z');
      end return;
   end Generate_Created;

   --------------------
   -- Generate_Nonce --
   --------------------

   function Generate_Nonce
     return League.Stream_Element_Vectors.Stream_Element_Vector
   is
      Result : constant
        Matreshka.Internals.Stream_Element_Vectors.
          Shared_Stream_Element_Vector_Access
            := Matreshka.Internals.Stream_Element_Vectors.Allocate (16);
      Value  : array (Natural range 0 .. 1) of Interfaces.Unsigned_64;
      for Value'Address use Result.Value'Address;
      pragma Import (Ada, Value);

   begin
      Value (0) := Unsigned_64_Random.Random (Generator);
      Value (1) := Unsigned_64_Random.Random (Generator);
      Result.Length := 16;

      return League.Stream_Element_Vectors.Internals.Wrap (Result);
   end Generate_Nonce;

   ----------------
   -- Initialize --
   ----------------

   procedure Initialize is
   begin
      Unsigned_64_Random.Reset (Generator);
   end Initialize;

   ------------------------------
   -- To_Binary_Message_Digest --
   ------------------------------

   function To_Binary_Message_Digest
    (Item : String) return Ada.Streams.Stream_Element_Array
   is
      use type Ada.Streams.Stream_Element;
      use type Ada.Streams.Stream_Element_Offset;

      function Decode (Item : Character) return Ada.Streams.Stream_Element;

      ------------
      -- Decode --
      ------------

      function Decode (Item : Character) return Ada.Streams.Stream_Element is
      begin
         case Item is
            when '0' .. '9' =>
               return Character'Pos (Item) - Character'Pos ('0');

            when 'a' .. 'f' =>
               return Character'Pos (Item) - Character'Pos ('a') + 10;

            when 'A' .. 'F' =>
               return Character'Pos (Item) - Character'Pos ('A') + 10;

            when others =>
               raise Constraint_Error;
         end case;
      end Decode;

      Result : Ada.Streams.Stream_Element_Array (1 .. Item'Length / 2);
      First  : Natural := Item'First;
      Unused : Ada.Streams.Stream_Element_Offset := Result'First;

   begin
      while Item'Last - First > 0 loop
         Result (Unused) :=
          (Decode (Item (First)) * 16) or Decode (Item (First + 1));
         Unused := Unused + 1;
         First := First + 2;
      end loop;

      return Result;
   end To_Binary_Message_Digest;

end Web_Services.SOAP.Security.Password_Digest_Utilities;