1 /* $NetBSD: pk11_api.c,v 1.1.1.4 2014/12/10 03:34:45 christos Exp $ */
4 * Copyright (C) 2014 Internet Systems Consortium, Inc. ("ISC")
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
11 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
12 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
13 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
14 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
15 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16 * PERFORMANCE OF THIS SOFTWARE.
23 /* missing code for WIN32 */
33 #include <isc/stdio.h>
34 #include <isc/thread.h>
37 #include <pk11/pk11.h>
38 #include <pk11/internal.h>
40 #define HAVE_GETPASSPHRASE
43 getpassphrase(const char *prompt
) {
49 h
= GetStdHandle(STD_INPUT_HANDLE
);
50 fputs(prompt
, stderr
);
53 FlushConsoleInputBuffer(h
);
54 GetConsoleMode(h
, &mode
);
55 SetConsoleMode(h
, ENABLE_PROCESSED_INPUT
);
57 for (cnt
= 0; cnt
< sizeof(buf
) - 1; cnt
++)
59 ReadFile(h
, buf
+ cnt
, 1, &cc
, NULL
);
67 SetConsoleMode(h
, mode
);
75 static HINSTANCE hPK11
= NULL
;
78 pkcs_C_Initialize(CK_VOID_PTR pReserved
) {
80 const char *lib_name
= pk11_get_lib_name();
83 return (CKR_LIBRARY_ALREADY_INITIALIZED
);
86 return (CKR_LIBRARY_FAILED_TO_LOAD
);
87 /* Visual Studio convertion issue... */
91 hPK11
= LoadLibraryA(lib_name
);
94 return (CKR_LIBRARY_FAILED_TO_LOAD
);
95 sym
= (CK_C_Initialize
)GetProcAddress(hPK11
, "C_Initialize");
97 return (CKR_SYMBOL_RESOLUTION_FAILED
);
98 return (*sym
)(pReserved
);
102 pkcs_C_Finalize(CK_VOID_PTR pReserved
) {
107 return (CKR_LIBRARY_FAILED_TO_LOAD
);
108 sym
= (CK_C_Finalize
)GetProcAddress(hPK11
, "C_Finalize");
110 return (CKR_SYMBOL_RESOLUTION_FAILED
);
111 rv
= (*sym
)(pReserved
);
112 if ((rv
== CKR_OK
) && (FreeLibrary(hPK11
) == 0))
113 return (CKR_LIBRARY_FAILED_TO_LOAD
);
119 pkcs_C_GetSlotList(CK_BBOOL tokenPresent
,
120 CK_SLOT_ID_PTR pSlotList
,
121 CK_ULONG_PTR pulCount
)
123 static CK_C_GetSlotList sym
= NULL
;
126 return (CKR_LIBRARY_FAILED_TO_LOAD
);
128 sym
= (CK_C_GetSlotList
)GetProcAddress(hPK11
, "C_GetSlotList");
130 return (CKR_SYMBOL_RESOLUTION_FAILED
);
131 return (*sym
)(tokenPresent
, pSlotList
, pulCount
);
135 pkcs_C_GetTokenInfo(CK_SLOT_ID slotID
,
136 CK_TOKEN_INFO_PTR pInfo
)
138 static CK_C_GetTokenInfo sym
= NULL
;
141 return (CKR_LIBRARY_FAILED_TO_LOAD
);
143 sym
= (CK_C_GetTokenInfo
)GetProcAddress(hPK11
,
146 return (CKR_SYMBOL_RESOLUTION_FAILED
);
147 return (*sym
)(slotID
, pInfo
);
151 pkcs_C_GetMechanismInfo(CK_SLOT_ID slotID
,
152 CK_MECHANISM_TYPE type
,
153 CK_MECHANISM_INFO_PTR pInfo
)
155 static CK_C_GetMechanismInfo sym
= NULL
;
158 return (CKR_LIBRARY_FAILED_TO_LOAD
);
160 sym
= (CK_C_GetMechanismInfo
)GetProcAddress(hPK11
,
161 "C_GetMechanismInfo");
163 return (CKR_SYMBOL_RESOLUTION_FAILED
);
164 return (*sym
)(slotID
, type
, pInfo
);
168 pkcs_C_OpenSession(CK_SLOT_ID slotID
,
170 CK_VOID_PTR pApplication
,
171 CK_RV (*Notify
) (CK_SESSION_HANDLE hSession
,
172 CK_NOTIFICATION event
,
173 CK_VOID_PTR pApplication
),
174 CK_SESSION_HANDLE_PTR phSession
)
176 static CK_C_OpenSession sym
= NULL
;
179 hPK11
= LoadLibraryA(pk11_get_lib_name());
181 return (CKR_LIBRARY_FAILED_TO_LOAD
);
183 sym
= (CK_C_OpenSession
)GetProcAddress(hPK11
, "C_OpenSession");
185 return (CKR_SYMBOL_RESOLUTION_FAILED
);
186 return (*sym
)(slotID
, flags
, pApplication
, Notify
, phSession
);
190 pkcs_C_CloseSession(CK_SESSION_HANDLE hSession
) {
191 static CK_C_CloseSession sym
= NULL
;
194 return (CKR_LIBRARY_FAILED_TO_LOAD
);
196 sym
= (CK_C_CloseSession
)GetProcAddress(hPK11
,
199 return (CKR_SYMBOL_RESOLUTION_FAILED
);
200 return (*sym
)(hSession
);
204 pkcs_C_Login(CK_SESSION_HANDLE hSession
,
205 CK_USER_TYPE userType
,
209 static CK_C_Login sym
= NULL
;
212 return (CKR_LIBRARY_FAILED_TO_LOAD
);
214 sym
= (CK_C_Login
)GetProcAddress(hPK11
, "C_Login");
216 return (CKR_SYMBOL_RESOLUTION_FAILED
);
217 return (*sym
)(hSession
, userType
, pPin
, usPinLen
);
221 pkcs_C_Logout(CK_SESSION_HANDLE hSession
) {
222 static CK_C_Logout sym
= NULL
;
225 return (CKR_LIBRARY_FAILED_TO_LOAD
);
227 sym
= (CK_C_Logout
)GetProcAddress(hPK11
, "C_Logout");
229 return (CKR_SYMBOL_RESOLUTION_FAILED
);
230 return (*sym
)(hSession
);
234 pkcs_C_CreateObject(CK_SESSION_HANDLE hSession
,
235 CK_ATTRIBUTE_PTR pTemplate
,
237 CK_OBJECT_HANDLE_PTR phObject
)
239 static CK_C_CreateObject sym
= NULL
;
242 return (CKR_LIBRARY_FAILED_TO_LOAD
);
244 sym
= (CK_C_CreateObject
)GetProcAddress(hPK11
,
247 return (CKR_SYMBOL_RESOLUTION_FAILED
);
248 return (*sym
)(hSession
, pTemplate
, usCount
, phObject
);
252 pkcs_C_DestroyObject(CK_SESSION_HANDLE hSession
, CK_OBJECT_HANDLE hObject
) {
253 static CK_C_DestroyObject sym
= NULL
;
256 return (CKR_LIBRARY_FAILED_TO_LOAD
);
258 sym
= (CK_C_DestroyObject
)GetProcAddress(hPK11
,
261 return (CKR_SYMBOL_RESOLUTION_FAILED
);
262 return (*sym
)(hSession
, hObject
);
266 pkcs_C_GetAttributeValue(CK_SESSION_HANDLE hSession
,
267 CK_OBJECT_HANDLE hObject
,
268 CK_ATTRIBUTE_PTR pTemplate
,
271 static CK_C_GetAttributeValue sym
= NULL
;
274 return (CKR_LIBRARY_FAILED_TO_LOAD
);
276 sym
= (CK_C_GetAttributeValue
)GetProcAddress(hPK11
,
277 "C_GetAttributeValue");
279 return (CKR_SYMBOL_RESOLUTION_FAILED
);
280 return (*sym
)(hSession
, hObject
, pTemplate
, usCount
);
284 pkcs_C_SetAttributeValue(CK_SESSION_HANDLE hSession
,
285 CK_OBJECT_HANDLE hObject
,
286 CK_ATTRIBUTE_PTR pTemplate
,
289 static CK_C_SetAttributeValue sym
= NULL
;
292 return (CKR_LIBRARY_FAILED_TO_LOAD
);
294 sym
= (CK_C_SetAttributeValue
)GetProcAddress(hPK11
,
295 "C_SetAttributeValue");
297 return (CKR_SYMBOL_RESOLUTION_FAILED
);
298 return (*sym
)(hSession
, hObject
, pTemplate
, usCount
);
302 pkcs_C_FindObjectsInit(CK_SESSION_HANDLE hSession
,
303 CK_ATTRIBUTE_PTR pTemplate
,
306 static CK_C_FindObjectsInit sym
= NULL
;
309 return (CKR_LIBRARY_FAILED_TO_LOAD
);
311 sym
= (CK_C_FindObjectsInit
)GetProcAddress(hPK11
,
312 "C_FindObjectsInit");
314 return (CKR_SYMBOL_RESOLUTION_FAILED
);
315 return (*sym
)(hSession
, pTemplate
, usCount
);
319 pkcs_C_FindObjects(CK_SESSION_HANDLE hSession
,
320 CK_OBJECT_HANDLE_PTR phObject
,
321 CK_ULONG usMaxObjectCount
,
322 CK_ULONG_PTR pusObjectCount
)
324 static CK_C_FindObjects sym
= NULL
;
327 return (CKR_LIBRARY_FAILED_TO_LOAD
);
329 sym
= (CK_C_FindObjects
)GetProcAddress(hPK11
, "C_FindObjects");
331 return (CKR_SYMBOL_RESOLUTION_FAILED
);
332 return (*sym
)(hSession
, phObject
, usMaxObjectCount
, pusObjectCount
);
336 pkcs_C_FindObjectsFinal(CK_SESSION_HANDLE hSession
) {
337 static CK_C_FindObjectsFinal sym
= NULL
;
340 return (CKR_LIBRARY_FAILED_TO_LOAD
);
342 sym
= (CK_C_FindObjectsFinal
)GetProcAddress(hPK11
,
343 "C_FindObjectsFinal");
345 return (CKR_SYMBOL_RESOLUTION_FAILED
);
346 return (*sym
)(hSession
);
350 pkcs_C_EncryptInit(CK_SESSION_HANDLE hSession
,
351 CK_MECHANISM_PTR pMechanism
,
352 CK_OBJECT_HANDLE hKey
)
354 static CK_C_EncryptInit sym
= NULL
;
357 return (CKR_LIBRARY_FAILED_TO_LOAD
);
359 sym
= (CK_C_EncryptInit
)GetProcAddress(hPK11
, "C_EncryptInit");
361 return (CKR_SYMBOL_RESOLUTION_FAILED
);
362 return (*sym
)(hSession
, pMechanism
, hKey
);
366 pkcs_C_Encrypt(CK_SESSION_HANDLE hSession
,
369 CK_BYTE_PTR pEncryptedData
,
370 CK_ULONG_PTR pulEncryptedDataLen
)
372 static CK_C_Encrypt sym
= NULL
;
375 return (CKR_LIBRARY_FAILED_TO_LOAD
);
377 sym
= (CK_C_Encrypt
)GetProcAddress(hPK11
, "C_Encrypt");
379 return (CKR_SYMBOL_RESOLUTION_FAILED
);
380 return (*sym
)(hSession
, pData
, ulDataLen
,
381 pEncryptedData
, pulEncryptedDataLen
);
385 pkcs_C_DigestInit(CK_SESSION_HANDLE hSession
,
386 CK_MECHANISM_PTR pMechanism
)
388 static CK_C_DigestInit sym
= NULL
;
391 return (CKR_LIBRARY_FAILED_TO_LOAD
);
393 sym
= (CK_C_DigestInit
)GetProcAddress(hPK11
, "C_DigestInit");
395 return (CKR_SYMBOL_RESOLUTION_FAILED
);
396 return (*sym
)(hSession
, pMechanism
);
400 pkcs_C_DigestUpdate(CK_SESSION_HANDLE hSession
,
404 static CK_C_DigestUpdate sym
= NULL
;
407 return (CKR_LIBRARY_FAILED_TO_LOAD
);
409 sym
= (CK_C_DigestUpdate
)GetProcAddress(hPK11
,
412 return (CKR_SYMBOL_RESOLUTION_FAILED
);
413 return (*sym
)(hSession
, pPart
, ulPartLen
);
417 pkcs_C_DigestFinal(CK_SESSION_HANDLE hSession
,
419 CK_ULONG_PTR pulDigestLen
)
421 static CK_C_DigestFinal sym
= NULL
;
424 return (CKR_LIBRARY_FAILED_TO_LOAD
);
426 sym
= (CK_C_DigestFinal
)GetProcAddress(hPK11
, "C_DigestFinal");
428 return (CKR_SYMBOL_RESOLUTION_FAILED
);
429 return (*sym
)(hSession
, pDigest
, pulDigestLen
);
433 pkcs_C_SignInit(CK_SESSION_HANDLE hSession
,
434 CK_MECHANISM_PTR pMechanism
,
435 CK_OBJECT_HANDLE hKey
)
437 static CK_C_SignInit sym
= NULL
;
440 return (CKR_LIBRARY_FAILED_TO_LOAD
);
442 sym
= (CK_C_SignInit
)GetProcAddress(hPK11
, "C_SignInit");
444 return (CKR_SYMBOL_RESOLUTION_FAILED
);
445 return (*sym
)(hSession
, pMechanism
, hKey
);
449 pkcs_C_Sign(CK_SESSION_HANDLE hSession
,
452 CK_BYTE_PTR pSignature
,
453 CK_ULONG_PTR pulSignatureLen
)
455 static CK_C_Sign sym
= NULL
;
458 return (CKR_LIBRARY_FAILED_TO_LOAD
);
460 sym
= (CK_C_Sign
)GetProcAddress(hPK11
, "C_Sign");
462 return (CKR_SYMBOL_RESOLUTION_FAILED
);
463 return (*sym
)(hSession
, pData
, ulDataLen
, pSignature
, pulSignatureLen
);
467 pkcs_C_SignUpdate(CK_SESSION_HANDLE hSession
,
471 static CK_C_SignUpdate sym
= NULL
;
474 return (CKR_LIBRARY_FAILED_TO_LOAD
);
476 sym
= (CK_C_SignUpdate
)GetProcAddress(hPK11
, "C_SignUpdate");
478 return (CKR_SYMBOL_RESOLUTION_FAILED
);
479 return (*sym
)(hSession
, pPart
, ulPartLen
);
483 pkcs_C_SignFinal(CK_SESSION_HANDLE hSession
,
484 CK_BYTE_PTR pSignature
,
485 CK_ULONG_PTR pulSignatureLen
)
487 static CK_C_SignFinal sym
= NULL
;
490 return (CKR_LIBRARY_FAILED_TO_LOAD
);
492 sym
= (CK_C_SignFinal
)GetProcAddress(hPK11
, "C_SignFinal");
494 return (CKR_SYMBOL_RESOLUTION_FAILED
);
495 return (*sym
)(hSession
, pSignature
, pulSignatureLen
);
499 pkcs_C_VerifyInit(CK_SESSION_HANDLE hSession
,
500 CK_MECHANISM_PTR pMechanism
,
501 CK_OBJECT_HANDLE hKey
)
503 static CK_C_VerifyInit sym
= NULL
;
506 return (CKR_LIBRARY_FAILED_TO_LOAD
);
508 sym
= (CK_C_VerifyInit
)GetProcAddress(hPK11
, "C_VerifyInit");
510 return (CKR_SYMBOL_RESOLUTION_FAILED
);
511 return (*sym
)(hSession
, pMechanism
, hKey
);
515 pkcs_C_Verify(CK_SESSION_HANDLE hSession
,
518 CK_BYTE_PTR pSignature
,
519 CK_ULONG ulSignatureLen
)
521 static CK_C_Verify sym
= NULL
;
524 return (CKR_LIBRARY_FAILED_TO_LOAD
);
526 sym
= (CK_C_Verify
)GetProcAddress(hPK11
, "C_Verify");
528 return (CKR_SYMBOL_RESOLUTION_FAILED
);
529 return (*sym
)(hSession
, pData
, ulDataLen
, pSignature
, ulSignatureLen
);
533 pkcs_C_VerifyUpdate(CK_SESSION_HANDLE hSession
,
537 static CK_C_VerifyUpdate sym
= NULL
;
540 return (CKR_LIBRARY_FAILED_TO_LOAD
);
542 sym
= (CK_C_VerifyUpdate
)GetProcAddress(hPK11
,
545 return (CKR_SYMBOL_RESOLUTION_FAILED
);
546 return (*sym
)(hSession
, pPart
, ulPartLen
);
550 pkcs_C_VerifyFinal(CK_SESSION_HANDLE hSession
,
551 CK_BYTE_PTR pSignature
,
552 CK_ULONG ulSignatureLen
)
554 static CK_C_VerifyFinal sym
= NULL
;
557 return (CKR_LIBRARY_FAILED_TO_LOAD
);
559 sym
= (CK_C_VerifyFinal
)GetProcAddress(hPK11
, "C_VerifyFinal");
561 return (CKR_SYMBOL_RESOLUTION_FAILED
);
562 return (*sym
)(hSession
, pSignature
, ulSignatureLen
);
566 pkcs_C_GenerateKey(CK_SESSION_HANDLE hSession
,
567 CK_MECHANISM_PTR pMechanism
,
568 CK_ATTRIBUTE_PTR pTemplate
,
570 CK_OBJECT_HANDLE_PTR phKey
)
572 static CK_C_GenerateKey sym
= NULL
;
575 return (CKR_LIBRARY_FAILED_TO_LOAD
);
577 sym
= (CK_C_GenerateKey
)GetProcAddress(hPK11
, "C_GenerateKey");
579 return (CKR_SYMBOL_RESOLUTION_FAILED
);
580 return (*sym
)(hSession
, pMechanism
, pTemplate
, ulCount
, phKey
);
584 pkcs_C_GenerateKeyPair(CK_SESSION_HANDLE hSession
,
585 CK_MECHANISM_PTR pMechanism
,
586 CK_ATTRIBUTE_PTR pPublicKeyTemplate
,
587 CK_ULONG usPublicKeyAttributeCount
,
588 CK_ATTRIBUTE_PTR pPrivateKeyTemplate
,
589 CK_ULONG usPrivateKeyAttributeCount
,
590 CK_OBJECT_HANDLE_PTR phPrivateKey
,
591 CK_OBJECT_HANDLE_PTR phPublicKey
)
593 static CK_C_GenerateKeyPair sym
= NULL
;
596 return (CKR_LIBRARY_FAILED_TO_LOAD
);
598 sym
= (CK_C_GenerateKeyPair
)GetProcAddress(hPK11
,
599 "C_GenerateKeyPair");
601 return (CKR_SYMBOL_RESOLUTION_FAILED
);
602 return (*sym
)(hSession
,
605 usPublicKeyAttributeCount
,
607 usPrivateKeyAttributeCount
,
613 pkcs_C_DeriveKey(CK_SESSION_HANDLE hSession
,
614 CK_MECHANISM_PTR pMechanism
,
615 CK_OBJECT_HANDLE hBaseKey
,
616 CK_ATTRIBUTE_PTR pTemplate
,
617 CK_ULONG ulAttributeCount
,
618 CK_OBJECT_HANDLE_PTR phKey
)
620 static CK_C_DeriveKey sym
= NULL
;
623 return (CKR_LIBRARY_FAILED_TO_LOAD
);
625 sym
= (CK_C_DeriveKey
)GetProcAddress(hPK11
, "C_DeriveKey");
627 return (CKR_SYMBOL_RESOLUTION_FAILED
);
628 return (*sym
)(hSession
,
637 pkcs_C_SeedRandom(CK_SESSION_HANDLE hSession
,
641 static CK_C_SeedRandom sym
= NULL
;
644 return (CKR_LIBRARY_FAILED_TO_LOAD
);
646 sym
= (CK_C_SeedRandom
)GetProcAddress(hPK11
, "C_SeedRandom");
648 return (CKR_SYMBOL_RESOLUTION_FAILED
);
649 return (*sym
)(hSession
, pSeed
, ulSeedLen
);
653 pkcs_C_GenerateRandom(CK_SESSION_HANDLE hSession
,
654 CK_BYTE_PTR RandomData
,
655 CK_ULONG ulRandomLen
)
657 static CK_C_GenerateRandom sym
= NULL
;
660 return (CKR_LIBRARY_FAILED_TO_LOAD
);
662 sym
= (CK_C_GenerateRandom
)GetProcAddress(hPK11
,
665 return (CKR_SYMBOL_RESOLUTION_FAILED
);
666 return (*sym
)(hSession
, RandomData
, ulRandomLen
);