1257
/* ---------------------------------------------------------------------------------------
1261
typedef struct _set_template_args {
1263
CK_OBJECT_HANDLE object;
1264
CK_ATTRIBUTE_TYPE type;
1265
GP11Attributes *attrs;
1266
} set_template_args;
1269
perform_set_template (set_template_args *args)
1276
attr.type = args->type;
1277
attr.pValue = _gp11_attributes_commit_out (args->attrs, &n_attrs);
1278
attr.ulValueLen = n_attrs * sizeof (CK_ATTRIBUTE);
1280
return (args->base.pkcs11->C_SetAttributeValue) (args->base.handle, args->object, &attr, 1);
1284
free_set_template (set_template_args *args)
1287
gp11_attributes_unref (args->attrs);
1292
* gp11_object_set_template:
1293
* @self: The object to set an attribute template on.
1294
* @attr_type: The attribute template type.
1295
* @attrs: The attribute template.
1296
* @err: A location to store an error.
1298
* Set an attribute template on the object. The attr_type must be for
1299
* an attribute which contains a template.
1301
* This call may block for an indefinite period.
1303
* Return value: TRUE if the operation succeeded.
1306
gp11_object_set_template (GP11Object *self, gulong attr_type, GP11Attributes *attrs,
1309
g_return_val_if_fail (GP11_IS_OBJECT (self), FALSE);
1310
g_return_val_if_fail (!err || !*err, FALSE);
1311
return gp11_object_set_template_full (self, attr_type, attrs, NULL, err);
1315
* gp11_object_set_template_full:
1316
* @self: The object to set an attribute template on.
1317
* @attr_type: The attribute template type.
1318
* @attrs: The attribute template.
1319
* @cancellable: Optional cancellation object, or NULL.
1320
* @err: A location to store an error.
1322
* Set an attribute template on the object. The attr_type must be for
1323
* an attribute which contains a template.
1325
* This call may block for an indefinite period.
1327
* Return value: TRUE if the operation succeeded.
1330
gp11_object_set_template_full (GP11Object *self, gulong attr_type, GP11Attributes *attrs,
1331
GCancellable *cancellable, GError **err)
1333
GP11ObjectData *data = GP11_OBJECT_GET_DATA (self);
1334
set_template_args args;
1335
GP11Session *session;
1336
gboolean ret = FALSE;
1338
g_return_val_if_fail (GP11_IS_OBJECT (self), FALSE);
1339
g_return_val_if_fail (attrs, FALSE);
1340
g_return_val_if_fail (!err || !*err, FALSE);
1342
_gp11_attributes_lock (attrs);
1344
memset (&args, 0, sizeof (args));
1346
args.type = attr_type;
1347
args.object = data->handle;
1349
session = require_session_sync (self, CKF_RW_SESSION, err);
1351
ret = _gp11_call_sync (session, perform_set_template, NULL, &args, cancellable, err);
1353
_gp11_attributes_unlock (attrs);
1354
g_object_unref (session);
1359
* gp11_object_set_template_async:
1360
* @self: The object to set an attribute template on.
1361
* @attr_type: The attribute template type.
1362
* @attrs: The attribute template.
1363
* @cancellable: Optional cancellation object, or NULL.
1364
* @callback: Called when the operation completes.
1365
* @user_data: Data to be passed to the callback.
1367
* Set an attribute template on the object. The attr_type must be for
1368
* an attribute which contains a template.
1370
* This call will return immediately and complete asynchronously.
1373
gp11_object_set_template_async (GP11Object *self, gulong attr_type, GP11Attributes *attrs,
1374
GCancellable *cancellable, GAsyncReadyCallback callback,
1377
GP11ObjectData *data = GP11_OBJECT_GET_DATA (self);
1378
set_template_args *args;
1381
g_return_if_fail (GP11_IS_OBJECT (self));
1382
g_return_if_fail (attrs);
1384
args = _gp11_call_async_prep (data->slot, self, perform_set_template,
1385
NULL, sizeof (*args), free_set_template);
1387
_gp11_attributes_lock (attrs);
1388
args->attrs = gp11_attributes_ref (attrs);
1389
args->type = attr_type;
1390
args->object = data->handle;
1392
call = _gp11_call_async_ready (args, cancellable, callback, user_data);
1393
require_session_async (self, call, CKF_RW_SESSION, cancellable);
1397
* gp11_object_set_template_finish:
1398
* @self: The object to set an attribute template on.
1399
* @result: The result passed to the callback.
1400
* @err: A location to store an error.
1402
* Get the result of an operation to set attribute template on
1405
* Return value: TRUE if the operation succeeded.
1408
gp11_object_set_template_finish (GP11Object *self, GAsyncResult *result, GError **err)
1410
set_template_args *args;
1412
g_return_val_if_fail (GP11_IS_OBJECT (self), FALSE);
1413
g_return_val_if_fail (GP11_IS_CALL (result), FALSE);
1414
g_return_val_if_fail (!err || !*err, FALSE);
1416
/* Unlock the attributes we were using */
1417
args = _gp11_call_arguments (result, set_template_args);
1418
g_assert (args->attrs);
1419
_gp11_attributes_unlock (args->attrs);
1421
return _gp11_call_basic_finish (result, err);
1424
/* ---------------------------------------------------------------------------------------
1428
typedef struct _get_template_args {
1430
CK_OBJECT_HANDLE object;
1431
CK_ATTRIBUTE_TYPE type;
1432
GP11Attributes *attrs;
1433
} get_template_args;
1436
perform_get_template (get_template_args *args)
1439
CK_ULONG n_attrs, i;
1443
g_assert (!args->attrs);
1445
args->attrs = gp11_attributes_new ();
1446
attr.type = args->type;
1447
attr.ulValueLen = 0;
1450
/* Get the length of the entire template */
1451
rv = (args->base.pkcs11->C_GetAttributeValue) (args->base.handle, args->object, &attr, 1);
1455
/* Number of attributes, rounded down */
1456
n_attrs = (attr.ulValueLen / sizeof (CK_ATTRIBUTE));
1457
for (i = 0; i < n_attrs; ++i)
1458
gp11_attributes_add_empty (args->attrs, 0);
1460
/* Prepare all the attributes */
1461
_gp11_attributes_lock (args->attrs);
1462
attr.pValue = _gp11_attributes_prepare_in (args->attrs, &n_attrs);
1464
/* Get the size of each value */
1465
rv = (args->base.pkcs11->C_GetAttributeValue) (args->base.handle, args->object, &attr, 1);
1469
/* Allocate memory for each value */
1470
attr.pValue = _gp11_attributes_commit_in (args->attrs, &n_attrs);
1472
/* Now get the actual values */
1473
return (args->base.pkcs11->C_GetAttributeValue) (args->base.handle, args->object, &attr, 1);
1477
free_get_template (get_template_args *args)
1480
gp11_attributes_unref (args->attrs);
1485
* gp11_object_get_template:
1486
* @self: The object to get an attribute template from.
1487
* @attr_type: The attribute template type.
1488
* @err: A location to store an error.
1490
* Get an attribute template from the object. The attr_type must be for
1491
* an attribute which returns a template.
1493
* This call may block for an indefinite period.
1495
* Return value: The resulting PKCS#11 attribute template, or NULL if an error occurred.
1498
gp11_object_get_template (GP11Object *self, gulong attr_type, GError **err)
1500
g_return_val_if_fail (GP11_IS_OBJECT (self), NULL);
1501
g_return_val_if_fail (!err || !*err, NULL);
1503
return gp11_object_get_template_full (self, attr_type, NULL, err);
1507
* gp11_object_get_template_full:
1508
* @self: The object to get an attribute template from.
1509
* @attr_type: The template attribute type.
1510
* @cancellable: Optional cancellation object, or NULL.
1511
* @err: A location to store an error.
1513
* Get an attribute template from the object. The attr_type must be for
1514
* an attribute which returns a template.
1516
* This call may block for an indefinite period.
1518
* Return value: The resulting PKCS#11 attribute template, or NULL if an error occurred.
1521
gp11_object_get_template_full (GP11Object *self, gulong attr_type,
1522
GCancellable *cancellable, GError **err)
1524
GP11ObjectData *data = GP11_OBJECT_GET_DATA (self);
1525
get_template_args args;
1526
GP11Session *session;
1529
g_return_val_if_fail (GP11_IS_OBJECT (self), NULL);
1530
g_return_val_if_fail (!err || !*err, NULL);
1532
session = require_session_sync (self, 0, err);
1536
memset (&args, 0, sizeof (args));
1537
args.object = data->handle;
1538
args.type = attr_type;
1540
ret = _gp11_call_sync (session, perform_get_template, NULL, &args, cancellable, err);
1541
g_object_unref (session);
1543
_gp11_attributes_unlock (args.attrs);
1545
/* Free any value if failed */
1547
gp11_attributes_unref (args.attrs);
1555
* gp11_object_get_template_async:
1556
* @self: The object to get an attribute template from.
1557
* @attr_type: The template attribute type.
1558
* @cancellable: Optional cancellation object, or NULL.
1559
* @callback: Called when the operation completes.
1560
* @user_data: Data to be passed to the callback.
1562
* Get an attribute template from the object. The attr_type must be for
1563
* an attribute which returns a template.
1565
* This call will return immediately and complete asynchronously.
1568
gp11_object_get_template_async (GP11Object *self, gulong attr_type,
1569
GCancellable *cancellable, GAsyncReadyCallback callback,
1572
GP11ObjectData *data = GP11_OBJECT_GET_DATA (self);
1573
get_template_args *args;
1576
g_return_if_fail (GP11_IS_OBJECT (self));
1578
args = _gp11_call_async_prep (data->slot, self, perform_get_template,
1579
NULL, sizeof (*args), free_get_template);
1581
args->object = data->handle;
1582
args->type = attr_type;
1584
call = _gp11_call_async_ready (args, cancellable, callback, user_data);
1585
require_session_async (self, call, 0, cancellable);
1589
* gp11_object_get_template_finish:
1590
* @self: The object to get an attribute from.
1591
* @result: The result passed to the callback.
1592
* @err: A location to store an error.
1594
* Get the result of an operation to get attribute template from
1597
* Return value: The resulting PKCS#11 attribute template, or NULL if an error occurred.
1600
gp11_object_get_template_finish (GP11Object *self, GAsyncResult *result,
1603
get_template_args *args;
1605
g_return_val_if_fail (GP11_IS_OBJECT (self), NULL);
1606
g_return_val_if_fail (GP11_IS_CALL (result), NULL);
1607
g_return_val_if_fail (!err || !*err, NULL);
1609
if (!_gp11_call_basic_finish (result, err))
1612
args = _gp11_call_arguments (result, get_template_args);
1613
_gp11_attributes_unlock (args->attrs);
1614
return gp11_attributes_ref (args->attrs);