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

« back to all changes in this revision

Viewing changes to gp11/tests/gp11-test-module.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:
75
75
        g_free (sess);
76
76
}
77
77
 
 
78
static GP11Attributes*
 
79
lookup_object (Session *session, CK_OBJECT_HANDLE hObject)
 
80
{
 
81
        GP11Attributes *attrs;
 
82
        attrs = g_hash_table_lookup (the_objects, GUINT_TO_POINTER (hObject));
 
83
        if (!attrs)
 
84
                attrs = g_hash_table_lookup (session->objects, GUINT_TO_POINTER (hObject));
 
85
        return attrs;
 
86
}
 
87
 
78
88
static CK_RV
79
89
test_C_Initialize (CK_VOID_PTR pInitArgs)
80
90
{
136
146
                                      CKA_ALLOWED_MECHANISMS, sizeof (value), &value,
137
147
                                      CKA_DECRYPT, GP11_BOOLEAN, TRUE,
138
148
                                      CKA_PRIVATE, GP11_BOOLEAN, TRUE,
 
149
                                      CKA_WRAP, GP11_BOOLEAN, TRUE,
 
150
                                      CKA_UNWRAP, GP11_BOOLEAN, TRUE,
 
151
                                      CKA_DERIVE, GP11_BOOLEAN, TRUE,
 
152
                                      CKA_VALUE, GP11_STRING, "value",
139
153
                                      GP11_INVALID);
140
154
        g_hash_table_insert (the_objects, GUINT_TO_POINTER (PRIVATE_KEY_CAPITALIZE), attrs);
141
155
 
146
160
                                      CKA_ALLOWED_MECHANISMS, sizeof (value), &value,
147
161
                                      CKA_ENCRYPT, GP11_BOOLEAN, TRUE,
148
162
                                      CKA_PRIVATE, GP11_BOOLEAN, FALSE,
 
163
                                      CKA_VALUE, GP11_STRING, "value",
149
164
                                      GP11_INVALID);
150
165
        g_hash_table_insert (the_objects, GUINT_TO_POINTER (PUBLIC_KEY_CAPITALIZE), attrs);
151
166
 
157
172
                                      CKA_SIGN, GP11_BOOLEAN, TRUE,
158
173
                                      CKA_PRIVATE, GP11_BOOLEAN, TRUE,
159
174
                                      CKA_ALWAYS_AUTHENTICATE, GP11_BOOLEAN, TRUE,
 
175
                                      CKA_VALUE, GP11_STRING, "value",
160
176
                                      GP11_INVALID);
161
177
        g_hash_table_insert (the_objects, GUINT_TO_POINTER (PRIVATE_KEY_PREFIX), attrs);
162
178
 
167
183
                                      CKA_ALLOWED_MECHANISMS, sizeof (value), &value,
168
184
                                      CKA_VERIFY, GP11_BOOLEAN, TRUE,
169
185
                                      CKA_PRIVATE, GP11_BOOLEAN, FALSE,
 
186
                                      CKA_VALUE, GP11_STRING, "value",
170
187
                                      GP11_INVALID);
171
188
        g_hash_table_insert (the_objects, GUINT_TO_POINTER (PUBLIC_KEY_PREFIX), attrs);
172
189
        
482
499
test_C_InitPIN (CK_SESSION_HANDLE hSession, CK_UTF8CHAR_PTR pPin, 
483
500
                CK_ULONG ulPinLen)
484
501
{
485
 
        g_assert_not_reached (); /* Not yet used by library */
486
 
        return CKR_FUNCTION_NOT_SUPPORTED;
 
502
        Session *session;
 
503
 
 
504
        session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession));
 
505
        g_assert (session != NULL && "No such session found");
 
506
        if (!session)
 
507
                return CKR_SESSION_HANDLE_INVALID;
 
508
 
 
509
        g_free (the_pin);
 
510
        the_pin = g_strndup ((gchar*)pPin, ulPinLen);
 
511
        return CKR_OK;
487
512
}
488
513
 
489
514
static CK_RV
490
515
test_C_SetPIN (CK_SESSION_HANDLE hSession, CK_UTF8CHAR_PTR pOldPin,
491
 
             CK_ULONG ulOldLen, CK_UTF8CHAR_PTR pNewPin, CK_ULONG ulNewLen)
 
516
               CK_ULONG ulOldLen, CK_UTF8CHAR_PTR pNewPin, CK_ULONG ulNewLen)
492
517
{
493
 
        g_assert_not_reached (); /* Not yet used by library */
494
 
        return CKR_FUNCTION_NOT_SUPPORTED;
 
518
        Session *session;
 
519
        gchar *old;
 
520
 
 
521
        session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession));
 
522
        g_assert (session != NULL && "No such session found");
 
523
        if (!session)
 
524
                return CKR_SESSION_HANDLE_INVALID;
 
525
 
 
526
        old = g_strndup ((gchar*)pOldPin, ulOldLen);
 
527
        if (!g_str_equal (old, the_pin))
 
528
                return CKR_PIN_INCORRECT;
 
529
 
 
530
        g_free (the_pin);
 
531
        the_pin = g_strndup ((gchar*)pNewPin, ulNewLen);
 
532
        return CKR_OK;
495
533
}
496
534
 
497
535
static CK_RV
613
651
        g_assert (session != NULL && "No such session found");
614
652
        if (!session)
615
653
                return CKR_SESSION_HANDLE_INVALID;
616
 
        
617
 
        attrs = g_hash_table_lookup (the_objects, GUINT_TO_POINTER (hObject));
618
 
        if (!attrs)
619
 
                attrs = g_hash_table_lookup (session->objects, GUINT_TO_POINTER (hObject));
 
654
 
 
655
        attrs = lookup_object (session, hObject);
620
656
        if (!attrs) {
621
657
                g_assert_not_reached (); /* "no such object found" */
622
658
                return CKR_OBJECT_HANDLE_INVALID;
654
690
        if (!session)
655
691
                return CKR_SESSION_HANDLE_INVALID;
656
692
        
657
 
        attrs = g_hash_table_lookup (the_objects, GUINT_TO_POINTER (hObject));
658
 
        if (!attrs)
659
 
                attrs = g_hash_table_lookup (session->objects, GUINT_TO_POINTER (hObject));
 
693
        attrs = lookup_object (session, hObject);
660
694
        if (!attrs) {
661
695
                g_assert_not_reached (); /* "invalid object handle passed" */
662
696
                return CKR_OBJECT_HANDLE_INVALID;
703
737
        if (!session)
704
738
                return CKR_SESSION_HANDLE_INVALID;
705
739
 
706
 
        attrs = g_hash_table_lookup (the_objects, GUINT_TO_POINTER (hObject));
707
 
        if (!attrs)
708
 
                attrs = g_hash_table_lookup (session->objects, GUINT_TO_POINTER (hObject));
 
740
        attrs = lookup_object (session, hObject);
709
741
        if (!attrs) {
710
742
                g_assert_not_reached (); /* "invalid object handle passed" */
711
743
                return CKR_OBJECT_HANDLE_INVALID;
1340
1372
 
1341
1373
static CK_RV
1342
1374
test_C_GenerateKeyPair (CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
1343
 
                      CK_ATTRIBUTE_PTR pPublicKeyTemplate, CK_ULONG ulPublicKeyAttributeCount,
1344
 
                      CK_ATTRIBUTE_PTR pPrivateKeyTemplate, CK_ULONG ulPrivateKeyAttributeCount,
1345
 
                      CK_OBJECT_HANDLE_PTR phPublicKey, CK_OBJECT_HANDLE_PTR phPrivateKey)
 
1375
                        CK_ATTRIBUTE_PTR pPublicKeyTemplate, CK_ULONG ulPublicKeyAttributeCount,
 
1376
                        CK_ATTRIBUTE_PTR pPrivateKeyTemplate, CK_ULONG ulPrivateKeyAttributeCount,
 
1377
                        CK_OBJECT_HANDLE_PTR phPublicKey, CK_OBJECT_HANDLE_PTR phPrivateKey)
1346
1378
{
1347
 
        g_assert_not_reached (); /* Not yet used by library */
1348
 
        return CKR_FUNCTION_NOT_SUPPORTED;
 
1379
        GP11Attributes *attrs;
 
1380
        Session *session;
 
1381
        gboolean token;
 
1382
        CK_ULONG i;
 
1383
 
 
1384
        session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession));
 
1385
        g_assert (session != NULL && "No such session found");
 
1386
        if (!session)
 
1387
                return CKR_SESSION_HANDLE_INVALID;
 
1388
 
 
1389
        g_assert (pMechanism);
 
1390
        g_assert (pPublicKeyTemplate);
 
1391
        g_assert (ulPublicKeyAttributeCount);
 
1392
        g_assert (pPrivateKeyTemplate);
 
1393
        g_assert (phPublicKey);
 
1394
        g_assert (phPrivateKey);
 
1395
 
 
1396
        if (pMechanism->mechanism != CKM_GENERATE)
 
1397
                return CKR_MECHANISM_INVALID;
 
1398
 
 
1399
        if (!pMechanism->pParameter || pMechanism->ulParameterLen != 9 ||
 
1400
            memcmp (pMechanism->pParameter, "generate", 9) != 0) {
 
1401
                g_assert_not_reached ();
 
1402
                return CKR_MECHANISM_PARAM_INVALID;
 
1403
        }
 
1404
 
 
1405
        attrs = gp11_attributes_new ();
 
1406
        gp11_attributes_add_string (attrs, CKA_VALUE, "generated");
 
1407
        for (i = 0; i < ulPublicKeyAttributeCount; ++i)
 
1408
                gp11_attributes_add_data (attrs, pPublicKeyTemplate[i].type,
 
1409
                                          pPublicKeyTemplate[i].pValue,
 
1410
                                          pPublicKeyTemplate[i].ulValueLen);
 
1411
        *phPublicKey = ++unique_identifier;
 
1412
        if (gp11_attributes_find_boolean (attrs, CKA_TOKEN, &token) && token)
 
1413
                g_hash_table_insert (the_objects, GUINT_TO_POINTER (*phPublicKey), attrs);
 
1414
        else
 
1415
                g_hash_table_insert (session->objects, GUINT_TO_POINTER (*phPublicKey), attrs);
 
1416
 
 
1417
        attrs = gp11_attributes_new ();
 
1418
        gp11_attributes_add_string (attrs, CKA_VALUE, "generated");
 
1419
        for (i = 0; i < ulPrivateKeyAttributeCount; ++i)
 
1420
                gp11_attributes_add_data (attrs, pPrivateKeyTemplate[i].type,
 
1421
                                          pPrivateKeyTemplate[i].pValue,
 
1422
                                          pPrivateKeyTemplate[i].ulValueLen);
 
1423
        *phPrivateKey = ++unique_identifier;
 
1424
        if (gp11_attributes_find_boolean (attrs, CKA_TOKEN, &token) && token)
 
1425
                g_hash_table_insert (the_objects, GUINT_TO_POINTER (*phPrivateKey), attrs);
 
1426
        else
 
1427
                g_hash_table_insert (session->objects, GUINT_TO_POINTER (*phPrivateKey), attrs);
 
1428
        return CKR_OK;
1349
1429
}
1350
1430
 
1351
1431
static CK_RV
1352
1432
test_C_WrapKey (CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
1353
 
              CK_OBJECT_HANDLE hWrappingKey, CK_OBJECT_HANDLE hKey,
1354
 
              CK_BYTE_PTR pWrappedKey, CK_ULONG_PTR pulWrappedKeyLen)
 
1433
                CK_OBJECT_HANDLE hWrappingKey, CK_OBJECT_HANDLE hKey,
 
1434
                CK_BYTE_PTR pWrappedKey, CK_ULONG_PTR pulWrappedKeyLen)
1355
1435
{
1356
 
        g_assert_not_reached (); /* Not yet used by library */
1357
 
        return CKR_FUNCTION_NOT_SUPPORTED;
 
1436
        GP11Attributes *attrs;
 
1437
        GP11Attribute *attr;
 
1438
        Session *session;
 
1439
 
 
1440
        session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession));
 
1441
        if (!session) {
 
1442
                g_assert_not_reached ();
 
1443
                return CKR_SESSION_HANDLE_INVALID;
 
1444
        }
 
1445
 
 
1446
        g_assert (pMechanism);
 
1447
        g_assert (hWrappingKey);
 
1448
        g_assert (hKey);
 
1449
        g_assert (pulWrappedKeyLen);
 
1450
 
 
1451
        attrs = lookup_object (session, hWrappingKey);
 
1452
        if (!attrs) {
 
1453
                g_assert_not_reached ();
 
1454
                return CKR_WRAPPING_KEY_HANDLE_INVALID;
 
1455
        }
 
1456
 
 
1457
        attrs = lookup_object (session, hKey);
 
1458
        if (!attrs) {
 
1459
                g_assert_not_reached ();
 
1460
                return CKR_WRAPPED_KEY_INVALID;
 
1461
        }
 
1462
 
 
1463
        if (pMechanism->mechanism != CKM_WRAP)
 
1464
                return CKR_MECHANISM_INVALID;
 
1465
 
 
1466
        if (pMechanism->pParameter) {
 
1467
                if (pMechanism->ulParameterLen != 4 ||
 
1468
                    memcmp (pMechanism->pParameter, "wrap", 4) != 0) {
 
1469
                        g_assert_not_reached ();
 
1470
                        return CKR_MECHANISM_PARAM_INVALID;
 
1471
                }
 
1472
        }
 
1473
 
 
1474
        attr = gp11_attributes_find (attrs, CKA_VALUE);
 
1475
        if (attr == NULL)
 
1476
                return CKR_WRAPPED_KEY_INVALID;
 
1477
 
 
1478
        if (!pWrappedKey) {
 
1479
                *pulWrappedKeyLen = attr->length;
 
1480
                return CKR_OK;
 
1481
        }
 
1482
 
 
1483
        if (*pulWrappedKeyLen < attr->length) {
 
1484
                *pulWrappedKeyLen = attr->length;
 
1485
                return CKR_BUFFER_TOO_SMALL;
 
1486
        }
 
1487
 
 
1488
        memcpy (pWrappedKey, attr->value, attr->length);
 
1489
        *pulWrappedKeyLen = attr->length;
 
1490
 
 
1491
        return CKR_OK;
1358
1492
}
1359
1493
 
1360
1494
static CK_RV
1363
1497
                CK_ULONG pulWrappedKeyLen, CK_ATTRIBUTE_PTR pTemplate,
1364
1498
                CK_ULONG ulCount, CK_OBJECT_HANDLE_PTR phKey)
1365
1499
{
1366
 
        g_assert_not_reached (); /* Not yet used by library */
1367
 
        return CKR_FUNCTION_NOT_SUPPORTED;
 
1500
        GP11Attributes *attrs;
 
1501
        Session *session;
 
1502
        gboolean token;
 
1503
        CK_ULONG i;
 
1504
 
 
1505
        session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession));
 
1506
        if (!session) {
 
1507
                g_assert_not_reached ();
 
1508
                return CKR_SESSION_HANDLE_INVALID;
 
1509
        }
 
1510
 
 
1511
        g_assert (pMechanism);
 
1512
        g_assert (pUnwrappingKey);
 
1513
        g_assert (pWrappedKey);
 
1514
        g_assert (pulWrappedKeyLen);
 
1515
        g_assert (phKey);
 
1516
        g_assert (ulCount);
 
1517
        g_assert (pTemplate);
 
1518
        g_assert (phKey);
 
1519
 
 
1520
        attrs = lookup_object (session, pUnwrappingKey);
 
1521
        if (!attrs) {
 
1522
                g_assert_not_reached ();
 
1523
                return CKR_WRAPPING_KEY_HANDLE_INVALID;
 
1524
        }
 
1525
 
 
1526
        if (pMechanism->mechanism != CKM_WRAP)
 
1527
                return CKR_MECHANISM_INVALID;
 
1528
 
 
1529
        if (pMechanism->pParameter) {
 
1530
                if (pMechanism->ulParameterLen != 4 ||
 
1531
                    memcmp (pMechanism->pParameter, "wrap", 4) != 0) {
 
1532
                        g_assert_not_reached ();
 
1533
                        return CKR_MECHANISM_PARAM_INVALID;
 
1534
                }
 
1535
        }
 
1536
 
 
1537
        attrs = gp11_attributes_new ();
 
1538
        gp11_attributes_add_data (attrs, CKA_VALUE, pWrappedKey, pulWrappedKeyLen);
 
1539
        for (i = 0; i < ulCount; ++i)
 
1540
                gp11_attributes_add_data (attrs, pTemplate[i].type,
 
1541
                                          pTemplate[i].pValue,
 
1542
                                          pTemplate[i].ulValueLen);
 
1543
        *phKey = ++unique_identifier;
 
1544
        if (gp11_attributes_find_boolean (attrs, CKA_TOKEN, &token) && token)
 
1545
                g_hash_table_insert (the_objects, GUINT_TO_POINTER (*phKey), attrs);
 
1546
        else
 
1547
                g_hash_table_insert (session->objects, GUINT_TO_POINTER (*phKey), attrs);
 
1548
 
 
1549
        return CKR_OK;
1368
1550
}
1369
1551
 
1370
1552
static CK_RV
1372
1554
                CK_OBJECT_HANDLE hBaseKey, CK_ATTRIBUTE_PTR pTemplate,
1373
1555
                CK_ULONG ulCount, CK_OBJECT_HANDLE_PTR phKey)
1374
1556
{
1375
 
        g_assert_not_reached (); /* Not yet used by library */
1376
 
        return CKR_FUNCTION_NOT_SUPPORTED;
 
1557
        GP11Attributes *attrs, *copy;
 
1558
        Session *session;
 
1559
        gboolean token;
 
1560
        CK_ULONG i;
 
1561
 
 
1562
        session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession));
 
1563
        if (!session) {
 
1564
                g_assert_not_reached ();
 
1565
                return CKR_SESSION_HANDLE_INVALID;
 
1566
        }
 
1567
 
 
1568
        g_assert (pMechanism);
 
1569
        g_assert (ulCount);
 
1570
        g_assert (pTemplate);
 
1571
        g_assert (phKey);
 
1572
 
 
1573
        attrs = lookup_object (session, hBaseKey);
 
1574
        if (!attrs) {
 
1575
                g_assert_not_reached ();
 
1576
                return CKR_WRAPPING_KEY_HANDLE_INVALID;
 
1577
        }
 
1578
 
 
1579
        if (pMechanism->mechanism != CKM_DERIVE)
 
1580
                return CKR_MECHANISM_INVALID;
 
1581
 
 
1582
        if (pMechanism->pParameter) {
 
1583
                if (pMechanism->ulParameterLen != 6 ||
 
1584
                    memcmp (pMechanism->pParameter, "derive", 6) != 0) {
 
1585
                        g_assert_not_reached ();
 
1586
                        return CKR_MECHANISM_PARAM_INVALID;
 
1587
                }
 
1588
        }
 
1589
 
 
1590
        copy = gp11_attributes_new ();
 
1591
        gp11_attributes_add_string (copy, CKA_VALUE, "derived");
 
1592
        for (i = 0; i < ulCount; ++i)
 
1593
                gp11_attributes_add_data (copy, pTemplate[i].type,
 
1594
                                          pTemplate[i].pValue,
 
1595
                                          pTemplate[i].ulValueLen);
 
1596
        for (i = 0; i < gp11_attributes_count (attrs); ++i)
 
1597
                gp11_attributes_add (copy, gp11_attributes_at (attrs, i));
 
1598
        *phKey = ++unique_identifier;
 
1599
        if (gp11_attributes_find_boolean (copy, CKA_TOKEN, &token) && token)
 
1600
                g_hash_table_insert (the_objects, GUINT_TO_POINTER (*phKey), copy);
 
1601
        else
 
1602
                g_hash_table_insert (session->objects, GUINT_TO_POINTER (*phKey), copy);
 
1603
 
 
1604
        return CKR_OK;
1377
1605
}
1378
1606
 
1379
1607
static CK_RV