~ubuntu-branches/ubuntu/natty/gnome-keyring/natty

« back to all changes in this revision

Viewing changes to gp11/gp11-object.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2010-02-16 19:00:06 UTC
  • mfrom: (1.1.58 upstream)
  • Revision ID: james.westby@ubuntu.com-20100216190006-cqpnic4zxlkmmi0o
Tags: 2.29.90git20100218-0ubuntu1
Updated to a git snapshot version

Show diffs side-by-side

added added

removed removed

Lines of Context:
129
129
                run_call_with_session (call, session);
130
130
                g_object_unref (session);
131
131
        } else {
132
 
                gp11_slot_open_session_async (data->slot, flags, cancellable, opened_session, call);
 
132
                gp11_slot_open_session_async (data->slot, flags, NULL, NULL,
 
133
                                              cancellable, opened_session, call);
133
134
        }
134
135
        
135
136
}
668
669
 * gp11_object_set:
669
670
 * @self: The object to set attributes on.
670
671
 * @err: A location to return an error.
671
 
 * ...: The attributes to set.
 
672
 * @...: The attributes to set.
672
673
 *
673
674
 * Set PKCS#11 attributes on an object.
674
675
 * This call may block for an indefinite period.
860
861
        /* Get the size of each value */
861
862
        rv = (args->base.pkcs11->C_GetAttributeValue) (args->base.handle, args->object,
862
863
                                                       attrs, n_attrs);
863
 
        if (!is_ok_get_attributes_rv (rv)) {
864
 
                g_free (attrs);
 
864
        if (!is_ok_get_attributes_rv (rv))
865
865
                return rv;
866
 
        }
867
 
        
 
866
 
868
867
        /* Allocate memory for each value */
869
868
        attrs = _gp11_attributes_commit_in (args->attrs, &n_attrs);
870
869
        
892
891
 * gp11_object_get:
893
892
 * @self: The object to get attributes from.
894
893
 * @err: A location to store an error.
895
 
 * ...: The attribute types to get.
896
 
 * 
 
894
 * @...: The attribute types to get.
 
895
 *
897
896
 * Get the specified attributes from the object. This call may
898
897
 * block for an indefinite period.
899
898
 * 
929
928
}
930
929
 
931
930
/**
932
 
 * gp11_object_get:
 
931
 * gp11_object_get_full:
933
932
 * @self: The object to get attributes from.
934
933
 * @attrs: The attributes to get, with the types filled in.
935
934
 * @cancellable: Optional cancellation object, or NULL.
1076
1075
                return rv;
1077
1076
        
1078
1077
        /* Allocate memory for the value */
1079
 
        args->result = (args->allocator) (NULL, attr.ulValueLen);
 
1078
        args->result = (args->allocator) (NULL, attr.ulValueLen + 1);
1080
1079
        g_assert (args->result);
1081
1080
        attr.pValue = args->result;
1082
 
        
 
1081
 
1083
1082
        /* Now get the actual value */
1084
1083
        rv = (args->base.pkcs11->C_GetAttributeValue) (args->base.handle, args->object,
1085
1084
                                                       &attr, 1);
1086
1085
 
1087
 
        if (rv == CKR_OK)
 
1086
        if (rv == CKR_OK) {
1088
1087
                args->n_result = attr.ulValueLen;
1089
 
        
 
1088
                args->result[args->n_result] = 0;
 
1089
        }
 
1090
 
1090
1091
        return rv;
1091
1092
}
1092
1093
 
1105
1106
 * @n_data: The length of the resulting data.
1106
1107
 * @err: A location to store an error.
1107
1108
 * 
1108
 
 * Get the data for the specified attribute from the object. This call 
1109
 
 * may block for an indefinite period.
 
1109
 * Get the data for the specified attribute from the object. For convenience
 
1110
 * the returned data has a null terminator.
 
1111
 *
 
1112
 * This call may block for an indefinite period.
1110
1113
 * 
1111
1114
 * Return value: The resulting PKCS#11 attribute data, or NULL if an error occurred. 
1112
1115
 **/
1129
1132
 * @n_data: The length of the resulting data.
1130
1133
 * @err: A location to store an error.
1131
1134
 * 
1132
 
 * Get the data for the specified attribute from the object. This call 
1133
 
 * may block for an indefinite period.
 
1135
 * Get the data for the specified attribute from the object. For convenience
 
1136
 * the returned data has an extra null terminator, not included in the returned length.
 
1137
 *
 
1138
 * This call may block for an indefinite period.
1134
1139
 * 
1135
1140
 * Return value: The resulting PKCS#11 attribute data, or NULL if an error occurred. 
1136
1141
 **/
1182
1187
 * @callback: Called when the operation completes.
1183
1188
 * @user_data: Data to be passed to the callback.
1184
1189
 * 
1185
 
 * Get the data for the specified attribute from the object. This call will
1186
 
 * return immediately and complete asynchronously.
 
1190
 * Get the data for the specified attribute from the object.
 
1191
 *
 
1192
 * This call will return immediately and complete asynchronously.
1187
1193
 **/
1188
1194
void
1189
1195
gp11_object_get_data_async (GP11Object *self, gulong attr_type, GP11Allocator allocator, 
1217
1223
 * @n_data: The length of the resulting data.
1218
1224
 * @err: A location to store an error.
1219
1225
 *
1220
 
 * Get the result of an operation to get attribute data from 
1221
 
 * an object. 
 
1226
 * Get the result of an operation to get attribute data from
 
1227
 * an object. For convenience the returned data has an extra null terminator,
 
1228
 * not included in the returned length.
 
1229
 *
1222
1230
 * 
1223
1231
 * Return value: The PKCS#11 attribute data or NULL if an error occurred.
1224
1232
 **/
1245
1253
        
1246
1254
        return data;
1247
1255
}
 
1256
 
 
1257
/* ---------------------------------------------------------------------------------------
 
1258
 * SET TEMPLATE
 
1259
 */
 
1260
 
 
1261
typedef struct _set_template_args {
 
1262
        GP11Arguments base;
 
1263
        CK_OBJECT_HANDLE object;
 
1264
        CK_ATTRIBUTE_TYPE type;
 
1265
        GP11Attributes *attrs;
 
1266
} set_template_args;
 
1267
 
 
1268
static CK_RV
 
1269
perform_set_template (set_template_args *args)
 
1270
{
 
1271
        CK_ATTRIBUTE attr;
 
1272
        CK_ULONG n_attrs;
 
1273
 
 
1274
        g_assert (args);
 
1275
 
 
1276
        attr.type = args->type;
 
1277
        attr.pValue = _gp11_attributes_commit_out (args->attrs, &n_attrs);
 
1278
        attr.ulValueLen = n_attrs * sizeof (CK_ATTRIBUTE);
 
1279
 
 
1280
        return (args->base.pkcs11->C_SetAttributeValue) (args->base.handle, args->object, &attr, 1);
 
1281
}
 
1282
 
 
1283
static void
 
1284
free_set_template (set_template_args *args)
 
1285
{
 
1286
        g_assert (args);
 
1287
        gp11_attributes_unref (args->attrs);
 
1288
        g_free (args);
 
1289
}
 
1290
 
 
1291
/**
 
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.
 
1297
 *
 
1298
 * Set an attribute template on the object. The attr_type must be for
 
1299
 * an attribute which contains a template.
 
1300
 *
 
1301
 * This call may block for an indefinite period.
 
1302
 *
 
1303
 * Return value: TRUE if the operation succeeded.
 
1304
 **/
 
1305
gboolean
 
1306
gp11_object_set_template (GP11Object *self, gulong attr_type, GP11Attributes *attrs,
 
1307
                          GError **err)
 
1308
{
 
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);
 
1312
}
 
1313
 
 
1314
/**
 
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.
 
1321
 *
 
1322
 * Set an attribute template on the object. The attr_type must be for
 
1323
 * an attribute which contains a template.
 
1324
 *
 
1325
 * This call may block for an indefinite period.
 
1326
 *
 
1327
 * Return value: TRUE if the operation succeeded.
 
1328
 **/
 
1329
gboolean
 
1330
gp11_object_set_template_full (GP11Object *self, gulong attr_type, GP11Attributes *attrs,
 
1331
                               GCancellable *cancellable, GError **err)
 
1332
{
 
1333
        GP11ObjectData *data = GP11_OBJECT_GET_DATA (self);
 
1334
        set_template_args args;
 
1335
        GP11Session *session;
 
1336
        gboolean ret = FALSE;
 
1337
 
 
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);
 
1341
 
 
1342
        _gp11_attributes_lock (attrs);
 
1343
 
 
1344
        memset (&args, 0, sizeof (args));
 
1345
        args.attrs = attrs;
 
1346
        args.type = attr_type;
 
1347
        args.object = data->handle;
 
1348
 
 
1349
        session = require_session_sync (self, CKF_RW_SESSION, err);
 
1350
        if (session)
 
1351
                ret = _gp11_call_sync (session, perform_set_template, NULL, &args, cancellable, err);
 
1352
 
 
1353
        _gp11_attributes_unlock (attrs);
 
1354
        g_object_unref (session);
 
1355
        return ret;
 
1356
}
 
1357
 
 
1358
/**
 
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.
 
1366
 *
 
1367
 * Set an attribute template on the object. The attr_type must be for
 
1368
 * an attribute which contains a template.
 
1369
 *
 
1370
 * This call will return immediately and complete asynchronously.
 
1371
 **/
 
1372
void
 
1373
gp11_object_set_template_async (GP11Object *self, gulong attr_type, GP11Attributes *attrs,
 
1374
                                GCancellable *cancellable, GAsyncReadyCallback callback,
 
1375
                                gpointer user_data)
 
1376
{
 
1377
        GP11ObjectData *data = GP11_OBJECT_GET_DATA (self);
 
1378
        set_template_args *args;
 
1379
        GP11Call *call;
 
1380
 
 
1381
        g_return_if_fail (GP11_IS_OBJECT (self));
 
1382
        g_return_if_fail (attrs);
 
1383
 
 
1384
        args = _gp11_call_async_prep (data->slot, self, perform_set_template,
 
1385
                                      NULL, sizeof (*args), free_set_template);
 
1386
 
 
1387
        _gp11_attributes_lock (attrs);
 
1388
        args->attrs = gp11_attributes_ref (attrs);
 
1389
        args->type = attr_type;
 
1390
        args->object = data->handle;
 
1391
 
 
1392
        call = _gp11_call_async_ready (args, cancellable, callback, user_data);
 
1393
        require_session_async (self, call, CKF_RW_SESSION, cancellable);
 
1394
}
 
1395
 
 
1396
/**
 
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.
 
1401
 *
 
1402
 * Get the result of an operation to set attribute template on
 
1403
 * an object.
 
1404
 *
 
1405
 * Return value: TRUE if the operation succeeded.
 
1406
 **/
 
1407
gboolean
 
1408
gp11_object_set_template_finish (GP11Object *self, GAsyncResult *result, GError **err)
 
1409
{
 
1410
        set_template_args *args;
 
1411
 
 
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);
 
1415
 
 
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);
 
1420
 
 
1421
        return _gp11_call_basic_finish (result, err);
 
1422
}
 
1423
 
 
1424
/* ---------------------------------------------------------------------------------------
 
1425
 * GET TEMPLATE
 
1426
 */
 
1427
 
 
1428
typedef struct _get_template_args {
 
1429
        GP11Arguments base;
 
1430
        CK_OBJECT_HANDLE object;
 
1431
        CK_ATTRIBUTE_TYPE type;
 
1432
        GP11Attributes *attrs;
 
1433
} get_template_args;
 
1434
 
 
1435
static CK_RV
 
1436
perform_get_template (get_template_args *args)
 
1437
{
 
1438
        CK_ATTRIBUTE attr;
 
1439
        CK_ULONG n_attrs, i;
 
1440
        CK_RV rv;
 
1441
 
 
1442
        g_assert (args);
 
1443
        g_assert (!args->attrs);
 
1444
 
 
1445
        args->attrs = gp11_attributes_new ();
 
1446
        attr.type = args->type;
 
1447
        attr.ulValueLen = 0;
 
1448
        attr.pValue = 0;
 
1449
 
 
1450
        /* Get the length of the entire template */
 
1451
        rv = (args->base.pkcs11->C_GetAttributeValue) (args->base.handle, args->object, &attr, 1);
 
1452
        if (rv != CKR_OK)
 
1453
                return rv;
 
1454
 
 
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);
 
1459
 
 
1460
        /* Prepare all the attributes */
 
1461
        _gp11_attributes_lock (args->attrs);
 
1462
        attr.pValue = _gp11_attributes_prepare_in (args->attrs, &n_attrs);
 
1463
 
 
1464
        /* Get the size of each value */
 
1465
        rv = (args->base.pkcs11->C_GetAttributeValue) (args->base.handle, args->object, &attr, 1);
 
1466
        if (rv != CKR_OK)
 
1467
                return rv;
 
1468
 
 
1469
        /* Allocate memory for each value */
 
1470
        attr.pValue = _gp11_attributes_commit_in (args->attrs, &n_attrs);
 
1471
 
 
1472
        /* Now get the actual values */
 
1473
        return (args->base.pkcs11->C_GetAttributeValue) (args->base.handle, args->object, &attr, 1);
 
1474
}
 
1475
 
 
1476
static void
 
1477
free_get_template (get_template_args *args)
 
1478
{
 
1479
        g_assert (args);
 
1480
        gp11_attributes_unref (args->attrs);
 
1481
        g_free (args);
 
1482
}
 
1483
 
 
1484
/**
 
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.
 
1489
 *
 
1490
 * Get an attribute template from the object. The attr_type must be for
 
1491
 * an attribute which returns a template.
 
1492
 *
 
1493
 * This call may block for an indefinite period.
 
1494
 *
 
1495
 * Return value: The resulting PKCS#11 attribute template, or NULL if an error occurred.
 
1496
 **/
 
1497
GP11Attributes*
 
1498
gp11_object_get_template (GP11Object *self, gulong attr_type, GError **err)
 
1499
{
 
1500
        g_return_val_if_fail (GP11_IS_OBJECT (self), NULL);
 
1501
        g_return_val_if_fail (!err || !*err, NULL);
 
1502
 
 
1503
        return gp11_object_get_template_full (self, attr_type, NULL, err);
 
1504
}
 
1505
 
 
1506
/**
 
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.
 
1512
 *
 
1513
 * Get an attribute template from the object. The attr_type must be for
 
1514
 * an attribute which returns a template.
 
1515
 *
 
1516
 * This call may block for an indefinite period.
 
1517
 *
 
1518
 * Return value: The resulting PKCS#11 attribute template, or NULL if an error occurred.
 
1519
 **/
 
1520
GP11Attributes*
 
1521
gp11_object_get_template_full (GP11Object *self, gulong attr_type,
 
1522
                               GCancellable *cancellable, GError **err)
 
1523
{
 
1524
        GP11ObjectData *data = GP11_OBJECT_GET_DATA (self);
 
1525
        get_template_args args;
 
1526
        GP11Session *session;
 
1527
        gboolean ret;
 
1528
 
 
1529
        g_return_val_if_fail (GP11_IS_OBJECT (self), NULL);
 
1530
        g_return_val_if_fail (!err || !*err, NULL);
 
1531
 
 
1532
        session = require_session_sync (self, 0, err);
 
1533
        if (!session)
 
1534
                return NULL;
 
1535
 
 
1536
        memset (&args, 0, sizeof (args));
 
1537
        args.object = data->handle;
 
1538
        args.type = attr_type;
 
1539
 
 
1540
        ret = _gp11_call_sync (session, perform_get_template, NULL, &args, cancellable, err);
 
1541
        g_object_unref (session);
 
1542
 
 
1543
        _gp11_attributes_unlock (args.attrs);
 
1544
 
 
1545
        /* Free any value if failed */
 
1546
        if (!ret) {
 
1547
                gp11_attributes_unref (args.attrs);
 
1548
                args.attrs = NULL;
 
1549
        }
 
1550
 
 
1551
        return args.attrs;
 
1552
}
 
1553
 
 
1554
/**
 
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.
 
1561
 *
 
1562
 * Get an attribute template from the object. The attr_type must be for
 
1563
 * an attribute which returns a template.
 
1564
 *
 
1565
 * This call will return immediately and complete asynchronously.
 
1566
 **/
 
1567
void
 
1568
gp11_object_get_template_async (GP11Object *self, gulong attr_type,
 
1569
                                GCancellable *cancellable, GAsyncReadyCallback callback,
 
1570
                                gpointer user_data)
 
1571
{
 
1572
        GP11ObjectData *data = GP11_OBJECT_GET_DATA (self);
 
1573
        get_template_args *args;
 
1574
        GP11Call *call;
 
1575
 
 
1576
        g_return_if_fail (GP11_IS_OBJECT (self));
 
1577
 
 
1578
        args = _gp11_call_async_prep (data->slot, self, perform_get_template,
 
1579
                                      NULL, sizeof (*args), free_get_template);
 
1580
 
 
1581
        args->object = data->handle;
 
1582
        args->type = attr_type;
 
1583
 
 
1584
        call = _gp11_call_async_ready (args, cancellable, callback, user_data);
 
1585
        require_session_async (self, call, 0, cancellable);
 
1586
}
 
1587
 
 
1588
/**
 
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.
 
1593
 *
 
1594
 * Get the result of an operation to get attribute template from
 
1595
 * an object.
 
1596
 *
 
1597
 * Return value: The resulting PKCS#11 attribute template, or NULL if an error occurred.
 
1598
 **/
 
1599
GP11Attributes*
 
1600
gp11_object_get_template_finish (GP11Object *self, GAsyncResult *result,
 
1601
                                 GError **err)
 
1602
{
 
1603
        get_template_args *args;
 
1604
 
 
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);
 
1608
 
 
1609
        if (!_gp11_call_basic_finish (result, err))
 
1610
                return NULL;
 
1611
 
 
1612
        args = _gp11_call_arguments (result, get_template_args);
 
1613
        _gp11_attributes_unlock (args->attrs);
 
1614
        return gp11_attributes_ref (args->attrs);
 
1615
}