@@ -69,9 +69,19 @@ package body AWS.Net.SSL is
69
69
70
70
subtype Datum_Type is Certificate.Impl.Datum_Type;
71
71
72
+ type String_Access is access all String (Positive);
73
+ function To_String_Access is
74
+ new Unchecked_Conversion (TSSL.a_unsigned_char_t, String_Access);
75
+
76
+ function To_String (Datum : TSSL.gnutls_datum_t) return String is
77
+ (To_String_Access (Datum.data) (1 .. Integer (Datum.size)));
78
+
72
79
function Load_File (Filename : String) return Datum_Type
73
80
renames Certificate.Impl.Load_File;
74
81
82
+ procedure ALPN_Set (Socket : Socket_Type);
83
+ -- Set ALPN from config to secure socket before handshake
84
+
75
85
type PCert_Array is
76
86
array (Positive range <>) of aliased TSSL.gnutls_pcert_st
77
87
with Convention => C;
@@ -163,7 +173,7 @@ package body AWS.Net.SSL is
163
173
164
174
function Equal (Left, Right : TSSL.gnutls_datum_t) return Boolean;
165
175
166
- function Hash (Item : TSSL.gnutls_datum_t) return Containers.Hash_Type;
176
+ function Hash (Item : TSSL.gnutls_datum_t) return Ada. Containers.Hash_Type;
167
177
168
178
procedure Check_File (Prefix, Filename : String);
169
179
-- Check that Filename is present, raise an exception adding
@@ -175,14 +185,14 @@ package body AWS.Net.SSL is
175
185
end record ;
176
186
177
187
package Session_Container is
178
- new Containers.Hashed_Maps
188
+ new Ada. Containers.Hashed_Maps
179
189
(Key_Type => TSSL.gnutls_datum_t,
180
190
Element_Type => Session_Element,
181
191
Hash => Hash,
182
192
Equivalent_Keys => Equal);
183
193
184
194
package Time_Set is
185
- new Containers.Ordered_Maps
195
+ new Ada. Containers.Ordered_Maps
186
196
(Key_Type => Calendar.Time,
187
197
Element_Type => TSSL.gnutls_datum_t,
188
198
" <" => Calendar." <" ,
@@ -235,6 +245,7 @@ package body AWS.Net.SSL is
235
245
CRL_File : C.Strings.chars_ptr := C.Strings.Null_Ptr;
236
246
CRL_Semaphore : Utils.Semaphore;
237
247
CRL_Time_Stamp : Calendar.Time := Utils.AWS_Epoch;
248
+ ALPN : SV.Vector;
238
249
end record ;
239
250
240
251
procedure Initialize
@@ -284,7 +295,8 @@ package body AWS.Net.SSL is
284
295
Certificate_Required : Boolean;
285
296
Trusted_CA_Filename : String;
286
297
CRL_Filename : String;
287
- Session_Cache_Size : Natural);
298
+ Session_Cache_Size : Natural;
299
+ ALPN : SV.Vector);
288
300
289
301
private
290
302
Done : Boolean := False;
@@ -445,6 +457,68 @@ package body AWS.Net.SSL is
445
457
(Host, (new PCert_Array'(Load_PCert_List (4 )), TLS_PK));
446
458
end Add_Host_Certificate ;
447
459
460
+ -- ------------
461
+ -- ALPN_Get --
462
+ -- ------------
463
+
464
+ function ALPN_Get (Socket : Socket_Type) return String is
465
+ use type System.Address;
466
+ Datum : aliased TSSL.gnutls_datum_t;
467
+ Code : constant C.int :=
468
+ TSSL.gnutls_alpn_get_selected_protocol
469
+ (Socket.SSL, Datum'Access );
470
+ begin
471
+ if Code = TSSL.GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE then
472
+ Datum.data := System.Null_Address;
473
+ else
474
+ Check_Error_Code (Code);
475
+ end if ;
476
+
477
+ if Datum.data = System.Null_Address then
478
+ return " " ;
479
+ end if ;
480
+
481
+ return To_String (Datum);
482
+ end ALPN_Get ;
483
+
484
+ -- ------------
485
+ -- ALPN_Set --
486
+ -- ------------
487
+
488
+ procedure ALPN_Set (Socket : Socket_Type) is
489
+ type Datum_List is
490
+ array (1 .. Natural (Socket.Config.ALPN.Length)) of
491
+ aliased TSSL.gnutls_datum_t
492
+ with Convention => C;
493
+
494
+ type Datum_List_Access is access all Datum_List;
495
+
496
+ function To_Datum_Access is new Ada.Unchecked_Conversion
497
+ (Datum_List_Access, TSSL.a_gnutls_datum_t);
498
+
499
+ Datums : aliased Datum_List;
500
+
501
+ begin
502
+ for J in Datums'Range loop
503
+ Datums (J).data := Socket.Config.ALPN (J).Element.all 'Address;
504
+ Datums (J).size := Socket.Config.ALPN (J).Element'Length;
505
+ end loop ;
506
+
507
+ Check_Error_Code
508
+ (TSSL.gnutls_alpn_set_protocols
509
+ (Socket.SSL, To_Datum_Access (Datums'Access ), Datums'Length,
510
+ flags => 0 ));
511
+ end ALPN_Set ;
512
+
513
+ -- ------------
514
+ -- ALPN_Set --
515
+ -- ------------
516
+
517
+ procedure ALPN_Set (Config : SSL.Config; Protocols : SV.Vector) is
518
+ begin
519
+ Config.ALPN := Protocols;
520
+ end ALPN_Set ;
521
+
448
522
-- ----------------
449
523
-- Check_Config --
450
524
-- ----------------
@@ -742,9 +816,11 @@ package body AWS.Net.SSL is
742
816
Certificate_Required : Boolean;
743
817
Trusted_CA_Filename : String;
744
818
CRL_Filename : String;
745
- Session_Cache_Size : Natural) is
819
+ Session_Cache_Size : Natural;
820
+ ALPN : SV.Vector) is
746
821
begin
747
822
if not Done then
823
+ Default_Config.ALPN := ALPN;
748
824
Initialize
749
825
(Default_Config,
750
826
Certificate_Filename, Security_Mode, Priorities,
@@ -1029,14 +1105,9 @@ package body AWS.Net.SSL is
1029
1105
-- --------
1030
1106
1031
1107
function Hash
1032
- (Item : TSSL.gnutls_datum_t) return Containers.Hash_Type
1033
- is
1034
- type String_Access is access all String (Positive);
1035
- function To_Access is
1036
- new Unchecked_Conversion (TSSL.a_unsigned_char_t, String_Access);
1108
+ (Item : TSSL.gnutls_datum_t) return Ada.Containers.Hash_Type is
1037
1109
begin
1038
- return Strings.Hash
1039
- (To_Access (Item.data) (1 .. Natural (Item.size)));
1110
+ return Strings.Hash (To_String (Item));
1040
1111
end Hash ;
1041
1112
1042
1113
-- ---------
@@ -1073,20 +1144,23 @@ package body AWS.Net.SSL is
1073
1144
procedure Initialize
1074
1145
(Config : in out SSL.Config;
1075
1146
Certificate_Filename : String;
1076
- Security_Mode : Method := TLS;
1077
- Priorities : String := " " ;
1078
- Ticket_Support : Boolean := False;
1079
- Key_Filename : String := " " ;
1080
- Exchange_Certificate : Boolean := False;
1081
- Certificate_Required : Boolean := False;
1082
- Trusted_CA_Filename : String := " " ;
1083
- CRL_Filename : String := " " ;
1084
- Session_Cache_Size : Natural := 16#4000# ) is
1147
+ Security_Mode : Method := TLS;
1148
+ Priorities : String := " " ;
1149
+ Ticket_Support : Boolean := False;
1150
+ Key_Filename : String := " " ;
1151
+ Exchange_Certificate : Boolean := False;
1152
+ Certificate_Required : Boolean := False;
1153
+ Trusted_CA_Filename : String := " " ;
1154
+ CRL_Filename : String := " " ;
1155
+ Session_Cache_Size : Natural := 16#4000# ;
1156
+ ALPN : SV.Vector := SV.Empty_Vector) is
1085
1157
begin
1086
1158
if Config = null then
1087
1159
Config := new TS_SSL;
1088
1160
end if ;
1089
1161
1162
+ Config.ALPN := ALPN;
1163
+
1090
1164
Initialize
1091
1165
(Config.all ,
1092
1166
Certificate_Filename => Certificate_Filename,
@@ -1271,20 +1345,21 @@ package body AWS.Net.SSL is
1271
1345
1272
1346
procedure Initialize_Default_Config
1273
1347
(Certificate_Filename : String;
1274
- Security_Mode : Method := TLS;
1275
- Priorities : String := " " ;
1276
- Ticket_Support : Boolean := False;
1277
- Key_Filename : String := " " ;
1278
- Exchange_Certificate : Boolean := False;
1279
- Certificate_Required : Boolean := False;
1280
- Trusted_CA_Filename : String := " " ;
1281
- CRL_Filename : String := " " ;
1282
- Session_Cache_Size : Natural := 16#4000# ) is
1348
+ Security_Mode : Method := TLS;
1349
+ Priorities : String := " " ;
1350
+ Ticket_Support : Boolean := False;
1351
+ Key_Filename : String := " " ;
1352
+ Exchange_Certificate : Boolean := False;
1353
+ Certificate_Required : Boolean := False;
1354
+ Trusted_CA_Filename : String := " " ;
1355
+ CRL_Filename : String := " " ;
1356
+ Session_Cache_Size : Natural := 16#4000# ;
1357
+ ALPN : SV.Vector := SV.Empty_Vector) is
1283
1358
begin
1284
1359
Default_Config_Sync.Initialize
1285
1360
(Certificate_Filename, Security_Mode, Priorities, Ticket_Support,
1286
1361
Key_Filename, Exchange_Certificate, Certificate_Required,
1287
- Trusted_CA_Filename, CRL_Filename, Session_Cache_Size);
1362
+ Trusted_CA_Filename, CRL_Filename, Session_Cache_Size, ALPN );
1288
1363
end Initialize_Default_Config ;
1289
1364
1290
1365
procedure Initialize_Default_Config is
@@ -1993,6 +2068,8 @@ package body AWS.Net.SSL is
1993
2068
-- Retrieve_Certificate for client.
1994
2069
1995
2070
TSSL.gnutls_session_set_ptr (Socket.SSL, Socket.Config.all 'Address);
2071
+
2072
+ ALPN_Set (Socket);
1996
2073
end Session_Transport ;
1997
2074
1998
2075
-- --------------
0 commit comments