pkcs11 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,94 @@
1
+ require "test/unit"
2
+ require "pkcs11"
3
+ require "test/helper"
4
+ require "openssl"
5
+
6
+ class TestPkcs11Object < Test::Unit::TestCase
7
+ include PKCS11
8
+
9
+ attr_reader :slots
10
+ attr_reader :slot
11
+ attr_reader :session
12
+ attr_reader :object
13
+
14
+ def setup
15
+ $pkcs11 ||= open_softokn
16
+ @slots = pk.active_slots
17
+ @slot = slots.last
18
+
19
+ flags = CKF_SERIAL_SESSION #| CKF_RW_SESSION
20
+ @session = slot.C_OpenSession(flags)
21
+ @session.login(:USER, "")
22
+
23
+ # Create session object for tests.
24
+ @object = session.create_object(
25
+ :CLASS=>CKO_DATA,
26
+ :TOKEN=>false,
27
+ :APPLICATION=>'My Application',
28
+ :VALUE=>'value')
29
+ end
30
+
31
+ def teardown
32
+ @session.logout
33
+ @session.close
34
+ end
35
+
36
+ def pk
37
+ $pkcs11
38
+ end
39
+
40
+ def test_attributes
41
+ assert_equal 1, object.attributes(:VALUE).length, 'There should be one resulting attribute'
42
+ assert_equal CK_ATTRIBUTE, object.attributes(:VALUE).first.class, 'Resulting attribute should be type CK_ATTRIBUTE'
43
+ assert_equal CKO_DATA, object.attributes(:CLASS).first.value, 'Resulting attribute should be Integer value CKO_DATA'
44
+ assert_equal 3, object.attributes(:VALUE, :TOKEN, :PRIVATE).length, 'An object should have some attributes'
45
+ assert_equal 3, object.attributes([:VALUE, :TOKEN, :APPLICATION]).length, 'Another way to retieve attributes'
46
+ assert_equal 2, object.attributes(:VALUE=>nil, :TOKEN=>nil).length, 'Third way to retieve attributes'
47
+
48
+ # The C language way to retrieve the attribute values:
49
+ template = [
50
+ CK_ATTRIBUTE.new(CKA_VALUE, nil),
51
+ ]
52
+ attrs = pk.C_GetAttributeValue(session, object, template)
53
+ attrs.each do |attr|
54
+ assert attr.value, 'There should be a value to the object'
55
+ end
56
+
57
+ assert object.attributes.length>=4, 'There should be at least the 4 stored attributes readable'
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
+ end
64
+
65
+ def test_attribute
66
+ attr = object.attributes(:CLASS).first
67
+ assert attr.inspect =~ /CLASS/, 'The attribute should tell about it\'s type'
68
+ assert attr.inspect =~ /#{CKO_DATA}/, 'The attribute should tell about it\'s type'
69
+ end
70
+
71
+ def test_set_attribute
72
+ object[:VALUE] = 'value2'
73
+ assert_equal 'value2', object[:VALUE], "Value should have changed"
74
+ end
75
+
76
+ def test_set_attributes
77
+ object.attributes = {:VALUE => 'value2', PKCS11::CKA_APPLICATION => 'app2'}
78
+
79
+ assert_equal 'value2', object[:VALUE], "Value should have changed"
80
+ assert_equal 'app2', object[:APPLICATION], "App should have changed"
81
+ end
82
+
83
+ def test_size
84
+ assert object.size, 'There should be an object size'
85
+ end
86
+
87
+ def test_destroy
88
+ object.destroy
89
+
90
+ assert_raise(PKCS11::Error, 'destroyed object shouldn\'t have any attributes') do
91
+ object[:VALUE]
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,97 @@
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
+ end
@@ -0,0 +1,66 @@
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
+ [
30
+ sinfo.slotDescription, sinfo.manufacturerID, sinfo.flags,
31
+ sinfo.hardwareVersion, sinfo.firmwareVersion
32
+ ]
33
+ end
34
+
35
+ def test_token_info
36
+ ti = slot.token_info
37
+ assert ti.inspect =~ /serialNumber=/, 'Token info should contain a serialNumber'
38
+ end
39
+
40
+ def test_mechanisms
41
+ assert_equal false, slot.mechanisms.empty?, 'There should be some mechanisms'
42
+ slot.mechanisms.each do |m|
43
+ info = slot.mechanism_info(m)
44
+ assert_equal CK_MECHANISM_INFO, info.class, 'Mechanism info should a CK_MECHANISM_INFO'
45
+ assert info.inspect =~ /ulMaxKeySize=/, 'Mechanism info should tell about max key size'
46
+ end
47
+ end
48
+
49
+ def test_session
50
+ flags = CKF_SERIAL_SESSION #| CKF_RW_SESSION
51
+ session = slot.open(flags){|_session|
52
+ assert _session.info.inspect =~ /state=/, 'Session info should tell about it\'s state'
53
+ }
54
+
55
+ session = slot.open(flags)
56
+ assert session.info.inspect =~ /flags=/, 'Session info should tell about it\'s flags'
57
+ session.close
58
+ end
59
+
60
+ def test_session2
61
+ flags = CKF_SERIAL_SESSION #| CKF_RW_SESSION
62
+ session = slot.open(flags)
63
+ slot.close_all_sessions
64
+ end
65
+ end
66
+
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pkcs11
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ryosuke Kutsuna
8
+ - GOTOU Yuuzou
9
+ - Lars Kanis
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+
14
+ date: 2010-05-27 00:00:00 +02:00
15
+ default_executable:
16
+ dependencies:
17
+ - !ruby/object:Gem::Dependency
18
+ name: rubyforge
19
+ type: :development
20
+ version_requirement:
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: 2.0.4
26
+ version:
27
+ - !ruby/object:Gem::Dependency
28
+ name: hoe
29
+ type: :development
30
+ version_requirement:
31
+ version_requirements: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: 2.6.0
36
+ version:
37
+ description: ""
38
+ email:
39
+ - ryosuke@deer-n-horse.jp
40
+ - gotoyuzo@notwork.org
41
+ - kanis@comcard.de
42
+ executables: []
43
+
44
+ extensions:
45
+ - ext/extconf.rb
46
+ extra_rdoc_files:
47
+ - History.txt
48
+ - Manifest.txt
49
+ - README.rdoc
50
+ - ext/pk11.c
51
+ files:
52
+ - .autotest
53
+ - History.txt
54
+ - MIT-LICENSE
55
+ - Manifest.txt
56
+ - README.rdoc
57
+ - Rakefile
58
+ - ext/extconf.rb
59
+ - ext/include/cryptoki.h
60
+ - ext/include/ct-kip.h
61
+ - ext/include/otp-pkcs11.h
62
+ - ext/include/pkcs-11v2-20a3.h
63
+ - ext/include/pkcs11.h
64
+ - ext/include/pkcs11f.h
65
+ - ext/include/pkcs11t.h
66
+ - ext/pk11.c
67
+ - ext/pk11.h
68
+ - ext/pk11_const.c
69
+ - lib/pkcs11.rb
70
+ - lib/pkcs11/extensions.rb
71
+ - lib/pkcs11/library.rb
72
+ - lib/pkcs11/object.rb
73
+ - lib/pkcs11/session.rb
74
+ - lib/pkcs11/slot.rb
75
+ - sample/firefox_certs.rb
76
+ - sample/nssckbi.rb
77
+ - test/fixtures/softokn/cert8.db
78
+ - test/fixtures/softokn/key3.db
79
+ - test/fixtures/softokn/secmod.db
80
+ - test/helper.rb
81
+ - test/test_pkcs11.rb
82
+ - test/test_pkcs11_crypt.rb
83
+ - test/test_pkcs11_object.rb
84
+ - test/test_pkcs11_session.rb
85
+ - test/test_pkcs11_slot.rb
86
+ has_rdoc: true
87
+ homepage: http://github.com/larskanis/pkcs11
88
+ licenses: []
89
+
90
+ post_install_message:
91
+ rdoc_options:
92
+ - --main
93
+ - README.rdoc
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: "0"
101
+ version:
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: "0"
107
+ version:
108
+ requirements: []
109
+
110
+ rubyforge_project: pkcs11
111
+ rubygems_version: 1.3.5
112
+ signing_key:
113
+ specification_version: 3
114
+ summary: ""
115
+ test_files:
116
+ - test/test_pkcs11.rb
117
+ - test/test_pkcs11_session.rb
118
+ - test/test_pkcs11_object.rb
119
+ - test/test_pkcs11_crypt.rb
120
+ - test/test_pkcs11_slot.rb