summaryrefslogtreecommitdiff
path: root/gp11/gp11-object.c
blob: fc2da1de7d9324a76b23e9773ffb46047f9c9777 (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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* gp11-object.c - the GObject PKCS#11 wrapper library

   Copyright (C) 2008, Stefan Walter

   The Gnome Keyring Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   The Gnome Keyring 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.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with the Gnome Library; see the file COPYING.LIB.  If not,
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.

   Author: Stef Walter <nielsen@memberwebs.com>
*/

#include "config.h"

#include "gp11.h"
#include "gp11-private.h"

#include <string.h>

/*
 * MT safe -- Nothing in GP11ObjectData changes between 
 * init and finalize. All GP11ObjectPrivate access between init
 * and finalize is locked.
 */

enum {
	PROP_0,
	PROP_MODULE,
	PROP_SLOT,
	PROP_HANDLE,
	PROP_SESSION
};

typedef struct _GP11ObjectData {
	GP11Module *module;
	GP11Slot *slot;
	CK_OBJECT_HANDLE handle;
} GP11ObjectData;

typedef struct _GP11ObjectPrivate {
	GP11ObjectData data;
	GStaticMutex mutex;
	GP11Session *session;
} GP11ObjectPrivate;

#define GP11_OBJECT_GET_DATA(o) \
      (G_TYPE_INSTANCE_GET_PRIVATE((o), GP11_TYPE_OBJECT, GP11ObjectData))

G_DEFINE_TYPE (GP11Object, gp11_object, G_TYPE_OBJECT);

/* ----------------------------------------------------------------------------
 * INTERNAL
 */

static void
run_call_with_session (GP11Call *call, GP11Session *session)
{
	g_assert (GP11_IS_CALL (call));
	g_assert (GP11_IS_SESSION (session));
	
	/* Hold onto this session for the length of the call */
	g_object_set_data_full (G_OBJECT (call), "call-opened-session", 
	                        g_object_ref (session), g_object_unref);

	_gp11_call_async_object (call, session);
	_gp11_call_async_go (call);	
}

static void
opened_session (GObject *obj, GAsyncResult *result, gpointer user_data)
{
	GP11Session *session;
	GError *err = NULL;
	GP11Call *call;
	
	g_assert (GP11_IS_CALL (user_data));
	call = GP11_CALL (user_data);
	
	session = gp11_slot_open_session_finish (GP11_SLOT (obj), result, &err);
	
	/* Transtfer the error to the outer call and finish */
	if (!session) {
		_gp11_call_async_short (user_data, err->code);
		g_error_free (err);
		return;
	}

	run_call_with_session (GP11_CALL (user_data), session);
	g_object_unref (session);
}

static void
require_session_async (GP11Object *self, GP11Call *call, 
                       gulong flags, GCancellable *cancellable)
{
	GP11ObjectData *data = GP11_OBJECT_GET_DATA (self);
	GP11Session *session;
	
	g_assert (GP11_IS_OBJECT (self));
	
	session = gp11_object_get_session (self);
	if (session) {
		run_call_with_session (call, session);
		g_object_unref (session);
	} else {
		gp11_slot_open_session_async (data->slot, flags, cancellable, opened_session, call);
	}
	
}

static GP11Session*
require_session_sync (GP11Object *self, gulong flags, GError **err)
{
	GP11ObjectData *data = GP11_OBJECT_GET_DATA (self);
	GP11Session *session;
	
	g_assert (GP11_IS_OBJECT (self));

	session = gp11_object_get_session (self);
	if (session)
		return session;
	
	return gp11_slot_open_session (data->slot, flags, err);
}

/* ----------------------------------------------------------------------------
 * OBJECT
 */

static void
gp11_object_init (GP11Object *self)
{
	GP11ObjectPrivate *pv = (G_TYPE_INSTANCE_GET_PRIVATE(self, GP11_TYPE_OBJECT, GP11ObjectPrivate));
	g_static_mutex_init (&pv->mutex);
}

static void
gp11_object_get_property (GObject *obj, guint prop_id, GValue *value, 
                          GParamSpec *pspec)
{
	GP11Object *self = GP11_OBJECT (obj);

	switch (prop_id) {
	case PROP_MODULE:
		g_value_take_object (value, gp11_object_get_module (self));
		break;
	case PROP_SLOT:
		g_value_take_object (value, gp11_object_get_slot (self));
		break;
	case PROP_SESSION:
		g_value_take_object (value, gp11_object_get_session (self));
		break;
	case PROP_HANDLE:
		g_value_set_ulong (value, gp11_object_get_handle (self));
		break;
	}
}

static void
gp11_object_set_property (GObject *obj, guint prop_id, const GValue *value, 
                          GParamSpec *pspec)
{
	GP11ObjectData *data = GP11_OBJECT_GET_DATA (obj);
	GP11Object *self = GP11_OBJECT (obj);
	
	/* The sets to data below are only allowed during construction */ 
	
	switch (prop_id) {
	case PROP_MODULE:
		g_return_if_fail (!data->module);
		data->module = g_value_get_object (value);
		g_return_if_fail (data->module);
		g_object_ref (data->module);
		break;
	case PROP_SLOT:
		g_return_if_fail (!data->slot);
		data->slot = g_value_get_object (value);
		g_return_if_fail (data->slot);
		g_object_ref (data->slot);
		break;
	case PROP_SESSION:
		gp11_object_set_session (self, g_value_get_object (value));
		break;
	case PROP_HANDLE:
		g_return_if_fail (!data->handle);
		data->handle = g_value_get_ulong (value);
		break;
	}
}

static void
gp11_object_finalize (GObject *obj)
{
	GP11ObjectPrivate *pv = (G_TYPE_INSTANCE_GET_PRIVATE(obj, GP11_TYPE_OBJECT, GP11ObjectPrivate));
	GP11ObjectData *data = GP11_OBJECT_GET_DATA (obj);

	if (data->slot)
		g_object_unref (data->slot);
	data->slot = NULL;
	
	if (data->module)
		g_object_unref (data->module);
	data->module = NULL;
	
	if (pv->session)
		g_object_unref (pv->session);
	pv->session = NULL;
	
	data->handle = 0;
	
	g_static_mutex_free (&pv->mutex);
	
	G_OBJECT_CLASS (gp11_object_parent_class)->finalize (obj);
}


static void
gp11_object_class_init (GP11ObjectClass *klass)
{
	GObjectClass *gobject_class = (GObjectClass*)klass;
	gp11_object_parent_class = g_type_class_peek_parent (klass);
	
	gobject_class->get_property = gp11_object_get_property;
	gobject_class->set_property = gp11_object_set_property;
	gobject_class->finalize = gp11_object_finalize;
	
	g_object_class_install_property (gobject_class, PROP_MODULE,
		g_param_spec_object ("module", "Module", "PKCS11 Module",
		                     GP11_TYPE_MODULE, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));

	g_object_class_install_property (gobject_class, PROP_SLOT,
		g_param_spec_object ("slot", "slot", "PKCS11 Slot",
		                     GP11_TYPE_SLOT, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));

	g_object_class_install_property (gobject_class, PROP_HANDLE,
		g_param_spec_ulong ("handle", "Object Handle", "PKCS11 Object Handle",
		                   0, G_MAXULONG, 0, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));

	g_object_class_install_property (gobject_class, PROP_SESSION,
		g_param_spec_object ("session", "session", "PKCS11 Session to make calls on",
		                     GP11_TYPE_SESSION, G_PARAM_READWRITE));
	
	g_type_class_add_private (klass, sizeof (GP11ObjectPrivate));
}

/* ----------------------------------------------------------------------------
 * PUBLIC 
 */

/**
 * gp11_object_from_handle:
 * @slot: The slot on which this object is present.
 * @handle: The raw handle of the object. 
 * 
 * Initialize a GP11Object from a raw PKCS#11 handle. Normally you would use 
 * gp11_session_create_object() or gp11_session_find_objects() to access objects. 
 * 
 * Return value: The new GP11Object. You should use g_object_unref() when done with this object.
 **/
GP11Object*
gp11_object_from_handle (GP11Slot *slot, CK_OBJECT_HANDLE handle)
{
	GP11Module *module = NULL;
	GP11Object *object;
	
	g_return_val_if_fail (GP11_IS_SLOT (slot), NULL);
	
	module = gp11_slot_get_module (slot);
	object = g_object_new (GP11_TYPE_OBJECT, "module", module, "handle", handle, "slot", slot, NULL);
	g_object_unref (module);
	
	return object;
}

/**
 * gp11_objects_from_handle_array:
 * @slot: The slot on which these objects are present.
 * @handles: The raw object handles.
 * @n_handles: The number of raw object handles.
 * 
 * Initialize a list of GP11Object from raw PKCS#11 handles. The handles argument must contain 
 * contiguous CK_OBJECT_HANDLE handles in an array.
 * 
 * Return value: The list of GP11Object. You should use gp11_list_unref_free() when done with 
 * this list. 
 **/
GList*
gp11_objects_from_handle_array (GP11Slot *slot, CK_OBJECT_HANDLE_PTR handles, CK_ULONG n_handles)
{
	GList *results = NULL;
	CK_ULONG i;
	
	g_return_val_if_fail (GP11_IS_SLOT (slot), NULL);
	g_return_val_if_fail (handles || !n_handles, NULL);
	
	for (i = 0; i < n_handles; ++i)
		results = g_list_prepend (results, gp11_object_from_handle (slot, handles[i]));
	return g_list_reverse (results);
}

/**
 * gp11_object_equal:
 * @object1: A pointer to the first GP11Object
 * @object2: A pointer to the second GP11Object
 * 
 * Checks equality of two objects. Two GP11Object objects can point to the same 
 * underlying PKCS#11 object.
 * 
 * Return value: TRUE if object1 and object2 are equal. FALSE if either is not a GP11Object.
 **/
gboolean
gp11_object_equal (gconstpointer object1, gconstpointer object2)
{
	GP11ObjectData *data1, *data2;

	if (object1 == object2)
		return TRUE;
	if (!GP11_IS_OBJECT (object1) || !GP11_IS_OBJECT (object2))
		return FALSE;
	
	data1 = GP11_OBJECT_GET_DATA (object1);
	data2 = GP11_OBJECT_GET_DATA (object2);
	
	return data1->handle == data2->handle && 
	       gp11_slot_equal (data1->slot, data2->slot);
}

/**
 * gp11_object_hash:
 * @object: A pointer to a GP11Object
 * 
 * Create a hash value for the GP11Object. 
 * 
 * This function is intended for easily hashing a GP11Object to add to 
 * a GHashTable or similar data structure.
 * 
 * Return value: An integer that can be used as a hash value, or 0 if invalid.
 **/
guint
gp11_object_hash (gconstpointer object)
{
	GP11ObjectData *data;
	
	g_return_val_if_fail (GP11_IS_OBJECT (object), 0);

	data = GP11_OBJECT_GET_DATA (object);
	
	return _gp11_ulong_hash (&data->handle) ^
	       gp11_slot_hash (data->slot);
}


/**
 * gp11_object_get_handle:
 * @self: The object.
 * 
 * Get the raw PKCS#11 handle of a GP11Object.
 * 
 * Return value: The raw object handle.
 **/
CK_OBJECT_HANDLE
gp11_object_get_handle (GP11Object *self)
{
	GP11ObjectData *data = GP11_OBJECT_GET_DATA (self);
	g_return_val_if_fail (GP11_IS_OBJECT (self), (CK_OBJECT_HANDLE)-1);
	return data->handle;
}

/**
 * gp11_object_get_module:
 * @self: The object.
 * 
 * Get the PKCS#11 module to which this object belongs.
 * 
 * Return value: The module, which should be unreffed after use.
 **/
GP11Module*
gp11_object_get_module (GP11Object *self)
{
	GP11ObjectData *data = GP11_OBJECT_GET_DATA (self);
	g_return_val_if_fail (GP11_IS_OBJECT (self), NULL);
	g_return_val_if_fail (GP11_IS_MODULE (data->module), NULL);
	return g_object_ref (data->module);
}

/**
 * gp11_object_get_slot:
 * @self: The object.
 * 
 * Get the PKCS#11 slot to which this object belongs.
 * 
 * Return value: The slot, which should be unreffed after use.
 **/
GP11Slot*
gp11_object_get_slot (GP11Object *self)
{
	GP11ObjectData *data = GP11_OBJECT_GET_DATA (self);
	g_return_val_if_fail (GP11_IS_OBJECT (self), NULL);
	g_return_val_if_fail (GP11_IS_SLOT (data->slot), NULL);
	return g_object_ref (data->slot);
}

/**
 * gp11_object_get_session:
 * @self: The object
 * 
 * Get the PKCS#11 session assigned to make calls on when operating
 * on this object.  
 * 
 * This will only return a session if it was set explitly on this 
 * object. By default an object will open and close sessions 
 * appropriate for its calls.
 * 
 * Return value: The assigned session, which must be unreffed after use.
 **/
GP11Session*
gp11_object_get_session (GP11Object *self)
{
	GP11ObjectPrivate *pv = (G_TYPE_INSTANCE_GET_PRIVATE (self, GP11_TYPE_OBJECT, GP11ObjectPrivate));
	GP11Session *session;
	
	g_return_val_if_fail (GP11_IS_OBJECT (self), NULL);
	
	g_static_mutex_lock (&pv->mutex);
	
	{
		session = pv->session ? g_object_ref (pv->session) : NULL;
	}
	
	g_static_mutex_unlock (&pv->mutex);
	
	return session;
}

/**
 * gp11_object_set_session:
 * @self: The object
 * @session: The assigned session
 * 
 * Set the PKCS#11 session assigned to make calls on when operating
 * on this object.  
 * 
 * It isn't always necessary to assign a session to an object. 
 * By default an object will open and close sessions appropriate for 
 * its calls.
 * 
 * If you assign a read-only session, then calls on this object
 * that modify the state of the object will probably fail.
 **/
void
gp11_object_set_session (GP11Object *self, GP11Session *session)
{
	GP11ObjectPrivate *pv = (G_TYPE_INSTANCE_GET_PRIVATE (self, GP11_TYPE_OBJECT, GP11ObjectPrivate));

	g_return_if_fail (GP11_IS_OBJECT (self));
	
	g_static_mutex_lock (&pv->mutex);
	
	{
		if (session)
			g_object_ref (session);
		if (pv->session)
			g_object_unref (pv->session);
		pv->session = session;
	}
	
	g_static_mutex_unlock (&pv->mutex);
}

/* --------------------------------------------------------------------------------------
 * DESTROY
 */

typedef struct _Destroy {
	GP11Arguments base;
	CK_OBJECT_HANDLE object;
} Destroy;

static CK_RV
perform_destroy (Destroy *args)
{
	g_assert (args);
	return (args->base.pkcs11->C_DestroyObject) (args->base.handle, args->object);
}

/**
 * gp11_object_destroy:
 * @self: The object to destroy.
 * @err: A location to return an error.
 * 
 * Destroy a PKCS#11 object, deleting it from storage or the session.
 * This call may block for an indefinite period.
 * 
 * Return value: Whether the call was successful or not.
 **/
gboolean
gp11_object_destroy (GP11Object *self, GError **err)
{
	g_return_val_if_fail (GP11_IS_OBJECT (self), FALSE);
	g_return_val_if_fail (!err || !*err, FALSE);
	return gp11_object_destroy_full (self, NULL, err);
}

/**
 * gp11_object_destroy_full:
 * @self: The object to destroy.
 * @cancellable: Optional cancellable object, or NULL to ignore. 
 * @err: A location to return an error.
 * 
 * Destroy a PKCS#11 object, deleting it from storage or the session.
 * This call may block for an indefinite period.
 * 
 * Return value: Whether the call was successful or not.
 **/
gboolean
gp11_object_destroy_full (GP11Object *self, GCancellable *cancellable, GError **err)
{
	GP11ObjectData *data = GP11_OBJECT_GET_DATA (self);
	Destroy args = { GP11_ARGUMENTS_INIT, 0 };
	GP11Session *session;
	gboolean ret = FALSE;
	
	g_return_val_if_fail (GP11_IS_OBJECT (self), FALSE);
	g_return_val_if_fail (GP11_IS_SLOT (data->slot), FALSE);
	g_return_val_if_fail (!err || !*err, FALSE);
	
	args.object = data->handle;

	session = require_session_sync (self, CKF_RW_SESSION, err);
	if (session)
		ret = _gp11_call_sync (session, perform_destroy, NULL, &args, cancellable, err);
	g_object_unref (session);
	return ret;
}

/**
 * gp11_object_destroy_async:
 * @self: The object to destroy.
 * @cancellable: Optional cancellable object, or NULL to ignore. 
 * @callback: Callback which is called when operation completes.
 * @user_data: Data to pass to the callback.
 * 
 * Destroy a PKCS#11 object, deleting it from storage or the session.
 * This call will return immediately and complete asynchronously.
 **/
void
gp11_object_destroy_async (GP11Object *self, GCancellable *cancellable,
                           GAsyncReadyCallback callback, gpointer user_data)
{
	GP11ObjectData *data = GP11_OBJECT_GET_DATA (self);
	Destroy* args;
	GP11Call *call;

	g_return_if_fail (GP11_IS_OBJECT (self));
	g_return_if_fail (GP11_IS_SLOT (data->slot));

	args = _gp11_call_async_prep (data->slot, self, perform_destroy, NULL, sizeof (*args), NULL);
	args->object = data->handle;
	
	call = _gp11_call_async_ready (args, cancellable, callback, user_data);
	require_session_async (self, call, CKF_RW_SESSION, cancellable);
}

/**
 * gp11_object_destroy_finish:
 * @self: The object being destroyed.
 * @result: The result of the destory operation passed to the callback.
 * @err: A location to store an error.
 * 
 * Get the status of the operation to destroy a PKCS#11 object, begun with 
 * gp11_object_destroy_async(). 
 * 
 * Return value: Whether the object was destroyed successfully or not.
 */
gboolean
gp11_object_destroy_finish (GP11Object *self, GAsyncResult *result, GError **err)
{
	g_return_val_if_fail (GP11_IS_OBJECT (self), FALSE);
	g_return_val_if_fail (GP11_IS_CALL (result), FALSE);
	return _gp11_call_basic_finish (result, err);
}

/* --------------------------------------------------------------------------------------
 * SET ATTRIBUTES
 */

typedef struct _SetAttributes {
	GP11Arguments base;
	GP11Attributes *attrs;
	CK_OBJECT_HANDLE object;
} SetAttributes;

static CK_RV
perform_set_attributes (SetAttributes *args)
{
	CK_ATTRIBUTE_PTR attrs;
	CK_ULONG n_attrs;
	
	g_assert (args);
	attrs = _gp11_attributes_commit_out (args->attrs, &n_attrs);
	
	return (args->base.pkcs11->C_SetAttributeValue) (args->base.handle, args->object, 
	                                                 attrs, n_attrs);
}

static void
free_set_attributes (SetAttributes *args)
{
	g_assert (args);
	gp11_attributes_unref (args->attrs);
	g_free (args);
}

/**
 * gp11_object_set:
 * @self: The object to set attributes on.
 * @err: A location to return an error.
 * ...: The attributes to set.
 *
 * Set PKCS#11 attributes on an object.
 * This call may block for an indefinite period.
 * 
 * The arguments must be triples of: attribute type, data type, value
 * 
 * <para>The variable argument list should contain:
 * 	<variablelist>
 *		<varlistentry>
 * 			<term>a)</term>
 * 			<listitem><para>The gulong attribute type (ie: CKA_LABEL). </para></listitem>
 * 		</varlistentry>
 * 		<varlistentry>
 * 			<term>b)</term>
 * 			<listitem><para>The attribute data type (one of GP11_BOOLEAN, GP11_ULONG, 
 * 				GP11_STRING, GP11_DATE) orthe raw attribute value length.</para></listitem>
 * 		</varlistentry>
 * 		<varlistentry>
 * 			<term>c)</term>
 * 			<listitem><para>The attribute value, either a gboolean, gulong, gchar*, GDate* or 
 * 				a pointer to a raw attribute value.</para></listitem>
 * 		</varlistentry>
 * 	</variablelist>
 * The variable argument list should be terminated with GP11_INVALID.</para> 
 * 
 * Return value: Whether the call was successful or not.
 **/
gboolean
gp11_object_set (GP11Object *self, GError **err, ...)
{
	GP11Attributes *attrs;
	va_list va;
	CK_RV rv;
	
	g_return_val_if_fail (GP11_IS_OBJECT (self), FALSE);
	g_return_val_if_fail (!err || !*err, FALSE);
	
	va_start (va, err);
	attrs = gp11_attributes_new_valist (g_realloc, va);
	va_end (va);
	
	rv = gp11_object_set_full (self, attrs, NULL, err);
	
	gp11_attributes_unref (attrs);
	return rv;
}

/**
 * gp11_object_set_full:
 * @self: The object to set attributes on.
 * @attrs: The attributes to set on the object.
 * @cancellable: Optional cancellable object, or NULL to ignore. 
 * @err: A location to return an error.
 * 
 * Set PKCS#11 attributes on an object. This call may block for an indefinite period.
 * 
 * Return value: Whether the call was successful or not.
 **/
gboolean
gp11_object_set_full (GP11Object *self, GP11Attributes *attrs,
                      GCancellable *cancellable, GError **err)
{
	GP11ObjectData *data = GP11_OBJECT_GET_DATA (self);
	SetAttributes args;
	GP11Session *session;
	gboolean ret = FALSE;
	
	g_return_val_if_fail (GP11_IS_OBJECT (self), FALSE);
	g_return_val_if_fail (attrs, FALSE);
	g_return_val_if_fail (!err || !*err, FALSE);
	
	_gp11_attributes_lock (attrs);
	
	memset (&args, 0, sizeof (args));
	args.attrs = attrs;
	args.object = data->handle;

	session = require_session_sync (self, CKF_RW_SESSION, err);
	if (session)
		ret = _gp11_call_sync (session, perform_set_attributes, NULL, &args, cancellable, err);
	
	_gp11_attributes_unlock (attrs);
	g_object_unref (session);
	return ret;
}

/**
 * gp11_object_set_async:
 * @self: The object to set attributes on.
 * @attrs: The attributes to set on the object.
 * @cancellable: Optional cancellable object, or NULL to ignore. 
 * @callback: Callback which is called when operation completes.
 * @user_data: Data to pass to the callback.
 * 
 * Set PKCS#11 attributes on an object. This call will return 
 * immediately and completes asynchronously.
 **/
void
gp11_object_set_async (GP11Object *self, GP11Attributes *attrs, GCancellable *cancellable,
                       GAsyncReadyCallback callback, gpointer user_data)
{
	GP11ObjectData *data = GP11_OBJECT_GET_DATA (self);
	SetAttributes *args;
	GP11Call *call;
	
	g_return_if_fail (GP11_IS_OBJECT (self));
	g_return_if_fail (attrs);

	args = _gp11_call_async_prep (data->slot, self, perform_set_attributes, 
	                              NULL, sizeof (*args), free_set_attributes);
	
	_gp11_attributes_lock (attrs);
	args->attrs = gp11_attributes_ref (attrs);
	args->object = data->handle;
	
	call = _gp11_call_async_ready (args, cancellable, callback, user_data);
	require_session_async (self, call, CKF_RW_SESSION, cancellable);
}

/**
 * gp11_object_set_finish:
 * @self: The object to set attributes on.
 * @result: The result of the destory operation passed to the callback.
 * @err: A location to store an error.
 * 
 * Get the status of the operation to set attributes on a PKCS#11 object, 
 * begun with gp11_object_set_async(). 
 * 
 * Return value: Whether the attributes were successfully set on the object or not.
 */
gboolean
gp11_object_set_finish (GP11Object *self, GAsyncResult *result, GError **err)
{
	SetAttributes *args;
	
	g_return_val_if_fail (GP11_IS_OBJECT (self), FALSE);
	g_return_val_if_fail (GP11_IS_CALL (result), FALSE);
	g_return_val_if_fail (!err || !*err, FALSE);
	
	/* Unlock the attributes we were using */
	args = _gp11_call_arguments (result, SetAttributes);
	g_assert (args->attrs);
	_gp11_attributes_unlock (args->attrs);

	return _gp11_call_basic_finish (result, err);
}

/* ------------------------------------------------------------------------------------
 * GET ATTRIBUTES
 */

typedef struct _GetAttributes {
	GP11Arguments base;
	CK_OBJECT_HANDLE object;
	GP11Attributes *attrs;
} GetAttributes;

/* 
 * Certain failure return values only apply to individual attributes
 * being retrieved. These are ignored, since the attribute should 
 * already have -1 set as the length.
 */
static gboolean
is_ok_get_attributes_rv (CK_RV rv) 
{
	switch (rv) {
	case CKR_OK:
	case CKR_ATTRIBUTE_SENSITIVE:
	case CKR_ATTRIBUTE_TYPE_INVALID:
		return TRUE;
	default:
		return FALSE;
	}
}

static CK_RV
perform_get_attributes (GetAttributes *args)
{
	CK_ATTRIBUTE_PTR attrs;
	CK_ULONG n_attrs;
	CK_RV rv;
	
	g_assert (args);
	g_assert (args->attrs);
	
	/* Prepare all the attributes */
	attrs = _gp11_attributes_prepare_in (args->attrs, &n_attrs);

	/* Get the size of each value */
	rv = (args->base.pkcs11->C_GetAttributeValue) (args->base.handle, args->object,
	                                               attrs, n_attrs);
	if (!is_ok_get_attributes_rv (rv)) {
		g_free (attrs);
		return rv;
	}
	
	/* Allocate memory for each value */
	attrs = _gp11_attributes_commit_in (args->attrs, &n_attrs);
	
	/* Now get the actual values */
	rv = (args->base.pkcs11->C_GetAttributeValue) (args->base.handle, args->object,
	                                               attrs, n_attrs);
	
	if (is_ok_get_attributes_rv (rv))
		rv = CKR_OK;
	
	return rv;
}

static void
free_get_attributes (GetAttributes *args)
{
	g_assert (args);
	g_assert (args->attrs);
	gp11_attributes_unref (args->attrs);
	g_free (args);
}


/**
 * gp11_object_get:
 * @self: The object to get attributes from.
 * @err: A location to store an error.
 * ...: The attribute types to get.
 * 
 * Get the specified attributes from the object. This call may
 * block for an indefinite period.
 * 
 * Return value: The resulting PKCS#11 attributes, or NULL if an error occurred. 
 * The result must be unreffed when you're finished with it.
 **/
GP11Attributes*
gp11_object_get (GP11Object *self, GError **err, ...)
{
	GP11Attributes *attrs;
	va_list va;
	gulong type;
	
	g_return_val_if_fail (GP11_IS_OBJECT (self), NULL);
	g_return_val_if_fail (!err || !*err, NULL);

	attrs = gp11_attributes_new ();
	va_start (va, err);
	for (;;) {
		type = va_arg (va, gulong);
		if (type == GP11_INVALID)
			break;
		gp11_attributes_add_invalid (attrs, type);
	}
	va_end (va);
	
	if (!gp11_object_get_full (self, attrs, NULL, err)) {
		gp11_attributes_unref (attrs);
		return NULL;
	}

	return attrs;
}

/**
 * gp11_object_get:
 * @self: The object to get attributes from.
 * @attrs: The attributes to get, with the types filled in.
 * @cancellable: Optional cancellation object, or NULL.
 * @err: A location to store an error.
 * 
 * Get the specified attributes from the object. This call may
 * block for an indefinite period.
 * 
 * No extra references are added to the returned attributes pointer. 
 * During this call you may not access the attributes in any way. 
 * 
 * Return value: A pointer to the filled in attributes if successful, 
 * or NULL if not. 
 **/
GP11Attributes*
gp11_object_get_full (GP11Object *self, GP11Attributes *attrs,
                      GCancellable *cancellable, GError **err)
{
	GP11ObjectData *data = GP11_OBJECT_GET_DATA (self);
	GetAttributes args;
	GP11Session *session;
	gboolean ret;
	
	g_return_val_if_fail (GP11_IS_OBJECT (self), NULL);
	g_return_val_if_fail (attrs, NULL);
	g_return_val_if_fail (!err || !*err, NULL);
	
	session = require_session_sync (self, 0, err);
	if (!session)
		return NULL;
	
	_gp11_attributes_lock (attrs);
	
	memset (&args, 0, sizeof (args));
	args.attrs = attrs;
	args.object = data->handle;

	ret = _gp11_call_sync (session, perform_get_attributes, NULL, &args, cancellable, err);
	_gp11_attributes_unlock (attrs);
	g_object_unref (session);
	
	return ret ? attrs : NULL;
}

/**
 * gp11_object_get_async:
 * @self: The object to get attributes from.
 * @attrs: The attributes to get, initialized with their types.
 * @cancellable: Optional cancellation object, or NULL.
 * @callback: A callback which is called when the operation completes.
 * @user_data: Data to be passed to the callback.
 * 
 * Get the specified attributes from the object. The attributes will be cleared
 * of their current values, and new attributes will be stored. The attributes
 * should not be accessed in any way except for referencing and unreferencing 
 * them until gp11_object_get_finish() is called.
 * 
 * This call returns immediately and completes asynchronously.
 **/
void
gp11_object_get_async (GP11Object *self, GP11Attributes *attrs, GCancellable *cancellable, 
                       GAsyncReadyCallback callback, gpointer user_data)
{
	GP11ObjectData *data = GP11_OBJECT_GET_DATA (self);
	GetAttributes *args;
	GP11Call *call;
	
	g_return_if_fail (GP11_IS_OBJECT (self));
	g_return_if_fail (attrs);

	args = _gp11_call_async_prep (data->slot, self, perform_get_attributes, 
	                              NULL, sizeof (*args), free_get_attributes);
	
	_gp11_attributes_lock (attrs);
	args->attrs = gp11_attributes_ref (attrs);
	args->object = data->handle;
	
	call = _gp11_call_async_ready (args, cancellable, callback, user_data);
	require_session_async (self, call, 0, cancellable);
}

/**
 * gp11_object_get_finish:
 * @self: The object to get attributes from.
 * @result: The result passed to the callback.
 * @err: A location to store an error.
 * 
 * Get the result of a get operation and return specified attributes from 
 * the object. 
 * 
 * No extra references are added to the returned attributes pointer. 
 * 
 * Return value: The filled in attributes structure if successful or 
 * NULL if not successful.
 **/
GP11Attributes*
gp11_object_get_finish (GP11Object *self, GAsyncResult *result, GError **err)
{
	GetAttributes *args;
	
	g_return_val_if_fail (GP11_IS_OBJECT (self), NULL);
	g_return_val_if_fail (GP11_IS_CALL (result), NULL);
	g_return_val_if_fail (!err || !*err, NULL);

	args = _gp11_call_arguments (result, GetAttributes);
	_gp11_attributes_unlock (args->attrs);

	if (!_gp11_call_basic_finish (result, err))
		return NULL;
	
	return args->attrs;
}

/* ---------------------------------------------------------------------------------
 * GET ATTRIBUTE DATA
 */

typedef struct _GetAttributeData {
	GP11Arguments base;
	CK_OBJECT_HANDLE object;
	CK_ATTRIBUTE_TYPE type;
	GP11Allocator allocator;
	guchar *result;
	gsize n_result;
} GetAttributeData;

static CK_RV
perform_get_attribute_data (GetAttributeData *args)
{
	CK_ATTRIBUTE attr;
	CK_RV rv;
	
	g_assert (args);
	g_assert (args->allocator);
	
	attr.type = args->type;
	attr.ulValueLen = 0;
	attr.pValue = 0;
	
	/* Get the size of the value */
	rv = (args->base.pkcs11->C_GetAttributeValue) (args->base.handle, args->object,
	                                               &attr, 1);
	if (rv != CKR_OK) 
		return rv;
	
	/* Allocate memory for the value */
	args->result = (args->allocator) (NULL, attr.ulValueLen);
	g_assert (args->result);
	attr.pValue = args->result;
	
	/* Now get the actual value */
	rv = (args->base.pkcs11->C_GetAttributeValue) (args->base.handle, args->object,
	                                               &attr, 1);

	if (rv == CKR_OK)
		args->n_result = attr.ulValueLen;
	
	return rv;
}

static void
free_get_attribute_data (GetAttributeData *args)
{
	g_assert (args);
	g_free (args->result);
	g_free (args);
}

/**
 * gp11_object_get_data:
 * @self: The object to get attribute data from.
 * @attr_type: The attribute to get data for.
 * @n_data: The length of the resulting data.
 * @err: A location to store an error.
 * 
 * Get the data for the specified attribute from the object. This call 
 * may block for an indefinite period.
 * 
 * Return value: The resulting PKCS#11 attribute data, or NULL if an error occurred. 
 **/
gpointer
gp11_object_get_data (GP11Object *self, gulong attr_type, gsize *n_data, GError **err)
{
	g_return_val_if_fail (GP11_IS_OBJECT (self), NULL);
	g_return_val_if_fail (n_data, NULL);
	g_return_val_if_fail (!err || !*err, NULL);
	
	return gp11_object_get_data_full (self, attr_type, g_realloc, NULL, n_data, err);
}

/**
 * gp11_object_get_data_full:
 * @self: The object to get attribute data from.
 * @attr_type: The attribute to get data for.
 * @allocator: An allocator with which to allocate memory for the data, or NULL for default.
 * @cancellable: Optional cancellation object, or NULL.
 * @n_data: The length of the resulting data.
 * @err: A location to store an error.
 * 
 * Get the data for the specified attribute from the object. This call 
 * may block for an indefinite period.
 * 
 * Return value: The resulting PKCS#11 attribute data, or NULL if an error occurred. 
 **/
gpointer
gp11_object_get_data_full (GP11Object *self, gulong attr_type, GP11Allocator allocator,
                           GCancellable *cancellable, gsize *n_data, GError **err)
{
	GP11ObjectData *data = GP11_OBJECT_GET_DATA (self);
	GetAttributeData args;
	GP11Session *session;
	gboolean ret;
	
	g_return_val_if_fail (GP11_IS_OBJECT (self), NULL);
	g_return_val_if_fail (n_data, NULL);
	g_return_val_if_fail (!err || !*err, NULL);
	
	if (!allocator)
		allocator = g_realloc;
	
	session = require_session_sync (self, 0, err);
	if (!session)
		return NULL;
	
	memset (&args, 0, sizeof (args));
	args.allocator = allocator;
	args.object = data->handle;
	args.type = attr_type;

	ret = _gp11_call_sync (session, perform_get_attribute_data, NULL, &args, cancellable, err);
	g_object_unref (session);
	
	/* Free any value if failed */
	if (!ret) {
		if (args.result)
			(allocator) (args.result, 0);
		return NULL;
	}
	
	*n_data = args.n_result;
	return args.result;
}

/**
 * gp11_object_get_data_async:
 * @self: The object to get attribute data from.
 * @attr_type: The attribute to get data for.
 * @allocator: An allocator with which to allocate memory for the data, or NULL for default.
 * @cancellable: Optional cancellation object, or NULL.
 * @callback: Called when the operation completes.
 * @user_data: Data to be passed to the callback.
 * 
 * Get the data for the specified attribute from the object. This call will
 * return immediately and complete asynchronously.
 **/
void
gp11_object_get_data_async (GP11Object *self, gulong attr_type, GP11Allocator allocator, 
                            GCancellable *cancellable, GAsyncReadyCallback callback, 
                            gpointer user_data)
{
	GP11ObjectData *data = GP11_OBJECT_GET_DATA (self);
	GetAttributeData *args;
	GP11Call *call;
	
	g_return_if_fail (GP11_IS_OBJECT (self));
	
	if (!allocator)
		allocator = g_realloc;

	args = _gp11_call_async_prep (data->slot, self, perform_get_attribute_data, 
	                              NULL, sizeof (*args), free_get_attribute_data);

	args->allocator = allocator;
	args->object = data->handle;
	args->type = attr_type;
	
	call = _gp11_call_async_ready (args, cancellable, callback, user_data);
	require_session_async (self, call, 0, cancellable);
}

/**
 * gp11_object_get_data_finish:
 * @self: The object to get an attribute from.
 * @result: The result passed to the callback.
 * @n_data: The length of the resulting data.
 * @err: A location to store an error.
 *
 * Get the result of an operation to get attribute data from 
 * an object. 
 * 
 * Return value: The PKCS#11 attribute data or NULL if an error occurred.
 **/
gpointer
gp11_object_get_data_finish (GP11Object *self, GAsyncResult *result, 
                             gsize *n_data, GError **err)
{
	GetAttributeData *args;
	guchar *data;
	
	g_return_val_if_fail (GP11_IS_OBJECT (self), NULL);
	g_return_val_if_fail (GP11_IS_CALL (result), NULL);
	g_return_val_if_fail (n_data, NULL);
	g_return_val_if_fail (!err || !*err, NULL);
	
	if (!_gp11_call_basic_finish (result, err))
		return NULL;

	args = _gp11_call_arguments (result, GetAttributeData);

	*n_data = args->n_result;
	data = args->result;
	args->result = NULL;
	
	return data;
}