📄 pkcs11.c
字号:
/****************************************************************************
* *
* cryptlib PKCS #11 Routines *
* Copyright Peter Gutmann 1998-2005 *
* *
****************************************************************************/
#include <stdlib.h>
#include <string.h>
#define PKC_CONTEXT /* Indicate that we're working with PKC context */
#if defined( INC_ALL )
#include "crypt.h"
#include "context.h"
#include "device.h"
#include "asn1.h"
#elif defined( INC_CHILD )
#include "../crypt.h"
#include "../context/context.h"
#include "device.h"
#include "../misc/asn1.h"
#else
#include "crypt.h"
#include "context/context.h"
#include "device/device.h"
#include "misc/asn1.h"
#endif /* Compiler-specific includes */
/* Before we can include the PKCS #11 headers we need to define a few OS-
specific things that are required by the headers */
#ifdef __WINDOWS__
#ifdef __WIN16__
#pragma pack( 1 ) /* Struct packing */
#define CK_PTR far * /* Pointer type */
#define CK_DEFINE_FUNCTION( returnType, name ) \
returnType __export _far _pascal name
#define CK_DECLARE_FUNCTION( returnType, name ) \
returnType __export _far _pascal name
#define CK_DECLARE_FUNCTION_POINTER( returnType, name ) \
returnType __export _far _pascal (* name)
#define CK_CALLBACK_FUNCTION( returnType, name ) \
returnType (_far _pascal * name)
#else
#pragma pack( push, cryptoki, 1 ) /* Struct packing */
#define CK_PTR * /* Pointer type */
#define CK_DEFINE_FUNCTION( returnType, name ) \
returnType __declspec( dllexport ) name
#define CK_DECLARE_FUNCTION( returnType, name ) \
returnType __declspec( dllimport ) name
#define CK_DECLARE_FUNCTION_POINTER( returnType, name ) \
returnType __declspec( dllimport ) (* name)
#define CK_CALLBACK_FUNCTION( returnType, name ) \
returnType (* name)
#endif /* Win16 vs.Win32 */
#else
#define CK_PTR * /* Pointer type */
#define CK_DEFINE_FUNCTION( returnType, name ) \
returnType name
#define CK_DECLARE_FUNCTION( returnType, name ) \
returnType name
#define CK_DECLARE_FUNCTION_POINTER( returnType, name ) \
returnType (* name)
#define CK_CALLBACK_FUNCTION( returnType, name ) \
returnType (* name)
#endif /* __WINDOWS__ */
#ifndef NULL_PTR
#define NULL_PTR NULL
#endif /* NULL_PTR */
#if defined( INC_ALL ) || defined( INC_CHILD )
#include "pkcs11.h"
#else
#include "device/pkcs11.h"
#endif /* Compiler-specific includes */
/* The max. number of drivers we can work with and the max.number of slots
per driver */
#define MAX_PKCS11_DRIVERS 5
#define MAX_PKCS11_SLOTS 16
/* The default slot to look for tokens in */
#define DEFAULT_SLOT 0
/* Occasionally we need to read things into host memory from a device in a
manner that can't be handled by a dynBuf since the data is coming from a
device rather than a cryptlib object. The following value defines the
maximum size of the on-stack buffer, if the data is larger than this we
dynamically allocate the buffer (this almost never occurs) */
#define MAX_BUFFER_SIZE 1024
/* Encryption contexts can store extra implementation-dependant parameters.
The following macro maps these generic parameter names to the PKCS #11
values */
#define paramKeyType param1
/* Prototypes for functions in cryptcap.c */
const void FAR_BSS *findCapabilityInfo( const void FAR_BSS *capabilityInfoPtr,
const CRYPT_ALGO_TYPE cryptAlgo );
/* Define the following to explicitly link each PKCS #11 function rather than
using C_GetFunctionList() */
/* #define USE_EXPLICIT_LINKING */
#ifdef USE_PKCS11
/****************************************************************************
* *
* Init/Shutdown Routines *
* *
****************************************************************************/
/* Whether the PKCS #11 library has been initialised or not, this is
initialised on demand the first time it's accessed */
static BOOLEAN pkcs11Initialised = FALSE;
#ifdef DYNAMIC_LOAD
/* Since we can be using multiple PKCS #11 drivers, we define an array of
them and access the appropriate one by name */
typedef struct {
char name[ 32 + 1 ]; /* Name of device */
INSTANCE_HANDLE hPKCS11; /* Handle to driver */
CK_FUNCTION_LIST_PTR functionListPtr; /* Driver access info */
#ifdef USE_EXPLICIT_LINKING
CK_C_CloseSession pC_CloseSession; /* Interface function pointers */
CK_C_CreateObject pC_CreateObject;
CK_C_Decrypt pC_Decrypt;
CK_C_DecryptInit pC_DecryptInit;
CK_C_DestroyObject pC_DestroyObject;
CK_C_Encrypt pC_Encrypt;
CK_C_EncryptInit pC_EncryptInit;
CK_C_Finalize pC_Finalize;
CK_C_FindObjects pC_FindObjects;
CK_C_FindObjectsFinal pC_FindObjectsFinal;
CK_C_FindObjectsInit pC_FindObjectsInit;
CK_C_GenerateKeyPair pC_GenerateKeyPair;
CK_C_GenerateRandom pC_GenerateRandom;
CK_C_GetAttributeValue pC_GetAttributeValue;
CK_C_GetMechanismInfo pC_GetMechanismInfo;
CK_C_GetSlotInfo pC_GetSlotInfo;
CK_C_GetSlotList pC_GetSlotList;
CK_C_GetTokenInfo pC_GetTokenInfo;
CK_C_InitPIN pC_InitPIN;
CK_C_InitToken pC_InitToken;
CK_C_Login pC_Login;
CK_C_Logout pC_Logout;
CK_C_OpenSession pC_OpenSession;
CK_C_SetAttributeValue pC_SetAttributeValue;
CK_C_SetPIN pC_SetPIN;
CK_C_Sign pC_Sign;
CK_C_SignInit pC_SignInit;
CK_C_UnwrapKey pC_UnwrapKey;
CK_C_Verify pC_Verify;
CK_C_VerifyInit pC_VerifyInit;
#endif /* USE_EXPLICIT_LINKING */
} PKCS11_DRIVER_INFO;
static PKCS11_DRIVER_INFO pkcs11InfoTbl[ MAX_PKCS11_DRIVERS ];
/* The use of dynamically bound function pointers vs.statically linked
functions requires a bit of sleight of hand since we can't give the
pointers the same names as prototyped functions. To get around this we
redefine the actual function names to the names of the pointers */
#ifdef USE_EXPLICIT_LINKING
#define C_CloseSession pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_CloseSession
#define C_CreateObject pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_CreateObject
#define C_Decrypt pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_Decrypt
#define C_DecryptInit pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_DecryptInit
#define C_DestroyObject pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_DestroyObject
#define C_Encrypt pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_Encrypt
#define C_EncryptInit pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_EncryptInit
#define C_Finalize pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_Finalize
#define C_FindObjects pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_FindObjects
#define C_FindObjectsFinal pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_FindObjectsFinal
#define C_FindObjectsInit pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_FindObjectsInit
#define C_GenerateKeyPair pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_GenerateKeyPair
#define C_GenerateRandom pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_GenerateRandom
#define C_GetAttributeValue pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_GetAttributeValue
#define C_GetMechanismInfo pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_GetMechanismInfo
#define C_GetSlotInfo pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_GetSlotInfo
#define C_GetSlotList pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_GetSlotList
#define C_GetTokenInfo pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_GetTokenInfo
#define C_Initialize pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_Initialize
#define C_InitPIN pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_InitPIN
#define C_InitToken pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_InitToken
#define C_Login pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_Login
#define C_Logout pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_Logout
#define C_OpenSession pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_OpenSession
#define C_SetAttributeValue pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_SetAttributeValue
#define C_SetPIN pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_SetPIN
#define C_Sign pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_Sign
#define C_SignInit pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_SignInit
#define C_UnwrapKey pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_UnwrapKey
#define C_Verify pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_Verify
#define C_VerifyInit pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_VerifyInit
#else
#define C_CloseSession ( ( CK_FUNCTION_LIST_PTR )( pkcs11Info->functionListPtr ) )->C_CloseSession
#define C_CreateObject ( ( CK_FUNCTION_LIST_PTR )( pkcs11Info->functionListPtr ) )->C_CreateObject
#define C_Decrypt ( ( CK_FUNCTION_LIST_PTR )( pkcs11Info->functionListPtr ) )->C_Decrypt
#define C_DecryptInit ( ( CK_FUNCTION_LIST_PTR )( pkcs11Info->functionListPtr ) )->C_DecryptInit
#define C_DeriveKey ( ( CK_FUNCTION_LIST_PTR )( pkcs11Info->functionListPtr ) )->C_DeriveKey
#define C_DestroyObject ( ( CK_FUNCTION_LIST_PTR )( pkcs11Info->functionListPtr ) )->C_DestroyObject
#define C_Encrypt ( ( CK_FUNCTION_LIST_PTR )( pkcs11Info->functionListPtr ) )->C_Encrypt
#define C_EncryptInit ( ( CK_FUNCTION_LIST_PTR )( pkcs11Info->functionListPtr ) )->C_EncryptInit
#define C_Finalize ( ( CK_FUNCTION_LIST_PTR )( pkcs11Info->functionListPtr ) )->C_Finalize
#define C_FindObjects ( ( CK_FUNCTION_LIST_PTR )( pkcs11Info->functionListPtr ) )->C_FindObjects
#define C_FindObjectsFinal ( ( CK_FUNCTION_LIST_PTR )( pkcs11Info->functionListPtr ) )->C_FindObjectsFinal
#define C_FindObjectsInit ( ( CK_FUNCTION_LIST_PTR )( pkcs11Info->functionListPtr ) )->C_FindObjectsInit
#define C_GenerateKeyPair ( ( CK_FUNCTION_LIST_PTR )( pkcs11Info->functionListPtr ) )->C_GenerateKeyPair
#define C_GenerateRandom ( ( CK_FUNCTION_LIST_PTR )( pkcs11Info->functionListPtr ) )->C_GenerateRandom
#define C_GetAttributeValue ( ( CK_FUNCTION_LIST_PTR )( pkcs11Info->functionListPtr ) )->C_GetAttributeValue
#define C_GetMechanismInfo ( ( CK_FUNCTION_LIST_PTR )( pkcs11Info->functionListPtr ) )->C_GetMechanismInfo
#define C_GetInfo ( ( CK_FUNCTION_LIST_PTR )( pkcs11Info->functionListPtr ) )->C_GetInfo
#define C_GetSlotInfo ( ( CK_FUNCTION_LIST_PTR )( pkcs11Info->functionListPtr ) )->C_GetSlotInfo
#define C_GetSlotList ( ( CK_FUNCTION_LIST_PTR )( pkcs11Info->functionListPtr ) )->C_GetSlotList
#define C_GetTokenInfo ( ( CK_FUNCTION_LIST_PTR )( pkcs11Info->functionListPtr ) )->C_GetTokenInfo
#define C_Initialize ( ( CK_FUNCTION_LIST_PTR )( pkcs11Info->functionListPtr ) )->C_Initialize
#define C_InitPIN ( ( CK_FUNCTION_LIST_PTR )( pkcs11Info->functionListPtr ) )->C_InitPIN
#define C_InitToken ( ( CK_FUNCTION_LIST_PTR )( pkcs11Info->functionListPtr ) )->C_InitToken
#define C_Login ( ( CK_FUNCTION_LIST_PTR )( pkcs11Info->functionListPtr ) )->C_Login
#define C_Logout ( ( CK_FUNCTION_LIST_PTR )( pkcs11Info->functionListPtr ) )->C_Logout
#define C_OpenSession ( ( CK_FUNCTION_LIST_PTR )( pkcs11Info->functionListPtr ) )->C_OpenSession
#define C_SetAttributeValue ( ( CK_FUNCTION_LIST_PTR )( pkcs11Info->functionListPtr ) )->C_SetAttributeValue
#define C_SetPIN ( ( CK_FUNCTION_LIST_PTR )( pkcs11Info->functionListPtr ) )->C_SetPIN
#define C_Sign ( ( CK_FUNCTION_LIST_PTR )( pkcs11Info->functionListPtr ) )->C_Sign
#define C_SignInit ( ( CK_FUNCTION_LIST_PTR )( pkcs11Info->functionListPtr ) )->C_SignInit
#define C_UnwrapKey ( ( CK_FUNCTION_LIST_PTR )( pkcs11Info->functionListPtr ) )->C_UnwrapKey
#define C_Verify ( ( CK_FUNCTION_LIST_PTR )( pkcs11Info->functionListPtr ) )->C_Verify
#define C_VerifyInit ( ( CK_FUNCTION_LIST_PTR )( pkcs11Info->functionListPtr ) )->C_VerifyInit
#endif /* USE_EXPLICIT_LINKING */
/* Dynamically load and unload any necessary PKCS #11 drivers */
static int loadPKCS11driver( PKCS11_DRIVER_INFO *pkcs11Info,
const char *driverName )
{
#ifdef USE_EXPLICIT_LINKING
CK_C_GetInfo pC_GetInfo;
CK_C_Initialize pC_Initialize;
#else
CK_C_GetFunctionList pC_GetFunctionList;
#endif /* USE_EXPLICIT_LINKING */
CK_INFO info;
CK_RV status;
#ifdef __WIN16__
UINT errorMode;
#endif /* __WIN16__ */
BOOLEAN isInitialised = FALSE;
int i = 32;
/* Obtain a handle to the device driver module */
#ifdef __WIN16__
errorMode = SetErrorMode( SEM_NOOPENFILEERRORBOX );
pkcs11Info->hPKCS11 = LoadLibrary( driverName );
SetErrorMode( errorMode );
if( pkcs11Info->hPKCS11 < HINSTANCE_ERROR )
{
pkcs11Info->hPKCS11 = NULL_HINSTANCE;
return( CRYPT_ERROR );
}
#else
if( ( pkcs11Info->hPKCS11 = DynamicLoad( driverName ) ) == NULL_INSTANCE )
return( CRYPT_ERROR );
#endif /* OS-specific dynamic load */
/* Now get pointers to the functions */
#ifdef USE_EXPLICIT_LINKING
pC_GetInfo = ( CK_C_GetInfo ) DynamicBind( pkcs11Info->hPKCS11, "C_GetInfo" );
pC_Initialize = ( CK_C_Initialize ) DynamicBind( pkcs11Info->hPKCS11, "C_Initialize" );
pkcs11Info->pC_CloseSession = ( CK_C_CloseSession ) DynamicBind( pkcs11Info->hPKCS11, "C_CloseSession" );
pkcs11Info->pC_CreateObject = ( CK_C_CreateObject ) DynamicBind( pkcs11Info->hPKCS11, "C_CreateObject" );
pkcs11Info->pC_Decrypt = ( CK_C_Decrypt ) DynamicBind( pkcs11Info->hPKCS11, "C_Decrypt" );
pkcs11Info->pC_DecryptInit = ( CK_C_DecryptInit ) DynamicBind( pkcs11Info->hPKCS11, "C_DecryptInit" );
pkcs11Info->pC_DestroyObject = ( CK_C_DestroyObject ) DynamicBind( pkcs11Info->hPKCS11, "C_DestroyObject" );
pkcs11Info->pC_Encrypt = ( CK_C_Encrypt ) DynamicBind( pkcs11Info->hPKCS11, "C_Encrypt" );
pkcs11Info->pC_EncryptInit = ( CK_C_EncryptInit ) DynamicBind( pkcs11Info->hPKCS11, "C_EncryptInit" );
pkcs11Info->pC_Finalize = ( CK_C_Finalize ) DynamicBind( pkcs11Info->hPKCS11, "C_Finalize" );
pkcs11Info->pC_FindObjects = ( CK_C_FindObjects ) DynamicBind( pkcs11Info->hPKCS11, "C_FindObjects" );
pkcs11Info->pC_FindObjectsFinal = ( CK_C_FindObjectsFinal ) DynamicBind( pkcs11Info->hPKCS11, "C_FindObjectsFinal" );
pkcs11Info->pC_FindObjectsInit = ( CK_C_FindObjectsInit ) DynamicBind( pkcs11Info->hPKCS11, "C_FindObjectsInit" );
pkcs11Info->pC_GenerateKeyPair = ( CK_C_GenerateKeyPair ) DynamicBind( pkcs11Info->hPKCS11, "C_GenerateKeyPair" );
pkcs11Info->pC_GenerateRandom = ( CK_C_GenerateRandom ) DynamicBind( pkcs11Info->hPKCS11, "C_GenerateRandom" );
pkcs11Info->pC_GetAttributeValue = ( CK_C_GetAttributeValue ) DynamicBind( pkcs11Info->hPKCS11, "C_GetAttributeValue" );
pkcs11Info->pC_GetMechanismInfo = ( CK_C_GetMechanismInfo ) DynamicBind( pkcs11Info->hPKCS11, "C_GetMechanismInfo" );
pkcs11Info->pC_GetSlotInfo = ( CK_C_GetSlotInfo ) DynamicBind( pkcs11Info->hPKCS11, "C_GetSlotInfo" );
pkcs11Info->pC_GetSlotList = ( CK_C_GetSlotList ) DynamicBind( pkcs11Info->hPKCS11, "C_GetSlotList" );
pkcs11Info->pC_GetTokenInfo = ( CK_C_GetTokenInfo ) DynamicBind( pkcs11Info->hPKCS11, "C_GetTokenInfo" );
pkcs11Info->pC_InitPIN = ( CK_C_InitPIN ) DynamicBind( pkcs11Info->hPKCS11, "C_InitPIN" );
pkcs11Info->pC_InitToken = ( CK_C_InitToken ) DynamicBind( pkcs11Info->hPKCS11, "C_InitToken" );
pkcs11Info->pC_Login = ( CK_C_Login ) DynamicBind( pkcs11Info->hPKCS11, "C_Login" );
pkcs11Info->pC_Logout = ( CK_C_Logout ) DynamicBind( pkcs11Info->hPKCS11, "C_Logout" );
pkcs11Info->pC_OpenSession = ( CK_C_OpenSession ) DynamicBind( pkcs11Info->hPKCS11, "C_OpenSession" );
pkcs11Info->pC_SetAttributeValue = ( CK_C_SetAttributeValue ) DynamicBind( pkcs11Info->hPKCS11, "C_SetAttributeValue" );
pkcs11Info->pC_SetPIN = ( CK_C_SetPIN ) DynamicBind( pkcs11Info->hPKCS11, "C_SetPIN" );
pkcs11Info->pC_Sign = ( CK_C_Sign ) DynamicBind( pkcs11Info->hPKCS11, "C_Sign" );
pkcs11Info->pC_SignInit = ( CK_C_SignInit ) DynamicBind( pkcs11Info->hPKCS11, "C_SignInit" );
pkcs11Info->pC_UnwrapKey = ( CK_C_UnwrapKey ) DynamicBind( pkcs11Info->hPKCS11, "C_UnwrapKey" );
pkcs11Info->pC_Verify = ( CK_C_Verify ) DynamicBind( pkcs11Info->hPKCS11, "C_Verify" );
pkcs11Info->pC_VerifyInit = ( CK_C_VerifyInit ) DynamicBind( pkcs11Info->hPKCS11, "C_VerifyInit" );
/* Make sure we got valid pointers for every device function.
C_FindObjectsFinal() wasn't added until 2.x and some drivers don't
implement it (a smaller subset of them nevertheless claim to be 2.x
drivers), so we allow this to be null - the code won't call it if it's
not present */
if( pC_GetInfo == NULL || pC_Initialize == NULL ||
pkcs11Info->pC_CloseSession == NULL ||
pkcs11Info->pC_CreateObject == NULL ||
pkcs11Info->pC_Decrypt == NULL ||
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -