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.
@@ -274,10 +274,10 @@ PKCS11_IMPLEMENT_STRING_PTR_LEN_ACCESSOR(CK_OTP_PARAM, pValue, ulValueLen);
274
274
  PKCS11_IMPLEMENT_ULONG_ACCESSOR(CK_OTP_PARAM, type);
275
275
 
276
276
  PKCS11_IMPLEMENT_STRUCT_WITH_ALLOCATOR(CK_OTP_PARAMS);
277
- PKCS11_IMPLEMENT_STRUCT_PTR_ARRAY_ACCESSOR(CK_OTP_PARAMS, CK_OTP_PARAM, pParams, ulCount);
277
+ PKCS11_IMPLEMENT_PKCS11_STRUCT_PTR_ARRAY_ACCESSOR(CK_OTP_PARAMS, CK_OTP_PARAM, pParams, ulCount);
278
278
 
279
279
  PKCS11_IMPLEMENT_STRUCT_WITH_ALLOCATOR(CK_OTP_SIGNATURE_INFO);
280
- PKCS11_IMPLEMENT_STRUCT_PTR_ARRAY_ACCESSOR(CK_OTP_SIGNATURE_INFO, CK_OTP_PARAM, pParams, ulCount);
280
+ PKCS11_IMPLEMENT_PKCS11_STRUCT_PTR_ARRAY_ACCESSOR(CK_OTP_SIGNATURE_INFO, CK_OTP_PARAM, pParams, ulCount);
281
281
 
282
282
  PKCS11_IMPLEMENT_STRUCT_WITH_ALLOCATOR(CK_KIP_PARAMS);
283
283
  PKCS11_IMPLEMENT_STRING_PTR_LEN_ACCESSOR(CK_KIP_PARAMS, pSeed, ulSeedLen);
@@ -79,13 +79,14 @@ get_ulong_ptr(VALUE obj, off_t offset)
79
79
  static VALUE
80
80
  set_ulong_ptr(VALUE obj, VALUE value, const char *name, off_t offset)
81
81
  {
82
+ VALUE new_obj;
82
83
  CK_ULONG_PTR *ptr = (CK_ULONG_PTR *)((char*)DATA_PTR(obj) + offset);
83
84
  if (NIL_P(value)){
84
85
  rb_iv_set(obj, name, value);
85
86
  *ptr = NULL_PTR;
86
87
  return value;
87
88
  }
88
- VALUE new_obj = Data_Make_Struct(rb_cInteger, CK_ULONG, 0, free, *ptr);
89
+ new_obj = Data_Make_Struct(rb_cInteger, CK_ULONG, 0, free, *ptr);
89
90
  rb_iv_set(obj, name, new_obj);
90
91
  **ptr = NUM2ULONG(value);
91
92
  return value;
@@ -236,9 +237,10 @@ get_struct_ptr_array(VALUE obj, VALUE klass, off_t offset, off_t offset_len, int
236
237
  unsigned long l = *(unsigned long*)(ptr+offset_len);
237
238
  VALUE ary = rb_ary_new();
238
239
  for (i = 0; i < l; i++){
240
+ VALUE new_obj;
239
241
  void *mem = xmalloc(sizeofstruct);
240
242
  memcpy(mem, p + sizeofstruct * i, sizeofstruct);
241
- VALUE new_obj = Data_Wrap_Struct(klass, 0, -1, mem);
243
+ new_obj = Data_Wrap_Struct(klass, 0, -1, mem);
242
244
  rb_ary_push(ary, new_obj);
243
245
  }
244
246
  return ary;
@@ -396,6 +398,16 @@ static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
396
398
  return set_struct_ptr_array(o, c##k, #k, v, #f, OFFSET_OF(s, f), OFFSET_OF(s, l), sizeof(k)); \
397
399
  }
398
400
 
401
+ #define PKCS11_IMPLEMENT_PKCS11_STRUCT_PTR_ARRAY_ACCESSOR(s, k, f, l) \
402
+ static VALUE c##s##_get_##f(VALUE o){ \
403
+ VALUE klass = rb_const_get(rb_const_get(rb_cObject, rb_intern("PKCS11")), rb_intern(#k)); \
404
+ return get_struct_ptr_array(o, klass, OFFSET_OF(s, f), OFFSET_OF(s, l), sizeof(k)); \
405
+ } \
406
+ static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
407
+ VALUE klass = rb_const_get(rb_const_get(rb_cObject, rb_intern("PKCS11")), rb_intern(#k)); \
408
+ return set_struct_ptr_array(o, klass, #k, v, #f, OFFSET_OF(s, f), OFFSET_OF(s, l), sizeof(k)); \
409
+ }
410
+
399
411
 
400
412
  /**************************************************/
401
413
  /* struct/attribute definition */
@@ -0,0 +1,6 @@
1
+ #ifndef RUBY_PK11_VERSION_H
2
+ #define RUBY_PK11_VERSION_H
3
+
4
+ static const char *VERSION = "0.2.2";
5
+
6
+ #endif
Binary file
Binary file
@@ -89,13 +89,13 @@ module PKCS11
89
89
  def to_attributes(template)
90
90
  case template
91
91
  when Array
92
- template.map{|v| PKCS11::CK_ATTRIBUTE.new(string_to_handle('CKA_', v), nil) }
92
+ template.map{|v| @pk.vendor_class_CK_ATTRIBUTE.new(string_to_handle('CKA_', v), nil) }
93
93
  when Hash
94
- template.map{|k,v| PKCS11::CK_ATTRIBUTE.new(string_to_handle('CKA_', k), v) }
94
+ template.map{|k,v| @pk.vendor_class_CK_ATTRIBUTE.new(string_to_handle('CKA_', k), v) }
95
95
  when String, Symbol
96
- [PKCS11::CK_ATTRIBUTE.new(string_to_handle('CKA_', template), nil)]
96
+ [@pk.vendor_class_CK_ATTRIBUTE.new(string_to_handle('CKA_', template), nil)]
97
97
  when Integer
98
- [PKCS11::CK_ATTRIBUTE.new(template, nil)]
98
+ [@pk.vendor_class_CK_ATTRIBUTE.new(template, nil)]
99
99
  else
100
100
  template
101
101
  end
@@ -104,7 +104,7 @@ module PKCS11
104
104
  def string_to_handle(prefix, attribute) # :nodoc:
105
105
  case attribute
106
106
  when String, Symbol
107
- PKCS11.const_get("#{prefix}#{attribute}")
107
+ @pk.vendor_const_get("#{prefix}#{attribute}")
108
108
  else
109
109
  attribute
110
110
  end
@@ -121,8 +121,8 @@ module PKCS11
121
121
  param = mechanism.values.first
122
122
  case param
123
123
  when Hash
124
- param_class = MechanismParameters[mech]
125
- raise ArgumentError, "unknown mechanism - please use mechanism parameter as String" unless param_class
124
+ param_class = @pk.vendor_mechanism_parameter_struct(mech)
125
+ raise ArgumentError, "unknown mechanism - please use String/Int/Struct as mechanism parameter" unless param_class
126
126
 
127
127
  pa = param_class.new
128
128
  param.each do |k, v|
@@ -54,14 +54,14 @@ module PKCS11
54
54
  alias info C_GetInfo
55
55
 
56
56
  alias unwrapped_C_GetSlotList C_GetSlotList
57
-
57
+
58
58
  # Obtain an array of Slot objects in the system.
59
59
  #
60
60
  # @param [true, false] tokenPresent indicates whether the list
61
61
  # obtained includes only those slots with a token present (true), or
62
62
  # all slots (false);
63
63
  # @return [Array<Slot>]
64
- def C_GetSlotList(tokenPresent=true)
64
+ def C_GetSlotList(tokenPresent=false)
65
65
  slots = unwrapped_C_GetSlotList(tokenPresent)
66
66
  slots.map{|slot|
67
67
  Slot.new self, slot
@@ -74,7 +74,7 @@ module PKCS11
74
74
  def active_slots
75
75
  slots(true)
76
76
  end
77
-
77
+
78
78
  # Obtain an array of Slot objects in the system regardless if a token is present.
79
79
  # @return [Array<Slot>]
80
80
  def all_slots
@@ -86,7 +86,31 @@ module PKCS11
86
86
  self.C_Finalize
87
87
  self.unload_library
88
88
  end
89
-
89
+
90
+ # Return the value of a named constant. Used for CKA_* and CKM_* .
91
+ # This method could be overloaded for vendor specific extensions.
92
+ #
93
+ # @param [String] name Name of the constant
94
+ # @return [Integer] Value of the constant
95
+ def vendor_const_get(name)
96
+ PKCS11.const_get(name)
97
+ end
98
+
99
+ # Return an array of all known CKA_* attributes as String.
100
+ # This method could be overloaded for vendor specific extensions.
101
+ def vendor_all_attribute_names
102
+ return ATTRIBUTES.values
103
+ end
104
+
105
+ # Return the parameter struct of a given mechanism.
106
+ # This method could be overloaded for vendor specific extensions.
107
+ #
108
+ # @param [Integer] mech Mechanism
109
+ # @return [PKCS11::CStruct] appropriate class as parameter for the mechanism
110
+ def vendor_mechanism_parameter_struct(mech)
111
+ Helper::MechanismParameters[mech]
112
+ end
113
+
90
114
  private :unwrapped_initialize
91
115
  private :unwrapped_C_GetSlotList
92
116
  private :unwrapped_C_GetInfo
@@ -26,13 +26,15 @@ module PKCS11
26
26
  "#<#{self.class} #{@obj.inspect}>"
27
27
  end
28
28
 
29
- # Get the value of one attribute of the object.
29
+ # Get the value of one or several attributes of the object.
30
30
  #
31
- # @param [String, Symbol, Integer] attribute can be String or Symbol of the attribute constant
32
- # or the attribute number as Integer.
31
+ # @param [String, Symbol, Integer, Array] attribute can be String or Symbol
32
+ # of the attribute(s) constant or the attribute(s) number as Integer.
33
33
  #
34
- # @return [String, Integer, Boolean, nil] the attribute value as String, Integer or true/false
35
- # depending on the attribute type.
34
+ # @return [String, Integer, Boolean, Array, nil] the attribute value as String,
35
+ # Integer or true/false depending on the attribute type.
36
+ # If called with more than one parameter or with an Array, a Array
37
+ # of attribute values is returned.
36
38
  # Unknown attributes (out of PKCS#11 v2.2) are not converted to adequate
37
39
  # ruby objects but returned as String.
38
40
  # That is true/false will be returned as "\\001" respectively "\\000".
@@ -40,18 +42,23 @@ module PKCS11
40
42
  # @example
41
43
  # object[:VALUE] # => "\000\000\000\000\000\000\000\000"
42
44
  # object[:MODULUS_BITS] # => 768
45
+ # object[:MODULUS_BITS, :LABEL] # => [1024, "MyKey"]
43
46
  #
44
47
  # See PKCS#11 for attribute definitions.
45
- def [](attribute)
46
- attrs = C_GetAttributeValue( [attribute] )
47
- attrs.first.value unless attrs.empty?
48
+ def [](*attributes)
49
+ attrs = C_GetAttributeValue( attributes.flatten )
50
+ if attrs.length>1 || attributes.first.kind_of?(Array)
51
+ attrs.map(&:value)
52
+ else
53
+ attrs.first.value unless attrs.empty?
54
+ end
48
55
  end
49
56
 
50
- # Modifies the value of one attribute the object.
57
+ # Modifies the value of one or several attributes of the object.
51
58
  #
52
- # @param [String, Symbol, Integer] attribute can be String or Symbol of the attribute constant
59
+ # @param [String, Symbol, Integer] attribute can be String or Symbol of the attribute constant
53
60
  # or the attribute value as Integer.
54
- # @param [String, Integer, Boolean, nil] value value the attribute will be set to.
61
+ # @param [String, Integer, Boolean, Array, nil] value value(s) the attribute(s) will be set to.
55
62
  #
56
63
  # Following value conversations are done from Ruby to C:
57
64
  # true -> 0x01
@@ -62,12 +69,16 @@ module PKCS11
62
69
  # @example
63
70
  # object[:VALUE] = "\000\000\000\000\000\000\000\000"
64
71
  # object[:MODULUS_BITS] = 768
72
+ # object[:MODULUS_BITS, :LABEL] = 1024, 'MyKey'
65
73
  #
66
74
  # See PKCS#11 for attribute definitions.
67
75
  # @return value
68
- def []=(attribute, value)
69
- C_SetAttributeValue( attribute => value )
70
- value
76
+ def []=(*attributes)
77
+ values = attributes.pop
78
+ values = [values] unless values.kind_of?(Array)
79
+ raise ArgumentError, "different number of attributes to set (#{attributes.length}) and given values (#{values.length})" unless attributes.length == values.length
80
+ map = values.each.with_index.inject({}){|s, v| s[attributes[v[1]]] = v[0]; s }
81
+ C_SetAttributeValue( map )
71
82
  end
72
83
 
73
84
  # Modifies the value of one or more attributes of the object in a single call.
@@ -80,7 +91,7 @@ module PKCS11
80
91
  template
81
92
  end
82
93
  alias attributes= C_SetAttributeValue
83
-
94
+
84
95
  # Obtains the value of one or more attributes of the object in a single call.
85
96
  #
86
97
  # @param [Array<String, Symbol, Integer>, Hash, String, Integer] attribute attribute names
@@ -98,9 +109,9 @@ module PKCS11
98
109
  def C_GetAttributeValue(*template)
99
110
  case template.length
100
111
  when 0
101
- return PKCS11::ATTRIBUTES.values.map{|attr|
112
+ return @pk.vendor_all_attribute_names.map{|attr|
102
113
  begin
103
- attributes(PKCS11.const_get(attr))
114
+ attributes(@pk.vendor_const_get(attr))
104
115
  rescue PKCS11::Error
105
116
  end
106
117
  }.flatten.compact
@@ -148,7 +159,7 @@ module PKCS11
148
159
  self
149
160
  end
150
161
  alias destroy C_DestroyObject
151
-
162
+
152
163
  # Gets the size of an object in bytes.
153
164
  # @return [Integer]
154
165
  def C_GetObjectSize()
@@ -0,0 +1,14 @@
1
+ .gemtest
2
+ .yardopts
3
+ Manifest.txt
4
+ README_PROTECT_SERVER.rdoc
5
+ Rakefile
6
+ ext/extconf.rb
7
+ ext/generate_constants.rb
8
+ ext/generate_structs.rb
9
+ ext/pk11s.c
10
+ lib/pkcs11_protect_server.rb
11
+ lib/pkcs11_protect_server/extensions.rb
12
+ test/helper.rb
13
+ test/test_pkcs11_protect_server.rb
14
+ test/test_pkcs11_protect_server_crypt.rb
@@ -1,11 +1,11 @@
1
- require "openssl"
1
+ require "pkcs11"
2
2
 
3
3
  def find_softokn
4
4
  if RUBY_PLATFORM =~ /mswin|mingw/
5
5
  lLIBSOFTOKEN3_SO = "softokn3.dll"
6
6
 
7
7
  # Try to find the firefox path.
8
- unless ENV['SOFTOKN_PATH']
8
+ unless so_path = ENV['SOFTOKN_PATH']
9
9
  require 'win32/registry'
10
10
  begin
11
11
  firefox_path = Win32::Registry::HKEY_LOCAL_MACHINE.open('SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\firefox.exe'){|reg|
@@ -21,15 +21,22 @@ def find_softokn
21
21
  else
22
22
  lLIBSOFTOKEN3_SO = "libsoftokn3.so"
23
23
  lLIBNSS_PATHS = %w(
24
- /usr/lib64 /usr/lib/ /usr/lib64/nss /usr/lib/nss
24
+ /usr/lib64
25
+ /usr/lib
26
+ /usr/lib64/nss
27
+ /usr/lib/nss
28
+ /usr/lib/i386-linux-gnu/nss
29
+ /usr/lib/x86_64-linux-gnu/nss
25
30
  )
26
31
  unless so_path = ENV['SOFTOKN_PATH']
27
32
  paths = lLIBNSS_PATHS.collect{|path| File.join(path, lLIBSOFTOKEN3_SO) }
28
- so_path = paths.find{|path| File.exist?(path) }
33
+ so_path = paths.find do |path|
34
+ File.exist?(path) && open_softokn(path).close rescue false
35
+ end
29
36
  end
30
37
  end
31
38
 
32
- raise "#{lLIBSOFTOKEN3_SO} not found - please install firefox or set ENV['SOFTOKN_PATH']" unless so_path
39
+ raise "#{lLIBSOFTOKEN3_SO} not found - please install firefox or libnss3 or set ENV['SOFTOKN_PATH']" unless so_path
33
40
  so_path
34
41
  end
35
42
 
@@ -42,9 +49,10 @@ def softokn_params
42
49
  ]
43
50
  end
44
51
 
45
- def open_softokn
46
- so_path = find_softokn
47
- nNSS_INIT_ARGS = softokn_params
52
+ def softokn_params_string
53
+ softokn_params.join(" ")
54
+ end
48
55
 
49
- PKCS11.open(so_path, :flags=>0, :pReserved=>nNSS_INIT_ARGS.join(" "))
56
+ def open_softokn(so_path=nil)
57
+ PKCS11.open(so_path || find_softokn, :flags=>0, :pReserved=>softokn_params_string)
50
58
  end
@@ -16,7 +16,7 @@ class TestPkcs11 < Test::Unit::TestCase
16
16
  def pk
17
17
  @pk
18
18
  end
19
-
19
+
20
20
  def test_info
21
21
  info = pk.info
22
22
  assert info.inspect =~ /cryptokiVersion=/, 'There should be a version in the library info'
@@ -34,14 +34,14 @@ class TestPkcs11 < Test::Unit::TestCase
34
34
 
35
35
  @pk = PKCS11.open
36
36
  pk.load_library(find_softokn)
37
-
37
+
38
38
  pk.C_GetFunctionList
39
-
39
+
40
40
  pargs = PKCS11::CK_C_INITIALIZE_ARGS.new
41
41
  pargs.flags = 0
42
42
  pargs.pReserved = softokn_params.join(" ")
43
43
  pk.C_Initialize(pargs)
44
-
44
+
45
45
  pk.info
46
46
  end
47
47
  end
@@ -1,7 +1,6 @@
1
1
  require "test/unit"
2
2
  require "pkcs11"
3
3
  require "test/helper"
4
- require "openssl"
5
4
 
6
5
  class TestPkcs11Object < Test::Unit::TestCase
7
6
  include PKCS11
@@ -15,11 +14,11 @@ class TestPkcs11Object < Test::Unit::TestCase
15
14
  $pkcs11 ||= open_softokn
16
15
  @slots = pk.active_slots
17
16
  @slot = slots.last
18
-
17
+
19
18
  flags = CKF_SERIAL_SESSION #| CKF_RW_SESSION
20
19
  @session = slot.C_OpenSession(flags)
21
20
  # @session.login(:USER, "")
22
-
21
+
23
22
  # Create session object for tests.
24
23
  @object = session.create_object(
25
24
  :CLASS=>CKO_DATA,
@@ -55,11 +54,15 @@ class TestPkcs11Object < Test::Unit::TestCase
55
54
  end
56
55
 
57
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
58
  end
59
59
 
60
60
  def test_accessor
61
61
  assert_equal 'value', object[:VALUE], "Value should be readable"
62
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"
63
66
  end
64
67
 
65
68
  def test_attribute
@@ -71,19 +74,30 @@ class TestPkcs11Object < Test::Unit::TestCase
71
74
  def test_set_attribute
72
75
  object[:VALUE] = 'value2'
73
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"
74
80
  end
75
81
 
76
82
  def test_set_attributes
77
- object.attributes = {:VALUE => 'value2', PKCS11::CKA_APPLICATION => 'app2'}
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
78
93
 
79
- assert_equal 'value2', object[:VALUE], "Value should have changed"
80
- assert_equal 'app2', object[:APPLICATION], "App should have changed"
94
+ assert_nothing_raised{ object[] = [] }
81
95
  end
82
96
 
83
97
  def test_size
84
98
  assert object.size, 'There should be an object size'
85
99
  end
86
-
100
+
87
101
  def test_copy_without_params
88
102
  new_obj = object.copy
89
103
  new_obj[:APPLICATION] = 'Copied object'
@@ -97,7 +111,7 @@ class TestPkcs11Object < Test::Unit::TestCase
97
111
  assert_equal 'Copied object', new_obj[:APPLICATION], "Application should be changed"
98
112
  assert_equal 'My Application', object[:APPLICATION], "Original object should be unchanged"
99
113
  end
100
-
114
+
101
115
  def test_destroy
102
116
  object.destroy
103
117
 
@@ -1,7 +1,6 @@
1
1
  require "test/unit"
2
2
  require "pkcs11"
3
3
  require "test/helper"
4
- require "openssl"
5
4
 
6
5
  class TestPkcs11Thread < Test::Unit::TestCase
7
6
  include PKCS11