pkcs11 0.2.4-x64-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. data.tar.gz.sig +0 -0
  2. data/.autotest +23 -0
  3. data/.gemtest +0 -0
  4. data/.yardopts +1 -0
  5. data/History.txt +57 -0
  6. data/MIT-LICENSE +22 -0
  7. data/Manifest.txt +57 -0
  8. data/README.rdoc +205 -0
  9. data/Rakefile +111 -0
  10. data/ext/extconf.rb +7 -0
  11. data/ext/generate_constants.rb +57 -0
  12. data/ext/generate_structs.rb +206 -0
  13. data/ext/generate_thread_funcs.rb +72 -0
  14. data/ext/include/cryptoki.h +66 -0
  15. data/ext/include/ct-kip.h +50 -0
  16. data/ext/include/otp-pkcs11.h +125 -0
  17. data/ext/include/pkcs-11v2-20a3.h +124 -0
  18. data/ext/include/pkcs11.h +299 -0
  19. data/ext/include/pkcs11f.h +912 -0
  20. data/ext/include/pkcs11t.h +1885 -0
  21. data/ext/pk11.c +1675 -0
  22. data/ext/pk11.h +81 -0
  23. data/ext/pk11_const.c +205 -0
  24. data/ext/pk11_const_def.inc +452 -0
  25. data/ext/pk11_const_macros.h +38 -0
  26. data/ext/pk11_struct.doc +792 -0
  27. data/ext/pk11_struct_def.inc +302 -0
  28. data/ext/pk11_struct_impl.inc +302 -0
  29. data/ext/pk11_struct_macros.h +435 -0
  30. data/ext/pk11_thread_funcs.c +411 -0
  31. data/ext/pk11_thread_funcs.h +482 -0
  32. data/ext/pk11_version.h +6 -0
  33. data/lib/2.0/pkcs11_ext.so +0 -0
  34. data/lib/pkcs11.rb +9 -0
  35. data/lib/pkcs11/extensions.rb +68 -0
  36. data/lib/pkcs11/helper.rb +144 -0
  37. data/lib/pkcs11/library.rb +140 -0
  38. data/lib/pkcs11/object.rb +171 -0
  39. data/lib/pkcs11/session.rb +765 -0
  40. data/lib/pkcs11/slot.rb +102 -0
  41. data/pkcs11_protect_server/Manifest.txt +14 -0
  42. data/pkcs11_protect_server/README_PROTECT_SERVER.rdoc +89 -0
  43. data/test/fixtures/softokn/cert8.db +0 -0
  44. data/test/fixtures/softokn/key3.db +0 -0
  45. data/test/fixtures/softokn/secmod.db +0 -0
  46. data/test/helper.rb +58 -0
  47. data/test/test_pkcs11.rb +71 -0
  48. data/test/test_pkcs11_crypt.rb +220 -0
  49. data/test/test_pkcs11_object.rb +122 -0
  50. data/test/test_pkcs11_session.rb +123 -0
  51. data/test/test_pkcs11_slot.rb +78 -0
  52. data/test/test_pkcs11_structs.rb +166 -0
  53. data/test/test_pkcs11_thread.rb +44 -0
  54. metadata +213 -0
  55. metadata.gz.sig +0 -0
@@ -0,0 +1,122 @@
1
+ require "test/unit"
2
+ require "pkcs11"
3
+ require "test/helper"
4
+
5
+ class TestPkcs11Object < Test::Unit::TestCase
6
+ include PKCS11
7
+
8
+ attr_reader :slots
9
+ attr_reader :slot
10
+ attr_reader :session
11
+ attr_reader :object
12
+
13
+ def setup
14
+ $pkcs11 ||= open_softokn
15
+ @slots = pk.active_slots
16
+ @slot = slots.last
17
+
18
+ flags = CKF_SERIAL_SESSION #| CKF_RW_SESSION
19
+ @session = slot.C_OpenSession(flags)
20
+ # @session.login(:USER, "")
21
+
22
+ # Create session object for tests.
23
+ @object = session.create_object(
24
+ :CLASS=>CKO_DATA,
25
+ :TOKEN=>false,
26
+ :APPLICATION=>'My Application',
27
+ :VALUE=>'value')
28
+ end
29
+
30
+ def teardown
31
+ # @session.logout
32
+ @session.close
33
+ end
34
+
35
+ def pk
36
+ $pkcs11
37
+ end
38
+
39
+ def test_attributes
40
+ assert_equal 1, object.attributes(:VALUE).length, 'There should be one resulting attribute'
41
+ assert_equal CK_ATTRIBUTE, object.attributes(:VALUE).first.class, 'Resulting attribute should be type CK_ATTRIBUTE'
42
+ assert_equal CKO_DATA, object.attributes(:CLASS).first.value, 'Resulting attribute should be Integer value CKO_DATA'
43
+ assert_equal 3, object.attributes(:VALUE, :TOKEN, :PRIVATE).length, 'An object should have some attributes'
44
+ assert_equal 3, object.attributes([:VALUE, :TOKEN, :APPLICATION]).length, 'Another way to retieve attributes'
45
+ assert_equal 2, object.attributes(:VALUE=>nil, :TOKEN=>nil).length, 'Third way to retieve attributes'
46
+
47
+ # The C language way to retrieve the attribute values:
48
+ template = [
49
+ CK_ATTRIBUTE.new(CKA_VALUE, nil),
50
+ ]
51
+ attrs = pk.C_GetAttributeValue(session, object, template)
52
+ attrs.each do |attr|
53
+ assert attr.value, 'There should be a value to the object'
54
+ end
55
+
56
+ assert object.attributes.length>=4, 'There should be at least the 4 stored attributes readable'
57
+ assert_not_nil object.attributes.find{|a| a.type==CKA_CLASS}, 'CKA_CLASS should be returned for Object#attributes'
58
+ end
59
+
60
+ def test_accessor
61
+ assert_equal 'value', object[:VALUE], "Value should be readable"
62
+ assert_equal CKO_DATA, object[:CLASS], "Class should be readable"
63
+ assert_equal ['value', CKO_DATA], object[:VALUE, :CLASS], "multiple values should be readable"
64
+ assert_equal ['value', CKO_DATA], object[[:VALUE, :CLASS]], "multiple values should be readable"
65
+ assert_equal [], object[[]], "multiple values should be readable"
66
+ end
67
+
68
+ def test_attribute
69
+ attr = object.attributes(:CLASS).first
70
+ assert attr.inspect =~ /CLASS/, 'The attribute should tell about it\'s type'
71
+ assert attr.inspect =~ /#{CKO_DATA}/, 'The attribute should tell about it\'s type'
72
+ end
73
+
74
+ def test_set_attribute
75
+ object[:VALUE] = 'value2'
76
+ assert_equal 'value2', object[:VALUE], "Value should have changed"
77
+
78
+ object[:VALUE] = ['value3']
79
+ assert_equal 'value3', object[:VALUE], "Value should have changed"
80
+ end
81
+
82
+ def test_set_attributes
83
+ object.attributes = {:VALUE => 'value4', PKCS11::CKA_APPLICATION => 'app4'}
84
+ assert_equal 'value4', object[:VALUE], "Value should have changed"
85
+ assert_equal 'app4', object[:APPLICATION], "App should have changed"
86
+
87
+ object[:VALUE, PKCS11::CKA_APPLICATION] = 'value5', 'app5'
88
+ assert_equal 'value5', object[:VALUE], "Value should have changed"
89
+ assert_equal 'app5', object[:APPLICATION], "App should have changed"
90
+ assert_raise(ArgumentError) do
91
+ object[:VALUE, PKCS11::CKA_APPLICATION, :CLASS] = 'value5', 'app5'
92
+ end
93
+
94
+ assert_nothing_raised{ object[] = [] }
95
+ end
96
+
97
+ def test_size
98
+ assert object.size, 'There should be an object size'
99
+ end
100
+
101
+ def test_copy_without_params
102
+ new_obj = object.copy
103
+ new_obj[:APPLICATION] = 'Copied object'
104
+ assert_equal 'Copied object', new_obj[:APPLICATION], "Application should be changed"
105
+ assert_equal 'My Application', object[:APPLICATION], "Original object should be unchanged"
106
+ end
107
+
108
+ def test_copy_with_params
109
+ new_obj = object.copy :APPLICATION=>'Copied object'
110
+ assert_equal 'value', new_obj[:VALUE], "Value should be copied"
111
+ assert_equal 'Copied object', new_obj[:APPLICATION], "Application should be changed"
112
+ assert_equal 'My Application', object[:APPLICATION], "Original object should be unchanged"
113
+ end
114
+
115
+ def test_destroy
116
+ object.destroy
117
+
118
+ assert_raise(CKR_OBJECT_HANDLE_INVALID, 'destroyed object shouldn\'t have any attributes') do
119
+ object[:VALUE]
120
+ end
121
+ end
122
+ end
@@ -0,0 +1,123 @@
1
+ require "test/unit"
2
+ require "pkcs11"
3
+ require "test/helper"
4
+ require "openssl"
5
+
6
+ class TestPkcs11Session < Test::Unit::TestCase
7
+ include PKCS11
8
+
9
+ attr_reader :slots
10
+ attr_reader :slot
11
+ attr_reader :session
12
+
13
+ TestCert_ID = "\230Z\275=\2614\236\337\fY\017Y\346\202\212\v\025\335\0239"
14
+
15
+ def setup
16
+ $pkcs11 ||= open_softokn
17
+ @slots = pk.active_slots
18
+ @slot = slots.last
19
+
20
+ flags = CKF_SERIAL_SESSION #| CKF_RW_SESSION
21
+ @session = slot.C_OpenSession(flags)
22
+ # @session.login(:USER, "")
23
+ end
24
+
25
+ def teardown
26
+ # @session.logout
27
+ @session.close
28
+ end
29
+
30
+ def pk
31
+ $pkcs11
32
+ end
33
+
34
+ def test_find_objects
35
+ obj = session.find_objects(:CLASS => CKO_CERTIFICATE)
36
+ assert obj.length>2, 'There should be some certificates in the test database'
37
+ assert_equal PKCS11::Object, obj.first.class, 'Retuned objects should be class Object'
38
+
39
+ session.find_objects(:CLASS => CKO_CERTIFICATE) do |obj2|
40
+ assert obj2[:SUBJECT], 'A certificate should have a subject'
41
+ assert OpenSSL::X509::Name.new(obj2[:SUBJECT]).to_s =~ /\/CN=/i, 'Every certificate should have a CN in the subject'
42
+ end
43
+ end
44
+
45
+ def test_random
46
+ session.seed_random('some entropy')
47
+ rnd1 = session.generate_random(13)
48
+ assert_equal rnd1.length, 13, 'expected length'
49
+ rnd2 = session.generate_random(13)
50
+ assert_equal rnd2.length, 13, 'expected length'
51
+ assert_not_equal rnd1, rnd2, 'Two random blocks should be different'
52
+ end
53
+
54
+ def test_session_info
55
+ info = session.info
56
+ assert info.inspect =~ /flags=/, 'Session info should have a flag attribute'
57
+ end
58
+
59
+ def test_create_data_object
60
+ obj = session.create_object(
61
+ :CLASS=>CKO_DATA,
62
+ :TOKEN=>false,
63
+ :APPLICATION=>'My Application',
64
+ :VALUE=>'value')
65
+ end
66
+
67
+ def test_create_certificate_object
68
+ obj1 = session.find_objects(:CLASS => CKO_CERTIFICATE, :ID=>TestCert_ID).first
69
+
70
+ obj = session.create_object(
71
+ :CLASS=>CKO_CERTIFICATE,
72
+ :SUBJECT=>obj1[:SUBJECT],
73
+ :TOKEN=>false,
74
+ :LABEL=>'test_create_object',
75
+ :CERTIFICATE_TYPE=>CKC_X_509,
76
+ :ISSUER=>obj1[:ISSUER],
77
+ :VALUE=>obj1[:VALUE],
78
+ :SERIAL_NUMBER=>'12345'
79
+ )
80
+
81
+ assert_equal '12345', obj[:SERIAL_NUMBER], 'Value as created'
82
+ end
83
+
84
+ def test_create_public_key_object
85
+ rsa = OpenSSL::PKey::RSA.generate(512)
86
+
87
+ obj = session.create_object(
88
+ :CLASS=>CKO_PUBLIC_KEY,
89
+ :KEY_TYPE=>CKK_RSA,
90
+ :TOKEN=>false,
91
+ :MODULUS=>rsa.n.to_s(2),
92
+ :PUBLIC_EXPONENT=>rsa.e.to_s(2),
93
+ :LABEL=>'test_create_public_key_object')
94
+
95
+ assert_equal 'test_create_public_key_object', obj[:LABEL], 'Value as created'
96
+ end
97
+
98
+ def test_get_set_operation_state
99
+ plaintext = "secret text"
100
+
101
+ # Start a digest operation
102
+ session.C_DigestInit(:SHA_1)
103
+ session.C_DigestUpdate(plaintext[0..3])
104
+
105
+ # Save the current state and close the session
106
+ state = session.get_operation_state
107
+ @session.close
108
+
109
+ assert state.length >= 4, 'There should be at least some bytes for the first part of plaintext in the state'
110
+
111
+ # Open a new session and restore the previous state
112
+ @session = @slot.open
113
+ session.login(:USER, "")
114
+ session.set_operation_state(state)
115
+
116
+ # Finish the digest
117
+ session.C_DigestUpdate(plaintext[4..-1])
118
+ digest1 = session.C_DigestFinal
119
+ digest2 = OpenSSL::Digest::SHA1.new(plaintext).digest
120
+
121
+ assert_equal digest2, digest1, 'Digests should be equal'
122
+ end
123
+ end
@@ -0,0 +1,78 @@
1
+ require "test/unit"
2
+ require "pkcs11"
3
+ require "test/helper"
4
+
5
+ class TestPkcs11Slot < Test::Unit::TestCase
6
+ include PKCS11
7
+
8
+ attr_reader :slots
9
+ attr_reader :slot
10
+
11
+ def setup
12
+ $pkcs11 ||= open_softokn
13
+ @slots = pk.active_slots
14
+ @slot = slots.last
15
+ end
16
+
17
+ def teardown
18
+ end
19
+
20
+ def pk
21
+ $pkcs11
22
+ end
23
+
24
+ def test_info
25
+ sinfo = slot.info
26
+
27
+ assert sinfo.inspect =~ /manufacturerID=/, 'Slot info should tell about manufacturerID'
28
+
29
+ assert_equal Fixnum, sinfo.flags.class
30
+ assert sinfo.manufacturerID =~ /Mozilla/i, "It's the mozilla libaray we test against"
31
+ assert sinfo.slotDescription =~ /Private Key/i, "It's the slot with users private keys"
32
+ assert_equal Fixnum, sinfo.hardwareVersion.major.class, "Version should be a number"
33
+ assert_equal Fixnum, sinfo.hardwareVersion.minor.class, "Version should be a number"
34
+ assert_equal Fixnum, sinfo.firmwareVersion.major.class, "Version should be a number"
35
+ assert_equal Fixnum, sinfo.firmwareVersion.minor.class, "Version should be a number"
36
+ end
37
+
38
+ def test_token_info
39
+ ti = slot.token_info
40
+ assert ti.inspect =~ /serialNumber=/, 'Token info should contain a serialNumber'
41
+ end
42
+
43
+ def test_mechanisms
44
+ assert_equal false, slot.mechanisms.empty?, 'There should be some mechanisms'
45
+ slot.mechanisms.each do |m|
46
+ info = slot.mechanism_info(m)
47
+ assert_equal CK_MECHANISM_INFO, info.class, 'Mechanism info should get a CK_MECHANISM_INFO'
48
+ assert info.inspect =~ /ulMaxKeySize=/, 'Mechanism info should tell about max key size'
49
+ end
50
+ end
51
+
52
+ def test_mechanism_info
53
+ info1 = slot.mechanism_info(:DES3_CBC)
54
+ assert_equal CK_MECHANISM_INFO, info1.class, 'Mechanism info should get a CK_MECHANISM_INFO'
55
+ assert info1.inspect =~ /ulMinKeySize=/, 'Mechanism info should tell about min key size'
56
+
57
+ info2 = slot.mechanism_info(CKM_DES3_CBC)
58
+ assert_equal info1.to_hash, info2.to_hash, 'Mechanism infos should be equal'
59
+ end
60
+
61
+ def test_session
62
+ flags = CKF_SERIAL_SESSION #| CKF_RW_SESSION
63
+ session = slot.open(flags){|_session|
64
+ assert _session.info.inspect =~ /state=/, 'Session info should tell about it\'s state'
65
+ }
66
+
67
+ session = slot.open(flags)
68
+ assert session.info.inspect =~ /flags=/, 'Session info should tell about it\'s flags'
69
+ session.close
70
+ end
71
+
72
+ def test_session2
73
+ flags = CKF_SERIAL_SESSION #| CKF_RW_SESSION
74
+ session = slot.open(flags)
75
+ slot.close_all_sessions
76
+ end
77
+ end
78
+
@@ -0,0 +1,166 @@
1
+ require "test/unit"
2
+ require "pkcs11"
3
+ require "test/helper"
4
+
5
+ class TestPkcs11Structs < Test::Unit::TestCase
6
+ include PKCS11
7
+
8
+ def setup
9
+ end
10
+
11
+ def teardown
12
+ end
13
+
14
+ def test_STRING_ACCESSOR
15
+ s = CK_DATE.new
16
+ assert_equal "\0\0", s.day
17
+ assert_equal "\0\0\0\0", s.year
18
+ s.day = "12345"
19
+ assert_equal "12", s.day
20
+ s.day = "9"
21
+ assert_equal "9\0", s.day
22
+ assert_raise(TypeError){ s.day = nil }
23
+ end
24
+
25
+ def test_ULONG_ACCESSOR
26
+ s = CK_SSL3_KEY_MAT_PARAMS.new
27
+ assert_equal 0, s.ulIVSizeInBits
28
+ s.ulIVSizeInBits = 1234567890
29
+ assert_equal 1234567890, s.ulIVSizeInBits
30
+ s.ulIVSizeInBits = 2345678901
31
+ assert_equal 2345678901, s.ulIVSizeInBits
32
+ assert_raise(TypeError){ s.ulIVSizeInBits = nil }
33
+ end
34
+
35
+ def test_BOOL_ACCESSOR
36
+ s = CK_SSL3_KEY_MAT_PARAMS.new
37
+ assert_equal false, s.bIsExport
38
+ s.bIsExport = true
39
+ assert_equal true, s.bIsExport
40
+ s.bIsExport = false
41
+ assert_equal false, s.bIsExport
42
+ assert_raise(ArgumentError){ s.bIsExport = nil }
43
+ end
44
+
45
+ def test_STRING_PTR_ACCESSOR
46
+ s = CK_WTLS_MASTER_KEY_DERIVE_PARAMS.new
47
+ assert_nil s.pVersion
48
+ s.pVersion = "1.2.3"
49
+ assert_equal "1.2.3", s.pVersion
50
+ s.pVersion = nil
51
+ assert_nil s.pVersion
52
+ end
53
+
54
+ def test_STRUCT_ACCESSOR
55
+ s = CK_SSL3_KEY_MAT_PARAMS.new
56
+ ri = s.RandomInfo
57
+ ro = s.RandomInfo
58
+ assert_nil ri.pClientRandom
59
+ assert_nil ro.pServerRandom
60
+ GC.start
61
+ ri.pServerRandom = 'serv'
62
+ ro.pClientRandom = 'client'
63
+ GC.start
64
+ assert_equal 'client', ri.pClientRandom
65
+ assert_equal 'serv', ro.pServerRandom
66
+
67
+ ro = CK_SSL3_RANDOM_DATA.new
68
+ ro.pClientRandom = 'clrnd'
69
+ s.RandomInfo = ro
70
+ assert_equal 'clrnd', ri.pClientRandom
71
+ assert_nil ri.pServerRandom
72
+
73
+ assert_raise(ArgumentError){ s.RandomInfo = nil }
74
+ end
75
+
76
+ def test_gc_STRUCT_ACCESSOR
77
+ ri = CK_SSL3_KEY_MAT_PARAMS.new.RandomInfo
78
+ ro = CK_SSL3_KEY_MAT_PARAMS.new.RandomInfo
79
+ ri.pServerRandom = 'serv'
80
+ ro.pServerRandom = '_serv'
81
+ GC.start
82
+ assert_equal '_serv', ro.pServerRandom
83
+ assert_equal 'serv', ri.pServerRandom
84
+ assert_nil ro.pClientRandom
85
+ assert_nil ri.pClientRandom
86
+ end
87
+
88
+ def test_STRING_PTR_LEN_ACCESSOR
89
+ s = CK_SSL3_RANDOM_DATA.new
90
+ assert_nil s.pServerRandom
91
+ GC.start
92
+ s.pServerRandom = 'serv'
93
+ s.pClientRandom = 'client'
94
+ GC.start
95
+ assert_equal 'client', s.pClientRandom
96
+ assert_equal 'serv', s.pServerRandom
97
+ GC.start
98
+ s.pServerRandom = nil
99
+ assert_nil s.pServerRandom
100
+ end
101
+
102
+ def test_STRUCT_PTR_ACCESSOR
103
+ s = CK_SSL3_KEY_MAT_PARAMS.new
104
+ assert_nil s.pReturnedKeyMaterial
105
+ ri = s.pReturnedKeyMaterial = CK_SSL3_KEY_MAT_OUT.new
106
+ assert_nil ri.pIVClient
107
+ ri.pIVClient = 'cli'
108
+ GC.start
109
+ assert_equal 'cli', ri.pIVClient
110
+ assert_equal 'cli', s.pReturnedKeyMaterial.pIVClient
111
+ s.pReturnedKeyMaterial = nil
112
+ assert_nil s.pReturnedKeyMaterial
113
+ end
114
+
115
+ def test_ULONG_PTR_ACCESSOR
116
+ s = CK_WTLS_PRF_PARAMS.new
117
+ assert_nil s.pulOutputLen
118
+ s.pulOutputLen = 123
119
+ GC.start
120
+ assert_equal 123, s.pulOutputLen
121
+ s.pulOutputLen = nil
122
+ assert_nil s.pulOutputLen
123
+ end
124
+
125
+ def test_STRUCT_ARRAY_ACCESSOR
126
+ s = CK_OTP_PARAMS.new
127
+ assert_equal [], s.pParams
128
+ s1 = CK_OTP_PARAM.new
129
+ s1.type = CK_OTP_VALUE
130
+ s1.pValue = "\0xyz"
131
+ s2 = CK_OTP_PARAM.new
132
+ s2.type = CK_OTP_PIN
133
+ s2.pValue = "1234"
134
+ s.pParams = [s1, s2]
135
+ assert_equal [s1.to_hash, s2.to_hash], s.pParams.map{|e| e.to_hash }
136
+ GC.start
137
+ assert_raise(ArgumentError){ s.pParams = [s1, s2, nil] }
138
+ assert_equal [s1.to_hash, s2.to_hash], s.pParams.map{|e| e.to_hash }
139
+
140
+ s.pParams = []
141
+ assert_equal [], s.pParams
142
+ end
143
+
144
+ def test_CStruct
145
+ s = CK_DATE.new
146
+ s.day, s.month, s.year = "31", "12", "2010"
147
+
148
+ assert s.inspect =~ /year="2010"/, 'There should be a year in CK_DATE'
149
+ assert_equal ["year", "month", "day"], s.members, 'CK_DATE should contain some attributes'
150
+ assert_equal ["2010", "12", "31"], s.values, 'values of CK_DATE'
151
+ assert_equal( {:day=>"31", :month=>"12", :year=>"2010"}, s.to_hash, 'CK_DATE as hash' )
152
+ end
153
+
154
+ def test_bignum_attribute
155
+ bignum = [-1].pack("l_").unpack("L_")[0]
156
+ attr = CK_ATTRIBUTE.new(CKA_KEY_TYPE, bignum)
157
+ assert_equal bignum, attr.value, "The bignum value should set"
158
+ end
159
+
160
+ def test_bignum_mechanism
161
+ bignum = [-1].pack("l_").unpack("L_")[0]
162
+ mech = CK_MECHANISM.new(bignum-1, bignum)
163
+ assert_equal bignum-1, mech.mechanism, "The bignum mechanism should set"
164
+ assert_equal [-1].pack("l_"), mech.pParameter, "The bignum parameter is retrieved as String"
165
+ end
166
+ end