axmpp_1.0.0_9133a13e/agnutls/gnutls.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
------------------------------------------------------------------------------
--                                                                          --
--                              AXMPP Project                               --
--                                                                          --
--                           XMPP Library for Ada                           --
--                                                                          --
------------------------------------------------------------------------------
--                                                                          --
-- Copyright © 2011, Alexander Basov <coopht@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 Alexander Basov, 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$ $Date$
------------------------------------------------------------------------------

package body GNUTLS is
   use type Ada.Streams.Stream_Element_Count;
   use type Interfaces.C.size_t;
   use type Interfaces.C.int;

   --
   function gnutls_anon_allocate_client_credentials
     (SC : access Anon_Client_Credentials) return Interfaces.C.int;
   pragma Import (C,
                  gnutls_anon_allocate_client_credentials,
                  "gnutls_anon_allocate_client_credentials");

   --
   function gnutls_global_init return Interfaces.C.int;
   pragma Import (C, gnutls_global_init, "gnutls_global_init");

   --
   function gnutls_init (S : access Session; CE : Connection_End)
      return Interfaces.C.int;
   pragma Import (C, gnutls_init, "gnutls_init");

   --
   function gnutls_priority_set_direct
     (S          : Session;
      Priorities : Interfaces.C.char_array;
      Error_Pos  : Interfaces.C.char_array)
      return Interfaces.C.int;
   pragma Import (C, gnutls_priority_set_direct, "gnutls_priority_set_direct");

   --
   function gnutls_set_default_priority (S : Session) return Interfaces.C.int;
   pragma Import
     (C, gnutls_set_default_priority, "gnutls_set_default_priority");

   --
   function gnutls_credentials_set_anon (S      : Session;
                                         C_Type : Credentials_Type;
                                         Cred   : Anon_Client_Credentials)
     return Interfaces.C.int;
   pragma Import (C,
                  gnutls_credentials_set_anon,
                  "gnutls_credentials_set");

   --
   function gnutls_credentials_set (S      : Session;
                                    C_Type : Credentials_Type;
                                    Cred   : Certificate_Client_Credentials)
      return Interfaces.C.int;
   pragma Import (C, gnutls_credentials_set, "gnutls_credentials_set");

   --
   function gnutls_certificate_allocate_credentials
     (SC : access Certificate_Client_Credentials)
      return Interfaces.C.int;
   pragma Import (C,
                  gnutls_certificate_allocate_credentials,
                  "gnutls_certificate_allocate_credentials");

   --
   procedure gnutls_transport_set_ptr (S   : Session;
                                       Ptr : Interfaces.C.int);
   pragma Import (C, gnutls_transport_set_ptr, "gnutls_transport_set_ptr");

   --
   function gnutls_handshake (S : Session) return Interfaces.C.int;
   pragma Import (C, gnutls_handshake, "gnutls_handshake");

   --
   function gnutls_record_send
     (S         : Session;
      Data      : GNAT.Sockets.Stream_Element_Reference;
      Data_Size : Interfaces.C.size_t)
      return Interfaces.C.size_t;
   pragma Import (C, gnutls_record_send, "gnutls_record_send");

   --
   function gnutls_record_recv
     (S         : Session;
      Data      : GNAT.Sockets.Stream_Element_Reference;
      Data_Size : Interfaces.C.size_t)
     return Interfaces.C.size_t;
   pragma Import (C, gnutls_record_recv, "gnutls_record_recv");

   --
   function gnutls_bye (S : Session; How : Close_Request)
      return Interfaces.C.int;
   pragma Import (C, gnutls_bye, "gnutls_bye");

   --
   procedure gnutls_deinit (S : Session);
   pragma Import (C, gnutls_deinit, "gnutls_deinit");

   --
   procedure gnutls_anon_free_client_credentials
     (SC : Anon_Client_Credentials);
   pragma Import (C,
                  gnutls_anon_free_client_credentials,
                  "gnutls_anon_free_client_credentials");

   --
   procedure gnutls_global_deinit;
   pragma Import (C, gnutls_global_deinit, "gnutls_global_deinit");

   --
   procedure gnutls_perror (Code : Interfaces.C.int);
   pragma Import (C, gnutls_perror, "gnutls_perror");

   --
   procedure gnutls_global_set_log_level (Level : Interfaces.C.int);
   pragma Import (C,
                  gnutls_global_set_log_level,
                  "gnutls_global_set_log_level");

   --
   function gnutls_record_get_direction (S : Session) return Interfaces.C.int;
   pragma Import
     (C, gnutls_record_get_direction, "gnutls_record_get_direction");

   --
   function gnutls_error_is_fatal (E : Interfaces.C.int)
      return Interfaces.C.int;
   pragma Import (C, gnutls_error_is_fatal, "gnutls_error_is_fatal");

   ----------------------------------------
   --  Anon_Allocate_Client_Credentials  --
   ----------------------------------------
   procedure Anon_Allocate_Client_Credentials
     (SC : out Anon_Client_Credentials)
   is
      Tmp : aliased Anon_Client_Credentials;
      Ret : constant Interfaces.C.int
        := gnutls_anon_allocate_client_credentials (Tmp'Access);

   begin
      if Ret /= 0 then
         gnutls_perror (Ret);
         raise GNUTLS_Error with "cannot do Anon_Allocate_Client_Credentials";
      end if;

      SC := Tmp;
   end Anon_Allocate_Client_Credentials;

   ------------------------------------
   --  Anon_Free_Client_Credentials  --
   ------------------------------------
   procedure Anon_Free_Client_Credentials (Cert : Anon_Client_Credentials)
   is
   begin
      gnutls_anon_free_client_credentials (Cert);
   end Anon_Free_Client_Credentials;

   -----------
   --  Bye  --
   -----------
   procedure Bye (S : Session; How : Close_Request)
   is
      Ret : constant Interfaces.C.int := gnutls_bye (S, How);

   begin
      if Ret /= 0 then
         gnutls_perror (Ret);
         raise GNUTLS_Error with "Bye failed";
      end if;
   end Bye;

   ----------------------------------------
   --  Certificate_Allocate_Credentials  --
   ----------------------------------------
   procedure Certificate_Allocate_Credentials
     (SC : out Certificate_Client_Credentials)
   is
      Tmp : aliased Certificate_Client_Credentials;
      Ret : constant Interfaces.C.int
        := gnutls_certificate_allocate_credentials (Tmp'Access);

   begin
      if Ret /= 0 then
         gnutls_perror (Ret);
         raise GNUTLS_Error with "Certificate_Allocate_Credentials failed";
      end if;

      SC := Tmp;
   end Certificate_Allocate_Credentials;

   -----------------------
   --  Credentials_Set  --
   -----------------------
   procedure Credentials_Set (S    : Session;
                              T    : Credentials_Type;
                              Cred : Anon_Client_Credentials)
   is
      Ret : constant Interfaces.C.int
        := gnutls_credentials_set_anon (S, T, Cred);

   begin
      if  Ret /= 0 then
         gnutls_perror (Ret);
         raise GNUTLS_Error with "Credentials_Set error";
      end if;
   end Credentials_Set;

   -----------------------
   --  Credentials_Set  --
   -----------------------
   procedure Credentials_Set (S    : Session;
                              T    : Credentials_Type;
                              Cred : Certificate_Client_Credentials)
   is
      Ret : constant Interfaces.C.int := gnutls_credentials_set (S, T, Cred);

   begin
      if  Ret /= 0 then
         gnutls_perror (Ret);
         raise GNUTLS_Error with "Credentials_Set error";
      end if;
   end Credentials_Set;

   --------------
   --  Deinit  --
   --------------
   procedure Deinit (S : Session)
   is
   begin
      gnutls_deinit (S);
   end Deinit;

   ----------------------
   --  Error_Is_Fatal  --
   ----------------------
   function Error_Is_Fatal (E : Integer) return Error_Kind is
   begin
      case gnutls_error_is_fatal (Interfaces.C.int (E)) is
         when 0 =>
            return NON_FATAL_ERROR;

         when 1 =>
            return FATAL_ERROR;

         when others =>
            return UNKNOWN_ERROR;
      end case;
   end Error_Is_Fatal;

   ---------------------
   --  Get_Direction  --
   ---------------------

   function Get_Direction (S : Session) return IO_Direction is
   begin
      if gnutls_record_get_direction (S) = 0 then
         return Read;

      else
         return Write;
      end if;
   end Get_Direction;

   -------------------
   --  Global_Init  --
   -------------------
   procedure Global_Deinit
   is
   begin
      gnutls_global_deinit;
   end Global_Deinit;

   -------------------
   --  Global_Init  --
   -------------------
   procedure Global_Init
   is
      Ret : constant Interfaces.C.int := gnutls_global_init;

   begin
      if Ret /= 0 then
         gnutls_perror (Ret);
         raise GNUTLS_Error
           with "Some error occuered during gnutls initialization";
      end if;
   end Global_Init;

   ---------------------
   --  Set_Log_Level  --
   ---------------------
   procedure Global_Set_Log_Level (Level : Integer) is
   begin
      gnutls_global_set_log_level (Interfaces.C.int (Level));
   end Global_Set_Log_Level;

   -----------------
   --  Handshake  --
   -----------------
   procedure Handshake (S : Session)
   is
      Ret : constant Interfaces.C.int := gnutls_handshake (S);

   begin
      if Ret /= 0 then
         gnutls_perror (Ret);
         raise GNUTLS_Error with "Handshake failed";
      end if;
   end Handshake;

   ------------
   --  Init  --
   ------------
   procedure Init (S : out Session; CE : Connection_End)
   is
      Tmp : aliased Session;
      Ret : constant Interfaces.C.int := gnutls_init (Tmp'Access, CE);

   begin
      if Ret /= 0 then
         gnutls_perror (Ret);
         raise GNUTLS_Error with "Cannot do Init";
      end if;
      S := Tmp;
   end Init;

   ---------------------------
   --  Priority_Set_Direct  --
   ---------------------------
   procedure Priority_Set_Direct (S : Session;
                                  Priorities : String;
                                  Error_Pos  : out String)
   is
      X : Interfaces.C.char_array (1 .. 256);
      pragma Warnings (Off, X);
      Ret : constant Interfaces.C.int
        := gnutls_priority_set_direct (S, Interfaces.C.To_C (Priorities), X);

   begin
      if Ret /= 0 then
         gnutls_perror (Ret);
         Error_Pos := Interfaces.C.To_Ada (X);
         raise GNUTLS_Error with "Priority_Set_Direct error";
      end if;
   end Priority_Set_Direct;

   -------------------
   --  Record_Recv  --
   -------------------
   procedure Record_Recv (S      : Session;
                          Data   : GNAT.Sockets.Vector_Type;
                          Length : out Ada.Streams.Stream_Element_Count)
   is
      N_Read : Interfaces.C.size_t;

   begin
      Length := 0;

      for I in Data'Range loop
         N_Read := gnutls_record_recv (S, Data (I).Base, Data (I).Length);

         if N_Read = 0 then
            gnutls_perror (Interfaces.C.int (N_Read));
            raise GNUTLS_Error with "Data read = 0";
         end if;

         Length := Length + Ada.Streams.Stream_Element_Count (N_Read);

         if N_Read < Data (I).Length then
            --  nothing to read
            return;
         end if;
      end loop;

   end Record_Recv;

   -------------------
   --  Record_Send  --
   -------------------
   procedure Record_Send (S      : Session;
                          Data   : GNAT.Sockets.Vector_Type;
                          Length : out Ada.Streams.Stream_Element_Count)
   is
      N_Write : Interfaces.C.size_t;

   begin
      Length := 0;

      for I in Data'Range loop
         N_Write := gnutls_record_send (S, Data (I).Base, Data (I).Length);

         if N_Write = 0 then
            raise GNUTLS_Error with "Data written = 0";
         end if;

         Length := Length + Ada.Streams.Stream_Element_Count (N_Write);

         if N_Write < Data (I).Length then
            --  nothing to write
            return;
         end if;
      end loop;

   end Record_Send;

   ----------------------------
   --  Set_Default_Priority  --
   ----------------------------

   procedure Set_Default_Priority (S : Session) is
      Ret : constant Interfaces.C.int := gnutls_set_default_priority (S);

   begin
      if Ret /= 0 then
         gnutls_perror (Ret);
         raise GNUTLS_Error with "Set_Default_Priority failed";
      end if;
   end Set_Default_Priority;

   -------------------------
   --  Transport_Set_Ptr  --
   -------------------------
   procedure Transport_Set_Ptr (S      : Session;
                                Socket : GNAT.Sockets.Socket_Type)
   is
      Sock : constant Interfaces.C.int
        := Interfaces.C.int (GNAT.Sockets.To_C (Socket));

   begin
      gnutls_transport_set_ptr (S, Sock);
   end Transport_Set_Ptr;

end GNUTLS;