test-slotevent.c

The following example shows how to use the slot event interface.

#include "../../config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if !defined(ENABLE_PKCS11H_SLOTEVENT)
int main () {
printf ("!win32, certificate, enum and crypto engine interfaces should be enabled for this test");
exit (0);
return 0;
}
#else
#if defined(_WIN32)
#include <windows.h>
#else
#include <unistd.h>
#endif
#include <pkcs11-helper-1.0/pkcs11h-core.h>
static
void
fatal (const char * const m, CK_RV rv) {
fprintf (stderr, "%s - %08lu - %s\n", m, rv, pkcs11h_getMessage (rv));
exit (1);
}
static
void
mypause (const char * const m) {
char temp[10];
fprintf (stdout, "%s", m);
fflush (stdout);
fgets (temp, sizeof (temp), stdin);
}
static
void
_pkcs11h_hooks_log (
IN void * const global_data,
IN unsigned flags,
IN const char * const format,
IN va_list args
) {
vfprintf (stdout, format, args);
fprintf (stdout, "\n");
fflush (stdout);
}
static
void
_pkcs11h_hooks_slotevent (
IN void * const global_data
) {
printf ("slotevent\n");
}
int main () {
CK_RV rv;
printf ("Initializing pkcs11-helper\n");
if ((rv = pkcs11h_initialize ()) != CKR_OK) {
fatal ("pkcs11h_initialize failed", rv);
}
printf ("Registering pkcs11-helper hooks\n");
if ((rv = pkcs11h_setLogHook (_pkcs11h_hooks_log, NULL)) != CKR_OK) {
fatal ("pkcs11h_setLogHook failed", rv);
}
pkcs11h_setLogLevel (TEST_LOG_LEVEL);
if ((rv = pkcs11h_setSlotEventHook (_pkcs11h_hooks_slotevent, NULL)) != CKR_OK) {
fatal ("pkcs11h_setSlotEventHook failed", rv);
}
printf ("Adding provider '%s' as auto\n", TEST_PROVIDER);
if (
(rv = pkcs11h_addProvider (
TEST_PROVIDER,
TEST_PROVIDER,
FALSE,
PKCS11H_PRIVATEMODE_MASK_AUTO,
PKCS11H_SLOTEVENT_METHOD_AUTO,
0,
FALSE
)) != CKR_OK
) {
fatal ("pkcs11h_addProvider failed", rv);
}
printf ("Please remove and insert tokens (pause for 30 seconds)\n");
#if defined(_WIN32)
Sleep (30*1024);
#else
sleep (30);
#endif
if ((rv = pkcs11h_removeProvider (TEST_PROVIDER)) != CKR_OK) {
fatal ("pkcs11h_removeProvider failed", rv);
}
printf ("Adding provider '%s' as trigger\n", TEST_PROVIDER);
if (
(rv = pkcs11h_addProvider (
TEST_PROVIDER,
TEST_PROVIDER,
FALSE,
PKCS11H_PRIVATEMODE_MASK_AUTO,
PKCS11H_SLOTEVENT_METHOD_TRIGGER,
0,
FALSE
)) != CKR_OK
) {
fatal ("pkcs11h_addProvider failed", rv);
}
printf ("Please remove and insert tokens (pause for 30 seconds)\n");
#if defined(_WIN32)
Sleep (30*1024);
#else
sleep (30);
#endif
if ((rv = pkcs11h_removeProvider (TEST_PROVIDER)) != CKR_OK) {
fatal ("pkcs11h_removeProvider failed", rv);
}
printf ("Adding provider '%s' as poll\n", TEST_PROVIDER);
if (
(rv = pkcs11h_addProvider (
TEST_PROVIDER,
TEST_PROVIDER,
FALSE,
PKCS11H_PRIVATEMODE_MASK_AUTO,
PKCS11H_SLOTEVENT_METHOD_POLL,
0,
FALSE
)) != CKR_OK
) {
fatal ("pkcs11h_addProvider failed", rv);
}
printf ("Please remove and insert tokens (pause for 30 seconds)\n");
#if defined(_WIN32)
Sleep (30*1024);
#else
sleep (30);
#endif
if ((rv = pkcs11h_removeProvider (TEST_PROVIDER)) != CKR_OK) {
fatal ("pkcs11h_removeProvider failed", rv);
}
printf ("Adding provider '%s' as fetch\n", TEST_PROVIDER);
if (
(rv = pkcs11h_addProvider (
TEST_PROVIDER,
TEST_PROVIDER,
FALSE,
PKCS11H_PRIVATEMODE_MASK_AUTO,
PKCS11H_SLOTEVENT_METHOD_FETCH,
0,
FALSE
)) != CKR_OK
) {
fatal ("pkcs11h_addProvider failed", rv);
}
printf ("Please remove and insert tokens (pause for 30 seconds)\n");
#if defined(_WIN32)
Sleep (30*1024);
#else
sleep (30);
#endif
printf ("Terminating pkcs11-helper\n");
if ((rv = pkcs11h_terminate ()) != CKR_OK) {
fatal ("pkcs11h_terminate failed", rv);
}
exit (0);
return 0;
}
#endif
PKCS11H_PRIVATEMODE_MASK_AUTO
#define PKCS11H_PRIVATEMODE_MASK_AUTO
Definition pkcs11h-core.h:146
PKCS11H_SLOTEVENT_METHOD_TRIGGER
#define PKCS11H_SLOTEVENT_METHOD_TRIGGER
Definition pkcs11h-core.h:165
PKCS11H_SLOTEVENT_METHOD_FETCH
#define PKCS11H_SLOTEVENT_METHOD_FETCH
Definition pkcs11h-core.h:169
PKCS11H_SLOTEVENT_METHOD_POLL
#define PKCS11H_SLOTEVENT_METHOD_POLL
Definition pkcs11h-core.h:167
pkcs11h_getMessage
const char * pkcs11h_getMessage(IN const CK_RV rv)
Get message by return value.
pkcs11h_setSlotEventHook
CK_RV pkcs11h_setSlotEventHook(IN const pkcs11h_hook_slotevent_t hook, IN void *const global_data)
Set a slot event callback.
pkcs11h_terminate
CK_RV pkcs11h_terminate(void)
Terminate helper interface.
pkcs11h_setLogLevel
void pkcs11h_setLogLevel(IN const unsigned flags)
Set current log level of the helper.
pkcs11h_removeProvider
CK_RV pkcs11h_removeProvider(IN const char *const reference)
Delete a PKCS#11 provider.
pkcs11h_initialize
CK_RV pkcs11h_initialize(void)
Initialize helper interface.
pkcs11h_setLogHook
CK_RV pkcs11h_setLogHook(IN const pkcs11h_hook_log_t hook, IN void *const global_data)
Set a log callback.
pkcs11h_addProvider
CK_RV pkcs11h_addProvider(IN const char *const reference, IN const char *const provider_location, IN const PKCS11H_BOOL allow_protected_auth, IN const unsigned mask_private_mode, IN const unsigned slot_event_method, IN const unsigned slot_poll_interval, IN const PKCS11H_BOOL cert_is_private)
Register, configure and initialize a PKCS#11 provider.
pkcs11h-core.h
pkcs11-helper core.

pkcs11-helper, Copyright (C) Alon Bar-Lev <alon.barlev@gmail.com> |