summaryrefslogtreecommitdiff
path: root/gcr/gcr-trust.c
blob: d5c8ec5ad77ee3fea4b2c35053f1e15f7ba4e073 (plain)
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
/*
 * gnome-keyring
 *
 * Copyright (C) 2010 Collabora Ltd
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This program 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.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
 * Author: Stef Walter <stefw@collabora.co.uk>
 */

#include "config.h"

#include "gcr-types.h"
#include "gcr-internal.h"
#include "gcr-library.h"
#include "gcr-trust.h"

#include "gck/gck.h"
#include "gck/pkcs11n.h"
#include "gck/pkcs11i.h"
#include "gck/pkcs11x.h"

#include <glib/gi18n-lib.h>

/**
 * GCR_PURPOSE_SERVER_AUTH:
 *
 * The purpose used to verify the server certificate in a TLS connection. This
 * is the most common purpose in use.
 */

/**
 * GCR_PURPOSE_CLIENT_AUTH:
 *
 * The purpose used to verify the client certificate in a TLS connection.
 */

/**
 * GCR_PURPOSE_CODE_SIGNING:
 *
 * The purpose used to verify certificate used for the signature on signed code.
 */

/**
 * GCR_PURPOSE_EMAIL:
 *
 * The purpose used to verify certificates that are used in email communication
 * such as S/MIME.
 */

/* ----------------------------------------------------------------------------------
 * HELPERS
 */

static void
prepare_trust_attrs (GcrCertificate *certificate,
                     CK_X_ASSERTION_TYPE type,
                     GckBuilder *builder)
{
	gconstpointer data;
	gsize n_data;

	gck_builder_add_ulong (builder, CKA_CLASS, CKO_X_TRUST_ASSERTION);
	gck_builder_add_ulong (builder, CKA_X_ASSERTION_TYPE, type);

	data = gcr_certificate_get_der_data (certificate, &n_data);
	g_return_if_fail (data);
	gck_builder_add_data (builder, CKA_X_CERTIFICATE_VALUE, data, n_data);
}

/* ----------------------------------------------------------------------------------
 * GET PINNED CERTIFICATE
 */

static GckAttributes *
prepare_is_certificate_pinned (GcrCertificate *certificate, const gchar *purpose, const gchar *peer)
{
	GckBuilder builder = GCK_BUILDER_INIT;

	prepare_trust_attrs (certificate, CKT_X_PINNED_CERTIFICATE, &builder);

	gck_builder_add_string (&builder, CKA_X_PURPOSE, purpose);
	gck_builder_add_string (&builder, CKA_X_PEER, peer);

	return gck_builder_end (&builder);
}

static gboolean
perform_is_certificate_pinned (GckAttributes *search,
                               GCancellable *cancellable,
                               GError **error)
{
	GckEnumerator *en;
	GList *slots;
	GckObject *object;

	if (!gcr_pkcs11_initialize (cancellable, error))
		return FALSE;

	slots = gcr_pkcs11_get_trust_lookup_slots ();
	g_debug ("searching for pinned certificate in %d slots",
	         g_list_length (slots));
	en = gck_slots_enumerate_objects (slots, search, 0);
	g_clear_list (&slots, g_object_unref);

	object = gck_enumerator_next (en, cancellable, error);
	g_object_unref (en);

	if (object)
		g_object_unref (object);

	g_debug ("%s certificate anchor", object ? "found" : "did not find");
	return (object != NULL);
}

/**
 * gcr_trust_is_certificate_pinned:
 * @certificate: a #GcrCertificate to check
 * @purpose: the purpose string
 * @peer: the peer for this pinned
 * @cancellable: a #GCancellable
 * @error: a #GError, or %NULL
 *
 * Check if @certificate is pinned for @purpose to communicate with @peer.
 * A pinned certificate overrides all other certificate verification.
 *
 * This call may block, see gcr_trust_is_certificate_pinned_async() for the
 * non-blocking version.
 *
 * In the case of an error, %FALSE is also returned. Check @error to detect
 * if an error occurred.
 *
 * Returns: %TRUE if the certificate is pinned for the host and purpose
 */
gboolean
gcr_trust_is_certificate_pinned (GcrCertificate *certificate, const gchar *purpose,
                                 const gchar *peer, GCancellable *cancellable, GError **error)
{
	GckAttributes *search;
	gboolean ret;

	g_return_val_if_fail (GCR_IS_CERTIFICATE (certificate), FALSE);
	g_return_val_if_fail (purpose, FALSE);
	g_return_val_if_fail (peer, FALSE);

	search = prepare_is_certificate_pinned (certificate, purpose, peer);
	g_return_val_if_fail (search, FALSE);

	ret = perform_is_certificate_pinned (search, cancellable, error);
	gck_attributes_unref (search);

	return ret;
}

static void
thread_is_certificate_pinned (GTask *task, gpointer object,
                              gpointer task_data, GCancellable *cancellable)
{
	GckAttributes *attrs = task_data;
	GError *error = NULL;
	gboolean found;

	found = perform_is_certificate_pinned (attrs, cancellable, &error);
	if (error == NULL)
		g_task_return_boolean (task, found);
	else
		g_task_return_error (task, g_steal_pointer (&error));
}

/**
 * gcr_trust_is_certificate_pinned_async:
 * @certificate: a #GcrCertificate to check
 * @purpose: the purpose string
 * @peer: the peer for this pinned
 * @cancellable: a #GCancellable
 * @callback: a #GAsyncReadyCallback to call when the operation completes
 * @user_data: the data to pass to callback function
 *
 * Check if @certificate is pinned for @purpose to communicate with @peer. A
 * pinned certificate overrides all other certificate verification.
 *
 * When the operation is finished, callback will be called. You can then call
 * [func@Gcr.trust_is_certificate_pinned_finish] to get the result of the
 * operation.
 */
void
gcr_trust_is_certificate_pinned_async (GcrCertificate *certificate, const gchar *purpose,
                                       const gchar *peer, GCancellable *cancellable,
                                       GAsyncReadyCallback callback, gpointer user_data)
{
	GTask *task;
	GckAttributes *attrs;

	g_return_if_fail (GCR_IS_CERTIFICATE (certificate));
	g_return_if_fail (purpose);
	g_return_if_fail (peer);

	task = g_task_new (NULL, cancellable, callback, user_data);
	g_task_set_source_tag (task, gcr_trust_is_certificate_pinned_async);

	attrs = prepare_is_certificate_pinned (certificate, purpose, peer);
	g_return_if_fail (attrs);
	g_task_set_task_data (task, attrs, gck_attributes_unref);

	g_task_run_in_thread (task, thread_is_certificate_pinned);

	g_clear_object (&task);
}

/**
 * gcr_trust_is_certificate_pinned_finish:
 * @result: the #GAsyncResult passed to the callback
 * @error: a #GError, or %NULL
 *
 * Finishes an asynchronous operation started by
 * gcr_trust_is_certificate_pinned_async().
 *
 * In the case of an error, %FALSE is also returned. Check @error to detect
 * if an error occurred.
 *
 * Returns: %TRUE if the certificate is pinned.
 */
gboolean
gcr_trust_is_certificate_pinned_finish (GAsyncResult *result, GError **error)
{
	g_return_val_if_fail (!error || !*error, FALSE);
	g_return_val_if_fail (g_task_is_valid (result, NULL), FALSE);

	return g_task_propagate_boolean (G_TASK (result), error);
}

/* ----------------------------------------------------------------------------------
 * ADD PINNED CERTIFICATE
 */

static GckAttributes *
prepare_add_pinned_certificate (GcrCertificate *certificate, const gchar *purpose, const gchar *peer)
{
	GckBuilder builder = GCK_BUILDER_INIT;

	prepare_trust_attrs (certificate, CKT_X_PINNED_CERTIFICATE, &builder);

	gck_builder_add_string (&builder, CKA_X_PURPOSE, purpose);
	gck_builder_add_string (&builder, CKA_X_PEER, peer);
	gck_builder_add_boolean (&builder, CKA_TOKEN, TRUE);

	return gck_builder_end (&builder);
}

static gboolean
perform_add_pinned_certificate (GckAttributes *search,
                                GCancellable *cancellable,
                                GError **error)
{
	GckBuilder builder = GCK_BUILDER_INIT;
	gboolean ret = FALSE;
	GError *lerr = NULL;
	GckObject *object;
	GckSession *session;
	GckSlot *slot;
	GckEnumerator *en;
	GList *slots;

	if (!gcr_pkcs11_initialize (cancellable, error))
		return FALSE;

	slots = gcr_pkcs11_get_trust_lookup_slots ();
	en = gck_slots_enumerate_objects (slots, search, CKF_RW_SESSION);
	g_clear_list (&slots, g_object_unref);

	object = gck_enumerator_next (en, cancellable, &lerr);
	g_object_unref (en);

	if (lerr != NULL) {
		g_propagate_error (error, lerr);
		return FALSE;
	}

	/* It already exists */
	if (object) {
		g_object_unref (object);
		return TRUE;
	}

	gck_builder_add_all (&builder, search);

	/* TODO: Add relevant label */

	/* Find an appropriate token */
	slot = gcr_pkcs11_get_trust_store_slot ();
	if (slot == NULL) {
		g_set_error (&lerr, GCK_ERROR, CKR_FUNCTION_FAILED,
		             /* Translators: A pinned certificate is an exception which
		                trusts a given certificate explicitly for a purpose and
		                communication with a certain peer. */
		             _("Couldn’t find a place to store the pinned certificate"));
		ret = FALSE;
	} else {
		session = gck_slot_open_session (slot, CKF_RW_SESSION, NULL, NULL, &lerr);
		if (session != NULL) {
			GckAttributes *attrs = gck_builder_end (&builder);
			object = gck_session_create_object (session, attrs,
			                                    cancellable, &lerr);
			if (object != NULL) {
				g_object_unref (object);
				ret = TRUE;
			}

			g_object_unref (session);
			gck_attributes_unref (attrs);
		}

		g_object_unref (slot);
	}

	gck_builder_clear (&builder);

	if (!ret)
		g_propagate_error (error, lerr);

	return ret;
}

/**
 * gcr_trust_add_pinned_certificate:
 * @certificate: a #GcrCertificate
 * @purpose: the purpose string
 * @peer: the peer for this pinned certificate
 * @cancellable: a #GCancellable
 * @error: a #GError, or %NULL
 *
 * Add a pinned @certificate for connections to @peer for @purpose. A pinned
 * certificate overrides all other certificate verification and should be
 * used with care.
 *
 * If the same pinned certificate already exists, then this operation
 * does not add another, and succeeds without error.
 *
 * This call may block, see gcr_trust_add_pinned_certificate_async() for the
 * non-blocking version.
 *
 * Returns: %TRUE if the pinned certificate is recorded successfully
 */
gboolean
gcr_trust_add_pinned_certificate (GcrCertificate *certificate, const gchar *purpose, const gchar *peer,
                                  GCancellable *cancellable, GError **error)
{
	GckAttributes *search;
	gboolean ret;

	g_return_val_if_fail (GCR_IS_CERTIFICATE (certificate), FALSE);
	g_return_val_if_fail (purpose, FALSE);
	g_return_val_if_fail (peer, FALSE);

	search = prepare_add_pinned_certificate (certificate, purpose, peer);
	g_return_val_if_fail (search, FALSE);

	ret = perform_add_pinned_certificate (search, cancellable, error);
	gck_attributes_unref (search);

	return ret;
}

static void
thread_add_pinned_certificate (GTask *task, gpointer object,
                               gpointer task_data, GCancellable *cancellable)
{
	GckAttributes *attrs = task_data;
	GError *error = NULL;

	perform_add_pinned_certificate (attrs, cancellable, &error);
	if (error == NULL)
		g_task_return_boolean (task, TRUE);
	else
		g_task_return_error (task, g_steal_pointer (&error));
}

/**
 * gcr_trust_add_pinned_certificate_async:
 * @certificate: a #GcrCertificate
 * @purpose: the purpose string
 * @peer: the peer for this pinned certificate
 * @cancellable: a #GCancellable
 * @callback: a #GAsyncReadyCallback to call when the operation completes
 * @user_data: the data to pass to callback function
 *
 * Add a pinned certificate for communication with @peer for @purpose. A pinned
 * certificate overrides all other certificate verification and should be used
 * with care.
 *
 * If the same pinned certificate already exists, then this operation
 * does not add another, and succeeds without error.
 *
 * When the operation is finished, callback will be called. You can then call
 * [func@Gcr.trust_add_pinned_certificate_finish] to get the result of the
 * operation.
 */
void
gcr_trust_add_pinned_certificate_async (GcrCertificate *certificate, const gchar *purpose,
                                        const gchar *peer, GCancellable *cancellable,
                                        GAsyncReadyCallback callback, gpointer user_data)
{
	GTask *task;
	GckAttributes *attrs;

	g_return_if_fail (GCR_IS_CERTIFICATE (certificate));
	g_return_if_fail (purpose);
	g_return_if_fail (peer);

	task = g_task_new (NULL, cancellable, callback, user_data);
	g_task_set_source_tag (task, gcr_trust_add_pinned_certificate_async);

	attrs = prepare_add_pinned_certificate (certificate, purpose, peer);
	g_return_if_fail (attrs);
	g_task_set_task_data (task, attrs, gck_attributes_unref);

	g_task_run_in_thread (task, thread_add_pinned_certificate);

	g_clear_object (&task);
}

/**
 * gcr_trust_add_pinned_certificate_finish:
 * @result: the #GAsyncResult passed to the callback
 * @error: a #GError, or %NULL
 *
 * Finishes an asynchronous operation started by
 * gcr_trust_add_pinned_certificate_async().
 *
 * Returns: %TRUE if the pinned certificate is recorded successfully
 */
gboolean
gcr_trust_add_pinned_certificate_finish (GAsyncResult *result, GError **error)
{
	g_return_val_if_fail (!error || !*error, FALSE);
	g_return_val_if_fail (g_task_is_valid (result, NULL), FALSE);

	return g_task_propagate_boolean (G_TASK (result), error);
}

/* -----------------------------------------------------------------------
 * REMOVE PINNED CERTIFICATE
 */

static GckAttributes *
prepare_remove_pinned_certificate (GcrCertificate *certificate, const gchar *purpose,
                                   const gchar *peer)
{
	GckBuilder builder = GCK_BUILDER_INIT;

	prepare_trust_attrs (certificate, CKT_X_PINNED_CERTIFICATE, &builder);
	gck_builder_add_string (&builder, CKA_X_PURPOSE, purpose);
	gck_builder_add_string (&builder, CKA_X_PEER, peer);

	return gck_builder_end (&builder);
}

static gboolean
perform_remove_pinned_certificate (GckAttributes *attrs,
                                   GCancellable *cancellable,
                                   GError **error)
{
	GList *objects, *l;
	GError *lerr = NULL;
	GckEnumerator *en;
	GList *slots;

	if (!gcr_pkcs11_initialize (cancellable, error))
		return FALSE;

	slots = gcr_pkcs11_get_trust_lookup_slots ();
	en = gck_slots_enumerate_objects (slots, attrs, CKF_RW_SESSION);
	g_clear_list (&slots, g_object_unref);

	/* We need an error below */
	if (error && !*error)
		*error = lerr;

	objects = gck_enumerator_next_n (en, -1, cancellable, error);
	g_object_unref (en);

	if (*error)
		return FALSE;

	for (l = objects; l; l = g_list_next (l)) {
		if (!gck_object_destroy (l->data, cancellable, error)) {

			/* In case there's a race condition */
			if (g_error_matches (*error, GCK_ERROR, CKR_OBJECT_HANDLE_INVALID)) {
				g_clear_error (error);
				continue;
			}

			g_clear_list (&objects, g_object_unref);
			return FALSE;
		}
	}

	g_clear_list (&objects, g_object_unref);
	return TRUE;
}

/**
 * gcr_trust_remove_pinned_certificate:
 * @certificate: a #GcrCertificate
 * @purpose: the purpose string
 * @peer: the peer for this pinned certificate
 * @cancellable: a #GCancellable
 * @error: a #GError, or %NULL
 *
 * Remove a pinned certificate for communication with @peer for @purpose.
 *
 * If the same pinned certificate does not exist, or was already removed,
 * then this operation succeeds without error.
 *
 * This call may block, see gcr_trust_remove_pinned_certificate_async() for the
 * non-blocking version.
 *
 * Returns: %TRUE if the pinned certificate no longer exists
 */
gboolean
gcr_trust_remove_pinned_certificate (GcrCertificate *certificate, const gchar *purpose, const gchar *peer,
                                     GCancellable *cancellable, GError **error)
{
	GckAttributes *search;
	gboolean ret;

	g_return_val_if_fail (GCR_IS_CERTIFICATE (certificate), FALSE);
	g_return_val_if_fail (purpose, FALSE);
	g_return_val_if_fail (peer, FALSE);

	search = prepare_remove_pinned_certificate (certificate, purpose, peer);
	g_return_val_if_fail (search, FALSE);

	ret = perform_remove_pinned_certificate (search, cancellable, error);
	gck_attributes_unref (search);

	return ret;
}

static void
thread_remove_pinned_certificate (GTask *task, gpointer object,
                                  gpointer task_data, GCancellable *cancellable)
{
	GckAttributes *attrs = task_data;
	GError *error = NULL;

	perform_remove_pinned_certificate (attrs, cancellable, &error);

	if (error == NULL)
		g_task_return_boolean (task, TRUE);
	else
		g_task_return_error (task, g_steal_pointer (&error));
}

/**
 * gcr_trust_remove_pinned_certificate_async:
 * @certificate: a #GcrCertificate
 * @purpose: the purpose string
 * @peer: the peer for this pinned certificate
 * @cancellable: a #GCancellable
 * @callback: a #GAsyncReadyCallback to call when the operation completes
 * @user_data: the data to pass to callback function
 *
 * Remove a pinned certificate for communication with @peer for @purpose.
 *
 * If the same pinned certificate does not exist, or was already removed,
 * then this operation succeeds without error.
 *
 * When the operation is finished, callback will be called. You can then call
 * gcr_trust_remove_pinned_certificate_finish() to get the result of the
 * operation.
 */
void
gcr_trust_remove_pinned_certificate_async (GcrCertificate *certificate,
                                           const gchar *purpose,
                                           const gchar *peer,
                                           GCancellable *cancellable,
                                           GAsyncReadyCallback callback,
                                           gpointer user_data)
{
	GTask *task;
	GckAttributes *attrs;

	g_return_if_fail (GCR_IS_CERTIFICATE (certificate));
	g_return_if_fail (purpose);
	g_return_if_fail (peer);

	task = g_task_new (NULL, cancellable, callback, user_data);
	g_task_set_source_tag (task, gcr_trust_remove_pinned_certificate_async);

	attrs = prepare_remove_pinned_certificate (certificate, purpose, peer);
	g_return_if_fail (attrs);
	g_task_set_task_data (task, attrs, gck_attributes_unref);

	g_task_run_in_thread (task, thread_remove_pinned_certificate);

	g_clear_object (&task);
}

/**
 * gcr_trust_remove_pinned_certificate_finish:
 * @result: the #GAsyncResult passed to the callback
 * @error: a #GError, or %NULL
 *
 * Finishes an asynchronous operation started by
 * gcr_trust_remove_pinned_certificate_async().
 *
 * Returns: %TRUE if the pinned certificate no longer exists
 */
gboolean
gcr_trust_remove_pinned_certificate_finish (GAsyncResult *result, GError **error)
{
	g_return_val_if_fail (!error || !*error, FALSE);
	g_return_val_if_fail (g_task_is_valid (result, NULL), FALSE);

	return g_task_propagate_boolean (G_TASK (result), error);
}

/* ----------------------------------------------------------------------------------
 * CERTIFICATE ROOT
 */

static GckAttributes *
prepare_is_certificate_anchored (GcrCertificate *certificate, const gchar *purpose)
{
	GckBuilder builder = GCK_BUILDER_INIT;

	prepare_trust_attrs (certificate, CKT_X_ANCHORED_CERTIFICATE, &builder);
	gck_builder_add_string (&builder, CKA_X_PURPOSE, purpose);

	return gck_builder_end (&builder);
}

static gboolean
perform_is_certificate_anchored (GckAttributes *attrs,
                                 GCancellable *cancellable,
                                 GError **error)
{
	GckEnumerator *en;
	GList *slots;
	GckObject *object;

	if (!gcr_pkcs11_initialize (cancellable, error))
		return FALSE;

	slots = gcr_pkcs11_get_trust_lookup_slots ();
	g_debug ("searching for certificate anchor in %d slots",
	         g_list_length (slots));
	en = gck_slots_enumerate_objects (slots, attrs, 0);
	g_clear_list (&slots, g_object_unref);

	object = gck_enumerator_next (en, cancellable, error);
	g_object_unref (en);

	if (object != NULL)
		g_object_unref (object);

	g_debug ("%s certificate anchor", object ? "found" : "did not find");
	return (object != NULL);
}

/**
 * gcr_trust_is_certificate_anchored:
 * @certificate: a #GcrCertificate to check
 * @purpose: the purpose string
 * @cancellable: a #GCancellable
 * @error: a #GError, or %NULL
 *
 * Check if the @certificate is a trust anchor for the given @purpose. A trust
 * anchor is used to verify the signatures on other certificates when verifying
 * a certificate chain. Also known as a trusted certificate authority.
 *
 * This call may block, see [func@Gcr.trust_is_certificate_anchored_async] for
 * the non-blocking version.
 *
 * In the case of an error, %FALSE is also returned. Check @error to detect
 * if an error occurred.
 *
 * Returns: %TRUE if the certificate is a trust anchor
 */
gboolean
gcr_trust_is_certificate_anchored (GcrCertificate *certificate, const gchar *purpose,
                                   GCancellable *cancellable, GError **error)
{
	GckAttributes *search;
	gboolean ret;

	g_return_val_if_fail (GCR_IS_CERTIFICATE (certificate), FALSE);
	g_return_val_if_fail (purpose, FALSE);

	search = prepare_is_certificate_anchored (certificate, purpose);
	g_return_val_if_fail (search, FALSE);

	ret = perform_is_certificate_anchored (search, cancellable, error);
	gck_attributes_unref (search);

	return ret;
}

static void
thread_is_certificate_anchored (GTask *task, gpointer object,
                                gpointer task_data, GCancellable *cancellable)
{
	GckAttributes *attrs = task_data;
	GError *error = NULL;
	gboolean found;

	found = perform_is_certificate_anchored (attrs, cancellable, &error);
	if (error == NULL)
		g_task_return_boolean (task, found);
	else
		g_task_return_error (task, g_steal_pointer (&error));
}

/**
 * gcr_trust_is_certificate_anchored_async:
 * @certificate: a #GcrCertificate to check
 * @purpose: the purpose string
 * @cancellable: a #GCancellable
 * @callback: a #GAsyncReadyCallback to call when the operation completes
 * @user_data: the data to pass to callback function
 *
 * Check if the @certificate is a trust anchor for the given @purpose. A trust
 * anchor is used to verify the signatures on other certificates when verifying
 * a certificate chain. Also known as a trusted certificate authority.
 *
 * When the operation is finished, callback will be called. You can then call
 * gcr_trust_is_certificate_anchored_finish() to get the result of the operation.
 */
void
gcr_trust_is_certificate_anchored_async (GcrCertificate *certificate, const gchar *purpose,
                                         GCancellable *cancellable, GAsyncReadyCallback callback,
                                         gpointer user_data)
{
	GTask *task;
	GckAttributes *attrs;

	g_return_if_fail (GCR_IS_CERTIFICATE (certificate));
	g_return_if_fail (purpose);

	task = g_task_new (NULL, cancellable, callback, user_data);
	g_task_set_source_tag (task, gcr_trust_is_certificate_anchored_async);

	attrs = prepare_is_certificate_anchored (certificate, purpose);
	g_return_if_fail (attrs);
	g_task_set_task_data (task, attrs, gck_attributes_unref);

	g_task_run_in_thread (task, thread_is_certificate_anchored);

	g_clear_object (&task);
}

/**
 * gcr_trust_is_certificate_anchored_finish:
 * @result: the #GAsyncResult passed to the callback
 * @error: a #GError, or %NULL
 *
 * Finishes an asynchronous operation started by
 * gcr_trust_is_certificate_anchored_async().
 *
 * In the case of an error, %FALSE is also returned. Check @error to detect
 * if an error occurred.
 *
 * Returns: %TRUE if the certificate is a trust anchor
 */
gboolean
gcr_trust_is_certificate_anchored_finish (GAsyncResult *result, GError **error)
{
	g_return_val_if_fail (!error || !*error, FALSE);
	g_return_val_if_fail (g_task_is_valid (result, NULL), FALSE);

	return g_task_propagate_boolean (G_TASK (result), error);
}

/* ----------------------------------------------------------------------------------
 * DISTRUSTED CERTIFICATE
 */

static GckAttributes *
prepare_is_certificate_distrusted (unsigned char *serial_nr,
                                   size_t         serial_nr_len,
                                   unsigned char *issuer,
                                   size_t         issuer_len)
{
    GckBuilder builder = GCK_BUILDER_INIT;

    gck_builder_add_ulong (&builder, CKA_CLASS, CKO_X_TRUST_ASSERTION);
    gck_builder_add_ulong (&builder, CKA_X_ASSERTION_TYPE, CKT_X_DISTRUSTED_CERTIFICATE);
    gck_builder_add_data (&builder, CKA_SERIAL_NUMBER, serial_nr, serial_nr_len);
    gck_builder_add_data (&builder, CKA_ISSUER, issuer, issuer_len);

    return gck_builder_end (&builder);
}

static gboolean
perform_is_certificate_distrusted (GckAttributes *attrs,
                                   GCancellable  *cancellable,
                                   GError       **error)
{
    GckEnumerator *en;
    GList *slots;
    GckObject *object;

    if (!gcr_pkcs11_initialize (cancellable, error))
        return FALSE;

    slots = gcr_pkcs11_get_trust_lookup_slots ();
    g_debug ("searching for certificate distrust assertion in %d slots",
             g_list_length (slots));
    en = gck_slots_enumerate_objects (slots, attrs, 0);
    g_clear_list (&slots, g_object_unref);

    object = gck_enumerator_next (en, cancellable, error);
    g_object_unref (en);

    if (object != NULL)
        g_object_unref (object);

    g_debug ("%s certificate distrust", object ? "found" : "did not find");
    return (object != NULL);
}

/**
 * gcr_trust_is_certificate_distrusted:
 * @serial_nr: (array length=serial_nr_len): The serial number of the certificate
 * @serial_nr_len: The nr of bytes in @serial_nr
 * @issuer: (array length=issuer_len): The raw issuer
 * @issuer_len: The nr of bytes in @issuer
 * @cancellable: (nullable): a #GCancellable or %NULL
 * @error: a #GError, or %NULL
 *
 * Checks whether the certificate that can be uniquely identified with the
 * given @serial_nr and @issuer is marked as distrusted (for example by the
 * user, or because it's part of a CRL).
 *
 * Since we can't directly use [iface@Certificate] to fetch these values, you
 * need to call these with the raw serial number and issuer as provided by the
 * PKCS#11 fields `CKA_SERIAL_NR` and `CKA_ISSUER`.
 *
 * Returns: %TRUE if the certificate is marked as distrusted
 */
gboolean
gcr_trust_is_certificate_distrusted (unsigned char *serial_nr,
                                     size_t         serial_nr_len,
                                     unsigned char *issuer,
                                     size_t         issuer_len,
                                     GCancellable  *cancellable,
                                     GError       **error)
{
    GckAttributes *search;
    gboolean ret;

    g_return_val_if_fail (serial_nr, FALSE);
    g_return_val_if_fail (serial_nr_len > 0, FALSE);
    g_return_val_if_fail (issuer, FALSE);
    g_return_val_if_fail (issuer_len > 0, FALSE);
    g_return_val_if_fail (G_IS_CANCELLABLE (cancellable) || !cancellable, FALSE);
    g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

    search = prepare_is_certificate_distrusted (serial_nr, serial_nr_len, issuer, issuer_len);
    g_return_val_if_fail (search, FALSE);

    ret = perform_is_certificate_distrusted (search, cancellable, error);
    gck_attributes_unref (search);

    return ret;
}

static void
thread_is_certificate_distrusted (GTask        *task,
                                  void         *object,
                                  void         *task_data,
                                  GCancellable *cancellable)
{
    GckAttributes *attrs = task_data;
    GError *error = NULL;
    gboolean found;

    found = perform_is_certificate_distrusted (attrs, cancellable, &error);
    if (error == NULL)
        g_task_return_boolean (task, found);
    else
        g_task_return_error (task, g_steal_pointer (&error));
}

/**
 * gcr_trust_is_certificate_distrusted_async:
 * @serial_nr: (array length=serial_nr_len): The serial number of the certificate
 * @serial_nr_len: The nr of bytes in @serial_nr
 * @issuer: (array length=issuer_len): The raw issuer
 * @issuer_len: The number of bytes in @issuer
 * @cancellable: (nullable): a #GCancellable or %NULL
 * @callback: a #GAsyncReadyCallback to call when the operation completes
 * @user_data: the data to pass to callback function
 *
 * Asynchronously checks whether the certificate that can be uniquely
 * identified with the given @serial_nr and @issuer is marked as distrusted
 * (for example by the user, or because it's part of a CRL).
 *
 * Since we can't directly use [iface@Certificate] to fetch these values, you
 * need to call these with the raw serial number and issuer as provided by the
 * PKCS#11 fields `CKA_SERIAL_NR` and `CKA_ISSUER`.
 *
 * When the operation is finished, @callback will be called. You can then call
 * [func@trust_is_certificate_distrusted_finish] to get the result of the
 * operation.
 */
void
gcr_trust_is_certificate_distrusted_async (unsigned char      *serial_nr,
                                           size_t              serial_nr_len,
                                           unsigned char      *issuer,
                                           size_t              issuer_len,
                                           GCancellable       *cancellable,
                                           GAsyncReadyCallback callback,
                                           void               *user_data)
{
    GTask *task;
    GckAttributes *attrs;

    g_return_if_fail (serial_nr);
    g_return_if_fail (serial_nr_len > 0);
    g_return_if_fail (issuer);
    g_return_if_fail (issuer_len > 0);
    g_return_if_fail (G_IS_CANCELLABLE (cancellable) || !cancellable);

    task = g_task_new (NULL, cancellable, callback, user_data);
    g_task_set_source_tag (task, gcr_trust_is_certificate_distrusted_async);

    attrs = prepare_is_certificate_distrusted (serial_nr, serial_nr_len, issuer, issuer_len);
    g_return_if_fail (attrs);
    g_task_set_task_data (task, attrs, gck_attributes_unref);

    g_task_run_in_thread (task, thread_is_certificate_distrusted);

    g_clear_object (&task);
}

/**
 * gcr_trust_is_certificate_distrusted_finish:
 * @result: the #GAsyncResult passed to the callback
 * @error: a #GError, or %NULL
 *
 * Finishes an asynchronous operation started by
 * [func@trust_is_certificate_distrusted_async].
 *
 * In the case of an error, %FALSE is also returned. Check @error to detect
 * if an error occurred.
 *
 * Returns: %TRUE if the certificate is a trust anchor
 */
gboolean
gcr_trust_is_certificate_distrusted_finish (GAsyncResult *result,
                                            GError      **error)
{
    g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
    g_return_val_if_fail (g_task_is_valid (result, NULL), FALSE);

    return g_task_propagate_boolean (G_TASK (result), error);
}