pkcs11 0.1.0-x86-mswin32 → 0.2.0-x86-mswin32
Sign up to get free protection for your applications and to get access to all the features.
- data/.yardopts +2 -0
- data/History.txt +23 -0
- data/Manifest.txt +6 -0
- data/README.rdoc +28 -15
- data/Rakefile +51 -2
- data/ext/extconf.rb +1 -0
- data/ext/generate_structs.rb +157 -0
- data/ext/generate_thread_funcs.rb +72 -0
- data/ext/pk11.c +482 -252
- data/ext/pk11.h +4 -1
- data/ext/pk11_const.c +25 -476
- data/ext/pk11_const_def.inc +452 -0
- data/ext/pk11_struct_def.inc +304 -0
- data/ext/pk11_struct_impl.inc +304 -0
- data/ext/pk11_thread_funcs.c +411 -0
- data/ext/pk11_thread_funcs.h +482 -0
- data/lib/1.8/pkcs11_ext.so +0 -0
- data/lib/1.9/pkcs11_ext.so +0 -0
- data/lib/pkcs11.rb +5 -8
- data/lib/pkcs11/extensions.rb +17 -109
- data/lib/pkcs11/helper.rb +151 -0
- data/lib/pkcs11/library.rb +44 -13
- data/lib/pkcs11/object.rb +73 -17
- data/lib/pkcs11/session.rb +318 -121
- data/lib/pkcs11/slot.rb +30 -9
- data/test/helper.rb +13 -6
- data/test/test_pkcs11.rb +13 -2
- data/test/test_pkcs11_crypt.rb +38 -3
- data/test/test_pkcs11_object.rb +18 -4
- data/test/test_pkcs11_session.rb +28 -2
- data/test/test_pkcs11_slot.rb +9 -6
- data/test/test_pkcs11_structs.rb +134 -0
- data/test/test_pkcs11_thread.rb +45 -0
- metadata +40 -7
data/ext/pk11.c
CHANGED
@@ -6,28 +6,36 @@
|
|
6
6
|
#include <dlfcn.h>
|
7
7
|
#endif
|
8
8
|
|
9
|
-
static const char *VERSION = "0.
|
9
|
+
static const char *VERSION = "0.2.0";
|
10
10
|
|
11
11
|
static ID sNEW;
|
12
12
|
static VALUE mPKCS11;
|
13
13
|
static VALUE cPKCS11;
|
14
14
|
static VALUE ePKCS11Error;
|
15
15
|
|
16
|
+
static VALUE cCStruct;
|
16
17
|
static VALUE cCK_ATTRIBUTE;
|
17
18
|
static VALUE cCK_C_INITIALIZE_ARGS;
|
19
|
+
static VALUE aCK_C_INITIALIZE_ARGS_members;
|
18
20
|
static VALUE cCK_INFO;
|
21
|
+
static VALUE aCK_INFO_members;
|
19
22
|
static VALUE cCK_TOKEN_INFO;
|
23
|
+
static VALUE aCK_TOKEN_INFO_members;
|
20
24
|
static VALUE cCK_SLOT_INFO;
|
25
|
+
static VALUE aCK_SLOT_INFO_members;
|
21
26
|
static VALUE cCK_MECHANISM_INFO;
|
27
|
+
static VALUE aCK_MECHANISM_INFO_members;
|
22
28
|
static VALUE cCK_SESSION_INFO;
|
29
|
+
static VALUE aCK_SESSION_INFO_members;
|
23
30
|
static VALUE cCK_MECHANISM;
|
31
|
+
static VALUE aCK_MECHANISM_members;
|
24
32
|
|
25
33
|
#define HANDLE2NUM(n) ULONG2NUM(n)
|
26
34
|
#define NUM2HANDLE(n) PKNUM2ULONG(n)
|
27
35
|
#define PKNUM2ULONG(n) pkcs11_num2ulong(n)
|
28
36
|
#define pkcs11_new_struct(klass) rb_funcall(klass, sNEW, 0)
|
29
37
|
|
30
|
-
VALUE
|
38
|
+
VALUE pkcs11_return_value_to_class(CK_RV, VALUE);
|
31
39
|
|
32
40
|
static VALUE
|
33
41
|
pkcs11_num2ulong(VALUE val)
|
@@ -41,9 +49,9 @@ pkcs11_num2ulong(VALUE val)
|
|
41
49
|
static void
|
42
50
|
pkcs11_raise(CK_RV rv)
|
43
51
|
{
|
44
|
-
VALUE
|
45
|
-
|
46
|
-
rb_raise(
|
52
|
+
VALUE class;
|
53
|
+
class = pkcs11_return_value_to_class(rv, ePKCS11Error);
|
54
|
+
rb_raise(class, "%li", rv);
|
47
55
|
}
|
48
56
|
|
49
57
|
///////////////////////////////////////
|
@@ -56,16 +64,31 @@ typedef struct {
|
|
56
64
|
#define GetFunction(obj, name, sval) \
|
57
65
|
{ \
|
58
66
|
pkcs11_ctx *ctx; \
|
59
|
-
Data_Get_Struct(
|
60
|
-
if (!ctx->functions) rb_raise(ePKCS11Error, "
|
67
|
+
Data_Get_Struct(obj, pkcs11_ctx, ctx); \
|
68
|
+
if (!ctx->functions) rb_raise(ePKCS11Error, "no function list"); \
|
61
69
|
sval = (CK_##name)ctx->functions->name; \
|
62
70
|
if (!sval) rb_raise(ePKCS11Error, #name " is not supported."); \
|
63
|
-
}
|
71
|
+
}
|
72
|
+
|
73
|
+
#ifdef HAVE_RB_THREAD_BLOCKING_REGION
|
74
|
+
#define CallFunction(name, func, rv, ...) \
|
75
|
+
{ \
|
76
|
+
struct tbr_##name##_params params = { \
|
77
|
+
func, {__VA_ARGS__}, CKR_FUNCTION_FAILED \
|
78
|
+
}; \
|
79
|
+
rb_thread_blocking_region(tbf_##name, ¶ms, RUBY_UBF_PROCESS, 0); \
|
80
|
+
rv = params.retval; \
|
81
|
+
}
|
82
|
+
|
83
|
+
#else
|
84
|
+
#define CallFunction(name, func, rv, ...) \
|
85
|
+
rv = func(__VA_ARGS__)
|
86
|
+
|
87
|
+
#endif
|
64
88
|
|
65
89
|
static void
|
66
90
|
pkcs11_ctx_unload_library(pkcs11_ctx *ctx)
|
67
91
|
{
|
68
|
-
if(ctx->functions) ctx->functions->C_Finalize(NULL_PTR);
|
69
92
|
#ifdef compile_for_windows
|
70
93
|
if(ctx->module) FreeLibrary(ctx->module);
|
71
94
|
#else
|
@@ -78,18 +101,43 @@ pkcs11_ctx_unload_library(pkcs11_ctx *ctx)
|
|
78
101
|
static void
|
79
102
|
pkcs11_ctx_free(pkcs11_ctx *ctx)
|
80
103
|
{
|
104
|
+
if(ctx->functions) ctx->functions->C_Finalize(NULL_PTR);
|
81
105
|
pkcs11_ctx_unload_library(ctx);
|
82
106
|
free(ctx);
|
83
107
|
}
|
84
108
|
|
109
|
+
/* rb_define_method(cPKCS11, "C_Finalize", pkcs11_C_Finalize, 0); */
|
110
|
+
/*
|
111
|
+
* Is called to indicate that an application is finished with the Cryptoki library.
|
112
|
+
* @see PKCS11::Library#close
|
113
|
+
*/
|
85
114
|
static VALUE
|
86
115
|
pkcs11_C_Finalize(VALUE self)
|
116
|
+
{
|
117
|
+
CK_C_Finalize func;
|
118
|
+
CK_RV rv;
|
119
|
+
|
120
|
+
GetFunction(self, C_Finalize, func);
|
121
|
+
CallFunction(C_Finalize, func, rv, NULL_PTR);
|
122
|
+
if (rv != CKR_OK) pkcs11_raise(rv);
|
123
|
+
|
124
|
+
return self;
|
125
|
+
}
|
126
|
+
|
127
|
+
/* rb_define_method(cPKCS11, "unload_library", pkcs11_unload_library, 0); */
|
128
|
+
/*
|
129
|
+
* Unloads the Cryptoki library from process memory.
|
130
|
+
* @see PKCS11::Library#close
|
131
|
+
*/
|
132
|
+
static VALUE
|
133
|
+
pkcs11_unload_library(VALUE self)
|
87
134
|
{
|
88
135
|
pkcs11_ctx *ctx;
|
89
|
-
|
136
|
+
|
90
137
|
Data_Get_Struct(self, pkcs11_ctx, ctx);
|
91
138
|
pkcs11_ctx_unload_library(ctx);
|
92
|
-
|
139
|
+
|
140
|
+
return self;
|
93
141
|
}
|
94
142
|
|
95
143
|
static VALUE
|
@@ -107,26 +155,18 @@ pkcs11_library_new(int argc, VALUE *argv, VALUE self)
|
|
107
155
|
return rb_funcall2(cPKCS11, sNEW, argc, argv);
|
108
156
|
}
|
109
157
|
|
110
|
-
|
111
|
-
|
158
|
+
/* rb_define_method(cPKCS11, "load_library", pkcs11_load_library, 0); */
|
159
|
+
/*
|
160
|
+
* Load a Cryptoki library into process memory.
|
161
|
+
* @see PKCS11::Library#initialize
|
162
|
+
*/
|
163
|
+
static VALUE
|
164
|
+
pkcs11_load_library(VALUE self, VALUE path)
|
112
165
|
{
|
113
|
-
VALUE path, init_args;
|
114
|
-
pkcs11_ctx *ctx;
|
115
166
|
const char *so_path;
|
116
|
-
|
117
|
-
CK_C_INITIALIZE_ARGS *args;
|
118
|
-
CK_RV rv;
|
167
|
+
pkcs11_ctx *ctx;
|
119
168
|
|
120
|
-
rb_scan_args(argc, argv, "11", &path, &init_args);
|
121
169
|
so_path = StringValuePtr(path);
|
122
|
-
if (NIL_P(init_args)) args = NULL_PTR;
|
123
|
-
else {
|
124
|
-
if (!rb_obj_is_kind_of(init_args, cCK_C_INITIALIZE_ARGS))
|
125
|
-
rb_raise(rb_eArgError, "2nd arg must be a PKCS11::CK_C_INITIALIZE_ARGS");
|
126
|
-
args = DATA_PTR(init_args);
|
127
|
-
}
|
128
|
-
args = NIL_P(init_args) ? NULL_PTR : DATA_PTR(init_args);
|
129
|
-
|
130
170
|
Data_Get_Struct(self, pkcs11_ctx, ctx);
|
131
171
|
#ifdef compile_for_windows
|
132
172
|
if((ctx->module = LoadLibrary(so_path)) == NULL) {
|
@@ -136,6 +176,31 @@ pkcs11_initialize(int argc, VALUE *argv, VALUE self)
|
|
136
176
|
(LPTSTR)&error_text, sizeof(error_text), NULL);
|
137
177
|
rb_raise(ePKCS11Error, error_text);
|
138
178
|
}
|
179
|
+
#else
|
180
|
+
if((ctx->module = dlopen(so_path, RTLD_NOW)) == NULL) {
|
181
|
+
rb_raise(ePKCS11Error, "%s", dlerror());
|
182
|
+
}
|
183
|
+
#endif
|
184
|
+
|
185
|
+
return self;
|
186
|
+
}
|
187
|
+
|
188
|
+
/* rb_define_method(cPKCS11, "C_GetFunctionList", pkcs11_C_GetFunctionList, 0); */
|
189
|
+
/*
|
190
|
+
* Obtains a pointer to the Cryptoki library's list of function pointers. The pointer
|
191
|
+
* is stored in the {PKCS11::Library} object and used to call any Cryptoki functions.
|
192
|
+
*
|
193
|
+
* @see PKCS11::Library#initialize
|
194
|
+
*/
|
195
|
+
static VALUE
|
196
|
+
pkcs11_C_GetFunctionList(VALUE self)
|
197
|
+
{
|
198
|
+
pkcs11_ctx *ctx;
|
199
|
+
CK_RV rv;
|
200
|
+
CK_C_GetFunctionList func;
|
201
|
+
|
202
|
+
Data_Get_Struct(self, pkcs11_ctx, ctx);
|
203
|
+
#ifdef compile_for_windows
|
139
204
|
func = (CK_C_GetFunctionList)GetProcAddress(ctx->module, "C_GetFunctionList");
|
140
205
|
if(!func){
|
141
206
|
char error_text[999] = "GetProcAddress() error";
|
@@ -145,14 +210,52 @@ pkcs11_initialize(int argc, VALUE *argv, VALUE self)
|
|
145
210
|
rb_raise(ePKCS11Error, error_text);
|
146
211
|
}
|
147
212
|
#else
|
148
|
-
if((ctx->module = dlopen(so_path, RTLD_NOW)) == NULL) {
|
149
|
-
rb_raise(ePKCS11Error, "%s", dlerror());
|
150
|
-
}
|
151
213
|
func = (CK_C_GetFunctionList)dlsym(ctx->module, "C_GetFunctionList");
|
152
214
|
if(!func) rb_raise(ePKCS11Error, "%s", dlerror());
|
153
215
|
#endif
|
154
|
-
|
155
|
-
if (
|
216
|
+
CallFunction(C_GetFunctionList, func, rv, &(ctx->functions));
|
217
|
+
if (rv != CKR_OK) pkcs11_raise(rv);
|
218
|
+
|
219
|
+
return self;
|
220
|
+
}
|
221
|
+
|
222
|
+
/* rb_define_method(cPKCS11, "C_Initialize", pkcs11_C_Initialize, 0); */
|
223
|
+
/*
|
224
|
+
* Initializes the Cryptoki library.
|
225
|
+
*/
|
226
|
+
static VALUE
|
227
|
+
pkcs11_C_Initialize(int argc, VALUE *argv, VALUE self)
|
228
|
+
{
|
229
|
+
VALUE init_args;
|
230
|
+
CK_C_Initialize func;
|
231
|
+
CK_C_INITIALIZE_ARGS *args;
|
232
|
+
CK_RV rv;
|
233
|
+
|
234
|
+
rb_scan_args(argc, argv, "01", &init_args);
|
235
|
+
if (NIL_P(init_args)) args = NULL_PTR;
|
236
|
+
else {
|
237
|
+
if (!rb_obj_is_kind_of(init_args, cCK_C_INITIALIZE_ARGS))
|
238
|
+
rb_raise(rb_eArgError, "2nd arg must be a PKCS11::CK_C_INITIALIZE_ARGS");
|
239
|
+
args = DATA_PTR(init_args);
|
240
|
+
}
|
241
|
+
GetFunction(self, C_Initialize, func);
|
242
|
+
CallFunction(C_Initialize, func, rv, args);
|
243
|
+
if (rv != CKR_OK) pkcs11_raise(rv);
|
244
|
+
|
245
|
+
return self;
|
246
|
+
}
|
247
|
+
|
248
|
+
static VALUE
|
249
|
+
pkcs11_initialize(int argc, VALUE *argv, VALUE self)
|
250
|
+
{
|
251
|
+
VALUE path, init_args;
|
252
|
+
|
253
|
+
rb_scan_args(argc, argv, "02", &path, &init_args);
|
254
|
+
if( !NIL_P(path) ){
|
255
|
+
pkcs11_load_library(self, path);
|
256
|
+
pkcs11_C_GetFunctionList(self);
|
257
|
+
pkcs11_C_Initialize(1, &init_args, self);
|
258
|
+
}
|
156
259
|
|
157
260
|
return self;
|
158
261
|
}
|
@@ -166,7 +269,7 @@ pkcs11_C_GetInfo(VALUE self)
|
|
166
269
|
|
167
270
|
GetFunction(self, C_GetInfo, func);
|
168
271
|
info = pkcs11_new_struct(cCK_INFO);
|
169
|
-
rv
|
272
|
+
CallFunction(C_GetInfo, func, rv, (CK_INFO_PTR)DATA_PTR(info));
|
170
273
|
if (rv != CKR_OK) pkcs11_raise(rv);
|
171
274
|
|
172
275
|
return info;
|
@@ -179,14 +282,14 @@ pkcs11_C_GetSlotList(VALUE self, VALUE presented)
|
|
179
282
|
CK_SLOT_ID_PTR pSlotList;
|
180
283
|
CK_RV rv;
|
181
284
|
CK_C_GetSlotList func;
|
182
|
-
|
285
|
+
CK_ULONG i;
|
183
286
|
VALUE ary = rb_ary_new();
|
184
287
|
|
185
288
|
GetFunction(self, C_GetSlotList, func);
|
186
|
-
rv
|
289
|
+
CallFunction(C_GetSlotList, func, rv, CK_FALSE, NULL_PTR, &ulSlotCount);
|
187
290
|
if (rv != CKR_OK) pkcs11_raise(rv);
|
188
291
|
pSlotList = (CK_SLOT_ID_PTR)malloc(ulSlotCount*sizeof(CK_SLOT_ID));
|
189
|
-
rv
|
292
|
+
CallFunction(C_GetSlotList, func, rv, RTEST(presented) ? CK_TRUE : CK_FALSE, pSlotList, &ulSlotCount);
|
190
293
|
if (rv != CKR_OK) {
|
191
294
|
free(pSlotList);
|
192
295
|
pkcs11_raise(rv);
|
@@ -207,7 +310,7 @@ pkcs11_C_GetSlotInfo(VALUE self, VALUE slot_id)
|
|
207
310
|
|
208
311
|
GetFunction(self, C_GetSlotInfo, func);
|
209
312
|
info = pkcs11_new_struct(cCK_SLOT_INFO);
|
210
|
-
rv
|
313
|
+
CallFunction(C_GetSlotInfo, func, rv, NUM2HANDLE(slot_id), DATA_PTR(info));
|
211
314
|
if (rv != CKR_OK) pkcs11_raise(rv);
|
212
315
|
|
213
316
|
return info;
|
@@ -222,7 +325,7 @@ pkcs11_C_GetTokenInfo(VALUE self, VALUE slot_id)
|
|
222
325
|
|
223
326
|
GetFunction(self, C_GetTokenInfo, func);
|
224
327
|
info = pkcs11_new_struct(cCK_TOKEN_INFO);
|
225
|
-
rv
|
328
|
+
CallFunction(C_GetTokenInfo, func, rv, NUM2HANDLE(slot_id), DATA_PTR(info));
|
226
329
|
if (rv != CKR_OK) pkcs11_raise(rv);
|
227
330
|
|
228
331
|
return info;
|
@@ -236,17 +339,17 @@ pkcs11_C_GetMechanismList(VALUE self, VALUE slot_id)
|
|
236
339
|
CK_MECHANISM_TYPE_PTR types;
|
237
340
|
CK_ULONG count;
|
238
341
|
VALUE ary;
|
239
|
-
|
342
|
+
CK_ULONG i;
|
240
343
|
|
241
344
|
ary = rb_ary_new();
|
242
345
|
GetFunction(self, C_GetMechanismList, func);
|
243
|
-
rv
|
346
|
+
CallFunction(C_GetMechanismList, func, rv, NUM2HANDLE(slot_id), NULL_PTR, &count);
|
244
347
|
if (rv != CKR_OK) pkcs11_raise(rv);
|
245
348
|
if (count == 0) return ary;
|
246
349
|
|
247
350
|
types = (CK_MECHANISM_TYPE_PTR)malloc(sizeof(CK_MECHANISM_TYPE)*count);
|
248
351
|
if (!types) rb_sys_fail(0);
|
249
|
-
rv
|
352
|
+
CallFunction(C_GetMechanismList, func, rv, NUM2HANDLE(slot_id), types, &count);
|
250
353
|
if (rv != CKR_OK){
|
251
354
|
free(types);
|
252
355
|
pkcs11_raise(rv);
|
@@ -267,7 +370,7 @@ pkcs11_C_GetMechanismInfo(VALUE self, VALUE slot_id, VALUE type)
|
|
267
370
|
|
268
371
|
info = pkcs11_new_struct(cCK_MECHANISM_INFO);
|
269
372
|
GetFunction(self, C_GetMechanismInfo, func);
|
270
|
-
rv
|
373
|
+
CallFunction(C_GetMechanismInfo, func, rv, NUM2HANDLE(slot_id), NUM2HANDLE(type), DATA_PTR(info));
|
271
374
|
if (rv != CKR_OK) pkcs11_raise(rv);
|
272
375
|
|
273
376
|
return info;
|
@@ -282,7 +385,7 @@ pkcs11_C_InitToken(VALUE self, VALUE slot_id, VALUE pin, VALUE label)
|
|
282
385
|
StringValue(pin);
|
283
386
|
StringValue(label);
|
284
387
|
GetFunction(self, C_InitToken, func);
|
285
|
-
rv
|
388
|
+
CallFunction(C_InitToken, func, rv, NUM2HANDLE(slot_id),
|
286
389
|
(CK_UTF8CHAR_PTR)RSTRING_PTR(pin), RSTRING_LEN(pin),
|
287
390
|
(CK_UTF8CHAR_PTR)RSTRING_PTR(label));
|
288
391
|
if (rv != CKR_OK) pkcs11_raise(rv);
|
@@ -298,7 +401,7 @@ pkcs11_C_InitPIN(VALUE self, VALUE session, VALUE pin)
|
|
298
401
|
|
299
402
|
StringValue(pin);
|
300
403
|
GetFunction(self, C_InitPIN, func);
|
301
|
-
rv
|
404
|
+
CallFunction(C_InitPIN, func, rv, NUM2HANDLE(session),
|
302
405
|
(CK_UTF8CHAR_PTR)RSTRING_PTR(pin), RSTRING_LEN(pin));
|
303
406
|
if (rv != CKR_OK) pkcs11_raise(rv);
|
304
407
|
|
@@ -306,14 +409,14 @@ pkcs11_C_InitPIN(VALUE self, VALUE session, VALUE pin)
|
|
306
409
|
}
|
307
410
|
|
308
411
|
static VALUE
|
309
|
-
pkcs11_C_OpenSession(VALUE self, VALUE slot_id, VALUE flags)
|
412
|
+
pkcs11_C_OpenSession(VALUE self, VALUE slot_id, VALUE flags)
|
310
413
|
{
|
311
414
|
CK_C_OpenSession func;
|
312
415
|
CK_RV rv;
|
313
416
|
CK_SESSION_HANDLE handle;
|
314
417
|
|
315
418
|
GetFunction(self, C_OpenSession, func);
|
316
|
-
rv
|
419
|
+
CallFunction(C_OpenSession, func, rv, NUM2HANDLE(slot_id), NUM2ULONG(flags), 0, 0, &handle);
|
317
420
|
if(rv != CKR_OK) pkcs11_raise(rv);
|
318
421
|
|
319
422
|
return HANDLE2NUM(handle);
|
@@ -327,7 +430,7 @@ pkcs11_C_Login(VALUE self, VALUE session, VALUE user_type, VALUE pin)
|
|
327
430
|
|
328
431
|
StringValue(pin);
|
329
432
|
GetFunction(self, C_Login, func);
|
330
|
-
rv
|
433
|
+
CallFunction(C_Login, func, rv, NUM2HANDLE(session), NUM2ULONG(user_type),
|
331
434
|
(CK_UTF8CHAR_PTR)RSTRING_PTR(pin), RSTRING_LEN(pin));
|
332
435
|
if(rv != CKR_OK) pkcs11_raise(rv);
|
333
436
|
|
@@ -341,7 +444,7 @@ pkcs11_C_Logout(VALUE self, VALUE session)
|
|
341
444
|
CK_RV rv;
|
342
445
|
|
343
446
|
GetFunction(self, C_Logout, func);
|
344
|
-
rv
|
447
|
+
CallFunction(C_Logout, func, rv, NUM2HANDLE(session));
|
345
448
|
if(rv != CKR_OK) pkcs11_raise(rv);
|
346
449
|
|
347
450
|
return self;
|
@@ -354,7 +457,7 @@ pkcs11_C_CloseSession(VALUE self, VALUE session)
|
|
354
457
|
CK_RV rv;
|
355
458
|
|
356
459
|
GetFunction(self, C_CloseSession, func);
|
357
|
-
rv
|
460
|
+
CallFunction(C_CloseSession, func, rv, NUM2HANDLE(session));
|
358
461
|
if(rv != CKR_OK) pkcs11_raise(rv);
|
359
462
|
|
360
463
|
return self;
|
@@ -367,7 +470,7 @@ pkcs11_C_CloseAllSessions(VALUE self, VALUE slot_id)
|
|
367
470
|
CK_RV rv;
|
368
471
|
|
369
472
|
GetFunction(self, C_CloseAllSessions, func);
|
370
|
-
rv
|
473
|
+
CallFunction(C_CloseAllSessions, func, rv, NUM2HANDLE(slot_id));
|
371
474
|
if(rv != CKR_OK) pkcs11_raise(rv);
|
372
475
|
|
373
476
|
return self;
|
@@ -382,7 +485,7 @@ pkcs11_C_GetSessionInfo(VALUE self, VALUE session)
|
|
382
485
|
|
383
486
|
info = pkcs11_new_struct(cCK_SESSION_INFO);
|
384
487
|
GetFunction(self, C_GetSessionInfo, func);
|
385
|
-
rv
|
488
|
+
CallFunction(C_GetSessionInfo, func, rv, NUM2HANDLE(session), DATA_PTR(info));
|
386
489
|
if (rv != CKR_OK) pkcs11_raise(rv);
|
387
490
|
|
388
491
|
return info;
|
@@ -397,11 +500,12 @@ pkcs11_C_GetOperationState(VALUE self, VALUE session)
|
|
397
500
|
CK_ULONG size;
|
398
501
|
|
399
502
|
GetFunction(self, C_GetOperationState, func);
|
400
|
-
rv
|
503
|
+
CallFunction(C_GetOperationState, func, rv, NUM2HANDLE(session), NULL_PTR, &size);
|
401
504
|
if (rv != CKR_OK) pkcs11_raise(rv);
|
402
505
|
state = rb_str_new(0, size);
|
403
|
-
rv
|
506
|
+
CallFunction(C_GetOperationState, func, rv, NUM2HANDLE(session), (CK_BYTE_PTR)RSTRING_PTR(state), &size);
|
404
507
|
if (rv != CKR_OK) pkcs11_raise(rv);
|
508
|
+
rb_str_set_len(state, size);
|
405
509
|
|
406
510
|
return state;
|
407
511
|
}
|
@@ -414,9 +518,9 @@ pkcs11_C_SetOperationState(VALUE self, VALUE session, VALUE state, VALUE enc_key
|
|
414
518
|
|
415
519
|
StringValue(state);
|
416
520
|
GetFunction(self, C_SetOperationState, func);
|
417
|
-
rv
|
521
|
+
CallFunction(C_SetOperationState, func, rv, NUM2HANDLE(session),
|
418
522
|
(CK_BYTE_PTR)RSTRING_PTR(state), RSTRING_LEN(state),
|
419
|
-
NUM2HANDLE(
|
523
|
+
NUM2HANDLE(enc_key), NUM2HANDLE(auth_key));
|
420
524
|
if (rv != CKR_OK) pkcs11_raise(rv);
|
421
525
|
|
422
526
|
return self;
|
@@ -431,7 +535,7 @@ pkcs11_C_SetPIN(VALUE self, VALUE session, VALUE old_pin, VALUE new_pin)
|
|
431
535
|
StringValue(old_pin);
|
432
536
|
StringValue(new_pin);
|
433
537
|
GetFunction(self, C_SetPIN, func);
|
434
|
-
rv
|
538
|
+
CallFunction(C_SetPIN, func, rv, NUM2HANDLE(session),
|
435
539
|
(CK_UTF8CHAR_PTR)RSTRING_PTR(old_pin), RSTRING_LEN(old_pin),
|
436
540
|
(CK_UTF8CHAR_PTR)RSTRING_PTR(new_pin), RSTRING_LEN(new_pin));
|
437
541
|
if(rv != CKR_OK) pkcs11_raise(rv);
|
@@ -468,10 +572,27 @@ pkcs11_C_CreateObject(VALUE self, VALUE session, VALUE template)
|
|
468
572
|
CK_RV rv;
|
469
573
|
CK_ATTRIBUTE *tmp;
|
470
574
|
CK_OBJECT_HANDLE handle;
|
471
|
-
|
575
|
+
|
472
576
|
tmp = pkcs11_attr_ary2buf(template);
|
473
577
|
GetFunction(self, C_CreateObject, func);
|
474
|
-
rv
|
578
|
+
CallFunction(C_CreateObject, func, rv, NUM2HANDLE(session), tmp, RARRAY_LEN(template), &handle);
|
579
|
+
free(tmp);
|
580
|
+
if(rv != CKR_OK) pkcs11_raise(rv);
|
581
|
+
|
582
|
+
return HANDLE2NUM(handle);
|
583
|
+
}
|
584
|
+
|
585
|
+
static VALUE
|
586
|
+
pkcs11_C_CopyObject(VALUE self, VALUE session, VALUE object, VALUE template)
|
587
|
+
{
|
588
|
+
CK_C_CopyObject func;
|
589
|
+
CK_RV rv;
|
590
|
+
CK_ATTRIBUTE *tmp;
|
591
|
+
CK_OBJECT_HANDLE handle;
|
592
|
+
|
593
|
+
tmp = pkcs11_attr_ary2buf(template);
|
594
|
+
GetFunction(self, C_CopyObject, func);
|
595
|
+
CallFunction(C_CopyObject, func, rv, NUM2HANDLE(session), NUM2HANDLE(object), tmp, RARRAY_LEN(template), &handle);
|
475
596
|
free(tmp);
|
476
597
|
if(rv != CKR_OK) pkcs11_raise(rv);
|
477
598
|
|
@@ -485,7 +606,7 @@ pkcs11_C_DestroyObject(VALUE self, VALUE session, VALUE handle)
|
|
485
606
|
CK_RV rv;
|
486
607
|
|
487
608
|
GetFunction(self, C_DestroyObject, func);
|
488
|
-
rv
|
609
|
+
CallFunction(C_DestroyObject, func, rv, NUM2HANDLE(session), NUM2HANDLE(handle));
|
489
610
|
if(rv != CKR_OK) pkcs11_raise(rv);
|
490
611
|
|
491
612
|
return self;
|
@@ -499,7 +620,7 @@ pkcs11_C_GetObjectSize(VALUE self, VALUE session, VALUE handle)
|
|
499
620
|
CK_ULONG size;
|
500
621
|
|
501
622
|
GetFunction(self, C_GetObjectSize, func);
|
502
|
-
rv
|
623
|
+
CallFunction(C_GetObjectSize, func, rv, NUM2HANDLE(session), NUM2HANDLE(handle), &size);
|
503
624
|
if(rv != CKR_OK) pkcs11_raise(rv);
|
504
625
|
|
505
626
|
return ULONG2NUM(size);
|
@@ -518,7 +639,7 @@ pkcs11_C_FindObjectsInit(VALUE self, VALUE session, VALUE template)
|
|
518
639
|
tmp_size = RARRAY_LEN(template);
|
519
640
|
}
|
520
641
|
GetFunction(self, C_FindObjectsInit, func);
|
521
|
-
rv
|
642
|
+
CallFunction(C_FindObjectsInit, func, rv, NUM2HANDLE(session), tmp, tmp_size);
|
522
643
|
free(tmp);
|
523
644
|
if(rv != CKR_OK) pkcs11_raise(rv);
|
524
645
|
|
@@ -532,7 +653,7 @@ pkcs11_C_FindObjectsFinal(VALUE self, VALUE session)
|
|
532
653
|
CK_RV rv;
|
533
654
|
|
534
655
|
GetFunction(self, C_FindObjectsFinal, func);
|
535
|
-
rv
|
656
|
+
CallFunction(C_FindObjectsFinal, func, rv, NUM2HANDLE(session));
|
536
657
|
if(rv != CKR_OK) pkcs11_raise(rv);
|
537
658
|
|
538
659
|
return self;
|
@@ -546,12 +667,12 @@ pkcs11_C_FindObjects(VALUE self, VALUE session, VALUE max_count)
|
|
546
667
|
CK_OBJECT_HANDLE_PTR handles;
|
547
668
|
CK_ULONG count = 0;
|
548
669
|
VALUE ary;
|
549
|
-
|
670
|
+
CK_ULONG i;
|
550
671
|
|
551
672
|
handles = (CK_OBJECT_HANDLE_PTR)
|
552
673
|
malloc(sizeof(CK_OBJECT_HANDLE)*NUM2ULONG(max_count));
|
553
674
|
GetFunction(self, C_FindObjects, func);
|
554
|
-
rv
|
675
|
+
CallFunction(C_FindObjects, func, rv, NUM2HANDLE(session), handles, NUM2ULONG(max_count), &count);
|
555
676
|
if(rv != CKR_OK){
|
556
677
|
free(handles);
|
557
678
|
pkcs11_raise(rv);
|
@@ -578,7 +699,7 @@ pkcs11_C_GetAttributeValue(VALUE self, VALUE session, VALUE handle, VALUE templa
|
|
578
699
|
tmp = pkcs11_attr_ary2buf(template);
|
579
700
|
template_size = RARRAY_LEN(template);
|
580
701
|
GetFunction(self, C_GetAttributeValue, func);
|
581
|
-
rv
|
702
|
+
CallFunction(C_GetAttributeValue, func, rv, NUM2HANDLE(session), NUM2HANDLE(handle), tmp, template_size);
|
582
703
|
if(rv != CKR_OK){
|
583
704
|
free(tmp);
|
584
705
|
pkcs11_raise(rv);
|
@@ -586,10 +707,10 @@ pkcs11_C_GetAttributeValue(VALUE self, VALUE session, VALUE handle, VALUE templa
|
|
586
707
|
|
587
708
|
for (i = 0; i < template_size; i++){
|
588
709
|
CK_ATTRIBUTE_PTR attr = tmp + i;
|
589
|
-
if (attr->ulValueLen != -1)
|
710
|
+
if (attr->ulValueLen != (CK_ULONG)-1)
|
590
711
|
attr->pValue = (CK_BYTE_PTR)malloc(attr->ulValueLen);
|
591
712
|
}
|
592
|
-
rv
|
713
|
+
CallFunction(C_GetAttributeValue, func, rv, NUM2HANDLE(session), NUM2HANDLE(handle), tmp, template_size);
|
593
714
|
if(rv != CKR_OK){
|
594
715
|
for (i = 0; i < template_size; i++){
|
595
716
|
CK_ATTRIBUTE_PTR attr = tmp + i;
|
@@ -601,7 +722,7 @@ pkcs11_C_GetAttributeValue(VALUE self, VALUE session, VALUE handle, VALUE templa
|
|
601
722
|
ary = rb_ary_new();
|
602
723
|
for (i = 0; i < template_size; i++){
|
603
724
|
CK_ATTRIBUTE_PTR attr = tmp + i;
|
604
|
-
if (attr->ulValueLen != -1){
|
725
|
+
if (attr->ulValueLen != (CK_ULONG)-1){
|
605
726
|
VALUE v = pkcs11_new_struct(cCK_ATTRIBUTE);
|
606
727
|
memcpy(DATA_PTR(v), attr, sizeof(CK_ATTRIBUTE));
|
607
728
|
rb_ary_push(ary, v);
|
@@ -623,7 +744,7 @@ pkcs11_C_SetAttributeValue(VALUE self, VALUE session, VALUE handle, VALUE templa
|
|
623
744
|
tmp = pkcs11_attr_ary2buf(template);
|
624
745
|
template_size = RARRAY_LEN(template);
|
625
746
|
GetFunction(self, C_SetAttributeValue, func);
|
626
|
-
rv
|
747
|
+
CallFunction(C_SetAttributeValue, func, rv, NUM2HANDLE(session), NUM2HANDLE(handle), tmp, template_size);
|
627
748
|
free(tmp);
|
628
749
|
if(rv != CKR_OK) pkcs11_raise(rv);
|
629
750
|
|
@@ -637,7 +758,7 @@ pkcs11_C_SeedRandom(VALUE self, VALUE session, VALUE seed)
|
|
637
758
|
CK_RV rv;
|
638
759
|
|
639
760
|
GetFunction(self, C_SeedRandom, func);
|
640
|
-
rv
|
761
|
+
CallFunction(C_SeedRandom, func, rv, NUM2HANDLE(session),
|
641
762
|
(CK_BYTE_PTR)RSTRING_PTR(seed), RSTRING_LEN(seed));
|
642
763
|
if(rv != CKR_OK) pkcs11_raise(rv);
|
643
764
|
|
@@ -653,7 +774,7 @@ pkcs11_C_GenerateRandom(VALUE self, VALUE session, VALUE size)
|
|
653
774
|
CK_RV rv;
|
654
775
|
|
655
776
|
GetFunction(self, C_GenerateRandom, func);
|
656
|
-
rv
|
777
|
+
CallFunction(C_GenerateRandom, func, rv, NUM2HANDLE(session), (CK_BYTE_PTR)RSTRING_PTR(buf), sz);
|
657
778
|
if(rv != CKR_OK) pkcs11_raise(rv);
|
658
779
|
|
659
780
|
return buf;
|
@@ -667,7 +788,7 @@ pkcs11_C_WaitForSlotEvent(VALUE self, VALUE flags)
|
|
667
788
|
CK_SLOT_ID slot_id;
|
668
789
|
|
669
790
|
GetFunction(self, C_WaitForSlotEvent, func);
|
670
|
-
rv
|
791
|
+
CallFunction(C_WaitForSlotEvent, func, rv, NUM2ULONG(flags), &slot_id, NULL_PTR);
|
671
792
|
if(rv == CKR_NO_EVENT) return Qnil;
|
672
793
|
if(rv != CKR_OK) pkcs11_raise(rv);
|
673
794
|
|
@@ -702,7 +823,8 @@ common_init(VALUE session, VALUE mechanism, VALUE key, init_func func)
|
|
702
823
|
if (!rb_obj_is_kind_of(mechanism, cCK_MECHANISM))
|
703
824
|
rb_raise(rb_eArgError, "2nd arg must be a PKCS11::CK_MECHANISM");
|
704
825
|
m = DATA_PTR(mechanism);
|
705
|
-
|
826
|
+
/* Use the function signature of any of the various C_*Init functions. */
|
827
|
+
CallFunction(C_EncryptInit, func, rv, NUM2HANDLE(session), m, NUM2HANDLE(key));
|
706
828
|
if(rv != CKR_OK) pkcs11_raise(rv);
|
707
829
|
|
708
830
|
return Qnil;
|
@@ -717,7 +839,7 @@ common_crypt_update(VALUE session, VALUE data, VALUE size, crypt_update_func fun
|
|
717
839
|
|
718
840
|
StringValue(data);
|
719
841
|
if (NIL_P(size)){
|
720
|
-
rv
|
842
|
+
CallFunction(C_EncryptUpdate, func, rv, NUM2HANDLE(session),
|
721
843
|
(CK_BYTE_PTR)RSTRING_PTR(data), RSTRING_LEN(data),
|
722
844
|
NULL_PTR, &sz);
|
723
845
|
if(rv != CKR_OK) pkcs11_raise(rv);
|
@@ -726,7 +848,7 @@ common_crypt_update(VALUE session, VALUE data, VALUE size, crypt_update_func fun
|
|
726
848
|
}
|
727
849
|
buf = rb_str_new(0, sz);
|
728
850
|
|
729
|
-
rv
|
851
|
+
CallFunction(C_EncryptUpdate, func, rv, NUM2HANDLE(session),
|
730
852
|
(CK_BYTE_PTR)RSTRING_PTR(data), RSTRING_LEN(data),
|
731
853
|
(CK_BYTE_PTR)RSTRING_PTR(buf), &sz);
|
732
854
|
if(rv != CKR_OK) pkcs11_raise(rv);
|
@@ -743,14 +865,14 @@ common_crypt_final(VALUE session, VALUE size, crypt_final_func func)
|
|
743
865
|
VALUE buf;
|
744
866
|
|
745
867
|
if (NIL_P(size)){
|
746
|
-
rv
|
868
|
+
CallFunction(C_EncryptFinal, func, rv, NUM2HANDLE(session), NULL_PTR, &sz);
|
747
869
|
if(rv != CKR_OK) pkcs11_raise(rv);
|
748
870
|
}else{
|
749
871
|
sz = NUM2ULONG(size);
|
750
872
|
}
|
751
873
|
buf = rb_str_new(0, sz);
|
752
874
|
|
753
|
-
rv
|
875
|
+
CallFunction(C_EncryptFinal, func, rv, NUM2HANDLE(session), (CK_BYTE_PTR)RSTRING_PTR(buf), &sz);
|
754
876
|
if(rv != CKR_OK) pkcs11_raise(rv);
|
755
877
|
rb_str_set_len(buf, sz);
|
756
878
|
|
@@ -763,7 +885,7 @@ common_sign_update(VALUE session, VALUE data, sign_update_func func)
|
|
763
885
|
CK_RV rv;
|
764
886
|
|
765
887
|
StringValue(data);
|
766
|
-
rv
|
888
|
+
CallFunction(C_SignUpdate, func, rv, NUM2HANDLE(session),
|
767
889
|
(CK_BYTE_PTR)RSTRING_PTR(data), RSTRING_LEN(data));
|
768
890
|
if(rv != CKR_OK) pkcs11_raise(rv);
|
769
891
|
|
@@ -777,7 +899,8 @@ common_verify(VALUE session, VALUE data, VALUE sig, verify_func func)
|
|
777
899
|
|
778
900
|
StringValue(data);
|
779
901
|
StringValue(sig);
|
780
|
-
|
902
|
+
/* Use the function signature of any of the various C_Verify* functions. */
|
903
|
+
CallFunction(C_Verify, func, rv, NUM2HANDLE(session),
|
781
904
|
(CK_BYTE_PTR)RSTRING_PTR(data), RSTRING_LEN(data),
|
782
905
|
(CK_BYTE_PTR)RSTRING_PTR(sig), RSTRING_LEN(sig));
|
783
906
|
if(rv != CKR_OK) pkcs11_raise(rv);
|
@@ -979,7 +1102,7 @@ pkcs11_C_DigestInit(VALUE self, VALUE session, VALUE mechanism)
|
|
979
1102
|
if (!rb_obj_is_kind_of(mechanism, cCK_MECHANISM))
|
980
1103
|
rb_raise(rb_eArgError, "2nd arg must be a PKCS11::CK_MECHANISM");
|
981
1104
|
m = DATA_PTR(mechanism);
|
982
|
-
rv
|
1105
|
+
CallFunction(C_DigestInit, func, rv, NUM2HANDLE(session), m);
|
983
1106
|
if(rv != CKR_OK) pkcs11_raise(rv);
|
984
1107
|
|
985
1108
|
return self;
|
@@ -1009,7 +1132,7 @@ pkcs11_C_DigestKey(VALUE self, VALUE session, VALUE handle)
|
|
1009
1132
|
CK_RV rv;
|
1010
1133
|
|
1011
1134
|
GetFunction(self, C_DigestKey, func);
|
1012
|
-
rv
|
1135
|
+
CallFunction(C_DigestKey, func, rv, NUM2HANDLE(session), NUM2HANDLE(handle));
|
1013
1136
|
if(rv != CKR_OK) pkcs11_raise(rv);
|
1014
1137
|
|
1015
1138
|
return self;
|
@@ -1068,7 +1191,7 @@ pkcs11_C_GenerateKey(VALUE self, VALUE session, VALUE mechanism, VALUE template)
|
|
1068
1191
|
rb_raise(rb_eArgError, "2nd arg must be a PKCS11::CK_MECHANISM");
|
1069
1192
|
m = DATA_PTR(mechanism);
|
1070
1193
|
tmp = pkcs11_attr_ary2buf(template);
|
1071
|
-
rv
|
1194
|
+
CallFunction(C_GenerateKey, func, rv, NUM2HANDLE(session), m, tmp, RARRAY_LEN(template), &handle);
|
1072
1195
|
free(tmp);
|
1073
1196
|
if(rv != CKR_OK) pkcs11_raise(rv);
|
1074
1197
|
|
@@ -1091,7 +1214,8 @@ pkcs11_C_GenerateKeyPair(VALUE self, VALUE session, VALUE mechanism, VALUE pubke
|
|
1091
1214
|
m = DATA_PTR(mechanism);
|
1092
1215
|
pubkey_tmp = pkcs11_attr_ary2buf(pubkey_template);
|
1093
1216
|
privkey_tmp = pkcs11_attr_ary2buf(privkey_template);
|
1094
|
-
|
1217
|
+
|
1218
|
+
CallFunction(C_GenerateKeyPair, func, rv, NUM2HANDLE(session), m,
|
1095
1219
|
pubkey_tmp, RARRAY_LEN(pubkey_template),
|
1096
1220
|
privkey_tmp, RARRAY_LEN(privkey_template),
|
1097
1221
|
&pubkey_handle, &privkey_handle);
|
@@ -1119,7 +1243,7 @@ pkcs11_C_WrapKey(VALUE self, VALUE session, VALUE mechanism, VALUE wrapping, VAL
|
|
1119
1243
|
rb_raise(rb_eArgError, "2nd arg must be a PKCS11::CK_MECHANISM");
|
1120
1244
|
m = DATA_PTR(mechanism);
|
1121
1245
|
if (NIL_P(size)){
|
1122
|
-
rv
|
1246
|
+
CallFunction(C_WrapKey, func, rv, NUM2HANDLE(session), m,
|
1123
1247
|
NUM2HANDLE(wrapping), NUM2HANDLE(wrapped),
|
1124
1248
|
(CK_BYTE_PTR)NULL_PTR, &sz);
|
1125
1249
|
if(rv != CKR_OK) pkcs11_raise(rv);
|
@@ -1128,7 +1252,7 @@ pkcs11_C_WrapKey(VALUE self, VALUE session, VALUE mechanism, VALUE wrapping, VAL
|
|
1128
1252
|
}
|
1129
1253
|
buf = rb_str_new(0, sz);
|
1130
1254
|
|
1131
|
-
rv
|
1255
|
+
CallFunction(C_WrapKey, func, rv, NUM2HANDLE(session), m,
|
1132
1256
|
NUM2HANDLE(wrapping), NUM2HANDLE(wrapped),
|
1133
1257
|
(CK_BYTE_PTR)RSTRING_PTR(buf), &sz);
|
1134
1258
|
if(rv != CKR_OK) pkcs11_raise(rv);
|
@@ -1151,7 +1275,7 @@ pkcs11_C_UnwrapKey(VALUE self, VALUE session, VALUE mechanism, VALUE wrapping, V
|
|
1151
1275
|
rb_raise(rb_eArgError, "2nd arg must be a PKCS11::CK_MECHANISM");
|
1152
1276
|
m = DATA_PTR(mechanism);
|
1153
1277
|
tmp = pkcs11_attr_ary2buf(template);
|
1154
|
-
rv
|
1278
|
+
CallFunction(C_UnwrapKey, func, rv, NUM2HANDLE(session), m, NUM2HANDLE(wrapping),
|
1155
1279
|
(CK_BYTE_PTR)RSTRING_PTR(wrapped), RSTRING_LEN(wrapped),
|
1156
1280
|
tmp, RARRAY_LEN(template), &h);
|
1157
1281
|
free(tmp);
|
@@ -1174,7 +1298,7 @@ pkcs11_C_DeriveKey(VALUE self, VALUE session, VALUE mechanism, VALUE base, VALUE
|
|
1174
1298
|
rb_raise(rb_eArgError, "2nd arg must be a PKCS11::CK_MECHANISM");
|
1175
1299
|
m = DATA_PTR(mechanism);
|
1176
1300
|
tmp = pkcs11_attr_ary2buf(template);
|
1177
|
-
rv
|
1301
|
+
CallFunction(C_DeriveKey, func, rv, NUM2HANDLE(session), m, NUM2HANDLE(base),
|
1178
1302
|
tmp, RARRAY_LEN(template), &h);
|
1179
1303
|
free(tmp);
|
1180
1304
|
if(rv != CKR_OK) pkcs11_raise(rv);
|
@@ -1241,6 +1365,10 @@ ck_attr_initialize(int argc, VALUE *argv, VALUE self)
|
|
1241
1365
|
return self;
|
1242
1366
|
}
|
1243
1367
|
|
1368
|
+
/* rb_define_method(cCK_ATTRIBUTE, "type", ck_attr_type, 0); */
|
1369
|
+
/*
|
1370
|
+
* @return [Integer] attribute type PKCS11::CKA_*
|
1371
|
+
*/
|
1244
1372
|
static VALUE
|
1245
1373
|
ck_attr_type(VALUE self)
|
1246
1374
|
{
|
@@ -1249,6 +1377,11 @@ ck_attr_type(VALUE self)
|
|
1249
1377
|
return ULONG2NUM(attr->type);
|
1250
1378
|
}
|
1251
1379
|
|
1380
|
+
/* rb_define_method(cCK_ATTRIBUTE, "value", ck_attr_value, 0); */
|
1381
|
+
/*
|
1382
|
+
* @return [String, Integer, Boolean] attribute value
|
1383
|
+
* @see PKCS11::Object#[]
|
1384
|
+
*/
|
1252
1385
|
static VALUE
|
1253
1386
|
ck_attr_value(VALUE self)
|
1254
1387
|
{
|
@@ -1296,6 +1429,11 @@ ck_attr_value(VALUE self)
|
|
1296
1429
|
case CKA_PIXEL_X:
|
1297
1430
|
case CKA_PIXEL_Y:
|
1298
1431
|
case CKA_RESOLUTION:
|
1432
|
+
case CKA_VALUE_LEN:
|
1433
|
+
case CKA_MODULUS_BITS:
|
1434
|
+
case CKA_PRIME_BITS:
|
1435
|
+
case CKA_SUBPRIME_BITS:
|
1436
|
+
case CKA_VALUE_BITS:
|
1299
1437
|
if (attr->ulValueLen == sizeof(CK_ULONG))
|
1300
1438
|
return ULONG2NUM(*(CK_ULONG_PTR)(attr->pValue));
|
1301
1439
|
break;
|
@@ -1340,25 +1478,79 @@ set_ulong(VALUE obj, VALUE value, off_t offset)
|
|
1340
1478
|
}
|
1341
1479
|
|
1342
1480
|
static VALUE
|
1343
|
-
|
1481
|
+
get_byte(VALUE obj, off_t offset)
|
1344
1482
|
{
|
1345
1483
|
char *ptr = (char*)DATA_PTR(obj);
|
1346
|
-
|
1347
|
-
char buf[64];
|
1348
|
-
v = (CK_VERSION_PTR)(ptr+offset);
|
1349
|
-
snprintf(buf, sizeof(buf), "%d.%d", v->major, v->minor);
|
1350
|
-
return rb_str_new2(buf);
|
1484
|
+
return ULONG2NUM(*(CK_BYTE_PTR)(ptr+offset));
|
1351
1485
|
}
|
1352
1486
|
|
1353
1487
|
static VALUE
|
1354
|
-
|
1488
|
+
set_byte(VALUE obj, VALUE value, off_t offset)
|
1355
1489
|
{
|
1356
|
-
|
1357
|
-
|
1490
|
+
char *ptr = (char*)DATA_PTR(obj);
|
1491
|
+
*(CK_BYTE_PTR)(ptr+offset) = NUM2ULONG(value);
|
1492
|
+
return value;
|
1493
|
+
}
|
1494
|
+
|
1495
|
+
static VALUE
|
1496
|
+
get_ulong_ptr(VALUE obj, off_t offset)
|
1497
|
+
{
|
1498
|
+
char *ptr = (char*)DATA_PTR(obj);
|
1499
|
+
CK_ULONG_PTR p = *(CK_ULONG_PTR *)(ptr+offset);
|
1500
|
+
if (!p) return Qnil;
|
1501
|
+
return ULONG2NUM(*p);
|
1502
|
+
}
|
1503
|
+
|
1504
|
+
static VALUE
|
1505
|
+
set_ulong_ptr(VALUE obj, VALUE value, const char *name, off_t offset)
|
1506
|
+
{
|
1507
|
+
CK_ULONG_PTR *ptr = (CK_ULONG_PTR *)((char*)DATA_PTR(obj) + offset);
|
1508
|
+
if (NIL_P(value)){
|
1509
|
+
rb_iv_set(obj, name, value);
|
1510
|
+
*ptr = NULL_PTR;
|
1511
|
+
return value;
|
1512
|
+
}
|
1513
|
+
VALUE new_obj = Data_Make_Struct(rb_cInteger, CK_ULONG, 0, free, *ptr);
|
1514
|
+
rb_iv_set(obj, name, new_obj);
|
1515
|
+
**ptr = NUM2ULONG(value);
|
1516
|
+
return value;
|
1517
|
+
}
|
1518
|
+
|
1519
|
+
static VALUE
|
1520
|
+
get_handle(VALUE obj, off_t offset)
|
1521
|
+
{
|
1522
|
+
char *ptr = (char*)DATA_PTR(obj);
|
1523
|
+
return HANDLE2NUM(*(CK_OBJECT_HANDLE_PTR)(ptr+offset));
|
1358
1524
|
}
|
1359
1525
|
|
1360
1526
|
static VALUE
|
1361
|
-
|
1527
|
+
set_handle(VALUE obj, VALUE value, off_t offset)
|
1528
|
+
{
|
1529
|
+
char *ptr = (char*)DATA_PTR(obj);
|
1530
|
+
*(CK_OBJECT_HANDLE_PTR)(ptr+offset) = NUM2HANDLE(value);
|
1531
|
+
return value;
|
1532
|
+
}
|
1533
|
+
|
1534
|
+
static VALUE
|
1535
|
+
get_bool(VALUE obj, off_t offset)
|
1536
|
+
{
|
1537
|
+
char *ptr = (char*)DATA_PTR(obj);
|
1538
|
+
if(*(CK_BBOOL*)(ptr+offset)) return Qtrue;
|
1539
|
+
else return Qfalse;
|
1540
|
+
}
|
1541
|
+
|
1542
|
+
static VALUE
|
1543
|
+
set_bool(VALUE obj, VALUE value, off_t offset)
|
1544
|
+
{
|
1545
|
+
char *ptr = (char*)DATA_PTR(obj);
|
1546
|
+
if(value == Qfalse) *(CK_BBOOL*)(ptr+offset) = 0;
|
1547
|
+
else if(value == Qtrue) *(CK_BBOOL*)(ptr+offset) = 1;
|
1548
|
+
else rb_raise(rb_eArgError, "arg must be true or false");
|
1549
|
+
return value;
|
1550
|
+
}
|
1551
|
+
|
1552
|
+
static VALUE
|
1553
|
+
get_string_ptr(VALUE obj, const char *name, off_t offset)
|
1362
1554
|
{
|
1363
1555
|
char *ptr = (char*)DATA_PTR(obj);
|
1364
1556
|
char *p = *(char**)(ptr+offset);
|
@@ -1367,17 +1559,91 @@ get_string_ptr(VALUE obj, char *name, off_t offset)
|
|
1367
1559
|
}
|
1368
1560
|
|
1369
1561
|
static VALUE
|
1370
|
-
set_string_ptr(VALUE obj, VALUE value, char *name, off_t offset)
|
1562
|
+
set_string_ptr(VALUE obj, VALUE value, const char *name, off_t offset)
|
1371
1563
|
{
|
1372
1564
|
char *ptr = (char*)DATA_PTR(obj);
|
1565
|
+
if (NIL_P(value)){
|
1566
|
+
rb_iv_set(obj, name, value);
|
1567
|
+
*(CK_VOID_PTR*)(ptr+offset) = NULL_PTR;
|
1568
|
+
return value;
|
1569
|
+
}
|
1570
|
+
StringValue(value);
|
1571
|
+
value = rb_obj_freeze(rb_str_dup(value));
|
1373
1572
|
rb_iv_set(obj, name, value);
|
1573
|
+
*(CK_VOID_PTR*)(ptr+offset) = RSTRING_PTR(value);
|
1574
|
+
return value;
|
1575
|
+
}
|
1576
|
+
|
1577
|
+
static VALUE
|
1578
|
+
get_string_ptr_len(VALUE obj, const char *name, off_t offset, off_t offset_len)
|
1579
|
+
{
|
1580
|
+
unsigned long l;
|
1581
|
+
char *ptr = (char*)DATA_PTR(obj);
|
1582
|
+
char *p = *(char**)(ptr+offset);
|
1583
|
+
if (!p) return Qnil;
|
1584
|
+
l = *(unsigned long*)(ptr+offset_len);
|
1585
|
+
return rb_str_new(p, l);
|
1586
|
+
}
|
1587
|
+
|
1588
|
+
static VALUE
|
1589
|
+
set_string_ptr_len(VALUE obj, VALUE value, const char *name, off_t offset, off_t offset_len)
|
1590
|
+
{
|
1591
|
+
char *ptr = (char*)DATA_PTR(obj);
|
1374
1592
|
if (NIL_P(value)){
|
1593
|
+
rb_iv_set(obj, name, value);
|
1375
1594
|
*(CK_VOID_PTR*)(ptr+offset) = NULL_PTR;
|
1595
|
+
*(unsigned long*)(ptr+offset_len) = 0;
|
1376
1596
|
return value;
|
1377
1597
|
}
|
1378
1598
|
StringValue(value);
|
1379
1599
|
value = rb_obj_freeze(rb_str_dup(value));
|
1600
|
+
rb_iv_set(obj, name, value);
|
1380
1601
|
*(CK_VOID_PTR*)(ptr+offset) = RSTRING_PTR(value);
|
1602
|
+
*(unsigned long*)(ptr+offset_len) = RSTRING_LEN(value);
|
1603
|
+
return value;
|
1604
|
+
}
|
1605
|
+
|
1606
|
+
static VALUE
|
1607
|
+
get_struct_inline(VALUE obj, VALUE klass, const char *name, off_t offset)
|
1608
|
+
{
|
1609
|
+
char *ptr = (char*)DATA_PTR(obj) + offset;
|
1610
|
+
VALUE inline_obj = Data_Wrap_Struct(klass, 0, 0, ptr);
|
1611
|
+
rb_iv_set(inline_obj, name, obj);
|
1612
|
+
return inline_obj;
|
1613
|
+
}
|
1614
|
+
|
1615
|
+
static VALUE
|
1616
|
+
set_struct_inline(VALUE obj, VALUE klass, const char *struct_name, VALUE value, const char *name, off_t offset, int sizeofstruct)
|
1617
|
+
{
|
1618
|
+
char *ptr = (char*)DATA_PTR(obj) + offset;
|
1619
|
+
if (!rb_obj_is_kind_of(value, klass))
|
1620
|
+
rb_raise(rb_eArgError, "arg must be a PKCS11::%s", struct_name);
|
1621
|
+
memcpy(ptr, DATA_PTR(value), sizeofstruct);
|
1622
|
+
return value;
|
1623
|
+
}
|
1624
|
+
|
1625
|
+
static VALUE
|
1626
|
+
get_struct_ptr(VALUE obj, VALUE klass, const char *name, off_t offset)
|
1627
|
+
{
|
1628
|
+
char *ptr = (char*)DATA_PTR(obj);
|
1629
|
+
char *p = *(char**)(ptr+offset);
|
1630
|
+
if (!p) return Qnil;
|
1631
|
+
return rb_iv_get(obj, name);
|
1632
|
+
}
|
1633
|
+
|
1634
|
+
static VALUE
|
1635
|
+
set_struct_ptr(VALUE obj, VALUE klass, const char *struct_name, VALUE value, const char *name, off_t offset)
|
1636
|
+
{
|
1637
|
+
char *ptr = (char*)DATA_PTR(obj) + offset;
|
1638
|
+
if (NIL_P(value)){
|
1639
|
+
rb_iv_set(obj, name, value);
|
1640
|
+
*(CK_VOID_PTR*)ptr = NULL_PTR;
|
1641
|
+
return value;
|
1642
|
+
}
|
1643
|
+
if (!rb_obj_is_kind_of(value, klass))
|
1644
|
+
rb_raise(rb_eArgError, "arg must be a PKCS11::%s", struct_name);
|
1645
|
+
*(CK_VOID_PTR*)ptr = DATA_PTR(value);
|
1646
|
+
rb_iv_set(obj, name, value);
|
1381
1647
|
return value;
|
1382
1648
|
}
|
1383
1649
|
|
@@ -1388,10 +1654,20 @@ set_string_ptr(VALUE obj, VALUE value, char *name, off_t offset)
|
|
1388
1654
|
static VALUE s##_s_alloc(VALUE self){ \
|
1389
1655
|
s *info; \
|
1390
1656
|
VALUE obj = Data_Make_Struct(self, s, 0, -1, info); \
|
1391
|
-
memset(info, 0, sizeof(s)); \
|
1392
1657
|
return obj; \
|
1658
|
+
} \
|
1659
|
+
static VALUE c##s##_to_s(VALUE self){ \
|
1660
|
+
return rb_str_new(DATA_PTR(self), sizeof(s)); \
|
1661
|
+
} \
|
1662
|
+
static VALUE c##s##_members(VALUE self){ \
|
1663
|
+
return a##s##_members; \
|
1393
1664
|
}
|
1394
1665
|
|
1666
|
+
#define PKCS11_IMPLEMENT_STRUCT_WITH_ALLOCATOR(s) \
|
1667
|
+
static VALUE c##s;\
|
1668
|
+
static VALUE a##s##_members;\
|
1669
|
+
PKCS11_IMPLEMENT_ALLOCATOR(s);
|
1670
|
+
|
1395
1671
|
#define PKCS11_IMPLEMENT_STRING_ACCESSOR(s, f) \
|
1396
1672
|
static VALUE c##s##_get_##f(VALUE o){ \
|
1397
1673
|
return get_string(o, OFFSET_OF(s, f), SIZE_OF(s, f)); \
|
@@ -1408,12 +1684,36 @@ static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
|
|
1408
1684
|
return set_ulong(o, v, OFFSET_OF(s, f)); \
|
1409
1685
|
}
|
1410
1686
|
|
1411
|
-
#define
|
1687
|
+
#define PKCS11_IMPLEMENT_BYTE_ACCESSOR(s, f) \
|
1412
1688
|
static VALUE c##s##_get_##f(VALUE o){ \
|
1413
|
-
return
|
1689
|
+
return get_byte(o, OFFSET_OF(s, f)); \
|
1414
1690
|
} \
|
1415
1691
|
static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
|
1416
|
-
return
|
1692
|
+
return set_byte(o, v, OFFSET_OF(s, f)); \
|
1693
|
+
}
|
1694
|
+
|
1695
|
+
#define PKCS11_IMPLEMENT_ULONG_PTR_ACCESSOR(s, f) \
|
1696
|
+
static VALUE c##s##_get_##f(VALUE o){ \
|
1697
|
+
return get_ulong_ptr(o, OFFSET_OF(s, f)); \
|
1698
|
+
} \
|
1699
|
+
static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
|
1700
|
+
return set_ulong_ptr(o, v, #f, OFFSET_OF(s, f)); \
|
1701
|
+
}
|
1702
|
+
|
1703
|
+
#define PKCS11_IMPLEMENT_HANDLE_ACCESSOR(s, f) \
|
1704
|
+
static VALUE c##s##_get_##f(VALUE o){ \
|
1705
|
+
return get_handle(o, OFFSET_OF(s, f)); \
|
1706
|
+
} \
|
1707
|
+
static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
|
1708
|
+
return set_handle(o, v, OFFSET_OF(s, f)); \
|
1709
|
+
}
|
1710
|
+
|
1711
|
+
#define PKCS11_IMPLEMENT_BOOL_ACCESSOR(s, f) \
|
1712
|
+
static VALUE c##s##_get_##f(VALUE o){ \
|
1713
|
+
return get_bool(o, OFFSET_OF(s, f)); \
|
1714
|
+
} \
|
1715
|
+
static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
|
1716
|
+
return set_bool(o, v, OFFSET_OF(s, f)); \
|
1417
1717
|
}
|
1418
1718
|
|
1419
1719
|
#define PKCS11_IMPLEMENT_STRING_PTR_ACCESSOR(s, f) \
|
@@ -1424,61 +1724,43 @@ static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
|
|
1424
1724
|
return set_string_ptr(o, v, #f, OFFSET_OF(s, f)); \
|
1425
1725
|
}
|
1426
1726
|
|
1727
|
+
#define PKCS11_IMPLEMENT_STRING_PTR_LEN_ACCESSOR(s, f, l) \
|
1728
|
+
static VALUE c##s##_get_##f(VALUE o){ \
|
1729
|
+
return get_string_ptr_len(o, #f, OFFSET_OF(s, f), OFFSET_OF(s, l)); \
|
1730
|
+
} \
|
1731
|
+
static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
|
1732
|
+
return set_string_ptr_len(o, v, #f, OFFSET_OF(s, f), OFFSET_OF(s, l)); \
|
1733
|
+
}
|
1734
|
+
|
1735
|
+
#define PKCS11_IMPLEMENT_STRUCT_ACCESSOR(s, k, f) \
|
1736
|
+
static VALUE c##s##_get_##f(VALUE o){ \
|
1737
|
+
return get_struct_inline(o, c##k, #f, OFFSET_OF(s, f)); \
|
1738
|
+
} \
|
1739
|
+
static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
|
1740
|
+
return set_struct_inline(o, c##k, #k, v, #f, OFFSET_OF(s, f), sizeof(k)); \
|
1741
|
+
}
|
1742
|
+
|
1743
|
+
#define PKCS11_IMPLEMENT_STRUCT_PTR_ACCESSOR(s, k, f) \
|
1744
|
+
static VALUE c##s##_get_##f(VALUE o){ \
|
1745
|
+
return get_struct_ptr(o, c##k, #f, OFFSET_OF(s, f)); \
|
1746
|
+
} \
|
1747
|
+
static VALUE c##s##_set_##f(VALUE o, VALUE v){ \
|
1748
|
+
return set_struct_ptr(o, c##k, #k, v, #f, OFFSET_OF(s, f)); \
|
1749
|
+
}
|
1750
|
+
|
1427
1751
|
///////////////////////////////////////
|
1428
1752
|
|
1429
|
-
|
1430
|
-
PKCS11_IMPLEMENT_ULONG_ACCESSOR(CK_C_INITIALIZE_ARGS, flags)
|
1431
|
-
PKCS11_IMPLEMENT_STRING_PTR_ACCESSOR(CK_C_INITIALIZE_ARGS, pReserved)
|
1432
|
-
|
1433
|
-
PKCS11_IMPLEMENT_ALLOCATOR(CK_INFO)
|
1434
|
-
PKCS11_IMPLEMENT_VERSION_ACCESSOR(CK_INFO, cryptokiVersion)
|
1435
|
-
PKCS11_IMPLEMENT_STRING_ACCESSOR(CK_INFO, manufacturerID)
|
1436
|
-
PKCS11_IMPLEMENT_ULONG_ACCESSOR(CK_INFO, flags)
|
1437
|
-
PKCS11_IMPLEMENT_STRING_ACCESSOR(CK_INFO, libraryDescription)
|
1438
|
-
PKCS11_IMPLEMENT_VERSION_ACCESSOR(CK_INFO, libraryVersion)
|
1439
|
-
|
1440
|
-
PKCS11_IMPLEMENT_ALLOCATOR(CK_SLOT_INFO)
|
1441
|
-
PKCS11_IMPLEMENT_STRING_ACCESSOR(CK_SLOT_INFO, slotDescription)
|
1442
|
-
PKCS11_IMPLEMENT_STRING_ACCESSOR(CK_SLOT_INFO, manufacturerID)
|
1443
|
-
PKCS11_IMPLEMENT_ULONG_ACCESSOR(CK_SLOT_INFO, flags)
|
1444
|
-
PKCS11_IMPLEMENT_VERSION_ACCESSOR(CK_SLOT_INFO, hardwareVersion)
|
1445
|
-
PKCS11_IMPLEMENT_VERSION_ACCESSOR(CK_SLOT_INFO, firmwareVersion)
|
1446
|
-
|
1447
|
-
PKCS11_IMPLEMENT_ALLOCATOR(CK_TOKEN_INFO);
|
1448
|
-
PKCS11_IMPLEMENT_STRING_ACCESSOR(CK_TOKEN_INFO, label);
|
1449
|
-
PKCS11_IMPLEMENT_STRING_ACCESSOR(CK_TOKEN_INFO, manufacturerID);
|
1450
|
-
PKCS11_IMPLEMENT_STRING_ACCESSOR(CK_TOKEN_INFO, model);
|
1451
|
-
PKCS11_IMPLEMENT_STRING_ACCESSOR(CK_TOKEN_INFO, serialNumber);
|
1452
|
-
PKCS11_IMPLEMENT_ULONG_ACCESSOR(CK_TOKEN_INFO, flags);
|
1453
|
-
PKCS11_IMPLEMENT_ULONG_ACCESSOR(CK_TOKEN_INFO, ulMaxSessionCount);
|
1454
|
-
PKCS11_IMPLEMENT_ULONG_ACCESSOR(CK_TOKEN_INFO, ulSessionCount);
|
1455
|
-
PKCS11_IMPLEMENT_ULONG_ACCESSOR(CK_TOKEN_INFO, ulMaxRwSessionCount);
|
1456
|
-
PKCS11_IMPLEMENT_ULONG_ACCESSOR(CK_TOKEN_INFO, ulRwSessionCount);
|
1457
|
-
PKCS11_IMPLEMENT_ULONG_ACCESSOR(CK_TOKEN_INFO, ulMaxPinLen);
|
1458
|
-
PKCS11_IMPLEMENT_ULONG_ACCESSOR(CK_TOKEN_INFO, ulMinPinLen);
|
1459
|
-
PKCS11_IMPLEMENT_ULONG_ACCESSOR(CK_TOKEN_INFO, ulTotalPublicMemory);
|
1460
|
-
PKCS11_IMPLEMENT_ULONG_ACCESSOR(CK_TOKEN_INFO, ulFreePublicMemory);
|
1461
|
-
PKCS11_IMPLEMENT_ULONG_ACCESSOR(CK_TOKEN_INFO, ulTotalPrivateMemory);
|
1462
|
-
PKCS11_IMPLEMENT_ULONG_ACCESSOR(CK_TOKEN_INFO, ulFreePrivateMemory);
|
1463
|
-
PKCS11_IMPLEMENT_VERSION_ACCESSOR(CK_TOKEN_INFO, hardwareVersion);
|
1464
|
-
PKCS11_IMPLEMENT_VERSION_ACCESSOR(CK_TOKEN_INFO, firmwareVersion);
|
1465
|
-
PKCS11_IMPLEMENT_STRING_ACCESSOR(CK_TOKEN_INFO, utcTime);
|
1466
|
-
|
1467
|
-
PKCS11_IMPLEMENT_ALLOCATOR(CK_MECHANISM_INFO);
|
1468
|
-
PKCS11_IMPLEMENT_ULONG_ACCESSOR(CK_MECHANISM_INFO, ulMinKeySize);
|
1469
|
-
PKCS11_IMPLEMENT_ULONG_ACCESSOR(CK_MECHANISM_INFO, ulMaxKeySize);
|
1470
|
-
PKCS11_IMPLEMENT_ULONG_ACCESSOR(CK_MECHANISM_INFO, flags);
|
1471
|
-
|
1472
|
-
PKCS11_IMPLEMENT_ALLOCATOR(CK_SESSION_INFO);
|
1473
|
-
PKCS11_IMPLEMENT_ULONG_ACCESSOR(CK_SESSION_INFO, slotID);
|
1474
|
-
PKCS11_IMPLEMENT_ULONG_ACCESSOR(CK_SESSION_INFO, state);
|
1475
|
-
PKCS11_IMPLEMENT_ULONG_ACCESSOR(CK_SESSION_INFO, flags);
|
1476
|
-
PKCS11_IMPLEMENT_ULONG_ACCESSOR(CK_SESSION_INFO, ulDeviceError);
|
1753
|
+
#include "pk11_struct_impl.inc"
|
1477
1754
|
|
1478
1755
|
///////////////////////////////////////
|
1479
1756
|
|
1480
1757
|
PKCS11_IMPLEMENT_ALLOCATOR(CK_MECHANISM)
|
1481
1758
|
|
1759
|
+
/*
|
1760
|
+
* Spezifies a particularly crypto mechanism.
|
1761
|
+
* @param [Integer, nil] mechanism The mechanism to use (PKCS11::CKM_*)
|
1762
|
+
* @param [String, Integer, PKCS11::CStruct, nil] pParameter optional parameter to the mechanism
|
1763
|
+
*/
|
1482
1764
|
static VALUE
|
1483
1765
|
cCK_MECHANISM_initialize(int argc, VALUE *argv, VALUE self)
|
1484
1766
|
{
|
@@ -1491,8 +1773,13 @@ cCK_MECHANISM_initialize(int argc, VALUE *argv, VALUE self)
|
|
1491
1773
|
return self;
|
1492
1774
|
}
|
1493
1775
|
|
1776
|
+
/* rb_define_method(cCK_MECHANISM, "mechanism", cCK_MECHANISM_get_mechanism, 0); */
|
1777
|
+
/* rb_define_method(cCK_MECHANISM, "mechanism=", cCK_MECHANISM_set_mechanism, 0); */
|
1494
1778
|
PKCS11_IMPLEMENT_ULONG_ACCESSOR(CK_MECHANISM, mechanism);
|
1495
1779
|
|
1780
|
+
/* rb_define_method(cCK_MECHANISM, "pParameter", cCK_MECHANISM_get_pParameter, 0); */
|
1781
|
+
/* rb_define_method(cCK_MECHANISM, "pParameter=", cCK_MECHANISM_set_pParameter, 0); */
|
1782
|
+
/* @see PKCS11::CK_MECHANISM#initialize */
|
1496
1783
|
static VALUE
|
1497
1784
|
cCK_MECHANISM_get_pParameter(VALUE self)
|
1498
1785
|
{
|
@@ -1505,17 +1792,32 @@ static VALUE
|
|
1505
1792
|
cCK_MECHANISM_set_pParameter(VALUE self, VALUE value)
|
1506
1793
|
{
|
1507
1794
|
CK_MECHANISM_PTR m = DATA_PTR(self);
|
1795
|
+
CK_ULONG ulong_val;
|
1508
1796
|
|
1509
|
-
|
1797
|
+
switch(TYPE(value)){
|
1798
|
+
case T_NIL:
|
1510
1799
|
m->pParameter = NULL_PTR;
|
1511
1800
|
m->ulParameterLen = 0;
|
1512
|
-
|
1513
|
-
|
1514
|
-
StringValue(value);
|
1801
|
+
break;
|
1802
|
+
case T_STRING:
|
1515
1803
|
value = rb_obj_freeze(rb_str_dup(value));
|
1516
1804
|
m->pParameter = RSTRING_PTR(value);
|
1517
1805
|
m->ulParameterLen = RSTRING_LEN(value);
|
1806
|
+
break;
|
1807
|
+
case T_FIXNUM:
|
1808
|
+
ulong_val = NUM2ULONG(value);
|
1809
|
+
value = rb_obj_freeze(rb_str_new((char*)&ulong_val, sizeof(ulong_val)));
|
1810
|
+
m->pParameter = RSTRING_PTR(value);
|
1811
|
+
m->ulParameterLen = RSTRING_LEN(value);
|
1812
|
+
break;
|
1813
|
+
case T_DATA:
|
1814
|
+
m->pParameter = DATA_PTR(value);
|
1815
|
+
m->ulParameterLen = NUM2LONG(rb_const_get(rb_funcall(value, rb_intern("class"), 0), rb_intern("SIZEOF_STRUCT")));
|
1816
|
+
break;
|
1817
|
+
default:
|
1818
|
+
rb_raise(rb_eArgError, "invalid argument");
|
1518
1819
|
}
|
1820
|
+
/* don't GC the value as long as this object is active */
|
1519
1821
|
rb_iv_set(self, "pParameter", value);
|
1520
1822
|
|
1521
1823
|
return value;
|
@@ -1528,16 +1830,22 @@ cCK_MECHANISM_set_pParameter(VALUE self, VALUE value)
|
|
1528
1830
|
|
1529
1831
|
#define PKCS11_DEFINE_STRUCT(s) \
|
1530
1832
|
do { \
|
1531
|
-
c##s = rb_define_class_under(mPKCS11, #s,
|
1833
|
+
c##s = rb_define_class_under(mPKCS11, #s, cCStruct); \
|
1834
|
+
a##s##_members = rb_ary_new(); \
|
1532
1835
|
rb_define_alloc_func(c##s, s##_s_alloc); \
|
1836
|
+
rb_define_const(c##s, "SIZEOF_STRUCT", ULONG2NUM(sizeof(s))); \
|
1837
|
+
rb_define_method(c##s, "to_s", c##s##_to_s, 0); \
|
1838
|
+
rb_define_method(c##s, "members", c##s##_members, 0); \
|
1839
|
+
rb_iv_set(c##s, "members", a##s##_members); \
|
1533
1840
|
} while(0)
|
1534
1841
|
|
1535
1842
|
#define PKCS11_DEFINE_MEMBER(s, f) \
|
1536
1843
|
do { \
|
1537
1844
|
rb_define_method(c##s, #f, c##s##_get_##f, 0); \
|
1538
1845
|
rb_define_method(c##s, #f "=", c##s##_set_##f, 1); \
|
1846
|
+
rb_ary_push(a##s##_members, rb_str_new2(#f)); \
|
1539
1847
|
} while(0)
|
1540
|
-
|
1848
|
+
|
1541
1849
|
void
|
1542
1850
|
Init_pkcs11_ext()
|
1543
1851
|
{
|
@@ -1547,17 +1855,23 @@ Init_pkcs11_ext()
|
|
1547
1855
|
|
1548
1856
|
/* Document-method: PKCS11.open
|
1549
1857
|
*
|
1550
|
-
* Alias function for PKCS11::Library
|
1858
|
+
* Alias function for {PKCS11::Library#initialize}
|
1551
1859
|
*/
|
1552
1860
|
rb_define_module_function(mPKCS11, "open", pkcs11_library_new, -1);
|
1553
|
-
|
1861
|
+
|
1554
1862
|
/* Library version */
|
1555
1863
|
rb_define_const( mPKCS11, "VERSION", rb_str_new2(VERSION) );
|
1556
|
-
|
1864
|
+
|
1865
|
+
/* Document-class: PKCS11::Error
|
1866
|
+
*
|
1867
|
+
* Base class for all Cryptoki exceptions (CKR_*) */
|
1557
1868
|
ePKCS11Error = rb_define_class_under(mPKCS11, "Error", rb_eStandardError);
|
1558
1869
|
rb_define_alloc_func(cPKCS11, pkcs11_s_alloc);
|
1559
1870
|
rb_define_method(cPKCS11, "initialize", pkcs11_initialize, -1);
|
1560
1871
|
|
1872
|
+
PKCS11_DEFINE_METHOD(load_library, 1);
|
1873
|
+
PKCS11_DEFINE_METHOD(C_GetFunctionList, 0);
|
1874
|
+
PKCS11_DEFINE_METHOD(C_Initialize, -1);
|
1561
1875
|
PKCS11_DEFINE_METHOD(C_GetInfo, 0);
|
1562
1876
|
PKCS11_DEFINE_METHOD(C_GetSlotList, 1);
|
1563
1877
|
PKCS11_DEFINE_METHOD(C_GetSlotInfo, 1);
|
@@ -1578,6 +1892,7 @@ Init_pkcs11_ext()
|
|
1578
1892
|
PKCS11_DEFINE_METHOD(C_SetPIN, 3);
|
1579
1893
|
|
1580
1894
|
PKCS11_DEFINE_METHOD(C_CreateObject, 2);
|
1895
|
+
PKCS11_DEFINE_METHOD(C_CopyObject, 3);
|
1581
1896
|
PKCS11_DEFINE_METHOD(C_DestroyObject, 2);
|
1582
1897
|
PKCS11_DEFINE_METHOD(C_GetObjectSize, 2);
|
1583
1898
|
PKCS11_DEFINE_METHOD(C_FindObjectsInit, 2);
|
@@ -1625,12 +1940,11 @@ Init_pkcs11_ext()
|
|
1625
1940
|
|
1626
1941
|
PKCS11_DEFINE_METHOD(C_WaitForSlotEvent, 1);
|
1627
1942
|
PKCS11_DEFINE_METHOD(C_Finalize, 0);
|
1943
|
+
PKCS11_DEFINE_METHOD(unload_library, 0);
|
1628
1944
|
|
1629
1945
|
///////////////////////////////////////
|
1630
1946
|
|
1631
|
-
|
1632
|
-
PKCS11_DEFINE_MEMBER(CK_C_INITIALIZE_ARGS, flags);
|
1633
|
-
PKCS11_DEFINE_MEMBER(CK_C_INITIALIZE_ARGS, pReserved);
|
1947
|
+
cCStruct = rb_define_class_under(mPKCS11, "CStruct", rb_cObject);
|
1634
1948
|
|
1635
1949
|
cCK_ATTRIBUTE = rb_define_class_under(mPKCS11, "CK_ATTRIBUTE", rb_cObject);
|
1636
1950
|
rb_define_alloc_func(cCK_ATTRIBUTE, ck_attr_s_alloc);
|
@@ -1638,100 +1952,16 @@ Init_pkcs11_ext()
|
|
1638
1952
|
rb_define_method(cCK_ATTRIBUTE, "type", ck_attr_type, 0);
|
1639
1953
|
rb_define_method(cCK_ATTRIBUTE, "value", ck_attr_value, 0);
|
1640
1954
|
|
1641
|
-
|
1642
|
-
|
1643
|
-
|
1644
|
-
|
1645
|
-
PKCS11_DEFINE_MEMBER(CK_INFO, libraryDescription);
|
1646
|
-
PKCS11_DEFINE_MEMBER(CK_INFO, libraryVersion);
|
1647
|
-
|
1648
|
-
PKCS11_DEFINE_STRUCT(CK_SLOT_INFO);
|
1649
|
-
PKCS11_DEFINE_MEMBER(CK_SLOT_INFO, slotDescription);
|
1650
|
-
PKCS11_DEFINE_MEMBER(CK_SLOT_INFO, manufacturerID);
|
1651
|
-
PKCS11_DEFINE_MEMBER(CK_SLOT_INFO, flags);
|
1652
|
-
PKCS11_DEFINE_MEMBER(CK_SLOT_INFO, hardwareVersion);
|
1653
|
-
PKCS11_DEFINE_MEMBER(CK_SLOT_INFO, firmwareVersion);
|
1654
|
-
|
1655
|
-
PKCS11_DEFINE_STRUCT(CK_TOKEN_INFO);
|
1656
|
-
PKCS11_DEFINE_MEMBER(CK_TOKEN_INFO, label);
|
1657
|
-
PKCS11_DEFINE_MEMBER(CK_TOKEN_INFO, manufacturerID);
|
1658
|
-
PKCS11_DEFINE_MEMBER(CK_TOKEN_INFO, model);
|
1659
|
-
PKCS11_DEFINE_MEMBER(CK_TOKEN_INFO, serialNumber);
|
1660
|
-
PKCS11_DEFINE_MEMBER(CK_TOKEN_INFO, flags);
|
1661
|
-
PKCS11_DEFINE_MEMBER(CK_TOKEN_INFO, ulMaxSessionCount);
|
1662
|
-
PKCS11_DEFINE_MEMBER(CK_TOKEN_INFO, ulSessionCount);
|
1663
|
-
PKCS11_DEFINE_MEMBER(CK_TOKEN_INFO, ulMaxRwSessionCount);
|
1664
|
-
PKCS11_DEFINE_MEMBER(CK_TOKEN_INFO, ulRwSessionCount);
|
1665
|
-
PKCS11_DEFINE_MEMBER(CK_TOKEN_INFO, ulMaxPinLen);
|
1666
|
-
PKCS11_DEFINE_MEMBER(CK_TOKEN_INFO, ulMinPinLen);
|
1667
|
-
PKCS11_DEFINE_MEMBER(CK_TOKEN_INFO, ulTotalPublicMemory);
|
1668
|
-
PKCS11_DEFINE_MEMBER(CK_TOKEN_INFO, ulFreePublicMemory);
|
1669
|
-
PKCS11_DEFINE_MEMBER(CK_TOKEN_INFO, ulTotalPrivateMemory);
|
1670
|
-
PKCS11_DEFINE_MEMBER(CK_TOKEN_INFO, ulFreePrivateMemory);
|
1671
|
-
PKCS11_DEFINE_MEMBER(CK_TOKEN_INFO, hardwareVersion);
|
1672
|
-
PKCS11_DEFINE_MEMBER(CK_TOKEN_INFO, firmwareVersion);
|
1673
|
-
PKCS11_DEFINE_MEMBER(CK_TOKEN_INFO, utcTime);
|
1674
|
-
|
1675
|
-
PKCS11_DEFINE_STRUCT(CK_MECHANISM_INFO);
|
1676
|
-
PKCS11_DEFINE_MEMBER(CK_MECHANISM_INFO, ulMinKeySize);
|
1677
|
-
PKCS11_DEFINE_MEMBER(CK_MECHANISM_INFO, ulMaxKeySize);
|
1678
|
-
PKCS11_DEFINE_MEMBER(CK_MECHANISM_INFO, flags);
|
1679
|
-
|
1680
|
-
PKCS11_DEFINE_STRUCT(CK_SESSION_INFO);
|
1681
|
-
PKCS11_DEFINE_MEMBER(CK_SESSION_INFO, slotID);
|
1682
|
-
PKCS11_DEFINE_MEMBER(CK_SESSION_INFO, state);
|
1683
|
-
PKCS11_DEFINE_MEMBER(CK_SESSION_INFO, flags);
|
1684
|
-
PKCS11_DEFINE_MEMBER(CK_SESSION_INFO, ulDeviceError);
|
1685
|
-
|
1955
|
+
/* Document-class: PKCS11::CK_MECHANISM
|
1956
|
+
*
|
1957
|
+
* Describes a crypto mechanism CKM_* with optional parameters. */
|
1958
|
+
/* cCK_MECHANISM = rb_define_class_under(mPKCS11, "CK_MECHANISM", rb_cObject); */
|
1686
1959
|
PKCS11_DEFINE_STRUCT(CK_MECHANISM);
|
1687
1960
|
rb_define_method(cCK_MECHANISM, "initialize", cCK_MECHANISM_initialize, -1);
|
1688
1961
|
PKCS11_DEFINE_MEMBER(CK_MECHANISM, mechanism);
|
1689
1962
|
PKCS11_DEFINE_MEMBER(CK_MECHANISM, pParameter);
|
1690
1963
|
|
1691
|
-
|
1692
|
-
//CK_RSA_PKCS_PSS_PARAMS
|
1693
|
-
//CK_AES_CBC_ENCRYPT_DATA_PARAMS
|
1694
|
-
//CK_AES_CTR_PARAMS
|
1695
|
-
//CK_ARIA_CBC_ENCRYPT_DATA_PARAMS
|
1696
|
-
//CK_CAMELLIA_CBC_ENCRYPT_DATA_PARAMS
|
1697
|
-
//CK_CAMELLIA_CTR_PARAMS
|
1698
|
-
//CK_CMS_SIG_PARAMS
|
1699
|
-
//CK_DES_CBC_ENCRYPT_DATA_PARAMS
|
1700
|
-
//CK_ECDH1_DERIVE_PARAMS
|
1701
|
-
//CK_ECDH2_DERIVE_PARAMS
|
1702
|
-
//CK_ECMQV_DERIVE_PARAMS
|
1703
|
-
//CK_FUNCTION_LIST
|
1704
|
-
//CK_KEA_DERIVE_PARAMS
|
1705
|
-
//CK_KEY_DERIVATION_STRING_DATA
|
1706
|
-
//CK_KEY_WRAP_SET_OAEP_PARAMS
|
1707
|
-
//CK_KIP_PARAMS
|
1708
|
-
//CK_OTP_PARAM
|
1709
|
-
//CK_OTP_PARAMS
|
1710
|
-
//CK_OTP_SIGNATURE_INFO
|
1711
|
-
//CK_PBE_PARAMS
|
1712
|
-
//CK_PKCS5_PBKD2_PARAMS
|
1713
|
-
//CK_RC2_CBC_PARAMS
|
1714
|
-
//CK_RC2_MAC_GENERAL_PARAMS
|
1715
|
-
//CK_RC5_CBC_PARAMS
|
1716
|
-
//CK_RC5_MAC_GENERAL_PARAMS
|
1717
|
-
//CK_RC5_PARAMS
|
1718
|
-
//CK_SKIPJACK_PRIVATE_WRAP_PARAMS
|
1719
|
-
//CK_SKIPJACK_RELAYX_PARAMS
|
1720
|
-
//CK_SSL3_KEY_MAT_OUT
|
1721
|
-
//CK_SSL3_KEY_MAT_PARAMS
|
1722
|
-
//CK_SSL3_MASTER_KEY_DERIVE_PARAMS
|
1723
|
-
//CK_SSL3_RANDOM_DATA
|
1724
|
-
//CK_TLS_PRF_PARAMS
|
1725
|
-
//CK_WTLS_KEY_MAT_OUT
|
1726
|
-
//CK_WTLS_KEY_MAT_PARAMS
|
1727
|
-
//CK_WTLS_MASTER_KEY_DERIVE_PARAMS
|
1728
|
-
//CK_WTLS_PRF_PARAMS
|
1729
|
-
//CK_WTLS_RANDOM_DATA
|
1730
|
-
//CK_X9_42_DH1_DERIVE_PARAMS
|
1731
|
-
//CK_X9_42_DH2_DERIVE_PARAMS
|
1732
|
-
//CK_X9_42_MQV_DERIVE_PARAMS
|
1733
|
-
|
1734
|
-
///////////////////////////////////////
|
1964
|
+
#include "pk11_struct_def.inc"
|
1735
1965
|
|
1736
|
-
Init_pkcs11_const(mPKCS11);
|
1966
|
+
Init_pkcs11_const(mPKCS11, ePKCS11Error);
|
1737
1967
|
}
|