keystoreada_1.1.0_d28d822c/src/keystore-repository.ads

  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
-----------------------------------------------------------------------
--  keystore-repository -- Repository management for the keystore
--  Copyright (C) 2019, 2020 Stephane Carrez
--  Written by Stephane Carrez (Stephane.Carrez@gmail.com)
--
--  Licensed under the Apache License, Version 2.0 (the "License");
--  you may not use this file except in compliance with the License.
--  You may obtain a copy of the License at
--
--      http://www.apache.org/licenses/LICENSE-2.0
--
--  Unless required by applicable law or agreed to in writing, software
--  distributed under the License is distributed on an "AS IS" BASIS,
--  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--  See the License for the specific language governing permissions and
--  limitations under the License.
-----------------------------------------------------------------------
with Ada.Calendar;
with Ada.Streams;
with Ada.Containers.Indefinite_Hashed_Maps;
with Ada.Containers.Doubly_Linked_Lists;
with Ada.Strings.Hash;

with Util.Encoders.AES;
with Keystore.IO;
with Keystore.Keys;
with Keystore.Passwords;
with Util.Streams;
with Interfaces;

with Keystore.Buffers;
private with Keystore.Random;
private with Ada.Finalization;
limited private with Keystore.Repository.Workers;
private package Keystore.Repository is

   type Wallet_Repository is tagged limited private;

   function Get_Identifier (Repository : in Wallet_Repository) return Wallet_Identifier;

   --  Open the wallet repository by reading the meta data block header from the wallet
   --  IO stream.  The wallet meta data is decrypted using AES-CTR using the given secret
   --  key and initial vector.
   procedure Open (Repository : in out Wallet_Repository;
                   Config     : in Keystore.Wallet_Config;
                   Ident      : in Wallet_Identifier;
                   Stream     : in IO.Wallet_Stream_Access);

   procedure Open (Repository   : in out Wallet_Repository;
                   Name         : in String;
                   Password     : in out Keystore.Passwords.Provider'Class;
                   Keys         : in out Keystore.Keys.Key_Manager;
                   Master_Block : in out Keystore.IO.Storage_Block;
                   Master_Ident : in out Wallet_Identifier;
                   Wallet       : in out Wallet_Repository);

   procedure Create (Repository : in out Wallet_Repository;
                     Password   : in out Keystore.Passwords.Provider'Class;
                     Config     : in Wallet_Config;
                     Block      : in IO.Storage_Block;
                     Ident      : in Wallet_Identifier;
                     Keys       : in out Keystore.Keys.Key_Manager;
                     Stream     : in IO.Wallet_Stream_Access);

   procedure Unlock (Repository : in out Wallet_Repository;
                     Password   : in out Keystore.Passwords.Provider'Class;
                     Block      : in Keystore.IO.Storage_Block;
                     Keys       : in out Keystore.Keys.Key_Manager);

   procedure Add (Repository : in out Wallet_Repository;
                  Name       : in String;
                  Kind       : in Entry_Type;
                  Content    : in Ada.Streams.Stream_Element_Array);

   procedure Add (Repository : in out Wallet_Repository;
                  Name       : in String;
                  Kind       : in Entry_Type;
                  Input      : in out Util.Streams.Input_Stream'Class);

   procedure Add_Wallet (Repository : in out Wallet_Repository;
                         Name       : in String;
                         Password   : in out Keystore.Passwords.Provider'Class;
                         Keys       : in out Keystore.Keys.Key_Manager;
                         Master_Block : in out Keystore.IO.Storage_Block;
                         Master_Ident : in out Wallet_Identifier;
                         Wallet     : in out Wallet_Repository);

   procedure Set (Repository : in out Wallet_Repository;
                  Name       : in String;
                  Kind       : in Entry_Type;
                  Content    : in Ada.Streams.Stream_Element_Array);

   procedure Set (Repository : in out Wallet_Repository;
                  Name       : in String;
                  Kind       : in Entry_Type;
                  Input      : in out Util.Streams.Input_Stream'Class);

   procedure Update (Repository : in out Wallet_Repository;
                     Name       : in String;
                     Kind       : in Entry_Type;
                     Content    : in Ada.Streams.Stream_Element_Array);

   procedure Update (Repository : in out Wallet_Repository;
                     Name       : in String;
                     Kind       : in Entry_Type;
                     Input      : in out Util.Streams.Input_Stream'Class);

   procedure Delete (Repository : in out Wallet_Repository;
                     Name       : in String);

   function Contains (Repository : in Wallet_Repository;
                      Name       : in String) return Boolean;

   procedure Find (Repository : in out Wallet_Repository;
                   Name       : in String;
                   Result     : out Entry_Info);

   procedure Get_Data (Repository : in out Wallet_Repository;
                       Name       : in String;
                       Result     : out Entry_Info;
                       Output     : out Ada.Streams.Stream_Element_Array);

   --  Write in the output stream the named entry value from the wallet.
   procedure Get_Data (Repository : in out Wallet_Repository;
                       Name       : in String;
                       Output     : in out Util.Streams.Output_Stream'Class);

   --  Get the list of entries contained in the wallet that correspond to the optional filter.
   procedure List (Repository : in out Wallet_Repository;
                   Filter     : in Filter_Type;
                   Content    : out Entry_Map);

   procedure List (Repository : in out Wallet_Repository;
                   Pattern    : in GNAT.Regpat.Pattern_Matcher;
                   Filter     : in Filter_Type;
                   Content    : out Entry_Map);

   --  Get the key slot number that was used to unlock the keystore.
   function Get_Key_Slot (Repository : in Wallet_Repository) return Key_Slot;

   --  Get stats information about the wallet (the number of entries, used key slots).
   procedure Fill_Stats (Repository : in Wallet_Repository;
                         Stats      : in out Wallet_Stats);

   procedure Set_Work_Manager (Repository : in out Wallet_Repository;
                               Workers    : in Keystore.Task_Manager_Access);

   procedure Close (Repository : in out Wallet_Repository);

private

   use Ada.Streams;

   function AES_Align (Size : in Stream_Element_Offset) return Stream_Element_Offset
     renames Util.Encoders.AES.Align;

   ET_WALLET_ENTRY      : constant := 16#0001#;
   ET_STRING_ENTRY      : constant := 16#0010#;
   ET_BINARY_ENTRY      : constant := 16#0200#;

   WALLET_ENTRY_SIZE    : constant := 4 + 2 + 8 + 32 + 16 + 4;

   DATA_NAME_ENTRY_SIZE : constant := 4 + 2 + 2 + 8 + 8 + 8;
   DATA_KEY_HEADER_SIZE : constant := 4 + 2 + 4;
   DATA_KEY_ENTRY_SIZE  : constant := 4 + 4 + 2 + 16 + 32;
   DATA_IV_OFFSET       : constant := 32;
   DATA_ENTRY_SIZE      : constant := 4 + 2 + 2 + 8 + 32;
   DATA_MAX_SIZE        : constant := IO.Block_Size - IO.BT_HMAC_HEADER_SIZE
     - IO.BT_TYPE_HEADER_SIZE - DATA_ENTRY_SIZE;
   DATA_MAX_KEY_COUNT   : constant := (DATA_MAX_SIZE - DATA_KEY_HEADER_SIZE) / DATA_KEY_ENTRY_SIZE;

   function Hash (Value : in Wallet_Entry_Index) return Ada.Containers.Hash_Type;

   type Wallet_Entry;
   type Wallet_Entry_Access is access all Wallet_Entry;

   type Wallet_Directory_Entry;
   type Wallet_Directory_Entry_Access is access Wallet_Directory_Entry;

   type Wallet_Data_Key_Entry is record
      Directory : Wallet_Directory_Entry_Access;
      Size      : Stream_Element_Offset;
   end record;

   package Wallet_Data_Key_List is
     new Ada.Containers.Doubly_Linked_Lists (Element_Type => Wallet_Data_Key_Entry,
                                             "="          => "=");

   type Wallet_Directory_Entry is record
      Block      : Keystore.IO.Storage_Block;
      Available  : IO.Buffer_Size := IO.Block_Index'Last - IO.BT_DATA_START - 4 - 2;
      Last_Pos   : IO.Block_Index := IO.BT_DATA_START + 4 + 2;
      Key_Pos    : IO.Block_Index := IO.Block_Index'Last;
      Next_Block : Interfaces.Unsigned_32 := 0;
      Count      : Natural := 0;
      Ready      : Boolean := False;
   end record;

   type Wallet_Entry (Length    : Natural;
                      Is_Wallet : Boolean) is
   limited record
      --  The block header that contains this entry.
      Header       : Wallet_Directory_Entry_Access;
      Id           : Wallet_Entry_Index;
      Kind         : Entry_Type := T_INVALID;
      Create_Date  : Ada.Calendar.Time;
      Update_Date  : Ada.Calendar.Time;
      Access_Date  : Ada.Calendar.Time;
      Entry_Offset : IO.Block_Index := IO.Block_Index'First;

      --  List of data key blocks.
      Data_Blocks  : Wallet_Data_Key_List.List;
      Block_Count  : Natural := 0;

      Name         : aliased String (1 .. Length);

      case Is_Wallet is
         when True =>
            Wallet_Id    : Wallet_Identifier;
            Master       : IO.Block_Number;

         when False =>
            Size         : Interfaces.Unsigned_64 := 0;

      end case;
   end record;

   package Wallet_Directory_List is
     new Ada.Containers.Doubly_Linked_Lists (Element_Type => Wallet_Directory_Entry_Access,
                                             "="          => "=");

   package Wallet_Maps is
     new Ada.Containers.Indefinite_Hashed_Maps (Key_Type        => String,
                                                Element_Type    => Wallet_Entry_Access,
                                                Hash            => Ada.Strings.Hash,
                                                Equivalent_Keys => "=",
                                                "="             => "=");

   package Wallet_Indexs is
     new Ada.Containers.Indefinite_Hashed_Maps (Key_Type        => Wallet_Entry_Index,
                                                Element_Type    => Wallet_Entry_Access,
                                                Hash            => Hash,
                                                Equivalent_Keys => "=",
                                                "="             => "=");

   type Wallet_Worker_Access is access all Keystore.Repository.Workers.Wallet_Worker;

   type Wallet_Repository is limited new Ada.Finalization.Limited_Controlled with record
      Parent         : access Wallet_Repository;
      Id             : Wallet_Identifier;
      Next_Id        : Wallet_Entry_Index;
      Next_Wallet_Id : Wallet_Identifier;
      Directory_List : Wallet_Directory_List.List;
      Root           : IO.Storage_Block;
      IV             : Util.Encoders.AES.Word_Block_Type;
      Config         : Keystore.Keys.Wallet_Config;
      Map            : Wallet_Maps.Map;
      Entry_Indexes  : Wallet_Indexs.Map;
      Random         : Keystore.Random.Generator;
      Current        : IO.Marshaller;
      Workers        : Wallet_Worker_Access;
      Cache          : Buffers.Buffer_Map;
      Modified       : Buffers.Buffer_Map;
      Stream         : Keystore.IO.Wallet_Stream_Access;
   end record;

   overriding
   procedure Finalize (Manager    : in out Wallet_Repository);

end Keystore.Repository;