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
|
#include "config.h"
#include "gp11.h"
#include "gp11-private.h"
#include <string.h>
enum {
PROP_0,
PROP_MODULE,
PROP_HANDLE
};
G_DEFINE_TYPE (GP11Slot, gp11_slot, G_TYPE_OBJECT);
/* ----------------------------------------------------------------------------
* OBJECT
*/
static void
gp11_slot_init (GP11Slot *slot)
{
}
static void
gp11_slot_get_property (GObject *obj, guint prop_id, GValue *value,
GParamSpec *pspec)
{
GP11Slot *slot = GP11_SLOT (obj);
switch (prop_id) {
case PROP_MODULE:
g_value_set_object (value, slot->module);
break;
case PROP_HANDLE:
g_value_set_uint (value, slot->handle);
break;
}
}
static void
gp11_slot_set_property (GObject *obj, guint prop_id, const GValue *value,
GParamSpec *pspec)
{
GP11Slot *slot = GP11_SLOT (obj);
switch (prop_id) {
case PROP_MODULE:
g_return_if_fail (!slot->module);
slot->module = g_value_get_object (value);
g_return_if_fail (slot->module);
g_object_ref (slot->module);
break;
case PROP_HANDLE:
g_return_if_fail (!slot->handle);
slot->handle = g_value_get_uint (value);
break;
}
}
static void
gp11_slot_dispose (GObject *obj)
{
GP11Slot *slot = GP11_SLOT (obj);
if (slot->module)
g_object_unref (slot->module);
slot->module = NULL;
G_OBJECT_CLASS (gp11_slot_parent_class)->dispose (obj);
}
static void
gp11_slot_finalize (GObject *obj)
{
GP11Slot *slot = GP11_SLOT (obj);
g_assert (slot->module == NULL);
slot->handle = 0;
G_OBJECT_CLASS (gp11_slot_parent_class)->finalize (obj);
}
static void
gp11_slot_class_init (GP11SlotClass *klass)
{
GObjectClass *gobject_class = (GObjectClass*)klass;
gp11_slot_parent_class = g_type_class_peek_parent (klass);
gobject_class->get_property = gp11_slot_get_property;
gobject_class->set_property = gp11_slot_set_property;
gobject_class->dispose = gp11_slot_dispose;
gobject_class->finalize = gp11_slot_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_HANDLE,
g_param_spec_uint ("handle", "Handle", "PKCS11 Slot ID",
0, G_MAXUINT, 0, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
}
/* ----------------------------------------------------------------------------
* PUBLIC
*/
void
gp11_slot_info_free (GP11SlotInfo *slot_info)
{
if (!slot_info)
return;
g_free (slot_info->slot_description);
g_free (slot_info->manufacturer_id);
g_free (slot_info);
}
void
gp11_token_info_free (GP11TokenInfo *token_info)
{
if (!token_info)
return;
g_free (token_info->label);
g_free (token_info->manufacturer_id);
g_free (token_info->model);
g_free (token_info->serial_number);
g_free (token_info);
}
void
gp11_mechanism_info_free (GP11MechanismInfo *mech_info)
{
if (!mech_info)
return;
g_free (mech_info);
}
GP11SlotInfo*
gp11_slot_get_info (GP11Slot *slot)
{
GP11SlotInfo *slotinfo;
CK_SLOT_INFO info;
CK_RV rv;
g_return_val_if_fail (GP11_IS_SLOT (slot), NULL);
g_return_val_if_fail (GP11_IS_MODULE (slot->module), NULL);
g_return_val_if_fail (slot->module->funcs, NULL);
memset (&info, 0, sizeof (info));
rv = (slot->module->funcs->C_GetSlotInfo) (slot->handle, &info);
if (rv != CKR_OK) {
g_warning ("couldn't get slot info: %s", gp11_message_from_rv (rv));
return NULL;
}
slotinfo = g_new0 (GP11SlotInfo, 1);
slotinfo->slot_description = gp11_string_from_chars (info.slotDescription,
sizeof (info.slotDescription));
slotinfo->manufacturer_id = gp11_string_from_chars (info.manufacturerID,
sizeof (info.manufacturerID));
slotinfo->flags = info.flags;
slotinfo->hardware_version_major = info.hardwareVersion.major;
slotinfo->hardware_version_minor = info.hardwareVersion.minor;
slotinfo->firmware_version_major = info.firmwareVersion.major;
slotinfo->firmware_version_minor = info.firmwareVersion.minor;
return slotinfo;
}
GP11TokenInfo*
gp11_slot_get_token_info (GP11Slot *slot)
{
GP11TokenInfo *tokeninfo;
CK_TOKEN_INFO info;
gchar *string;
struct tm tm;
CK_RV rv;
g_return_val_if_fail (GP11_IS_SLOT (slot), NULL);
g_return_val_if_fail (GP11_IS_MODULE (slot->module), NULL);
g_return_val_if_fail (slot->module->funcs, NULL);
memset (&info, 0, sizeof (info));
rv = (slot->module->funcs->C_GetTokenInfo) (slot->handle, &info);
if (rv != CKR_OK) {
g_warning ("couldn't get slot info: %s", gp11_message_from_rv (rv));
return NULL;
}
tokeninfo = g_new0 (GP11TokenInfo, 1);
tokeninfo->label = gp11_string_from_chars (info.label, sizeof (info.label));
tokeninfo->model = gp11_string_from_chars (info.model, sizeof (info.model));
tokeninfo->manufacturer_id = gp11_string_from_chars (info.manufacturerID,
sizeof (info.manufacturerID));
tokeninfo->serial_number = gp11_string_from_chars (info.serialNumber,
sizeof (info.serialNumber));
tokeninfo->flags = info.flags;
tokeninfo->max_session_count = info.ulMaxSessionCount;
tokeninfo->session_count = info.ulSessionCount;
tokeninfo->max_rw_session_count = info.ulMaxRwSessionCount;
tokeninfo->rw_session_count = info.ulRwSessionCount;
tokeninfo->max_pin_len = info.ulMaxPinLen;
tokeninfo->min_pin_len = info.ulMinPinLen;
tokeninfo->total_public_memory = info.ulTotalPublicMemory;
tokeninfo->total_private_memory = info.ulTotalPrivateMemory;
tokeninfo->free_private_memory = info.ulFreePrivateMemory;
tokeninfo->free_public_memory = info.ulFreePublicMemory;
tokeninfo->hardware_version_major = info.hardwareVersion.major;
tokeninfo->hardware_version_minor = info.hardwareVersion.minor;
tokeninfo->firmware_version_major = info.firmwareVersion.major;
tokeninfo->firmware_version_minor = info.firmwareVersion.minor;
/* Parse the time into seconds since epoch */
if (info.flags & CKF_CLOCK_ON_TOKEN) {
string = g_strndup ((gchar*)info.utcTime, MIN (14, sizeof (info.utcTime)));
if (!strptime (string, "%Y%m%d%H%M%S", &tm))
tokeninfo->utc_time = -1;
else
tokeninfo->utc_time = mktime (&tm);
} else {
tokeninfo->utc_time = -1;
}
return tokeninfo;
}
GSList*
gp11_slot_get_mechanisms (GP11Slot *slot)
{
CK_MECHANISM_TYPE_PTR mech_list;
CK_ULONG count, i;
GSList *result;
CK_RV rv;
g_return_val_if_fail (GP11_IS_SLOT (slot), NULL);
g_return_val_if_fail (GP11_IS_MODULE (slot->module), NULL);
g_return_val_if_fail (slot->module->funcs, NULL);
rv = (slot->module->funcs->C_GetMechanismList) (slot->handle, NULL, &count);
if (rv != CKR_OK) {
g_warning ("couldn't get mechanism count: %s", gp11_message_from_rv (rv));
return NULL;
}
if (!count)
return NULL;
mech_list = g_new (CK_MECHANISM_TYPE, count);
rv = (slot->module->funcs->C_GetMechanismList) (slot->handle, mech_list, &count);
if (rv != CKR_OK) {
g_warning ("couldn't get mechanism list: %s", gp11_message_from_rv (rv));
g_free (mech_list);
return NULL;
}
result = NULL;
for (i = 0; i < count; ++i)
result = g_slist_prepend (result, GUINT_TO_POINTER (mech_list[i]));
g_free (mech_list);
return g_slist_reverse (result);
}
GP11MechanismInfo*
gp11_slot_get_mechanism_info (GP11Slot *slot, guint mech_type)
{
GP11MechanismInfo *mechinfo;
CK_MECHANISM_INFO info;
struct tm;
CK_RV rv;
g_return_val_if_fail (GP11_IS_SLOT (slot), NULL);
g_return_val_if_fail (GP11_IS_MODULE (slot->module), NULL);
g_return_val_if_fail (slot->module->funcs, NULL);
memset (&info, 0, sizeof (info));
rv = (slot->module->funcs->C_GetMechanismInfo) (slot->handle, mech_type, &info);
if (rv != CKR_OK) {
g_warning ("couldn't get mechanism info: %s", gp11_message_from_rv (rv));
return NULL;
}
mechinfo = g_new0 (GP11MechanismInfo, 1);
mechinfo->flags = info.flags;
mechinfo->max_key_size = info.ulMaxKeySize;
mechinfo->min_key_size = info.ulMinKeySize;
return mechinfo;
}
#if UNIMPLEMENTED
typedef struct InitToken {
GP11Arguments base;
const guchar *pin;
gsize length;
const gchar *label;
} InitToken;
static CK_RV
perform_init_token (InitToken *args)
{
return (args->base.pkcs11->C_InitToken) (args->base.handle,
args->pin, args->length,
args->label);
}
gboolean
gp11_slot_init_token (GP11Slot *slot, const guchar *pin, gsize length,
const gchar *label, GCancellable *cancellable,
GError **err)
{
InitToken args = { GP11_ARGUMENTS_INIT, pin, length, label };
return _gp11_call_sync (slot, perform_init_token, &args, err);
}
void
gp11_slot_init_token_async (GP11Slot *slot, const guchar *pin, gsize length,
const gchar *label, GCancellable *cancellable,
GAsyncReadyCallback callback, gpointer user_data)
{
InitToken* args = _gp11_call_async_prep (slot, perform_init_token,
sizeof (*args));
args->pin = pin;
args->length = length;
args->label = label;
_gp11_call_async_go (args, cancellable, callback, user_data);
}
gboolean
gp11_slot_init_token_finish (GP11Slot *slot, GAsyncResult *result, GError **err)
{
return _gp11_call_basic_finish (slot, result, err);
}
#endif /* UNIMPLEMENTED */
typedef struct OpenSession {
GP11Arguments base;
guint flags;
CK_SESSION_HANDLE session;
} OpenSession;
static CK_RV
perform_open_session (OpenSession *args)
{
return (args->base.pkcs11->C_OpenSession) (args->base.handle,
args->flags | CKF_SERIAL_SESSION,
NULL, NULL, &args->session);
}
GP11Session*
gp11_slot_open_session (GP11Slot *slot, guint flags, GError **err)
{
return gp11_slot_open_session_full (slot, flags, NULL, err);
}
GP11Session*
gp11_slot_open_session_full (GP11Slot *slot, guint flags, GCancellable *cancellable, GError **err)
{
OpenSession args = { GP11_ARGUMENTS_INIT, flags, 0 };
if (!_gp11_call_sync (slot, perform_open_session, &args, cancellable, err))
return FALSE;
return gp11_session_from_handle (slot, args.session);
}
void
gp11_slot_open_session_async (GP11Slot *slot, guint flags, GCancellable *cancellable,
GAsyncReadyCallback callback, gpointer user_data)
{
OpenSession *args = _gp11_call_async_prep (slot, perform_open_session,
sizeof (*args), NULL);
args->flags = flags;
args->session = 0;
_gp11_call_async_go (args, cancellable, callback, user_data);
}
GP11Session*
gp11_slot_open_session_finish (GP11Slot *slot, GAsyncResult *result, GError **err)
{
OpenSession *args;
if (!_gp11_call_basic_finish (slot, result, err))
return NULL;
args = _gp11_call_arguments (result, OpenSession);
return gp11_session_from_handle (slot, args->session);
}
#if UNIMPLEMENTED
static CK_RV
perform_close_all_sessions (GP11Arguments *args)
{
return (args->pkcs11->C_CloseAllSessions) (args->handle);
}
gboolean
gp11_slot_close_all_sessions (GP11Slot *slot, GError **err)
{
return gp11_slot_close_all_sessions_full (slot, NULL, err);
}
gboolean
gp11_slot_close_all_sessions_full (GP11Slot *slot, GCancellable *cancellable, GError **err)
{
GP11Arguments args = GP11_ARGUMENTS_INIT;
return _gp11_call_sync (slot, perform_close_all_sessions, &args, cancellable, err);
}
void
gp11_slot_close_all_sessions_async (GP11Slot *slot, GCancellable *cancellable,
GAsyncReadyCallback callback, gpointer user_data)
{
GP11Arguments *args = _gp11_call_async_prep (slot, perform_close_all_sessions, 0, NULL);
_gp11_call_async_go (args, cancellable, callback, user_data);
}
gboolean
gp11_slot_close_all_sessions_finish (GP11Slot *slot, GAsyncResult *result,
GError **err)
{
return _gp11_call_basic_finish (slot, result, err);
}
#endif
|