pkcs11 0.2.0-x86-mswin32 → 0.2.1-x86-mswin32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/ext/pk11.c CHANGED
@@ -1,4 +1,5 @@
1
1
  #include "pk11.h"
2
+ #include "pk11_struct_macros.h"
2
3
 
3
4
  #if defined(compile_for_windows)
4
5
  #include <winbase.h> /* for LoadLibrary() */
@@ -6,7 +7,7 @@
6
7
  #include <dlfcn.h>
7
8
  #endif
8
9
 
9
- static const char *VERSION = "0.2.0";
10
+ static const char *VERSION = "0.2.1";
10
11
 
11
12
  static ID sNEW;
12
13
  static VALUE mPKCS11;
@@ -30,28 +31,21 @@ static VALUE aCK_SESSION_INFO_members;
30
31
  static VALUE cCK_MECHANISM;
31
32
  static VALUE aCK_MECHANISM_members;
32
33
 
33
- #define HANDLE2NUM(n) ULONG2NUM(n)
34
- #define NUM2HANDLE(n) PKNUM2ULONG(n)
35
- #define PKNUM2ULONG(n) pkcs11_num2ulong(n)
34
+ #define MODULE_FOR_STRUCTS mPKCS11
35
+ #define BASECLASS_FOR_STRUCTS cCStruct
36
36
  #define pkcs11_new_struct(klass) rb_funcall(klass, sNEW, 0)
37
37
 
38
- VALUE pkcs11_return_value_to_class(CK_RV, VALUE);
38
+ #define PKCS11_DEFINE_METHOD(name, args) \
39
+ rb_define_method(cPKCS11, #name, pkcs11_##name, args);
39
40
 
40
- static VALUE
41
- pkcs11_num2ulong(VALUE val)
42
- {
43
- if (TYPE(val) == T_BIGNUM || TYPE(val) == T_FIXNUM) {
44
- return NUM2ULONG(val);
45
- }
46
- return NUM2ULONG(rb_to_int(val));
47
- }
41
+ VALUE pkcs11_return_value_to_class(CK_RV, VALUE);
48
42
 
49
43
  static void
50
44
  pkcs11_raise(CK_RV rv)
51
45
  {
52
46
  VALUE class;
53
47
  class = pkcs11_return_value_to_class(rv, ePKCS11Error);
54
- rb_raise(class, "%li", rv);
48
+ rb_raise(class, "%lu", rv);
55
49
  }
56
50
 
57
51
  ///////////////////////////////////////
@@ -1389,52 +1383,60 @@ ck_attr_value(VALUE self)
1389
1383
  Data_Get_Struct(self, CK_ATTRIBUTE, attr);
1390
1384
  if (attr->ulValueLen == 0) return Qnil;
1391
1385
  switch(attr->type){
1392
- case CKA_TOKEN:
1393
- case CKA_PRIVATE:
1394
- case CKA_SENSITIVE:
1395
- case CKA_ENCRYPT:
1386
+ case CKA_ALWAYS_AUTHENTICATE:
1387
+ case CKA_ALWAYS_SENSITIVE:
1388
+ case CKA_COLOR:
1389
+ /* case CKA_COPYABLE: v2.3 */
1396
1390
  case CKA_DECRYPT:
1397
- case CKA_WRAP:
1398
- case CKA_UNWRAP:
1399
- case CKA_SIGN:
1400
- case CKA_SIGN_RECOVER:
1401
- case CKA_VERIFY:
1402
- case CKA_VERIFY_RECOVER:
1403
1391
  case CKA_DERIVE:
1404
- case CKA_TRUSTED:
1392
+ case CKA_ENCRYPT:
1405
1393
  case CKA_EXTRACTABLE:
1394
+ case CKA_HAS_RESET:
1406
1395
  case CKA_LOCAL:
1407
- case CKA_NEVER_EXTRACTABLE:
1408
- case CKA_ALWAYS_SENSITIVE:
1409
1396
  case CKA_MODIFIABLE:
1410
- case CKA_HAS_RESET:
1411
- case CKA_ALWAYS_AUTHENTICATE:
1412
- case CKA_COLOR:
1397
+ case CKA_NEVER_EXTRACTABLE:
1413
1398
  case CKA_OTP_USER_FRIENDLY_MODE:
1399
+ case CKA_PRIVATE:
1400
+ case CKA_SENSITIVE:
1401
+ case CKA_SIGN:
1402
+ case CKA_SIGN_RECOVER:
1403
+ case CKA_TOKEN:
1404
+ case CKA_TRUSTED:
1405
+ case CKA_UNWRAP:
1406
+ case CKA_VERIFY:
1407
+ case CKA_VERIFY_RECOVER:
1408
+ case CKA_WRAP:
1414
1409
  case CKA_WRAP_WITH_TRUSTED:
1415
1410
  if (attr->ulValueLen == sizeof(CK_BBOOL))
1416
1411
  return (*(CK_BBOOL*)(attr->pValue)) == CK_TRUE ? Qtrue : Qfalse;
1417
1412
  break;
1418
- case CKA_CLASS:
1419
- case CKA_CERTIFICATE_TYPE:
1420
- case CKA_KEY_TYPE:
1421
- case CKA_HW_FEATURE_TYPE:
1422
1413
  case CKA_BITS_PER_PIXEL:
1423
1414
  case CKA_CERTIFICATE_CATEGORY:
1415
+ case CKA_CERTIFICATE_TYPE:
1424
1416
  case CKA_CHAR_COLUMNS:
1425
1417
  case CKA_CHAR_ROWS:
1418
+ case CKA_CLASS:
1419
+ case CKA_HW_FEATURE_TYPE:
1426
1420
  case CKA_JAVA_MIDP_SECURITY_DOMAIN:
1421
+ case CKA_KEY_TYPE:
1427
1422
  case CKA_MECHANISM_TYPE:
1423
+ case CKA_MODULUS_BITS:
1424
+ case CKA_OTP_CHALLENGE_REQUIREMENT:
1425
+ case CKA_OTP_COUNTER_REQUIREMENT:
1426
+ case CKA_OTP_FORMAT:
1427
+ case CKA_OTP_LENGTH:
1428
+ case CKA_OTP_PIN_REQUIREMENT:
1429
+ case CKA_OTP_TIME_INTERVAL:
1430
+ case CKA_OTP_TIME_REQUIREMENT:
1428
1431
  case CKA_OTP_SERVICE_LOGO_TYPE:
1429
1432
  case CKA_PIXEL_X:
1430
1433
  case CKA_PIXEL_Y:
1431
- case CKA_RESOLUTION:
1432
- case CKA_VALUE_LEN:
1433
- case CKA_MODULUS_BITS:
1434
1434
  case CKA_PRIME_BITS:
1435
+ case CKA_RESOLUTION:
1435
1436
  case CKA_SUBPRIME_BITS:
1436
1437
  case CKA_VALUE_BITS:
1437
- if (attr->ulValueLen == sizeof(CK_ULONG))
1438
+ case CKA_VALUE_LEN:
1439
+ if (attr->ulValueLen == sizeof(CK_ULONG))
1438
1440
  return ULONG2NUM(*(CK_ULONG_PTR)(attr->pValue));
1439
1441
  break;
1440
1442
  }
@@ -1443,313 +1445,6 @@ ck_attr_value(VALUE self)
1443
1445
 
1444
1446
  ///////////////////////////////////////
1445
1447
 
1446
- static VALUE
1447
- get_string(VALUE obj, off_t offset, size_t size)
1448
- {
1449
- char *ptr = (char*)DATA_PTR(obj);
1450
- return rb_str_new(ptr+offset, size);
1451
- }
1452
-
1453
- static VALUE
1454
- set_string(VALUE obj, VALUE value, off_t offset, size_t size)
1455
- {
1456
- char *ptr = (char*)DATA_PTR(obj);
1457
- int len = size;
1458
- StringValue(value);
1459
- if (RSTRING_LEN(value) < len) len = RSTRING_LEN(value);
1460
- memset(ptr+offset, 0, size);
1461
- memcpy(ptr+offset, RSTRING_PTR(value), len);
1462
- return value;
1463
- }
1464
-
1465
- static VALUE
1466
- get_ulong(VALUE obj, off_t offset)
1467
- {
1468
- char *ptr = (char*)DATA_PTR(obj);
1469
- return ULONG2NUM(*(CK_ULONG_PTR)(ptr+offset));
1470
- }
1471
-
1472
- static VALUE
1473
- set_ulong(VALUE obj, VALUE value, off_t offset)
1474
- {
1475
- char *ptr = (char*)DATA_PTR(obj);
1476
- *(CK_ULONG_PTR)(ptr+offset) = NUM2ULONG(value);
1477
- return value;
1478
- }
1479
-
1480
- static VALUE
1481
- get_byte(VALUE obj, off_t offset)
1482
- {
1483
- char *ptr = (char*)DATA_PTR(obj);
1484
- return ULONG2NUM(*(CK_BYTE_PTR)(ptr+offset));
1485
- }
1486
-
1487
- static VALUE
1488
- set_byte(VALUE obj, VALUE value, off_t offset)
1489
- {
1490
- char *ptr = (char*)DATA_PTR(obj);
1491
- *(CK_BYTE_PTR)(ptr+offset) = NUM2ULONG(value);
1492
- return value;
1493
- }
1494
-
1495
- static VALUE
1496
- get_ulong_ptr(VALUE obj, off_t offset)
1497
- {
1498
- char *ptr = (char*)DATA_PTR(obj);
1499
- CK_ULONG_PTR p = *(CK_ULONG_PTR *)(ptr+offset);
1500
- if (!p) return Qnil;
1501
- return ULONG2NUM(*p);
1502
- }
1503
-
1504
- static VALUE
1505
- set_ulong_ptr(VALUE obj, VALUE value, const char *name, off_t offset)
1506
- {
1507
- CK_ULONG_PTR *ptr = (CK_ULONG_PTR *)((char*)DATA_PTR(obj) + offset);
1508
- if (NIL_P(value)){
1509
- rb_iv_set(obj, name, value);
1510
- *ptr = NULL_PTR;
1511
- return value;
1512
- }
1513
- VALUE new_obj = Data_Make_Struct(rb_cInteger, CK_ULONG, 0, free, *ptr);
1514
- rb_iv_set(obj, name, new_obj);
1515
- **ptr = NUM2ULONG(value);
1516
- return value;
1517
- }
1518
-
1519
- static VALUE
1520
- get_handle(VALUE obj, off_t offset)
1521
- {
1522
- char *ptr = (char*)DATA_PTR(obj);
1523
- return HANDLE2NUM(*(CK_OBJECT_HANDLE_PTR)(ptr+offset));
1524
- }
1525
-
1526
- static VALUE
1527
- set_handle(VALUE obj, VALUE value, off_t offset)
1528
- {
1529
- char *ptr = (char*)DATA_PTR(obj);
1530
- *(CK_OBJECT_HANDLE_PTR)(ptr+offset) = NUM2HANDLE(value);
1531
- return value;
1532
- }
1533
-
1534
- static VALUE
1535
- get_bool(VALUE obj, off_t offset)
1536
- {
1537
- char *ptr = (char*)DATA_PTR(obj);
1538
- if(*(CK_BBOOL*)(ptr+offset)) return Qtrue;
1539
- else return Qfalse;
1540
- }
1541
-
1542
- static VALUE
1543
- set_bool(VALUE obj, VALUE value, off_t offset)
1544
- {
1545
- char *ptr = (char*)DATA_PTR(obj);
1546
- if(value == Qfalse) *(CK_BBOOL*)(ptr+offset) = 0;
1547
- else if(value == Qtrue) *(CK_BBOOL*)(ptr+offset) = 1;
1548
- else rb_raise(rb_eArgError, "arg must be true or false");
1549
- return value;
1550
- }
1551
-
1552
- static VALUE
1553
- get_string_ptr(VALUE obj, const char *name, off_t offset)
1554
- {
1555
- char *ptr = (char*)DATA_PTR(obj);
1556
- char *p = *(char**)(ptr+offset);
1557
- if (!p) return Qnil;
1558
- return rb_str_new2(p);
1559
- }
1560
-
1561
- static VALUE
1562
- set_string_ptr(VALUE obj, VALUE value, const char *name, off_t offset)
1563
- {
1564
- char *ptr = (char*)DATA_PTR(obj);
1565
- if (NIL_P(value)){
1566
- rb_iv_set(obj, name, value);
1567
- *(CK_VOID_PTR*)(ptr+offset) = NULL_PTR;
1568
- return value;
1569
- }
1570
- StringValue(value);
1571
- value = rb_obj_freeze(rb_str_dup(value));
1572
- rb_iv_set(obj, name, value);
1573
- *(CK_VOID_PTR*)(ptr+offset) = RSTRING_PTR(value);
1574
- return value;
1575
- }
1576
-
1577
- static VALUE
1578
- get_string_ptr_len(VALUE obj, const char *name, off_t offset, off_t offset_len)
1579
- {
1580
- unsigned long l;
1581
- char *ptr = (char*)DATA_PTR(obj);
1582
- char *p = *(char**)(ptr+offset);
1583
- if (!p) return Qnil;
1584
- l = *(unsigned long*)(ptr+offset_len);
1585
- return rb_str_new(p, l);
1586
- }
1587
-
1588
- static VALUE
1589
- set_string_ptr_len(VALUE obj, VALUE value, const char *name, off_t offset, off_t offset_len)
1590
- {
1591
- char *ptr = (char*)DATA_PTR(obj);
1592
- if (NIL_P(value)){
1593
- rb_iv_set(obj, name, value);
1594
- *(CK_VOID_PTR*)(ptr+offset) = NULL_PTR;
1595
- *(unsigned long*)(ptr+offset_len) = 0;
1596
- return value;
1597
- }
1598
- StringValue(value);
1599
- value = rb_obj_freeze(rb_str_dup(value));
1600
- rb_iv_set(obj, name, value);
1601
- *(CK_VOID_PTR*)(ptr+offset) = RSTRING_PTR(value);
1602
- *(unsigned long*)(ptr+offset_len) = RSTRING_LEN(value);
1603
- return value;
1604
- }
1605
-
1606
- static VALUE
1607
- get_struct_inline(VALUE obj, VALUE klass, const char *name, off_t offset)
1608
- {
1609
- char *ptr = (char*)DATA_PTR(obj) + offset;
1610
- VALUE inline_obj = Data_Wrap_Struct(klass, 0, 0, ptr);
1611
- rb_iv_set(inline_obj, name, obj);
1612
- return inline_obj;
1613
- }
1614
-
1615
- static VALUE
1616
- set_struct_inline(VALUE obj, VALUE klass, const char *struct_name, VALUE value, const char *name, off_t offset, int sizeofstruct)
1617
- {
1618
- char *ptr = (char*)DATA_PTR(obj) + offset;
1619
- if (!rb_obj_is_kind_of(value, klass))
1620
- rb_raise(rb_eArgError, "arg must be a PKCS11::%s", struct_name);
1621
- memcpy(ptr, DATA_PTR(value), sizeofstruct);
1622
- return value;
1623
- }
1624
-
1625
- static VALUE
1626
- get_struct_ptr(VALUE obj, VALUE klass, const char *name, off_t offset)
1627
- {
1628
- char *ptr = (char*)DATA_PTR(obj);
1629
- char *p = *(char**)(ptr+offset);
1630
- if (!p) return Qnil;
1631
- return rb_iv_get(obj, name);
1632
- }
1633
-
1634
- static VALUE
1635
- set_struct_ptr(VALUE obj, VALUE klass, const char *struct_name, VALUE value, const char *name, off_t offset)
1636
- {
1637
- char *ptr = (char*)DATA_PTR(obj) + offset;
1638
- if (NIL_P(value)){
1639
- rb_iv_set(obj, name, value);
1640
- *(CK_VOID_PTR*)ptr = NULL_PTR;
1641
- return value;
1642
- }
1643
- if (!rb_obj_is_kind_of(value, klass))
1644
- rb_raise(rb_eArgError, "arg must be a PKCS11::%s", struct_name);
1645
- *(CK_VOID_PTR*)ptr = DATA_PTR(value);
1646
- rb_iv_set(obj, name, value);
1647
- return value;
1648
- }
1649
-
1650
- #define OFFSET_OF(s, f) ((off_t)((char*)&(((s*)0)->f) - (char*)0))
1651
- #define SIZE_OF(s, f) (sizeof(((s*)0)->f))
1652
-
1653
- #define PKCS11_IMPLEMENT_ALLOCATOR(s) \
1654
- static VALUE s##_s_alloc(VALUE self){ \
1655
- s *info; \
1656
- VALUE obj = Data_Make_Struct(self, s, 0, -1, info); \
1657
- return obj; \
1658
- } \
1659
- static VALUE c##s##_to_s(VALUE self){ \
1660
- return rb_str_new(DATA_PTR(self), sizeof(s)); \
1661
- } \
1662
- static VALUE c##s##_members(VALUE self){ \
1663
- return a##s##_members; \
1664
- }
1665
-
1666
- #define PKCS11_IMPLEMENT_STRUCT_WITH_ALLOCATOR(s) \
1667
- static VALUE c##s;\
1668
- static VALUE a##s##_members;\
1669
- PKCS11_IMPLEMENT_ALLOCATOR(s);
1670
-
1671
- #define PKCS11_IMPLEMENT_STRING_ACCESSOR(s, f) \
1672
- static VALUE c##s##_get_##f(VALUE o){ \
1673
- return get_string(o, OFFSET_OF(s, f), SIZE_OF(s, f)); \
1674
- } \
1675
- static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
1676
- return set_string(o, v, OFFSET_OF(s, f), SIZE_OF(s, f)); \
1677
- }
1678
-
1679
- #define PKCS11_IMPLEMENT_ULONG_ACCESSOR(s, f) \
1680
- static VALUE c##s##_get_##f(VALUE o){ \
1681
- return get_ulong(o, OFFSET_OF(s, f)); \
1682
- } \
1683
- static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
1684
- return set_ulong(o, v, OFFSET_OF(s, f)); \
1685
- }
1686
-
1687
- #define PKCS11_IMPLEMENT_BYTE_ACCESSOR(s, f) \
1688
- static VALUE c##s##_get_##f(VALUE o){ \
1689
- return get_byte(o, OFFSET_OF(s, f)); \
1690
- } \
1691
- static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
1692
- return set_byte(o, v, OFFSET_OF(s, f)); \
1693
- }
1694
-
1695
- #define PKCS11_IMPLEMENT_ULONG_PTR_ACCESSOR(s, f) \
1696
- static VALUE c##s##_get_##f(VALUE o){ \
1697
- return get_ulong_ptr(o, OFFSET_OF(s, f)); \
1698
- } \
1699
- static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
1700
- return set_ulong_ptr(o, v, #f, OFFSET_OF(s, f)); \
1701
- }
1702
-
1703
- #define PKCS11_IMPLEMENT_HANDLE_ACCESSOR(s, f) \
1704
- static VALUE c##s##_get_##f(VALUE o){ \
1705
- return get_handle(o, OFFSET_OF(s, f)); \
1706
- } \
1707
- static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
1708
- return set_handle(o, v, OFFSET_OF(s, f)); \
1709
- }
1710
-
1711
- #define PKCS11_IMPLEMENT_BOOL_ACCESSOR(s, f) \
1712
- static VALUE c##s##_get_##f(VALUE o){ \
1713
- return get_bool(o, OFFSET_OF(s, f)); \
1714
- } \
1715
- static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
1716
- return set_bool(o, v, OFFSET_OF(s, f)); \
1717
- }
1718
-
1719
- #define PKCS11_IMPLEMENT_STRING_PTR_ACCESSOR(s, f) \
1720
- static VALUE c##s##_get_##f(VALUE o){ \
1721
- return get_string_ptr(o, #f, OFFSET_OF(s, f)); \
1722
- } \
1723
- static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
1724
- return set_string_ptr(o, v, #f, OFFSET_OF(s, f)); \
1725
- }
1726
-
1727
- #define PKCS11_IMPLEMENT_STRING_PTR_LEN_ACCESSOR(s, f, l) \
1728
- static VALUE c##s##_get_##f(VALUE o){ \
1729
- return get_string_ptr_len(o, #f, OFFSET_OF(s, f), OFFSET_OF(s, l)); \
1730
- } \
1731
- static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
1732
- return set_string_ptr_len(o, v, #f, OFFSET_OF(s, f), OFFSET_OF(s, l)); \
1733
- }
1734
-
1735
- #define PKCS11_IMPLEMENT_STRUCT_ACCESSOR(s, k, f) \
1736
- static VALUE c##s##_get_##f(VALUE o){ \
1737
- return get_struct_inline(o, c##k, #f, OFFSET_OF(s, f)); \
1738
- } \
1739
- static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
1740
- return set_struct_inline(o, c##k, #k, v, #f, OFFSET_OF(s, f), sizeof(k)); \
1741
- }
1742
-
1743
- #define PKCS11_IMPLEMENT_STRUCT_PTR_ACCESSOR(s, k, f) \
1744
- static VALUE c##s##_get_##f(VALUE o){ \
1745
- return get_struct_ptr(o, c##k, #f, OFFSET_OF(s, f)); \
1746
- } \
1747
- static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
1748
- return set_struct_ptr(o, c##k, #k, v, #f, OFFSET_OF(s, f)); \
1749
- }
1750
-
1751
- ///////////////////////////////////////
1752
-
1753
1448
  #include "pk11_struct_impl.inc"
1754
1449
 
1755
1450
  ///////////////////////////////////////
@@ -1823,28 +1518,6 @@ cCK_MECHANISM_set_pParameter(VALUE self, VALUE value)
1823
1518
  return value;
1824
1519
  }
1825
1520
 
1826
- ///////////////////////////////////////
1827
-
1828
- #define PKCS11_DEFINE_METHOD(name, args) \
1829
- rb_define_method(cPKCS11, #name, pkcs11_##name, args);
1830
-
1831
- #define PKCS11_DEFINE_STRUCT(s) \
1832
- do { \
1833
- c##s = rb_define_class_under(mPKCS11, #s, cCStruct); \
1834
- a##s##_members = rb_ary_new(); \
1835
- rb_define_alloc_func(c##s, s##_s_alloc); \
1836
- rb_define_const(c##s, "SIZEOF_STRUCT", ULONG2NUM(sizeof(s))); \
1837
- rb_define_method(c##s, "to_s", c##s##_to_s, 0); \
1838
- rb_define_method(c##s, "members", c##s##_members, 0); \
1839
- rb_iv_set(c##s, "members", a##s##_members); \
1840
- } while(0)
1841
-
1842
- #define PKCS11_DEFINE_MEMBER(s, f) \
1843
- do { \
1844
- rb_define_method(c##s, #f, c##s##_get_##f, 0); \
1845
- rb_define_method(c##s, #f "=", c##s##_set_##f, 1); \
1846
- rb_ary_push(a##s##_members, rb_str_new2(#f)); \
1847
- } while(0)
1848
1521
 
1849
1522
  void
1850
1523
  Init_pkcs11_ext()