-
Notifications
You must be signed in to change notification settings - Fork 37
/
aws-net-ssl__openssl.adb
2885 lines (2316 loc) · 86.4 KB
/
aws-net-ssl__openssl.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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
------------------------------------------------------------------------------
-- Ada Web Server --
-- --
-- Copyright (C) 2000-2022, AdaCore --
-- --
-- This library is free software; you can redistribute it and/or modify --
-- it under terms of the GNU General Public License as published by the --
-- Free Software Foundation; either version 3, 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. --
-- --
-- As a special exception under Section 7 of GPL version 3, you are --
-- granted additional permissions described in the GCC Runtime Library --
-- Exception, version 3.1, as published by the Free Software Foundation. --
-- --
-- You should have received a copy of the GNU General Public License and --
-- a copy of the GCC Runtime Library Exception along with this program; --
-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
-- <http://www.gnu.org/licenses/>. --
-- --
-- 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. --
------------------------------------------------------------------------------
pragma Ada_2012;
-- Routines here are wrappers around standard sockets and SSL.
--
-- IMPORTANT: The default certificate used for the SSL connection is
-- "cert.pem" (in the working directory) if it exists. If this file does
-- not exists it is required to initialize the SSL layer certificate with
-- AWS.Server.Set_Security.
with Ada.Command_Line;
with Ada.Containers.Indefinite_Hashed_Maps;
with Ada.Containers.Indefinite_Holders;
with Ada.Directories;
with Ada.Strings.Equal_Case_Insensitive;
with Ada.Strings.Hash_Case_Insensitive;
with Ada.Strings.Unbounded;
with Ada.Task_Attributes;
with Ada.Task_Identification;
with Ada.Task_Termination;
with Ada.Unchecked_Conversion;
with Ada.Unchecked_Deallocation;
with Interfaces.C.Strings;
with System.Memory;
with System.Storage_Elements;
with AWS.Config;
with AWS.Net.Log;
with AWS.Net.SSL.Certificate.Impl;
with AWS.Net.SSL.RSA_DH_Generators;
with AWS.OS_Lib;
with AWS.Translator;
with AWS.Utils;
with GNAT.String_Split;
package body AWS.Net.SSL is
use Ada.Strings;
use Interfaces;
use type C.int;
use type C.long;
use type TSSL.Pointer;
use type TSSL.SSL_CTX;
use type TSSL.SSL_Handle;
subtype NSST is Net.Std.Socket_Type;
package Locking is
procedure Initialize;
-- Initialize OpenSSL version less 1.1 locking callbacks, this makes
-- OpenSSL implementation thread safe.
end Locking;
function Lib_Realloc
(Ptr : System.Address;
Size : System.Memory.size_t) return System.Address with Convention => C;
-- C library could use null pointer as input parameter for realloc, but
-- gnatmem does not care about it and logging Free of the null pointer.
function BIO_Debug_Write
(BIO : TSSL.BIO_Access;
Buf : C.Strings.chars_ptr;
Size : C.int) return C.int with Convention => C;
Debug_BIO_Method : TSSL.BIO_Method_Access;
Debug_BIO_Output : TSSL.BIO_Access;
package Host_Certificates is new Ada.Containers.Indefinite_Hashed_Maps
(String, TSSL.SSL_CTX, Hash => Hash_Case_Insensitive,
Equivalent_Keys => Equal_Case_Insensitive);
package Char_Array_Holder is new Ada.Containers.Indefinite_Holders
(C.char_array, "=" => C."=");
procedure Set_Session_Cache_Size (Context : TSSL.SSL_CTX; Size : Integer);
-- Check the session cache size for specified context. Check error codes
-- and raise exception on errors.
function To_Char_Array (Protocols : SV.Vector) return C.char_array;
-- Converts Protocols to OpenSSL library protocol-list format
function To_Char_Array (Protocol : String) return C.char_array is
(C."&" (C.char'Val (Protocol'Length), C.To_C (Protocol, False)));
-- Converts Protocol to OpenSSL library protocol name format
Empty_Char_Array : constant C.char_array := (1 .. 0 => C.nul);
function ALPN_Callback
(SSL : TSSL.SSL_Handle;
Out_Pr : access C.Strings.chars_ptr;
Outlen : access C.unsigned_char;
In_Pr : C.Strings.chars_ptr;
Inlen : C.unsigned;
Arg : TSSL.Pointer) return C.int with Convention => C;
protected type TS_SSL is
procedure Set_IO (Socket : in out Socket_Type);
-- Bind the SSL handle with the BIO pair
procedure Initialize
(Certificate_Filename : String;
Security_Mode : Method;
Priorities : String;
Ticket_Support : Boolean;
Key_Filename : String;
Exchange_Certificate : Boolean;
Certificate_Required : Boolean;
Trusted_CA_Filename : String;
CRL_Filename : String;
Session_Cache_Size : Natural;
ALPN : C.char_array);
procedure Prepare
(Security_Mode : Method;
Priorities : String;
Ticket_Support : Boolean;
Exchange_Certificate : Boolean;
Certificate_Required : Boolean;
Trusted_CA_Filename : String;
CRL_Filename : String;
Session_Cache_Size : Natural);
procedure Initialize_Host_Certificate
(Host : String;
Certificate_Filename : String;
Key_Filename : String);
procedure ALPN_Set (Protos : C.char_array);
procedure ALPN_Include (Protocol : String);
procedure Finalize;
procedure Clear_Session_Cache;
procedure Set_Session_Cache_Size (Size : Natural);
function Session_Cache_Number return Natural;
procedure Set_Verify_Callback (Callback : System.Address);
procedure Check_CRL;
-- Check Certificate Revocation List, if this file has changed reload it
function Get_Context return TSSL.SSL_CTX;
private
Default_Context : TSSL.SSL_CTX := TSSL.Null_CTX;
CRL_Filename : C.Strings.chars_ptr := C.Strings.Null_Ptr;
CRL_Time_Stamp : Calendar.Time := Utils.AWS_Epoch;
Hosts : Host_Certificates.Map;
Security_Mode : Method;
Ticket_Support : Boolean;
Exchange_Certificate : Boolean;
Certificate_Required : Boolean;
Session_Cache_Size : Natural;
Priorities : C.Strings.chars_ptr;
Prio_TLS13 : C.Strings.chars_ptr;
ALPN : Char_Array_Holder.Holder;
Trusted_CA_Filename : C.Strings.chars_ptr;
Trusted_CA_Stack : TSSL.STACK_OF_X509_NAME :=
TSSL.Null_STACK_OF_X509_NAME;
end TS_SSL;
function Server_Name_Callback
(Session : SSL_Handle;
ad : access C.int; -- documentation not found
Hosts : access Host_Certificates.Map) return C.int
with Convention => C;
-- Callback to get server name on server side as client sent
type Memory_Access is access all
Stream_Element_Array (1 .. Stream_Element_Offset'Last);
Default_Config : constant Config := new TS_SSL;
Data_Index : C.int;
-- Application specific data's index
Max_Overhead : Stream_Element_Count range 0 .. 2**15 := 180 with Atomic;
for Max_Overhead'Size use 16;
-- Need size limitation because Stream_Element_Count is 64 bit and could
-- not guarantee Atomic on 32 bit platform.
Debug_Level : Natural := 0 with Atomic;
DH_Params : array (0 .. 1) of aliased TSSL.DH :=
(others => TSSL.Null_Pointer) with Atomic_Components;
RSA_Params : array (0 .. 1) of aliased TSSL.RSA :=
(others => TSSL.Null_Pointer) with Atomic_Components;
-- 0 element for current use, 1 element for remain usage after creation new
-- 0 element.
DH_Length : constant C.int := 2048;
RSA_Length : constant C.int := 2048;
function DH_Generate_cb
(a, b : C.int; cb : access TSSL.BN_GENCB) return C.int
with Convention => C;
DH_Generate_Callback : aliased TSSL.BN_GENCB :=
(cb => DH_Generate_cb'Access, others => <>);
procedure Socket_Read (Socket : Socket_Type);
-- Read encripted data from socket if necessary
procedure Socket_Write (Socket : Socket_Type; Gone : C.int := 0);
-- Write encripted data to socket if availabe
procedure Error_If (Error : Boolean) with Inline;
-- Raises Socket_Error if Error is true. Attach the SSL error message
procedure Error_If (Socket : Socket_Type; Error : Boolean) with Inline;
-- Raises and log Socket_Error if Error is true.
-- Attach the SSL error message.
procedure File_Error (Prefix, Name : String) with No_Return;
-- Prefix is the type of file key or certificate that was expected
procedure Do_Handshake (Socket : in out Socket_Type; Success : out Boolean);
-- Perform SSL handshake
function Error_Stack return String renames Certificate.Impl.Error_Stack;
-- Returns error stack of the last SSL error in multiple lines
function Error_Str (Code : TSSL.Error_Code) return String
renames Certificate.Impl.Error_Str;
-- Returns the SSL error message for error Code
procedure Init_Random;
-- Initialize the SSL library with a random number
procedure Initialize_Default_Config;
-- Initializes default config. It could be called more then once, because
-- secondary initialization is ignored.
function Verify_Callback
(preverify_ok : C.int; ctx : TSSL.X509_STORE_CTX) return C.int
with Convention => C;
-- This routine is needed to be able to retreive the client's certificate
-- and validate it thought the user's verification routine if provided.
function Tmp_RSA_Callback
(SSL : SSL_Handle; Is_Export : C.int; Keylength : C.int) return TSSL.RSA
with Convention => C;
function Tmp_DH_Callback
(SSL : SSL_Handle; Is_Export : C.int; Keylength : C.int) return TSSL.DH
with Convention => C;
procedure Secure
(Source : Net.Socket_Type'Class;
Target : out Socket_Type;
Config : SSL.Config);
-- Common code for Secure_Server and Secure_Client routines
procedure Set_Accept_State (Socket : Socket_Type);
-- Server session initialization
procedure Set_Connect_State (Socket : Socket_Type; Host : String);
-- Client session initialization
-------------------------
-- Abort_DH_Generation --
-------------------------
procedure Abort_DH_Generation is
begin
Abort_DH_Flag := True;
end Abort_DH_Generation;
-------------------
-- Accept_Socket --
-------------------
overriding procedure Accept_Socket
(Socket : Net.Socket_Type'Class; New_Socket : in out Socket_Type)
is
Success : Boolean;
begin
if New_Socket.Config = null then
Initialize_Default_Config;
New_Socket.Config := Default_Config;
end if;
SSL_Accept : loop
Net.Std.Accept_Socket (Socket, NSST (New_Socket));
New_Socket.Config.Set_IO (New_Socket);
Set_Accept_State (New_Socket);
Do_Handshake (New_Socket, Success);
exit SSL_Accept when Success;
Shutdown (New_Socket);
end loop SSL_Accept;
end Accept_Socket;
--------------------------
-- Add_Host_Certificate --
--------------------------
procedure Add_Host_Certificate
(Config : SSL.Config;
Host : String;
Certificate_Filename : String;
Key_Filename : String := "") is
begin
Config.Initialize_Host_Certificate
(Host, Certificate_Filename, Key_Filename);
end Add_Host_Certificate;
-------------------
-- ALPN_Callback --
-------------------
function ALPN_Callback
(SSL : TSSL.SSL_Handle;
Out_Pr : access C.Strings.chars_ptr;
Outlen : access C.unsigned_char;
In_Pr : C.Strings.chars_ptr;
Inlen : C.unsigned;
Arg : TSSL.Pointer) return C.int
is
pragma Unreferenced (SSL);
ALPN : Char_Array_Holder.Holder with Import;
for ALPN'Address use Arg;
Server : constant C.Strings.char_array_access :=
ALPN.Reference.Element;
begin
if TSSL.SSL_select_next_proto
(Out_Pr, Outlen, C.Strings.To_Chars_Ptr (Server), Server'Length, In_Pr,
Inlen) /= TSSL.OPENSSL_NPN_NEGOTIATED
then
Outlen.all := 0;
end if;
return TSSL.SSL_TLSEXT_ERR_OK;
end ALPN_Callback;
--------------
-- ALPN_Set --
--------------
function ALPN_Get (Socket : Socket_Type) return String is
use type C.unsigned, C.Strings.chars_ptr;
Data : aliased C.Strings.chars_ptr;
Size : aliased C.unsigned;
begin
TSSL.SSL_get0_alpn_selected (Socket.SSL, Data'Access, Size'Access);
if Data = C.Strings.Null_Ptr or else Size = 0 then
return "";
end if;
return C.Strings.Value (Data, C.size_t (Size));
end ALPN_Get;
------------------
-- ALPN_Include --
------------------
procedure ALPN_Include (Config : SSL.Config; Protocol : String) is
begin
Config.ALPN_Include (Protocol);
end ALPN_Include;
--------------
-- ALPN_Set --
--------------
procedure ALPN_Set (Config : SSL.Config; Protocols : SV.Vector) is
begin
Config.ALPN_Set (To_Char_Array (Protocols));
end ALPN_Set;
---------------------
-- BIO_Debug_Write --
---------------------
function BIO_Debug_Write
(BIO : TSSL.BIO_Access;
Buf : C.Strings.chars_ptr;
Size : C.int) return C.int
is
pragma Unreferenced (BIO);
begin
Debug_Output (C.Strings.Value (Buf, C.size_t (Size)));
return Size;
end BIO_Debug_Write;
------------------------
-- Cipher_Description --
------------------------
overriding function Cipher_Description
(Socket : Socket_Type) return String
is
Buffer : aliased C.char_array := (1 .. 256 => <>);
Result : constant String :=
C.Strings.Value
(TSSL.SSL_CIPHER_description
(TSSL.SSL_get_current_cipher (Socket.SSL).all,
Buffer'Unchecked_Access, Buffer'Length));
begin
if Result'Length > 0 and then Result (Result'Last) = ASCII.LF then
return Result (Result'First .. Result'Last - 1);
end if;
return Result;
end Cipher_Description;
-------------
-- Ciphers --
-------------
procedure Ciphers (Cipher : not null access procedure (Name : String)) is
use type C.Strings.chars_ptr;
Name : C.Strings.chars_ptr;
Ctx : constant TSSL.SSL_CTX := TSSL.SSL_CTX_new (TSSL.TLS_method);
SSL : SSL_Handle;
begin
Error_If (Ctx = TSSL.Null_CTX);
SSL := TSSL.SSL_new (Ctx);
Error_If (SSL = TSSL.Null_Handle);
for J in 0 .. C.int'Last loop
Name := TSSL.SSL_get_cipher_list (SSL, J);
exit when Name = C.Strings.Null_Ptr;
Cipher (C.Strings.Value (Name));
end loop;
TSSL.SSL_free (SSL);
TSSL.SSL_CTX_free (Ctx);
end Ciphers;
-------------------------
-- Clear_Session_Cache --
-------------------------
procedure Clear_Session_Cache (Config : SSL.Config := Null_Config) is
begin
if Config = Null_Config then
Default_Config.Clear_Session_Cache;
else
Config.Clear_Session_Cache;
end if;
end Clear_Session_Cache;
-------------
-- Connect --
-------------
overriding procedure Connect
(Socket : in out Socket_Type;
Host : String;
Port : Positive;
Wait : Boolean := True;
Family : Family_Type := Family_Unspec)
is
Success : Boolean;
begin
Net.Std.Connect (NSST (Socket), Host, Port, Wait, Family);
if Socket.Config = null then
Initialize_Default_Config;
Socket.Config := Default_Config;
end if;
Socket.Config.Set_IO (Socket);
if Socket.Sessn /= null then
Set_Session_Data (Socket, Socket.Sessn);
Socket.Sessn := null;
end if;
Set_Connect_State (Socket, Host);
if Wait then
-- Do handshake only in case of wait connection completion
Do_Handshake (Socket, Success);
if not Success then
declare
Error_Text : constant String := Error_Stack;
begin
Net.Log.Error (Socket, Error_Text);
Net.Std.Shutdown (NSST (Socket));
raise Socket_Error with Error_Text;
end;
end if;
end if;
end Connect;
--------------------
-- DH_Generate_cb --
--------------------
function DH_Generate_cb
(a, b : C.int; cb : access TSSL.BN_GENCB) return C.int
is
pragma Unreferenced (a, b, cb);
begin
return Boolean'Pos (not Abort_DH_Flag);
end DH_Generate_cb;
------------------
-- Do_Handshake --
------------------
procedure Do_Handshake
(Socket : in out Socket_Type; Success : out Boolean)
is
use TSSL;
Res : C.int;
begin
loop
Res := SSL_do_handshake (Socket.SSL);
Success := Res = 1;
exit when Success;
case SSL_get_error (Socket.SSL, Res) is
when SSL_ERROR_WANT_READ => Socket_Read (Socket);
when SSL_ERROR_WANT_WRITE => Socket_Write (Socket);
when others => exit;
end case;
end loop;
Socket_Write (Socket);
end Do_Handshake;
procedure Do_Handshake (Socket : in out Socket_Type) is
Success : Boolean;
begin
Do_Handshake (Socket, Success);
if not Success then
Raise_Socket_Error (Socket, Error_Stack);
end if;
end Do_Handshake;
--------------
-- Error_If --
--------------
procedure Error_If (Error : Boolean) is
begin
if Error then
Raise_Socket_Error
(Socket_Type'(Std.Socket_Type with others => <>), Error_Stack);
end if;
end Error_If;
procedure Error_If (Socket : Socket_Type; Error : Boolean) is
begin
if Error then
Raise_Socket_Error (Socket, Error_Stack);
end if;
end Error_If;
----------------
-- File_Error --
----------------
procedure File_Error (Prefix, Name : String) is
begin
raise Socket_Error with
Prefix & " file """ & Name & """ error." & ASCII.LF & Error_Stack;
end File_Error;
----------
-- Free --
----------
overriding procedure Free (Socket : in out Socket_Type) is
begin
if Socket.SSL /= TSSL.Null_Handle then
TSSL.SSL_free (Socket.SSL);
TSSL.BIO_free (Socket.IO);
Socket.SSL := TSSL.Null_Handle;
end if;
NSST (Socket).Free;
end Free;
----------
-- Free --
----------
procedure Free (Session : in out Session_Type) is
begin
if Session /= null then
TSSL.SSL_SESSION_free (TSSL.SSL_Session (Session));
Session := null;
end if;
end Free;
procedure Free (Key : in out Private_Key) is
begin
if Key /= Null_Private_Key then
EVP_PKEY_free (Key);
Key := Null_Private_Key;
end if;
end Free;
-----------------
-- Generate_DH --
-----------------
procedure Generate_DH is
use type TSSL.BIO_Access;
OK : Boolean;
DH : aliased TSSL.DH;
Bits : constant C.int := DH_Length;
function Loaded return Boolean;
procedure Save;
------------
-- Loaded --
------------
function Loaded return Boolean is
Filename : constant String :=
RSA_DH_Generators.Parameters_Filename
("dh-" & Utils.Image (Integer (Bits)), Exist => True);
C_Name : aliased C.char_array := C.To_C (Filename);
IO : TSSL.BIO_Access;
begin
if Filename = "" then
return False;
end if;
IO := TSSL.BIO_new (TSSL.BIO_s_file);
Error_If (IO = null);
Error_If
(TSSL.BIO_read_filename
(IO, C.Strings.To_Chars_Ptr (C_Name'Unchecked_Access)) = 0);
Error_If
(TSSL.PEM_read_bio_DHparams (IO, DH'Access, null, TSSL.Null_Pointer)
/= DH);
DH_Time (DH_Time_Idx + 1) :=
Ada.Directories.Modification_Time (Filename);
DH_Time_Idx := DH_Time_Idx + 1;
TSSL.BIO_free (IO);
return True;
end Loaded;
----------
-- Save --
----------
procedure Save is
Filename : constant String :=
RSA_DH_Generators.Parameters_Filename
("dh-" & Utils.Image (Integer (Bits)), Exist => False);
C_Name : aliased C.char_array := C.To_C (Filename);
BIO : TSSL.BIO_Access;
begin
if Filename = "" then
return;
end if;
BIO := TSSL.BIO_new (TSSL.BIO_s_file);
Error_If (BIO = null);
Error_If
(TSSL.BIO_write_filename
(BIO, C.Strings.To_Chars_Ptr (C_Name'Unchecked_Access)) = 0);
Error_If (TSSL.PEM_write_bio_DHparams (BIO, DH) = 0);
TSSL.BIO_free (BIO);
end Save;
begin
DH_Lock.Try_Lock (OK);
if not OK then
return;
end if;
DH := TSSL.DH_new;
Error_If (DH = TSSL.Null_Pointer);
if DH_Params (0) /= TSSL.Null_Pointer or else not Loaded then
if TSSL.DH_generate_parameters_ex
(params => DH,
prime_len => DH_Length,
generator => (if DH_Time_Idx = 0 then 2 else 5),
cb => DH_Generate_Callback'Access) = 0
then
Error_If (not Abort_DH_Flag);
else
DH_Time (DH_Time_Idx + 1) := Ada.Calendar.Clock;
DH_Time_Idx := DH_Time_Idx + 1;
Save;
end if;
end if;
if DH_Params (1) /= TSSL.Null_Pointer then
TSSL.DH_free (DH_Params (1));
end if;
DH_Params (1) := DH_Params (0);
DH_Params (0) := DH;
DH_Lock.Unlock;
end Generate_DH;
------------------
-- Generate_RSA --
------------------
procedure Generate_RSA is
OK : Boolean;
BN : TSSL.BIGNUM;
RSA : TSSL.RSA;
begin
RSA_Lock.Try_Lock (OK);
if not OK then
return;
end if;
BN := TSSL.BN_new;
Error_If (TSSL."=" (BN, TSSL.BIGNUM (TSSL.Null_Pointer)));
Error_If (TSSL.BN_set_word (BN, TSSL.RSA_F4) = 0);
RSA := TSSL.RSA_new;
Error_If (RSA = TSSL.Null_Pointer);
Error_If
(TSSL.RSA_generate_key_ex
(RSA, RSA_Length, BN, TSSL.Null_Pointer) = 0);
TSSL.BN_free (BN);
if RSA_Params (1) /= TSSL.Null_Pointer then
TSSL.RSA_free (RSA_Params (1));
end if;
RSA_Params (1) := RSA_Params (0);
RSA_Params (0) := RSA;
RSA_Time (RSA_Time_Idx + 1) := Ada.Calendar.Clock;
RSA_Time_Idx := RSA_Time_Idx + 1;
RSA_Lock.Unlock;
end Generate_RSA;
-----------------
-- Init_Random --
-----------------
procedure Init_Random is
use Ada.Calendar;
use System.Storage_Elements;
Buf : String :=
Duration'Image
(Clock - Time_Of (Year => Year_Number'First,
Month => Month_Number'First,
Day => Day_Number'First))
& Integer_Address'Image (To_Integer (Init_Random'Address));
begin
TSSL.RAND_seed (Buf'Address, Buf'Length);
end Init_Random;
----------------
-- Initialize --
----------------
procedure Initialize
(Config : in out SSL.Config;
Certificate_Filename : String;
Security_Mode : Method := TLS;
Priorities : String := "";
Ticket_Support : Boolean := False;
Key_Filename : String := "";
Exchange_Certificate : Boolean := False;
Certificate_Required : Boolean := False;
Trusted_CA_Filename : String := "";
CRL_Filename : String := "";
Session_Cache_Size : Natural := 16#4000#;
ALPN : SV.Vector := SV.Empty_Vector) is
begin
if Config = null then
Config := new TS_SSL;
end if;
Config.Initialize
(Certificate_Filename, Security_Mode, Priorities, Ticket_Support,
Key_Filename, Exchange_Certificate, Certificate_Required,
Trusted_CA_Filename, CRL_Filename, Session_Cache_Size,
To_Char_Array (ALPN));
end Initialize;
-------------------------------
-- Initialize_Default_Config --
-------------------------------
procedure Initialize_Default_Config
(Certificate_Filename : String;
Security_Mode : Method := TLS;
Priorities : String := "";
Ticket_Support : Boolean := False;
Key_Filename : String := "";
Exchange_Certificate : Boolean := False;
Certificate_Required : Boolean := False;
Trusted_CA_Filename : String := "";
CRL_Filename : String := "";
Session_Cache_Size : Natural := 16#4000#;
ALPN : SV.Vector := SV.Empty_Vector) is
begin
Default_Config.Initialize
(Certificate_Filename, Security_Mode, Priorities, Ticket_Support,
Key_Filename, Exchange_Certificate, Certificate_Required,
Trusted_CA_Filename, CRL_Filename, Session_Cache_Size,
To_Char_Array (ALPN));
end Initialize_Default_Config;
procedure Initialize_Default_Config is
package CNF renames AWS.Config;
Default : CNF.Object renames CNF.Default_Config;
begin
Default_Config.Initialize
(Certificate_Filename => CNF.Certificate (Default),
Security_Mode => Method'Value (CNF.Security_Mode (Default)),
Priorities => CNF.Cipher_Priorities (Default),
Ticket_Support => CNF.TLS_Ticket_Support (Default),
Key_Filename => CNF.Key (Default),
Exchange_Certificate => CNF.Exchange_Certificate (Default),
Certificate_Required => CNF.Certificate_Required (Default),
Trusted_CA_Filename => CNF.Trusted_CA (Default),
CRL_Filename => CNF.CRL_File (Default),
Session_Cache_Size => 16#4000#,
ALPN => Empty_Char_Array);
end Initialize_Default_Config;
-----------------
-- Lib_Realloc --
-----------------
function Lib_Realloc
(Ptr : System.Address;
Size : System.Memory.size_t) return System.Address is
begin
if Ptr = System.Null_Address then
return System.Memory.Alloc (Size);
else
return System.Memory.Realloc (Ptr, Size);
end if;
end Lib_Realloc;
----------
-- Load --
----------
function Load (Filename : String) return Private_Key is
Key : aliased Private_Key := EVP_PKEY_new;
IO : constant TSSL.BIO_Access := TSSL.BIO_new (TSSL.BIO_s_file);
Name : aliased C.char_array := C.To_C (Filename);
Pwd : aliased C.char_array :=
C.To_C (Net.SSL.Certificate.Get_Password (Filename));
begin
if TSSL.BIO_read_filename
(IO, C.Strings.To_Chars_Ptr (Name'Unchecked_Access)) = 0
or else PEM_read_bio_PrivateKey
(IO, Key'Access, null,
(if Pwd'Length <= 1 -- 1 means just the nul char
then TSSL.Null_Pointer
else Pwd'Address)) /= Key
then
TSSL.BIO_free (IO);
EVP_PKEY_free (Key);
File_Error ("Key", Filename);
end if;
TSSL.BIO_free (IO);
return Key;
end Load;
---------------
-- Log_Error --
---------------
procedure Log_Error (Text : String) is
begin
Log.Error (Socket_Type'(Net.Std.Socket_Type with others => <>), Text);
end Log_Error;
-------------
-- Pending --
-------------
overriding function Pending
(Socket : Socket_Type) return Stream_Element_Count
is
Res : constant C.int := TSSL.SSL_pending (Socket.SSL);
begin
Error_If (Socket, Res < 0);
return Stream_Element_Count (Res);
end Pending;
-------------
-- Receive --
-------------
overriding procedure Receive
(Socket : Socket_Type;
Data : out Stream_Element_Array;
Last : out Stream_Element_Offset)
is
Len : C.int;
First : Stream_Element_Offset := Data'First;
begin
loop
Len := TSSL.SSL_read
(Socket.SSL, Data (First)'Address,
Data'Length - C.int (First - Data'First));
if Len > 0 then
First := First + Stream_Element_Offset (Len);
Last := First - 1;
exit when Last = Data'Last;
else
exit when First > Data'First and then NSST (Socket).Pending = 0;
case TSSL.SSL_get_error (Socket.SSL, Len) is
when TSSL.SSL_ERROR_WANT_READ => Socket_Read (Socket);
when TSSL.SSL_ERROR_WANT_WRITE => Socket_Write (Socket);
when TSSL.SSL_ERROR_SYSCALL =>
Raise_Socket_Error
(Socket,
"System error ("
& Utils.Image (OS_Lib.Socket_Errno)
& ") on SSL receive");
when TSSL.SSL_ERROR_ZERO_RETURN =>
if Len = 0 then
raise Socket_Error with Peer_Closed_Message;
else