tlsada_2.5.2_7e2dcb92/src/tls-contexts.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
-- The base types and operations used by all context types
--
--
-- Copyright (c) 2022 nytpu <alex [at] nytpu.com>
-- SPDX-License-Identifier: MPL-2.0
-- For more license details, see LICENSE or <https://www.mozilla.org/en-US/MPL/2.0/>.

pragma Ada_2012;

with Ada.Calendar.Formatting;
with Interfaces;
with Interfaces.C;  use Interfaces.C;
with System.Storage_Elements;

pragma Style_Checks(Off);

package body TLS.Contexts is

	package Bs renames libTLS_Bindings;


-------------------------------------------------------------------------------
--------------------------------- Base Context --------------------------------
-------------------------------------------------------------------------------

	--------------------
	-- Is_Initialized --
	--------------------

	function Is_Initialized (Ctx : Context) return Boolean
	is (Ctx.Context.Context /= null);


	------------------
	-- Is_Configued --
	------------------

	function Is_Configued (Ctx : Context) return Boolean
	is (Ctx.Context.Configured);


	------------------
	-- Is_Connected --
	------------------

	function Is_Connected (Ctx : Context) return Boolean
	is (Ctx.Context.Connected);


	---------------
	-- Configure --
	---------------

	procedure Configure (Ctx : in out Context; Conf : Config) is
		use Interfaces;


		-------------------
		-- Assign_String --
		-------------------

		-- This is the function prototype used for most of the
		-- tls_config_set_*_file functions
		type Config_String_Function is access
			function (C : access Bs.tls_config; S : chars_ptr) return int
				with Convention => C;

		-- Given an Ada string, convert that to a C string and call the given
		-- String_Function with it, throwing an exception if the
		-- String_Function call didn't return 0.
		procedure Assign_String
			(Cfg : access Bs.tls_config; Str : String;
			Call : Config_String_Function)
		is
			String_Holder : chars_ptr := Null_Ptr;
			RC : int;
		begin
			if Str'Length /= 0 then
				String_Holder := Create_C_String(Str);
				RC := Call(Cfg, String_Holder);
				Free(String_Holder);
				if RC /= 0 then
					raise TLS_Config_Error
						with "unable to load file: " &
							Value(Bs.tls_config_error(Cfg));
				end if;
			end if;
		end Assign_String;

		-- Same as Assign_String but take an Unbounded_String for convenience
		procedure Assign_String
			(Cfg : access Bs.tls_config; Str : Unbounded_String;
			Call : Config_String_Function)
		is
		begin
			Assign_String(Cfg, To_String(Str), Call);
		end Assign_String;

		C : access Bs.tls_config;
		RC : int;
	begin
		C := Bs.tls_config_new;
		if C = null then
			raise TLS_Config_Error
				with "unable to generate libtls tls_config";
		end if;

		if Conf.Require_OCSP_Stapling then
			Bs.tls_config_ocsp_require_stapling(C);
		end if;

		declare
			Protocols : constant Unsigned_32 :=
				(if Conf.TLSv1_0 then Bs.TLS_PROTOCOL_TLSv1_0 else 0) or
				(if Conf.TLSv1_1 then Bs.TLS_PROTOCOL_TLSv1_1 else 0) or
				(if Conf.TLSv1_2 then Bs.TLS_PROTOCOL_TLSv1_2 else 0) or
				(if Conf.TLSv1_3 then Bs.TLS_PROTOCOL_TLSv1_3 else 0);
		begin
			RC := Bs.tls_config_set_protocols(C, Protocols);
			if RC < 0 then
				raise TLS_Config_Error
					with "unable to configure protocols: " &
						Value(Bs.tls_config_error(C));
			end if;
		end;

		case Conf.Cipher_Preference is
			when Server => Bs.tls_config_prefer_ciphers_server(C);
			when Client => Bs.tls_config_prefer_ciphers_client(C);
		end case;

		-- due to the way libTLS works, we have to enable all verifications
		-- then disable one-by-one
		Bs.tls_config_verify(C);
		if not Conf.Verify_Certs then
			Bs.tls_config_insecure_noverifycert(C);
		end if;
		if not Conf.Verify_Server_Name then
			Bs.tls_config_insecure_noverifyname(C);
		end if;
		if not Conf.Verify_Expirys then
			Bs.tls_config_insecure_noverifytime(C);
		end if;

		Assign_String(C, Conf.CA_Search_Path,
			Bs.tls_config_set_ca_path'Access);
		Assign_String(C, Conf.CA_File,
			Bs.tls_config_set_ca_file'Access);
		Assign_String(C, Conf.Cert_File,
			Bs.tls_config_set_cert_file'Access);
		Assign_String(C, Conf.CRL_File,
			Bs.tls_config_set_crl_file'Access);
		Assign_String(C, Conf.Key_File,
			Bs.tls_config_set_key_file'Access);
		Assign_String(C, Conf.OCSP_Staple_File,
			Bs.tls_config_set_ocsp_staple_file'Access);

		for KP of Conf.Additional_Keypairs loop
			if Length(KP.Cert_File) /= 0 and Length(KP.Key_File) /= 0 then
				declare
					Cert, Key, OCSP : chars_ptr := Null_Ptr;
					RC : int;
				begin
					Cert := Create_C_String(To_String(KP.Cert_File));
					Key := Create_C_String(To_String(KP.Key_File));
					if Length(KP.OCSP_Staple_File) /= 0 then
						OCSP := Create_C_String(To_String(KP.OCSP_Staple_File));
						RC := Bs.tls_config_add_keypair_ocsp_file
							(C, Cert, Key, OCSP);
					else
						RC := Bs.tls_config_add_keypair_file
							(C, Cert, Key);
					end if;
					Free(Cert);
					Free(Key);
					Free(OCSP);
					if RC /= 0 then
						raise TLS_Config_Error
							with "unable to load file: " &
								Value(Bs.tls_config_error(C));
					end if;
				end;
			end if;
		end loop;

		RC := Bs.tls_config_set_verify_depth
			(C, int(Conf.Cert_Verify_Depth));
		if RC /= 0 then
			raise TLS_Config_Error
				with "unable to set verify depth: " &
					Value(Bs.tls_config_error(C));
		end if;

		case Conf.Verify_Client_Cert is
			when Mandatory => Bs.tls_config_verify_client(C);
			when Optional => Bs.tls_config_verify_client_optional(C);
			when None => null;
		end case;

		-- Unchecked and unsafe settings
		case Conf.DHE_Parameters is
			when None => null; -- current libtls default
			when Auto =>
				Assign_String(C, "auto", Bs.tls_config_set_dheparams'Access);
			when Legacy =>
				Assign_String(C, "legacy", Bs.tls_config_set_dheparams'Access);
		end case;
		Assign_String(C, Conf.ALPN_Protocols,
			Bs.tls_config_set_alpn'Access);
		Assign_String(C, Conf.Ciphers,
			Bs.tls_config_set_ciphers'Access);
		Assign_String(C, Conf.ECDHE_Curves,
			Bs.tls_config_set_ecdhecurves'Access);

		-- Actually configure the context
		RC := Bs.tls_configure(Ctx.Context.Context, C);
		Bs.tls_config_free(C);
		if RC /= 0 then
			raise TLS_Error
				with "unable to configure context: " & Retrieve_Error_Message(Ctx.Context);
		end if;
		Ctx.Context.Configured := True;
	end Configure;


	----------
	-- Read --
	----------

	overriding procedure Read
		(Ctx : in out Context;
		Item : out Stream_Element_Array; Last : out Stream_Element_Offset)
	is
		-- make sure we get the ceiling of the value (i.e. always rounded up)
		-- <https://old.reddit.com/r/ada/comments/sxxxis/convert_array_length_tofrom_size_t/hxw2v2h/>
		Stream_Element_Size : constant size_t :=
			(Stream_Element'Size + CHAR_BIT - 1) / CHAR_BIT;

		Sz : Bs.ssize_t;
	begin
		loop
			Sz := Bs.tls_read(
				Ctx.Context.Context,
				Item(Item'First)'Address,
				Item'Length * Stream_Element_Size
				-- Alternatively:
				-- size_t(Item'Size + CHAR_BIT - 1) / CHAR_BIT
			);
			exit when Sz /= Bs.TLS_WANT_POLLIN and Sz /= Bs.TLS_WANT_POLLOUT;
		end loop;

		if Sz < 0 then
			raise Device_Error with "unable to read data: " &
				Retrieve_Error_Message(Ctx.Context);
		elsif Sz = 0 then
			Last := Stream_Element_Offset'First;
		else
			-- Subtract one from Item'First since C is zero-indexed while Item
			-- isn't necessarily.
			Last := (Item'First - 1) + Stream_Element_Offset(size_t(Sz) / Stream_Element_Size);
		end if;
	end Read;


	-----------
	-- Write --
	-----------

	overriding procedure Write
		(Ctx : in out Context; Item : Stream_Element_Array)
	is
		use System.Storage_Elements;

		Remaining : Bs.ssize_t :=
			Bs.ssize_t(Item'Length) * Bs.ssize_t(Stream_Element'Size / CHAR_BIT);
		Current : System.Address := Item(Item'First)'Address;

		Sz : Bs.ssize_t;
	begin
		-- Keep going until we write the entire buffer, since tls_write can
		-- decide to write as little as it wants.
		while Remaining > 0 loop
			Sz := Bs.tls_write(
				Ctx.Context.Context,
				Current,
				size_t(Remaining)
			);
			case Sz is
				when -1 =>
					raise Device_Error with "unable to write data: " &
						Retrieve_Error_Message(Ctx.Context);

				when Bs.TLS_WANT_POLLIN | Bs.TLS_WANT_POLLOUT => null;

				when others =>
					-- FIXME: pointer arithmetic, really? There has to be a
					-- better way to do this...
					Remaining := Remaining - Sz;
					Current := Current + Storage_Offset(Sz);
			end case;
		end loop;
	end Write;


	-----------
	-- Close --
	-----------

	procedure Close (Ctx : in out Context) is
	begin
		Close(Ctx.Context);
		Bs.tls_reset(Ctx.Context.Context);
		Ctx.Context.Configured := False;
	end Close;


	-------------------------
	-- Get_Connection_Info --
	-------------------------

	function Get_Connection_Info (Ctx : Context) return Connection_Info is
		R : int;
		O : Connection_Info;
	begin
		O.TLS_Version :=
			Call_String_Function(Bs.tls_conn_version'Access, Ctx.Context);
		O.Cipher :=
			Call_String_Function(Bs.tls_conn_cipher'Access, Ctx.Context);
		O.ALPN :=
			Call_String_Function(Bs.tls_conn_alpn_selected'Access, Ctx.Context);

		R := Bs.tls_conn_cipher_strength(Ctx.Context.Context);
		if R < 0 then
			raise TLS_Error with Retrieve_Error_Message(Ctx.Context);
		end if;
		O.Cipher_Strength := Natural(R);

		return O;
	end Get_Connection_Info;


	-------------------------------
	-- Peer_Certificate_Provided --
	-------------------------------

	function Peer_Certificate_Provided (Ctx : Context) return Boolean
	is (
		case Bs.tls_peer_cert_provided(Ctx.Context.Context) is
			when 1 => True,
			when 0 => False,
			when others =>
				raise TLS_Error
					with Retrieve_Error_Message(Ctx.Context)
	);


	--------------------------
	-- Get_Certificate_Info --
	--------------------------

	function Get_Certificate_Info (Ctx : Context) return Certificate_Info is
		R : Bs.time_t;
		O : Certificate_Info;
	begin
		O.Hash :=
			Call_String_Function(Bs.tls_peer_cert_hash'Access, Ctx.Context);
		O.Issuer :=
			Call_String_Function(Bs.tls_peer_cert_issuer'Access, Ctx.Context);
		O.Subject :=
			Call_String_Function(Bs.tls_peer_cert_subject'Access, Ctx.Context);

		R := Bs.tls_peer_cert_notbefore(Ctx.Context.Context);
		if R < 0 then
			raise TLS_Error with Retrieve_Error_Message(Ctx.Context);
		end if;
		O.Not_Before := Convert_Time(R);

		R := Bs.tls_peer_cert_notafter(Ctx.Context.Context);
		if R < 0 then
			raise TLS_Error with Retrieve_Error_Message(Ctx.Context);
		end if;
		O.Not_Before := Convert_Time(R);

		return O;
	end Get_Certificate_Info;


	------------------------------------
	-- Peer_Certificate_Contains_Name --
	------------------------------------

	function Peer_Certificate_Contains_Name
		(Ctx : Context; Name : String)
		return Boolean
	is
		Name_Ptr : chars_ptr := Create_C_String(Name);
		R : int;
	begin
		R := Bs.tls_peer_cert_contains_name(Ctx.Context.Context, Name_Ptr);
		Free(Name_Ptr);
		case R is
			when 1 => return True;
			when 0 => return False;
			when others =>
				raise TLS_Error with Retrieve_Error_Message(Ctx.Context);
		end case;
	end Peer_Certificate_Contains_Name;


	-------------------
	-- Get_OCSP_Info --
	-------------------

	function Get_OCSP_Info (Ctx : Context) return OCSP_Info is
		O : OCSP_Info;
		RT : Bs.time_t;
	begin
		O.OCSP_URL :=
			Call_String_Function(Bs.tls_peer_ocsp_url'Access, Ctx.Context);

		O.Response_Status := (
			case Bs.tls_peer_ocsp_response_status(Ctx.Context.Context) is
				when Bs.TLS_OCSP_RESPONSE_SUCCESSFUL => Successful,
				when Bs.TLS_OCSP_RESPONSE_MALFORMED => Malformed,
				when Bs.TLS_OCSP_RESPONSE_INTERNALERROR => Internal_Error,
				when Bs.TLS_OCSP_RESPONSE_TRYLATER => Try_Later,
				when Bs.TLS_OCSP_RESPONSE_SIGREQUIRED => Sig_Required,
				when Bs.TLS_OCSP_RESPONSE_UNAUTHORIZED => Unauthorized,
				when others => raise TLS_Error with "no ocsp validation available"
		);

		O.Certificate_Status := (
			case Bs.tls_peer_ocsp_cert_status(Ctx.Context.Context) is
				when Bs.TLS_OCSP_CERT_GOOD => Good,
				when Bs.TLS_OCSP_CERT_REVOKED => Revoked,
				when Bs.TLS_OCSP_CERT_UNKNOWN => Unknown,
				when others => raise TLS_Error with "no ocsp validation available"
		);

		O.Revocation_Reason := (
			case Bs.tls_peer_ocsp_crl_reason(Ctx.Context.Context) is
				when Bs.TLS_CRL_REASON_UNSPECIFIED => Unspecified,
				when Bs.TLS_CRL_REASON_KEY_COMPROMISE => Key_Compromise,
				when Bs.TLS_CRL_REASON_CA_COMPROMISE => CA_Compromise,
				when Bs.TLS_CRL_REASON_AFFILIATION_CHANGED => Affiliation_Changed,
				when Bs.TLS_CRL_REASON_SUPERSEDED => Superseded,
				when Bs.TLS_CRL_REASON_CESSATION_OF_OPERATION => Cessation_of_Operation,
				when Bs.TLS_CRL_REASON_CERTIFICATE_HOLD => Certificate_Hold,
				when Bs.TLS_CRL_REASON_REMOVE_FROM_CRL => Remove_From_CRL,
				when Bs.TLS_CRL_REASON_PRIVILEGE_WITHDRAWN => Priviledge_Withdrawn,
				when Bs.TLS_CRL_REASON_AA_COMPROMISE => AA_Compromise,
				when others => raise TLS_Error with "no ocsp validation available"
		);

		RT := Bs.tls_peer_ocsp_revocation_time(Ctx.Context.Context);
		if RT < 0 then
			raise TLS_Error with "no ocsp validation available";
		end if;
		O.Revocation_Time := Convert_Time(RT);

		RT := Bs.tls_peer_ocsp_this_update(Ctx.Context.Context);
		if RT < 0 then
			raise TLS_Error with "no ocsp validation available";
		end if;
		O.This_Update := Convert_Time(RT);

		RT := Bs.tls_peer_ocsp_next_update(Ctx.Context.Context);
		if RT < 0 then
			raise TLS_Error with "no ocsp validation available";
		end if;
		O.Next_Update := Convert_Time(RT);

		return O;
	end Get_OCSP_Info;


-------------------------------------------------------------------------------
------------------------------ Helper Subprograms -----------------------------
-------------------------------------------------------------------------------

	------------------
	-- Convert_Time --
	------------------

	function Convert_Time (T : libTLS_Bindings.time_t) return Time is
		Epoch : constant Time :=
			Ada.Calendar.Formatting.Time_Of(1970, 1, 1, 0.0);
	begin
		return Epoch + Duration(T);
	end Convert_Time;


	---------------------
	-- Create_C_String --
	---------------------

	function Create_C_String (S : String) return chars_ptr is
		O : chars_ptr := New_String(S);
	begin
		if O = Null_Ptr then
			raise TLS_Error with "error allocating c string for '" & S & "'";
		end if;
		return O;
	end Create_C_String;


	----------------------------
	-- Retrieve_Error_Message --
	----------------------------

	function Retrieve_Error_Message (Ctx : Context_Wrapper) return String is
		S : constant chars_ptr := Bs.tls_error(Ctx.Context);
	begin
		if S = Null_Ptr then
			return "";
		end if;
		-- Returns a static pointer so there is no need to free it
		return Value(S);
	end Retrieve_Error_Message;


	--------------------------
	-- Call_String_Function --
	--------------------------

	function Call_String_Function
		(Func : String_Function; Ctx : Context_Wrapper)
		return Unbounded_String
	is
		S : constant chars_ptr := Func(Ctx.Context);
	begin
		if S = Null_Ptr then
			raise TLS_Error with Retrieve_Error_Message(Ctx);
		end if;
		return To_Unbounded_String(Value(S));
	end Call_String_Function;


-------------------------------------------------------------------------------
------------------------------- Context_Wrapper -------------------------------
-------------------------------------------------------------------------------

	-----------------------
	-- Initialize_Client --
	-----------------------

	procedure Initialize_Client (Object : in out Context_Wrapper) is
	begin
		Object.Context := Bs.tls_client;
		if Object.Context = null then
			raise TLS_Error
				with "unable to generate libtls tls context";
		end if;
	end Initialize_Client;


	-----------------------
	-- Initialize_Server --
	-----------------------

	procedure Initialize_Server (Object : in out Context_Wrapper) is
	begin
		Object.Context := Bs.tls_server;
		if Object.Context = null then
			raise TLS_Error
				with "unable to generate libtls tls context";
		end if;
	end Initialize_Server;


	-----------
	-- Close --
	-----------

	procedure Close (Ctx : in out Context_Wrapper) is
		R : int;
	begin
		if Ctx.Connected then
			loop
				R := Bs.tls_close(Ctx.Context);
				if R = -1 then
					raise Connect_Error
						with "unable to close connection: " &
							Retrieve_Error_Message(Ctx);
				end if;
				exit when R /= Bs.TLS_WANT_POLLIN and R /= Bs.TLS_WANT_POLLOUT;
			end loop;
			Ctx.Connected := False;
		end if;
	end Close;


	--------------
	-- Finalize --
	--------------

	overriding procedure Finalize (Ctx : in out Context_Wrapper) is
	begin
		if Ctx.Context /= null then
			Close(Ctx);

			-- Both types of internal contexts are freed the same way
			Bs.tls_free(Ctx.Context);

			Ctx.Context := null;
			Ctx.Configured := False;
		end if;
	end Finalize;

end TLS.Contexts;