pkcs11 0.2.0-x86-mswin32 → 0.2.1-x86-mswin32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,423 @@
1
+ #ifndef PK11_STRUCT_MACROS_INCLUDED
2
+ #define PK11_STRUCT_MACROS_INCLUDED
3
+
4
+ /**************************************************/
5
+ /* struct/attribute implementation */
6
+ /**************************************************/
7
+
8
+ #define HANDLE2NUM(n) ULONG2NUM(n)
9
+ #define NUM2HANDLE(n) PKNUM2ULONG(n)
10
+ #define PKNUM2ULONG(n) pkcs11_num2ulong(n)
11
+
12
+ static VALUE
13
+ pkcs11_num2ulong(VALUE val)
14
+ {
15
+ if (TYPE(val) == T_BIGNUM || TYPE(val) == T_FIXNUM) {
16
+ return NUM2ULONG(val);
17
+ }
18
+ return NUM2ULONG(rb_to_int(val));
19
+ }
20
+
21
+ static VALUE
22
+ get_string(VALUE obj, off_t offset, size_t size)
23
+ {
24
+ char *ptr = (char*)DATA_PTR(obj);
25
+ return rb_str_new(ptr+offset, size);
26
+ }
27
+
28
+ static VALUE
29
+ set_string(VALUE obj, VALUE value, off_t offset, size_t size)
30
+ {
31
+ char *ptr = (char*)DATA_PTR(obj);
32
+ int len = size;
33
+ StringValue(value);
34
+ if (RSTRING_LEN(value) < len) len = RSTRING_LEN(value);
35
+ memset(ptr+offset, 0, size);
36
+ memcpy(ptr+offset, RSTRING_PTR(value), len);
37
+ return value;
38
+ }
39
+
40
+ static VALUE
41
+ get_ulong(VALUE obj, off_t offset)
42
+ {
43
+ char *ptr = (char*)DATA_PTR(obj);
44
+ return ULONG2NUM(*(CK_ULONG_PTR)(ptr+offset));
45
+ }
46
+
47
+ static VALUE
48
+ set_ulong(VALUE obj, VALUE value, off_t offset)
49
+ {
50
+ char *ptr = (char*)DATA_PTR(obj);
51
+ *(CK_ULONG_PTR)(ptr+offset) = NUM2ULONG(value);
52
+ return value;
53
+ }
54
+
55
+ static VALUE
56
+ get_byte(VALUE obj, off_t offset)
57
+ {
58
+ char *ptr = (char*)DATA_PTR(obj);
59
+ return ULONG2NUM(*(CK_BYTE_PTR)(ptr+offset));
60
+ }
61
+
62
+ static VALUE
63
+ set_byte(VALUE obj, VALUE value, off_t offset)
64
+ {
65
+ char *ptr = (char*)DATA_PTR(obj);
66
+ *(CK_BYTE_PTR)(ptr+offset) = NUM2ULONG(value);
67
+ return value;
68
+ }
69
+
70
+ static VALUE
71
+ get_ulong_ptr(VALUE obj, off_t offset)
72
+ {
73
+ char *ptr = (char*)DATA_PTR(obj);
74
+ CK_ULONG_PTR p = *(CK_ULONG_PTR *)(ptr+offset);
75
+ if (!p) return Qnil;
76
+ return ULONG2NUM(*p);
77
+ }
78
+
79
+ static VALUE
80
+ set_ulong_ptr(VALUE obj, VALUE value, const char *name, off_t offset)
81
+ {
82
+ CK_ULONG_PTR *ptr = (CK_ULONG_PTR *)((char*)DATA_PTR(obj) + offset);
83
+ if (NIL_P(value)){
84
+ rb_iv_set(obj, name, value);
85
+ *ptr = NULL_PTR;
86
+ return value;
87
+ }
88
+ VALUE new_obj = Data_Make_Struct(rb_cInteger, CK_ULONG, 0, free, *ptr);
89
+ rb_iv_set(obj, name, new_obj);
90
+ **ptr = NUM2ULONG(value);
91
+ return value;
92
+ }
93
+
94
+ static VALUE
95
+ get_handle(VALUE obj, off_t offset)
96
+ {
97
+ char *ptr = (char*)DATA_PTR(obj);
98
+ return HANDLE2NUM(*(CK_OBJECT_HANDLE_PTR)(ptr+offset));
99
+ }
100
+
101
+ static VALUE
102
+ set_handle(VALUE obj, VALUE value, off_t offset)
103
+ {
104
+ char *ptr = (char*)DATA_PTR(obj);
105
+ *(CK_OBJECT_HANDLE_PTR)(ptr+offset) = NUM2HANDLE(value);
106
+ return value;
107
+ }
108
+
109
+ static VALUE
110
+ get_bool(VALUE obj, off_t offset)
111
+ {
112
+ char *ptr = (char*)DATA_PTR(obj);
113
+ if(*(CK_BBOOL*)(ptr+offset)) return Qtrue;
114
+ else return Qfalse;
115
+ }
116
+
117
+ static VALUE
118
+ set_bool(VALUE obj, VALUE value, off_t offset)
119
+ {
120
+ char *ptr = (char*)DATA_PTR(obj);
121
+ if(value == Qfalse) *(CK_BBOOL*)(ptr+offset) = 0;
122
+ else if(value == Qtrue) *(CK_BBOOL*)(ptr+offset) = 1;
123
+ else rb_raise(rb_eArgError, "arg must be true or false");
124
+ return value;
125
+ }
126
+
127
+ static VALUE
128
+ get_string_ptr(VALUE obj, const char *name, off_t offset)
129
+ {
130
+ char *ptr = (char*)DATA_PTR(obj);
131
+ char *p = *(char**)(ptr+offset);
132
+ if (!p) return Qnil;
133
+ return rb_str_new2(p);
134
+ }
135
+
136
+ static VALUE
137
+ set_string_ptr(VALUE obj, VALUE value, const char *name, off_t offset)
138
+ {
139
+ char *ptr = (char*)DATA_PTR(obj);
140
+ if (NIL_P(value)){
141
+ rb_iv_set(obj, name, value);
142
+ *(CK_VOID_PTR*)(ptr+offset) = NULL_PTR;
143
+ return value;
144
+ }
145
+ StringValue(value);
146
+ value = rb_obj_freeze(rb_str_dup(value));
147
+ rb_iv_set(obj, name, value);
148
+ *(CK_VOID_PTR*)(ptr+offset) = RSTRING_PTR(value);
149
+ return value;
150
+ }
151
+
152
+ static VALUE
153
+ get_string_ptr_len(VALUE obj, const char *name, off_t offset, off_t offset_len)
154
+ {
155
+ unsigned long l;
156
+ char *ptr = (char*)DATA_PTR(obj);
157
+ char *p = *(char**)(ptr+offset);
158
+ if (!p) return Qnil;
159
+ l = *(unsigned long*)(ptr+offset_len);
160
+ return rb_str_new(p, l);
161
+ }
162
+
163
+ static VALUE
164
+ set_string_ptr_len(VALUE obj, VALUE value, const char *name, off_t offset, off_t offset_len)
165
+ {
166
+ char *ptr = (char*)DATA_PTR(obj);
167
+ if (NIL_P(value)){
168
+ rb_iv_set(obj, name, value);
169
+ *(CK_VOID_PTR*)(ptr+offset) = NULL_PTR;
170
+ *(unsigned long*)(ptr+offset_len) = 0;
171
+ return value;
172
+ }
173
+ StringValue(value);
174
+ value = rb_obj_freeze(rb_str_dup(value));
175
+ rb_iv_set(obj, name, value);
176
+ *(CK_VOID_PTR*)(ptr+offset) = RSTRING_PTR(value);
177
+ *(unsigned long*)(ptr+offset_len) = RSTRING_LEN(value);
178
+ return value;
179
+ }
180
+
181
+ static VALUE
182
+ get_struct_inline(VALUE obj, VALUE klass, const char *name, off_t offset)
183
+ {
184
+ char *ptr = (char*)DATA_PTR(obj) + offset;
185
+ VALUE inline_obj = Data_Wrap_Struct(klass, 0, 0, ptr);
186
+ rb_iv_set(inline_obj, name, obj);
187
+ return inline_obj;
188
+ }
189
+
190
+ static VALUE
191
+ set_struct_inline(VALUE obj, VALUE klass, const char *struct_name, VALUE value, const char *name, off_t offset, int sizeofstruct)
192
+ {
193
+ char *ptr = (char*)DATA_PTR(obj) + offset;
194
+ if (!rb_obj_is_kind_of(value, klass))
195
+ rb_raise(rb_eArgError, "arg must be a PKCS11::%s", struct_name);
196
+ memcpy(ptr, DATA_PTR(value), sizeofstruct);
197
+ return value;
198
+ }
199
+
200
+ static VALUE
201
+ get_struct_ptr(VALUE obj, VALUE klass, const char *name, off_t offset, int sizeofstruct)
202
+ {
203
+ char *ptr = (char*)DATA_PTR(obj);
204
+ char *p = *(char**)(ptr+offset);
205
+ void *mem;
206
+ VALUE new_obj;
207
+ if (!p) return Qnil;
208
+ mem = xmalloc(sizeofstruct);
209
+ memcpy(mem, p, sizeofstruct);
210
+ new_obj = Data_Wrap_Struct(klass, 0, -1, mem);
211
+ return new_obj;
212
+ }
213
+
214
+ static VALUE
215
+ set_struct_ptr(VALUE obj, VALUE klass, const char *struct_name, VALUE value, const char *name, off_t offset)
216
+ {
217
+ char *ptr = (char*)DATA_PTR(obj) + offset;
218
+ if (NIL_P(value)){
219
+ rb_iv_set(obj, name, value);
220
+ *(CK_VOID_PTR*)ptr = NULL_PTR;
221
+ return value;
222
+ }
223
+ if (!rb_obj_is_kind_of(value, klass))
224
+ rb_raise(rb_eArgError, "arg must be a PKCS11::%s", struct_name);
225
+ *(CK_VOID_PTR*)ptr = DATA_PTR(value);
226
+ rb_iv_set(obj, name, value);
227
+ return value;
228
+ }
229
+
230
+ static VALUE
231
+ get_struct_ptr_array(VALUE obj, VALUE klass, off_t offset, off_t offset_len, int sizeofstruct)
232
+ {
233
+ unsigned long i;
234
+ char *ptr = DATA_PTR(obj);
235
+ char *p = *(char **)(ptr+offset);
236
+ unsigned long l = *(unsigned long*)(ptr+offset_len);
237
+ VALUE ary = rb_ary_new();
238
+ for (i = 0; i < l; i++){
239
+ void *mem = xmalloc(sizeofstruct);
240
+ memcpy(mem, p + sizeofstruct * i, sizeofstruct);
241
+ VALUE new_obj = Data_Wrap_Struct(klass, 0, -1, mem);
242
+ rb_ary_push(ary, new_obj);
243
+ }
244
+ return ary;
245
+ }
246
+
247
+ static VALUE
248
+ set_struct_ptr_array(VALUE obj, VALUE klass, const char *struct_name, VALUE value, const char *name, off_t offset, off_t offset_len, int sizeofstruct)
249
+ {
250
+ int i;
251
+ VALUE str_buf;
252
+ char *ptr = DATA_PTR(obj);
253
+ Check_Type(value, T_ARRAY);
254
+
255
+ str_buf = rb_str_buf_new(sizeofstruct * RARRAY_LEN(value));
256
+
257
+ for (i = 0; i < RARRAY_LEN(value); i++){
258
+ VALUE entry = rb_ary_entry(value, i);
259
+ if (!rb_obj_is_kind_of(entry, klass))
260
+ rb_raise(rb_eArgError, "arg must be array of PKCS11::%s", struct_name);
261
+ memcpy(RSTRING_PTR(str_buf) + sizeofstruct * i, DATA_PTR(entry), sizeofstruct);
262
+ }
263
+ *(CK_VOID_PTR*)(ptr+offset) = RSTRING_PTR(str_buf);
264
+ *(unsigned long*)(ptr+offset_len) = RARRAY_LEN(value);
265
+ rb_iv_set(obj, name, str_buf);
266
+ return value;
267
+ }
268
+
269
+
270
+ #define OFFSET_OF(s, f) ((off_t)((char*)&(((s*)0)->f) - (char*)0))
271
+ #define SIZE_OF(s, f) (sizeof(((s*)0)->f))
272
+
273
+ #define PKCS11_IMPLEMENT_ALLOCATOR(s) \
274
+ static VALUE s##_s_alloc(VALUE self){ \
275
+ s *info; \
276
+ VALUE obj = Data_Make_Struct(self, s, 0, -1, info); \
277
+ return obj; \
278
+ } \
279
+ static VALUE c##s##_to_s(VALUE self){ \
280
+ return rb_str_new(DATA_PTR(self), sizeof(s)); \
281
+ } \
282
+ static VALUE c##s##_members(VALUE self){ \
283
+ return a##s##_members; \
284
+ }
285
+
286
+ #define PKCS11_IMPLEMENT_STRUCT_WITH_ALLOCATOR(s) \
287
+ static VALUE c##s;\
288
+ static VALUE a##s##_members;\
289
+ PKCS11_IMPLEMENT_ALLOCATOR(s);
290
+
291
+ #define PKCS11_IMPLEMENT_STRING_ACCESSOR(s, f) \
292
+ static VALUE c##s##_get_##f(VALUE o){ \
293
+ return get_string(o, OFFSET_OF(s, f), SIZE_OF(s, f)); \
294
+ } \
295
+ static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
296
+ return set_string(o, v, OFFSET_OF(s, f), SIZE_OF(s, f)); \
297
+ }
298
+
299
+ #define PKCS11_IMPLEMENT_ULONG_ACCESSOR(s, f) \
300
+ static VALUE c##s##_get_##f(VALUE o){ \
301
+ return get_ulong(o, OFFSET_OF(s, f)); \
302
+ } \
303
+ static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
304
+ return set_ulong(o, v, OFFSET_OF(s, f)); \
305
+ }
306
+
307
+ #define PKCS11_IMPLEMENT_BYTE_ACCESSOR(s, f) \
308
+ static VALUE c##s##_get_##f(VALUE o){ \
309
+ return get_byte(o, OFFSET_OF(s, f)); \
310
+ } \
311
+ static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
312
+ return set_byte(o, v, OFFSET_OF(s, f)); \
313
+ }
314
+
315
+ #define PKCS11_IMPLEMENT_ULONG_PTR_ACCESSOR(s, f) \
316
+ static VALUE c##s##_get_##f(VALUE o){ \
317
+ return get_ulong_ptr(o, OFFSET_OF(s, f)); \
318
+ } \
319
+ static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
320
+ return set_ulong_ptr(o, v, #f, OFFSET_OF(s, f)); \
321
+ }
322
+
323
+ #define PKCS11_IMPLEMENT_HANDLE_ACCESSOR(s, f) \
324
+ static VALUE c##s##_get_##f(VALUE o){ \
325
+ return get_handle(o, OFFSET_OF(s, f)); \
326
+ } \
327
+ static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
328
+ return set_handle(o, v, OFFSET_OF(s, f)); \
329
+ }
330
+
331
+ #define PKCS11_IMPLEMENT_BOOL_ACCESSOR(s, f) \
332
+ static VALUE c##s##_get_##f(VALUE o){ \
333
+ return get_bool(o, OFFSET_OF(s, f)); \
334
+ } \
335
+ static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
336
+ return set_bool(o, v, OFFSET_OF(s, f)); \
337
+ }
338
+
339
+ #define PKCS11_IMPLEMENT_STRING_PTR_ACCESSOR(s, f) \
340
+ static VALUE c##s##_get_##f(VALUE o){ \
341
+ return get_string_ptr(o, #f, OFFSET_OF(s, f)); \
342
+ } \
343
+ static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
344
+ return set_string_ptr(o, v, #f, OFFSET_OF(s, f)); \
345
+ }
346
+
347
+ #define PKCS11_IMPLEMENT_STRING_PTR_LEN_ACCESSOR(s, f, l) \
348
+ static VALUE c##s##_get_##f(VALUE o){ \
349
+ return get_string_ptr_len(o, #f, OFFSET_OF(s, f), OFFSET_OF(s, l)); \
350
+ } \
351
+ static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
352
+ return set_string_ptr_len(o, v, #f, OFFSET_OF(s, f), OFFSET_OF(s, l)); \
353
+ }
354
+
355
+ #define PKCS11_IMPLEMENT_STRUCT_ACCESSOR(s, k, f) \
356
+ static VALUE c##s##_get_##f(VALUE o){ \
357
+ return get_struct_inline(o, c##k, #f, OFFSET_OF(s, f)); \
358
+ } \
359
+ static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
360
+ return set_struct_inline(o, c##k, #k, v, #f, OFFSET_OF(s, f), sizeof(k)); \
361
+ }
362
+
363
+ #define PKCS11_IMPLEMENT_PKCS11_STRUCT_ACCESSOR(s, k, f) \
364
+ static VALUE c##s##_get_##f(VALUE o){ \
365
+ VALUE klass = rb_const_get(rb_const_get(rb_cObject, rb_intern("PKCS11")), rb_intern(#k)); \
366
+ return get_struct_inline(o, klass, #f, OFFSET_OF(s, f)); \
367
+ } \
368
+ static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
369
+ VALUE klass = rb_const_get(rb_const_get(rb_cObject, rb_intern("PKCS11")), rb_intern(#k)); \
370
+ return set_struct_inline(o, klass, #k, v, #f, OFFSET_OF(s, f), sizeof(k)); \
371
+ }
372
+
373
+ #define PKCS11_IMPLEMENT_STRUCT_PTR_ACCESSOR(s, k, f) \
374
+ static VALUE c##s##_get_##f(VALUE o){ \
375
+ return get_struct_ptr(o, c##k, #f, OFFSET_OF(s, f), sizeof(k)); \
376
+ } \
377
+ static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
378
+ return set_struct_ptr(o, c##k, #k, v, #f, OFFSET_OF(s, f)); \
379
+ }
380
+
381
+ #define PKCS11_IMPLEMENT_PKCS11_STRUCT_PTR_ACCESSOR(s, k, f) \
382
+ static VALUE c##s##_get_##f(VALUE o){ \
383
+ VALUE klass = rb_const_get(rb_const_get(rb_cObject, rb_intern("PKCS11")), rb_intern(#k)); \
384
+ return get_struct_ptr(o, klass, #f, OFFSET_OF(s, f), sizeof(k)); \
385
+ } \
386
+ static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
387
+ VALUE klass = rb_const_get(rb_const_get(rb_cObject, rb_intern("PKCS11")), rb_intern(#k)); \
388
+ return set_struct_ptr(o, klass, #k, v, #f, OFFSET_OF(s, f)); \
389
+ }
390
+
391
+ #define PKCS11_IMPLEMENT_STRUCT_PTR_ARRAY_ACCESSOR(s, k, f, l) \
392
+ static VALUE c##s##_get_##f(VALUE o){ \
393
+ return get_struct_ptr_array(o, c##k, OFFSET_OF(s, f), OFFSET_OF(s, l), sizeof(k)); \
394
+ } \
395
+ static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
396
+ return set_struct_ptr_array(o, c##k, #k, v, #f, OFFSET_OF(s, f), OFFSET_OF(s, l), sizeof(k)); \
397
+ }
398
+
399
+
400
+ /**************************************************/
401
+ /* struct/attribute definition */
402
+ /**************************************************/
403
+
404
+ #define PKCS11_DEFINE_STRUCT(s) \
405
+ do { \
406
+ c##s = rb_define_class_under(MODULE_FOR_STRUCTS, #s, BASECLASS_FOR_STRUCTS); \
407
+ a##s##_members = rb_ary_new(); \
408
+ rb_define_alloc_func(c##s, s##_s_alloc); \
409
+ rb_define_const(c##s, "SIZEOF_STRUCT", ULONG2NUM(sizeof(s))); \
410
+ rb_define_method(c##s, "to_s", c##s##_to_s, 0); \
411
+ rb_define_method(c##s, "members", c##s##_members, 0); \
412
+ rb_iv_set(c##s, "members", a##s##_members); \
413
+ } while(0)
414
+
415
+ #define PKCS11_DEFINE_MEMBER(s, f) \
416
+ do { \
417
+ rb_define_method(c##s, #f, c##s##_get_##f, 0); \
418
+ rb_define_method(c##s, #f "=", c##s##_set_##f, 1); \
419
+ rb_ary_push(a##s##_members, rb_str_new2(#f)); \
420
+ } while(0)
421
+
422
+
423
+ #endif
Binary file
Binary file
@@ -138,14 +138,5 @@ module PKCS11
138
138
  mechanism
139
139
  end
140
140
  end
141
-
142
- def to_mechanism_int(mechanism) # :nodoc:
143
- case mechanism
144
- when String, Symbol
145
- PKCS11::MECHANISMS[string_to_handle('CKM_', mechanism)]
146
- else
147
- mechanism
148
- end
149
- end
150
141
  end
151
142
  end
@@ -59,7 +59,7 @@ module PKCS11
59
59
  # @param [Integer, Symbol] mechanism
60
60
  # @return [CK_MECHANISM_INFO]
61
61
  def C_GetMechanismInfo(mechanism)
62
- @pk.C_GetMechanismInfo(@slot, to_mechanism_int(mechanism))
62
+ @pk.C_GetMechanismInfo(@slot, string_to_handle('CKM_', mechanism))
63
63
  end
64
64
  alias mechanism_info C_GetMechanismInfo
65
65