pkcs11 0.2.1-x86-mingw32 → 0.2.2-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
File without changes
@@ -1,3 +1,13 @@
1
+ === 0.2.2 / 2011-12-12
2
+
3
+ * add ability to change PKCS11 base methods in order to use vendor defined attributes, return codes and mechanisms
4
+ * restructured C code to avoid duplicated code in vendor extension gems
5
+ * add gem pkcs11_protect_server to cover many ProtectServer specific extensions
6
+ * update test/helper to find newer libsoftokn3
7
+ * add task for generating combined docs of PKCS11 and PKCS11::ProtectServer
8
+ * changed PKCS11#slots to return all slots of the device by default
9
+ * add ability to set/get multiple attributes with Object#[] and Object#[]=
10
+
1
11
  === 0.2.1 / 2011-04-21
2
12
 
3
13
  * add some more CK_ULONG and CK_BOOL attributes
@@ -1,4 +1,5 @@
1
1
  .autotest
2
+ .gemtest
2
3
  .yardopts
3
4
  History.txt
4
5
  MIT-LICENSE
@@ -21,6 +22,7 @@ ext/pk11.h
21
22
  ext/pk11_const.c
22
23
  ext/pk11_const_macros.h
23
24
  ext/pk11_struct_macros.h
25
+ ext/pk11_version.h
24
26
  lib/pkcs11.rb
25
27
  lib/pkcs11/extensions.rb
26
28
  lib/pkcs11/helper.rb
@@ -28,8 +30,20 @@ lib/pkcs11/library.rb
28
30
  lib/pkcs11/object.rb
29
31
  lib/pkcs11/session.rb
30
32
  lib/pkcs11/slot.rb
31
- sample/firefox_certs.rb
32
- sample/nssckbi.rb
33
+ pkcs11_protect_server/.gemtest
34
+ pkcs11_protect_server/.yardopts
35
+ pkcs11_protect_server/Manifest.txt
36
+ pkcs11_protect_server/README_PROTECT_SERVER.rdoc
37
+ pkcs11_protect_server/Rakefile
38
+ pkcs11_protect_server/ext/extconf.rb
39
+ pkcs11_protect_server/ext/generate_constants.rb
40
+ pkcs11_protect_server/ext/generate_structs.rb
41
+ pkcs11_protect_server/ext/pk11s.c
42
+ pkcs11_protect_server/lib/pkcs11_protect_server.rb
43
+ pkcs11_protect_server/lib/pkcs11_protect_server/extensions.rb
44
+ pkcs11_protect_server/test/helper.rb
45
+ pkcs11_protect_server/test/test_pkcs11_protect_server.rb
46
+ pkcs11_protect_server/test/test_pkcs11_protect_server_crypt.rb
33
47
  test/fixtures/softokn/cert8.db
34
48
  test/fixtures/softokn/key3.db
35
49
  test/fixtures/softokn/secmod.db
@@ -55,7 +55,7 @@ This opens a PKCS#11 library and prints it's information block.
55
55
  Then a {PKCS11::Session} to the first active slot of the device is opened and
56
56
  a login is done on the user account. Now, a 112 bit DES3 key is generated and
57
57
  some plaintext is encrypted with it. A 8-byte zero IV is used. In many cases method parameters
58
- can be Integer (like CKA_LABEL) or, as in the sample, Symbol (:LABEL) which is internally
58
+ can be Integer (like PKCS11::CKA_LABEL) or, as in the sample, Symbol (:LABEL) which is internally
59
59
  converted.
60
60
 
61
61
  Many more usage examples can be found in the unit tests of the <tt>test</tt>
@@ -65,8 +65,24 @@ Detail information for the API specification is provided by RSA Security Inc.
65
65
  Please refer the URL: http://www.rsa.com/rsalabs/node.asp?id=2133. Browsable HTML
66
66
  can be found at http://www.cryptsoft.com/pkcs11doc.
67
67
 
68
+ === Vendor extensions
69
+ Some vendors extend their libraries beyond the standard, in it's own way.
70
+ This can be used by vendor specific packages:
71
+ * Safenet ProtectServer: {file:pkcs11_protect_server/README_PROTECT_SERVER.rdoc}
68
72
 
69
- == Cross compiling for mswin32
73
+ === Threading
74
+
75
+ The pkcs11 binding fully supports native, background Ruby threads.
76
+ This of course only applies to Rubinius and Ruby 1.9.x or higher since
77
+ earlier versions of Ruby do not support native threads.
78
+
79
+ Calling the Cryptoki library from multiple threads simultaneously,
80
+ requires to open it with flag PKCS11::CKF_OS_LOCKING_OK.
81
+ Application-supplied synchronization primitives
82
+ (CreateMutex, DestroyMutex, LockMutex, UnlockMutex) are not supported.
83
+
84
+
85
+ == Cross compiling for Windows
70
86
 
71
87
  Using rake-compiler a cross compiled pkcs11-gem can be build on a linux host for
72
88
  the win32 platform. There are no runtime dependencies to any but the standard Windows DLLs.
@@ -85,20 +101,21 @@ Install the rake-compiler:
85
101
 
86
102
  Download and cross compile ruby for win32:
87
103
 
88
- rake-compiler cross-ruby VERSION=1.8.6-p287
104
+ rake-compiler cross-ruby VERSION=1.8.7-p352
105
+ rake-compiler cross-ruby VERSION=1.9.2-p290
89
106
 
90
- Download and cross compile pkcs11 for win32:
107
+ Download and cross compile pkcs11 for win32 (MRI 1.8+1.9 fat gem):
91
108
 
92
- rake cross native gem
109
+ rake cross native gem RUBY_CC_VERSION=1.8.7:1.9.2
93
110
 
94
- If everything works, there should be pkcs11-VERSION-x86-mswin32.gem in the pkg
111
+ If everything works, there should be pkcs11-VERSION-x86-mingw32.gem in the pkg
95
112
  directory.
96
113
 
97
114
 
98
115
  == ToDo
99
116
 
100
117
  * encoding support for Ruby 1.9
101
- * support for proprietary extensions of different vendors
118
+ * support for proprietary extensions of different vendors (done for Safenet-ProtectServer)
102
119
  * PKCS#11 v2.3
103
120
 
104
121
  == Development Status
data/Rakefile CHANGED
@@ -6,11 +6,16 @@ require 'hoe'
6
6
  require 'rake/extensiontask'
7
7
  require 'rbconfig'
8
8
 
9
- CLEAN.include 'ext/pk11_struct_def.inc'
10
- CLEAN.include 'ext/pk11_struct_impl.inc'
11
- CLEAN.include 'ext/pk11_const_def.inc'
12
- CLEAN.include 'ext/pk11_thread_funcs.h'
13
- CLEAN.include 'ext/pk11_thread_funcs.c'
9
+ GENERATED_FILES = [
10
+ 'ext/pk11_struct_impl.inc',
11
+ 'ext/pk11_struct_def.inc',
12
+ 'ext/pk11_const_def.inc',
13
+ 'ext/pk11_struct.doc',
14
+ 'ext/pk11_thread_funcs.h',
15
+ 'ext/pk11_thread_funcs.c',
16
+ ]
17
+
18
+ CLEAN.include GENERATED_FILES
14
19
  CLEAN.include 'lib/pkcs11_ext.so'
15
20
  CLEAN.include 'tmp'
16
21
 
@@ -28,43 +33,49 @@ hoe = Hoe.spec 'pkcs11' do
28
33
  self.readme_file = 'README.rdoc'
29
34
  self.extra_rdoc_files << self.readme_file << 'ext/pk11.c'
30
35
  spec_extras[:extensions] = 'ext/extconf.rb'
31
- spec_extras[:files] = File.read_utf("Manifest.txt").split(/\r?\n\r?/)
32
- spec_extras[:files] << 'ext/pk11_struct_impl.inc'
33
- spec_extras[:files] << 'ext/pk11_struct_def.inc'
34
- spec_extras[:files] << 'ext/pk11_const_def.inc'
35
- spec_extras[:files] << 'ext/pk11_thread_funcs.h'
36
- spec_extras[:files] << 'ext/pk11_thread_funcs.c'
36
+ spec_extras[:files] = File.read_utf("Manifest.txt").split(/\r?\n\r?/).reject{|f| f=~/^pkcs11_/ }
37
+ spec_extras[:files] += GENERATED_FILES
37
38
  spec_extras[:has_rdoc] = 'yard'
38
39
  end
39
40
 
40
- ENV['RUBY_CC_VERSION'] ||= '1.8.6:1.9.2'
41
+ ENV['RUBY_CC_VERSION'] ||= '1.8.7:1.9.2'
41
42
 
42
43
  Rake::ExtensionTask.new('pkcs11_ext', hoe.spec) do |ext|
43
44
  ext.ext_dir = 'ext'
44
45
  ext.cross_compile = true # enable cross compilation (requires cross compile toolchain)
45
- ext.cross_platform = ['i386-mswin32', 'i386-mingw32'] # forces the Windows platform instead of the default one
46
+ ext.cross_platform = ['i386-mingw32'] # forces the Windows platform instead of the default one
46
47
  end
47
48
 
48
49
  file 'ext/extconf.rb' => ['ext/pk11_struct_def.inc', 'ext/pk11_thread_funcs.c']
49
50
  file 'ext/pk11_struct_def.inc' => 'ext/generate_structs.rb' do
50
- sh "#{Config::CONFIG['ruby_install_name']} ext/generate_structs.rb --def ext/pk11_struct_def.inc --impl ext/pk11_struct_impl.inc --doc ext/pk11_struct.doc ext/include/pkcs11t.h"
51
+ sh "#{RbConfig::CONFIG['ruby_install_name']} ext/generate_structs.rb --def ext/pk11_struct_def.inc --impl ext/pk11_struct_impl.inc --doc ext/pk11_struct.doc ext/include/pkcs11t.h"
51
52
  end
52
53
  file 'ext/pk11_struct_impl.inc' => 'ext/pk11_struct_def.inc'
54
+ file 'ext/pk11_struct.doc' => 'ext/pk11_struct_def.inc'
55
+
53
56
  file 'ext/pk11_const_def.inc' => 'ext/generate_constants.rb' do
54
- sh "#{Config::CONFIG['ruby_install_name']} ext/generate_constants.rb --const ext/pk11_const_def.inc ext/include/pkcs11t.h"
57
+ sh "#{RbConfig::CONFIG['ruby_install_name']} ext/generate_constants.rb --const ext/pk11_const_def.inc ext/include/pkcs11t.h"
55
58
  end
56
59
  file 'ext/pk11.c' => ['ext/pk11_struct_def.inc', 'ext/pk11_struct_impl.inc', 'ext/pk11_struct_macros.h']
57
60
  file 'ext/pk11_const.c' => ['ext/pk11_const_def.inc', 'ext/pk11_const_macros.h']
58
61
 
59
62
  file 'ext/pk11_thread_funcs.h' => 'ext/generate_thread_funcs.rb' do
60
- sh "#{Config::CONFIG['ruby_install_name']} ext/generate_thread_funcs.rb --impl ext/pk11_thread_funcs.c --decl ext/pk11_thread_funcs.h ext/include/pkcs11f.h"
63
+ sh "#{RbConfig::CONFIG['ruby_install_name']} ext/generate_thread_funcs.rb --impl ext/pk11_thread_funcs.c --decl ext/pk11_thread_funcs.h ext/include/pkcs11f.h"
61
64
  end
62
65
  file 'ext/pk11_thread_funcs.c' => 'ext/pk11_thread_funcs.h'
63
66
  file 'ext/pk11.h' => 'ext/pk11_thread_funcs.h'
64
67
 
68
+ task :docs_of_vendor_extensions do
69
+ Dir['pkcs11_*'].each do |dir|
70
+ chdir(dir) do
71
+ sh "rake doc_files"
72
+ end
73
+ end
74
+ end
75
+
65
76
  desc "Generate static HTML documentation with YARD"
66
- task :yardoc do
67
- sh "yardoc"
77
+ task :yardoc=>['ext/pk11_struct.doc', :docs_of_vendor_extensions] do
78
+ sh "yardoc --title \"PKCS#11/Ruby Interface\" --no-private lib/**/*.rb ext/*.c ext/*.doc pkcs11_protect_server/lib/**/*.rb pkcs11_protect_server/ext/*.c pkcs11_protect_server/ext/*.doc - pkcs11_protect_server/README_PROTECT_SERVER.rdoc"
68
79
  end
69
80
 
70
81
  desc "Publish YARD to wherever you want."
@@ -36,7 +36,10 @@ class StructParser
36
36
  Attribute = Struct.new(:type, :name, :qual, :mark)
37
37
  IgnoreStructs = %w[CK_ATTRIBUTE CK_MECHANISM]
38
38
  OnlyAllocatorStructs = %w[CK_MECHANISM_INFO CK_C_INITIALIZE_ARGS CK_INFO CK_SLOT_INFO CK_TOKEN_INFO CK_SESSION_INFO]
39
- STRUCT_MODULE = 'PKCS11'
39
+
40
+ def struct_module
41
+ 'PKCS11'
42
+ end
40
43
 
41
44
  class CStruct
42
45
  def attr_by_sign(key)
@@ -44,6 +47,12 @@ class StructParser
44
47
  end
45
48
  end
46
49
 
50
+ class Attribute
51
+ def type_noptr
52
+ type.gsub(/_PTR$/,'')
53
+ end
54
+ end
55
+
47
56
  def parse_files(files)
48
57
  structs = []
49
58
  files.each do |file_h|
@@ -69,6 +78,8 @@ class StructParser
69
78
  write_files
70
79
  end
71
80
 
81
+ def array_attribute_names; ['pParams']; end
82
+
72
83
  def write_files
73
84
  File.open(options.def, "w") do |fd_def|
74
85
  File.open(options.impl, "w") do |fd_impl|
@@ -82,24 +93,25 @@ class StructParser
82
93
  fd_impl.puts "PKCS11_IMPLEMENT_STRUCT_WITH_ALLOCATOR(#{struct.name});"
83
94
  end
84
95
  fd_def.puts "PKCS11_DEFINE_STRUCT(#{struct.name});"
85
- fd_doc.puts"class #{STRUCT_MODULE}::#{struct.name} < #{STRUCT_MODULE}::CStruct"
96
+ fd_doc.puts"class #{struct_module}::#{struct.name} < #{struct_module}::CStruct"
86
97
  fd_doc.puts"# Size of corresponding C struct in bytes\nSIZEOF_STRUCT=Integer"
87
98
  fd_doc.puts"# @return [String] Binary copy of the C struct\ndef to_s; end"
88
99
  fd_doc.puts"# @return [Array<String>] Attributes of this struct\ndef members; end"
89
100
 
90
101
  # find attributes belonging together for array of struct
91
- struct.attrs.select{|attr| structs_by_name[attr.type.gsub(/_PTR$/,'')] }.each do |attr|
92
- if attr.name=='pParams' && (len_attr = struct.attr_by_sign("CK_ULONG ulCount"))
93
- fd_impl.puts "PKCS11_IMPLEMENT_STRUCT_PTR_ARRAY_ACCESSOR(#{struct.name}, #{attr.type.gsub(/_PTR$/,'')}, #{attr.name}, #{len_attr.name});"
102
+ struct.attrs.select{|attr| structs_by_name[attr.type_noptr] || std_structs_by_name[attr.type_noptr] }.each do |attr|
103
+ if array_attribute_names.include?(attr.name) && (len_attr = struct.attr_by_sign("CK_ULONG ulCount") || struct.attr_by_sign("CK_ULONG count") || struct.attr_by_sign("CK_ULONG #{attr.name}Count"))
104
+ std_struct = "PKCS11_" if std_structs_by_name[attr.type_noptr]
105
+ fd_impl.puts "PKCS11_IMPLEMENT_#{std_struct}STRUCT_PTR_ARRAY_ACCESSOR(#{struct.name}, #{attr.type_noptr}, #{attr.name}, #{len_attr.name});"
94
106
  fd_def.puts "PKCS11_DEFINE_MEMBER(#{struct.name}, #{attr.name});"
95
- fd_doc.puts"# @return [Array<PKCS11::#{attr.type.gsub(/_PTR$/,'')}>] accessor for #{attr.name} and #{len_attr.name}\nattr_accessor :#{attr.name}"
107
+ fd_doc.puts"# @return [Array<PKCS11::#{attr.type_noptr}>] accessor for #{attr.name} and #{len_attr.name}\nattr_accessor :#{attr.name}"
96
108
  len_attr.mark = true
97
109
  attr.mark = true
98
110
  end
99
111
  end
100
112
  # find string attributes belonging together
101
113
  struct.attrs.select{|attr| ['CK_BYTE_PTR', 'CK_VOID_PTR', 'CK_UTF8CHAR_PTR', 'CK_CHAR_PTR'].include?(attr.type) }.each do |attr|
102
- if len_attr=struct.attr_by_sign("CK_ULONG #{attr.name.gsub(/^p/, "ul")}Len")
114
+ if len_attr=struct.attr_by_sign("CK_ULONG #{attr.name.gsub(/^p([A-Z])/){ "ul"+$1 }}Len")
103
115
  fd_impl.puts "PKCS11_IMPLEMENT_STRING_PTR_LEN_ACCESSOR(#{struct.name}, #{attr.name}, #{len_attr.name});"
104
116
  fd_def.puts "PKCS11_DEFINE_MEMBER(#{struct.name}, #{attr.name});"
105
117
  fd_doc.puts"# @return [String, nil] accessor for #{attr.name} and #{len_attr.name}\nattr_accessor :#{attr.name}"
@@ -136,7 +148,7 @@ class StructParser
136
148
  fd_impl.puts "PKCS11_IMPLEMENT_BYTE_ACCESSOR(#{struct.name}, #{attr.name});"
137
149
  fd_def.puts "PKCS11_DEFINE_MEMBER(#{struct.name}, #{attr.name});"
138
150
  fd_doc.puts"# @return [Integer] accessor for #{attr.name} (CK_BYTE)\nattr_accessor :#{attr.name}"
139
- when 'CK_ULONG', 'CK_FLAGS', 'CK_SLOT_ID', 'CK_STATE', 'CK_COUNT', 'CK_SIZE', /CK_[A-Z_0-9]+_TYPE/
151
+ when 'CK_ULONG', 'CK_FLAGS', 'CK_SLOT_ID', 'CK_STATE', /CK_[A-Z_0-9]+_TYPE/
140
152
  fd_impl.puts "PKCS11_IMPLEMENT_ULONG_ACCESSOR(#{struct.name}, #{attr.name});"
141
153
  fd_def.puts "PKCS11_DEFINE_MEMBER(#{struct.name}, #{attr.name});"
142
154
  fd_doc.puts"# @return [Integer] accessor for #{attr.name} (CK_ULONG)\nattr_accessor :#{attr.name}"
@@ -148,7 +160,7 @@ class StructParser
148
160
  fd_impl.puts "PKCS11_IMPLEMENT_BOOL_ACCESSOR(#{struct.name}, #{attr.name});"
149
161
  fd_def.puts "PKCS11_DEFINE_MEMBER(#{struct.name}, #{attr.name});"
150
162
  fd_doc.puts"# @return [Boolean] Bool value\nattr_accessor :#{attr.name}"
151
- when 'CK_ULONG_PTR', 'CK_COUNT_PTR'
163
+ when 'CK_ULONG_PTR'
152
164
  fd_impl.puts "PKCS11_IMPLEMENT_ULONG_PTR_ACCESSOR(#{struct.name}, #{attr.name});"
153
165
  fd_def.puts "PKCS11_DEFINE_MEMBER(#{struct.name}, #{attr.name});"
154
166
  fd_doc.puts"# @return [Integer, nil] accessor for #{attr.name} (CK_ULONG_PTR)\nattr_accessor :#{attr.name}"
@@ -157,19 +169,19 @@ class StructParser
157
169
  if structs_by_name[attr.type]
158
170
  fd_impl.puts "PKCS11_IMPLEMENT_STRUCT_ACCESSOR(#{struct.name}, #{attr.type}, #{attr.name});"
159
171
  fd_def.puts "PKCS11_DEFINE_MEMBER(#{struct.name}, #{attr.name});"
160
- fd_doc.puts"# @return [#{STRUCT_MODULE}::#{attr.type}] inline struct\nattr_accessor :#{attr.name}"
161
- elsif (attr_noptr=attr.type.gsub(/_PTR$/,'')) && structs_by_name[attr_noptr]
162
- fd_impl.puts "PKCS11_IMPLEMENT_STRUCT_PTR_ACCESSOR(#{struct.name}, #{attr_noptr}, #{attr.name});"
172
+ fd_doc.puts"# @return [#{struct_module}::#{attr.type}] inline struct\nattr_accessor :#{attr.name}"
173
+ elsif structs_by_name[attr.type_noptr]
174
+ fd_impl.puts "PKCS11_IMPLEMENT_STRUCT_PTR_ACCESSOR(#{struct.name}, #{attr.type_noptr}, #{attr.name});"
163
175
  fd_def.puts "PKCS11_DEFINE_MEMBER(#{struct.name}, #{attr.name});"
164
- fd_doc.puts"# @return [#{STRUCT_MODULE}::#{attr_noptr}, nil] pointer to struct\nattr_accessor :#{attr.name}"
176
+ fd_doc.puts"# @return [#{struct_module}::#{attr.type_noptr}, nil] pointer to struct\nattr_accessor :#{attr.name}"
165
177
  elsif std_structs_by_name[attr.type]
166
178
  fd_impl.puts "PKCS11_IMPLEMENT_PKCS11_STRUCT_ACCESSOR(#{struct.name}, #{attr.type}, #{attr.name});"
167
179
  fd_def.puts "PKCS11_DEFINE_MEMBER(#{struct.name}, #{attr.name});"
168
180
  fd_doc.puts"# @return [PKCS11::#{attr.type}] inline struct (see pkcs11.gem)\nattr_accessor :#{attr.name}"
169
- elsif (attr_noptr=attr.type.gsub(/_PTR$/,'')) && std_structs_by_name[attr_noptr]
170
- fd_impl.puts "PKCS11_IMPLEMENT_PKCS11_STRUCT_PTR_ACCESSOR(#{struct.name}, #{attr_noptr}, #{attr.name});"
181
+ elsif std_structs_by_name[attr.type_noptr]
182
+ fd_impl.puts "PKCS11_IMPLEMENT_PKCS11_STRUCT_PTR_ACCESSOR(#{struct.name}, #{attr.type_noptr}, #{attr.name});"
171
183
  fd_def.puts "PKCS11_DEFINE_MEMBER(#{struct.name}, #{attr.name});"
172
- fd_doc.puts"# @return [PKCS11::#{attr_noptr}, nil] pointer to struct (see pkcs11.gem)\nattr_accessor :#{attr.name}"
184
+ fd_doc.puts"# @return [PKCS11::#{attr.type_noptr}, nil] pointer to struct (see pkcs11.gem)\nattr_accessor :#{attr.name}"
173
185
  else
174
186
  fd_impl.puts "/* unimplemented attr #{attr.type} #{attr.name} #{attr.qual} */"
175
187
  fd_def.puts "/* unimplemented attr #{attr.type} #{attr.name} #{attr.qual} */"
data/ext/pk11.c CHANGED
@@ -1,5 +1,6 @@
1
1
  #include "pk11.h"
2
2
  #include "pk11_struct_macros.h"
3
+ #include "pk11_version.h"
3
4
 
4
5
  #if defined(compile_for_windows)
5
6
  #include <winbase.h> /* for LoadLibrary() */
@@ -7,8 +8,6 @@
7
8
  #include <dlfcn.h>
8
9
  #endif
9
10
 
10
- static const char *VERSION = "0.2.1";
11
-
12
11
  static ID sNEW;
13
12
  static VALUE mPKCS11;
14
13
  static VALUE cPKCS11;
@@ -41,11 +40,10 @@ static VALUE aCK_MECHANISM_members;
41
40
  VALUE pkcs11_return_value_to_class(CK_RV, VALUE);
42
41
 
43
42
  static void
44
- pkcs11_raise(CK_RV rv)
43
+ pkcs11_raise(VALUE self, CK_RV rv)
45
44
  {
46
- VALUE class;
47
- class = pkcs11_return_value_to_class(rv, ePKCS11Error);
48
- rb_raise(class, "%lu", rv);
45
+ rb_funcall(self, rb_intern("vendor_raise_on_return_value"), 1, ULONG2NUM(rv));
46
+ rb_raise(ePKCS11Error, "method vendor_raise_on_return_value should never return");
49
47
  }
50
48
 
51
49
  ///////////////////////////////////////
@@ -113,7 +111,7 @@ pkcs11_C_Finalize(VALUE self)
113
111
 
114
112
  GetFunction(self, C_Finalize, func);
115
113
  CallFunction(C_Finalize, func, rv, NULL_PTR);
116
- if (rv != CKR_OK) pkcs11_raise(rv);
114
+ if (rv != CKR_OK) pkcs11_raise(self,rv);
117
115
 
118
116
  return self;
119
117
  }
@@ -208,7 +206,7 @@ pkcs11_C_GetFunctionList(VALUE self)
208
206
  if(!func) rb_raise(ePKCS11Error, "%s", dlerror());
209
207
  #endif
210
208
  CallFunction(C_GetFunctionList, func, rv, &(ctx->functions));
211
- if (rv != CKR_OK) pkcs11_raise(rv);
209
+ if (rv != CKR_OK) pkcs11_raise(self,rv);
212
210
 
213
211
  return self;
214
212
  }
@@ -234,7 +232,7 @@ pkcs11_C_Initialize(int argc, VALUE *argv, VALUE self)
234
232
  }
235
233
  GetFunction(self, C_Initialize, func);
236
234
  CallFunction(C_Initialize, func, rv, args);
237
- if (rv != CKR_OK) pkcs11_raise(rv);
235
+ if (rv != CKR_OK) pkcs11_raise(self,rv);
238
236
 
239
237
  return self;
240
238
  }
@@ -264,7 +262,7 @@ pkcs11_C_GetInfo(VALUE self)
264
262
  GetFunction(self, C_GetInfo, func);
265
263
  info = pkcs11_new_struct(cCK_INFO);
266
264
  CallFunction(C_GetInfo, func, rv, (CK_INFO_PTR)DATA_PTR(info));
267
- if (rv != CKR_OK) pkcs11_raise(rv);
265
+ if (rv != CKR_OK) pkcs11_raise(self,rv);
268
266
 
269
267
  return info;
270
268
  }
@@ -281,12 +279,12 @@ pkcs11_C_GetSlotList(VALUE self, VALUE presented)
281
279
 
282
280
  GetFunction(self, C_GetSlotList, func);
283
281
  CallFunction(C_GetSlotList, func, rv, CK_FALSE, NULL_PTR, &ulSlotCount);
284
- if (rv != CKR_OK) pkcs11_raise(rv);
282
+ if (rv != CKR_OK) pkcs11_raise(self,rv);
285
283
  pSlotList = (CK_SLOT_ID_PTR)malloc(ulSlotCount*sizeof(CK_SLOT_ID));
286
284
  CallFunction(C_GetSlotList, func, rv, RTEST(presented) ? CK_TRUE : CK_FALSE, pSlotList, &ulSlotCount);
287
285
  if (rv != CKR_OK) {
288
286
  free(pSlotList);
289
- pkcs11_raise(rv);
287
+ pkcs11_raise(self,rv);
290
288
  }
291
289
  for (i = 0; i < ulSlotCount; i++)
292
290
  rb_ary_push(ary, HANDLE2NUM(pSlotList[i]));
@@ -305,7 +303,7 @@ pkcs11_C_GetSlotInfo(VALUE self, VALUE slot_id)
305
303
  GetFunction(self, C_GetSlotInfo, func);
306
304
  info = pkcs11_new_struct(cCK_SLOT_INFO);
307
305
  CallFunction(C_GetSlotInfo, func, rv, NUM2HANDLE(slot_id), DATA_PTR(info));
308
- if (rv != CKR_OK) pkcs11_raise(rv);
306
+ if (rv != CKR_OK) pkcs11_raise(self,rv);
309
307
 
310
308
  return info;
311
309
  }
@@ -320,7 +318,7 @@ pkcs11_C_GetTokenInfo(VALUE self, VALUE slot_id)
320
318
  GetFunction(self, C_GetTokenInfo, func);
321
319
  info = pkcs11_new_struct(cCK_TOKEN_INFO);
322
320
  CallFunction(C_GetTokenInfo, func, rv, NUM2HANDLE(slot_id), DATA_PTR(info));
323
- if (rv != CKR_OK) pkcs11_raise(rv);
321
+ if (rv != CKR_OK) pkcs11_raise(self,rv);
324
322
 
325
323
  return info;
326
324
  }
@@ -338,7 +336,7 @@ pkcs11_C_GetMechanismList(VALUE self, VALUE slot_id)
338
336
  ary = rb_ary_new();
339
337
  GetFunction(self, C_GetMechanismList, func);
340
338
  CallFunction(C_GetMechanismList, func, rv, NUM2HANDLE(slot_id), NULL_PTR, &count);
341
- if (rv != CKR_OK) pkcs11_raise(rv);
339
+ if (rv != CKR_OK) pkcs11_raise(self,rv);
342
340
  if (count == 0) return ary;
343
341
 
344
342
  types = (CK_MECHANISM_TYPE_PTR)malloc(sizeof(CK_MECHANISM_TYPE)*count);
@@ -346,7 +344,7 @@ pkcs11_C_GetMechanismList(VALUE self, VALUE slot_id)
346
344
  CallFunction(C_GetMechanismList, func, rv, NUM2HANDLE(slot_id), types, &count);
347
345
  if (rv != CKR_OK){
348
346
  free(types);
349
- pkcs11_raise(rv);
347
+ pkcs11_raise(self,rv);
350
348
  }
351
349
  for (i = 0; i < count; i++)
352
350
  rb_ary_push(ary, HANDLE2NUM(*(types+i)));
@@ -365,7 +363,7 @@ pkcs11_C_GetMechanismInfo(VALUE self, VALUE slot_id, VALUE type)
365
363
  info = pkcs11_new_struct(cCK_MECHANISM_INFO);
366
364
  GetFunction(self, C_GetMechanismInfo, func);
367
365
  CallFunction(C_GetMechanismInfo, func, rv, NUM2HANDLE(slot_id), NUM2HANDLE(type), DATA_PTR(info));
368
- if (rv != CKR_OK) pkcs11_raise(rv);
366
+ if (rv != CKR_OK) pkcs11_raise(self,rv);
369
367
 
370
368
  return info;
371
369
  }
@@ -382,7 +380,7 @@ pkcs11_C_InitToken(VALUE self, VALUE slot_id, VALUE pin, VALUE label)
382
380
  CallFunction(C_InitToken, func, rv, NUM2HANDLE(slot_id),
383
381
  (CK_UTF8CHAR_PTR)RSTRING_PTR(pin), RSTRING_LEN(pin),
384
382
  (CK_UTF8CHAR_PTR)RSTRING_PTR(label));
385
- if (rv != CKR_OK) pkcs11_raise(rv);
383
+ if (rv != CKR_OK) pkcs11_raise(self,rv);
386
384
 
387
385
  return self;
388
386
  }
@@ -397,7 +395,7 @@ pkcs11_C_InitPIN(VALUE self, VALUE session, VALUE pin)
397
395
  GetFunction(self, C_InitPIN, func);
398
396
  CallFunction(C_InitPIN, func, rv, NUM2HANDLE(session),
399
397
  (CK_UTF8CHAR_PTR)RSTRING_PTR(pin), RSTRING_LEN(pin));
400
- if (rv != CKR_OK) pkcs11_raise(rv);
398
+ if (rv != CKR_OK) pkcs11_raise(self,rv);
401
399
 
402
400
  return self;
403
401
  }
@@ -411,7 +409,7 @@ pkcs11_C_OpenSession(VALUE self, VALUE slot_id, VALUE flags)
411
409
 
412
410
  GetFunction(self, C_OpenSession, func);
413
411
  CallFunction(C_OpenSession, func, rv, NUM2HANDLE(slot_id), NUM2ULONG(flags), 0, 0, &handle);
414
- if(rv != CKR_OK) pkcs11_raise(rv);
412
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
415
413
 
416
414
  return HANDLE2NUM(handle);
417
415
  }
@@ -426,7 +424,7 @@ pkcs11_C_Login(VALUE self, VALUE session, VALUE user_type, VALUE pin)
426
424
  GetFunction(self, C_Login, func);
427
425
  CallFunction(C_Login, func, rv, NUM2HANDLE(session), NUM2ULONG(user_type),
428
426
  (CK_UTF8CHAR_PTR)RSTRING_PTR(pin), RSTRING_LEN(pin));
429
- if(rv != CKR_OK) pkcs11_raise(rv);
427
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
430
428
 
431
429
  return self;
432
430
  }
@@ -439,7 +437,7 @@ pkcs11_C_Logout(VALUE self, VALUE session)
439
437
 
440
438
  GetFunction(self, C_Logout, func);
441
439
  CallFunction(C_Logout, func, rv, NUM2HANDLE(session));
442
- if(rv != CKR_OK) pkcs11_raise(rv);
440
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
443
441
 
444
442
  return self;
445
443
  }
@@ -452,7 +450,7 @@ pkcs11_C_CloseSession(VALUE self, VALUE session)
452
450
 
453
451
  GetFunction(self, C_CloseSession, func);
454
452
  CallFunction(C_CloseSession, func, rv, NUM2HANDLE(session));
455
- if(rv != CKR_OK) pkcs11_raise(rv);
453
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
456
454
 
457
455
  return self;
458
456
  }
@@ -465,7 +463,7 @@ pkcs11_C_CloseAllSessions(VALUE self, VALUE slot_id)
465
463
 
466
464
  GetFunction(self, C_CloseAllSessions, func);
467
465
  CallFunction(C_CloseAllSessions, func, rv, NUM2HANDLE(slot_id));
468
- if(rv != CKR_OK) pkcs11_raise(rv);
466
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
469
467
 
470
468
  return self;
471
469
  }
@@ -480,7 +478,7 @@ pkcs11_C_GetSessionInfo(VALUE self, VALUE session)
480
478
  info = pkcs11_new_struct(cCK_SESSION_INFO);
481
479
  GetFunction(self, C_GetSessionInfo, func);
482
480
  CallFunction(C_GetSessionInfo, func, rv, NUM2HANDLE(session), DATA_PTR(info));
483
- if (rv != CKR_OK) pkcs11_raise(rv);
481
+ if (rv != CKR_OK) pkcs11_raise(self,rv);
484
482
 
485
483
  return info;
486
484
  }
@@ -495,10 +493,10 @@ pkcs11_C_GetOperationState(VALUE self, VALUE session)
495
493
 
496
494
  GetFunction(self, C_GetOperationState, func);
497
495
  CallFunction(C_GetOperationState, func, rv, NUM2HANDLE(session), NULL_PTR, &size);
498
- if (rv != CKR_OK) pkcs11_raise(rv);
496
+ if (rv != CKR_OK) pkcs11_raise(self,rv);
499
497
  state = rb_str_new(0, size);
500
498
  CallFunction(C_GetOperationState, func, rv, NUM2HANDLE(session), (CK_BYTE_PTR)RSTRING_PTR(state), &size);
501
- if (rv != CKR_OK) pkcs11_raise(rv);
499
+ if (rv != CKR_OK) pkcs11_raise(self,rv);
502
500
  rb_str_set_len(state, size);
503
501
 
504
502
  return state;
@@ -515,7 +513,7 @@ pkcs11_C_SetOperationState(VALUE self, VALUE session, VALUE state, VALUE enc_key
515
513
  CallFunction(C_SetOperationState, func, rv, NUM2HANDLE(session),
516
514
  (CK_BYTE_PTR)RSTRING_PTR(state), RSTRING_LEN(state),
517
515
  NUM2HANDLE(enc_key), NUM2HANDLE(auth_key));
518
- if (rv != CKR_OK) pkcs11_raise(rv);
516
+ if (rv != CKR_OK) pkcs11_raise(self,rv);
519
517
 
520
518
  return self;
521
519
  }
@@ -532,7 +530,7 @@ pkcs11_C_SetPIN(VALUE self, VALUE session, VALUE old_pin, VALUE new_pin)
532
530
  CallFunction(C_SetPIN, func, rv, NUM2HANDLE(session),
533
531
  (CK_UTF8CHAR_PTR)RSTRING_PTR(old_pin), RSTRING_LEN(old_pin),
534
532
  (CK_UTF8CHAR_PTR)RSTRING_PTR(new_pin), RSTRING_LEN(new_pin));
535
- if(rv != CKR_OK) pkcs11_raise(rv);
533
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
536
534
 
537
535
  return self;
538
536
  }
@@ -571,7 +569,7 @@ pkcs11_C_CreateObject(VALUE self, VALUE session, VALUE template)
571
569
  GetFunction(self, C_CreateObject, func);
572
570
  CallFunction(C_CreateObject, func, rv, NUM2HANDLE(session), tmp, RARRAY_LEN(template), &handle);
573
571
  free(tmp);
574
- if(rv != CKR_OK) pkcs11_raise(rv);
572
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
575
573
 
576
574
  return HANDLE2NUM(handle);
577
575
  }
@@ -588,7 +586,7 @@ pkcs11_C_CopyObject(VALUE self, VALUE session, VALUE object, VALUE template)
588
586
  GetFunction(self, C_CopyObject, func);
589
587
  CallFunction(C_CopyObject, func, rv, NUM2HANDLE(session), NUM2HANDLE(object), tmp, RARRAY_LEN(template), &handle);
590
588
  free(tmp);
591
- if(rv != CKR_OK) pkcs11_raise(rv);
589
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
592
590
 
593
591
  return HANDLE2NUM(handle);
594
592
  }
@@ -601,7 +599,7 @@ pkcs11_C_DestroyObject(VALUE self, VALUE session, VALUE handle)
601
599
 
602
600
  GetFunction(self, C_DestroyObject, func);
603
601
  CallFunction(C_DestroyObject, func, rv, NUM2HANDLE(session), NUM2HANDLE(handle));
604
- if(rv != CKR_OK) pkcs11_raise(rv);
602
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
605
603
 
606
604
  return self;
607
605
  }
@@ -615,7 +613,7 @@ pkcs11_C_GetObjectSize(VALUE self, VALUE session, VALUE handle)
615
613
 
616
614
  GetFunction(self, C_GetObjectSize, func);
617
615
  CallFunction(C_GetObjectSize, func, rv, NUM2HANDLE(session), NUM2HANDLE(handle), &size);
618
- if(rv != CKR_OK) pkcs11_raise(rv);
616
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
619
617
 
620
618
  return ULONG2NUM(size);
621
619
  }
@@ -635,7 +633,7 @@ pkcs11_C_FindObjectsInit(VALUE self, VALUE session, VALUE template)
635
633
  GetFunction(self, C_FindObjectsInit, func);
636
634
  CallFunction(C_FindObjectsInit, func, rv, NUM2HANDLE(session), tmp, tmp_size);
637
635
  free(tmp);
638
- if(rv != CKR_OK) pkcs11_raise(rv);
636
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
639
637
 
640
638
  return self;
641
639
  }
@@ -648,7 +646,7 @@ pkcs11_C_FindObjectsFinal(VALUE self, VALUE session)
648
646
 
649
647
  GetFunction(self, C_FindObjectsFinal, func);
650
648
  CallFunction(C_FindObjectsFinal, func, rv, NUM2HANDLE(session));
651
- if(rv != CKR_OK) pkcs11_raise(rv);
649
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
652
650
 
653
651
  return self;
654
652
  }
@@ -669,7 +667,7 @@ pkcs11_C_FindObjects(VALUE self, VALUE session, VALUE max_count)
669
667
  CallFunction(C_FindObjects, func, rv, NUM2HANDLE(session), handles, NUM2ULONG(max_count), &count);
670
668
  if(rv != CKR_OK){
671
669
  free(handles);
672
- pkcs11_raise(rv);
670
+ pkcs11_raise(self,rv);
673
671
  }
674
672
  ary = rb_ary_new();
675
673
  for(i = 0; i < count; i++)
@@ -689,6 +687,7 @@ pkcs11_C_GetAttributeValue(VALUE self, VALUE session, VALUE handle, VALUE templa
689
687
  CK_ULONG i, template_size;
690
688
  CK_ATTRIBUTE_PTR tmp;
691
689
  VALUE ary;
690
+ VALUE class_attr = rb_funcall(self, rb_intern("vendor_class_CK_ATTRIBUTE"), 0);
692
691
 
693
692
  tmp = pkcs11_attr_ary2buf(template);
694
693
  template_size = RARRAY_LEN(template);
@@ -696,7 +695,7 @@ pkcs11_C_GetAttributeValue(VALUE self, VALUE session, VALUE handle, VALUE templa
696
695
  CallFunction(C_GetAttributeValue, func, rv, NUM2HANDLE(session), NUM2HANDLE(handle), tmp, template_size);
697
696
  if(rv != CKR_OK){
698
697
  free(tmp);
699
- pkcs11_raise(rv);
698
+ pkcs11_raise(self,rv);
700
699
  }
701
700
 
702
701
  for (i = 0; i < template_size; i++){
@@ -711,13 +710,13 @@ pkcs11_C_GetAttributeValue(VALUE self, VALUE session, VALUE handle, VALUE templa
711
710
  if (attr->pValue) free(attr->pValue);
712
711
  }
713
712
  free(tmp);
714
- pkcs11_raise(rv);
713
+ pkcs11_raise(self,rv);
715
714
  }
716
715
  ary = rb_ary_new();
717
716
  for (i = 0; i < template_size; i++){
718
717
  CK_ATTRIBUTE_PTR attr = tmp + i;
719
718
  if (attr->ulValueLen != (CK_ULONG)-1){
720
- VALUE v = pkcs11_new_struct(cCK_ATTRIBUTE);
719
+ VALUE v = pkcs11_new_struct(class_attr);
721
720
  memcpy(DATA_PTR(v), attr, sizeof(CK_ATTRIBUTE));
722
721
  rb_ary_push(ary, v);
723
722
  }
@@ -740,7 +739,7 @@ pkcs11_C_SetAttributeValue(VALUE self, VALUE session, VALUE handle, VALUE templa
740
739
  GetFunction(self, C_SetAttributeValue, func);
741
740
  CallFunction(C_SetAttributeValue, func, rv, NUM2HANDLE(session), NUM2HANDLE(handle), tmp, template_size);
742
741
  free(tmp);
743
- if(rv != CKR_OK) pkcs11_raise(rv);
742
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
744
743
 
745
744
  return self;
746
745
  }
@@ -754,7 +753,7 @@ pkcs11_C_SeedRandom(VALUE self, VALUE session, VALUE seed)
754
753
  GetFunction(self, C_SeedRandom, func);
755
754
  CallFunction(C_SeedRandom, func, rv, NUM2HANDLE(session),
756
755
  (CK_BYTE_PTR)RSTRING_PTR(seed), RSTRING_LEN(seed));
757
- if(rv != CKR_OK) pkcs11_raise(rv);
756
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
758
757
 
759
758
  return self;
760
759
  }
@@ -769,7 +768,7 @@ pkcs11_C_GenerateRandom(VALUE self, VALUE session, VALUE size)
769
768
 
770
769
  GetFunction(self, C_GenerateRandom, func);
771
770
  CallFunction(C_GenerateRandom, func, rv, NUM2HANDLE(session), (CK_BYTE_PTR)RSTRING_PTR(buf), sz);
772
- if(rv != CKR_OK) pkcs11_raise(rv);
771
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
773
772
 
774
773
  return buf;
775
774
  }
@@ -784,7 +783,7 @@ pkcs11_C_WaitForSlotEvent(VALUE self, VALUE flags)
784
783
  GetFunction(self, C_WaitForSlotEvent, func);
785
784
  CallFunction(C_WaitForSlotEvent, func, rv, NUM2ULONG(flags), &slot_id, NULL_PTR);
786
785
  if(rv == CKR_NO_EVENT) return Qnil;
787
- if(rv != CKR_OK) pkcs11_raise(rv);
786
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
788
787
 
789
788
  return HANDLE2NUM(slot_id);
790
789
  }
@@ -806,10 +805,10 @@ typedef VALUE (*verify_func)
806
805
  typedef VALUE (*verify_final_func)
807
806
  (CK_SESSION_HANDLE, CK_BYTE_PTR, CK_ULONG);
808
807
 
809
- #define common_crypt(s, d, sz, f) common_crypt_update(s, d, sz, f)
808
+ #define common_crypt(self, s, d, sz, f) common_crypt_update(self, s, d, sz, f)
810
809
 
811
810
  static VALUE
812
- common_init(VALUE session, VALUE mechanism, VALUE key, init_func func)
811
+ common_init(VALUE self, VALUE session, VALUE mechanism, VALUE key, init_func func)
813
812
  {
814
813
  CK_RV rv;
815
814
  CK_MECHANISM_PTR m;
@@ -819,13 +818,13 @@ common_init(VALUE session, VALUE mechanism, VALUE key, init_func func)
819
818
  m = DATA_PTR(mechanism);
820
819
  /* Use the function signature of any of the various C_*Init functions. */
821
820
  CallFunction(C_EncryptInit, func, rv, NUM2HANDLE(session), m, NUM2HANDLE(key));
822
- if(rv != CKR_OK) pkcs11_raise(rv);
821
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
823
822
 
824
823
  return Qnil;
825
824
  }
826
825
 
827
826
  static VALUE
828
- common_crypt_update(VALUE session, VALUE data, VALUE size, crypt_update_func func)
827
+ common_crypt_update(VALUE self, VALUE session, VALUE data, VALUE size, crypt_update_func func)
829
828
  {
830
829
  CK_RV rv;
831
830
  CK_ULONG sz = 0;
@@ -836,7 +835,7 @@ common_crypt_update(VALUE session, VALUE data, VALUE size, crypt_update_func fun
836
835
  CallFunction(C_EncryptUpdate, func, rv, NUM2HANDLE(session),
837
836
  (CK_BYTE_PTR)RSTRING_PTR(data), RSTRING_LEN(data),
838
837
  NULL_PTR, &sz);
839
- if(rv != CKR_OK) pkcs11_raise(rv);
838
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
840
839
  }else{
841
840
  sz = NUM2ULONG(size);
842
841
  }
@@ -845,14 +844,14 @@ common_crypt_update(VALUE session, VALUE data, VALUE size, crypt_update_func fun
845
844
  CallFunction(C_EncryptUpdate, func, rv, NUM2HANDLE(session),
846
845
  (CK_BYTE_PTR)RSTRING_PTR(data), RSTRING_LEN(data),
847
846
  (CK_BYTE_PTR)RSTRING_PTR(buf), &sz);
848
- if(rv != CKR_OK) pkcs11_raise(rv);
847
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
849
848
  rb_str_set_len(buf, sz);
850
849
 
851
850
  return buf;
852
851
  }
853
852
 
854
853
  static VALUE
855
- common_crypt_final(VALUE session, VALUE size, crypt_final_func func)
854
+ common_crypt_final(VALUE self, VALUE session, VALUE size, crypt_final_func func)
856
855
  {
857
856
  CK_RV rv;
858
857
  CK_ULONG sz = 0;
@@ -860,34 +859,34 @@ common_crypt_final(VALUE session, VALUE size, crypt_final_func func)
860
859
 
861
860
  if (NIL_P(size)){
862
861
  CallFunction(C_EncryptFinal, func, rv, NUM2HANDLE(session), NULL_PTR, &sz);
863
- if(rv != CKR_OK) pkcs11_raise(rv);
862
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
864
863
  }else{
865
864
  sz = NUM2ULONG(size);
866
865
  }
867
866
  buf = rb_str_new(0, sz);
868
867
 
869
868
  CallFunction(C_EncryptFinal, func, rv, NUM2HANDLE(session), (CK_BYTE_PTR)RSTRING_PTR(buf), &sz);
870
- if(rv != CKR_OK) pkcs11_raise(rv);
869
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
871
870
  rb_str_set_len(buf, sz);
872
871
 
873
872
  return buf;
874
873
  }
875
874
 
876
875
  static VALUE
877
- common_sign_update(VALUE session, VALUE data, sign_update_func func)
876
+ common_sign_update(VALUE self, VALUE session, VALUE data, sign_update_func func)
878
877
  {
879
878
  CK_RV rv;
880
879
 
881
880
  StringValue(data);
882
881
  CallFunction(C_SignUpdate, func, rv, NUM2HANDLE(session),
883
882
  (CK_BYTE_PTR)RSTRING_PTR(data), RSTRING_LEN(data));
884
- if(rv != CKR_OK) pkcs11_raise(rv);
883
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
885
884
 
886
885
  return Qnil;
887
886
  }
888
887
 
889
888
  static VALUE
890
- common_verify(VALUE session, VALUE data, VALUE sig, verify_func func)
889
+ common_verify(VALUE self, VALUE session, VALUE data, VALUE sig, verify_func func)
891
890
  {
892
891
  CK_RV rv;
893
892
 
@@ -897,7 +896,7 @@ common_verify(VALUE session, VALUE data, VALUE sig, verify_func func)
897
896
  CallFunction(C_Verify, func, rv, NUM2HANDLE(session),
898
897
  (CK_BYTE_PTR)RSTRING_PTR(data), RSTRING_LEN(data),
899
898
  (CK_BYTE_PTR)RSTRING_PTR(sig), RSTRING_LEN(sig));
900
- if(rv != CKR_OK) pkcs11_raise(rv);
899
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
901
900
 
902
901
  return Qnil;
903
902
  }
@@ -909,7 +908,7 @@ pkcs11_C_EncryptInit(VALUE self, VALUE session, VALUE mechanism, VALUE key)
909
908
  {
910
909
  CK_C_EncryptInit func;
911
910
  GetFunction(self, C_EncryptInit, func);
912
- common_init(session, mechanism, key, func);
911
+ common_init(self, session, mechanism, key, func);
913
912
  return self;
914
913
  }
915
914
 
@@ -918,7 +917,7 @@ pkcs11_C_Encrypt(VALUE self, VALUE session, VALUE data, VALUE size)
918
917
  {
919
918
  CK_C_Encrypt func;
920
919
  GetFunction(self, C_Encrypt, func);
921
- return common_crypt(session, data, size, func);
920
+ return common_crypt(self, session, data, size, func);
922
921
  }
923
922
 
924
923
  static VALUE
@@ -926,7 +925,7 @@ pkcs11_C_EncryptUpdate(VALUE self, VALUE session, VALUE data, VALUE size)
926
925
  {
927
926
  CK_C_EncryptUpdate func;
928
927
  GetFunction(self, C_EncryptUpdate, func);
929
- return common_crypt_update(session, data, size, func);
928
+ return common_crypt_update(self, session, data, size, func);
930
929
  }
931
930
 
932
931
  static VALUE
@@ -934,7 +933,7 @@ pkcs11_C_EncryptFinal(VALUE self, VALUE session, VALUE size)
934
933
  {
935
934
  CK_C_EncryptFinal func;
936
935
  GetFunction(self, C_EncryptFinal, func);
937
- return common_crypt_final(session, size, func);
936
+ return common_crypt_final(self, session, size, func);
938
937
  }
939
938
 
940
939
  static VALUE
@@ -942,7 +941,7 @@ pkcs11_C_DecryptInit(VALUE self, VALUE session, VALUE mechanism, VALUE key)
942
941
  {
943
942
  CK_C_DecryptInit func;
944
943
  GetFunction(self, C_DecryptInit, func);
945
- common_init(session, mechanism, key, func);
944
+ common_init(self, session, mechanism, key, func);
946
945
  return self;
947
946
  }
948
947
 
@@ -951,7 +950,7 @@ pkcs11_C_Decrypt(VALUE self, VALUE session, VALUE data, VALUE size)
951
950
  {
952
951
  CK_C_Decrypt func;
953
952
  GetFunction(self, C_Decrypt, func);
954
- return common_crypt(session, data, size, func);
953
+ return common_crypt(self, session, data, size, func);
955
954
  }
956
955
 
957
956
  static VALUE
@@ -959,7 +958,7 @@ pkcs11_C_DecryptUpdate(VALUE self, VALUE session, VALUE data, VALUE size)
959
958
  {
960
959
  CK_C_DecryptUpdate func;
961
960
  GetFunction(self, C_DecryptUpdate, func);
962
- return common_crypt_update(session, data, size, func);
961
+ return common_crypt_update(self, session, data, size, func);
963
962
  }
964
963
 
965
964
  static VALUE
@@ -967,21 +966,21 @@ pkcs11_C_DecryptFinal(VALUE self, VALUE session, VALUE size)
967
966
  {
968
967
  CK_C_DecryptFinal func;
969
968
  GetFunction(self, C_DecryptFinal, func);
970
- return common_crypt_final(session, size, func);
969
+ return common_crypt_final(self, session, size, func);
971
970
  }
972
971
 
973
- #define common_sign(s, d, sz, f) common_crypt(s, d, sz, f)
974
- #define common_sign_final(s, sz, f) common_crypt_final(s, sz, f)
975
- #define common_verify_update(s, d, f) common_sign_update(s, d, f)
976
- #define common_verify_final(s, d, f) common_sign_update(s, d, f)
977
- #define common_verify_recover(s, d, sz, f) common_sign(s, d, sz, f)
972
+ #define common_sign(self, s, d, sz, f) common_crypt(self, s, d, sz, f)
973
+ #define common_sign_final(self, s, sz, f) common_crypt_final(self, s, sz, f)
974
+ #define common_verify_update(self, s, d, f) common_sign_update(self, s, d, f)
975
+ #define common_verify_final(self, s, d, f) common_sign_update(self, s, d, f)
976
+ #define common_verify_recover(self, s, d, sz, f) common_sign(self, s, d, sz, f)
978
977
 
979
978
  static VALUE
980
979
  pkcs11_C_SignInit(VALUE self, VALUE session, VALUE mechanism, VALUE key)
981
980
  {
982
981
  CK_C_SignInit func;
983
982
  GetFunction(self, C_SignInit, func);
984
- common_init(session, mechanism, key, func);
983
+ common_init(self, session, mechanism, key, func);
985
984
  return self;
986
985
  }
987
986
 
@@ -990,7 +989,7 @@ pkcs11_C_Sign(VALUE self, VALUE session, VALUE data, VALUE size)
990
989
  {
991
990
  CK_C_Sign func;
992
991
  GetFunction(self, C_Sign, func);
993
- return common_sign(session, data, size, func);
992
+ return common_sign(self, session, data, size, func);
994
993
  }
995
994
 
996
995
  static VALUE
@@ -998,7 +997,7 @@ pkcs11_C_SignUpdate(VALUE self, VALUE session, VALUE data)
998
997
  {
999
998
  CK_C_SignUpdate func;
1000
999
  GetFunction(self, C_SignUpdate, func);
1001
- common_sign_update(session, data, func);
1000
+ common_sign_update(self, session, data, func);
1002
1001
  return self;
1003
1002
  }
1004
1003
 
@@ -1007,7 +1006,7 @@ pkcs11_C_SignFinal(VALUE self, VALUE session, VALUE size)
1007
1006
  {
1008
1007
  CK_C_SignFinal func;
1009
1008
  GetFunction(self, C_SignFinal, func);
1010
- return common_sign_final(session, size, func);
1009
+ return common_sign_final(self, session, size, func);
1011
1010
  }
1012
1011
 
1013
1012
  static VALUE
@@ -1015,7 +1014,7 @@ pkcs11_C_SignRecoverInit(VALUE self, VALUE session, VALUE mechanism, VALUE key)
1015
1014
  {
1016
1015
  CK_C_SignRecoverInit func;
1017
1016
  GetFunction(self, C_SignRecoverInit, func);
1018
- common_init(session, mechanism, key, func);
1017
+ common_init(self, session, mechanism, key, func);
1019
1018
  return self;
1020
1019
  }
1021
1020
 
@@ -1024,7 +1023,7 @@ pkcs11_C_SignRecover(VALUE self, VALUE session, VALUE data, VALUE size)
1024
1023
  {
1025
1024
  CK_C_SignRecover func;
1026
1025
  GetFunction(self, C_SignRecover, func);
1027
- return common_sign(session, data, size, func);
1026
+ return common_sign(self, session, data, size, func);
1028
1027
  }
1029
1028
 
1030
1029
  static VALUE
@@ -1032,7 +1031,7 @@ pkcs11_C_VerifyInit(VALUE self, VALUE session, VALUE mechanism, VALUE key)
1032
1031
  {
1033
1032
  CK_C_VerifyInit func;
1034
1033
  GetFunction(self, C_VerifyInit, func);
1035
- common_init(session, mechanism, key, func);
1034
+ common_init(self, session, mechanism, key, func);
1036
1035
  return self;
1037
1036
  }
1038
1037
 
@@ -1041,7 +1040,7 @@ pkcs11_C_Verify(VALUE self, VALUE session, VALUE data, VALUE sig)
1041
1040
  {
1042
1041
  CK_C_Verify func;
1043
1042
  GetFunction(self, C_Verify, func);
1044
- common_verify(session, data, sig, func);
1043
+ common_verify(self, session, data, sig, func);
1045
1044
  return Qtrue;
1046
1045
  }
1047
1046
 
@@ -1050,7 +1049,7 @@ pkcs11_C_VerifyUpdate(VALUE self, VALUE session, VALUE data)
1050
1049
  {
1051
1050
  CK_C_VerifyUpdate func;
1052
1051
  GetFunction(self, C_VerifyUpdate, func);
1053
- common_verify_update(session, data, func);
1052
+ common_verify_update(self, session, data, func);
1054
1053
  return self;
1055
1054
  }
1056
1055
 
@@ -1059,7 +1058,7 @@ pkcs11_C_VerifyFinal(VALUE self, VALUE session, VALUE sig)
1059
1058
  {
1060
1059
  CK_C_VerifyFinal func;
1061
1060
  GetFunction(self, C_VerifyFinal, func);
1062
- common_verify_final(session, sig, func);
1061
+ common_verify_final(self, session, sig, func);
1063
1062
  return Qtrue;
1064
1063
  }
1065
1064
 
@@ -1068,7 +1067,7 @@ pkcs11_C_VerifyRecoverInit(VALUE self, VALUE session, VALUE mechanism, VALUE key
1068
1067
  {
1069
1068
  CK_C_VerifyRecoverInit func;
1070
1069
  GetFunction(self, C_VerifyRecoverInit, func);
1071
- common_init(session, mechanism, key, func);
1070
+ common_init(self, session, mechanism, key, func);
1072
1071
  return self;
1073
1072
  }
1074
1073
 
@@ -1077,13 +1076,13 @@ pkcs11_C_VerifyRecover(VALUE self, VALUE session, VALUE sig, VALUE size)
1077
1076
  {
1078
1077
  CK_C_VerifyRecover func;
1079
1078
  GetFunction(self, C_VerifyRecover, func);
1080
- common_verify_recover(session, sig, size, func);
1079
+ common_verify_recover(self, session, sig, size, func);
1081
1080
  return Qtrue;
1082
1081
  }
1083
1082
 
1084
- #define common_digest(s, d, sz, f) common_crypt(s, d, sz, f)
1085
- #define common_digest_update(s, d, f) common_sign_update(s, d, f)
1086
- #define common_digest_final(s, sz, f) common_crypt_final(s, sz, f)
1083
+ #define common_digest(self, s, d, sz, f) common_crypt(self, s, d, sz, f)
1084
+ #define common_digest_update(self, s, d, f) common_sign_update(self, s, d, f)
1085
+ #define common_digest_final(self, s, sz, f) common_crypt_final(self, s, sz, f)
1087
1086
 
1088
1087
  VALUE
1089
1088
  pkcs11_C_DigestInit(VALUE self, VALUE session, VALUE mechanism)
@@ -1097,7 +1096,7 @@ pkcs11_C_DigestInit(VALUE self, VALUE session, VALUE mechanism)
1097
1096
  rb_raise(rb_eArgError, "2nd arg must be a PKCS11::CK_MECHANISM");
1098
1097
  m = DATA_PTR(mechanism);
1099
1098
  CallFunction(C_DigestInit, func, rv, NUM2HANDLE(session), m);
1100
- if(rv != CKR_OK) pkcs11_raise(rv);
1099
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
1101
1100
 
1102
1101
  return self;
1103
1102
  }
@@ -1107,7 +1106,7 @@ pkcs11_C_Digest(VALUE self, VALUE session, VALUE data, VALUE size)
1107
1106
  {
1108
1107
  CK_C_Digest func;
1109
1108
  GetFunction(self, C_Digest, func);
1110
- return common_digest(session, data, size, func);
1109
+ return common_digest(self, session, data, size, func);
1111
1110
  }
1112
1111
 
1113
1112
  VALUE
@@ -1115,7 +1114,7 @@ pkcs11_C_DigestUpdate(VALUE self, VALUE session, VALUE data)
1115
1114
  {
1116
1115
  CK_C_DigestUpdate func;
1117
1116
  GetFunction(self, C_DigestUpdate, func);
1118
- common_digest_update(session, data, func);
1117
+ common_digest_update(self, session, data, func);
1119
1118
  return self;
1120
1119
  }
1121
1120
 
@@ -1127,7 +1126,7 @@ pkcs11_C_DigestKey(VALUE self, VALUE session, VALUE handle)
1127
1126
 
1128
1127
  GetFunction(self, C_DigestKey, func);
1129
1128
  CallFunction(C_DigestKey, func, rv, NUM2HANDLE(session), NUM2HANDLE(handle));
1130
- if(rv != CKR_OK) pkcs11_raise(rv);
1129
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
1131
1130
 
1132
1131
  return self;
1133
1132
  }
@@ -1137,7 +1136,7 @@ pkcs11_C_DigestFinal(VALUE self, VALUE session, VALUE size)
1137
1136
  {
1138
1137
  CK_C_DigestFinal func;
1139
1138
  GetFunction(self, C_DigestFinal, func);
1140
- return common_digest_final(session, size, func);
1139
+ return common_digest_final(self, session, size, func);
1141
1140
  }
1142
1141
 
1143
1142
  VALUE
@@ -1145,7 +1144,7 @@ pkcs11_C_DigestEncryptUpdate(VALUE self, VALUE session, VALUE data, VALUE size)
1145
1144
  {
1146
1145
  CK_C_DigestEncryptUpdate func;
1147
1146
  GetFunction(self, C_DigestEncryptUpdate, func);
1148
- return common_crypt_update(session, data, size, func);
1147
+ return common_crypt_update(self, session, data, size, func);
1149
1148
  }
1150
1149
 
1151
1150
  VALUE
@@ -1153,7 +1152,7 @@ pkcs11_C_DecryptDigestUpdate(VALUE self, VALUE session, VALUE data, VALUE size)
1153
1152
  {
1154
1153
  CK_C_DecryptDigestUpdate func;
1155
1154
  GetFunction(self, C_DecryptDigestUpdate, func);
1156
- return common_crypt_update(session, data, size, func);
1155
+ return common_crypt_update(self, session, data, size, func);
1157
1156
  }
1158
1157
 
1159
1158
  VALUE
@@ -1161,7 +1160,7 @@ pkcs11_C_SignEncryptUpdate(VALUE self, VALUE session, VALUE data, VALUE size)
1161
1160
  {
1162
1161
  CK_C_SignEncryptUpdate func;
1163
1162
  GetFunction(self, C_SignEncryptUpdate, func);
1164
- return common_crypt_update(session, data, size, func);
1163
+ return common_crypt_update(self, session, data, size, func);
1165
1164
  }
1166
1165
 
1167
1166
  VALUE
@@ -1169,7 +1168,7 @@ pkcs11_C_DecryptVerifyUpdate(VALUE self, VALUE session, VALUE data, VALUE size)
1169
1168
  {
1170
1169
  CK_C_DecryptVerifyUpdate func;
1171
1170
  GetFunction(self, C_DecryptVerifyUpdate, func);
1172
- return common_crypt_update(session, data, size, func);
1171
+ return common_crypt_update(self, session, data, size, func);
1173
1172
  }
1174
1173
 
1175
1174
  VALUE
@@ -1187,7 +1186,7 @@ pkcs11_C_GenerateKey(VALUE self, VALUE session, VALUE mechanism, VALUE template)
1187
1186
  tmp = pkcs11_attr_ary2buf(template);
1188
1187
  CallFunction(C_GenerateKey, func, rv, NUM2HANDLE(session), m, tmp, RARRAY_LEN(template), &handle);
1189
1188
  free(tmp);
1190
- if(rv != CKR_OK) pkcs11_raise(rv);
1189
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
1191
1190
 
1192
1191
  return HANDLE2NUM(handle);
1193
1192
  }
@@ -1215,7 +1214,7 @@ pkcs11_C_GenerateKeyPair(VALUE self, VALUE session, VALUE mechanism, VALUE pubke
1215
1214
  &pubkey_handle, &privkey_handle);
1216
1215
  free(pubkey_tmp);
1217
1216
  free(privkey_tmp);
1218
- if(rv != CKR_OK) pkcs11_raise(rv);
1217
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
1219
1218
  ary = rb_ary_new();
1220
1219
  rb_ary_push(ary, HANDLE2NUM(pubkey_handle));
1221
1220
  rb_ary_push(ary, HANDLE2NUM(privkey_handle));
@@ -1240,7 +1239,7 @@ pkcs11_C_WrapKey(VALUE self, VALUE session, VALUE mechanism, VALUE wrapping, VAL
1240
1239
  CallFunction(C_WrapKey, func, rv, NUM2HANDLE(session), m,
1241
1240
  NUM2HANDLE(wrapping), NUM2HANDLE(wrapped),
1242
1241
  (CK_BYTE_PTR)NULL_PTR, &sz);
1243
- if(rv != CKR_OK) pkcs11_raise(rv);
1242
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
1244
1243
  }else{
1245
1244
  sz = NUM2ULONG(size);
1246
1245
  }
@@ -1249,7 +1248,7 @@ pkcs11_C_WrapKey(VALUE self, VALUE session, VALUE mechanism, VALUE wrapping, VAL
1249
1248
  CallFunction(C_WrapKey, func, rv, NUM2HANDLE(session), m,
1250
1249
  NUM2HANDLE(wrapping), NUM2HANDLE(wrapped),
1251
1250
  (CK_BYTE_PTR)RSTRING_PTR(buf), &sz);
1252
- if(rv != CKR_OK) pkcs11_raise(rv);
1251
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
1253
1252
  rb_str_set_len(buf, sz);
1254
1253
 
1255
1254
  return buf;
@@ -1273,7 +1272,7 @@ pkcs11_C_UnwrapKey(VALUE self, VALUE session, VALUE mechanism, VALUE wrapping, V
1273
1272
  (CK_BYTE_PTR)RSTRING_PTR(wrapped), RSTRING_LEN(wrapped),
1274
1273
  tmp, RARRAY_LEN(template), &h);
1275
1274
  free(tmp);
1276
- if(rv != CKR_OK) pkcs11_raise(rv);
1275
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
1277
1276
 
1278
1277
  return HANDLE2NUM(h);
1279
1278
  }
@@ -1295,11 +1294,43 @@ pkcs11_C_DeriveKey(VALUE self, VALUE session, VALUE mechanism, VALUE base, VALUE
1295
1294
  CallFunction(C_DeriveKey, func, rv, NUM2HANDLE(session), m, NUM2HANDLE(base),
1296
1295
  tmp, RARRAY_LEN(template), &h);
1297
1296
  free(tmp);
1298
- if(rv != CKR_OK) pkcs11_raise(rv);
1297
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
1299
1298
 
1300
1299
  return HANDLE2NUM(h);
1301
1300
  }
1302
1301
 
1302
+ /* rb_define_method(cPKCS11, "vendor_raise_on_return_value", pkcs11_vendor_raise_on_return_value, 1); */
1303
+ /*
1304
+ * Raise an exception for the given PKCS#11 return value. This method can be overloaded
1305
+ * to raise vendor specific exceptions. It is only called for rv!=0 and it should never
1306
+ * return regulary, but always by an exception.
1307
+ * @param [Integer] rv return value of the latest operation
1308
+ */
1309
+ static VALUE
1310
+ pkcs11_vendor_raise_on_return_value(VALUE self, VALUE rv_value)
1311
+ {
1312
+ VALUE class;
1313
+ CK_RV rv = NUM2ULONG(rv_value);
1314
+ class = pkcs11_return_value_to_class(rv, ePKCS11Error);
1315
+ rb_raise(class, "%lu", rv);
1316
+
1317
+ return Qnil;
1318
+ }
1319
+
1320
+
1321
+ /* rb_define_method(cPKCS11, "vendor_class_CK_ATTRIBUTE", pkcs11_vendor_class_CK_ATTRIBUTE, 0); */
1322
+ /*
1323
+ * Return class CK_ATTRIBUTE. This method can be overloaded
1324
+ * to return a derived class that appropriate converts vendor specific attributes.
1325
+ * @return [CK_ATTRIBUTE] some kind of CK_ATTRIBUTE
1326
+ */
1327
+ static VALUE
1328
+ pkcs11_vendor_class_CK_ATTRIBUTE(VALUE self)
1329
+ {
1330
+ return cCK_ATTRIBUTE;
1331
+ }
1332
+
1333
+
1303
1334
  ///////////////////////////////////////
1304
1335
 
1305
1336
  static void
@@ -1614,6 +1645,8 @@ Init_pkcs11_ext()
1614
1645
  PKCS11_DEFINE_METHOD(C_WaitForSlotEvent, 1);
1615
1646
  PKCS11_DEFINE_METHOD(C_Finalize, 0);
1616
1647
  PKCS11_DEFINE_METHOD(unload_library, 0);
1648
+ PKCS11_DEFINE_METHOD(vendor_raise_on_return_value, 1);
1649
+ PKCS11_DEFINE_METHOD(vendor_class_CK_ATTRIBUTE, 0);
1617
1650
 
1618
1651
  ///////////////////////////////////////
1619
1652