pkcs11 0.2.4-x64-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. data.tar.gz.sig +0 -0
  2. data/.autotest +23 -0
  3. data/.gemtest +0 -0
  4. data/.yardopts +1 -0
  5. data/History.txt +57 -0
  6. data/MIT-LICENSE +22 -0
  7. data/Manifest.txt +57 -0
  8. data/README.rdoc +205 -0
  9. data/Rakefile +111 -0
  10. data/ext/extconf.rb +7 -0
  11. data/ext/generate_constants.rb +57 -0
  12. data/ext/generate_structs.rb +206 -0
  13. data/ext/generate_thread_funcs.rb +72 -0
  14. data/ext/include/cryptoki.h +66 -0
  15. data/ext/include/ct-kip.h +50 -0
  16. data/ext/include/otp-pkcs11.h +125 -0
  17. data/ext/include/pkcs-11v2-20a3.h +124 -0
  18. data/ext/include/pkcs11.h +299 -0
  19. data/ext/include/pkcs11f.h +912 -0
  20. data/ext/include/pkcs11t.h +1885 -0
  21. data/ext/pk11.c +1675 -0
  22. data/ext/pk11.h +81 -0
  23. data/ext/pk11_const.c +205 -0
  24. data/ext/pk11_const_def.inc +452 -0
  25. data/ext/pk11_const_macros.h +38 -0
  26. data/ext/pk11_struct.doc +792 -0
  27. data/ext/pk11_struct_def.inc +302 -0
  28. data/ext/pk11_struct_impl.inc +302 -0
  29. data/ext/pk11_struct_macros.h +435 -0
  30. data/ext/pk11_thread_funcs.c +411 -0
  31. data/ext/pk11_thread_funcs.h +482 -0
  32. data/ext/pk11_version.h +6 -0
  33. data/lib/2.0/pkcs11_ext.so +0 -0
  34. data/lib/pkcs11.rb +9 -0
  35. data/lib/pkcs11/extensions.rb +68 -0
  36. data/lib/pkcs11/helper.rb +144 -0
  37. data/lib/pkcs11/library.rb +140 -0
  38. data/lib/pkcs11/object.rb +171 -0
  39. data/lib/pkcs11/session.rb +765 -0
  40. data/lib/pkcs11/slot.rb +102 -0
  41. data/pkcs11_protect_server/Manifest.txt +14 -0
  42. data/pkcs11_protect_server/README_PROTECT_SERVER.rdoc +89 -0
  43. data/test/fixtures/softokn/cert8.db +0 -0
  44. data/test/fixtures/softokn/key3.db +0 -0
  45. data/test/fixtures/softokn/secmod.db +0 -0
  46. data/test/helper.rb +58 -0
  47. data/test/test_pkcs11.rb +71 -0
  48. data/test/test_pkcs11_crypt.rb +220 -0
  49. data/test/test_pkcs11_object.rb +122 -0
  50. data/test/test_pkcs11_session.rb +123 -0
  51. data/test/test_pkcs11_slot.rb +78 -0
  52. data/test/test_pkcs11_structs.rb +166 -0
  53. data/test/test_pkcs11_thread.rb +44 -0
  54. metadata +213 -0
  55. metadata.gz.sig +0 -0
@@ -0,0 +1,1675 @@
1
+ #include "pk11.h"
2
+ #include "pk11_struct_macros.h"
3
+ #include "pk11_version.h"
4
+
5
+ #if defined(compile_for_windows)
6
+ #include <winbase.h> /* for LoadLibrary() */
7
+ #else
8
+ #include <dlfcn.h>
9
+ #endif
10
+
11
+ static ID sNEW;
12
+ static VALUE mPKCS11;
13
+ static VALUE cPKCS11;
14
+ static VALUE ePKCS11Error;
15
+
16
+ static VALUE cCStruct;
17
+ static VALUE cCK_ATTRIBUTE;
18
+ static VALUE cCK_C_INITIALIZE_ARGS;
19
+ static VALUE aCK_C_INITIALIZE_ARGS_members;
20
+ static VALUE cCK_INFO;
21
+ static VALUE aCK_INFO_members;
22
+ static VALUE cCK_TOKEN_INFO;
23
+ static VALUE aCK_TOKEN_INFO_members;
24
+ static VALUE cCK_SLOT_INFO;
25
+ static VALUE aCK_SLOT_INFO_members;
26
+ static VALUE cCK_MECHANISM_INFO;
27
+ static VALUE aCK_MECHANISM_INFO_members;
28
+ static VALUE cCK_SESSION_INFO;
29
+ static VALUE aCK_SESSION_INFO_members;
30
+ static VALUE cCK_MECHANISM;
31
+ static VALUE aCK_MECHANISM_members;
32
+
33
+ #define MODULE_FOR_STRUCTS mPKCS11
34
+ #define BASECLASS_FOR_STRUCTS cCStruct
35
+ #define pkcs11_new_struct(klass) rb_funcall(klass, sNEW, 0)
36
+
37
+ #define PKCS11_DEFINE_METHOD(name, args) \
38
+ rb_define_method(cPKCS11, #name, pkcs11_##name, args);
39
+
40
+ VALUE pkcs11_return_value_to_class(CK_RV, VALUE);
41
+
42
+ static void
43
+ pkcs11_raise(VALUE self, CK_RV rv)
44
+ {
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");
47
+ }
48
+
49
+ ///////////////////////////////////////
50
+
51
+ typedef struct {
52
+ void *module;
53
+ CK_FUNCTION_LIST_PTR functions;
54
+ } pkcs11_ctx;
55
+
56
+ #define GetFunction(obj, name, sval) \
57
+ { \
58
+ pkcs11_ctx *ctx; \
59
+ Data_Get_Struct(obj, pkcs11_ctx, ctx); \
60
+ if (!ctx->functions) rb_raise(ePKCS11Error, "no function list"); \
61
+ sval = (CK_##name)ctx->functions->name; \
62
+ if (!sval) rb_raise(ePKCS11Error, #name " is not supported."); \
63
+ }
64
+
65
+ #ifdef HAVE_RB_THREAD_BLOCKING_REGION
66
+ #define CallFunction(name, func, rv, ...) \
67
+ { \
68
+ struct tbr_##name##_params params = { \
69
+ func, {__VA_ARGS__}, CKR_FUNCTION_FAILED \
70
+ }; \
71
+ rb_thread_blocking_region(tbf_##name, &params, RUBY_UBF_PROCESS, 0); \
72
+ rv = params.retval; \
73
+ }
74
+
75
+ #else
76
+ #define CallFunction(name, func, rv, ...) \
77
+ rv = func(__VA_ARGS__)
78
+
79
+ #endif
80
+
81
+ static void
82
+ pkcs11_ctx_unload_library(pkcs11_ctx *ctx)
83
+ {
84
+ #ifdef compile_for_windows
85
+ if(ctx->module) FreeLibrary(ctx->module);
86
+ #else
87
+ if(ctx->module) dlclose(ctx->module);
88
+ #endif
89
+ ctx->module = NULL;
90
+ ctx->functions = NULL;
91
+ }
92
+
93
+ static void
94
+ pkcs11_ctx_free(pkcs11_ctx *ctx)
95
+ {
96
+ if(ctx->functions) ctx->functions->C_Finalize(NULL_PTR);
97
+ pkcs11_ctx_unload_library(ctx);
98
+ free(ctx);
99
+ }
100
+
101
+ /* rb_define_method(cPKCS11, "C_Finalize", pkcs11_C_Finalize, 0); */
102
+ /*
103
+ * Is called to indicate that an application is finished with the Cryptoki library.
104
+ * @see PKCS11::Library#close
105
+ */
106
+ static VALUE
107
+ pkcs11_C_Finalize(VALUE self)
108
+ {
109
+ CK_C_Finalize func;
110
+ CK_RV rv;
111
+
112
+ GetFunction(self, C_Finalize, func);
113
+ CallFunction(C_Finalize, func, rv, NULL_PTR);
114
+ if (rv != CKR_OK) pkcs11_raise(self,rv);
115
+
116
+ return self;
117
+ }
118
+
119
+ /* rb_define_method(cPKCS11, "unload_library", pkcs11_unload_library, 0); */
120
+ /*
121
+ * Unloads the Cryptoki library from process memory.
122
+ * @see PKCS11::Library#close
123
+ */
124
+ static VALUE
125
+ pkcs11_unload_library(VALUE self)
126
+ {
127
+ pkcs11_ctx *ctx;
128
+
129
+ Data_Get_Struct(self, pkcs11_ctx, ctx);
130
+ pkcs11_ctx_unload_library(ctx);
131
+
132
+ return self;
133
+ }
134
+
135
+ static VALUE
136
+ pkcs11_s_alloc(VALUE self)
137
+ {
138
+ VALUE obj;
139
+ pkcs11_ctx *ctx;
140
+ obj = Data_Make_Struct(self, pkcs11_ctx, 0, pkcs11_ctx_free, ctx);
141
+ return obj;
142
+ }
143
+
144
+ static VALUE
145
+ pkcs11_library_new(int argc, VALUE *argv, VALUE self)
146
+ {
147
+ return rb_funcall2(cPKCS11, sNEW, argc, argv);
148
+ }
149
+
150
+ /* rb_define_method(cPKCS11, "load_library", pkcs11_load_library, 0); */
151
+ /*
152
+ * Load a Cryptoki library into process memory.
153
+ * @see PKCS11::Library#initialize
154
+ */
155
+ static VALUE
156
+ pkcs11_load_library(VALUE self, VALUE path)
157
+ {
158
+ const char *so_path;
159
+ pkcs11_ctx *ctx;
160
+
161
+ so_path = StringValuePtr(path);
162
+ Data_Get_Struct(self, pkcs11_ctx, ctx);
163
+ #ifdef compile_for_windows
164
+ if((ctx->module = LoadLibrary(so_path)) == NULL) {
165
+ char error_text[999] = "LoadLibrary() error";
166
+ FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_MAX_WIDTH_MASK,
167
+ NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
168
+ (LPTSTR)&error_text, sizeof(error_text), NULL);
169
+ rb_raise(ePKCS11Error, error_text);
170
+ }
171
+ #else
172
+ if((ctx->module = dlopen(so_path, RTLD_NOW)) == NULL) {
173
+ rb_raise(ePKCS11Error, "%s", dlerror());
174
+ }
175
+ #endif
176
+
177
+ return self;
178
+ }
179
+
180
+ /* rb_define_method(cPKCS11, "C_GetFunctionList", pkcs11_C_GetFunctionList, 0); */
181
+ /*
182
+ * Obtains a pointer to the Cryptoki library's list of function pointers. The pointer
183
+ * is stored in the {PKCS11::Library} object and used to call any Cryptoki functions.
184
+ *
185
+ * @see PKCS11::Library#initialize
186
+ */
187
+ static VALUE
188
+ pkcs11_C_GetFunctionList(VALUE self)
189
+ {
190
+ pkcs11_ctx *ctx;
191
+ CK_RV rv;
192
+ CK_C_GetFunctionList func;
193
+
194
+ Data_Get_Struct(self, pkcs11_ctx, ctx);
195
+ #ifdef compile_for_windows
196
+ func = (CK_C_GetFunctionList)GetProcAddress(ctx->module, "C_GetFunctionList");
197
+ if(!func){
198
+ char error_text[999] = "GetProcAddress() error";
199
+ FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_MAX_WIDTH_MASK,
200
+ NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
201
+ (LPTSTR)&error_text, sizeof(error_text), NULL);
202
+ rb_raise(ePKCS11Error, error_text);
203
+ }
204
+ #else
205
+ func = (CK_C_GetFunctionList)dlsym(ctx->module, "C_GetFunctionList");
206
+ if(!func) rb_raise(ePKCS11Error, "%s", dlerror());
207
+ #endif
208
+ CallFunction(C_GetFunctionList, func, rv, &(ctx->functions));
209
+ if (rv != CKR_OK) pkcs11_raise(self,rv);
210
+
211
+ return self;
212
+ }
213
+
214
+ /* rb_define_method(cPKCS11, "C_Initialize", pkcs11_C_Initialize, 0); */
215
+ /*
216
+ * Initializes the Cryptoki library.
217
+ */
218
+ static VALUE
219
+ pkcs11_C_Initialize(int argc, VALUE *argv, VALUE self)
220
+ {
221
+ VALUE init_args;
222
+ CK_C_Initialize func;
223
+ CK_C_INITIALIZE_ARGS *args;
224
+ CK_RV rv;
225
+
226
+ rb_scan_args(argc, argv, "01", &init_args);
227
+ if (NIL_P(init_args)) args = NULL_PTR;
228
+ else {
229
+ if (!rb_obj_is_kind_of(init_args, cCK_C_INITIALIZE_ARGS))
230
+ rb_raise(rb_eArgError, "2nd arg must be a PKCS11::CK_C_INITIALIZE_ARGS");
231
+ args = DATA_PTR(init_args);
232
+ }
233
+ GetFunction(self, C_Initialize, func);
234
+ CallFunction(C_Initialize, func, rv, args);
235
+ if (rv != CKR_OK) pkcs11_raise(self,rv);
236
+
237
+ return self;
238
+ }
239
+
240
+ static VALUE
241
+ pkcs11_initialize(int argc, VALUE *argv, VALUE self)
242
+ {
243
+ VALUE path, init_args;
244
+
245
+ rb_scan_args(argc, argv, "02", &path, &init_args);
246
+ if( !NIL_P(path) ){
247
+ rb_funcall(self, rb_intern("load_library"), 1, path);
248
+ rb_funcall(self, rb_intern("C_GetFunctionList"), 0);
249
+ rb_funcall2(self, rb_intern("C_Initialize"), 1, &init_args);
250
+ }
251
+
252
+ return self;
253
+ }
254
+
255
+ static VALUE
256
+ pkcs11_C_GetInfo(VALUE self)
257
+ {
258
+ CK_C_GetInfo func;
259
+ CK_RV rv;
260
+ VALUE info;
261
+
262
+ GetFunction(self, C_GetInfo, func);
263
+ info = pkcs11_new_struct(cCK_INFO);
264
+ CallFunction(C_GetInfo, func, rv, (CK_INFO_PTR)DATA_PTR(info));
265
+ if (rv != CKR_OK) pkcs11_raise(self,rv);
266
+
267
+ return info;
268
+ }
269
+
270
+ static VALUE
271
+ pkcs11_C_GetSlotList(VALUE self, VALUE presented)
272
+ {
273
+ CK_ULONG ulSlotCount;
274
+ CK_SLOT_ID_PTR pSlotList;
275
+ CK_RV rv;
276
+ CK_C_GetSlotList func;
277
+ CK_ULONG i;
278
+ VALUE ary = rb_ary_new();
279
+
280
+ GetFunction(self, C_GetSlotList, func);
281
+ CallFunction(C_GetSlotList, func, rv, CK_FALSE, NULL_PTR, &ulSlotCount);
282
+ if (rv != CKR_OK) pkcs11_raise(self,rv);
283
+ pSlotList = (CK_SLOT_ID_PTR)malloc(ulSlotCount*sizeof(CK_SLOT_ID));
284
+ CallFunction(C_GetSlotList, func, rv, RTEST(presented) ? CK_TRUE : CK_FALSE, pSlotList, &ulSlotCount);
285
+ if (rv != CKR_OK) {
286
+ free(pSlotList);
287
+ pkcs11_raise(self,rv);
288
+ }
289
+ for (i = 0; i < ulSlotCount; i++)
290
+ rb_ary_push(ary, HANDLE2NUM(pSlotList[i]));
291
+ free(pSlotList);
292
+
293
+ return ary;
294
+ }
295
+
296
+ static VALUE
297
+ pkcs11_C_GetSlotInfo(VALUE self, VALUE slot_id)
298
+ {
299
+ CK_RV rv;
300
+ CK_C_GetSlotInfo func;
301
+ VALUE info;
302
+
303
+ GetFunction(self, C_GetSlotInfo, func);
304
+ info = pkcs11_new_struct(cCK_SLOT_INFO);
305
+ CallFunction(C_GetSlotInfo, func, rv, NUM2HANDLE(slot_id), DATA_PTR(info));
306
+ if (rv != CKR_OK) pkcs11_raise(self,rv);
307
+
308
+ return info;
309
+ }
310
+
311
+ static VALUE
312
+ pkcs11_C_GetTokenInfo(VALUE self, VALUE slot_id)
313
+ {
314
+ CK_RV rv;
315
+ CK_C_GetTokenInfo func;
316
+ VALUE info;
317
+
318
+ GetFunction(self, C_GetTokenInfo, func);
319
+ info = pkcs11_new_struct(cCK_TOKEN_INFO);
320
+ CallFunction(C_GetTokenInfo, func, rv, NUM2HANDLE(slot_id), DATA_PTR(info));
321
+ if (rv != CKR_OK) pkcs11_raise(self,rv);
322
+
323
+ return info;
324
+ }
325
+
326
+ static VALUE
327
+ pkcs11_C_GetMechanismList(VALUE self, VALUE slot_id)
328
+ {
329
+ CK_RV rv;
330
+ CK_C_GetMechanismList func;
331
+ CK_MECHANISM_TYPE_PTR types;
332
+ CK_ULONG count;
333
+ VALUE ary;
334
+ CK_ULONG i;
335
+
336
+ ary = rb_ary_new();
337
+ GetFunction(self, C_GetMechanismList, func);
338
+ CallFunction(C_GetMechanismList, func, rv, NUM2HANDLE(slot_id), NULL_PTR, &count);
339
+ if (rv != CKR_OK) pkcs11_raise(self,rv);
340
+ if (count == 0) return ary;
341
+
342
+ types = (CK_MECHANISM_TYPE_PTR)malloc(sizeof(CK_MECHANISM_TYPE)*count);
343
+ if (!types) rb_sys_fail(0);
344
+ CallFunction(C_GetMechanismList, func, rv, NUM2HANDLE(slot_id), types, &count);
345
+ if (rv != CKR_OK){
346
+ free(types);
347
+ pkcs11_raise(self,rv);
348
+ }
349
+ for (i = 0; i < count; i++)
350
+ rb_ary_push(ary, HANDLE2NUM(*(types+i)));
351
+ free(types);
352
+
353
+ return ary;
354
+ }
355
+
356
+ static VALUE
357
+ pkcs11_C_GetMechanismInfo(VALUE self, VALUE slot_id, VALUE type)
358
+ {
359
+ CK_RV rv;
360
+ CK_C_GetMechanismInfo func;
361
+ VALUE info;
362
+
363
+ info = pkcs11_new_struct(cCK_MECHANISM_INFO);
364
+ GetFunction(self, C_GetMechanismInfo, func);
365
+ CallFunction(C_GetMechanismInfo, func, rv, NUM2HANDLE(slot_id), NUM2HANDLE(type), DATA_PTR(info));
366
+ if (rv != CKR_OK) pkcs11_raise(self,rv);
367
+
368
+ return info;
369
+ }
370
+
371
+ static VALUE
372
+ pkcs11_C_InitToken(VALUE self, VALUE slot_id, VALUE pin, VALUE label)
373
+ {
374
+ CK_RV rv;
375
+ CK_C_InitToken func;
376
+
377
+ StringValue(pin);
378
+ StringValue(label);
379
+ GetFunction(self, C_InitToken, func);
380
+ CallFunction(C_InitToken, func, rv, NUM2HANDLE(slot_id),
381
+ (CK_UTF8CHAR_PTR)RSTRING_PTR(pin), RSTRING_LEN(pin),
382
+ (CK_UTF8CHAR_PTR)RSTRING_PTR(label));
383
+ if (rv != CKR_OK) pkcs11_raise(self,rv);
384
+
385
+ return self;
386
+ }
387
+
388
+ static VALUE
389
+ pkcs11_C_InitPIN(VALUE self, VALUE session, VALUE pin)
390
+ {
391
+ CK_RV rv;
392
+ CK_C_InitPIN func;
393
+
394
+ StringValue(pin);
395
+ GetFunction(self, C_InitPIN, func);
396
+ CallFunction(C_InitPIN, func, rv, NUM2HANDLE(session),
397
+ (CK_UTF8CHAR_PTR)RSTRING_PTR(pin), RSTRING_LEN(pin));
398
+ if (rv != CKR_OK) pkcs11_raise(self,rv);
399
+
400
+ return self;
401
+ }
402
+
403
+ static VALUE
404
+ pkcs11_C_OpenSession(VALUE self, VALUE slot_id, VALUE flags)
405
+ {
406
+ CK_C_OpenSession func;
407
+ CK_RV rv;
408
+ CK_SESSION_HANDLE handle;
409
+
410
+ GetFunction(self, C_OpenSession, func);
411
+ CallFunction(C_OpenSession, func, rv, NUM2HANDLE(slot_id), NUM2ULONG(flags), 0, 0, &handle);
412
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
413
+
414
+ return HANDLE2NUM(handle);
415
+ }
416
+
417
+ static VALUE
418
+ pkcs11_C_Login(VALUE self, VALUE session, VALUE user_type, VALUE pin)
419
+ {
420
+ CK_C_Login func;
421
+ CK_RV rv;
422
+
423
+ StringValue(pin);
424
+ GetFunction(self, C_Login, func);
425
+ CallFunction(C_Login, func, rv, NUM2HANDLE(session), NUM2ULONG(user_type),
426
+ (CK_UTF8CHAR_PTR)RSTRING_PTR(pin), RSTRING_LEN(pin));
427
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
428
+
429
+ return self;
430
+ }
431
+
432
+ static VALUE
433
+ pkcs11_C_Logout(VALUE self, VALUE session)
434
+ {
435
+ CK_C_Logout func;
436
+ CK_RV rv;
437
+
438
+ GetFunction(self, C_Logout, func);
439
+ CallFunction(C_Logout, func, rv, NUM2HANDLE(session));
440
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
441
+
442
+ return self;
443
+ }
444
+
445
+ static VALUE
446
+ pkcs11_C_CloseSession(VALUE self, VALUE session)
447
+ {
448
+ CK_C_CloseSession func;
449
+ CK_RV rv;
450
+
451
+ GetFunction(self, C_CloseSession, func);
452
+ CallFunction(C_CloseSession, func, rv, NUM2HANDLE(session));
453
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
454
+
455
+ return self;
456
+ }
457
+
458
+ static VALUE
459
+ pkcs11_C_CloseAllSessions(VALUE self, VALUE slot_id)
460
+ {
461
+ CK_C_CloseAllSessions func;
462
+ CK_RV rv;
463
+
464
+ GetFunction(self, C_CloseAllSessions, func);
465
+ CallFunction(C_CloseAllSessions, func, rv, NUM2HANDLE(slot_id));
466
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
467
+
468
+ return self;
469
+ }
470
+
471
+ static VALUE
472
+ pkcs11_C_GetSessionInfo(VALUE self, VALUE session)
473
+ {
474
+ CK_RV rv;
475
+ CK_C_GetSessionInfo func;
476
+ VALUE info;
477
+
478
+ info = pkcs11_new_struct(cCK_SESSION_INFO);
479
+ GetFunction(self, C_GetSessionInfo, func);
480
+ CallFunction(C_GetSessionInfo, func, rv, NUM2HANDLE(session), DATA_PTR(info));
481
+ if (rv != CKR_OK) pkcs11_raise(self,rv);
482
+
483
+ return info;
484
+ }
485
+
486
+ static VALUE
487
+ pkcs11_C_GetOperationState(VALUE self, VALUE session)
488
+ {
489
+ CK_RV rv;
490
+ CK_C_GetOperationState func;
491
+ VALUE state;
492
+ CK_ULONG size;
493
+
494
+ GetFunction(self, C_GetOperationState, func);
495
+ CallFunction(C_GetOperationState, func, rv, NUM2HANDLE(session), NULL_PTR, &size);
496
+ if (rv != CKR_OK) pkcs11_raise(self,rv);
497
+ state = rb_str_new(0, size);
498
+ CallFunction(C_GetOperationState, func, rv, NUM2HANDLE(session), (CK_BYTE_PTR)RSTRING_PTR(state), &size);
499
+ if (rv != CKR_OK) pkcs11_raise(self,rv);
500
+ rb_str_set_len(state, size);
501
+
502
+ return state;
503
+ }
504
+
505
+ static VALUE
506
+ pkcs11_C_SetOperationState(VALUE self, VALUE session, VALUE state, VALUE enc_key, VALUE auth_key)
507
+ {
508
+ CK_RV rv;
509
+ CK_C_SetOperationState func;
510
+
511
+ StringValue(state);
512
+ GetFunction(self, C_SetOperationState, func);
513
+ CallFunction(C_SetOperationState, func, rv, NUM2HANDLE(session),
514
+ (CK_BYTE_PTR)RSTRING_PTR(state), RSTRING_LEN(state),
515
+ NUM2HANDLE(enc_key), NUM2HANDLE(auth_key));
516
+ if (rv != CKR_OK) pkcs11_raise(self,rv);
517
+
518
+ return self;
519
+ }
520
+
521
+ static VALUE
522
+ pkcs11_C_SetPIN(VALUE self, VALUE session, VALUE old_pin, VALUE new_pin)
523
+ {
524
+ CK_C_SetPIN func;
525
+ CK_RV rv;
526
+
527
+ StringValue(old_pin);
528
+ StringValue(new_pin);
529
+ GetFunction(self, C_SetPIN, func);
530
+ CallFunction(C_SetPIN, func, rv, NUM2HANDLE(session),
531
+ (CK_UTF8CHAR_PTR)RSTRING_PTR(old_pin), RSTRING_LEN(old_pin),
532
+ (CK_UTF8CHAR_PTR)RSTRING_PTR(new_pin), RSTRING_LEN(new_pin));
533
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
534
+
535
+ return self;
536
+ }
537
+
538
+ static CK_ATTRIBUTE*
539
+ pkcs11_attr_ary2buf(VALUE template)
540
+ {
541
+ int i;
542
+ CK_ATTRIBUTE *tmp;
543
+
544
+ Check_Type(template, T_ARRAY);
545
+ tmp = (CK_ATTRIBUTE*)
546
+ malloc(sizeof(CK_ATTRIBUTE)*RARRAY_LEN(template));
547
+ if (!tmp) rb_sys_fail(0);
548
+ for (i = 0; i < RARRAY_LEN(template); i++){
549
+ VALUE attr = rb_ary_entry(template, i);
550
+ if (!rb_obj_is_kind_of(attr, cCK_ATTRIBUTE)) {
551
+ free(tmp);
552
+ rb_raise(rb_eArgError, "templates must be an ary of PKCS11::CK_ATTRIBUTE");
553
+ }
554
+ memcpy(tmp+i, DATA_PTR(attr), sizeof(CK_ATTRIBUTE));
555
+ }
556
+
557
+ return tmp;
558
+ }
559
+
560
+ static VALUE
561
+ pkcs11_C_CreateObject(VALUE self, VALUE session, VALUE template)
562
+ {
563
+ CK_C_CreateObject func;
564
+ CK_RV rv;
565
+ CK_ATTRIBUTE *tmp;
566
+ CK_OBJECT_HANDLE handle;
567
+
568
+ tmp = pkcs11_attr_ary2buf(template);
569
+ GetFunction(self, C_CreateObject, func);
570
+ CallFunction(C_CreateObject, func, rv, NUM2HANDLE(session), tmp, RARRAY_LEN(template), &handle);
571
+ free(tmp);
572
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
573
+
574
+ return HANDLE2NUM(handle);
575
+ }
576
+
577
+ static VALUE
578
+ pkcs11_C_CopyObject(VALUE self, VALUE session, VALUE object, VALUE template)
579
+ {
580
+ CK_C_CopyObject func;
581
+ CK_RV rv;
582
+ CK_ATTRIBUTE *tmp;
583
+ CK_OBJECT_HANDLE handle;
584
+
585
+ tmp = pkcs11_attr_ary2buf(template);
586
+ GetFunction(self, C_CopyObject, func);
587
+ CallFunction(C_CopyObject, func, rv, NUM2HANDLE(session), NUM2HANDLE(object), tmp, RARRAY_LEN(template), &handle);
588
+ free(tmp);
589
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
590
+
591
+ return HANDLE2NUM(handle);
592
+ }
593
+
594
+ static VALUE
595
+ pkcs11_C_DestroyObject(VALUE self, VALUE session, VALUE handle)
596
+ {
597
+ CK_C_DestroyObject func;
598
+ CK_RV rv;
599
+
600
+ GetFunction(self, C_DestroyObject, func);
601
+ CallFunction(C_DestroyObject, func, rv, NUM2HANDLE(session), NUM2HANDLE(handle));
602
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
603
+
604
+ return self;
605
+ }
606
+
607
+ static VALUE
608
+ pkcs11_C_GetObjectSize(VALUE self, VALUE session, VALUE handle)
609
+ {
610
+ CK_C_GetObjectSize func;
611
+ CK_RV rv;
612
+ CK_ULONG size;
613
+
614
+ GetFunction(self, C_GetObjectSize, func);
615
+ CallFunction(C_GetObjectSize, func, rv, NUM2HANDLE(session), NUM2HANDLE(handle), &size);
616
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
617
+
618
+ return ULONG2NUM(size);
619
+ }
620
+
621
+ static VALUE
622
+ pkcs11_C_FindObjectsInit(VALUE self, VALUE session, VALUE template)
623
+ {
624
+ CK_C_FindObjectsInit func;
625
+ CK_RV rv;
626
+ CK_ATTRIBUTE_PTR tmp = NULL_PTR;
627
+ CK_ULONG tmp_size = 0;
628
+
629
+ if (!NIL_P(template)){
630
+ tmp = pkcs11_attr_ary2buf(template);
631
+ tmp_size = RARRAY_LEN(template);
632
+ }
633
+ GetFunction(self, C_FindObjectsInit, func);
634
+ CallFunction(C_FindObjectsInit, func, rv, NUM2HANDLE(session), tmp, tmp_size);
635
+ free(tmp);
636
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
637
+
638
+ return self;
639
+ }
640
+
641
+ static VALUE
642
+ pkcs11_C_FindObjectsFinal(VALUE self, VALUE session)
643
+ {
644
+ CK_C_FindObjectsFinal func;
645
+ CK_RV rv;
646
+
647
+ GetFunction(self, C_FindObjectsFinal, func);
648
+ CallFunction(C_FindObjectsFinal, func, rv, NUM2HANDLE(session));
649
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
650
+
651
+ return self;
652
+ }
653
+
654
+ static VALUE
655
+ pkcs11_C_FindObjects(VALUE self, VALUE session, VALUE max_count)
656
+ {
657
+ CK_C_FindObjects func;
658
+ CK_RV rv;
659
+ CK_OBJECT_HANDLE_PTR handles;
660
+ CK_ULONG count = 0;
661
+ VALUE ary;
662
+ CK_ULONG i;
663
+
664
+ handles = (CK_OBJECT_HANDLE_PTR)
665
+ malloc(sizeof(CK_OBJECT_HANDLE)*NUM2ULONG(max_count));
666
+ GetFunction(self, C_FindObjects, func);
667
+ CallFunction(C_FindObjects, func, rv, NUM2HANDLE(session), handles, NUM2ULONG(max_count), &count);
668
+ if(rv != CKR_OK){
669
+ free(handles);
670
+ pkcs11_raise(self,rv);
671
+ }
672
+ ary = rb_ary_new();
673
+ for(i = 0; i < count; i++)
674
+ rb_ary_push(ary, HANDLE2NUM(*(handles+i)));
675
+ free(handles);
676
+
677
+ return ary;
678
+ }
679
+
680
+ static VALUE ck_attr_s_alloc(VALUE);
681
+
682
+ static VALUE
683
+ pkcs11_C_GetAttributeValue(VALUE self, VALUE session, VALUE handle, VALUE template)
684
+ {
685
+ CK_C_GetAttributeValue func;
686
+ CK_RV rv;
687
+ CK_ULONG i, template_size;
688
+ CK_ATTRIBUTE_PTR tmp;
689
+ VALUE ary;
690
+ VALUE class_attr = rb_funcall(self, rb_intern("vendor_class_CK_ATTRIBUTE"), 0);
691
+
692
+ tmp = pkcs11_attr_ary2buf(template);
693
+ template_size = RARRAY_LEN(template);
694
+ GetFunction(self, C_GetAttributeValue, func);
695
+ CallFunction(C_GetAttributeValue, func, rv, NUM2HANDLE(session), NUM2HANDLE(handle), tmp, template_size);
696
+ if(rv != CKR_OK){
697
+ free(tmp);
698
+ pkcs11_raise(self,rv);
699
+ }
700
+
701
+ for (i = 0; i < template_size; i++){
702
+ CK_ATTRIBUTE_PTR attr = tmp + i;
703
+ if (attr->ulValueLen != (CK_ULONG)-1)
704
+ attr->pValue = (CK_BYTE_PTR)malloc(attr->ulValueLen);
705
+ }
706
+ CallFunction(C_GetAttributeValue, func, rv, NUM2HANDLE(session), NUM2HANDLE(handle), tmp, template_size);
707
+ if(rv != CKR_OK){
708
+ for (i = 0; i < template_size; i++){
709
+ CK_ATTRIBUTE_PTR attr = tmp + i;
710
+ if (attr->pValue) free(attr->pValue);
711
+ }
712
+ free(tmp);
713
+ pkcs11_raise(self,rv);
714
+ }
715
+ ary = rb_ary_new();
716
+ for (i = 0; i < template_size; i++){
717
+ CK_ATTRIBUTE_PTR attr = tmp + i;
718
+ if (attr->ulValueLen != (CK_ULONG)-1){
719
+ VALUE v = pkcs11_new_struct(class_attr);
720
+ memcpy(DATA_PTR(v), attr, sizeof(CK_ATTRIBUTE));
721
+ rb_ary_push(ary, v);
722
+ }
723
+ }
724
+ free(tmp);
725
+
726
+ return ary;
727
+ }
728
+
729
+ static VALUE
730
+ pkcs11_C_SetAttributeValue(VALUE self, VALUE session, VALUE handle, VALUE template)
731
+ {
732
+ CK_C_SetAttributeValue func;
733
+ CK_RV rv;
734
+ CK_ATTRIBUTE *tmp;
735
+ CK_ULONG template_size;
736
+
737
+ tmp = pkcs11_attr_ary2buf(template);
738
+ template_size = RARRAY_LEN(template);
739
+ GetFunction(self, C_SetAttributeValue, func);
740
+ CallFunction(C_SetAttributeValue, func, rv, NUM2HANDLE(session), NUM2HANDLE(handle), tmp, template_size);
741
+ free(tmp);
742
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
743
+
744
+ return self;
745
+ }
746
+
747
+ static VALUE
748
+ pkcs11_C_SeedRandom(VALUE self, VALUE session, VALUE seed)
749
+ {
750
+ CK_C_SeedRandom func;
751
+ CK_RV rv;
752
+
753
+ GetFunction(self, C_SeedRandom, func);
754
+ CallFunction(C_SeedRandom, func, rv, NUM2HANDLE(session),
755
+ (CK_BYTE_PTR)RSTRING_PTR(seed), RSTRING_LEN(seed));
756
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
757
+
758
+ return self;
759
+ }
760
+
761
+ static VALUE
762
+ pkcs11_C_GenerateRandom(VALUE self, VALUE session, VALUE size)
763
+ {
764
+ CK_C_GenerateRandom func;
765
+ CK_ULONG sz = NUM2ULONG(size);
766
+ VALUE buf = rb_str_new(0, sz);
767
+ CK_RV rv;
768
+
769
+ GetFunction(self, C_GenerateRandom, func);
770
+ CallFunction(C_GenerateRandom, func, rv, NUM2HANDLE(session), (CK_BYTE_PTR)RSTRING_PTR(buf), sz);
771
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
772
+
773
+ return buf;
774
+ }
775
+
776
+ static VALUE
777
+ pkcs11_C_WaitForSlotEvent(VALUE self, VALUE flags)
778
+ {
779
+ CK_C_WaitForSlotEvent func;
780
+ CK_RV rv;
781
+ CK_SLOT_ID slot_id;
782
+
783
+ GetFunction(self, C_WaitForSlotEvent, func);
784
+ CallFunction(C_WaitForSlotEvent, func, rv, NUM2ULONG(flags), &slot_id, NULL_PTR);
785
+ if(rv == CKR_NO_EVENT) return Qnil;
786
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
787
+
788
+ return HANDLE2NUM(slot_id);
789
+ }
790
+
791
+ ///////////////////////////////////////
792
+
793
+ typedef VALUE (*init_func)
794
+ (CK_SESSION_HANDLE, CK_MECHANISM_PTR, CK_OBJECT_HANDLE);
795
+ typedef VALUE (*crypt_func)
796
+ (CK_SESSION_HANDLE, CK_BYTE_PTR, CK_ULONG, CK_BYTE_PTR, CK_ULONG_PTR);
797
+ typedef VALUE (*crypt_update_func)
798
+ (CK_SESSION_HANDLE, CK_BYTE_PTR, CK_ULONG, CK_BYTE_PTR, CK_ULONG_PTR);
799
+ typedef VALUE (*crypt_final_func)
800
+ (CK_SESSION_HANDLE, CK_BYTE_PTR, CK_ULONG_PTR);
801
+ typedef VALUE (*sign_update_func)
802
+ (CK_SESSION_HANDLE, CK_BYTE_PTR, CK_ULONG);
803
+ typedef VALUE (*verify_func)
804
+ (CK_SESSION_HANDLE, CK_BYTE_PTR, CK_ULONG, CK_BYTE_PTR, CK_ULONG);
805
+ typedef VALUE (*verify_final_func)
806
+ (CK_SESSION_HANDLE, CK_BYTE_PTR, CK_ULONG);
807
+
808
+ #define common_crypt(self, s, d, sz, f) common_crypt_update(self, s, d, sz, f)
809
+
810
+ static VALUE
811
+ common_init(VALUE self, VALUE session, VALUE mechanism, VALUE key, init_func func)
812
+ {
813
+ CK_RV rv;
814
+ CK_MECHANISM_PTR m;
815
+
816
+ if (!rb_obj_is_kind_of(mechanism, cCK_MECHANISM))
817
+ rb_raise(rb_eArgError, "2nd arg must be a PKCS11::CK_MECHANISM");
818
+ m = DATA_PTR(mechanism);
819
+ /* Use the function signature of any of the various C_*Init functions. */
820
+ CallFunction(C_EncryptInit, func, rv, NUM2HANDLE(session), m, NUM2HANDLE(key));
821
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
822
+
823
+ return Qnil;
824
+ }
825
+
826
+ static VALUE
827
+ common_crypt_update(VALUE self, VALUE session, VALUE data, VALUE size, crypt_update_func func)
828
+ {
829
+ CK_RV rv;
830
+ CK_ULONG sz = 0;
831
+ VALUE buf;
832
+
833
+ StringValue(data);
834
+ if (NIL_P(size)){
835
+ CallFunction(C_EncryptUpdate, func, rv, NUM2HANDLE(session),
836
+ (CK_BYTE_PTR)RSTRING_PTR(data), RSTRING_LEN(data),
837
+ NULL_PTR, &sz);
838
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
839
+ }else{
840
+ sz = NUM2ULONG(size);
841
+ }
842
+ buf = rb_str_new(0, sz);
843
+
844
+ CallFunction(C_EncryptUpdate, func, rv, NUM2HANDLE(session),
845
+ (CK_BYTE_PTR)RSTRING_PTR(data), RSTRING_LEN(data),
846
+ (CK_BYTE_PTR)RSTRING_PTR(buf), &sz);
847
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
848
+ rb_str_set_len(buf, sz);
849
+
850
+ return buf;
851
+ }
852
+
853
+ static VALUE
854
+ common_crypt_final(VALUE self, VALUE session, VALUE size, crypt_final_func func)
855
+ {
856
+ CK_RV rv;
857
+ CK_ULONG sz = 0;
858
+ VALUE buf;
859
+
860
+ if (NIL_P(size)){
861
+ CallFunction(C_EncryptFinal, func, rv, NUM2HANDLE(session), NULL_PTR, &sz);
862
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
863
+ }else{
864
+ sz = NUM2ULONG(size);
865
+ }
866
+ buf = rb_str_new(0, sz);
867
+
868
+ CallFunction(C_EncryptFinal, func, rv, NUM2HANDLE(session), (CK_BYTE_PTR)RSTRING_PTR(buf), &sz);
869
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
870
+ rb_str_set_len(buf, sz);
871
+
872
+ return buf;
873
+ }
874
+
875
+ static VALUE
876
+ common_sign_update(VALUE self, VALUE session, VALUE data, sign_update_func func)
877
+ {
878
+ CK_RV rv;
879
+
880
+ StringValue(data);
881
+ CallFunction(C_SignUpdate, func, rv, NUM2HANDLE(session),
882
+ (CK_BYTE_PTR)RSTRING_PTR(data), RSTRING_LEN(data));
883
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
884
+
885
+ return Qnil;
886
+ }
887
+
888
+ static VALUE
889
+ common_verify(VALUE self, VALUE session, VALUE data, VALUE sig, verify_func func)
890
+ {
891
+ CK_RV rv;
892
+
893
+ StringValue(data);
894
+ StringValue(sig);
895
+ /* Use the function signature of any of the various C_Verify* functions. */
896
+ CallFunction(C_Verify, func, rv, NUM2HANDLE(session),
897
+ (CK_BYTE_PTR)RSTRING_PTR(data), RSTRING_LEN(data),
898
+ (CK_BYTE_PTR)RSTRING_PTR(sig), RSTRING_LEN(sig));
899
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
900
+
901
+ return Qnil;
902
+ }
903
+
904
+ ////
905
+
906
+ static VALUE
907
+ pkcs11_C_EncryptInit(VALUE self, VALUE session, VALUE mechanism, VALUE key)
908
+ {
909
+ CK_C_EncryptInit func;
910
+ GetFunction(self, C_EncryptInit, func);
911
+ common_init(self, session, mechanism, key, func);
912
+ return self;
913
+ }
914
+
915
+ static VALUE
916
+ pkcs11_C_Encrypt(VALUE self, VALUE session, VALUE data, VALUE size)
917
+ {
918
+ CK_C_Encrypt func;
919
+ GetFunction(self, C_Encrypt, func);
920
+ return common_crypt(self, session, data, size, func);
921
+ }
922
+
923
+ static VALUE
924
+ pkcs11_C_EncryptUpdate(VALUE self, VALUE session, VALUE data, VALUE size)
925
+ {
926
+ CK_C_EncryptUpdate func;
927
+ GetFunction(self, C_EncryptUpdate, func);
928
+ return common_crypt_update(self, session, data, size, func);
929
+ }
930
+
931
+ static VALUE
932
+ pkcs11_C_EncryptFinal(VALUE self, VALUE session, VALUE size)
933
+ {
934
+ CK_C_EncryptFinal func;
935
+ GetFunction(self, C_EncryptFinal, func);
936
+ return common_crypt_final(self, session, size, func);
937
+ }
938
+
939
+ static VALUE
940
+ pkcs11_C_DecryptInit(VALUE self, VALUE session, VALUE mechanism, VALUE key)
941
+ {
942
+ CK_C_DecryptInit func;
943
+ GetFunction(self, C_DecryptInit, func);
944
+ common_init(self, session, mechanism, key, func);
945
+ return self;
946
+ }
947
+
948
+ static VALUE
949
+ pkcs11_C_Decrypt(VALUE self, VALUE session, VALUE data, VALUE size)
950
+ {
951
+ CK_C_Decrypt func;
952
+ GetFunction(self, C_Decrypt, func);
953
+ return common_crypt(self, session, data, size, func);
954
+ }
955
+
956
+ static VALUE
957
+ pkcs11_C_DecryptUpdate(VALUE self, VALUE session, VALUE data, VALUE size)
958
+ {
959
+ CK_C_DecryptUpdate func;
960
+ GetFunction(self, C_DecryptUpdate, func);
961
+ return common_crypt_update(self, session, data, size, func);
962
+ }
963
+
964
+ static VALUE
965
+ pkcs11_C_DecryptFinal(VALUE self, VALUE session, VALUE size)
966
+ {
967
+ CK_C_DecryptFinal func;
968
+ GetFunction(self, C_DecryptFinal, func);
969
+ return common_crypt_final(self, session, size, func);
970
+ }
971
+
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)
977
+
978
+ static VALUE
979
+ pkcs11_C_SignInit(VALUE self, VALUE session, VALUE mechanism, VALUE key)
980
+ {
981
+ CK_C_SignInit func;
982
+ GetFunction(self, C_SignInit, func);
983
+ common_init(self, session, mechanism, key, func);
984
+ return self;
985
+ }
986
+
987
+ static VALUE
988
+ pkcs11_C_Sign(VALUE self, VALUE session, VALUE data, VALUE size)
989
+ {
990
+ CK_C_Sign func;
991
+ GetFunction(self, C_Sign, func);
992
+ return common_sign(self, session, data, size, func);
993
+ }
994
+
995
+ static VALUE
996
+ pkcs11_C_SignUpdate(VALUE self, VALUE session, VALUE data)
997
+ {
998
+ CK_C_SignUpdate func;
999
+ GetFunction(self, C_SignUpdate, func);
1000
+ common_sign_update(self, session, data, func);
1001
+ return self;
1002
+ }
1003
+
1004
+ static VALUE
1005
+ pkcs11_C_SignFinal(VALUE self, VALUE session, VALUE size)
1006
+ {
1007
+ CK_C_SignFinal func;
1008
+ GetFunction(self, C_SignFinal, func);
1009
+ return common_sign_final(self, session, size, func);
1010
+ }
1011
+
1012
+ static VALUE
1013
+ pkcs11_C_SignRecoverInit(VALUE self, VALUE session, VALUE mechanism, VALUE key)
1014
+ {
1015
+ CK_C_SignRecoverInit func;
1016
+ GetFunction(self, C_SignRecoverInit, func);
1017
+ common_init(self, session, mechanism, key, func);
1018
+ return self;
1019
+ }
1020
+
1021
+ static VALUE
1022
+ pkcs11_C_SignRecover(VALUE self, VALUE session, VALUE data, VALUE size)
1023
+ {
1024
+ CK_C_SignRecover func;
1025
+ GetFunction(self, C_SignRecover, func);
1026
+ return common_sign(self, session, data, size, func);
1027
+ }
1028
+
1029
+ static VALUE
1030
+ pkcs11_C_VerifyInit(VALUE self, VALUE session, VALUE mechanism, VALUE key)
1031
+ {
1032
+ CK_C_VerifyInit func;
1033
+ GetFunction(self, C_VerifyInit, func);
1034
+ common_init(self, session, mechanism, key, func);
1035
+ return self;
1036
+ }
1037
+
1038
+ static VALUE
1039
+ pkcs11_C_Verify(VALUE self, VALUE session, VALUE data, VALUE sig)
1040
+ {
1041
+ CK_C_Verify func;
1042
+ GetFunction(self, C_Verify, func);
1043
+ common_verify(self, session, data, sig, func);
1044
+ return Qtrue;
1045
+ }
1046
+
1047
+ static VALUE
1048
+ pkcs11_C_VerifyUpdate(VALUE self, VALUE session, VALUE data)
1049
+ {
1050
+ CK_C_VerifyUpdate func;
1051
+ GetFunction(self, C_VerifyUpdate, func);
1052
+ common_verify_update(self, session, data, func);
1053
+ return self;
1054
+ }
1055
+
1056
+ static VALUE
1057
+ pkcs11_C_VerifyFinal(VALUE self, VALUE session, VALUE sig)
1058
+ {
1059
+ CK_C_VerifyFinal func;
1060
+ GetFunction(self, C_VerifyFinal, func);
1061
+ common_verify_final(self, session, sig, func);
1062
+ return Qtrue;
1063
+ }
1064
+
1065
+ static VALUE
1066
+ pkcs11_C_VerifyRecoverInit(VALUE self, VALUE session, VALUE mechanism, VALUE key)
1067
+ {
1068
+ CK_C_VerifyRecoverInit func;
1069
+ GetFunction(self, C_VerifyRecoverInit, func);
1070
+ common_init(self, session, mechanism, key, func);
1071
+ return self;
1072
+ }
1073
+
1074
+ static VALUE
1075
+ pkcs11_C_VerifyRecover(VALUE self, VALUE session, VALUE sig, VALUE size)
1076
+ {
1077
+ CK_C_VerifyRecover func;
1078
+ GetFunction(self, C_VerifyRecover, func);
1079
+ common_verify_recover(self, session, sig, size, func);
1080
+ return Qtrue;
1081
+ }
1082
+
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)
1086
+
1087
+ VALUE
1088
+ pkcs11_C_DigestInit(VALUE self, VALUE session, VALUE mechanism)
1089
+ {
1090
+ CK_C_DigestInit func;
1091
+ CK_MECHANISM_PTR m;
1092
+ CK_RV rv;
1093
+
1094
+ GetFunction(self, C_DigestInit, func);
1095
+ if (!rb_obj_is_kind_of(mechanism, cCK_MECHANISM))
1096
+ rb_raise(rb_eArgError, "2nd arg must be a PKCS11::CK_MECHANISM");
1097
+ m = DATA_PTR(mechanism);
1098
+ CallFunction(C_DigestInit, func, rv, NUM2HANDLE(session), m);
1099
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
1100
+
1101
+ return self;
1102
+ }
1103
+
1104
+ VALUE
1105
+ pkcs11_C_Digest(VALUE self, VALUE session, VALUE data, VALUE size)
1106
+ {
1107
+ CK_C_Digest func;
1108
+ GetFunction(self, C_Digest, func);
1109
+ return common_digest(self, session, data, size, func);
1110
+ }
1111
+
1112
+ VALUE
1113
+ pkcs11_C_DigestUpdate(VALUE self, VALUE session, VALUE data)
1114
+ {
1115
+ CK_C_DigestUpdate func;
1116
+ GetFunction(self, C_DigestUpdate, func);
1117
+ common_digest_update(self, session, data, func);
1118
+ return self;
1119
+ }
1120
+
1121
+ VALUE
1122
+ pkcs11_C_DigestKey(VALUE self, VALUE session, VALUE handle)
1123
+ {
1124
+ CK_C_DigestKey func;
1125
+ CK_RV rv;
1126
+
1127
+ GetFunction(self, C_DigestKey, func);
1128
+ CallFunction(C_DigestKey, func, rv, NUM2HANDLE(session), NUM2HANDLE(handle));
1129
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
1130
+
1131
+ return self;
1132
+ }
1133
+
1134
+ VALUE
1135
+ pkcs11_C_DigestFinal(VALUE self, VALUE session, VALUE size)
1136
+ {
1137
+ CK_C_DigestFinal func;
1138
+ GetFunction(self, C_DigestFinal, func);
1139
+ return common_digest_final(self, session, size, func);
1140
+ }
1141
+
1142
+ VALUE
1143
+ pkcs11_C_DigestEncryptUpdate(VALUE self, VALUE session, VALUE data, VALUE size)
1144
+ {
1145
+ CK_C_DigestEncryptUpdate func;
1146
+ GetFunction(self, C_DigestEncryptUpdate, func);
1147
+ return common_crypt_update(self, session, data, size, func);
1148
+ }
1149
+
1150
+ VALUE
1151
+ pkcs11_C_DecryptDigestUpdate(VALUE self, VALUE session, VALUE data, VALUE size)
1152
+ {
1153
+ CK_C_DecryptDigestUpdate func;
1154
+ GetFunction(self, C_DecryptDigestUpdate, func);
1155
+ return common_crypt_update(self, session, data, size, func);
1156
+ }
1157
+
1158
+ VALUE
1159
+ pkcs11_C_SignEncryptUpdate(VALUE self, VALUE session, VALUE data, VALUE size)
1160
+ {
1161
+ CK_C_SignEncryptUpdate func;
1162
+ GetFunction(self, C_SignEncryptUpdate, func);
1163
+ return common_crypt_update(self, session, data, size, func);
1164
+ }
1165
+
1166
+ VALUE
1167
+ pkcs11_C_DecryptVerifyUpdate(VALUE self, VALUE session, VALUE data, VALUE size)
1168
+ {
1169
+ CK_C_DecryptVerifyUpdate func;
1170
+ GetFunction(self, C_DecryptVerifyUpdate, func);
1171
+ return common_crypt_update(self, session, data, size, func);
1172
+ }
1173
+
1174
+ VALUE
1175
+ pkcs11_C_GenerateKey(VALUE self, VALUE session, VALUE mechanism, VALUE template){
1176
+ CK_C_GenerateKey func;
1177
+ CK_ATTRIBUTE_PTR tmp;
1178
+ CK_OBJECT_HANDLE handle;
1179
+ CK_MECHANISM_PTR m;
1180
+ CK_RV rv;
1181
+
1182
+ GetFunction(self, C_GenerateKey, func);
1183
+ if (!rb_obj_is_kind_of(mechanism, cCK_MECHANISM))
1184
+ rb_raise(rb_eArgError, "2nd arg must be a PKCS11::CK_MECHANISM");
1185
+ m = DATA_PTR(mechanism);
1186
+ tmp = pkcs11_attr_ary2buf(template);
1187
+ CallFunction(C_GenerateKey, func, rv, NUM2HANDLE(session), m, tmp, RARRAY_LEN(template), &handle);
1188
+ free(tmp);
1189
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
1190
+
1191
+ return HANDLE2NUM(handle);
1192
+ }
1193
+
1194
+ VALUE
1195
+ pkcs11_C_GenerateKeyPair(VALUE self, VALUE session, VALUE mechanism, VALUE pubkey_template, VALUE privkey_template)
1196
+ {
1197
+ CK_C_GenerateKeyPair func;
1198
+ CK_ATTRIBUTE_PTR pubkey_tmp, privkey_tmp;
1199
+ CK_OBJECT_HANDLE pubkey_handle, privkey_handle;
1200
+ CK_MECHANISM_PTR m;
1201
+ CK_RV rv;
1202
+ VALUE ary;
1203
+
1204
+ GetFunction(self, C_GenerateKeyPair, func);
1205
+ if (!rb_obj_is_kind_of(mechanism, cCK_MECHANISM))
1206
+ rb_raise(rb_eArgError, "2nd arg must be a PKCS11::CK_MECHANISM");
1207
+ m = DATA_PTR(mechanism);
1208
+ pubkey_tmp = pkcs11_attr_ary2buf(pubkey_template);
1209
+ privkey_tmp = pkcs11_attr_ary2buf(privkey_template);
1210
+
1211
+ CallFunction(C_GenerateKeyPair, func, rv, NUM2HANDLE(session), m,
1212
+ pubkey_tmp, RARRAY_LEN(pubkey_template),
1213
+ privkey_tmp, RARRAY_LEN(privkey_template),
1214
+ &pubkey_handle, &privkey_handle);
1215
+ free(pubkey_tmp);
1216
+ free(privkey_tmp);
1217
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
1218
+ ary = rb_ary_new();
1219
+ rb_ary_push(ary, HANDLE2NUM(pubkey_handle));
1220
+ rb_ary_push(ary, HANDLE2NUM(privkey_handle));
1221
+
1222
+ return ary;
1223
+ }
1224
+
1225
+ VALUE
1226
+ pkcs11_C_WrapKey(VALUE self, VALUE session, VALUE mechanism, VALUE wrapping, VALUE wrapped, VALUE size)
1227
+ {
1228
+ CK_C_WrapKey func;
1229
+ CK_MECHANISM_PTR m;
1230
+ CK_ULONG sz = 0;
1231
+ VALUE buf;
1232
+ CK_RV rv;
1233
+
1234
+ GetFunction(self, C_WrapKey, func);
1235
+ if (!rb_obj_is_kind_of(mechanism, cCK_MECHANISM))
1236
+ rb_raise(rb_eArgError, "2nd arg must be a PKCS11::CK_MECHANISM");
1237
+ m = DATA_PTR(mechanism);
1238
+ if (NIL_P(size)){
1239
+ CallFunction(C_WrapKey, func, rv, NUM2HANDLE(session), m,
1240
+ NUM2HANDLE(wrapping), NUM2HANDLE(wrapped),
1241
+ (CK_BYTE_PTR)NULL_PTR, &sz);
1242
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
1243
+ }else{
1244
+ sz = NUM2ULONG(size);
1245
+ }
1246
+ buf = rb_str_new(0, sz);
1247
+
1248
+ CallFunction(C_WrapKey, func, rv, NUM2HANDLE(session), m,
1249
+ NUM2HANDLE(wrapping), NUM2HANDLE(wrapped),
1250
+ (CK_BYTE_PTR)RSTRING_PTR(buf), &sz);
1251
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
1252
+ rb_str_set_len(buf, sz);
1253
+
1254
+ return buf;
1255
+ }
1256
+
1257
+ VALUE
1258
+ pkcs11_C_UnwrapKey(VALUE self, VALUE session, VALUE mechanism, VALUE wrapping, VALUE wrapped, VALUE template)
1259
+ {
1260
+ CK_C_UnwrapKey func;
1261
+ CK_MECHANISM_PTR m;
1262
+ CK_ATTRIBUTE_PTR tmp;
1263
+ CK_OBJECT_HANDLE h;
1264
+ CK_RV rv;
1265
+
1266
+ GetFunction(self, C_UnwrapKey, func);
1267
+ if (!rb_obj_is_kind_of(mechanism, cCK_MECHANISM))
1268
+ rb_raise(rb_eArgError, "2nd arg must be a PKCS11::CK_MECHANISM");
1269
+ m = DATA_PTR(mechanism);
1270
+ tmp = pkcs11_attr_ary2buf(template);
1271
+ CallFunction(C_UnwrapKey, func, rv, NUM2HANDLE(session), m, NUM2HANDLE(wrapping),
1272
+ (CK_BYTE_PTR)RSTRING_PTR(wrapped), RSTRING_LEN(wrapped),
1273
+ tmp, RARRAY_LEN(template), &h);
1274
+ free(tmp);
1275
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
1276
+
1277
+ return HANDLE2NUM(h);
1278
+ }
1279
+
1280
+ VALUE
1281
+ pkcs11_C_DeriveKey(VALUE self, VALUE session, VALUE mechanism, VALUE base, VALUE template)
1282
+ {
1283
+ CK_C_DeriveKey func;
1284
+ CK_MECHANISM_PTR m;
1285
+ CK_ATTRIBUTE_PTR tmp;
1286
+ CK_OBJECT_HANDLE h;
1287
+ CK_RV rv;
1288
+
1289
+ GetFunction(self, C_DeriveKey, func);
1290
+ if (!rb_obj_is_kind_of(mechanism, cCK_MECHANISM))
1291
+ rb_raise(rb_eArgError, "2nd arg must be a PKCS11::CK_MECHANISM");
1292
+ m = DATA_PTR(mechanism);
1293
+ tmp = pkcs11_attr_ary2buf(template);
1294
+ CallFunction(C_DeriveKey, func, rv, NUM2HANDLE(session), m, NUM2HANDLE(base),
1295
+ tmp, RARRAY_LEN(template), &h);
1296
+ free(tmp);
1297
+ if(rv != CKR_OK) pkcs11_raise(self,rv);
1298
+
1299
+ return HANDLE2NUM(h);
1300
+ }
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
+
1334
+ ///////////////////////////////////////
1335
+
1336
+ static void
1337
+ ck_attr_free(CK_ATTRIBUTE *attr)
1338
+ {
1339
+ if (attr->pValue) free(attr->pValue);
1340
+ free(attr);
1341
+ }
1342
+
1343
+ static VALUE
1344
+ ck_attr_s_alloc(VALUE self)
1345
+ {
1346
+ VALUE obj;
1347
+ CK_ATTRIBUTE *attr;
1348
+ obj = Data_Make_Struct(self, CK_ATTRIBUTE, 0, ck_attr_free, attr);
1349
+ return obj;
1350
+ }
1351
+
1352
+ static VALUE
1353
+ ck_attr_initialize(int argc, VALUE *argv, VALUE self)
1354
+ {
1355
+ VALUE type, value;
1356
+ CK_ATTRIBUTE *attr;
1357
+
1358
+ rb_scan_args(argc, argv, "02", &type, &value);
1359
+ Data_Get_Struct(self, CK_ATTRIBUTE, attr);
1360
+ if (argc == 0) return self;
1361
+ attr->type = NUM2HANDLE(type);
1362
+ attr->pValue = NULL;
1363
+ switch(TYPE(value)){
1364
+ case T_TRUE:
1365
+ attr->pValue = (CK_BYTE_PTR)malloc(sizeof(CK_BBOOL));
1366
+ *((CK_BBOOL*)attr->pValue) = TRUE;
1367
+ attr->ulValueLen = sizeof(CK_BBOOL);
1368
+ break;
1369
+ case T_FALSE:
1370
+ attr->pValue = (CK_BYTE_PTR)malloc(sizeof(CK_BBOOL));
1371
+ *((CK_BBOOL*)attr->pValue) = FALSE;
1372
+ attr->ulValueLen = sizeof(CK_BBOOL);
1373
+ break;
1374
+ case T_NIL:
1375
+ attr->pValue = (CK_BYTE_PTR)NULL;
1376
+ attr->ulValueLen = 0;
1377
+ break;
1378
+ case T_FIXNUM:
1379
+ case T_BIGNUM:
1380
+ attr->pValue = (CK_BYTE_PTR)malloc(sizeof(CK_OBJECT_CLASS));
1381
+ *((CK_OBJECT_CLASS*)attr->pValue) = NUM2ULONG(value);
1382
+ attr->ulValueLen = sizeof(CK_OBJECT_CLASS);
1383
+ break;
1384
+ default:
1385
+ StringValue(value);
1386
+ attr->pValue = (CK_BYTE_PTR)malloc(RSTRING_LEN(value));
1387
+ memcpy(attr->pValue, RSTRING_PTR(value), RSTRING_LEN(value));
1388
+ attr->ulValueLen = RSTRING_LEN(value);
1389
+ break;
1390
+ }
1391
+ return self;
1392
+ }
1393
+
1394
+ /* rb_define_method(cCK_ATTRIBUTE, "type", ck_attr_type, 0); */
1395
+ /*
1396
+ * @return [Integer] attribute type PKCS11::CKA_*
1397
+ */
1398
+ static VALUE
1399
+ ck_attr_type(VALUE self)
1400
+ {
1401
+ CK_ATTRIBUTE *attr;
1402
+ Data_Get_Struct(self, CK_ATTRIBUTE, attr);
1403
+ return ULONG2NUM(attr->type);
1404
+ }
1405
+
1406
+ /* rb_define_method(cCK_ATTRIBUTE, "value", ck_attr_value, 0); */
1407
+ /*
1408
+ * @return [String, Integer, Boolean] attribute value
1409
+ * @see PKCS11::Object#[]
1410
+ */
1411
+ static VALUE
1412
+ ck_attr_value(VALUE self)
1413
+ {
1414
+ CK_ATTRIBUTE *attr;
1415
+ Data_Get_Struct(self, CK_ATTRIBUTE, attr);
1416
+ if (attr->ulValueLen == 0) return Qnil;
1417
+ switch(attr->type){
1418
+ case CKA_ALWAYS_AUTHENTICATE:
1419
+ case CKA_ALWAYS_SENSITIVE:
1420
+ case CKA_COLOR:
1421
+ /* case CKA_COPYABLE: v2.3 */
1422
+ case CKA_DECRYPT:
1423
+ case CKA_DERIVE:
1424
+ case CKA_ENCRYPT:
1425
+ case CKA_EXTRACTABLE:
1426
+ case CKA_HAS_RESET:
1427
+ case CKA_LOCAL:
1428
+ case CKA_MODIFIABLE:
1429
+ case CKA_NEVER_EXTRACTABLE:
1430
+ case CKA_OTP_USER_FRIENDLY_MODE:
1431
+ case CKA_PRIVATE:
1432
+ case CKA_SENSITIVE:
1433
+ case CKA_SIGN:
1434
+ case CKA_SIGN_RECOVER:
1435
+ case CKA_TOKEN:
1436
+ case CKA_TRUSTED:
1437
+ case CKA_UNWRAP:
1438
+ case CKA_VERIFY:
1439
+ case CKA_VERIFY_RECOVER:
1440
+ case CKA_WRAP:
1441
+ case CKA_WRAP_WITH_TRUSTED:
1442
+ if (attr->ulValueLen == sizeof(CK_BBOOL))
1443
+ return (*(CK_BBOOL*)(attr->pValue)) == CK_TRUE ? Qtrue : Qfalse;
1444
+ break;
1445
+ case CKA_BITS_PER_PIXEL:
1446
+ case CKA_CERTIFICATE_CATEGORY:
1447
+ case CKA_CERTIFICATE_TYPE:
1448
+ case CKA_CHAR_COLUMNS:
1449
+ case CKA_CHAR_ROWS:
1450
+ case CKA_CLASS:
1451
+ case CKA_HW_FEATURE_TYPE:
1452
+ case CKA_JAVA_MIDP_SECURITY_DOMAIN:
1453
+ case CKA_KEY_TYPE:
1454
+ case CKA_MECHANISM_TYPE:
1455
+ case CKA_MODULUS_BITS:
1456
+ case CKA_OTP_CHALLENGE_REQUIREMENT:
1457
+ case CKA_OTP_COUNTER_REQUIREMENT:
1458
+ case CKA_OTP_FORMAT:
1459
+ case CKA_OTP_LENGTH:
1460
+ case CKA_OTP_PIN_REQUIREMENT:
1461
+ case CKA_OTP_TIME_INTERVAL:
1462
+ case CKA_OTP_TIME_REQUIREMENT:
1463
+ case CKA_OTP_SERVICE_LOGO_TYPE:
1464
+ case CKA_PIXEL_X:
1465
+ case CKA_PIXEL_Y:
1466
+ case CKA_PRIME_BITS:
1467
+ case CKA_RESOLUTION:
1468
+ case CKA_SUBPRIME_BITS:
1469
+ case CKA_VALUE_BITS:
1470
+ case CKA_VALUE_LEN:
1471
+ if (attr->ulValueLen == sizeof(CK_ULONG))
1472
+ return ULONG2NUM(*(CK_ULONG_PTR)(attr->pValue));
1473
+ break;
1474
+ }
1475
+ return rb_str_new(attr->pValue, attr->ulValueLen);
1476
+ }
1477
+
1478
+ ///////////////////////////////////////
1479
+
1480
+ #include "pk11_struct_impl.inc"
1481
+
1482
+ ///////////////////////////////////////
1483
+
1484
+ PKCS11_IMPLEMENT_ALLOCATOR(CK_MECHANISM)
1485
+
1486
+ /*
1487
+ * Spezifies a particularly crypto mechanism.
1488
+ * @param [Integer, nil] mechanism The mechanism to use (PKCS11::CKM_*)
1489
+ * @param [String, Integer, PKCS11::CStruct, nil] pParameter optional parameter to the mechanism
1490
+ */
1491
+ static VALUE
1492
+ cCK_MECHANISM_initialize(int argc, VALUE *argv, VALUE self)
1493
+ {
1494
+ VALUE type, param;
1495
+
1496
+ rb_scan_args(argc, argv, "02", &type, &param);
1497
+ rb_funcall(self, rb_intern("mechanism="), 1, type);
1498
+ rb_funcall(self, rb_intern("pParameter="), 1, param);
1499
+
1500
+ return self;
1501
+ }
1502
+
1503
+ /* rb_define_method(cCK_MECHANISM, "mechanism", cCK_MECHANISM_get_mechanism, 0); */
1504
+ /* rb_define_method(cCK_MECHANISM, "mechanism=", cCK_MECHANISM_set_mechanism, 0); */
1505
+ PKCS11_IMPLEMENT_ULONG_ACCESSOR(CK_MECHANISM, mechanism);
1506
+
1507
+ /* rb_define_method(cCK_MECHANISM, "pParameter", cCK_MECHANISM_get_pParameter, 0); */
1508
+ /* rb_define_method(cCK_MECHANISM, "pParameter=", cCK_MECHANISM_set_pParameter, 0); */
1509
+ /* @see PKCS11::CK_MECHANISM#initialize */
1510
+ static VALUE
1511
+ cCK_MECHANISM_get_pParameter(VALUE self)
1512
+ {
1513
+ CK_MECHANISM_PTR m = DATA_PTR(self);
1514
+ if (!m->pParameter) return Qnil;
1515
+ else return rb_str_new(m->pParameter, m->ulParameterLen);
1516
+ }
1517
+
1518
+ static VALUE
1519
+ cCK_MECHANISM_set_pParameter(VALUE self, VALUE value)
1520
+ {
1521
+ CK_MECHANISM_PTR m = DATA_PTR(self);
1522
+ CK_ULONG ulong_val;
1523
+
1524
+ switch(TYPE(value)){
1525
+ case T_NIL:
1526
+ m->pParameter = NULL_PTR;
1527
+ m->ulParameterLen = 0;
1528
+ break;
1529
+ case T_STRING:
1530
+ value = rb_obj_freeze(rb_str_dup(value));
1531
+ m->pParameter = RSTRING_PTR(value);
1532
+ m->ulParameterLen = RSTRING_LEN(value);
1533
+ break;
1534
+ case T_FIXNUM:
1535
+ case T_BIGNUM:
1536
+ ulong_val = NUM2ULONG(value);
1537
+ value = rb_obj_freeze(rb_str_new((char*)&ulong_val, sizeof(ulong_val)));
1538
+ m->pParameter = RSTRING_PTR(value);
1539
+ m->ulParameterLen = RSTRING_LEN(value);
1540
+ break;
1541
+ case T_DATA:
1542
+ m->pParameter = DATA_PTR(value);
1543
+ m->ulParameterLen = NUM2LONG(rb_const_get(rb_funcall(value, rb_intern("class"), 0), rb_intern("SIZEOF_STRUCT")));
1544
+ break;
1545
+ default:
1546
+ rb_raise(rb_eArgError, "invalid argument");
1547
+ }
1548
+ /* don't GC the value as long as this object is active */
1549
+ rb_iv_set(self, "pParameter", value);
1550
+
1551
+ return value;
1552
+ }
1553
+
1554
+
1555
+ void
1556
+ Init_pkcs11_ext()
1557
+ {
1558
+ mPKCS11 = rb_define_module("PKCS11");
1559
+ sNEW = rb_intern("new");
1560
+ cPKCS11 = rb_define_class_under(mPKCS11, "Library", rb_cObject);
1561
+
1562
+ /* Document-method: PKCS11.open
1563
+ *
1564
+ * Alias function for {PKCS11::Library#initialize}
1565
+ */
1566
+ rb_define_module_function(mPKCS11, "open", pkcs11_library_new, -1);
1567
+
1568
+ /* Library version */
1569
+ rb_define_const( mPKCS11, "VERSION", rb_str_new2(VERSION) );
1570
+
1571
+ /* Document-class: PKCS11::Error
1572
+ *
1573
+ * Base class for all Cryptoki exceptions (CKR_*) */
1574
+ ePKCS11Error = rb_define_class_under(mPKCS11, "Error", rb_eStandardError);
1575
+ rb_define_alloc_func(cPKCS11, pkcs11_s_alloc);
1576
+ rb_define_method(cPKCS11, "initialize", pkcs11_initialize, -1);
1577
+
1578
+ PKCS11_DEFINE_METHOD(load_library, 1);
1579
+ PKCS11_DEFINE_METHOD(C_GetFunctionList, 0);
1580
+ PKCS11_DEFINE_METHOD(C_Initialize, -1);
1581
+ PKCS11_DEFINE_METHOD(C_GetInfo, 0);
1582
+ PKCS11_DEFINE_METHOD(C_GetSlotList, 1);
1583
+ PKCS11_DEFINE_METHOD(C_GetSlotInfo, 1);
1584
+ PKCS11_DEFINE_METHOD(C_GetTokenInfo, 1);
1585
+ PKCS11_DEFINE_METHOD(C_GetMechanismList, 1);
1586
+ PKCS11_DEFINE_METHOD(C_GetMechanismInfo, 2);
1587
+ PKCS11_DEFINE_METHOD(C_InitToken, 3);
1588
+ PKCS11_DEFINE_METHOD(C_InitPIN, 2);
1589
+
1590
+ PKCS11_DEFINE_METHOD(C_OpenSession, 2);
1591
+ PKCS11_DEFINE_METHOD(C_CloseSession, 1);
1592
+ PKCS11_DEFINE_METHOD(C_CloseAllSessions, 1);
1593
+ PKCS11_DEFINE_METHOD(C_GetSessionInfo, 1);
1594
+ PKCS11_DEFINE_METHOD(C_GetOperationState, 1);
1595
+ PKCS11_DEFINE_METHOD(C_SetOperationState, 4);
1596
+ PKCS11_DEFINE_METHOD(C_Login, 3);
1597
+ PKCS11_DEFINE_METHOD(C_Logout, 1);
1598
+ PKCS11_DEFINE_METHOD(C_SetPIN, 3);
1599
+
1600
+ PKCS11_DEFINE_METHOD(C_CreateObject, 2);
1601
+ PKCS11_DEFINE_METHOD(C_CopyObject, 3);
1602
+ PKCS11_DEFINE_METHOD(C_DestroyObject, 2);
1603
+ PKCS11_DEFINE_METHOD(C_GetObjectSize, 2);
1604
+ PKCS11_DEFINE_METHOD(C_FindObjectsInit, 2);
1605
+ PKCS11_DEFINE_METHOD(C_FindObjectsFinal, 1);
1606
+ PKCS11_DEFINE_METHOD(C_FindObjects, 2);
1607
+ PKCS11_DEFINE_METHOD(C_GetAttributeValue, 3);
1608
+ PKCS11_DEFINE_METHOD(C_SetAttributeValue, 3);
1609
+
1610
+ PKCS11_DEFINE_METHOD(C_EncryptInit, 3);
1611
+ PKCS11_DEFINE_METHOD(C_Encrypt, 3);
1612
+ PKCS11_DEFINE_METHOD(C_EncryptUpdate, 3);
1613
+ PKCS11_DEFINE_METHOD(C_EncryptFinal, 2);
1614
+ PKCS11_DEFINE_METHOD(C_DecryptInit, 3);
1615
+ PKCS11_DEFINE_METHOD(C_Decrypt, 3);
1616
+ PKCS11_DEFINE_METHOD(C_DecryptUpdate, 3);
1617
+ PKCS11_DEFINE_METHOD(C_DecryptFinal, 2);
1618
+ PKCS11_DEFINE_METHOD(C_DigestInit, 2);
1619
+ PKCS11_DEFINE_METHOD(C_Digest, 3);
1620
+ PKCS11_DEFINE_METHOD(C_DigestUpdate, 2);
1621
+ PKCS11_DEFINE_METHOD(C_DigestKey, 2);
1622
+ PKCS11_DEFINE_METHOD(C_DigestFinal, 2);
1623
+ PKCS11_DEFINE_METHOD(C_SignInit, 3);
1624
+ PKCS11_DEFINE_METHOD(C_Sign, 3);
1625
+ PKCS11_DEFINE_METHOD(C_SignUpdate, 2);
1626
+ PKCS11_DEFINE_METHOD(C_SignFinal, 2);
1627
+ PKCS11_DEFINE_METHOD(C_SignRecoverInit, 3);
1628
+ PKCS11_DEFINE_METHOD(C_SignRecover, 3);
1629
+ PKCS11_DEFINE_METHOD(C_VerifyInit, 3);
1630
+ PKCS11_DEFINE_METHOD(C_Verify, 3);
1631
+ PKCS11_DEFINE_METHOD(C_VerifyUpdate, 2);
1632
+ PKCS11_DEFINE_METHOD(C_VerifyFinal, 2);
1633
+ PKCS11_DEFINE_METHOD(C_VerifyRecoverInit, 3);
1634
+ PKCS11_DEFINE_METHOD(C_VerifyRecover, 3);
1635
+ PKCS11_DEFINE_METHOD(C_DigestEncryptUpdate, 3);
1636
+ PKCS11_DEFINE_METHOD(C_DecryptDigestUpdate, 3);
1637
+ PKCS11_DEFINE_METHOD(C_SignEncryptUpdate, 3);
1638
+ PKCS11_DEFINE_METHOD(C_DecryptVerifyUpdate, 3);
1639
+ PKCS11_DEFINE_METHOD(C_GenerateKey, 3);
1640
+ PKCS11_DEFINE_METHOD(C_GenerateKeyPair, 4);
1641
+ PKCS11_DEFINE_METHOD(C_WrapKey, 5);
1642
+ PKCS11_DEFINE_METHOD(C_UnwrapKey, 5);
1643
+ PKCS11_DEFINE_METHOD(C_DeriveKey, 4);
1644
+ PKCS11_DEFINE_METHOD(C_SeedRandom, 2);
1645
+ PKCS11_DEFINE_METHOD(C_GenerateRandom, 2);
1646
+
1647
+ PKCS11_DEFINE_METHOD(C_WaitForSlotEvent, 1);
1648
+ PKCS11_DEFINE_METHOD(C_Finalize, 0);
1649
+ PKCS11_DEFINE_METHOD(unload_library, 0);
1650
+ PKCS11_DEFINE_METHOD(vendor_raise_on_return_value, 1);
1651
+ PKCS11_DEFINE_METHOD(vendor_class_CK_ATTRIBUTE, 0);
1652
+
1653
+ ///////////////////////////////////////
1654
+
1655
+ cCStruct = rb_define_class_under(mPKCS11, "CStruct", rb_cObject);
1656
+
1657
+ cCK_ATTRIBUTE = rb_define_class_under(mPKCS11, "CK_ATTRIBUTE", rb_cObject);
1658
+ rb_define_alloc_func(cCK_ATTRIBUTE, ck_attr_s_alloc);
1659
+ rb_define_method(cCK_ATTRIBUTE, "initialize", ck_attr_initialize, -1);
1660
+ rb_define_method(cCK_ATTRIBUTE, "type", ck_attr_type, 0);
1661
+ rb_define_method(cCK_ATTRIBUTE, "value", ck_attr_value, 0);
1662
+
1663
+ /* Document-class: PKCS11::CK_MECHANISM
1664
+ *
1665
+ * Describes a crypto mechanism CKM_* with optional parameters. */
1666
+ /* cCK_MECHANISM = rb_define_class_under(mPKCS11, "CK_MECHANISM", rb_cObject); */
1667
+ PKCS11_DEFINE_STRUCT(CK_MECHANISM);
1668
+ rb_define_method(cCK_MECHANISM, "initialize", cCK_MECHANISM_initialize, -1);
1669
+ PKCS11_DEFINE_MEMBER(CK_MECHANISM, mechanism);
1670
+ PKCS11_DEFINE_MEMBER(CK_MECHANISM, pParameter);
1671
+
1672
+ #include "pk11_struct_def.inc"
1673
+
1674
+ Init_pkcs11_const(mPKCS11, ePKCS11Error);
1675
+ }