summaryrefslogtreecommitdiffstats
path: root/lib/isc/unix/pk11_api.c
blob: bbc55afe2e4ce50ec90d092e5c592cdbc74bfe11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
/*
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * See the COPYRIGHT file distributed with this work for additional
 * information regarding copyright ownership.
 */


/*! \file */

#include <config.h>

#include <string.h>
#include <dlfcn.h>

#include <isc/log.h>
#include <isc/mem.h>
#include <isc/once.h>
#include <isc/print.h>
#include <isc/stdio.h>
#include <isc/thread.h>
#include <isc/util.h>

#include <pkcs11/cryptoki.h>
#include <pkcs11/pkcs11.h>

#define KEEP_PKCS11_NAMES
#include <pk11/pk11.h>
#include <pk11/internal.h>

static void *hPK11 = NULL;
static char loaderrmsg[1024];

CK_RV
pkcs_C_Initialize(CK_VOID_PTR pReserved) {
	CK_C_Initialize sym;

	if (hPK11 != NULL)
		return (CKR_LIBRARY_ALREADY_INITIALIZED);

	hPK11 = dlopen(pk11_get_lib_name(), RTLD_NOW);

	if (hPK11 == NULL) {
		snprintf(loaderrmsg, sizeof(loaderrmsg),
			 "dlopen(\"%s\") failed: %s\n",
			 pk11_get_lib_name(), dlerror());
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	}
	sym = (CK_C_Initialize)dlsym(hPK11, "C_Initialize");
	if (sym == NULL)
		return (CKR_SYMBOL_RESOLUTION_FAILED);
	return (*sym)(pReserved);
}

char *pk11_get_load_error_message(void) {
	return (loaderrmsg);
}

CK_RV
pkcs_C_Finalize(CK_VOID_PTR pReserved) {
	CK_C_Finalize sym;
	CK_RV rv;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	sym = (CK_C_Finalize)dlsym(hPK11, "C_Finalize");
	if (sym == NULL)
		return (CKR_SYMBOL_RESOLUTION_FAILED);
	rv = (*sym)(pReserved);
	if ((rv == CKR_OK) && (dlclose(hPK11) != 0))
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	hPK11 = NULL;
	return (rv);
}

CK_RV
pkcs_C_GetSlotList(CK_BBOOL tokenPresent, CK_SLOT_ID_PTR pSlotList,
		   CK_ULONG_PTR pulCount)
{
	static CK_C_GetSlotList sym = NULL;
	static void *pPK11 = NULL;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if ((sym == NULL) || (hPK11 != pPK11)) {
		pPK11 = hPK11;
		sym = (CK_C_GetSlotList)dlsym(hPK11, "C_GetSlotList");
	}
	if (sym == NULL)
		return (CKR_SYMBOL_RESOLUTION_FAILED);
	return (*sym)(tokenPresent, pSlotList, pulCount);
}

CK_RV
pkcs_C_GetTokenInfo(CK_SLOT_ID slotID, CK_TOKEN_INFO_PTR pInfo) {
	static CK_C_GetTokenInfo sym = NULL;
	static void *pPK11 = NULL;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if ((sym == NULL) || (hPK11 != pPK11)) {
		pPK11 = hPK11;
		sym = (CK_C_GetTokenInfo)dlsym(hPK11, "C_GetTokenInfo");
	}
	if (sym == NULL)
		return (CKR_SYMBOL_RESOLUTION_FAILED);
	return (*sym)(slotID, pInfo);
}

CK_RV
pkcs_C_GetMechanismInfo(CK_SLOT_ID slotID, CK_MECHANISM_TYPE type,
			CK_MECHANISM_INFO_PTR pInfo)
{
	static CK_C_GetMechanismInfo sym = NULL;
	static void *pPK11 = NULL;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if ((sym == NULL) || (hPK11 != pPK11)) {
		pPK11 = hPK11;
		sym = (CK_C_GetMechanismInfo)dlsym(hPK11,
						   "C_GetMechanismInfo");
	}
	if (sym == NULL)
		return (CKR_SYMBOL_RESOLUTION_FAILED);
	return (*sym)(slotID, type, pInfo);
}

CK_RV
pkcs_C_OpenSession(CK_SLOT_ID slotID, CK_FLAGS flags,
		   CK_VOID_PTR pApplication,
		   CK_RV  (*Notify) (CK_SESSION_HANDLE hSession,
				     CK_NOTIFICATION event,
				     CK_VOID_PTR pApplication),
		   CK_SESSION_HANDLE_PTR phSession)
{
	static CK_C_OpenSession sym = NULL;
	static void *pPK11 = NULL;

	if (hPK11 == NULL)
		hPK11 = dlopen(pk11_get_lib_name(), RTLD_NOW);
	if (hPK11 == NULL) {
		snprintf(loaderrmsg, sizeof(loaderrmsg),
			 "dlopen(\"%s\") failed: %s\n",
			 pk11_get_lib_name(), dlerror());
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	}
	if ((sym == NULL) || (hPK11 != pPK11)) {
		pPK11 = hPK11;
		sym = (CK_C_OpenSession)dlsym(hPK11, "C_OpenSession");
	}
	if (sym == NULL)
		return (CKR_SYMBOL_RESOLUTION_FAILED);
	return (*sym)(slotID, flags, pApplication, Notify, phSession);
}

CK_RV
pkcs_C_CloseSession(CK_SESSION_HANDLE hSession) {
	static CK_C_CloseSession sym = NULL;
	static void *pPK11 = NULL;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if ((sym == NULL) || (hPK11 != pPK11)) {
		pPK11 = hPK11;
		sym = (CK_C_CloseSession)dlsym(hPK11, "C_CloseSession");
	}
	if (sym == NULL)
		return (CKR_SYMBOL_RESOLUTION_FAILED);
	return (*sym)(hSession);
}

CK_RV
pkcs_C_Login(CK_SESSION_HANDLE hSession, CK_USER_TYPE userType,
	     CK_CHAR_PTR pPin, CK_ULONG usPinLen)
{
	static CK_C_Login sym = NULL;
	static void *pPK11 = NULL;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if ((sym == NULL) || (hPK11 != pPK11)) {
		pPK11 = hPK11;
		sym = (CK_C_Login)dlsym(hPK11, "C_Login");
	}
	if (sym == NULL)
		return (CKR_SYMBOL_RESOLUTION_FAILED);
	return (*sym)(hSession, userType, pPin, usPinLen);
}

CK_RV
pkcs_C_Logout(CK_SESSION_HANDLE hSession) {
	static CK_C_Logout sym = NULL;
	static void *pPK11 = NULL;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if ((sym == NULL) || (hPK11 != pPK11)) {
		pPK11 = hPK11;
		sym = (CK_C_Logout)dlsym(hPK11, "C_Logout");
	}
	if (sym == NULL)
		return (CKR_SYMBOL_RESOLUTION_FAILED);
	return (*sym)(hSession);
}

CK_RV
pkcs_C_CreateObject(CK_SESSION_HANDLE hSession, CK_ATTRIBUTE_PTR pTemplate,
		    CK_ULONG usCount, CK_OBJECT_HANDLE_PTR phObject)
{
	static CK_C_CreateObject sym = NULL;
	static void *pPK11 = NULL;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if ((sym == NULL) || (hPK11 != pPK11)) {
		pPK11 = hPK11;
		sym = (CK_C_CreateObject)dlsym(hPK11, "C_CreateObject");
	}
	if (sym == NULL)
		return (CKR_SYMBOL_RESOLUTION_FAILED);
	return (*sym)(hSession, pTemplate, usCount, phObject);
}

CK_RV
pkcs_C_DestroyObject(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject) {
	static CK_C_DestroyObject sym = NULL;
	static void *pPK11 = NULL;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if ((sym == NULL) || (hPK11 != pPK11)) {
		pPK11 = hPK11;
		sym = (CK_C_DestroyObject)dlsym(hPK11, "C_DestroyObject");
	}
	if (sym == NULL)
		return (CKR_SYMBOL_RESOLUTION_FAILED);
	return (*sym)(hSession, hObject);
}

CK_RV
pkcs_C_GetAttributeValue(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject,
			 CK_ATTRIBUTE_PTR pTemplate, CK_ULONG usCount)
{
	static CK_C_GetAttributeValue sym = NULL;
	static void *pPK11 = NULL;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if ((sym == NULL) || (hPK11 != pPK11)) {
		pPK11 = hPK11;
		sym = (CK_C_GetAttributeValue)dlsym(hPK11,
						    "C_GetAttributeValue");
	}
	if (sym == NULL)
		return (CKR_SYMBOL_RESOLUTION_FAILED);
	return (*sym)(hSession, hObject, pTemplate, usCount);
}

CK_RV
pkcs_C_SetAttributeValue(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject,
			 CK_ATTRIBUTE_PTR pTemplate, CK_ULONG usCount)
{
	static CK_C_SetAttributeValue sym = NULL;
	static void *pPK11 = NULL;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if ((sym == NULL) || (hPK11 != pPK11)) {
		pPK11 = hPK11;
		sym = (CK_C_SetAttributeValue)dlsym(hPK11,
						    "C_SetAttributeValue");
	}
	if (sym == NULL)
		return (CKR_SYMBOL_RESOLUTION_FAILED);
	return (*sym)(hSession, hObject, pTemplate, usCount);
}

CK_RV
pkcs_C_FindObjectsInit(CK_SESSION_HANDLE hSession, CK_ATTRIBUTE_PTR pTemplate,
		       CK_ULONG usCount)
{
	static CK_C_FindObjectsInit sym = NULL;
	static void *pPK11 = NULL;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if ((sym == NULL) || (hPK11 != pPK11)) {
		pPK11 = hPK11;
		sym = (CK_C_FindObjectsInit)dlsym(hPK11, "C_FindObjectsInit");
	}
	if (sym == NULL)
		return (CKR_SYMBOL_RESOLUTION_FAILED);
	return (*sym)(hSession, pTemplate, usCount);
}

CK_RV
pkcs_C_FindObjects(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE_PTR phObject,
		   CK_ULONG usMaxObjectCount, CK_ULONG_PTR pusObjectCount)
{
	static CK_C_FindObjects sym = NULL;
	static void *pPK11 = NULL;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if ((sym == NULL) || (hPK11 != pPK11)) {
		pPK11 = hPK11;
		sym = (CK_C_FindObjects)dlsym(hPK11, "C_FindObjects");
	}
	if (sym == NULL)
		return (CKR_SYMBOL_RESOLUTION_FAILED);
	return (*sym)(hSession, phObject, usMaxObjectCount, pusObjectCount);
}

CK_RV
pkcs_C_FindObjectsFinal(CK_SESSION_HANDLE hSession)
{
	static CK_C_FindObjectsFinal sym = NULL;
	static void *pPK11 = NULL;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if ((sym == NULL) || (hPK11 != pPK11)) {
		pPK11 = hPK11;
		sym = (CK_C_FindObjectsFinal)dlsym(hPK11,
						   "C_FindObjectsFinal");
	}
	if (sym == NULL)
		return (CKR_SYMBOL_RESOLUTION_FAILED);
	return (*sym)(hSession);
}

CK_RV
pkcs_C_EncryptInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
		   CK_OBJECT_HANDLE hKey)
{
	static CK_C_EncryptInit sym = NULL;
	static void *pPK11 = NULL;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if ((sym == NULL) || (hPK11 != pPK11)) {
		pPK11 = hPK11;
		sym = (CK_C_EncryptInit)dlsym(hPK11, "C_EncryptInit");
	}
	if (sym == NULL)
		return (CKR_SYMBOL_RESOLUTION_FAILED);
	return (*sym)(hSession, pMechanism, hKey);
}

CK_RV
pkcs_C_Encrypt(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData,
	       CK_ULONG ulDataLen, CK_BYTE_PTR pEncryptedData,
	       CK_ULONG_PTR pulEncryptedDataLen)
{
	static CK_C_Encrypt sym = NULL;
	static void *pPK11 = NULL;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if ((sym == NULL) || (hPK11 != pPK11)) {
		pPK11 = hPK11;
		sym = (CK_C_Encrypt)dlsym(hPK11, "C_Encrypt");
	}
	if (sym == NULL)
		return (CKR_SYMBOL_RESOLUTION_FAILED);
	return (*sym)(hSession, pData, ulDataLen,
		      pEncryptedData, pulEncryptedDataLen);
}

CK_RV
pkcs_C_DigestInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism) {
	static CK_C_DigestInit sym = NULL;
	static void *pPK11 = NULL;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if ((sym == NULL) || (hPK11 != pPK11)) {
		pPK11 = hPK11;
		sym = (CK_C_DigestInit)dlsym(hPK11, "C_DigestInit");
	}
	if (sym == NULL)
		return (CKR_SYMBOL_RESOLUTION_FAILED);
	return (*sym)(hSession, pMechanism);
}

CK_RV
pkcs_C_DigestUpdate(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart,
		    CK_ULONG ulPartLen)
{
	static CK_C_DigestUpdate sym = NULL;
	static void *pPK11 = NULL;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if ((sym == NULL) || (hPK11 != pPK11)) {
		pPK11 = hPK11;
		sym = (CK_C_DigestUpdate)dlsym(hPK11, "C_DigestUpdate");
	}
	if (sym == NULL)
		return (CKR_SYMBOL_RESOLUTION_FAILED);
	return (*sym)(hSession, pPart, ulPartLen);
}

CK_RV
pkcs_C_DigestFinal(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pDigest,
		   CK_ULONG_PTR pulDigestLen)
{
	static CK_C_DigestFinal sym = NULL;
	static void *pPK11 = NULL;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if ((sym == NULL) || (hPK11 != pPK11)) {
		pPK11 = hPK11;
		sym = (CK_C_DigestFinal)dlsym(hPK11, "C_DigestFinal");
	}
	if (sym == NULL)
		return (CKR_SYMBOL_RESOLUTION_FAILED);
	return (*sym)(hSession, pDigest, pulDigestLen);
}

CK_RV
pkcs_C_SignInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
		CK_OBJECT_HANDLE hKey)
{
	static CK_C_SignInit sym = NULL;
	static void *pPK11 = NULL;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if ((sym == NULL) || (hPK11 != pPK11)) {
		pPK11 = hPK11;
		sym = (CK_C_SignInit)dlsym(hPK11, "C_SignInit");
	}
	if (sym == NULL)
		return (CKR_SYMBOL_RESOLUTION_FAILED);
	return (*sym)(hSession, pMechanism, hKey);
}

CK_RV
pkcs_C_Sign(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData,
	    CK_ULONG ulDataLen, CK_BYTE_PTR pSignature,
	    CK_ULONG_PTR pulSignatureLen)
{
	static CK_C_Sign sym = NULL;
	static void *pPK11 = NULL;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if ((sym == NULL) || (hPK11 != pPK11)) {
		pPK11 = hPK11;
		sym = (CK_C_Sign)dlsym(hPK11, "C_Sign");
	}
	if (sym == NULL)
		return (CKR_SYMBOL_RESOLUTION_FAILED);
	return (*sym)(hSession, pData, ulDataLen, pSignature, pulSignatureLen);
}

CK_RV
pkcs_C_SignUpdate(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart,
		  CK_ULONG ulPartLen)
{
	static CK_C_SignUpdate sym = NULL;
	static void *pPK11 = NULL;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if ((sym == NULL) || (hPK11 != pPK11)) {
		pPK11 = hPK11;
		sym = (CK_C_SignUpdate)dlsym(hPK11, "C_SignUpdate");
	}
	if (sym == NULL)
		return (CKR_SYMBOL_RESOLUTION_FAILED);
	return (*sym)(hSession, pPart, ulPartLen);
}

CK_RV
pkcs_C_SignFinal(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pSignature,
		 CK_ULONG_PTR pulSignatureLen)
{
	static CK_C_SignFinal sym = NULL;
	static void *pPK11 = NULL;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if ((sym == NULL) || (hPK11 != pPK11)) {
		pPK11 = hPK11;
		sym = (CK_C_SignFinal)dlsym(hPK11, "C_SignFinal");
	}
	if (sym == NULL)
		return (CKR_SYMBOL_RESOLUTION_FAILED);
	return (*sym)(hSession, pSignature, pulSignatureLen);
}

CK_RV
pkcs_C_VerifyInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
		  CK_OBJECT_HANDLE hKey)
{
	static CK_C_VerifyInit sym = NULL;
	static void *pPK11 = NULL;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if ((sym == NULL) || (hPK11 != pPK11)) {
		pPK11 = hPK11;
		sym = (CK_C_VerifyInit)dlsym(hPK11, "C_VerifyInit");
	}
	if (sym == NULL)
		return (CKR_SYMBOL_RESOLUTION_FAILED);
	return (*sym)(hSession, pMechanism, hKey);
}

CK_RV
pkcs_C_Verify(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData,
	      CK_ULONG ulDataLen, CK_BYTE_PTR pSignature,
	      CK_ULONG ulSignatureLen)
{
	static CK_C_Verify sym = NULL;
	static void *pPK11 = NULL;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if ((sym == NULL) || (hPK11 != pPK11)) {
		pPK11 = hPK11;
		sym = (CK_C_Verify)dlsym(hPK11, "C_Verify");
	}
	if (sym == NULL)
		return (CKR_SYMBOL_RESOLUTION_FAILED);
	return (*sym)(hSession, pData, ulDataLen, pSignature, ulSignatureLen);
}

CK_RV
pkcs_C_VerifyUpdate(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart,
		    CK_ULONG ulPartLen)
{
	static CK_C_VerifyUpdate sym = NULL;
	static void *pPK11 = NULL;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if ((sym == NULL) || (hPK11 != pPK11)) {
		pPK11 = hPK11;
		sym = (CK_C_VerifyUpdate)dlsym(hPK11, "C_VerifyUpdate");
	}
	if (sym == NULL)
		return (CKR_SYMBOL_RESOLUTION_FAILED);
	return (*sym)(hSession, pPart, ulPartLen);
}

CK_RV
pkcs_C_VerifyFinal(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pSignature,
		   CK_ULONG ulSignatureLen)
{
	static CK_C_VerifyFinal sym = NULL;
	static void *pPK11 = NULL;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if ((sym == NULL) || (hPK11 != pPK11)) {
		pPK11 = hPK11;
		sym = (CK_C_VerifyFinal)dlsym(hPK11, "C_VerifyFinal");
	}
	if (sym == NULL)
		return (CKR_SYMBOL_RESOLUTION_FAILED);
	return (*sym)(hSession, pSignature, ulSignatureLen);
}

CK_RV
pkcs_C_GenerateKey(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
		   CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount,
		   CK_OBJECT_HANDLE_PTR phKey)
{
	static CK_C_GenerateKey sym = NULL;
	static void *pPK11 = NULL;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if ((sym == NULL) || (hPK11 != pPK11)) {
		pPK11 = hPK11;
		sym = (CK_C_GenerateKey)dlsym(hPK11, "C_GenerateKey");
	}
	if (sym == NULL)
		return (CKR_SYMBOL_RESOLUTION_FAILED);
	return (*sym)(hSession, pMechanism, pTemplate, ulCount, phKey);
}

CK_RV
pkcs_C_GenerateKeyPair(CK_SESSION_HANDLE hSession,
		       CK_MECHANISM_PTR pMechanism,
		       CK_ATTRIBUTE_PTR pPublicKeyTemplate,
		       CK_ULONG usPublicKeyAttributeCount,
		       CK_ATTRIBUTE_PTR pPrivateKeyTemplate,
		       CK_ULONG usPrivateKeyAttributeCount,
		       CK_OBJECT_HANDLE_PTR phPrivateKey,
		       CK_OBJECT_HANDLE_PTR phPublicKey)
{
	static CK_C_GenerateKeyPair sym = NULL;
	static void *pPK11 = NULL;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if ((sym == NULL) || (hPK11 != pPK11)) {
		pPK11 = hPK11;
		sym = (CK_C_GenerateKeyPair)dlsym(hPK11, "C_GenerateKeyPair");
	}
	if (sym == NULL)
		return (CKR_SYMBOL_RESOLUTION_FAILED);
	return (*sym)(hSession,
		      pMechanism,
		      pPublicKeyTemplate,
		      usPublicKeyAttributeCount,
		      pPrivateKeyTemplate,
		      usPrivateKeyAttributeCount,
		      phPrivateKey,
		      phPublicKey);
}

CK_RV
pkcs_C_DeriveKey(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
		 CK_OBJECT_HANDLE hBaseKey, CK_ATTRIBUTE_PTR pTemplate,
		 CK_ULONG ulAttributeCount, CK_OBJECT_HANDLE_PTR phKey)
{
	static CK_C_DeriveKey sym = NULL;
	static void *pPK11 = NULL;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if ((sym == NULL) || (hPK11 != pPK11)) {
		pPK11 = hPK11;
		sym = (CK_C_DeriveKey)dlsym(hPK11, "C_DeriveKey");
	}
	if (sym == NULL)
		return (CKR_SYMBOL_RESOLUTION_FAILED);
	return (*sym)(hSession,
		      pMechanism,
		      hBaseKey,
		      pTemplate,
		      ulAttributeCount,
		      phKey);
}

CK_RV
pkcs_C_SeedRandom(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pSeed,
		  CK_ULONG ulSeedLen)
{
	static CK_C_SeedRandom sym = NULL;
	static void *pPK11 = NULL;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if ((sym == NULL) || (hPK11 != pPK11)) {
		pPK11 = hPK11;
		sym = (CK_C_SeedRandom)dlsym(hPK11, "C_SeedRandom");
	}
	if (sym == NULL)
		return (CKR_SYMBOL_RESOLUTION_FAILED);
	return (*sym)(hSession, pSeed, ulSeedLen);
}

CK_RV
pkcs_C_GenerateRandom(CK_SESSION_HANDLE hSession, CK_BYTE_PTR RandomData,
		      CK_ULONG ulRandomLen)
{
	static CK_C_GenerateRandom sym = NULL;
	static void *pPK11 = NULL;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if ((sym == NULL) || (hPK11 != pPK11)) {
		pPK11 = hPK11;
		sym = (CK_C_GenerateRandom)dlsym(hPK11, "C_GenerateRandom");
	}
	if (sym == NULL)
		return (CKR_SYMBOL_RESOLUTION_FAILED);
	return (*sym)(hSession, RandomData, ulRandomLen);
}