⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 host_api.c

📁 IBM的Linux上的PKCS#11实现
💻 C
📖 第 1 页 / 共 5 页
字号:
   if ( enabled){         va_start(pvar, fmt);         vsprintf(buffer,fmt,pvar);         va_end(pvar);#if (AIX)         syslog_r(LOG_DEBUG,&log_data,buffer);#elif (LINUX)         syslog(LOG_DEBUG,buffer);#else#error "Define LINUX or AIX or change the code for your flavor"#endif   }}#endif#ifdef SOCKET#include <sys/socket.h>#include <netinet/in.h>#include <netdb.h>#include <unistd.h>#define PORT  4000#define HOSTNAME  "mayfield.lexington.ibm.com"struct  proxy_req {   CK_ULONG  pid;	CK_ULONG  cmd_id;	CK_ULONG req_len;	CK_ULONG repl_len;  // (of course this will be the actual replen that I'm looking for dereferenced off *repl_len parameter)	CK_ULONG out_len;	CK_ULONG in_len;//	[Data from pRequest]//	[data from pOut]   This will not be present if pOut is NULL};typedef struct proxy_req proxy_req_t;struct proxy_resp{	CK_ULONG return_code;	CK_ULONG repl_len;	CK_ULONG  in_len;	//[Reply Data]	//[In Data]};typedef struct proxy_resp proxy_resp_t;#endif#if 0   // BIG BLOCK OF COMMENTS//XXX Development NoteNote for 64 bit enablers.   The leeds card is a 486 processor with a 32 bit pkcsimplementation running on it.  All the values will have to be translated tolittle endian 32 bit values BEFORE being passed into the card.  Results willhave to be converted to 64 bit values.  This is not a trivial task.Notes:session handles - returned values from the adapter are byte swapped because of the way the original code from Lex was implemented.   the session create code wasmodified to swap the value that was returned.Object handles on the other hand are NOT reversed.   They have meaning only tothe card and to many places would have to be changed to swap the handles.  Much ofthe Lex code originally tried to swap the results without originaly swapping the values.Every effort has been made to  properly ifdef the code to correctly handle thesesituations.#endif// XXX  Communicate2 added on 12-09 and following to handle performance // improvements etc with the firmware.  The interface to the card had to// change.CK_RV communicate( CK_ULONG    cmd_id,  CK_SLOT_ID   slot_id,                    CK_VOID_PTR pReq,    CK_ULONG     req_len,                    CK_VOID_PTR pRep,    CK_ULONG_PTR rep_len,                    CK_BYTE_PTR pOut,    CK_ULONG     out_len,                    CK_BYTE_PTR pIn,     CK_ULONG     in_len );CK_BBOOL          initialized = FALSE;pid_t             initedpid=0;CK_ULONG          usage_count = 0;#ifdef PKCS64CK_ULONG_32          pid_list[ PKW_MAX_DEVICES ] = {0,0,0,0,0,0,0,0,0,0};#elseCK_ULONG          pid_list[ PKW_MAX_DEVICES ] = {0,0,0,0,0,0,0,0,0,0};#endifstatic sccAdapterNumber_t  num_adapters;static sccAgentID_t        leeds_id;static sccAdapterHandle_t  adapter_handle[MAX_SLOT_ID];// Need to handle the mutex correctly.  for portability purposes // make the NT Wait and Release map to the pthreads code#ifdef PKCS64pthread_mutex_t   pkcs_mutex; #elsepthread_mutex_t   pkcs_mutex = PTHREAD_MUTEX_INITIALIZER; // Local module lock#endif#ifdef SOCKETpthread_mutex_t   smtx = PTHREAD_MUTEX_INITIALIZER; // Local module lock#endif#define   WaitForSingleObject( x, y )  pthread_mutex_lock(&x) #define   ReleaseMutex( x )            pthread_mutex_unlock(&x)typedef unsigned char UCHAR;#if __64BIT__#define  ADJUSTMECHPTR()  \      { \      CK_ULONG_PTR  padjust; \      padjust = (CK_ULONG_PTR)adjust_mech_parameter(pMechanism); \      if (padjust ) { \         memcpy((void *) ptr,(void *) padjust, get_mech_parameter_len(pMechanism)); \         free(padjust); \      } else { \         memcpy( ptr, pMechanism->pParameter, pMechanism->ulParameterLen ); \      } \      }#else#define  ADJUSTMECHPTR()  \      { \      CK_ULONG_PTR  padjust; \      padjust = (CK_ULONG_PTR)adjust_mech_parameter(pMechanism); \      if (padjust ) { \         memcpy((void *) ptr,(void *) padjust, pMechanism->ulParameterLen ); \         free(padjust); \      } else { \         memcpy( ptr, pMechanism->pParameter, pMechanism->ulParameterLen ); \      } \      }#endifDL_NODE *session_list = NULL;#ifdef PKCS64CK_ULONG_32 next_session_handle = 1;#elseCK_ULONG next_session_handle = 1;#endifCK_BBOOLInitialized(){  if (initialized == FALSE ) return FALSE;  if (initedpid != getpid()) return FALSE;  return TRUE;}// Global initialized value for each slot indicating if the coorelator// used has been initialized already.struct  Cor_init{   unsigned char init;   CK_SLOT_ID    API_Slotid;} correlator_init[PKW_MAX_DEVICES];struct ST_FCN_LIST  function_list;  extern CK_RV SC_Initialize();                                 /* extern CK_RV SC_Initialize             */  extern CK_RV SC_GetTokenInfo();                               /* extern CK_RV SC_GetTokenInfo           */  extern CK_RV SC_GetMechanismList();                           /* extern CK_RV SC_GetMechanismList       */  extern CK_RV SC_GetMechanismInfo();                           /* extern CK_RV SC_GetMechanismInfo       */  extern CK_RV SC_InitToken();                                  /* extern CK_RV SC_InitToken              */  extern CK_RV SC_InitPIN();                                    /* extern CK_RV SC_InitPIN                */  extern CK_RV SC_SetPIN();                                     /* extern CK_RV SC_SetPIN                 */  extern CK_RV SC_OpenSession();                                /* extern CK_RV SC_OpenSession            */  extern CK_RV SC_CloseSession();                               /* extern CK_RV SC_CloseSession           */  extern CK_RV SC_CloseAllSessions();                           /* extern CK_RV SC_CloseAllSessions       */  extern CK_RV SC_GetSessionInfo();                             /* extern CK_RV SC_GetSessionInfo         */  extern CK_RV SC_GetOperationState();                          /* extern CK_RV SC_GetOperationState      */  extern CK_RV SC_SetOperationState();                          /* extern CK_RV SC_SetOperationState      */  extern CK_RV SC_Login();                                      /* extern CK_RV SC_Login                  */  extern CK_RV SC_Logout();                                     /* extern CK_RV SC_Logout                 */  extern CK_RV SC_CreateObject();                               /* extern CK_RV SC_CreateObject           */  extern CK_RV SC_CopyObject();                                 /* extern CK_RV SC_CopyObject             */  extern CK_RV SC_DestroyObject();                              /* extern CK_RV SC_DestroyObject          */  extern CK_RV SC_GetObjectSize();                              /* extern CK_RV SC_GetObjectSize          */  extern CK_RV SC_GetAttributeValue();                          /* extern CK_RV SC_GetAttributeValue      */  extern CK_RV SC_SetAttributeValue();                          /* extern CK_RV SC_SetAttributeValue      */  extern CK_RV SC_FindObjectsInit();                            /* extern CK_RV SC_FindObjectsInit        */  extern CK_RV SC_FindObjects();                                /* extern CK_RV SC_FindObjects            */  extern CK_RV SC_FindObjectsFinal();                           /* extern CK_RV SC_FindObjectsFinal       */  extern CK_RV SC_EncryptInit();                                /* extern CK_RV SC_EncryptInit            */  extern CK_RV SC_Encrypt();                                    /* extern CK_RV SC_Encrypt                */  extern CK_RV SC_EncryptUpdate();                              /* extern CK_RV SC_EncryptUpdate          */  extern CK_RV SC_EncryptFinal();                               /* extern CK_RV SC_EncryptFinal           */  extern CK_RV SC_DecryptInit();                                /* extern CK_RV SC_DecryptInit            */  extern CK_RV SC_Decrypt();                                    /* extern CK_RV SC_Decrypt                */  extern CK_RV SC_DecryptUpdate();                              /* extern CK_RV SC_DecryptUpdate          */  extern CK_RV SC_DecryptFinal();                               /* extern CK_RV SC_DecryptFinal           */  extern CK_RV SC_DigestInit();                                 /* extern CK_RV SC_DigestInit             */  extern CK_RV SC_Digest();                                     /* extern CK_RV SC_Digest                 */  extern CK_RV SC_DigestUpdate();                               /* extern CK_RV SC_DigestUpdate           */  extern CK_RV SC_DigestKey();                                  /* extern CK_RV SC_DigestKey              */  extern CK_RV SC_DigestFinal();                                /* extern CK_RV SC_DigestFinal            */  extern CK_RV SC_SignInit();                                   /* extern CK_RV SC_SignInit               */  extern CK_RV SC_Sign();                                       /* extern CK_RV SC_Sign                   */  extern CK_RV SC_SignUpdate();                                 /* extern CK_RV SC_SignUpdate             */  extern CK_RV SC_SignFinal();                                  /* extern CK_RV SC_SignFinal              */  extern CK_RV SC_SignRecoverInit();                            /* extern CK_RV SC_SignRecoverInit        */  extern CK_RV SC_SignRecover();                                /* extern CK_RV SC_SignRecover            */  extern CK_RV SC_VerifyInit();                                 /* extern CK_RV SC_VerifyInit             */  extern CK_RV SC_Verify();                                     /* extern CK_RV SC_Verify                 */  extern CK_RV SC_VerifyUpdate();                               /* extern CK_RV SC_VerifyUpdate           */  extern CK_RV SC_VerifyFinal();                                /* extern CK_RV SC_VerifyFinal            */  extern CK_RV SC_VerifyRecoverInit();                          /* extern CK_RV SC_VerifyRecoverInit      */  extern CK_RV SC_VerifyRecover();                              /* extern CK_RV SC_VerifyRecover          */  extern CK_RV SC_DigestEncryptUpdate();                        /* extern CK_RV SC_DigestEncryptUpdate    */  extern CK_RV SC_DecryptDigestUpdate();                        /* extern CK_RV SC_DecryptDigestUpdate    */  extern CK_RV SC_SignEncryptUpdate();                          /* extern CK_RV SC_SignEncryptUpdate      */  extern CK_RV SC_DecryptVerifyUpdate();                        /* extern CK_RV SC_DecryptVerifyUpdate    */  extern CK_RV SC_GenerateKey();                                /* extern CK_RV SC_GenerateKey            */  extern CK_RV SC_GenerateKeyPair();                            /* extern CK_RV SC_GenerateKeyPair        */  extern CK_RV SC_WrapKey();                                    /* extern CK_RV SC_WrapKey                */  extern CK_RV SC_UnwrapKey();                                  /* extern CK_RV SC_UnwrapKey              */  extern CK_RV SC_DeriveKey();                                  /* extern CK_RV SC_DeriveKey              */  extern CK_RV SC_SeedRandom();                                 /* extern CK_RV SC_SeedRandom             */  extern CK_RV SC_GenerateRandom();                             /* extern CK_RV SC_GenerateRandom         */  extern CK_RV SC_GetFunctionStatus();                          /* extern CK_RV SC_GetFunctionStatus      */  extern CK_RV SC_CancelFunction();                             /* extern CK_RV SC_CancelFunction         */// Added for AIX workvoidSC_SetFunctionList(void){   function_list.ST_Initialize          = SC_Initialize;   function_list.ST_GetTokenInfo        = SC_GetTokenInfo;   function_list.ST_GetMechanismList    = SC_GetMechanismList;   function_list.ST_GetMechanismInfo    = SC_GetMechanismInfo;   function_list.ST_InitToken           = SC_InitToken;   function_list.ST_InitPIN             = SC_InitPIN;   function_list.ST_SetPIN              = SC_SetPIN;   function_list.ST_OpenSession         = SC_OpenSession;   function_list.ST_CloseSession        = SC_CloseSession;   function_list.ST_GetSessionInfo      = SC_GetSessionInfo;   function_list.ST_GetOperationState   = SC_GetOperationState;   function_list.ST_SetOperationState   = SC_SetOperationState;   function_list.ST_Login               = SC_Login;   function_list.ST_Logout              = SC_Logout;   function_list.ST_CreateObject        = SC_CreateObject;   function_list.ST_CopyObject          = SC_CopyObject;   function_list.ST_DestroyObject       = SC_DestroyObject;   function_list.ST_GetObjectSize       = SC_GetObjectSize;   function_list.ST_GetAttributeValue   = SC_GetAttributeValue;   function_list.ST_SetAttributeValue   = SC_SetAttributeValue;   function_list.ST_FindObjectsInit     = SC_FindObjectsInit;   function_list.ST_FindObjects         = SC_FindObjects;   function_list.ST_FindObjectsFinal    = SC_FindObjectsFinal;   function_list.ST_EncryptInit         = SC_EncryptInit;   function_list.ST_Encrypt             = SC_Encrypt;   function_list.ST_EncryptUpdate       = SC_EncryptUpdate;   function_list.ST_EncryptFinal        = SC_EncryptFinal;   function_list.ST_DecryptInit         = SC_DecryptInit;   function_list.ST_Decrypt             = SC_Decrypt;   function_list.ST_DecryptUpdate       = SC_DecryptUpdate;   function_list.ST_DecryptFinal        = SC_DecryptFinal;   function_list.ST_DigestInit          = SC_DigestInit;   function_list.ST_Digest              = SC_Digest;   function_list.ST_DigestUpdate        = SC_DigestUpdate;   function_list.ST_DigestKey           = SC_DigestKey;   function_list.ST_DigestFinal         = SC_DigestFinal;   function_list.ST_SignInit            = SC_SignInit;   function_list.ST_Sign                = SC_Sign;   function_list.ST_SignUpdate          = SC_SignUpdate;   function_list.ST_SignFinal           = SC_SignFinal;   function_list.ST_SignRecoverInit     = SC_SignRecoverInit;   function_list.ST_SignRecover         = SC_SignRecover;   function_list.ST_VerifyInit          = SC_VerifyInit;   function_list.ST_Verify              = SC_Verify;   function_list.ST_VerifyUpdate        = SC_VerifyUpdate;   function_list.ST_VerifyFinal         = SC_VerifyFinal;   function_list.ST_VerifyRecoverInit   = SC_VerifyRecoverInit;   function_list.ST_VerifyRecover       = SC_VerifyRecover;   function_list.ST_DigestEncryptUpdate = NULL; // SC_DigestEncryptUpdate;   function_list.ST_DecryptDigestUpdate = NULL; // SC_DecryptDigestUpdate;   function_list.ST_SignEncryptUpdate   = NULL; //SC_SignEncryptUpdate;   function_list.ST_DecryptVerifyUpdate = NULL; // SC_DecryptVerifyUpdate;   function_list.ST_GenerateKey         = SC_GenerateKey;   function_list.ST_GenerateKeyPair     = SC_GenerateKeyPair;   function_list.ST_WrapKey             = SC_WrapKey;   function_list.ST_UnwrapKey           = SC_UnwrapKey;   function_list.ST_DeriveKey           = SC_DeriveKey;   function_list.ST_SeedRandom          = SC_SeedRandom ;   function_list.ST_GenerateRandom      = SC_GenerateRandom;   function_list.ST_GetFunctionStatus   = NULL; // SC_GetFunctionStatus;   function_list.ST_CancelFunction      = NULL; // SC_CancelFunction;}intAPISlot2Local(snum)   CK_SLOT_ID  snum;{   int i;//   return snum; /// temp to change this..   for (i=0;i< PKW_MAX_DEVICES; i++){      if (correlator_init[i].init)         if (correlator_init[i].API_Slotid == snum)            return (i+1);  // Slots from Lex code are 1 based not 0...   }   return -1;}#define  SLT_CHECK  \   CK_SLOT_ID     slot_id; \   int            sid1; \ \   if ( (sid1 = APISlot2Local(sid)) != -1 ){ \      slot_id = sid1; \   } else { \      return CKR_ARGUMENTS_BAD; \   } #define SESSION   sSession.sessionh#define SLOTID    APISlot2Local(sSession.slotID)#define  SESS_SET \   CK_SESSION_HANDLE  hSession; \\   hSession = sSession.sessionh; voidhex_dump(char *str,CK_BYTE_PTR ptr, int  len ){   int  lendone=0;   int  by10 = 0;   int   i,j;   printf("%s Dumping  %d bytes \n",str,len);   by10 = len/10;   for (i=0;i< by10;i++){      for (j=0;j<10;j++){         lendone ++;         printf("0x%02X ",*ptr);         ptr++;      }      printf("\n");   }   for (j=lendone; j< len;j++){         printf("0x%02X ",*ptr);         ptr++;   }      printf("\n Done Dumping  \n");}CK_RVMY_LockMutex(void)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -