Blame usr/include/slotmgr.h

Packit 8681c6
/*
Packit 8681c6
 * COPYRIGHT (c) International Business Machines Corp. 2001-2017
Packit 8681c6
 *
Packit 8681c6
 * This program is provided under the terms of the Common Public License,
Packit 8681c6
 * version 1.0 (CPL-1.0). Any use, reproduction or distribution for this
Packit 8681c6
 * software constitutes recipient's acceptance of CPL-1.0 terms which can be
Packit 8681c6
 * found in the file LICENSE file or at
Packit 8681c6
 * https://opensource.org/licenses/cpl1.0.php
Packit 8681c6
 */
Packit 8681c6
Packit 8681c6
//
Packit 8681c6
//Slot Manager Daemon  Constants...
Packit 8681c6
//
Packit 8681c6
//
Packit 8681c6
Packit 8681c6
#include <stdint.h>
Packit 8681c6
#include <pkcs11types.h>
Packit 8681c6
#include <limits.h>
Packit 8681c6
#include <local_types.h>
Packit 8681c6
#include <pthread.h>
Packit 8681c6
#include <sys/mman.h>
Packit Service 8aa27d
#include <stdarg.h>
Packit Service 8aa27d
#include <stdio.h>
Packit 8681c6
Packit 8681c6
#include "local_types.h"
Packit 8681c6
Packit 8681c6
#ifndef _SLOTMGR_H
Packit 8681c6
#define _SLOTMGR_H
Packit 8681c6
Packit 8681c6
#define TOK_PATH  SBIN_PATH "/pkcsslotd"
Packit 8681c6
#define OCK_API_LOCK_FILE LOCKDIR_PATH "/LCK..APIlock"
Packit 8681c6
Packit 8681c6
#define SOCKET_FILE_PATH "/var/run/pkcsslotd.socket"
Packit 8681c6
Packit 8681c6
#define PID_FILE_PATH "/var/run/pkcsslotd.pid"
Packit 8681c6
#define OCK_CONFIG OCK_CONFDIR "/opencryptoki.conf"
Packit 8681c6
Packit 8681c6
#ifndef CK_BOOL
Packit 8681c6
#define CK_BOOL  CK_BBOOL
Packit 8681c6
#endif                          /* CK_BOOL */
Packit 8681c6
Packit 8681c6
#ifndef TEST_COND_VARS
Packit 8681c6
#define TEST_COND_VARS 0
Packit 8681c6
#endif                          /* TEST_COND_VARS */
Packit 8681c6
Packit 8681c6
#define NUMBER_SLOTS_MANAGED 1024
Packit 8681c6
#define NUMBER_PROCESSES_ALLOWED  1000
Packit 8681c6
Packit 8681c6
//
Packit 8681c6
// Per Process Data structure
Packit 8681c6
// one entry in the table is grabbed by each process
Packit 8681c6
// when it attaches to the shared memory and released
Packit 8681c6
// when the C_Finalize is called.
Packit 8681c6
Packit 8681c6
typedef struct {
Packit 8681c6
    pthread_mutex_t proc_mutex;
Packit 8681c6
    pthread_cond_t proc_slot_cond;
Packit 8681c6
Packit 8681c6
    CK_BOOL inuse;              // flag indicating if the entry is in use
Packit 8681c6
    pid_t proc_id;              /* This could also be used to indicate inuse.
Packit 8681c6
                                 * however we will actualy use it to provide
Packit 8681c6
                                 * a check for a bad process which did not
Packit 8681c6
                                 * C_finalize and remove itself properly.
Packit 8681c6
                                 */
Packit 8681c6
    uint32 slotmap;             /* Bit map of the slots with events App uses
Packit 8681c6
                                 * in the C_WaitForSlotEvent call
Packit 8681c6
                                 */
Packit 8681c6
Packit 8681c6
    uint8 blocking;             /* Flag to use if a thread is blocking on the
Packit 8681c6
                                 * condition variable Used by C_Finalize to
Packit 8681c6
                                 * wake up the
Packit 8681c6
                                 */
Packit 8681c6
Packit 8681c6
    uint8 error;                /* indication of an error causing the thread
Packit 8681c6
                                 * sleeping on the condition variable to wakeup.
Packit 8681c6
                                 */
Packit 8681c6
    uint32 slot_session_count[NUMBER_SLOTS_MANAGED];    /* Per process session
Packit 8681c6
                                                         * count for garbage
Packit 8681c6
                                                         * collection clean up
Packit 8681c6
                                                         * of the global
Packit 8681c6
                                                         * session count.
Packit 8681c6
                                                         */
Packit 8681c6
    time_t reg_time;            // Time application registered
Packit 8681c6
} Slot_Mgr_Proc_t;
Packit 8681c6
Packit 8681c6
Packit 8681c6
// Slot info structure which contains the PKCS11 CK_SLOT_INFO
Packit 8681c6
// as well as the local information
Packit 8681c6
typedef struct {
Packit 8681c6
    CK_SLOT_ID slot_number;
Packit 8681c6
    CK_BOOL present;
Packit 8681c6
    CK_SLOT_INFO pk_slot;
Packit 8681c6
    char dll_location[NAME_MAX + 1];    // location of slot management  DLL
Packit 8681c6
    char slot_init_fcn[NAME_MAX + 1];   /* function to call to initialize the
Packit 8681c6
                                         * token in the slot
Packit 8681c6
                                         */
Packit 8681c6
    LW_SHM_TYPE *shm_addr;      // token specific shm address
Packit 8681c6
} Slot_Info_t;
Packit 8681c6
Packit 8681c6
Packit 8681c6
#ifdef PKCS64
Packit 8681c6
Packit 8681c6
/*
Packit 8681c6
 * Constant size types and structures to allow 32-bit daemon to work with
Packit 8681c6
 * 64-bit libraries.
Packit 8681c6
 *
Packit 8681c6
 * Note - ulong long is 8 bytes for both 32-bit and 64-bit applications.
Packit 8681c6
 *
Packit 8681c6
 */
Packit 8681c6
Packit 8681c6
typedef signed long long pid_t_64;
Packit 8681c6
typedef unsigned long long time_t_64;
Packit 8681c6
typedef unsigned long long CK_SLOT_ID_64;
Packit 8681c6
typedef unsigned long long CK_FLAGS_64;
Packit 8681c6
Packit 8681c6
typedef struct CK_INFO_64 {
Packit 8681c6
    CK_VERSION cryptokiVersion; /* Cryptoki interface ver */
Packit 8681c6
    CK_CHAR manufacturerID[32]; /* blank padded */
Packit 8681c6
    CK_CHAR pad1[6];            /* pad for dword alignment */
Packit 8681c6
    CK_FLAGS_64 flags;          /* must be zero */
Packit 8681c6
Packit 8681c6
    /* libraryDescription and libraryVersion are new for v2.0 */
Packit 8681c6
    CK_CHAR libraryDescription[32];     /* blank padded */
Packit 8681c6
    CK_VERSION libraryVersion;  /* version of library */
Packit 8681c6
    CK_CHAR pad2[6];            /* pad for dword alignment */
Packit 8681c6
} CK_INFO_64;
Packit 8681c6
Packit 8681c6
typedef CK_INFO_64 CK_PTR CK_INFO_PTR_64;
Packit 8681c6
Packit 8681c6
typedef struct CK_SLOT_INFO_64 {
Packit 8681c6
    CK_CHAR slotDescription[64];        /* blank padded */
Packit 8681c6
    CK_CHAR manufacturerID[32]; /* blank padded */
Packit 8681c6
    CK_FLAGS_64 flags;
Packit 8681c6
Packit 8681c6
    /* hardwareVersion and firmwareVersion are new for v2.0 */
Packit 8681c6
    CK_VERSION hardwareVersion; /* version of hardware */
Packit 8681c6
    CK_VERSION firmwareVersion; /* version of firmware */
Packit 8681c6
    CK_CHAR pad[4];             /* pad for dword alignment */
Packit 8681c6
} CK_SLOT_INFO_64;
Packit 8681c6
Packit 8681c6
Packit 8681c6
typedef struct Slot_Mgr_Proc_t_64 {
Packit 8681c6
    // pthread_cond_t   proc_slot_cond;
Packit 8681c6
Packit 8681c6
    CK_BOOL inuse;              // flag indicating if the entry is in use
Packit 8681c6
    pid_t proc_id;              /* This could also be used to indicate inuse.
Packit 8681c6
                                 * however we will actualy use it to provide
Packit 8681c6
                                 * a check for a bad process which did not
Packit 8681c6
                                 * C_finalize and remove itself properly.
Packit 8681c6
                                 */
Packit 8681c6
    uint32 slotmap;             /* Bit map of the slots with events App uses
Packit 8681c6
                                 * this in the C_WaitForSlotEvent call
Packit 8681c6
                                 */
Packit 8681c6
Packit 8681c6
    uint8 blocking;             /* Flag to use if a thread is blocking on the
Packit 8681c6
                                 * condition variable Used by C_Finalize to
Packit 8681c6
                                 * wake up the
Packit 8681c6
                                 */
Packit 8681c6
Packit 8681c6
    uint8 error;                /* indication of an error causing the thread
Packit 8681c6
                                 * sleeping on the condition variable to wakeup.
Packit 8681c6
                                 */
Packit 8681c6
    uint32 slot_session_count[NUMBER_SLOTS_MANAGED];    /* Per process session
Packit 8681c6
                                                         * count for garbage
Packit 8681c6
                                                         * collection clean up
Packit 8681c6
                                                         * of the global
Packit 8681c6
                                                         * session count.
Packit 8681c6
                                                         */
Packit 8681c6
    time_t_64 reg_time;         // Time application registered
Packit 8681c6
} Slot_Mgr_Proc_t_64;
Packit 8681c6
Packit 8681c6
//
Packit 8681c6
// Shared Memory Region of Slot information
Packit 8681c6
//
Packit 8681c6
Packit 8681c6
// Slot info structure which contains the PKCS11 CK_SLOT_INFO
Packit 8681c6
// as well as the local information
Packit 8681c6
typedef struct {
Packit 8681c6
    CK_SLOT_ID_64 slot_number;
Packit 8681c6
    CK_BOOL present;
Packit 8681c6
    char pad1[7];               // pad for dword alignment
Packit 8681c6
    CK_SLOT_INFO_64 pk_slot;
Packit 8681c6
    char dll_location[NAME_MAX + 1];    // location of slot's  DLL
Packit 8681c6
    char confname[NAME_MAX + 1];        // token specific config file
Packit 8681c6
    char tokname[NAME_MAX + 1]; // token specific directory
Packit 8681c6
    LW_SHM_TYPE *shm_addr;      // token specific shm address
Packit 8681c6
    uint32_t version; // version: major<<16|minor
Packit 8681c6
} Slot_Info_t_64;
Packit 8681c6
Packit 8681c6
typedef Slot_Info_t_64 SLOT_INFO;
Packit 8681c6
Packit 8681c6
typedef struct {
Packit 8681c6
Packit 8681c6
    /* Information that the API calls will use. */
Packit 8681c6
    uint32 slot_global_sessions[NUMBER_SLOTS_MANAGED];
Packit 8681c6
    Slot_Mgr_Proc_t_64 proc_table[NUMBER_PROCESSES_ALLOWED];
Packit 8681c6
} Slot_Mgr_Shr_t;
Packit 8681c6
Packit 8681c6
typedef struct {
Packit 8681c6
    uint8 num_slots;
Packit 8681c6
    CK_INFO_64 ck_info;
Packit 8681c6
    Slot_Info_t_64 slot_info[NUMBER_SLOTS_MANAGED];
Packit 8681c6
} Slot_Mgr_Socket_t;
Packit 8681c6
Packit 8681c6
#else                           // PKCS64
Packit 8681c6
Packit 8681c6
typedef struct {
Packit 8681c6
    /* Information that the API calls will use. */
Packit 8681c6
    uint32 slot_global_sessions[NUMBER_SLOTS_MANAGED];
Packit 8681c6
    Slot_Mgr_Proc_t proc_table[NUMBER_PROCESSES_ALLOWED];
Packit 8681c6
} Slot_Mgr_Shr_t;
Packit 8681c6
Packit 8681c6
typedef struct {
Packit 8681c6
    uint8 num_slots;
Packit 8681c6
    CK_INFO ck_info;
Packit 8681c6
    Slot_Info_t slot_info[NUMBER_SLOTS_MANAGED];
Packit 8681c6
} Slot_Mgr_Socket_t;
Packit 8681c6
Packit 8681c6
typedef Slot_Info_t SLOT_INFO;
Packit 8681c6
Packit 8681c6
#endif                          // PKCS64
Packit 8681c6
Packit 8681c6
Packit 8681c6
// Loging type constants
Packit 8681c6
//
Packit 8681c6
#define ERROR 1
Packit 8681c6
#define INFO  2
Packit 8681c6
Packit 8681c6
Packit 8681c6
//  Call to populate the shared memory
Packit 8681c6
#define STR "01234567890123456789012345678901"
Packit 8681c6
#define MFG "IBM                             "
Packit 8681c6
#define LIB "openCryptoki                    "
Packit 8681c6
Packit 8681c6
#ifndef CRYPTOKI_API_MAJOR_V
Packit Service 8aa27d
#define CRYPTOKI_API_MAJOR_V 0x3
Packit 8681c6
#endif
Packit 8681c6
Packit 8681c6
#ifndef CRYPTOKI_API_MINOR_V
Packit Service 8aa27d
#define CRYPTOKI_API_MINOR_V 0x0
Packit 8681c6
#endif
Packit 8681c6
Packit 8681c6
#define LIB_MAJOR_V 1
Packit 8681c6
#define LIB_MINOR_V 4
Packit 8681c6
Packit 8681c6
#define RESTART_SYS_CALLS 1
Packit 8681c6
Packit Service 8aa27d
#if defined(__GNUC__) || defined(__clang__)
Packit Service 8aa27d
__attribute__((__format__ (__printf__, 3, 4)))
Packit Service 8aa27d
#endif
Packit Service 8aa27d
static inline int ock_snprintf(char *buf, size_t buflen, const char *fmt, ...)
Packit Service 8aa27d
{
Packit Service 8aa27d
    va_list ap;
Packit Service 8aa27d
    int n;
Packit Service 8aa27d
Packit Service 8aa27d
    va_start(ap, fmt);
Packit Service 8aa27d
    n = vsnprintf(buf, buflen, fmt, ap);
Packit Service 8aa27d
    va_end(ap);
Packit Service 8aa27d
Packit Service 8aa27d
    if (n < 0 || (size_t)n >= buflen)
Packit Service 8aa27d
        return -1;
Packit Service 8aa27d
Packit Service 8aa27d
    return 0;
Packit Service 8aa27d
}
Packit Service 8aa27d
Packit Service 8aa27d
Packit 8681c6
#endif                          /* _SLOTMGR_H */