Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

refacturing of C code to better allow vendor specific PKCS11-extensions

  • Loading branch information...
commit 818adb9de859ccf69fcb0b1ec260b91df8b98595 1 parent ca666a2
Lars Kanis authored
2  Manifest.txt
View
@@ -19,6 +19,8 @@ ext/include/pkcs11t.h
ext/pk11.c
ext/pk11.h
ext/pk11_const.c
+ext/pk11_const_macros.h
+ext/pk11_struct_macros.h
lib/pkcs11.rb
lib/pkcs11/extensions.rb
lib/pkcs11/helper.rb
4 Rakefile
View
@@ -53,8 +53,8 @@ file 'ext/pk11_struct_impl.inc' => 'ext/pk11_struct_def.inc'
file 'ext/pk11_const_def.inc' => 'ext/generate_constants.rb' do
sh "#{Config::CONFIG['ruby_install_name']} ext/generate_constants.rb --const ext/pk11_const_def.inc ext/include/pkcs11t.h"
end
-file 'ext/pk11.c' => 'ext/pk11_struct_def.inc'
-file 'ext/pk11_const.c' => 'ext/pk11_const_def.inc'
+file 'ext/pk11.c' => ['ext/pk11_struct_def.inc', 'ext/pk11_struct_impl.inc', 'ext/pk11_struct_macros.h']
+file 'ext/pk11_const.c' => ['ext/pk11_const_def.inc', 'ext/pk11_const_macros.h']
file 'ext/pk11_thread_funcs.h' => 'ext/generate_thread_funcs.rb' do
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"
50 ext/generate_structs.rb
View
@@ -10,6 +10,7 @@ class StructParser
attr_accessor :options
attr_accessor :structs
attr_accessor :structs_by_name
+ attr_accessor :std_structs_by_name
def self.run(argv)
s = self.new
@@ -31,10 +32,11 @@ def self.run(argv)
s.start!
end
- CStruct = Struct.new(:name, :attrs, :seq_nr)
- Attribute = Struct.new(:type, :name, :qual, :seq_nr, :mark)
+ CStruct = Struct.new(:name, :attrs)
+ Attribute = Struct.new(:type, :name, :qual, :mark)
IgnoreStructs = %w[CK_ATTRIBUTE CK_MECHANISM]
OnlyAllocatorStructs = %w[CK_MECHANISM_INFO CK_C_INITIALIZE_ARGS CK_INFO CK_SLOT_INFO CK_TOKEN_INFO CK_SESSION_INFO]
+ STRUCT_MODULE = 'PKCS11'
class CStruct
def attr_by_sign(key)
@@ -44,30 +46,30 @@ def attr_by_sign(key)
def parse_files(files)
structs = []
- sseq_nr = 0
files.each do |file_h|
c_src = IO.read(file_h)
c_src.scan(/struct\s+([A-Z_0-9]+)\s*\{(.*?)\}/m) do |struct|
struct_text = $2
- struct = CStruct.new( $1, [], sseq_nr )
+ struct = CStruct.new( $1, [] )
- aseq_nr = 0
struct_text.scan(/^\s+([A-Z_0-9]+)\s+([\w_]+)\s*(\[\s*(\d+)\s*\])?/) do |elem|
- struct.attrs << Attribute.new($1, $2, $4, aseq_nr)
- aseq_nr += 1
+ struct.attrs << Attribute.new($1, $2, $4)
end
structs << struct
- sseq_nr += 1
end
end
-
- @structs_by_name = structs.inject({}){|sum, v| sum[v.name]=v; sum }
- @structs = structs
+ return structs
end
def start!
- parse_files(options.files)
+ @structs = parse_files(options.files)
+ @structs_by_name = @structs.inject({}){|sum, v| sum[v.name]=v; sum }
+ @std_structs_by_name = @structs_by_name.dup
+ write_files
+ end
+
+ def write_files
File.open(options.def, "w") do |fd_def|
File.open(options.impl, "w") do |fd_impl|
File.open(options.doc, "w") do |fd_doc|
@@ -80,13 +82,13 @@ def start!
fd_impl.puts "PKCS11_IMPLEMENT_STRUCT_WITH_ALLOCATOR(#{struct.name});"
end
fd_def.puts "PKCS11_DEFINE_STRUCT(#{struct.name});"
- fd_doc.puts"class PKCS11::#{struct.name} < PKCS11::CStruct"
+ fd_doc.puts"class #{STRUCT_MODULE}::#{struct.name} < #{STRUCT_MODULE}::CStruct"
fd_doc.puts"# Size of corresponding C struct in bytes\nSIZEOF_STRUCT=Integer"
fd_doc.puts"# @return [String] Binary copy of the C struct\ndef to_s; end"
fd_doc.puts"# @return [Array<String>] Attributes of this struct\ndef members; end"
# try to find attributes belonging together
- struct.attrs.select{|attr| ['CK_BYTE_PTR', 'CK_VOID_PTR', 'CK_UTF8CHAR_PTR'].include?(attr.type) }.each do |attr|
+ struct.attrs.select{|attr| ['CK_BYTE_PTR', 'CK_VOID_PTR', 'CK_UTF8CHAR_PTR', 'CK_CHAR_PTR'].include?(attr.type) }.each do |attr|
if len_attr=struct.attr_by_sign("CK_ULONG #{attr.name.gsub(/^p/, "ul")}Len")
fd_impl.puts "PKCS11_IMPLEMENT_STRING_PTR_LEN_ACCESSOR(#{struct.name}, #{attr.name}, #{len_attr.name});"
fd_def.puts "PKCS11_DEFINE_MEMBER(#{struct.name}, #{attr.name});"
@@ -124,7 +126,7 @@ def start!
fd_impl.puts "PKCS11_IMPLEMENT_BYTE_ACCESSOR(#{struct.name}, #{attr.name});"
fd_def.puts "PKCS11_DEFINE_MEMBER(#{struct.name}, #{attr.name});"
fd_doc.puts"# @return [Integer] accessor for #{attr.name} (CK_BYTE)\nattr_accessor :#{attr.name}"
- when 'CK_ULONG', 'CK_FLAGS', 'CK_SLOT_ID', 'CK_STATE', /CK_[A-Z_0-9]+_TYPE/
+ when 'CK_ULONG', 'CK_FLAGS', 'CK_SLOT_ID', 'CK_STATE', 'CK_COUNT', 'CK_SIZE', /CK_[A-Z_0-9]+_TYPE/
fd_impl.puts "PKCS11_IMPLEMENT_ULONG_ACCESSOR(#{struct.name}, #{attr.name});"
fd_def.puts "PKCS11_DEFINE_MEMBER(#{struct.name}, #{attr.name});"
fd_doc.puts"# @return [Integer] accessor for #{attr.name} (CK_ULONG)\nattr_accessor :#{attr.name}"
@@ -136,7 +138,7 @@ def start!
fd_impl.puts "PKCS11_IMPLEMENT_BOOL_ACCESSOR(#{struct.name}, #{attr.name});"
fd_def.puts "PKCS11_DEFINE_MEMBER(#{struct.name}, #{attr.name});"
fd_doc.puts"# @return [Boolean] Bool value\nattr_accessor :#{attr.name}"
- when 'CK_ULONG_PTR'
+ when 'CK_ULONG_PTR', 'CK_COUNT_PTR'
fd_impl.puts "PKCS11_IMPLEMENT_ULONG_PTR_ACCESSOR(#{struct.name}, #{attr.name});"
fd_def.puts "PKCS11_DEFINE_MEMBER(#{struct.name}, #{attr.name});"
fd_doc.puts"# @return [Integer, nil] accessor for #{attr.name} (CK_ULONG_PTR)\nattr_accessor :#{attr.name}"
@@ -145,11 +147,19 @@ def start!
if structs_by_name[attr.type]
fd_impl.puts "PKCS11_IMPLEMENT_STRUCT_ACCESSOR(#{struct.name}, #{attr.type}, #{attr.name});"
fd_def.puts "PKCS11_DEFINE_MEMBER(#{struct.name}, #{attr.name});"
- fd_doc.puts"# @return [PKCS11::#{attr.type}] inline struct\nattr_accessor :#{attr.name}"
- elsif structs_by_name[attr.type.gsub(/_PTR$/,'')]
- fd_impl.puts "PKCS11_IMPLEMENT_STRUCT_PTR_ACCESSOR(#{struct.name}, #{attr.type.gsub(/_PTR$/,'')}, #{attr.name});"
+ fd_doc.puts"# @return [#{STRUCT_MODULE}::#{attr.type}] inline struct\nattr_accessor :#{attr.name}"
+ elsif (attr_noptr=attr.type.gsub(/_PTR$/,'')) && structs_by_name[attr_noptr]
+ fd_impl.puts "PKCS11_IMPLEMENT_STRUCT_PTR_ACCESSOR(#{struct.name}, #{attr_noptr}, #{attr.name});"
+ fd_def.puts "PKCS11_DEFINE_MEMBER(#{struct.name}, #{attr.name});"
+ fd_doc.puts"# @return [#{STRUCT_MODULE}::#{attr_noptr}, nil] pointer to struct\nattr_accessor :#{attr.name}"
+ elsif std_structs_by_name[attr.type]
+ fd_impl.puts "PKCS11_IMPLEMENT_PKCS11_STRUCT_ACCESSOR(#{struct.name}, #{attr.type}, #{attr.name});"
+ fd_def.puts "PKCS11_DEFINE_MEMBER(#{struct.name}, #{attr.name});"
+ fd_doc.puts"# @return [PKCS11::#{attr.type}] inline struct (see pkcs11.gem)\nattr_accessor :#{attr.name}"
+ elsif (attr_noptr=attr.type.gsub(/_PTR$/,'')) && std_structs_by_name[attr_noptr]
+ fd_impl.puts "PKCS11_IMPLEMENT_PKCS11_STRUCT_PTR_ACCESSOR(#{struct.name}, #{attr_noptr}, #{attr.name});"
fd_def.puts "PKCS11_DEFINE_MEMBER(#{struct.name}, #{attr.name});"
- fd_doc.puts"# @return [PKCS11::#{attr.type.gsub(/_PTR$/,'')}, nil] pointer to struct\nattr_accessor :#{attr.name}"
+ fd_doc.puts"# @return [PKCS11::#{attr_noptr}, nil] pointer to struct (see pkcs11.gem)\nattr_accessor :#{attr.name}"
else
fd_impl.puts "/* unimplemented attr #{attr.type} #{attr.name} #{attr.qual} */"
fd_def.puts "/* unimplemented attr #{attr.type} #{attr.name} #{attr.qual} */"
347 ext/pk11.c
View
@@ -1,4 +1,5 @@
#include "pk11.h"
+#include "pk11_struct_macros.h"
#if defined(compile_for_windows)
#include <winbase.h> /* for LoadLibrary() */
@@ -30,21 +31,14 @@ static VALUE aCK_SESSION_INFO_members;
static VALUE cCK_MECHANISM;
static VALUE aCK_MECHANISM_members;
-#define HANDLE2NUM(n) ULONG2NUM(n)
-#define NUM2HANDLE(n) PKNUM2ULONG(n)
-#define PKNUM2ULONG(n) pkcs11_num2ulong(n)
+#define MODULE_FOR_STRUCTS mPKCS11
+#define BASECLASS_FOR_STRUCTS cCStruct
#define pkcs11_new_struct(klass) rb_funcall(klass, sNEW, 0)
-VALUE pkcs11_return_value_to_class(CK_RV, VALUE);
+#define PKCS11_DEFINE_METHOD(name, args) \
+ rb_define_method(cPKCS11, #name, pkcs11_##name, args);
-static VALUE
-pkcs11_num2ulong(VALUE val)
-{
- if (TYPE(val) == T_BIGNUM || TYPE(val) == T_FIXNUM) {
- return NUM2ULONG(val);
- }
- return NUM2ULONG(rb_to_int(val));
-}
+VALUE pkcs11_return_value_to_class(CK_RV, VALUE);
static void
pkcs11_raise(CK_RV rv)
@@ -1451,313 +1445,6 @@ ck_attr_value(VALUE self)
///////////////////////////////////////
-static VALUE
-get_string(VALUE obj, off_t offset, size_t size)
-{
- char *ptr = (char*)DATA_PTR(obj);
- return rb_str_new(ptr+offset, size);
-}
-
-static VALUE
-set_string(VALUE obj, VALUE value, off_t offset, size_t size)
-{
- char *ptr = (char*)DATA_PTR(obj);
- int len = size;
- StringValue(value);
- if (RSTRING_LEN(value) < len) len = RSTRING_LEN(value);
- memset(ptr+offset, 0, size);
- memcpy(ptr+offset, RSTRING_PTR(value), len);
- return value;
-}
-
-static VALUE
-get_ulong(VALUE obj, off_t offset)
-{
- char *ptr = (char*)DATA_PTR(obj);
- return ULONG2NUM(*(CK_ULONG_PTR)(ptr+offset));
-}
-
-static VALUE
-set_ulong(VALUE obj, VALUE value, off_t offset)
-{
- char *ptr = (char*)DATA_PTR(obj);
- *(CK_ULONG_PTR)(ptr+offset) = NUM2ULONG(value);
- return value;
-}
-
-static VALUE
-get_byte(VALUE obj, off_t offset)
-{
- char *ptr = (char*)DATA_PTR(obj);
- return ULONG2NUM(*(CK_BYTE_PTR)(ptr+offset));
-}
-
-static VALUE
-set_byte(VALUE obj, VALUE value, off_t offset)
-{
- char *ptr = (char*)DATA_PTR(obj);
- *(CK_BYTE_PTR)(ptr+offset) = NUM2ULONG(value);
- return value;
-}
-
-static VALUE
-get_ulong_ptr(VALUE obj, off_t offset)
-{
- char *ptr = (char*)DATA_PTR(obj);
- CK_ULONG_PTR p = *(CK_ULONG_PTR *)(ptr+offset);
- if (!p) return Qnil;
- return ULONG2NUM(*p);
-}
-
-static VALUE
-set_ulong_ptr(VALUE obj, VALUE value, const char *name, off_t offset)
-{
- CK_ULONG_PTR *ptr = (CK_ULONG_PTR *)((char*)DATA_PTR(obj) + offset);
- if (NIL_P(value)){
- rb_iv_set(obj, name, value);
- *ptr = NULL_PTR;
- return value;
- }
- VALUE new_obj = Data_Make_Struct(rb_cInteger, CK_ULONG, 0, free, *ptr);
- rb_iv_set(obj, name, new_obj);
- **ptr = NUM2ULONG(value);
- return value;
-}
-
-static VALUE
-get_handle(VALUE obj, off_t offset)
-{
- char *ptr = (char*)DATA_PTR(obj);
- return HANDLE2NUM(*(CK_OBJECT_HANDLE_PTR)(ptr+offset));
-}
-
-static VALUE
-set_handle(VALUE obj, VALUE value, off_t offset)
-{
- char *ptr = (char*)DATA_PTR(obj);
- *(CK_OBJECT_HANDLE_PTR)(ptr+offset) = NUM2HANDLE(value);
- return value;
-}
-
-static VALUE
-get_bool(VALUE obj, off_t offset)
-{
- char *ptr = (char*)DATA_PTR(obj);
- if(*(CK_BBOOL*)(ptr+offset)) return Qtrue;
- else return Qfalse;
-}
-
-static VALUE
-set_bool(VALUE obj, VALUE value, off_t offset)
-{
- char *ptr = (char*)DATA_PTR(obj);
- if(value == Qfalse) *(CK_BBOOL*)(ptr+offset) = 0;
- else if(value == Qtrue) *(CK_BBOOL*)(ptr+offset) = 1;
- else rb_raise(rb_eArgError, "arg must be true or false");
- return value;
-}
-
-static VALUE
-get_string_ptr(VALUE obj, const char *name, off_t offset)
-{
- char *ptr = (char*)DATA_PTR(obj);
- char *p = *(char**)(ptr+offset);
- if (!p) return Qnil;
- return rb_str_new2(p);
-}
-
-static VALUE
-set_string_ptr(VALUE obj, VALUE value, const char *name, off_t offset)
-{
- char *ptr = (char*)DATA_PTR(obj);
- if (NIL_P(value)){
- rb_iv_set(obj, name, value);
- *(CK_VOID_PTR*)(ptr+offset) = NULL_PTR;
- return value;
- }
- StringValue(value);
- value = rb_obj_freeze(rb_str_dup(value));
- rb_iv_set(obj, name, value);
- *(CK_VOID_PTR*)(ptr+offset) = RSTRING_PTR(value);
- return value;
-}
-
-static VALUE
-get_string_ptr_len(VALUE obj, const char *name, off_t offset, off_t offset_len)
-{
- unsigned long l;
- char *ptr = (char*)DATA_PTR(obj);
- char *p = *(char**)(ptr+offset);
- if (!p) return Qnil;
- l = *(unsigned long*)(ptr+offset_len);
- return rb_str_new(p, l);
-}
-
-static VALUE
-set_string_ptr_len(VALUE obj, VALUE value, const char *name, off_t offset, off_t offset_len)
-{
- char *ptr = (char*)DATA_PTR(obj);
- if (NIL_P(value)){
- rb_iv_set(obj, name, value);
- *(CK_VOID_PTR*)(ptr+offset) = NULL_PTR;
- *(unsigned long*)(ptr+offset_len) = 0;
- return value;
- }
- StringValue(value);
- value = rb_obj_freeze(rb_str_dup(value));
- rb_iv_set(obj, name, value);
- *(CK_VOID_PTR*)(ptr+offset) = RSTRING_PTR(value);
- *(unsigned long*)(ptr+offset_len) = RSTRING_LEN(value);
- return value;
-}
-
-static VALUE
-get_struct_inline(VALUE obj, VALUE klass, const char *name, off_t offset)
-{
- char *ptr = (char*)DATA_PTR(obj) + offset;
- VALUE inline_obj = Data_Wrap_Struct(klass, 0, 0, ptr);
- rb_iv_set(inline_obj, name, obj);
- return inline_obj;
-}
-
-static VALUE
-set_struct_inline(VALUE obj, VALUE klass, const char *struct_name, VALUE value, const char *name, off_t offset, int sizeofstruct)
-{
- char *ptr = (char*)DATA_PTR(obj) + offset;
- if (!rb_obj_is_kind_of(value, klass))
- rb_raise(rb_eArgError, "arg must be a PKCS11::%s", struct_name);
- memcpy(ptr, DATA_PTR(value), sizeofstruct);
- return value;
-}
-
-static VALUE
-get_struct_ptr(VALUE obj, VALUE klass, const char *name, off_t offset)
-{
- char *ptr = (char*)DATA_PTR(obj);
- char *p = *(char**)(ptr+offset);
- if (!p) return Qnil;
- return rb_iv_get(obj, name);
-}
-
-static VALUE
-set_struct_ptr(VALUE obj, VALUE klass, const char *struct_name, VALUE value, const char *name, off_t offset)
-{
- char *ptr = (char*)DATA_PTR(obj) + offset;
- if (NIL_P(value)){
- rb_iv_set(obj, name, value);
- *(CK_VOID_PTR*)ptr = NULL_PTR;
- return value;
- }
- if (!rb_obj_is_kind_of(value, klass))
- rb_raise(rb_eArgError, "arg must be a PKCS11::%s", struct_name);
- *(CK_VOID_PTR*)ptr = DATA_PTR(value);
- rb_iv_set(obj, name, value);
- return value;
-}
-
-#define OFFSET_OF(s, f) ((off_t)((char*)&(((s*)0)->f) - (char*)0))
-#define SIZE_OF(s, f) (sizeof(((s*)0)->f))
-
-#define PKCS11_IMPLEMENT_ALLOCATOR(s) \
-static VALUE s##_s_alloc(VALUE self){ \
- s *info; \
- VALUE obj = Data_Make_Struct(self, s, 0, -1, info); \
- return obj; \
-} \
-static VALUE c##s##_to_s(VALUE self){ \
- return rb_str_new(DATA_PTR(self), sizeof(s)); \
-} \
-static VALUE c##s##_members(VALUE self){ \
- return a##s##_members; \
-}
-
-#define PKCS11_IMPLEMENT_STRUCT_WITH_ALLOCATOR(s) \
-static VALUE c##s;\
-static VALUE a##s##_members;\
-PKCS11_IMPLEMENT_ALLOCATOR(s);
-
-#define PKCS11_IMPLEMENT_STRING_ACCESSOR(s, f) \
-static VALUE c##s##_get_##f(VALUE o){ \
- return get_string(o, OFFSET_OF(s, f), SIZE_OF(s, f)); \
-} \
-static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
- return set_string(o, v, OFFSET_OF(s, f), SIZE_OF(s, f)); \
-}
-
-#define PKCS11_IMPLEMENT_ULONG_ACCESSOR(s, f) \
-static VALUE c##s##_get_##f(VALUE o){ \
- return get_ulong(o, OFFSET_OF(s, f)); \
-} \
-static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
- return set_ulong(o, v, OFFSET_OF(s, f)); \
-}
-
-#define PKCS11_IMPLEMENT_BYTE_ACCESSOR(s, f) \
-static VALUE c##s##_get_##f(VALUE o){ \
- return get_byte(o, OFFSET_OF(s, f)); \
-} \
-static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
- return set_byte(o, v, OFFSET_OF(s, f)); \
-}
-
-#define PKCS11_IMPLEMENT_ULONG_PTR_ACCESSOR(s, f) \
-static VALUE c##s##_get_##f(VALUE o){ \
- return get_ulong_ptr(o, OFFSET_OF(s, f)); \
-} \
-static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
- return set_ulong_ptr(o, v, #f, OFFSET_OF(s, f)); \
-}
-
-#define PKCS11_IMPLEMENT_HANDLE_ACCESSOR(s, f) \
-static VALUE c##s##_get_##f(VALUE o){ \
- return get_handle(o, OFFSET_OF(s, f)); \
-} \
-static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
- return set_handle(o, v, OFFSET_OF(s, f)); \
-}
-
-#define PKCS11_IMPLEMENT_BOOL_ACCESSOR(s, f) \
-static VALUE c##s##_get_##f(VALUE o){ \
- return get_bool(o, OFFSET_OF(s, f)); \
-} \
-static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
- return set_bool(o, v, OFFSET_OF(s, f)); \
-}
-
-#define PKCS11_IMPLEMENT_STRING_PTR_ACCESSOR(s, f) \
-static VALUE c##s##_get_##f(VALUE o){ \
- return get_string_ptr(o, #f, OFFSET_OF(s, f)); \
-} \
-static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
- return set_string_ptr(o, v, #f, OFFSET_OF(s, f)); \
-}
-
-#define PKCS11_IMPLEMENT_STRING_PTR_LEN_ACCESSOR(s, f, l) \
-static VALUE c##s##_get_##f(VALUE o){ \
- return get_string_ptr_len(o, #f, OFFSET_OF(s, f), OFFSET_OF(s, l)); \
-} \
-static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
- return set_string_ptr_len(o, v, #f, OFFSET_OF(s, f), OFFSET_OF(s, l)); \
-}
-
-#define PKCS11_IMPLEMENT_STRUCT_ACCESSOR(s, k, f) \
-static VALUE c##s##_get_##f(VALUE o){ \
- return get_struct_inline(o, c##k, #f, OFFSET_OF(s, f)); \
-} \
-static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
- return set_struct_inline(o, c##k, #k, v, #f, OFFSET_OF(s, f), sizeof(k)); \
-}
-
-#define PKCS11_IMPLEMENT_STRUCT_PTR_ACCESSOR(s, k, f) \
-static VALUE c##s##_get_##f(VALUE o){ \
- return get_struct_ptr(o, c##k, #f, OFFSET_OF(s, f)); \
-} \
-static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
- return set_struct_ptr(o, c##k, #k, v, #f, OFFSET_OF(s, f)); \
-}
-
-///////////////////////////////////////
-
#include "pk11_struct_impl.inc"
///////////////////////////////////////
@@ -1831,28 +1518,6 @@ cCK_MECHANISM_set_pParameter(VALUE self, VALUE value)
return value;
}
-///////////////////////////////////////
-
-#define PKCS11_DEFINE_METHOD(name, args) \
- rb_define_method(cPKCS11, #name, pkcs11_##name, args);
-
-#define PKCS11_DEFINE_STRUCT(s) \
- do { \
- c##s = rb_define_class_under(mPKCS11, #s, cCStruct); \
- a##s##_members = rb_ary_new(); \
- rb_define_alloc_func(c##s, s##_s_alloc); \
- rb_define_const(c##s, "SIZEOF_STRUCT", ULONG2NUM(sizeof(s))); \
- rb_define_method(c##s, "to_s", c##s##_to_s, 0); \
- rb_define_method(c##s, "members", c##s##_members, 0); \
- rb_iv_set(c##s, "members", a##s##_members); \
- } while(0)
-
-#define PKCS11_DEFINE_MEMBER(s, f) \
- do { \
- rb_define_method(c##s, #f, c##s##_get_##f, 0); \
- rb_define_method(c##s, #f "=", c##s##_set_##f, 1); \
- rb_ary_push(a##s##_members, rb_str_new2(#f)); \
- } while(0)
void
Init_pkcs11_ext()
434 ext/pk11_const.c
View
@@ -1,229 +1,205 @@
-#include "pk11.h"
-
-#define PKCS11_DEFINE_CONST(constant) \
- rb_define_const(cPKCS11, #constant, INT2NUM(constant))
-
-#define PKCS11_DEFINE_CONST_GROUP(group, name, value) \
- do { \
- VALUE rvalue = ULONG2NUM(value); \
- rb_define_const(cPKCS11, name, rvalue); \
- VALUE str = rb_obj_freeze(rb_str_new2(name)); \
- VALUE old = rb_hash_aref(group, rvalue); \
- if (!NIL_P(old)) rb_warning("%s is equal to %s", RSTRING_PTR(old), name); \
- rb_hash_aset(group, rvalue, str); \
- } while(0)
-
-#define PKCS11_DEFINE_OBJECT_CLASS(constant) \
- PKCS11_DEFINE_CONST_GROUP(vOBJECT_CLASSES, #constant, constant)
-#define PKCS11_DEFINE_ATTRIBUTE(constant) \
- PKCS11_DEFINE_CONST_GROUP(vATTRIBUTES, #constant, constant)
-#define PKCS11_DEFINE_MECHANISM(constant) \
- PKCS11_DEFINE_CONST_GROUP(vMECHANISMS, #constant, constant)
-#define PKCS11_DEFINE_RETURN_VALUE(constant) \
- do { \
- VALUE eError = rb_define_class_under(cPKCS11, #constant, ePKCS11Error); \
- VALUE rvalue = ULONG2NUM(constant); \
- VALUE old = rb_hash_aref(vRETURN_VALUES, rvalue); \
- if (!NIL_P(old)) rb_warning("%s is equal to %s", RSTRING_PTR(old), #constant); \
- rb_hash_aset(vRETURN_VALUES, rvalue, eError); \
- } while(0)
-
-static VALUE vOBJECT_CLASSES;
-static VALUE vATTRIBUTES;
-static VALUE vMECHANISMS;
-static VALUE vRETURN_VALUES;
-
-VALUE
-pkcs11_return_value_to_class(CK_RV rv, VALUE ePKCS11Error)
-{
- VALUE class;
-
- class = rb_hash_aref(vRETURN_VALUES, INT2NUM(rv));
- if (NIL_P(class)){
- class = ePKCS11Error;
- }
-
- return class;
-}
-
-void
-Init_pkcs11_const(VALUE cPKCS11, VALUE ePKCS11Error)
-{
- PKCS11_DEFINE_CONST(CRYPTOKI_VERSION_MAJOR);
- PKCS11_DEFINE_CONST(CRYPTOKI_VERSION_MINOR);
- PKCS11_DEFINE_CONST(CRYPTOKI_VERSION_AMENDMENT);
-
- /* General constant */
- PKCS11_DEFINE_CONST(CK_UNAVAILABLE_INFORMATION);
- PKCS11_DEFINE_CONST(CK_EFFECTIVELY_INFINITE);
- PKCS11_DEFINE_CONST(CK_INVALID_HANDLE);
-
- /* Notification */
- PKCS11_DEFINE_CONST(CKN_SURRENDER);
-
- /* Bit flag */
- PKCS11_DEFINE_CONST(CKF_TOKEN_PRESENT);
- PKCS11_DEFINE_CONST(CKF_REMOVABLE_DEVICE);
- PKCS11_DEFINE_CONST(CKF_HW_SLOT);
- PKCS11_DEFINE_CONST(CKF_RNG);
- PKCS11_DEFINE_CONST(CKF_WRITE_PROTECTED);
- PKCS11_DEFINE_CONST(CKF_LOGIN_REQUIRED);
- PKCS11_DEFINE_CONST(CKF_USER_PIN_INITIALIZED);
- PKCS11_DEFINE_CONST(CKF_RESTORE_KEY_NOT_NEEDED);
- PKCS11_DEFINE_CONST(CKF_CLOCK_ON_TOKEN);
- PKCS11_DEFINE_CONST(CKF_PROTECTED_AUTHENTICATION_PATH);
- PKCS11_DEFINE_CONST(CKF_DUAL_CRYPTO_OPERATIONS);
- PKCS11_DEFINE_CONST(CKF_TOKEN_INITIALIZED);
- PKCS11_DEFINE_CONST(CKF_SECONDARY_AUTHENTICATION);
- PKCS11_DEFINE_CONST(CKF_USER_PIN_COUNT_LOW);
- PKCS11_DEFINE_CONST(CKF_USER_PIN_FINAL_TRY);
- PKCS11_DEFINE_CONST(CKF_USER_PIN_LOCKED);
- PKCS11_DEFINE_CONST(CKF_USER_PIN_TO_BE_CHANGED);
- PKCS11_DEFINE_CONST(CKF_SO_PIN_COUNT_LOW);
- PKCS11_DEFINE_CONST(CKF_SO_PIN_FINAL_TRY);
- PKCS11_DEFINE_CONST(CKF_SO_PIN_LOCKED);
- PKCS11_DEFINE_CONST(CKF_SO_PIN_TO_BE_CHANGED);
- PKCS11_DEFINE_CONST(CKF_RW_SESSION);
- PKCS11_DEFINE_CONST(CKF_SERIAL_SESSION);
- PKCS11_DEFINE_CONST(CKF_LIBRARY_CANT_CREATE_OS_THREADS);
- PKCS11_DEFINE_CONST(CKF_OS_LOCKING_OK);
- PKCS11_DEFINE_CONST(CKF_DONT_BLOCK);
- PKCS11_DEFINE_CONST(CKF_HW);
- PKCS11_DEFINE_CONST(CKF_ENCRYPT);
- PKCS11_DEFINE_CONST(CKF_DECRYPT);
- PKCS11_DEFINE_CONST(CKF_DIGEST);
- PKCS11_DEFINE_CONST(CKF_SIGN);
- PKCS11_DEFINE_CONST(CKF_SIGN_RECOVER);
- PKCS11_DEFINE_CONST(CKF_VERIFY);
- PKCS11_DEFINE_CONST(CKF_VERIFY_RECOVER);
- PKCS11_DEFINE_CONST(CKF_GENERATE);
- PKCS11_DEFINE_CONST(CKF_GENERATE_KEY_PAIR);
- PKCS11_DEFINE_CONST(CKF_WRAP);
- PKCS11_DEFINE_CONST(CKF_UNWRAP);
- PKCS11_DEFINE_CONST(CKF_DERIVE);
- PKCS11_DEFINE_CONST(CKF_EC_F_P);
- PKCS11_DEFINE_CONST(CKF_EC_F_2M);
- PKCS11_DEFINE_CONST(CKF_EC_ECPARAMETERS);
- PKCS11_DEFINE_CONST(CKF_EC_NAMEDCURVE);
- PKCS11_DEFINE_CONST(CKF_EC_UNCOMPRESS);
- PKCS11_DEFINE_CONST(CKF_EC_COMPRESS);
- PKCS11_DEFINE_CONST(CKF_EXTENSION);
- PKCS11_DEFINE_CONST(CKF_ARRAY_ATTRIBUTE);
- PKCS11_DEFINE_CONST(CKF_EXCLUDE_CHALLENGE);
- PKCS11_DEFINE_CONST(CKF_EXCLUDE_COUNTER);
- PKCS11_DEFINE_CONST(CKF_EXCLUDE_PIN);
- PKCS11_DEFINE_CONST(CKF_EXCLUDE_TIME);
- PKCS11_DEFINE_CONST(CKF_NEXT_OTP);
- PKCS11_DEFINE_CONST(CKF_USER_FRIENDLY_OTP);
-
- /* User type */
- PKCS11_DEFINE_CONST(CKU_SO);
- PKCS11_DEFINE_CONST(CKU_USER);
- PKCS11_DEFINE_CONST(CKU_CONTEXT_SPECIFIC);
-
- /* Hardware feature type */
- PKCS11_DEFINE_CONST(CKH_MONOTONIC_COUNTER);
- PKCS11_DEFINE_CONST(CKH_CLOCK);
- PKCS11_DEFINE_CONST(CKH_USER_INTERFACE);
- PKCS11_DEFINE_CONST(CKH_VENDOR_DEFINED);
-
- /* Key type */
- PKCS11_DEFINE_CONST(CKK_RSA);
- PKCS11_DEFINE_CONST(CKK_DSA);
- PKCS11_DEFINE_CONST(CKK_DH);
- PKCS11_DEFINE_CONST(CKK_ECDSA);
- PKCS11_DEFINE_CONST(CKK_EC);
- PKCS11_DEFINE_CONST(CKK_X9_42_DH);
- PKCS11_DEFINE_CONST(CKK_KEA);
- PKCS11_DEFINE_CONST(CKK_GENERIC_SECRET);
- PKCS11_DEFINE_CONST(CKK_RC2);
- PKCS11_DEFINE_CONST(CKK_RC4);
- PKCS11_DEFINE_CONST(CKK_DES);
- PKCS11_DEFINE_CONST(CKK_DES2);
- PKCS11_DEFINE_CONST(CKK_DES3);
- PKCS11_DEFINE_CONST(CKK_CAST);
- PKCS11_DEFINE_CONST(CKK_CAST3);
- PKCS11_DEFINE_CONST(CKK_CAST5);
- PKCS11_DEFINE_CONST(CKK_CAST128);
- PKCS11_DEFINE_CONST(CKK_RC5);
- PKCS11_DEFINE_CONST(CKK_IDEA);
- PKCS11_DEFINE_CONST(CKK_SKIPJACK);
- PKCS11_DEFINE_CONST(CKK_BATON);
- PKCS11_DEFINE_CONST(CKK_JUNIPER);
- PKCS11_DEFINE_CONST(CKK_CDMF);
- PKCS11_DEFINE_CONST(CKK_AES);
- PKCS11_DEFINE_CONST(CKK_ACTI);
- PKCS11_DEFINE_CONST(CKK_ARIA);
- PKCS11_DEFINE_CONST(CKK_BLOWFISH);
- PKCS11_DEFINE_CONST(CKK_CAMELLIA);
- PKCS11_DEFINE_CONST(CKK_HOTP);
- PKCS11_DEFINE_CONST(CKK_SECURID);
- PKCS11_DEFINE_CONST(CKK_TWOFISH);
- PKCS11_DEFINE_CONST(CKK_VENDOR_DEFINED);
-
- /* Certificate type */
- PKCS11_DEFINE_CONST(CKC_X_509);
- PKCS11_DEFINE_CONST(CKC_X_509_ATTR_CERT);
- PKCS11_DEFINE_CONST(CKC_WTLS);
- PKCS11_DEFINE_CONST(CKC_VENDOR_DEFINED);
-
- /* Session state */
- PKCS11_DEFINE_CONST(CKS_RO_PUBLIC_SESSION);
- PKCS11_DEFINE_CONST(CKS_RO_USER_FUNCTIONS);
- PKCS11_DEFINE_CONST(CKS_RW_PUBLIC_SESSION);
- PKCS11_DEFINE_CONST(CKS_RW_USER_FUNCTIONS);
- PKCS11_DEFINE_CONST(CKS_RW_SO_FUNCTIONS);
-
- /* Mask generation function */
- PKCS11_DEFINE_CONST(CKG_MGF1_SHA1);
- PKCS11_DEFINE_CONST(CKG_MGF1_SHA224);
- PKCS11_DEFINE_CONST(CKG_MGF1_SHA256);
- PKCS11_DEFINE_CONST(CKG_MGF1_SHA384);
- PKCS11_DEFINE_CONST(CKG_MGF1_SHA512);
-
- /* Key delivation function */
- PKCS11_DEFINE_CONST(CKD_NULL);
- PKCS11_DEFINE_CONST(CKD_SHA1_KDF);
- PKCS11_DEFINE_CONST(CKD_SHA1_KDF_ASN1);
- PKCS11_DEFINE_CONST(CKD_SHA1_KDF_CONCATENATE);
-
- /* Pseudo-random function */
- PKCS11_DEFINE_CONST(CKP_PKCS5_PBKD2_HMAC_SHA1);
-
- /* Salt/Encoding parameter store */
- PKCS11_DEFINE_CONST(CKZ_DATA_SPECIFIED);
- PKCS11_DEFINE_CONST(CKZ_SALT_SPECIFIED);
-
- vOBJECT_CLASSES = rb_hash_new();
- vATTRIBUTES = rb_hash_new();
- vMECHANISMS = rb_hash_new();
- vRETURN_VALUES = rb_hash_new();
- rb_define_const(cPKCS11, "OBJECT_CLASSES", vOBJECT_CLASSES);
- rb_define_const(cPKCS11, "ATTRIBUTES", vATTRIBUTES);
- rb_define_const(cPKCS11, "MECHANISMS", vMECHANISMS);
- rb_define_const(cPKCS11, "RETURN_VALUES", vRETURN_VALUES);
-
- #include "pk11_const_def.inc"
-
- rb_obj_freeze(vOBJECT_CLASSES);
- rb_obj_freeze(vATTRIBUTES);
- rb_obj_freeze(vMECHANISMS);
- rb_obj_freeze(vRETURN_VALUES);
-
- /* OTP parameters */
- PKCS11_DEFINE_CONST(CK_OTP_CHALLENGE);
- PKCS11_DEFINE_CONST(CK_OTP_COUNTER);
- PKCS11_DEFINE_CONST(CK_OTP_FLAGS);
- PKCS11_DEFINE_CONST(CK_OTP_FORMAT_ALPHANUMERIC);
- PKCS11_DEFINE_CONST(CK_OTP_FORMAT_BINARY);
- PKCS11_DEFINE_CONST(CK_OTP_FORMAT_DECIMAL);
- PKCS11_DEFINE_CONST(CK_OTP_FORMAT_HEXADECIMAL);
- PKCS11_DEFINE_CONST(CK_OTP_OUTPUT_FORMAT);
- PKCS11_DEFINE_CONST(CK_OTP_OUTPUT_LENGTH);
- PKCS11_DEFINE_CONST(CK_OTP_PARAM_IGNORED);
- PKCS11_DEFINE_CONST(CK_OTP_PARAM_MANDATORY);
- PKCS11_DEFINE_CONST(CK_OTP_PARAM_OPTIONAL);
- PKCS11_DEFINE_CONST(CK_OTP_PIN);
- PKCS11_DEFINE_CONST(CK_OTP_TIME);
- PKCS11_DEFINE_CONST(CK_OTP_VALUE);
-}
+#include "pk11.h"
+#include "pk11_const_macros.h"
+
+#define MODULE_FOR_CONSTS cPKCS11
+#define BASECLASS_FOR_ERRORS ePKCS11Error
+
+static VALUE vOBJECT_CLASSES;
+static VALUE vATTRIBUTES;
+static VALUE vMECHANISMS;
+static VALUE vRETURN_VALUES;
+
+VALUE
+pkcs11_return_value_to_class(CK_RV rv, VALUE ePKCS11Error)
+{
+ VALUE class;
+
+ class = rb_hash_aref(vRETURN_VALUES, INT2NUM(rv));
+ if (NIL_P(class)){
+ class = ePKCS11Error;
+ }
+
+ return class;
+}
+
+void
+Init_pkcs11_const(VALUE cPKCS11, VALUE ePKCS11Error)
+{
+ PKCS11_DEFINE_CONST(CRYPTOKI_VERSION_MAJOR);
+ PKCS11_DEFINE_CONST(CRYPTOKI_VERSION_MINOR);
+ PKCS11_DEFINE_CONST(CRYPTOKI_VERSION_AMENDMENT);
+
+ /* General constant */
+ PKCS11_DEFINE_CONST(CK_UNAVAILABLE_INFORMATION);
+ PKCS11_DEFINE_CONST(CK_EFFECTIVELY_INFINITE);
+ PKCS11_DEFINE_CONST(CK_INVALID_HANDLE);
+
+ /* Notification */
+ PKCS11_DEFINE_CONST(CKN_SURRENDER);
+
+ /* Bit flag */
+ PKCS11_DEFINE_CONST(CKF_TOKEN_PRESENT);
+ PKCS11_DEFINE_CONST(CKF_REMOVABLE_DEVICE);
+ PKCS11_DEFINE_CONST(CKF_HW_SLOT);
+ PKCS11_DEFINE_CONST(CKF_RNG);
+ PKCS11_DEFINE_CONST(CKF_WRITE_PROTECTED);
+ PKCS11_DEFINE_CONST(CKF_LOGIN_REQUIRED);
+ PKCS11_DEFINE_CONST(CKF_USER_PIN_INITIALIZED);
+ PKCS11_DEFINE_CONST(CKF_RESTORE_KEY_NOT_NEEDED);
+ PKCS11_DEFINE_CONST(CKF_CLOCK_ON_TOKEN);
+ PKCS11_DEFINE_CONST(CKF_PROTECTED_AUTHENTICATION_PATH);
+ PKCS11_DEFINE_CONST(CKF_DUAL_CRYPTO_OPERATIONS);
+ PKCS11_DEFINE_CONST(CKF_TOKEN_INITIALIZED);
+ PKCS11_DEFINE_CONST(CKF_SECONDARY_AUTHENTICATION);
+ PKCS11_DEFINE_CONST(CKF_USER_PIN_COUNT_LOW);
+ PKCS11_DEFINE_CONST(CKF_USER_PIN_FINAL_TRY);
+ PKCS11_DEFINE_CONST(CKF_USER_PIN_LOCKED);
+ PKCS11_DEFINE_CONST(CKF_USER_PIN_TO_BE_CHANGED);
+ PKCS11_DEFINE_CONST(CKF_SO_PIN_COUNT_LOW);
+ PKCS11_DEFINE_CONST(CKF_SO_PIN_FINAL_TRY);
+ PKCS11_DEFINE_CONST(CKF_SO_PIN_LOCKED);
+ PKCS11_DEFINE_CONST(CKF_SO_PIN_TO_BE_CHANGED);
+ PKCS11_DEFINE_CONST(CKF_RW_SESSION);
+ PKCS11_DEFINE_CONST(CKF_SERIAL_SESSION);
+ PKCS11_DEFINE_CONST(CKF_LIBRARY_CANT_CREATE_OS_THREADS);
+ PKCS11_DEFINE_CONST(CKF_OS_LOCKING_OK);
+ PKCS11_DEFINE_CONST(CKF_DONT_BLOCK);
+ PKCS11_DEFINE_CONST(CKF_HW);
+ PKCS11_DEFINE_CONST(CKF_ENCRYPT);
+ PKCS11_DEFINE_CONST(CKF_DECRYPT);
+ PKCS11_DEFINE_CONST(CKF_DIGEST);
+ PKCS11_DEFINE_CONST(CKF_SIGN);
+ PKCS11_DEFINE_CONST(CKF_SIGN_RECOVER);
+ PKCS11_DEFINE_CONST(CKF_VERIFY);
+ PKCS11_DEFINE_CONST(CKF_VERIFY_RECOVER);
+ PKCS11_DEFINE_CONST(CKF_GENERATE);
+ PKCS11_DEFINE_CONST(CKF_GENERATE_KEY_PAIR);
+ PKCS11_DEFINE_CONST(CKF_WRAP);
+ PKCS11_DEFINE_CONST(CKF_UNWRAP);
+ PKCS11_DEFINE_CONST(CKF_DERIVE);
+ PKCS11_DEFINE_CONST(CKF_EC_F_P);
+ PKCS11_DEFINE_CONST(CKF_EC_F_2M);
+ PKCS11_DEFINE_CONST(CKF_EC_ECPARAMETERS);
+ PKCS11_DEFINE_CONST(CKF_EC_NAMEDCURVE);
+ PKCS11_DEFINE_CONST(CKF_EC_UNCOMPRESS);
+ PKCS11_DEFINE_CONST(CKF_EC_COMPRESS);
+ PKCS11_DEFINE_CONST(CKF_EXTENSION);
+ PKCS11_DEFINE_CONST(CKF_ARRAY_ATTRIBUTE);
+ PKCS11_DEFINE_CONST(CKF_EXCLUDE_CHALLENGE);
+ PKCS11_DEFINE_CONST(CKF_EXCLUDE_COUNTER);
+ PKCS11_DEFINE_CONST(CKF_EXCLUDE_PIN);
+ PKCS11_DEFINE_CONST(CKF_EXCLUDE_TIME);
+ PKCS11_DEFINE_CONST(CKF_NEXT_OTP);
+ PKCS11_DEFINE_CONST(CKF_USER_FRIENDLY_OTP);
+
+ /* User type */
+ PKCS11_DEFINE_CONST(CKU_SO);
+ PKCS11_DEFINE_CONST(CKU_USER);
+ PKCS11_DEFINE_CONST(CKU_CONTEXT_SPECIFIC);
+
+ /* Hardware feature type */
+ PKCS11_DEFINE_CONST(CKH_MONOTONIC_COUNTER);
+ PKCS11_DEFINE_CONST(CKH_CLOCK);
+ PKCS11_DEFINE_CONST(CKH_USER_INTERFACE);
+ PKCS11_DEFINE_CONST(CKH_VENDOR_DEFINED);
+
+ /* Key type */
+ PKCS11_DEFINE_CONST(CKK_RSA);
+ PKCS11_DEFINE_CONST(CKK_DSA);
+ PKCS11_DEFINE_CONST(CKK_DH);
+ PKCS11_DEFINE_CONST(CKK_ECDSA);
+ PKCS11_DEFINE_CONST(CKK_EC);
+ PKCS11_DEFINE_CONST(CKK_X9_42_DH);
+ PKCS11_DEFINE_CONST(CKK_KEA);
+ PKCS11_DEFINE_CONST(CKK_GENERIC_SECRET);
+ PKCS11_DEFINE_CONST(CKK_RC2);
+ PKCS11_DEFINE_CONST(CKK_RC4);
+ PKCS11_DEFINE_CONST(CKK_DES);
+ PKCS11_DEFINE_CONST(CKK_DES2);
+ PKCS11_DEFINE_CONST(CKK_DES3);
+ PKCS11_DEFINE_CONST(CKK_CAST);
+ PKCS11_DEFINE_CONST(CKK_CAST3);
+ PKCS11_DEFINE_CONST(CKK_CAST5);
+ PKCS11_DEFINE_CONST(CKK_CAST128);
+ PKCS11_DEFINE_CONST(CKK_RC5);
+ PKCS11_DEFINE_CONST(CKK_IDEA);
+ PKCS11_DEFINE_CONST(CKK_SKIPJACK);
+ PKCS11_DEFINE_CONST(CKK_BATON);
+ PKCS11_DEFINE_CONST(CKK_JUNIPER);
+ PKCS11_DEFINE_CONST(CKK_CDMF);
+ PKCS11_DEFINE_CONST(CKK_AES);
+ PKCS11_DEFINE_CONST(CKK_ACTI);
+ PKCS11_DEFINE_CONST(CKK_ARIA);
+ PKCS11_DEFINE_CONST(CKK_BLOWFISH);
+ PKCS11_DEFINE_CONST(CKK_CAMELLIA);
+ PKCS11_DEFINE_CONST(CKK_HOTP);
+ PKCS11_DEFINE_CONST(CKK_SECURID);
+ PKCS11_DEFINE_CONST(CKK_TWOFISH);
+ PKCS11_DEFINE_CONST(CKK_VENDOR_DEFINED);
+
+ /* Certificate type */
+ PKCS11_DEFINE_CONST(CKC_X_509);
+ PKCS11_DEFINE_CONST(CKC_X_509_ATTR_CERT);
+ PKCS11_DEFINE_CONST(CKC_WTLS);
+ PKCS11_DEFINE_CONST(CKC_VENDOR_DEFINED);
+
+ /* Session state */
+ PKCS11_DEFINE_CONST(CKS_RO_PUBLIC_SESSION);
+ PKCS11_DEFINE_CONST(CKS_RO_USER_FUNCTIONS);
+ PKCS11_DEFINE_CONST(CKS_RW_PUBLIC_SESSION);
+ PKCS11_DEFINE_CONST(CKS_RW_USER_FUNCTIONS);
+ PKCS11_DEFINE_CONST(CKS_RW_SO_FUNCTIONS);
+
+ /* Mask generation function */
+ PKCS11_DEFINE_CONST(CKG_MGF1_SHA1);
+ PKCS11_DEFINE_CONST(CKG_MGF1_SHA224);
+ PKCS11_DEFINE_CONST(CKG_MGF1_SHA256);
+ PKCS11_DEFINE_CONST(CKG_MGF1_SHA384);
+ PKCS11_DEFINE_CONST(CKG_MGF1_SHA512);
+
+ /* Key delivation function */
+ PKCS11_DEFINE_CONST(CKD_NULL);
+ PKCS11_DEFINE_CONST(CKD_SHA1_KDF);
+ PKCS11_DEFINE_CONST(CKD_SHA1_KDF_ASN1);
+ PKCS11_DEFINE_CONST(CKD_SHA1_KDF_CONCATENATE);
+
+ /* Pseudo-random function */
+ PKCS11_DEFINE_CONST(CKP_PKCS5_PBKD2_HMAC_SHA1);
+
+ /* Salt/Encoding parameter store */
+ PKCS11_DEFINE_CONST(CKZ_DATA_SPECIFIED);
+ PKCS11_DEFINE_CONST(CKZ_SALT_SPECIFIED);
+
+ vOBJECT_CLASSES = rb_hash_new();
+ vATTRIBUTES = rb_hash_new();
+ vMECHANISMS = rb_hash_new();
+ vRETURN_VALUES = rb_hash_new();
+ rb_define_const(cPKCS11, "OBJECT_CLASSES", vOBJECT_CLASSES);
+ rb_define_const(cPKCS11, "ATTRIBUTES", vATTRIBUTES);
+ rb_define_const(cPKCS11, "MECHANISMS", vMECHANISMS);
+ rb_define_const(cPKCS11, "RETURN_VALUES", vRETURN_VALUES);
+
+ #include "pk11_const_def.inc"
+
+ rb_obj_freeze(vOBJECT_CLASSES);
+ rb_obj_freeze(vATTRIBUTES);
+ rb_obj_freeze(vMECHANISMS);
+ rb_obj_freeze(vRETURN_VALUES);
+
+ /* OTP parameters */
+ PKCS11_DEFINE_CONST(CK_OTP_CHALLENGE);
+ PKCS11_DEFINE_CONST(CK_OTP_COUNTER);
+ PKCS11_DEFINE_CONST(CK_OTP_FLAGS);
+ PKCS11_DEFINE_CONST(CK_OTP_FORMAT_ALPHANUMERIC);
+ PKCS11_DEFINE_CONST(CK_OTP_FORMAT_BINARY);
+ PKCS11_DEFINE_CONST(CK_OTP_FORMAT_DECIMAL);
+ PKCS11_DEFINE_CONST(CK_OTP_FORMAT_HEXADECIMAL);
+ PKCS11_DEFINE_CONST(CK_OTP_OUTPUT_FORMAT);
+ PKCS11_DEFINE_CONST(CK_OTP_OUTPUT_LENGTH);
+ PKCS11_DEFINE_CONST(CK_OTP_PARAM_IGNORED);
+ PKCS11_DEFINE_CONST(CK_OTP_PARAM_MANDATORY);
+ PKCS11_DEFINE_CONST(CK_OTP_PARAM_OPTIONAL);
+ PKCS11_DEFINE_CONST(CK_OTP_PIN);
+ PKCS11_DEFINE_CONST(CK_OTP_TIME);
+ PKCS11_DEFINE_CONST(CK_OTP_VALUE);
+}
37 ext/pk11_const_macros.h
View
@@ -0,0 +1,37 @@
+#ifndef PK11_CONST_MACROS_INCLUDED
+#define PK11_CONST_MACROS_INCLUDED
+
+/**************************************************/
+/* constant definition */
+/**************************************************/
+
+#define PKCS11_DEFINE_CONST(constant) \
+ rb_define_const(MODULE_FOR_CONSTS, #constant, INT2NUM(constant))
+
+#define PKCS11_DEFINE_CONST_GROUP(group, name, value) \
+ do { \
+ VALUE rvalue = ULONG2NUM(value); \
+ rb_define_const(MODULE_FOR_CONSTS, name, rvalue); \
+ VALUE str = rb_obj_freeze(rb_str_new2(name)); \
+ VALUE old = rb_hash_aref(group, rvalue); \
+ if (!NIL_P(old)) rb_warning("%s is equal to %s", RSTRING_PTR(old), name); \
+ rb_hash_aset(group, rvalue, str); \
+ } while(0)
+
+#define PKCS11_DEFINE_OBJECT_CLASS(constant) \
+ PKCS11_DEFINE_CONST_GROUP(vOBJECT_CLASSES, #constant, constant)
+#define PKCS11_DEFINE_ATTRIBUTE(constant) \
+ PKCS11_DEFINE_CONST_GROUP(vATTRIBUTES, #constant, constant)
+#define PKCS11_DEFINE_MECHANISM(constant) \
+ PKCS11_DEFINE_CONST_GROUP(vMECHANISMS, #constant, constant)
+#define PKCS11_DEFINE_RETURN_VALUE(constant) \
+ do { \
+ VALUE eError = rb_define_class_under(MODULE_FOR_CONSTS, #constant, BASECLASS_FOR_ERRORS); \
+ VALUE rvalue = ULONG2NUM(constant); \
+ VALUE old = rb_hash_aref(vRETURN_VALUES, rvalue); \
+ if (!NIL_P(old)) rb_warning("%s is equal to %s", RSTRING_PTR(old), #constant); \
+ rb_hash_aset(vRETURN_VALUES, rvalue, eError); \
+ } while(0)
+
+
+#endif
369 ext/pk11_struct_macros.h
View
@@ -0,0 +1,369 @@
+#ifndef PK11_STRUCT_MACROS_INCLUDED
+#define PK11_STRUCT_MACROS_INCLUDED
+
+/**************************************************/
+/* struct/attribute implementation */
+/**************************************************/
+
+#define HANDLE2NUM(n) ULONG2NUM(n)
+#define NUM2HANDLE(n) PKNUM2ULONG(n)
+#define PKNUM2ULONG(n) pkcs11_num2ulong(n)
+
+static VALUE
+pkcs11_num2ulong(VALUE val)
+{
+ if (TYPE(val) == T_BIGNUM || TYPE(val) == T_FIXNUM) {
+ return NUM2ULONG(val);
+ }
+ return NUM2ULONG(rb_to_int(val));
+}
+
+static VALUE
+get_string(VALUE obj, off_t offset, size_t size)
+{
+ char *ptr = (char*)DATA_PTR(obj);
+ return rb_str_new(ptr+offset, size);
+}
+
+static VALUE
+set_string(VALUE obj, VALUE value, off_t offset, size_t size)
+{
+ char *ptr = (char*)DATA_PTR(obj);
+ int len = size;
+ StringValue(value);
+ if (RSTRING_LEN(value) < len) len = RSTRING_LEN(value);
+ memset(ptr+offset, 0, size);
+ memcpy(ptr+offset, RSTRING_PTR(value), len);
+ return value;
+}
+
+static VALUE
+get_ulong(VALUE obj, off_t offset)
+{
+ char *ptr = (char*)DATA_PTR(obj);
+ return ULONG2NUM(*(CK_ULONG_PTR)(ptr+offset));
+}
+
+static VALUE
+set_ulong(VALUE obj, VALUE value, off_t offset)
+{
+ char *ptr = (char*)DATA_PTR(obj);
+ *(CK_ULONG_PTR)(ptr+offset) = NUM2ULONG(value);
+ return value;
+}
+
+static VALUE
+get_byte(VALUE obj, off_t offset)
+{
+ char *ptr = (char*)DATA_PTR(obj);
+ return ULONG2NUM(*(CK_BYTE_PTR)(ptr+offset));
+}
+
+static VALUE
+set_byte(VALUE obj, VALUE value, off_t offset)
+{
+ char *ptr = (char*)DATA_PTR(obj);
+ *(CK_BYTE_PTR)(ptr+offset) = NUM2ULONG(value);
+ return value;
+}
+
+static VALUE
+get_ulong_ptr(VALUE obj, off_t offset)
+{
+ char *ptr = (char*)DATA_PTR(obj);
+ CK_ULONG_PTR p = *(CK_ULONG_PTR *)(ptr+offset);
+ if (!p) return Qnil;
+ return ULONG2NUM(*p);
+}
+
+static VALUE
+set_ulong_ptr(VALUE obj, VALUE value, const char *name, off_t offset)
+{
+ CK_ULONG_PTR *ptr = (CK_ULONG_PTR *)((char*)DATA_PTR(obj) + offset);
+ if (NIL_P(value)){
+ rb_iv_set(obj, name, value);
+ *ptr = NULL_PTR;
+ return value;
+ }
+ VALUE new_obj = Data_Make_Struct(rb_cInteger, CK_ULONG, 0, free, *ptr);
+ rb_iv_set(obj, name, new_obj);
+ **ptr = NUM2ULONG(value);
+ return value;
+}
+
+static VALUE
+get_handle(VALUE obj, off_t offset)
+{
+ char *ptr = (char*)DATA_PTR(obj);
+ return HANDLE2NUM(*(CK_OBJECT_HANDLE_PTR)(ptr+offset));
+}
+
+static VALUE
+set_handle(VALUE obj, VALUE value, off_t offset)
+{
+ char *ptr = (char*)DATA_PTR(obj);
+ *(CK_OBJECT_HANDLE_PTR)(ptr+offset) = NUM2HANDLE(value);
+ return value;
+}
+
+static VALUE
+get_bool(VALUE obj, off_t offset)
+{
+ char *ptr = (char*)DATA_PTR(obj);
+ if(*(CK_BBOOL*)(ptr+offset)) return Qtrue;
+ else return Qfalse;
+}
+
+static VALUE
+set_bool(VALUE obj, VALUE value, off_t offset)
+{
+ char *ptr = (char*)DATA_PTR(obj);
+ if(value == Qfalse) *(CK_BBOOL*)(ptr+offset) = 0;
+ else if(value == Qtrue) *(CK_BBOOL*)(ptr+offset) = 1;
+ else rb_raise(rb_eArgError, "arg must be true or false");
+ return value;
+}
+
+static VALUE
+get_string_ptr(VALUE obj, const char *name, off_t offset)
+{
+ char *ptr = (char*)DATA_PTR(obj);
+ char *p = *(char**)(ptr+offset);
+ if (!p) return Qnil;
+ return rb_str_new2(p);
+}
+
+static VALUE
+set_string_ptr(VALUE obj, VALUE value, const char *name, off_t offset)
+{
+ char *ptr = (char*)DATA_PTR(obj);
+ if (NIL_P(value)){
+ rb_iv_set(obj, name, value);
+ *(CK_VOID_PTR*)(ptr+offset) = NULL_PTR;
+ return value;
+ }
+ StringValue(value);
+ value = rb_obj_freeze(rb_str_dup(value));
+ rb_iv_set(obj, name, value);
+ *(CK_VOID_PTR*)(ptr+offset) = RSTRING_PTR(value);
+ return value;
+}
+
+static VALUE
+get_string_ptr_len(VALUE obj, const char *name, off_t offset, off_t offset_len)
+{
+ unsigned long l;
+ char *ptr = (char*)DATA_PTR(obj);
+ char *p = *(char**)(ptr+offset);
+ if (!p) return Qnil;
+ l = *(unsigned long*)(ptr+offset_len);
+ return rb_str_new(p, l);
+}
+
+static VALUE
+set_string_ptr_len(VALUE obj, VALUE value, const char *name, off_t offset, off_t offset_len)
+{
+ char *ptr = (char*)DATA_PTR(obj);
+ if (NIL_P(value)){
+ rb_iv_set(obj, name, value);
+ *(CK_VOID_PTR*)(ptr+offset) = NULL_PTR;
+ *(unsigned long*)(ptr+offset_len) = 0;
+ return value;
+ }
+ StringValue(value);
+ value = rb_obj_freeze(rb_str_dup(value));
+ rb_iv_set(obj, name, value);
+ *(CK_VOID_PTR*)(ptr+offset) = RSTRING_PTR(value);
+ *(unsigned long*)(ptr+offset_len) = RSTRING_LEN(value);
+ return value;
+}
+
+static VALUE
+get_struct_inline(VALUE obj, VALUE klass, const char *name, off_t offset)
+{
+ char *ptr = (char*)DATA_PTR(obj) + offset;
+ VALUE inline_obj = Data_Wrap_Struct(klass, 0, 0, ptr);
+ rb_iv_set(inline_obj, name, obj);
+ return inline_obj;
+}
+
+static VALUE
+set_struct_inline(VALUE obj, VALUE klass, const char *struct_name, VALUE value, const char *name, off_t offset, int sizeofstruct)
+{
+ char *ptr = (char*)DATA_PTR(obj) + offset;
+ if (!rb_obj_is_kind_of(value, klass))
+ rb_raise(rb_eArgError, "arg must be a PKCS11::%s", struct_name);
+ memcpy(ptr, DATA_PTR(value), sizeofstruct);
+ return value;
+}
+
+static VALUE
+get_struct_ptr(VALUE obj, VALUE klass, const char *name, off_t offset)
+{
+ char *ptr = (char*)DATA_PTR(obj);
+ char *p = *(char**)(ptr+offset);
+ if (!p) return Qnil;
+ return rb_iv_get(obj, name);
+}
+
+static VALUE
+set_struct_ptr(VALUE obj, VALUE klass, const char *struct_name, VALUE value, const char *name, off_t offset)
+{
+ char *ptr = (char*)DATA_PTR(obj) + offset;
+ if (NIL_P(value)){
+ rb_iv_set(obj, name, value);
+ *(CK_VOID_PTR*)ptr = NULL_PTR;
+ return value;
+ }
+ if (!rb_obj_is_kind_of(value, klass))
+ rb_raise(rb_eArgError, "arg must be a PKCS11::%s", struct_name);
+ *(CK_VOID_PTR*)ptr = DATA_PTR(value);
+ rb_iv_set(obj, name, value);
+ return value;
+}
+
+#define OFFSET_OF(s, f) ((off_t)((char*)&(((s*)0)->f) - (char*)0))
+#define SIZE_OF(s, f) (sizeof(((s*)0)->f))
+
+#define PKCS11_IMPLEMENT_ALLOCATOR(s) \
+static VALUE s##_s_alloc(VALUE self){ \
+ s *info; \
+ VALUE obj = Data_Make_Struct(self, s, 0, -1, info); \
+ return obj; \
+} \
+static VALUE c##s##_to_s(VALUE self){ \
+ return rb_str_new(DATA_PTR(self), sizeof(s)); \
+} \
+static VALUE c##s##_members(VALUE self){ \
+ return a##s##_members; \
+}
+
+#define PKCS11_IMPLEMENT_STRUCT_WITH_ALLOCATOR(s) \
+static VALUE c##s;\
+static VALUE a##s##_members;\
+PKCS11_IMPLEMENT_ALLOCATOR(s);
+
+#define PKCS11_IMPLEMENT_STRING_ACCESSOR(s, f) \
+static VALUE c##s##_get_##f(VALUE o){ \
+ return get_string(o, OFFSET_OF(s, f), SIZE_OF(s, f)); \
+} \
+static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
+ return set_string(o, v, OFFSET_OF(s, f), SIZE_OF(s, f)); \
+}
+
+#define PKCS11_IMPLEMENT_ULONG_ACCESSOR(s, f) \
+static VALUE c##s##_get_##f(VALUE o){ \
+ return get_ulong(o, OFFSET_OF(s, f)); \
+} \
+static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
+ return set_ulong(o, v, OFFSET_OF(s, f)); \
+}
+
+#define PKCS11_IMPLEMENT_BYTE_ACCESSOR(s, f) \
+static VALUE c##s##_get_##f(VALUE o){ \
+ return get_byte(o, OFFSET_OF(s, f)); \
+} \
+static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
+ return set_byte(o, v, OFFSET_OF(s, f)); \
+}
+
+#define PKCS11_IMPLEMENT_ULONG_PTR_ACCESSOR(s, f) \
+static VALUE c##s##_get_##f(VALUE o){ \
+ return get_ulong_ptr(o, OFFSET_OF(s, f)); \
+} \
+static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
+ return set_ulong_ptr(o, v, #f, OFFSET_OF(s, f)); \
+}
+
+#define PKCS11_IMPLEMENT_HANDLE_ACCESSOR(s, f) \
+static VALUE c##s##_get_##f(VALUE o){ \
+ return get_handle(o, OFFSET_OF(s, f)); \
+} \
+static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
+ return set_handle(o, v, OFFSET_OF(s, f)); \
+}
+
+#define PKCS11_IMPLEMENT_BOOL_ACCESSOR(s, f) \
+static VALUE c##s##_get_##f(VALUE o){ \
+ return get_bool(o, OFFSET_OF(s, f)); \
+} \
+static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
+ return set_bool(o, v, OFFSET_OF(s, f)); \
+}
+
+#define PKCS11_IMPLEMENT_STRING_PTR_ACCESSOR(s, f) \
+static VALUE c##s##_get_##f(VALUE o){ \
+ return get_string_ptr(o, #f, OFFSET_OF(s, f)); \
+} \
+static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
+ return set_string_ptr(o, v, #f, OFFSET_OF(s, f)); \
+}
+
+#define PKCS11_IMPLEMENT_STRING_PTR_LEN_ACCESSOR(s, f, l) \
+static VALUE c##s##_get_##f(VALUE o){ \
+ return get_string_ptr_len(o, #f, OFFSET_OF(s, f), OFFSET_OF(s, l)); \
+} \
+static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
+ return set_string_ptr_len(o, v, #f, OFFSET_OF(s, f), OFFSET_OF(s, l)); \
+}
+
+#define PKCS11_IMPLEMENT_STRUCT_ACCESSOR(s, k, f) \
+static VALUE c##s##_get_##f(VALUE o){ \
+ return get_struct_inline(o, c##k, #f, OFFSET_OF(s, f)); \
+} \
+static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
+ return set_struct_inline(o, c##k, #k, v, #f, OFFSET_OF(s, f), sizeof(k)); \
+}
+
+#define PKCS11_IMPLEMENT_PKCS11_STRUCT_ACCESSOR(s, k, f) \
+static VALUE c##s##_get_##f(VALUE o){ \
+ VALUE klass = rb_const_get(rb_const_get(rb_cObject, rb_intern("PKCS11")), rb_intern(#k)); \
+ return get_struct_inline(o, klass, #f, OFFSET_OF(s, f)); \
+} \
+static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
+ VALUE klass = rb_const_get(rb_const_get(rb_cObject, rb_intern("PKCS11")), rb_intern(#k)); \
+ return set_struct_inline(o, klass, #k, v, #f, OFFSET_OF(s, f), sizeof(k)); \
+}
+
+#define PKCS11_IMPLEMENT_STRUCT_PTR_ACCESSOR(s, k, f) \
+static VALUE c##s##_get_##f(VALUE o){ \
+ return get_struct_ptr(o, c##k, #f, OFFSET_OF(s, f)); \
+} \
+static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
+ return set_struct_ptr(o, c##k, #k, v, #f, OFFSET_OF(s, f)); \
+}
+
+#define PKCS11_IMPLEMENT_PKCS11_STRUCT_PTR_ACCESSOR(s, k, f) \
+static VALUE c##s##_get_##f(VALUE o){ \
+ VALUE klass = rb_const_get(rb_const_get(rb_cObject, rb_intern("PKCS11")), rb_intern(#k)); \
+ return get_struct_ptr(o, klass, #f, OFFSET_OF(s, f)); \
+} \
+static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
+ VALUE klass = rb_const_get(rb_const_get(rb_cObject, rb_intern("PKCS11")), rb_intern(#k)); \
+ return set_struct_ptr(o, klass, #k, v, #f, OFFSET_OF(s, f)); \
+}
+
+/**************************************************/
+/* struct/attribute definition */
+/**************************************************/
+
+#define PKCS11_DEFINE_STRUCT(s) \
+ do { \
+ c##s = rb_define_class_under(MODULE_FOR_STRUCTS, #s, BASECLASS_FOR_STRUCTS); \
+ a##s##_members = rb_ary_new(); \
+ rb_define_alloc_func(c##s, s##_s_alloc); \
+ rb_define_const(c##s, "SIZEOF_STRUCT", ULONG2NUM(sizeof(s))); \
+ rb_define_method(c##s, "to_s", c##s##_to_s, 0); \
+ rb_define_method(c##s, "members", c##s##_members, 0); \
+ rb_iv_set(c##s, "members", a##s##_members); \
+ } while(0)
+
+#define PKCS11_DEFINE_MEMBER(s, f) \
+ do { \
+ rb_define_method(c##s, #f, c##s##_get_##f, 0); \
+ rb_define_method(c##s, #f "=", c##s##_set_##f, 1); \
+ rb_ary_push(a##s##_members, rb_str_new2(#f)); \
+ } while(0)
+
+
+#endif
Please sign in to comment.
Something went wrong with that request. Please try again.