summaryrefslogtreecommitdiffstats
path: root/lib/isc/win32/pk11_api.c
blob: 76e4abb4afb8beae8bfb89c6861eaabe211851cd (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
/*
 * 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 */

/* missing code for WIN32 */

#include <config.h>

#include <string.h>
#include <windows.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 <pk11/pk11.h>
#include <pk11/internal.h>

#define HAVE_GETPASSPHRASE

char *
getpassphrase(const char *prompt) {
	static char buf[128];
	HANDLE h;
	DWORD cc, mode;
	int cnt;

	h = GetStdHandle(STD_INPUT_HANDLE);
	fputs(prompt, stderr);
	fflush(stderr);
	fflush(stdout);
	FlushConsoleInputBuffer(h);
	GetConsoleMode(h, &mode);
	SetConsoleMode(h, ENABLE_PROCESSED_INPUT);

	for (cnt = 0; cnt < sizeof(buf) - 1; cnt++)
	{
		ReadFile(h, buf + cnt, 1, &cc, NULL);
		if (buf[cnt] == '\r')
			break;
		fputc('*', stdout);
		fflush(stderr);
		fflush(stdout);
	}

	SetConsoleMode(h, mode);
	buf[cnt] = '\0';
	fputs("\n", stderr);
	return (buf);
}

/* load PKCS11 DLL */

static HINSTANCE hPK11 = NULL;
static char loaderrmsg[1024];

CK_RV
pkcs_C_Initialize(CK_VOID_PTR pReserved) {
	CK_C_Initialize sym;
	const char *lib_name = pk11_get_lib_name();

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

	if (lib_name == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	/* Visual Studio convertion issue... */
	if (*lib_name == ' ')
		lib_name++;

	hPK11 = LoadLibraryA(lib_name);

	if (hPK11 == NULL) {
		const DWORD err = GetLastError();
		snprintf(loaderrmsg, sizeof(loaderrmsg),
			 "LoadLibraryA(\"%s\") failed with 0x%X\n",
			 lib_name, err);
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	}
	sym = (CK_C_Initialize)GetProcAddress(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)GetProcAddress(hPK11, "C_Finalize");
	if (sym == NULL)
		return (CKR_SYMBOL_RESOLUTION_FAILED);
	rv = (*sym)(pReserved);
	if ((rv == CKR_OK) && (FreeLibrary(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;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if (sym == NULL)
		sym = (CK_C_GetSlotList)GetProcAddress(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;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if (sym == NULL)
		sym = (CK_C_GetTokenInfo)GetProcAddress(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;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if (sym == NULL)
		sym = (CK_C_GetMechanismInfo)GetProcAddress(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;

	if (hPK11 == NULL)
		hPK11 = LoadLibraryA(pk11_get_lib_name());
	if (hPK11 == NULL) {
		const DWORD err = GetLastError();
		snprintf(loaderrmsg, sizeof(loaderrmsg),
			 "LoadLibraryA(\"%s\") failed with 0x%X\n",
			 pk11_get_lib_name(), err);
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	}
	if (sym == NULL)
		sym = (CK_C_OpenSession)GetProcAddress(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;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if (sym == NULL)
		sym = (CK_C_CloseSession)GetProcAddress(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;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if (sym == NULL)
		sym = (CK_C_Login)GetProcAddress(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;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if (sym == NULL)
		sym = (CK_C_Logout)GetProcAddress(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;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if (sym == NULL)
		sym = (CK_C_CreateObject)GetProcAddress(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;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if (sym == NULL)
		sym = (CK_C_DestroyObject)GetProcAddress(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;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if (sym == NULL)
		sym = (CK_C_GetAttributeValue)GetProcAddress(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;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if (sym == NULL)
		sym = (CK_C_SetAttributeValue)GetProcAddress(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;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if (sym == NULL)
		sym = (CK_C_FindObjectsInit)GetProcAddress(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;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if (sym == NULL)
		sym = (CK_C_FindObjects)GetProcAddress(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;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if (sym == NULL)
		sym = (CK_C_FindObjectsFinal)GetProcAddress(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;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if (sym == NULL)
		sym = (CK_C_EncryptInit)GetProcAddress(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;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if (sym == NULL)
		sym = (CK_C_Encrypt)GetProcAddress(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;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if (sym == NULL)
		sym = (CK_C_DigestInit)GetProcAddress(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;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if (sym == NULL)
		sym = (CK_C_DigestUpdate)GetProcAddress(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;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if (sym == NULL)
		sym = (CK_C_DigestFinal)GetProcAddress(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;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if (sym == NULL)
		sym = (CK_C_SignInit)GetProcAddress(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;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if (sym == NULL)
		sym = (CK_C_Sign)GetProcAddress(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;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if (sym == NULL)
		sym = (CK_C_SignUpdate)GetProcAddress(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;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if (sym == NULL)
		sym = (CK_C_SignFinal)GetProcAddress(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;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if (sym == NULL)
		sym = (CK_C_VerifyInit)GetProcAddress(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;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if (sym == NULL)
		sym = (CK_C_Verify)GetProcAddress(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;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if (sym == NULL)
		sym = (CK_C_VerifyUpdate)GetProcAddress(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;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if (sym == NULL)
		sym = (CK_C_VerifyFinal)GetProcAddress(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;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if (sym == NULL)
		sym = (CK_C_GenerateKey)GetProcAddress(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;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if (sym == NULL)
		sym = (CK_C_GenerateKeyPair)GetProcAddress(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;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if (sym == NULL)
		sym = (CK_C_DeriveKey)GetProcAddress(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;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if (sym == NULL)
		sym = (CK_C_SeedRandom)GetProcAddress(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;

	if (hPK11 == NULL)
		return (CKR_LIBRARY_FAILED_TO_LOAD);
	if (sym == NULL)
		sym = (CK_C_GenerateRandom)GetProcAddress(hPK11,
							  "C_GenerateRandom");
	if (sym == NULL)
		return (CKR_SYMBOL_RESOLUTION_FAILED);
	return (*sym)(hSession, RandomData, ulRandomLen);
}