Debian Codegraph @

pkcs11.go

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 // Copyright 2013 Miek Gieben. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // Package pkcs11 is a wrapper around the PKCS#11 cryptographic library.
6 package pkcs11
7
8 // It is *assumed*, that:
9 //
10 // * Go's uint size == PKCS11's CK_ULONG size
11 // * CK_ULONG never overflows an Go int
12
13 /*
14 #cgo windows CFLAGS: -DPACKED_STRUCTURES
15 #cgo linux LDFLAGS: -ldl
16 #cgo darwin LDFLAGS: -ldl
17 #cgo openbsd LDFLAGS: -ldl
18 #cgo freebsd LDFLAGS: -ldl
19
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <unistd.h>
24
25 #include "pkcs11go.h"
26
27 #ifdef _WIN32
28 #include <windows.h>
29
30 struct ctx {
31 HMODULE handle;
32 CK_FUNCTION_LIST_PTR sym;
33 };
34
35 // New initializes a ctx and fills the symbol table.
36 struct ctx *New(const char *module)
37 {
38 CK_C_GetFunctionList list;
39 struct ctx *c = calloc(1, sizeof(struct ctx));
40 c->handle = LoadLibrary(module);
41 if (c->handle == NULL) {
42 free(c);
43 return NULL;
44 }
45 list = (CK_C_GetFunctionList) GetProcAddress(c->handle, "C_GetFunctionList");
46 if (list == NULL) {
47 free(c);
48 return NULL;
49 }
50 list(&c->sym);
51 return c;
52 }
53
54 // Destroy cleans up a ctx.
55 void Destroy(struct ctx *c)
56 {
57 if (!c) {
58 return;
59 }
60 free(c);
61 }
62 #else
63 #include <dlfcn.h>
64
65 struct ctx {
66 void *handle;
67 CK_FUNCTION_LIST_PTR sym;
68 };
69
70 // New initializes a ctx and fills the symbol table.
71 struct ctx *New(const char *module)
72 {
73 CK_C_GetFunctionList list;
74 struct ctx *c = calloc(1, sizeof(struct ctx));
75 c->handle = dlopen(module, RTLD_LAZY);
76 if (c->handle == NULL) {
77 free(c);
78 return NULL;
79 }
80 list = (CK_C_GetFunctionList) dlsym(c->handle, "C_GetFunctionList");
81 if (list == NULL) {
82 free(c);
83 return NULL;
84 }
85 list(&c->sym);
86 return c;
87 }
88
89 // Destroy cleans up a ctx.
90 void Destroy(struct ctx *c)
91 {
92 if (!c) {
93 return;
94 }
95 if (c->handle == NULL) {
96 return;
97 }
98 if (dlclose(c->handle) < 0) {
99 return;
100 }
101 free(c);
102 }
103 #endif
104
105 CK_RV Initialize(struct ctx * c)
106 {
107 CK_C_INITIALIZE_ARGS args;
108 memset(&args, 0, sizeof(args));
109 args.flags = CKF_OS_LOCKING_OK;
110 return c->sym->C_Initialize(&args);
111 }
112
113 CK_RV Finalize(struct ctx * c)
114 {
115 return c->sym->C_Finalize(NULL);
116 }
117
118 CK_RV GetInfo(struct ctx * c, ckInfoPtr info)
119 {
120 CK_INFO p;
121 CK_RV e = c->sym->C_GetInfo(&p);
122 if (e != CKR_OK) {
123 return e;
124 }
125 info->cryptokiVersion = p.cryptokiVersion;
126 memcpy(info->manufacturerID, p.manufacturerID, sizeof(p.manufacturerID));
127 info->flags = p.flags;
128 memcpy(info->libraryDescription, p.libraryDescription, sizeof(p.libraryDescription));
129 info->libraryVersion = p.libraryVersion;
130 return e;
131 }
132
133 CK_RV GetSlotList(struct ctx * c, CK_BBOOL tokenPresent,
134 CK_ULONG_PTR * slotList, CK_ULONG_PTR ulCount)
135 {
136 CK_RV e = c->sym->C_GetSlotList(tokenPresent, NULL, ulCount);
137 if (e != CKR_OK) {
138 return e;
139 }
140 *slotList = calloc(*ulCount, sizeof(CK_SLOT_ID));
141 e = c->sym->C_GetSlotList(tokenPresent, *slotList, ulCount);
142 return e;
143 }
144
145 CK_RV GetSlotInfo(struct ctx * c, CK_ULONG slotID, CK_SLOT_INFO_PTR info)
146 {
147 CK_RV e = c->sym->C_GetSlotInfo((CK_SLOT_ID) slotID, info);
148 return e;
149 }
150
151 CK_RV GetTokenInfo(struct ctx * c, CK_ULONG slotID, CK_TOKEN_INFO_PTR info)
152 {
153 CK_RV e = c->sym->C_GetTokenInfo((CK_SLOT_ID) slotID, info);
154 return e;
155 }
156
157 CK_RV GetMechanismList(struct ctx * c, CK_ULONG slotID,
158 CK_ULONG_PTR * mech, CK_ULONG_PTR mechlen)
159 {
160 CK_RV e =
161 c->sym->C_GetMechanismList((CK_SLOT_ID) slotID, NULL, mechlen);
162 // Gemaltos PKCS11 implementation returns CKR_BUFFER_TOO_SMALL on a NULL ptr instad of CKR_OK as the spec states.
163 if (e != CKR_OK && e != CKR_BUFFER_TOO_SMALL) {
164 return e;
165 }
166 *mech = calloc(*mechlen, sizeof(CK_MECHANISM_TYPE));
167 e = c->sym->C_GetMechanismList((CK_SLOT_ID) slotID,
168 (CK_MECHANISM_TYPE_PTR) * mech, mechlen);
169 return e;
170 }
171
172 CK_RV GetMechanismInfo(struct ctx * c, CK_ULONG slotID, CK_MECHANISM_TYPE mech,
173 CK_MECHANISM_INFO_PTR info)
174 {
175 CK_RV e = c->sym->C_GetMechanismInfo((CK_SLOT_ID) slotID, mech, info);
176 return e;
177 }
178
179 CK_RV InitToken(struct ctx * c, CK_ULONG slotID, char *pin, CK_ULONG pinlen,
180 char *label)
181 {
182 CK_RV e =
183 c->sym->C_InitToken((CK_SLOT_ID) slotID, (CK_UTF8CHAR_PTR) pin,
184 pinlen, (CK_UTF8CHAR_PTR) label);
185 return e;
186 }
187
188 CK_RV InitPIN(struct ctx * c, CK_SESSION_HANDLE sh, char *pin, CK_ULONG pinlen)
189 {
190 CK_RV e = c->sym->C_InitPIN(sh, (CK_UTF8CHAR_PTR) pin, pinlen);
191 return e;
192 }
193
194 CK_RV SetPIN(struct ctx * c, CK_SESSION_HANDLE sh, char *oldpin,
195 CK_ULONG oldpinlen, char *newpin, CK_ULONG newpinlen)
196 {
197 CK_RV e = c->sym->C_SetPIN(sh, (CK_UTF8CHAR_PTR) oldpin, oldpinlen,
198 (CK_UTF8CHAR_PTR) newpin, newpinlen);
199 return e;
200 }
201
202 CK_RV OpenSession(struct ctx * c, CK_ULONG slotID, CK_ULONG flags,
203 CK_SESSION_HANDLE_PTR session)
204 {
205 CK_RV e =
206 c->sym->C_OpenSession((CK_SLOT_ID) slotID, (CK_FLAGS) flags, NULL,
207 NULL, session);
208 return e;
209 }
210
211 CK_RV CloseSession(struct ctx * c, CK_SESSION_HANDLE session)
212 {
213 CK_RV e = c->sym->C_CloseSession(session);
214 return e;
215 }
216
217 CK_RV CloseAllSessions(struct ctx * c, CK_ULONG slotID)
218 {
219 CK_RV e = c->sym->C_CloseAllSessions(slotID);
220 return e;
221 }
222
223 CK_RV GetSessionInfo(struct ctx * c, CK_SESSION_HANDLE session,
224 CK_SESSION_INFO_PTR info)
225 {
226 CK_RV e = c->sym->C_GetSessionInfo(session, info);
227 return e;
228 }
229
230 CK_RV GetOperationState(struct ctx * c, CK_SESSION_HANDLE session,
231 CK_BYTE_PTR * state, CK_ULONG_PTR statelen)
232 {
233 CK_RV rv = c->sym->C_GetOperationState(session, NULL, statelen);
234 if (rv != CKR_OK) {
235 return rv;
236 }
237 *state = calloc(*statelen, sizeof(CK_BYTE));
238 if (*state == NULL) {
239 return CKR_HOST_MEMORY;
240 }
241 rv = c->sym->C_GetOperationState(session, *state, statelen);
242 return rv;
243 }
244
245 CK_RV SetOperationState(struct ctx * c, CK_SESSION_HANDLE session,
246 CK_BYTE_PTR state, CK_ULONG statelen,
247 CK_OBJECT_HANDLE encryptkey, CK_OBJECT_HANDLE authkey)
248 {
249 return c->sym->C_SetOperationState(session, state, statelen, encryptkey,
250 authkey);
251 }
252
253 CK_RV Login(struct ctx *c, CK_SESSION_HANDLE session, CK_USER_TYPE userType,
254 char *pin, CK_ULONG pinLen)
255 {
256 if (pinLen == 0) {
257 pin = NULL;
258 }
259 CK_RV e =
260 c->sym->C_Login(session, userType, (CK_UTF8CHAR_PTR) pin, pinLen);
261 return e;
262 }
263
264 CK_RV Logout(struct ctx * c, CK_SESSION_HANDLE session)
265 {
266 CK_RV e = c->sym->C_Logout(session);
267 return e;
268 }
269
270 CK_RV CreateObject(struct ctx * c, CK_SESSION_HANDLE session,
271 CK_ATTRIBUTE_PTR temp, CK_ULONG tempCount,
272 CK_OBJECT_HANDLE_PTR obj)
273 {
274 return c->sym->C_CreateObject(session, temp, tempCount, obj);
275 }
276
277 CK_RV CopyObject(struct ctx * c, CK_SESSION_HANDLE session, CK_OBJECT_HANDLE o,
278 CK_ATTRIBUTE_PTR temp, CK_ULONG tempCount,
279 CK_OBJECT_HANDLE_PTR obj)
280 {
281 return c->sym->C_CopyObject(session, o, temp, tempCount, obj);
282 }
283
284 CK_RV DestroyObject(struct ctx * c, CK_SESSION_HANDLE session,
285 CK_OBJECT_HANDLE object)
286 {
287 CK_RV e = c->sym->C_DestroyObject(session, object);
288 return e;
289 }
290
291 CK_RV GetObjectSize(struct ctx * c, CK_SESSION_HANDLE session,
292 CK_OBJECT_HANDLE object, CK_ULONG_PTR size)
293 {
294 CK_RV e = c->sym->C_GetObjectSize(session, object, size);
295 return e;
296 }
297
298 CK_RV GetAttributeValue(struct ctx * c, CK_SESSION_HANDLE session,
299 CK_OBJECT_HANDLE object, CK_ATTRIBUTE_PTR temp,
300 CK_ULONG templen)
301 {
302 // Call for the first time, check the returned ulValue in the attributes, then
303 // allocate enough space and try again.
304 CK_RV e = c->sym->C_GetAttributeValue(session, object, temp, templen);
305 if (e != CKR_OK) {
306 return e;
307 }
308 CK_ULONG i;
309 for (i = 0; i < templen; i++) {
310 if ((CK_LONG) temp[i].ulValueLen == -1) {
311 // either access denied or no such object
312 continue;
313 }
314 temp[i].pValue = calloc(temp[i].ulValueLen, sizeof(CK_BYTE));
315 }
316 return c->sym->C_GetAttributeValue(session, object, temp, templen);
317 }
318
319 CK_RV SetAttributeValue(struct ctx * c, CK_SESSION_HANDLE session,
320 CK_OBJECT_HANDLE object, CK_ATTRIBUTE_PTR temp,
321 CK_ULONG templen)
322 {
323 return c->sym->C_SetAttributeValue(session, object, temp, templen);
324 }
325
326 CK_RV FindObjectsInit(struct ctx * c, CK_SESSION_HANDLE session,
327 CK_ATTRIBUTE_PTR temp, CK_ULONG tempCount)
328 {
329 return c->sym->C_FindObjectsInit(session, temp, tempCount);
330 }
331
332 CK_RV FindObjects(struct ctx * c, CK_SESSION_HANDLE session,
333 CK_OBJECT_HANDLE_PTR * obj, CK_ULONG max,
334 CK_ULONG_PTR objCount)
335 {
336 *obj = calloc(max, sizeof(CK_OBJECT_HANDLE));
337 CK_RV e = c->sym->C_FindObjects(session, *obj, max, objCount);
338 return e;
339 }
340
341 CK_RV FindObjectsFinal(struct ctx * c, CK_SESSION_HANDLE session)
342 {
343 CK_RV e = c->sym->C_FindObjectsFinal(session);
344 return e;
345 }
346
347 CK_RV EncryptInit(struct ctx * c, CK_SESSION_HANDLE session,
348 CK_MECHANISM_PTR mechanism, CK_OBJECT_HANDLE key)
349 {
350 return c->sym->C_EncryptInit(session, mechanism, key);
351 }
352
353 CK_RV Encrypt(struct ctx * c, CK_SESSION_HANDLE session, CK_BYTE_PTR message,
354 CK_ULONG mlen, CK_BYTE_PTR * enc, CK_ULONG_PTR enclen)
355 {
356 CK_RV rv = c->sym->C_Encrypt(session, message, mlen, NULL, enclen);
357 if (rv != CKR_OK) {
358 return rv;
359 }
360 *enc = calloc(*enclen, sizeof(CK_BYTE));
361 if (*enc == NULL) {
362 return CKR_HOST_MEMORY;
363 }
364 rv = c->sym->C_Encrypt(session, message, mlen, *enc, enclen);
365 return rv;
366 }
367
368 CK_RV EncryptUpdate(struct ctx * c, CK_SESSION_HANDLE session,
369 CK_BYTE_PTR plain, CK_ULONG plainlen, CK_BYTE_PTR * cipher,
370 CK_ULONG_PTR cipherlen)
371 {
372 CK_RV rv =
373 c->sym->C_EncryptUpdate(session, plain, plainlen, NULL, cipherlen);
374 if (rv != CKR_OK) {
375 return rv;
376 }
377 *cipher = calloc(*cipherlen, sizeof(CK_BYTE));
378 if (*cipher == NULL) {
379 return CKR_HOST_MEMORY;
380 }
381 rv = c->sym->C_EncryptUpdate(session, plain, plainlen, *cipher,
382 cipherlen);
383 return rv;
384 }
385
386 CK_RV EncryptFinal(struct ctx * c, CK_SESSION_HANDLE session,
387 CK_BYTE_PTR * cipher, CK_ULONG_PTR cipherlen)
388 {
389 CK_RV rv = c->sym->C_EncryptFinal(session, NULL, cipherlen);
390 if (rv != CKR_OK) {
391 return rv;
392 }
393 *cipher = calloc(*cipherlen, sizeof(CK_BYTE));
394 if (*cipher == NULL) {
395 return CKR_HOST_MEMORY;
396 }
397 rv = c->sym->C_EncryptFinal(session, *cipher, cipherlen);
398 return rv;
399 }
400
401 CK_RV DecryptInit(struct ctx * c, CK_SESSION_HANDLE session,
402 CK_MECHANISM_PTR mechanism, CK_OBJECT_HANDLE key)
403 {
404 return c->sym->C_DecryptInit(session, mechanism, key);
405 }
406
407 CK_RV Decrypt(struct ctx * c, CK_SESSION_HANDLE session, CK_BYTE_PTR cipher,
408 CK_ULONG clen, CK_BYTE_PTR * plain, CK_ULONG_PTR plainlen)
409 {
410 CK_RV e = c->sym->C_Decrypt(session, cipher, clen, NULL, plainlen);
411 if (e != CKR_OK) {
412 return e;
413 }
414 *plain = calloc(*plainlen, sizeof(CK_BYTE));
415 if (*plain == NULL) {
416 return CKR_HOST_MEMORY;
417 }
418 e = c->sym->C_Decrypt(session, cipher, clen, *plain, plainlen);
419 return e;
420 }
421
422 CK_RV DecryptUpdate(struct ctx * c, CK_SESSION_HANDLE session,
423 CK_BYTE_PTR cipher, CK_ULONG cipherlen, CK_BYTE_PTR * part,
424 CK_ULONG_PTR partlen)
425 {
426 CK_RV rv =
427 c->sym->C_DecryptUpdate(session, cipher, cipherlen, NULL, partlen);
428 if (rv != CKR_OK) {
429 return rv;
430 }
431 *part = calloc(*partlen, sizeof(CK_BYTE));
432 if (*part == NULL) {
433 return CKR_HOST_MEMORY;
434 }
435 rv = c->sym->C_DecryptUpdate(session, cipher, cipherlen, *part,
436 partlen);
437 return rv;
438 }
439
440 CK_RV DecryptFinal(struct ctx * c, CK_SESSION_HANDLE session,
441 CK_BYTE_PTR * plain, CK_ULONG_PTR plainlen)
442 {
443 CK_RV rv = c->sym->C_DecryptFinal(session, NULL, plainlen);
444 if (rv != CKR_OK) {
445 return rv;
446 }
447 *plain = calloc(*plainlen, sizeof(CK_BYTE));
448 if (*plain == NULL) {
449 return CKR_HOST_MEMORY;
450 }
451 rv = c->sym->C_DecryptFinal(session, *plain, plainlen);
452 return rv;
453 }
454
455 CK_RV DigestInit(struct ctx * c, CK_SESSION_HANDLE session,
456 CK_MECHANISM_PTR mechanism)
457 {
458 return c->sym->C_DigestInit(session, mechanism);
459 }
460
461 CK_RV Digest(struct ctx * c, CK_SESSION_HANDLE session, CK_BYTE_PTR message,
462 CK_ULONG mlen, CK_BYTE_PTR * hash, CK_ULONG_PTR hashlen)
463 {
464 CK_RV rv = c->sym->C_Digest(session, message, mlen, NULL, hashlen);
465 if (rv != CKR_OK) {
466 return rv;
467 }
468 *hash = calloc(*hashlen, sizeof(CK_BYTE));
469 if (*hash == NULL) {
470 return CKR_HOST_MEMORY;
471 }
472 rv = c->sym->C_Digest(session, message, mlen, *hash, hashlen);
473 return rv;
474 }
475
476 CK_RV DigestUpdate(struct ctx * c, CK_SESSION_HANDLE session,
477 CK_BYTE_PTR message, CK_ULONG mlen)
478 {
479 CK_RV rv = c->sym->C_DigestUpdate(session, message, mlen);
480 return rv;
481 }
482
483 CK_RV DigestKey(struct ctx * c, CK_SESSION_HANDLE session, CK_OBJECT_HANDLE key)
484 {
485 CK_RV rv = c->sym->C_DigestKey(session, key);
486 return rv;
487 }
488
489 CK_RV DigestFinal(struct ctx * c, CK_SESSION_HANDLE session, CK_BYTE_PTR * hash,
490 CK_ULONG_PTR hashlen)
491 {
492 CK_RV rv = c->sym->C_DigestFinal(session, NULL, hashlen);
493 if (rv != CKR_OK) {
494 return rv;
495 }
496 *hash = calloc(*hashlen, sizeof(CK_BYTE));
497 if (*hash == NULL) {
498 return CKR_HOST_MEMORY;
499 }
500 rv = c->sym->C_DigestFinal(session, *hash, hashlen);
501 return rv;
502 }
503
504 CK_RV SignInit(struct ctx * c, CK_SESSION_HANDLE session,
505 CK_MECHANISM_PTR mechanism, CK_OBJECT_HANDLE key)
506 {
507 return c->sym->C_SignInit(session, mechanism, key);
508 }
509
510 CK_RV Sign(struct ctx * c, CK_SESSION_HANDLE session, CK_BYTE_PTR message,
511 CK_ULONG mlen, CK_BYTE_PTR * sig, CK_ULONG_PTR siglen)
512 {
513 CK_RV rv = c->sym->C_Sign(session, message, mlen, NULL, siglen);
514 if (rv != CKR_OK) {
515 return rv;
516 }
517 *sig = calloc(*siglen, sizeof(CK_BYTE));
518 if (*sig == NULL) {
519 return CKR_HOST_MEMORY;
520 }
521 rv = c->sym->C_Sign(session, message, mlen, *sig, siglen);
522 return rv;
523 }
524
525 CK_RV SignUpdate(struct ctx * c, CK_SESSION_HANDLE session,
526 CK_BYTE_PTR message, CK_ULONG mlen)
527 {
528 CK_RV rv = c->sym->C_SignUpdate(session, message, mlen);
529 return rv;
530 }
531
532 CK_RV SignFinal(struct ctx * c, CK_SESSION_HANDLE session, CK_BYTE_PTR * sig,
533 CK_ULONG_PTR siglen)
534 {
535 CK_RV rv = c->sym->C_SignFinal(session, NULL, siglen);
536 if (rv != CKR_OK) {
537 return rv;
538 }
539 *sig = calloc(*siglen, sizeof(CK_BYTE));
540 if (*sig == NULL) {
541 return CKR_HOST_MEMORY;
542 }
543 rv = c->sym->C_SignFinal(session, *sig, siglen);
544 return rv;
545 }
546
547 CK_RV SignRecoverInit(struct ctx * c, CK_SESSION_HANDLE session,
548 CK_MECHANISM_PTR mechanism, CK_OBJECT_HANDLE key)
549 {
550 return c->sym->C_SignRecoverInit(session, mechanism, key);
551 }
552
553 CK_RV SignRecover(struct ctx * c, CK_SESSION_HANDLE session, CK_BYTE_PTR data,
554 CK_ULONG datalen, CK_BYTE_PTR * sig, CK_ULONG_PTR siglen)
555 {
556 CK_RV rv = c->sym->C_SignRecover(session, data, datalen, NULL, siglen);
557 if (rv != CKR_OK) {
558 return rv;
559 }
560 *sig = calloc(*siglen, sizeof(CK_BYTE));
561 if (*sig == NULL) {
562 return CKR_HOST_MEMORY;
563 }
564 rv = c->sym->C_SignRecover(session, data, datalen, *sig, siglen);
565 return rv;
566 }
567
568 CK_RV VerifyInit(struct ctx * c, CK_SESSION_HANDLE session,
569 CK_MECHANISM_PTR mechanism, CK_OBJECT_HANDLE key)
570 {
571 return c->sym->C_VerifyInit(session, mechanism, key);
572 }
573
574 CK_RV Verify(struct ctx * c, CK_SESSION_HANDLE session, CK_BYTE_PTR message,
575 CK_ULONG mesglen, CK_BYTE_PTR sig, CK_ULONG siglen)
576 {
577 CK_RV rv = c->sym->C_Verify(session, message, mesglen, sig, siglen);
578 return rv;
579 }
580
581 CK_RV VerifyUpdate(struct ctx * c, CK_SESSION_HANDLE session,
582 CK_BYTE_PTR part, CK_ULONG partlen)
583 {
584 CK_RV rv = c->sym->C_VerifyUpdate(session, part, partlen);
585 return rv;
586 }
587
588 CK_RV VerifyFinal(struct ctx * c, CK_SESSION_HANDLE session, CK_BYTE_PTR sig,
589 CK_ULONG siglen)
590 {
591 CK_RV rv = c->sym->C_VerifyFinal(session, sig, siglen);
592 return rv;
593 }
594
595 CK_RV VerifyRecoverInit(struct ctx * c, CK_SESSION_HANDLE session,
596 CK_MECHANISM_PTR mechanism, CK_OBJECT_HANDLE key)
597 {
598 return c->sym->C_VerifyRecoverInit(session, mechanism, key);
599 }
600
601 CK_RV VerifyRecover(struct ctx * c, CK_SESSION_HANDLE session, CK_BYTE_PTR sig,
602 CK_ULONG siglen, CK_BYTE_PTR * data, CK_ULONG_PTR datalen)
603 {
604 CK_RV rv = c->sym->C_VerifyRecover(session, sig, siglen, NULL, datalen);
605 if (rv != CKR_OK) {
606 return rv;
607 }
608 *data = calloc(*datalen, sizeof(CK_BYTE));
609 if (*data == NULL) {
610 return CKR_HOST_MEMORY;
611 }
612 rv = c->sym->C_VerifyRecover(session, sig, siglen, *data, datalen);
613 return rv;
614 }
615
616 CK_RV DigestEncryptUpdate(struct ctx * c, CK_SESSION_HANDLE session,
617 CK_BYTE_PTR part, CK_ULONG partlen, CK_BYTE_PTR * enc,
618 CK_ULONG_PTR enclen)
619 {
620 CK_RV rv =
621 c->sym->C_DigestEncryptUpdate(session, part, partlen, NULL, enclen);
622 if (rv != CKR_OK) {
623 return rv;
624 }
625 *enc = calloc(*enclen, sizeof(CK_BYTE));
626 if (*enc == NULL) {
627 return CKR_HOST_MEMORY;
628 }
629 rv = c->sym->C_DigestEncryptUpdate(session, part, partlen, *enc,
630 enclen);
631 return rv;
632 }
633
634 CK_RV DecryptDigestUpdate(struct ctx * c, CK_SESSION_HANDLE session,
635 CK_BYTE_PTR cipher, CK_ULONG cipherlen,
636 CK_BYTE_PTR * part, CK_ULONG_PTR partlen)
637 {
638 CK_RV rv =
639 c->sym->C_DecryptDigestUpdate(session, cipher, cipherlen, NULL,
640 partlen);
641 if (rv != CKR_OK) {
642 return rv;
643 }
644 *part = calloc(*partlen, sizeof(CK_BYTE));
645 if (*part == NULL) {
646 return CKR_HOST_MEMORY;
647 }
648 rv = c->sym->C_DecryptDigestUpdate(session, cipher, cipherlen, *part,
649 partlen);
650 return rv;
651 }
652
653 CK_RV SignEncryptUpdate(struct ctx * c, CK_SESSION_HANDLE session,
654 CK_BYTE_PTR part, CK_ULONG partlen, CK_BYTE_PTR * enc,
655 CK_ULONG_PTR enclen)
656 {
657 CK_RV rv =
658 c->sym->C_SignEncryptUpdate(session, part, partlen, NULL, enclen);
659 if (rv != CKR_OK) {
660 return rv;
661 }
662 *enc = calloc(*enclen, sizeof(CK_BYTE));
663 if (*enc == NULL) {
664 return CKR_HOST_MEMORY;
665 }
666 rv = c->sym->C_SignEncryptUpdate(session, part, partlen, *enc, enclen);
667 return rv;
668 }
669
670 CK_RV DecryptVerifyUpdate(struct ctx * c, CK_SESSION_HANDLE session,
671 CK_BYTE_PTR cipher, CK_ULONG cipherlen,
672 CK_BYTE_PTR * part, CK_ULONG_PTR partlen)
673 {
674 CK_RV rv =
675 c->sym->C_DecryptVerifyUpdate(session, cipher, cipherlen, NULL,
676 partlen);
677 if (rv != CKR_OK) {
678 return rv;
679 }
680 *part = calloc(*partlen, sizeof(CK_BYTE));
681 if (*part == NULL) {
682 return CKR_HOST_MEMORY;
683 }
684 rv = c->sym->C_DecryptVerifyUpdate(session, cipher, cipherlen, *part,
685 partlen);
686 return rv;
687 }
688
689 CK_RV GenerateKey(struct ctx * c, CK_SESSION_HANDLE session,
690 CK_MECHANISM_PTR mechanism, CK_ATTRIBUTE_PTR temp,
691 CK_ULONG tempCount, CK_OBJECT_HANDLE_PTR key)
692 {
693 return c->sym->C_GenerateKey(session, mechanism, temp, tempCount, key);
694 }
695
696 CK_RV GenerateKeyPair(struct ctx * c, CK_SESSION_HANDLE session,
697 CK_MECHANISM_PTR mechanism, CK_ATTRIBUTE_PTR pub,
698 CK_ULONG pubCount, CK_ATTRIBUTE_PTR priv,
699 CK_ULONG privCount, CK_OBJECT_HANDLE_PTR pubkey,
700 CK_OBJECT_HANDLE_PTR privkey)
701 {
702 return c->sym->C_GenerateKeyPair(session, mechanism, pub, pubCount,
703 priv, privCount, pubkey, privkey);
704 }
705
706 CK_RV WrapKey(struct ctx * c, CK_SESSION_HANDLE session,
707 CK_MECHANISM_PTR mechanism, CK_OBJECT_HANDLE wrappingkey,
708 CK_OBJECT_HANDLE key, CK_BYTE_PTR * wrapped,
709 CK_ULONG_PTR wrappedlen)
710 {
711 CK_RV rv = c->sym->C_WrapKey(session, mechanism, wrappingkey, key, NULL,
712 wrappedlen);
713 if (rv != CKR_OK) {
714 return rv;
715 }
716 *wrapped = calloc(*wrappedlen, sizeof(CK_BYTE));
717 if (*wrapped == NULL) {
718 return CKR_HOST_MEMORY;
719 }
720 rv = c->sym->C_WrapKey(session, mechanism, wrappingkey, key, *wrapped,
721 wrappedlen);
722 return rv;
723 }
724
725 CK_RV DeriveKey(struct ctx * c, CK_SESSION_HANDLE session,
726 CK_MECHANISM_PTR mechanism, CK_OBJECT_HANDLE basekey,
727 CK_ATTRIBUTE_PTR a, CK_ULONG alen, CK_OBJECT_HANDLE_PTR key)
728 {
729 return c->sym->C_DeriveKey(session, mechanism, basekey, a, alen, key);
730 }
731
732 CK_RV UnwrapKey(struct ctx * c, CK_SESSION_HANDLE session,
733 CK_MECHANISM_PTR mechanism, CK_OBJECT_HANDLE unwrappingkey,
734 CK_BYTE_PTR wrappedkey, CK_ULONG wrappedkeylen,
735 CK_ATTRIBUTE_PTR a, CK_ULONG alen, CK_OBJECT_HANDLE_PTR key)
736 {
737 return c->sym->C_UnwrapKey(session, mechanism, unwrappingkey, wrappedkey,
738 wrappedkeylen, a, alen, key);
739 }
740
741 CK_RV SeedRandom(struct ctx * c, CK_SESSION_HANDLE session, CK_BYTE_PTR seed,
742 CK_ULONG seedlen)
743 {
744 CK_RV e = c->sym->C_SeedRandom(session, seed, seedlen);
745 return e;
746 }
747
748 CK_RV GenerateRandom(struct ctx * c, CK_SESSION_HANDLE session,
749 CK_BYTE_PTR * rand, CK_ULONG length)
750 {
751 *rand = calloc(length, sizeof(CK_BYTE));
752 if (*rand == NULL) {
753 return CKR_HOST_MEMORY;
754 }
755 CK_RV e = c->sym->C_GenerateRandom(session, *rand, length);
756 return e;
757 }
758
759 CK_RV WaitForSlotEvent(struct ctx * c, CK_FLAGS flags, CK_ULONG_PTR slot)
760 {
761 CK_RV e =
762 c->sym->C_WaitForSlotEvent(flags, (CK_SLOT_ID_PTR) slot, NULL);
763 return e;
764 }
765
766 static inline CK_VOID_PTR getAttributePval(CK_ATTRIBUTE_PTR a)
767 {
768 return a->pValue;
769 }
770
771 */
772 import "C"
773 import "strings"
774
775 import "unsafe"
776
777 // Ctx contains the current pkcs11 context.
778 type Ctx struct {
779 ctx *C.struct_ctx
780 }
781
782 // New creates a new context and initializes the module/library for use.
783 func New(module string) *Ctx {
784 c := new(Ctx)
785 mod := C.CString(module)
786 defer C.free(unsafe.Pointer(mod))
787 c.ctx = C.New(mod)
788 if c.ctx == nil {
789 return nil
790 }
791 return c
792 }
793
794 // Destroy unloads the module/library and frees any remaining memory.
795 func (c *Ctx) Destroy() {
796 if c == nil || c.ctx == nil {
797 return
798 }
799 C.Destroy(c.ctx)
800 c.ctx = nil
801 }
802
803 // Initialize initializes the Cryptoki library.
804 func (c *Ctx) Initialize() error {
805 e := C.Initialize(c.ctx)
806 return toError(e)
807 }
808
809 // Finalize indicates that an application is done with the Cryptoki library.
810 func (c *Ctx) Finalize() error {
811 if c.ctx == nil {
812 return toError(CKR_CRYPTOKI_NOT_INITIALIZED)
813 }
814 e := C.Finalize(c.ctx)
815 return toError(e)
816 }
817
818 // GetInfo returns general information about Cryptoki.
819 func (c *Ctx) GetInfo() (Info, error) {
820 var p C.ckInfo
821 e := C.GetInfo(c.ctx, &p)
822 i := Info{
823 CryptokiVersion: toVersion(p.cryptokiVersion),
824 ManufacturerID: strings.TrimRight(string(C.GoBytes(unsafe.Pointer(&p.manufacturerID[0]), 32)), " "),
825 Flags: uint(p.flags),
826 LibraryDescription: strings.TrimRight(string(C.GoBytes(unsafe.Pointer(&p.libraryDescription[0]), 32)), " "),
827 LibraryVersion: toVersion(p.libraryVersion),
828 }
829 return i, toError(e)
830 }
831
832 // GetSlotList obtains a list of slots in the system.
833 func (c *Ctx) GetSlotList(tokenPresent bool) ([]uint, error) {
834 var (
835 slotList C.CK_ULONG_PTR
836 ulCount C.CK_ULONG
837 )
838 e := C.GetSlotList(c.ctx, cBBool(tokenPresent), &slotList, &ulCount)
839 if toError(e) != nil {
840 return nil, toError(e)
841 }
842 l := toList(slotList, ulCount)
843 return l, nil
844 }
845
846 // GetSlotInfo obtains information about a particular slot in the system.
847 func (c *Ctx) GetSlotInfo(slotID uint) (SlotInfo, error) {
848 var csi C.CK_SLOT_INFO
849 e := C.GetSlotInfo(c.ctx, C.CK_ULONG(slotID), &csi)
850 s := SlotInfo{
851 SlotDescription: strings.TrimRight(string(C.GoBytes(unsafe.Pointer(&csi.slotDescription[0]), 64)), " "),
852 ManufacturerID: strings.TrimRight(string(C.GoBytes(unsafe.Pointer(&csi.manufacturerID[0]), 32)), " "),
853 Flags: uint(csi.flags),
854 HardwareVersion: toVersion(csi.hardwareVersion),
855 FirmwareVersion: toVersion(csi.firmwareVersion),
856 }
857 return s, toError(e)
858 }
859
860 // GetTokenInfo obtains information about a particular token
861 // in the system.
862 func (c *Ctx) GetTokenInfo(slotID uint) (TokenInfo, error) {
863 var cti C.CK_TOKEN_INFO
864 e := C.GetTokenInfo(c.ctx, C.CK_ULONG(slotID), &cti)
865 s := TokenInfo{
866 Label: strings.TrimRight(string(C.GoBytes(unsafe.Pointer(&cti.label[0]), 32)), " "),
867 ManufacturerID: strings.TrimRight(string(C.GoBytes(unsafe.Pointer(&cti.manufacturerID[0]), 32)), " "),
868 Model: strings.TrimRight(string(C.GoBytes(unsafe.Pointer(&cti.model[0]), 16)), " "),
869 SerialNumber: strings.TrimRight(string(C.GoBytes(unsafe.Pointer(&cti.serialNumber[0]), 16)), " "),
870 Flags: uint(cti.flags),
871 MaxSessionCount: uint(cti.ulMaxSessionCount),
872 SessionCount: uint(cti.ulSessionCount),
873 MaxRwSessionCount: uint(cti.ulMaxRwSessionCount),
874 RwSessionCount: uint(cti.ulRwSessionCount),
875 MaxPinLen: uint(cti.ulMaxPinLen),
876 MinPinLen: uint(cti.ulMinPinLen),
877 TotalPublicMemory: uint(cti.ulTotalPublicMemory),
878 FreePublicMemory: uint(cti.ulFreePublicMemory),
879 TotalPrivateMemory: uint(cti.ulTotalPrivateMemory),
880 FreePrivateMemory: uint(cti.ulFreePrivateMemory),
881 HardwareVersion: toVersion(cti.hardwareVersion),
882 FirmwareVersion: toVersion(cti.firmwareVersion),
883 UTCTime: strings.TrimRight(string(C.GoBytes(unsafe.Pointer(&cti.utcTime[0]), 16)), " "),
884 }
885 return s, toError(e)
886 }
887
888 // GetMechanismList obtains a list of mechanism types supported by a token.
889 func (c *Ctx) GetMechanismList(slotID uint) ([]*Mechanism, error) {
890 var (
891 mech C.CK_ULONG_PTR // in pkcs#11 we're all CK_ULONGs \o/
892 mechlen C.CK_ULONG
893 )
894 e := C.GetMechanismList(c.ctx, C.CK_ULONG(slotID), &mech, &mechlen)
895 if toError(e) != nil {
896 return nil, toError(e)
897 }
898 // Although the function returns only type, cast them back into real
899 // attributes as this is used in other functions.
900 m := make([]*Mechanism, int(mechlen))
901 for i, typ := range toList(mech, mechlen) {
902 m[i] = NewMechanism(typ, nil)
903 }
904 return m, nil
905 }
906
907 // GetMechanismInfo obtains information about a particular
908 // mechanism possibly supported by a token.
909 func (c *Ctx) GetMechanismInfo(slotID uint, m []*Mechanism) (MechanismInfo, error) {
910 var cm C.CK_MECHANISM_INFO
911 e := C.GetMechanismInfo(c.ctx, C.CK_ULONG(slotID), C.CK_MECHANISM_TYPE(m[0].Mechanism),
912 C.CK_MECHANISM_INFO_PTR(&cm))
913 mi := MechanismInfo{
914 MinKeySize: uint(cm.ulMinKeySize),
915 MaxKeySize: uint(cm.ulMaxKeySize),
916 Flags: uint(cm.flags),
917 }
918 return mi, toError(e)
919 }
920
921 // InitToken initializes a token. The label must be 32 characters
922 // long, it is blank padded if it is not. If it is longer it is capped
923 // to 32 characters.
924 func (c *Ctx) InitToken(slotID uint, pin string, label string) error {
925 p := C.CString(pin)
926 defer C.free(unsafe.Pointer(p))
927 ll := len(label)
928 for ll < 32 {
929 label += " "
930 ll++
931 }
932 l := C.CString(label[:32])
933 defer C.free(unsafe.Pointer(l))
934 e := C.InitToken(c.ctx, C.CK_ULONG(slotID), p, C.CK_ULONG(len(pin)), l)
935 return toError(e)
936 }
937
938 // InitPIN initializes the normal user's PIN.
939 func (c *Ctx) InitPIN(sh SessionHandle, pin string) error {
940 p := C.CString(pin)
941 defer C.free(unsafe.Pointer(p))
942 e := C.InitPIN(c.ctx, C.CK_SESSION_HANDLE(sh), p, C.CK_ULONG(len(pin)))
943 return toError(e)
944 }
945
946 // SetPIN modifies the PIN of the user who is logged in.
947 func (c *Ctx) SetPIN(sh SessionHandle, oldpin string, newpin string) error {
948 old := C.CString(oldpin)
949 defer C.free(unsafe.Pointer(old))
950 new := C.CString(newpin)
951 defer C.free(unsafe.Pointer(new))
952 e := C.SetPIN(c.ctx, C.CK_SESSION_HANDLE(sh), old, C.CK_ULONG(len(oldpin)), new, C.CK_ULONG(len(newpin)))
953 return toError(e)
954 }
955
956 // OpenSession opens a session between an application and a token.
957 func (c *Ctx) OpenSession(slotID uint, flags uint) (SessionHandle, error) {
958 var s C.CK_SESSION_HANDLE
959 e := C.OpenSession(c.ctx, C.CK_ULONG(slotID), C.CK_ULONG(flags), C.CK_SESSION_HANDLE_PTR(&s))
960 return SessionHandle(s), toError(e)
961 }
962
963 // CloseSession closes a session between an application and a token.
964 func (c *Ctx) CloseSession(sh SessionHandle) error {
965 if c.ctx == nil {
966 return toError(CKR_CRYPTOKI_NOT_INITIALIZED)
967 }
968 e := C.CloseSession(c.ctx, C.CK_SESSION_HANDLE(sh))
969 return toError(e)
970 }
971
972 // CloseAllSessions closes all sessions with a token.
973 func (c *Ctx) CloseAllSessions(slotID uint) error {
974 if c.ctx == nil {
975 return toError(CKR_CRYPTOKI_NOT_INITIALIZED)
976 }
977 e := C.CloseAllSessions(c.ctx, C.CK_ULONG(slotID))
978 return toError(e)
979 }
980
981 // GetSessionInfo obtains information about the session.
982 func (c *Ctx) GetSessionInfo(sh SessionHandle) (SessionInfo, error) {
983 var csi C.CK_SESSION_INFO
984 e := C.GetSessionInfo(c.ctx, C.CK_SESSION_HANDLE(sh), &csi)
985 s := SessionInfo{SlotID: uint(csi.slotID),
986 State: uint(csi.state),
987 Flags: uint(csi.flags),
988 DeviceError: uint(csi.ulDeviceError),
989 }
990 return s, toError(e)
991 }
992
993 // GetOperationState obtains the state of the cryptographic operation in a session.
994 func (c *Ctx) GetOperationState(sh SessionHandle) ([]byte, error) {
995 var (
996 state C.CK_BYTE_PTR
997 statelen C.CK_ULONG
998 )
999 e := C.GetOperationState(c.ctx, C.CK_SESSION_HANDLE(sh), &state, &statelen)
1000 defer C.free(unsafe.Pointer(state))
1001 if toError(e) != nil {
1002 return nil, toError(e)
1003 }
1004 b := C.GoBytes(unsafe.Pointer(state), C.int(statelen))
1005 return b, nil
1006 }
1007
1008 // SetOperationState restores the state of the cryptographic operation in a session.
1009 func (c *Ctx) SetOperationState(sh SessionHandle, state []byte, encryptKey, authKey ObjectHandle) error {
1010 e := C.SetOperationState(c.ctx, C.CK_SESSION_HANDLE(sh), C.CK_BYTE_PTR(unsafe.Pointer(&state[0])),
1011 C.CK_ULONG(len(state)), C.CK_OBJECT_HANDLE(encryptKey), C.CK_OBJECT_HANDLE(authKey))
1012 return toError(e)
1013 }
1014
1015 // Login logs a user into a token.
1016 func (c *Ctx) Login(sh SessionHandle, userType uint, pin string) error {
1017 p := C.CString(pin)
1018 defer C.free(unsafe.Pointer(p))
1019 e := C.Login(c.ctx, C.CK_SESSION_HANDLE(sh), C.CK_USER_TYPE(userType), p, C.CK_ULONG(len(pin)))
1020 return toError(e)
1021 }
1022
1023 // Logout logs a user out from a token.
1024 func (c *Ctx) Logout(sh SessionHandle) error {
1025 if c.ctx == nil {
1026 return toError(CKR_CRYPTOKI_NOT_INITIALIZED)
1027 }
1028 e := C.Logout(c.ctx, C.CK_SESSION_HANDLE(sh))
1029 return toError(e)
1030 }
1031
1032 // CreateObject creates a new object.
1033 func (c *Ctx) CreateObject(sh SessionHandle, temp []*Attribute) (ObjectHandle, error) {
1034 var obj C.CK_OBJECT_HANDLE
1035 arena, t, tcount := cAttributeList(temp)
1036 defer arena.Free()
1037 e := C.CreateObject(c.ctx, C.CK_SESSION_HANDLE(sh), t, tcount, C.CK_OBJECT_HANDLE_PTR(&obj))
1038 e1 := toError(e)
1039 if e1 == nil {
1040 return ObjectHandle(obj), nil
1041 }
1042 return 0, e1
1043 }
1044
1045 // CopyObject copies an object, creating a new object for the copy.
1046 func (c *Ctx) CopyObject(sh SessionHandle, o ObjectHandle, temp []*Attribute) (ObjectHandle, error) {
1047 var obj C.CK_OBJECT_HANDLE
1048 arena, t, tcount := cAttributeList(temp)
1049 defer arena.Free()
1050
1051 e := C.CopyObject(c.ctx, C.CK_SESSION_HANDLE(sh), C.CK_OBJECT_HANDLE(o), t, tcount, C.CK_OBJECT_HANDLE_PTR(&obj))
1052 e1 := toError(e)
1053 if e1 == nil {
1054 return ObjectHandle(obj), nil
1055 }
1056 return 0, e1
1057 }
1058
1059 // DestroyObject destroys an object.
1060 func (c *Ctx) DestroyObject(sh SessionHandle, oh ObjectHandle) error {
1061 e := C.DestroyObject(c.ctx, C.CK_SESSION_HANDLE(sh), C.CK_OBJECT_HANDLE(oh))
1062 return toError(e)
1063 }
1064
1065 // GetObjectSize gets the size of an object in bytes.
1066 func (c *Ctx) GetObjectSize(sh SessionHandle, oh ObjectHandle) (uint, error) {
1067 var size C.CK_ULONG
1068 e := C.GetObjectSize(c.ctx, C.CK_SESSION_HANDLE(sh), C.CK_OBJECT_HANDLE(oh), &size)
1069 return uint(size), toError(e)
1070 }
1071
1072 // GetAttributeValue obtains the value of one or more object attributes.
1073 func (c *Ctx) GetAttributeValue(sh SessionHandle, o ObjectHandle, a []*Attribute) ([]*Attribute, error) {
1074 // copy the attribute list and make all the values nil, so that
1075 // the C function can (allocate) fill them in
1076 pa := make([]C.CK_ATTRIBUTE, len(a))
1077 for i := 0; i < len(a); i++ {
1078 pa[i]._type = C.CK_ATTRIBUTE_TYPE(a[i].Type)
1079 }
1080 e := C.GetAttributeValue(c.ctx, C.CK_SESSION_HANDLE(sh), C.CK_OBJECT_HANDLE(o), &pa[0], C.CK_ULONG(len(a)))
1081 if err := toError(e); err != nil {
1082 return nil, err
1083 }
1084 a1 := make([]*Attribute, len(a))
1085 for i, c := range pa {
1086 x := new(Attribute)
1087 x.Type = uint(c._type)
1088 if int(c.ulValueLen) != -1 {
1089 buf := unsafe.Pointer(C.getAttributePval(&c))
1090 x.Value = C.GoBytes(buf, C.int(c.ulValueLen))
1091 C.free(buf)
1092 }
1093 a1[i] = x
1094 }
1095 return a1, nil
1096 }
1097
1098 // SetAttributeValue modifies the value of one or more object attributes
1099 func (c *Ctx) SetAttributeValue(sh SessionHandle, o ObjectHandle, a []*Attribute) error {
1100 arena, pa, palen := cAttributeList(a)
1101 defer arena.Free()
1102 e := C.SetAttributeValue(c.ctx, C.CK_SESSION_HANDLE(sh), C.CK_OBJECT_HANDLE(o), pa, palen)
1103 return toError(e)
1104 }
1105
1106 // FindObjectsInit initializes a search for token and session
1107 // objects that match a template.
1108 func (c *Ctx) FindObjectsInit(sh SessionHandle, temp []*Attribute) error {
1109 arena, t, tcount := cAttributeList(temp)
1110 defer arena.Free()
1111 e := C.FindObjectsInit(c.ctx, C.CK_SESSION_HANDLE(sh), t, tcount)
1112 return toError(e)
1113 }
1114
1115 // FindObjects continues a search for token and session
1116 // objects that match a template, obtaining additional object
1117 // handles. Calling the function repeatedly may yield additional results until
1118 // an empty slice is returned.
1119 //
1120 // The returned boolean value is deprecated and should be ignored.
1121 func (c *Ctx) FindObjects(sh SessionHandle, max int) ([]ObjectHandle, bool, error) {
1122 var (
1123 objectList C.CK_OBJECT_HANDLE_PTR
1124 ulCount C.CK_ULONG
1125 )
1126 e := C.FindObjects(c.ctx, C.CK_SESSION_HANDLE(sh), &objectList, C.CK_ULONG(max), &ulCount)
1127 if toError(e) != nil {
1128 return nil, false, toError(e)
1129 }
1130 l := toList(C.CK_ULONG_PTR(unsafe.Pointer(objectList)), ulCount)
1131 // Make again a new list of the correct type.
1132 // This is copying data, but this is not an often used function.
1133 o := make([]ObjectHandle, len(l))
1134 for i, v := range l {
1135 o[i] = ObjectHandle(v)
1136 }
1137 return o, ulCount > C.CK_ULONG(max), nil
1138 }
1139
1140 // FindObjectsFinal finishes a search for token and session objects.
1141 func (c *Ctx) FindObjectsFinal(sh SessionHandle) error {
1142 e := C.FindObjectsFinal(c.ctx, C.CK_SESSION_HANDLE(sh))
1143 return toError(e)
1144 }
1145
1146 // EncryptInit initializes an encryption operation.
1147 func (c *Ctx) EncryptInit(sh SessionHandle, m []*Mechanism, o ObjectHandle) error {
1148 arena, mech := cMechanism(m)
1149 defer arena.Free()
1150 e := C.EncryptInit(c.ctx, C.CK_SESSION_HANDLE(sh), mech, C.CK_OBJECT_HANDLE(o))
1151 return toError(e)
1152 }
1153
1154 // Encrypt encrypts single-part data.
1155 func (c *Ctx) Encrypt(sh SessionHandle, message []byte) ([]byte, error) {
1156 var (
1157 enc C.CK_BYTE_PTR
1158 enclen C.CK_ULONG
1159 )
1160 e := C.Encrypt(c.ctx, C.CK_SESSION_HANDLE(sh), cMessage(message), C.CK_ULONG(len(message)), &enc, &enclen)
1161 if toError(e) != nil {
1162 return nil, toError(e)
1163 }
1164 s := C.GoBytes(unsafe.Pointer(enc), C.int(enclen))
1165 C.free(unsafe.Pointer(enc))
1166 return s, nil
1167 }
1168
1169 // EncryptUpdate continues a multiple-part encryption operation.
1170 func (c *Ctx) EncryptUpdate(sh SessionHandle, plain []byte) ([]byte, error) {
1171 var (
1172 part C.CK_BYTE_PTR
1173 partlen C.CK_ULONG
1174 )
1175 e := C.EncryptUpdate(c.ctx, C.CK_SESSION_HANDLE(sh), cMessage(plain), C.CK_ULONG(len(plain)), &part, &partlen)
1176 if toError(e) != nil {
1177 return nil, toError(e)
1178 }
1179 h := C.GoBytes(unsafe.Pointer(part), C.int(partlen))
1180 C.free(unsafe.Pointer(part))
1181 return h, nil
1182 }
1183
1184 // EncryptFinal finishes a multiple-part encryption operation.
1185 func (c *Ctx) EncryptFinal(sh SessionHandle) ([]byte, error) {
1186 var (
1187 enc C.CK_BYTE_PTR
1188 enclen C.CK_ULONG
1189 )
1190 e := C.EncryptFinal(c.ctx, C.CK_SESSION_HANDLE(sh), &enc, &enclen)
1191 if toError(e) != nil {
1192 return nil, toError(e)
1193 }
1194 h := C.GoBytes(unsafe.Pointer(enc), C.int(enclen))
1195 C.free(unsafe.Pointer(enc))
1196 return h, nil
1197 }
1198
1199 // DecryptInit initializes a decryption operation.
1200 func (c *Ctx) DecryptInit(sh SessionHandle, m []*Mechanism, o ObjectHandle) error {
1201 arena, mech := cMechanism(m)
1202 defer arena.Free()
1203 e := C.DecryptInit(c.ctx, C.CK_SESSION_HANDLE(sh), mech, C.CK_OBJECT_HANDLE(o))
1204 return toError(e)
1205 }
1206
1207 // Decrypt decrypts encrypted data in a single part.
1208 func (c *Ctx) Decrypt(sh SessionHandle, cipher []byte) ([]byte, error) {
1209 var (
1210 plain C.CK_BYTE_PTR
1211 plainlen C.CK_ULONG
1212 )
1213 e := C.Decrypt(c.ctx, C.CK_SESSION_HANDLE(sh), cMessage(cipher), C.CK_ULONG(len(cipher)), &plain, &plainlen)
1214 if toError(e) != nil {
1215 return nil, toError(e)
1216 }
1217 s := C.GoBytes(unsafe.Pointer(plain), C.int(plainlen))
1218 C.free(unsafe.Pointer(plain))
1219 return s, nil
1220 }
1221
1222 // DecryptUpdate continues a multiple-part decryption operation.
1223 func (c *Ctx) DecryptUpdate(sh SessionHandle, cipher []byte) ([]byte, error) {
1224 var (
1225 part C.CK_BYTE_PTR
1226 partlen C.CK_ULONG
1227 )
1228 e := C.DecryptUpdate(c.ctx, C.CK_SESSION_HANDLE(sh), cMessage(cipher), C.CK_ULONG(len(cipher)), &part, &partlen)
1229 if toError(e) != nil {
1230 return nil, toError(e)
1231 }
1232 h := C.GoBytes(unsafe.Pointer(part), C.int(partlen))
1233 C.free(unsafe.Pointer(part))
1234 return h, nil
1235 }
1236
1237 // DecryptFinal finishes a multiple-part decryption operation.
1238 func (c *Ctx) DecryptFinal(sh SessionHandle) ([]byte, error) {
1239 var (
1240 plain C.CK_BYTE_PTR
1241 plainlen C.CK_ULONG
1242 )
1243 e := C.DecryptFinal(c.ctx, C.CK_SESSION_HANDLE(sh), &plain, &plainlen)
1244 if toError(e) != nil {
1245 return nil, toError(e)
1246 }
1247 h := C.GoBytes(unsafe.Pointer(plain), C.int(plainlen))
1248 C.free(unsafe.Pointer(plain))
1249 return h, nil
1250 }
1251
1252 // DigestInit initializes a message-digesting operation.
1253 func (c *Ctx) DigestInit(sh SessionHandle, m []*Mechanism) error {
1254 arena, mech := cMechanism(m)
1255 defer arena.Free()
1256 e := C.DigestInit(c.ctx, C.CK_SESSION_HANDLE(sh), mech)
1257 return toError(e)
1258 }
1259
1260 // Digest digests message in a single part.
1261 func (c *Ctx) Digest(sh SessionHandle, message []byte) ([]byte, error) {
1262 var (
1263 hash C.CK_BYTE_PTR
1264 hashlen C.CK_ULONG
1265 )
1266 e := C.Digest(c.ctx, C.CK_SESSION_HANDLE(sh), cMessage(message), C.CK_ULONG(len(message)), &hash, &hashlen)
1267 if toError(e) != nil {
1268 return nil, toError(e)
1269 }
1270 h := C.GoBytes(unsafe.Pointer(hash), C.int(hashlen))
1271 C.free(unsafe.Pointer(hash))
1272 return h, nil
1273 }
1274
1275 // DigestUpdate continues a multiple-part message-digesting operation.
1276 func (c *Ctx) DigestUpdate(sh SessionHandle, message []byte) error {
1277 e := C.DigestUpdate(c.ctx, C.CK_SESSION_HANDLE(sh), cMessage(message), C.CK_ULONG(len(message)))
1278 if toError(e) != nil {
1279 return toError(e)
1280 }
1281 return nil
1282 }
1283
1284 // DigestKey continues a multi-part message-digesting
1285 // operation, by digesting the value of a secret key as part of
1286 // the data already digested.
1287 func (c *Ctx) DigestKey(sh SessionHandle, key ObjectHandle) error {
1288 e := C.DigestKey(c.ctx, C.CK_SESSION_HANDLE(sh), C.CK_OBJECT_HANDLE(key))
1289 if toError(e) != nil {
1290 return toError(e)
1291 }
1292 return nil
1293 }
1294
1295 // DigestFinal finishes a multiple-part message-digesting operation.
1296 func (c *Ctx) DigestFinal(sh SessionHandle) ([]byte, error) {
1297 var (
1298 hash C.CK_BYTE_PTR
1299 hashlen C.CK_ULONG
1300 )
1301 e := C.DigestFinal(c.ctx, C.CK_SESSION_HANDLE(sh), &hash, &hashlen)
1302 if toError(e) != nil {
1303 return nil, toError(e)
1304 }
1305 h := C.GoBytes(unsafe.Pointer(hash), C.int(hashlen))
1306 C.free(unsafe.Pointer(hash))
1307 return h, nil
1308 }
1309
1310 // SignInit initializes a signature (private key encryption)
1311 // operation, where the signature is (will be) an appendix to
1312 // the data, and plaintext cannot be recovered from the signature.
1313 func (c *Ctx) SignInit(sh SessionHandle, m []*Mechanism, o ObjectHandle) error {
1314 arena, mech := cMechanism(m)
1315 defer arena.Free()
1316 e := C.SignInit(c.ctx, C.CK_SESSION_HANDLE(sh), mech, C.CK_OBJECT_HANDLE(o))
1317 return toError(e)
1318 }
1319
1320 // Sign signs (encrypts with private key) data in a single part, where the signature
1321 // is (will be) an appendix to the data, and plaintext cannot be recovered from the signature.
1322 func (c *Ctx) Sign(sh SessionHandle, message []byte) ([]byte, error) {
1323 var (
1324 sig C.CK_BYTE_PTR
1325 siglen C.CK_ULONG
1326 )
1327 e := C.Sign(c.ctx, C.CK_SESSION_HANDLE(sh), cMessage(message), C.CK_ULONG(len(message)), &sig, &siglen)
1328 if toError(e) != nil {
1329 return nil, toError(e)
1330 }
1331 s := C.GoBytes(unsafe.Pointer(sig), C.int(siglen))
1332 C.free(unsafe.Pointer(sig))
1333 return s, nil
1334 }
1335
1336 // SignUpdate continues a multiple-part signature operation,
1337 // where the signature is (will be) an appendix to the data,
1338 // and plaintext cannot be recovered from the signature.
1339 func (c *Ctx) SignUpdate(sh SessionHandle, message []byte) error {
1340 e := C.SignUpdate(c.ctx, C.CK_SESSION_HANDLE(sh), cMessage(message), C.CK_ULONG(len(message)))
1341 return toError(e)
1342 }
1343
1344 // SignFinal finishes a multiple-part signature operation returning the signature.
1345 func (c *Ctx) SignFinal(sh SessionHandle) ([]byte, error) {
1346 var (
1347 sig C.CK_BYTE_PTR
1348 siglen C.CK_ULONG
1349 )
1350 e := C.SignFinal(c.ctx, C.CK_SESSION_HANDLE(sh), &sig, &siglen)
1351 if toError(e) != nil {
1352 return nil, toError(e)
1353 }
1354 h := C.GoBytes(unsafe.Pointer(sig), C.int(siglen))
1355 C.free(unsafe.Pointer(sig))
1356 return h, nil
1357 }
1358
1359 // SignRecoverInit initializes a signature operation, where the data can be recovered from the signature.
1360 func (c *Ctx) SignRecoverInit(sh SessionHandle, m []*Mechanism, key ObjectHandle) error {
1361 arena, mech := cMechanism(m)
1362 defer arena.Free()
1363 e := C.SignRecoverInit(c.ctx, C.CK_SESSION_HANDLE(sh), mech, C.CK_OBJECT_HANDLE(key))
1364 return toError(e)
1365 }
1366
1367 // SignRecover signs data in a single operation, where the data can be recovered from the signature.
1368 func (c *Ctx) SignRecover(sh SessionHandle, data []byte) ([]byte, error) {
1369 var (
1370 sig C.CK_BYTE_PTR
1371 siglen C.CK_ULONG
1372 )
1373 e := C.SignRecover(c.ctx, C.CK_SESSION_HANDLE(sh), cMessage(data), C.CK_ULONG(len(data)), &sig, &siglen)
1374 if toError(e) != nil {
1375 return nil, toError(e)
1376 }
1377 h := C.GoBytes(unsafe.Pointer(sig), C.int(siglen))
1378 C.free(unsafe.Pointer(sig))
1379 return h, nil
1380 }
1381
1382 // VerifyInit initializes a verification operation, where the
1383 // signature is an appendix to the data, and plaintext cannot
1384 // be recovered from the signature (e.g. DSA).
1385 func (c *Ctx) VerifyInit(sh SessionHandle, m []*Mechanism, key ObjectHandle) error {
1386 arena, mech := cMechanism(m)
1387 defer arena.Free()
1388 e := C.VerifyInit(c.ctx, C.CK_SESSION_HANDLE(sh), mech, C.CK_OBJECT_HANDLE(key))
1389 return toError(e)
1390 }
1391
1392 // Verify verifies a signature in a single-part operation,
1393 // where the signature is an appendix to the data, and plaintext
1394 // cannot be recovered from the signature.
1395 func (c *Ctx) Verify(sh SessionHandle, data []byte, signature []byte) error {
1396 e := C.Verify(c.ctx, C.CK_SESSION_HANDLE(sh), cMessage(data), C.CK_ULONG(len(data)), cMessage(signature), C.CK_ULONG(len(signature)))
1397 return toError(e)
1398 }
1399
1400 // VerifyUpdate continues a multiple-part verification
1401 // operation, where the signature is an appendix to the data,
1402 // and plaintext cannot be recovered from the signature.
1403 func (c *Ctx) VerifyUpdate(sh SessionHandle, part []byte) error {
1404 e := C.VerifyUpdate(c.ctx, C.CK_SESSION_HANDLE(sh), cMessage(part), C.CK_ULONG(len(part)))
1405 return toError(e)
1406 }
1407
1408 // VerifyFinal finishes a multiple-part verification
1409 // operation, checking the signature.
1410 func (c *Ctx) VerifyFinal(sh SessionHandle, signature []byte) error {
1411 e := C.VerifyFinal(c.ctx, C.CK_SESSION_HANDLE(sh), cMessage(signature), C.CK_ULONG(len(signature)))
1412 return toError(e)
1413 }
1414
1415 // VerifyRecoverInit initializes a signature verification
1416 // operation, where the data is recovered from the signature.
1417 func (c *Ctx) VerifyRecoverInit(sh SessionHandle, m []*Mechanism, key ObjectHandle) error {
1418 arena, mech := cMechanism(m)
1419 defer arena.Free()
1420 e := C.VerifyRecoverInit(c.ctx, C.CK_SESSION_HANDLE(sh), mech, C.CK_OBJECT_HANDLE(key))
1421 return toError(e)
1422 }
1423
1424 // VerifyRecover verifies a signature in a single-part
1425 // operation, where the data is recovered from the signature.
1426 func (c *Ctx) VerifyRecover(sh SessionHandle, signature []byte) ([]byte, error) {
1427 var (
1428 data C.CK_BYTE_PTR
1429 datalen C.CK_ULONG
1430 )
1431 e := C.DecryptVerifyUpdate(c.ctx, C.CK_SESSION_HANDLE(sh), cMessage(signature), C.CK_ULONG(len(signature)), &data, &datalen)
1432 if toError(e) != nil {
1433 return nil, toError(e)
1434 }
1435 h := C.GoBytes(unsafe.Pointer(data), C.int(datalen))
1436 C.free(unsafe.Pointer(data))
1437 return h, nil
1438 }
1439
1440 // DigestEncryptUpdate continues a multiple-part digesting and encryption operation.
1441 func (c *Ctx) DigestEncryptUpdate(sh SessionHandle, part []byte) ([]byte, error) {
1442 var (
1443 enc C.CK_BYTE_PTR
1444 enclen C.CK_ULONG
1445 )
1446 e := C.DigestEncryptUpdate(c.ctx, C.CK_SESSION_HANDLE(sh), cMessage(part), C.CK_ULONG(len(part)), &enc, &enclen)
1447 if toError(e) != nil {
1448 return nil, toError(e)
1449 }
1450 h := C.GoBytes(unsafe.Pointer(enc), C.int(enclen))
1451 C.free(unsafe.Pointer(enc))
1452 return h, nil
1453 }
1454
1455 // DecryptDigestUpdate continues a multiple-part decryption and digesting operation.
1456 func (c *Ctx) DecryptDigestUpdate(sh SessionHandle, cipher []byte) ([]byte, error) {
1457 var (
1458 part C.CK_BYTE_PTR
1459 partlen C.CK_ULONG
1460 )
1461 e := C.DecryptDigestUpdate(c.ctx, C.CK_SESSION_HANDLE(sh), cMessage(cipher), C.CK_ULONG(len(cipher)), &part, &partlen)
1462 if toError(e) != nil {
1463 return nil, toError(e)
1464 }
1465 h := C.GoBytes(unsafe.Pointer(part), C.int(partlen))
1466 C.free(unsafe.Pointer(part))
1467 return h, nil
1468 }
1469
1470 // SignEncryptUpdate continues a multiple-part signing and encryption operation.
1471 func (c *Ctx) SignEncryptUpdate(sh SessionHandle, part []byte) ([]byte, error) {
1472 var (
1473 enc C.CK_BYTE_PTR
1474 enclen C.CK_ULONG
1475 )
1476 e := C.SignEncryptUpdate(c.ctx, C.CK_SESSION_HANDLE(sh), cMessage(part), C.CK_ULONG(len(part)), &enc, &enclen)
1477 if toError(e) != nil {
1478 return nil, toError(e)
1479 }
1480 h := C.GoBytes(unsafe.Pointer(enc), C.int(enclen))
1481 C.free(unsafe.Pointer(enc))
1482 return h, nil
1483 }
1484
1485 // DecryptVerifyUpdate continues a multiple-part decryption and verify operation.
1486 func (c *Ctx) DecryptVerifyUpdate(sh SessionHandle, cipher []byte) ([]byte, error) {
1487 var (
1488 part C.CK_BYTE_PTR
1489 partlen C.CK_ULONG
1490 )
1491 e := C.DecryptVerifyUpdate(c.ctx, C.CK_SESSION_HANDLE(sh), cMessage(cipher), C.CK_ULONG(len(cipher)), &part, &partlen)
1492 if toError(e) != nil {
1493 return nil, toError(e)
1494 }
1495 h := C.GoBytes(unsafe.Pointer(part), C.int(partlen))
1496 C.free(unsafe.Pointer(part))
1497 return h, nil
1498 }
1499
1500 // GenerateKey generates a secret key, creating a new key object.
1501 func (c *Ctx) GenerateKey(sh SessionHandle, m []*Mechanism, temp []*Attribute) (ObjectHandle, error) {
1502 var key C.CK_OBJECT_HANDLE
1503 attrarena, t, tcount := cAttributeList(temp)
1504 defer attrarena.Free()
1505 mecharena, mech := cMechanism(m)
1506 defer mecharena.Free()
1507 e := C.GenerateKey(c.ctx, C.CK_SESSION_HANDLE(sh), mech, t, tcount, C.CK_OBJECT_HANDLE_PTR(&key))
1508 e1 := toError(e)
1509 if e1 == nil {
1510 return ObjectHandle(key), nil
1511 }
1512 return 0, e1
1513 }
1514
1515 // GenerateKeyPair generates a public-key/private-key pair creating new key objects.
1516 func (c *Ctx) GenerateKeyPair(sh SessionHandle, m []*Mechanism, public, private []*Attribute) (ObjectHandle, ObjectHandle, error) {
1517 var (
1518 pubkey C.CK_OBJECT_HANDLE
1519 privkey C.CK_OBJECT_HANDLE
1520 )
1521 pubarena, pub, pubcount := cAttributeList(public)
1522 defer pubarena.Free()
1523 privarena, priv, privcount := cAttributeList(private)
1524 defer privarena.Free()
1525 mecharena, mech := cMechanism(m)
1526 defer mecharena.Free()
1527 e := C.GenerateKeyPair(c.ctx, C.CK_SESSION_HANDLE(sh), mech, pub, pubcount, priv, privcount, C.CK_OBJECT_HANDLE_PTR(&pubkey), C.CK_OBJECT_HANDLE_PTR(&privkey))
1528 e1 := toError(e)
1529 if e1 == nil {
1530 return ObjectHandle(pubkey), ObjectHandle(privkey), nil
1531 }
1532 return 0, 0, e1
1533 }
1534
1535 // WrapKey wraps (i.e., encrypts) a key.
1536 func (c *Ctx) WrapKey(sh SessionHandle, m []*Mechanism, wrappingkey, key ObjectHandle) ([]byte, error) {
1537 var (
1538 wrappedkey C.CK_BYTE_PTR
1539 wrappedkeylen C.CK_ULONG
1540 )
1541 arena, mech := cMechanism(m)
1542 defer arena.Free()
1543 e := C.WrapKey(c.ctx, C.CK_SESSION_HANDLE(sh), mech, C.CK_OBJECT_HANDLE(wrappingkey), C.CK_OBJECT_HANDLE(key), &wrappedkey, &wrappedkeylen)
1544 if toError(e) != nil {
1545 return nil, toError(e)
1546 }
1547 h := C.GoBytes(unsafe.Pointer(wrappedkey), C.int(wrappedkeylen))
1548 C.free(unsafe.Pointer(wrappedkey))
1549 return h, nil
1550 }
1551
1552 // UnwrapKey unwraps (decrypts) a wrapped key, creating a new key object.
1553 func (c *Ctx) UnwrapKey(sh SessionHandle, m []*Mechanism, unwrappingkey ObjectHandle, wrappedkey []byte, a []*Attribute) (ObjectHandle, error) {
1554 var key C.CK_OBJECT_HANDLE
1555 attrarena, ac, aclen := cAttributeList(a)
1556 defer attrarena.Free()
1557 mecharena, mech := cMechanism(m)
1558 defer mecharena.Free()
1559 e := C.UnwrapKey(c.ctx, C.CK_SESSION_HANDLE(sh), mech, C.CK_OBJECT_HANDLE(unwrappingkey), C.CK_BYTE_PTR(unsafe.Pointer(&wrappedkey[0])), C.CK_ULONG(len(wrappedkey)), ac, aclen, &key)
1560 return ObjectHandle(key), toError(e)
1561 }
1562
1563 // DeriveKey derives a key from a base key, creating a new key object.
1564 func (c *Ctx) DeriveKey(sh SessionHandle, m []*Mechanism, basekey ObjectHandle, a []*Attribute) (ObjectHandle, error) {
1565 var key C.CK_OBJECT_HANDLE
1566 attrarena, ac, aclen := cAttributeList(a)
1567 defer attrarena.Free()
1568 mecharena, mech := cMechanism(m)
1569 defer mecharena.Free()
1570 e := C.DeriveKey(c.ctx, C.CK_SESSION_HANDLE(sh), mech, C.CK_OBJECT_HANDLE(basekey), ac, aclen, &key)
1571 return ObjectHandle(key), toError(e)
1572 }
1573
1574 // SeedRandom mixes additional seed material into the token's
1575 // random number generator.
1576 func (c *Ctx) SeedRandom(sh SessionHandle, seed []byte) error {
1577 e := C.SeedRandom(c.ctx, C.CK_SESSION_HANDLE(sh), C.CK_BYTE_PTR(unsafe.Pointer(&seed[0])), C.CK_ULONG(len(seed)))
1578 return toError(e)
1579 }
1580
1581 // GenerateRandom generates random data.
1582 func (c *Ctx) GenerateRandom(sh SessionHandle, length int) ([]byte, error) {
1583 var rand C.CK_BYTE_PTR
1584 e := C.GenerateRandom(c.ctx, C.CK_SESSION_HANDLE(sh), &rand, C.CK_ULONG(length))
1585 if toError(e) != nil {
1586 return nil, toError(e)
1587 }
1588 h := C.GoBytes(unsafe.Pointer(rand), C.int(length))
1589 C.free(unsafe.Pointer(rand))
1590 return h, nil
1591 }
1592
1593 // WaitForSlotEvent returns a channel which returns a slot event
1594 // (token insertion, removal, etc.) when it occurs.
1595 func (c *Ctx) WaitForSlotEvent(flags uint) chan SlotEvent {
1596 sl := make(chan SlotEvent, 1) // hold one element
1597 go c.waitForSlotEventHelper(flags, sl)
1598 return sl
1599 }
1600
1601 func (c *Ctx) waitForSlotEventHelper(f uint, sl chan SlotEvent) {
1602 var slotID C.CK_ULONG
1603 C.WaitForSlotEvent(c.ctx, C.CK_FLAGS(f), &slotID)
1604 sl <- SlotEvent{uint(slotID)}
1605 close(sl) // TODO(miek): Sending and then closing ...?
1606 }