27 #define MAX_PKCS11_DRIVERS 5
28 #define MAX_PKCS11_SLOTS 16
32 #define DEFAULT_SLOT 0
59 status = C_GenerateRandom( pkcs11Info->
hSession, buffer, length );
65 static int selfTestFunction(
void )
96 char name[ 32 + 1 + 8 ];
97 INSTANCE_HANDLE hPKCS11;
99 #ifdef USE_EXPLICIT_LINKING
100 CK_C_CloseSession pC_CloseSession;
101 CK_C_CreateObject pC_CreateObject;
102 CK_C_Decrypt pC_Decrypt;
103 CK_C_DecryptInit pC_DecryptInit;
104 CK_C_DestroyObject pC_DestroyObject;
105 CK_C_Digest pC_Digest;
106 CK_C_DigestInit pC_DigestInit;
107 CK_C_Encrypt pC_Encrypt;
108 CK_C_EncryptInit pC_EncryptInit;
109 CK_C_Finalize pC_Finalize;
110 CK_C_FindObjects pC_FindObjects;
111 CK_C_FindObjectsFinal pC_FindObjectsFinal;
112 CK_C_FindObjectsInit pC_FindObjectsInit;
113 CK_C_GenerateKey pC_GenerateKey;
114 CK_C_GenerateKeyPair pC_GenerateKeyPair;
115 CK_C_GenerateRandom pC_GenerateRandom;
116 CK_C_GetAttributeValue pC_GetAttributeValue;
117 CK_C_GetMechanismInfo pC_GetMechanismInfo;
118 CK_C_GetSlotInfo pC_GetSlotInfo;
119 CK_C_GetSlotList pC_GetSlotList;
120 CK_C_GetTokenInfo pC_GetTokenInfo;
121 CK_C_InitPIN pC_InitPIN;
122 CK_C_InitToken pC_InitToken;
124 CK_C_Logout pC_Logout;
125 CK_C_OpenSession pC_OpenSession;
126 CK_C_SetAttributeValue pC_SetAttributeValue;
127 CK_C_SetPIN pC_SetPIN;
129 CK_C_SignFinal pC_SignFinal;
130 CK_C_SignInit pC_SignInit;
131 CK_C_SignUpdate pC_SignUpdate;
132 CK_C_Verify pC_Verify;
133 CK_C_VerifyInit pC_VerifyInit;
134 CK_C_WrapKey pC_WrapKey;
135 CK_C_UnwrapKey pC_UnwrapKey;
137 } PKCS11_DRIVER_INFO;
139 static PKCS11_DRIVER_INFO pkcs11InfoTbl[ MAX_PKCS11_DRIVERS + 8 ];
146 #ifdef USE_EXPLICIT_LINKING
148 #define C_CloseSession pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_CloseSession
149 #define C_CreateObject pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_CreateObject
150 #define C_Decrypt pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_Decrypt
151 #define C_DecryptInit pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_DecryptInit
152 #define C_DestroyObject pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_DestroyObject
153 #define C_Digest pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_Digest
154 #define C_DigestInit pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_DigestInit
155 #define C_Encrypt pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_Encrypt
156 #define C_EncryptInit pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_EncryptInit
157 #define C_Finalize pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_Finalize
158 #define C_FindObjects pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_FindObjects
159 #define C_FindObjectsFinal pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_FindObjectsFinal
160 #define C_FindObjectsInit pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_FindObjectsInit
161 #define C_GenerateKey pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_GenerateKey
162 #define C_GenerateKeyPair pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_GenerateKeyPair
163 #define C_GenerateRandom pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_GenerateRandom
164 #define C_GetAttributeValue pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_GetAttributeValue
165 #define C_GetMechanismInfo pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_GetMechanismInfo
166 #define C_GetSlotInfo pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_GetSlotInfo
167 #define C_GetSlotList pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_GetSlotList
168 #define C_GetTokenInfo pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_GetTokenInfo
169 #define C_Initialize pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_Initialize
170 #define C_InitPIN pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_InitPIN
171 #define C_InitToken pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_InitToken
172 #define C_Login pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_Login
173 #define C_Logout pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_Logout
174 #define C_OpenSession pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_OpenSession
175 #define C_SetAttributeValue pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_SetAttributeValue
176 #define C_SetPIN pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_SetPIN
177 #define C_Sign pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_Sign
178 #define C_SignFinal pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_SignFinal
179 #define C_SignInit pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_SignInit
180 #define C_SignUpdate pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_SignUpdate
181 #define C_Verify pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_Verify
182 #define C_VerifyInit pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_VerifyInit
183 #define C_WrapKey pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_WrapKey
184 #define C_UnwrapKey pkcs11InfoTbl[ pkcs11Info->deviceNo ].pC_UnwrapKey
190 static int loadPKCS11driver( PKCS11_DRIVER_INFO *pkcs11Info,
191 const char *driverName )
193 #ifdef USE_EXPLICIT_LINKING
194 CK_C_GetInfo pC_GetInfo;
195 CK_C_Initialize pC_Initialize;
197 CK_C_GetFunctionList pC_GetFunctionList;
207 assert(
isWritePtr( pkcs11Info,
sizeof( PKCS11_DRIVER_INFO ) ) );
212 errorMode = SetErrorMode( SEM_NOOPENFILEERRORBOX );
213 pkcs11Info->hPKCS11 = LoadLibrary( driverName );
214 SetErrorMode( errorMode );
215 if( pkcs11Info->hPKCS11 < HINSTANCE_ERROR )
217 pkcs11Info->hPKCS11 = NULL_HINSTANCE;
221 if( ( pkcs11Info->hPKCS11 = DynamicLoad( driverName ) ) == NULL_INSTANCE )
226 #ifdef USE_EXPLICIT_LINKING
227 pC_GetInfo = ( CK_C_GetInfo ) DynamicBind( pkcs11Info->hPKCS11,
"C_GetInfo" );
228 pC_Initialize = ( CK_C_Initialize ) DynamicBind( pkcs11Info->hPKCS11,
"C_Initialize" );
229 pkcs11Info->pC_CloseSession = ( CK_C_CloseSession ) DynamicBind( pkcs11Info->hPKCS11,
"C_CloseSession" );
230 pkcs11Info->pC_CreateObject = ( CK_C_CreateObject ) DynamicBind( pkcs11Info->hPKCS11,
"C_CreateObject" );
231 pkcs11Info->pC_Decrypt = ( CK_C_Decrypt ) DynamicBind( pkcs11Info->hPKCS11,
"C_Decrypt" );
232 pkcs11Info->pC_DecryptInit = ( CK_C_DecryptInit ) DynamicBind( pkcs11Info->hPKCS11,
"C_DecryptInit" );
233 pkcs11Info->pC_DestroyObject = ( CK_C_DestroyObject ) DynamicBind( pkcs11Info->hPKCS11,
"C_DestroyObject" );
234 pkcs11Info->pC_Digest = ( CK_C_Digest ) DynamicBind( pkcs11Info->hPKCS11,
"C_Digest" );
235 pkcs11Info->pC_DigestInit = ( CK_C_DigestInit ) DynamicBind( pkcs11Info->hPKCS11,
"C_DigestInit" );
236 pkcs11Info->pC_Encrypt = ( CK_C_Encrypt ) DynamicBind( pkcs11Info->hPKCS11,
"C_Encrypt" );
237 pkcs11Info->pC_EncryptInit = ( CK_C_EncryptInit ) DynamicBind( pkcs11Info->hPKCS11,
"C_EncryptInit" );
238 pkcs11Info->pC_Finalize = ( CK_C_Finalize ) DynamicBind( pkcs11Info->hPKCS11,
"C_Finalize" );
239 pkcs11Info->pC_FindObjects = ( CK_C_FindObjects ) DynamicBind( pkcs11Info->hPKCS11,
"C_FindObjects" );
240 pkcs11Info->pC_FindObjectsFinal = ( CK_C_FindObjectsFinal ) DynamicBind( pkcs11Info->hPKCS11,
"C_FindObjectsFinal" );
241 pkcs11Info->pC_FindObjectsInit = ( CK_C_FindObjectsInit ) DynamicBind( pkcs11Info->hPKCS11,
"C_FindObjectsInit" );
242 pkcs11Info->pC_GenerateKey = ( CK_C_GenerateKey ) DynamicBind( pkcs11Info->hPKCS11,
"C_GenerateKey" );
243 pkcs11Info->pC_GenerateKeyPair = ( CK_C_GenerateKeyPair ) DynamicBind( pkcs11Info->hPKCS11,
"C_GenerateKeyPair" );
244 pkcs11Info->pC_GenerateRandom = ( CK_C_GenerateRandom ) DynamicBind( pkcs11Info->hPKCS11,
"C_GenerateRandom" );
245 pkcs11Info->pC_GetAttributeValue = ( CK_C_GetAttributeValue ) DynamicBind( pkcs11Info->hPKCS11,
"C_GetAttributeValue" );
246 pkcs11Info->pC_GetMechanismInfo = ( CK_C_GetMechanismInfo ) DynamicBind( pkcs11Info->hPKCS11,
"C_GetMechanismInfo" );
247 pkcs11Info->pC_GetSlotInfo = ( CK_C_GetSlotInfo ) DynamicBind( pkcs11Info->hPKCS11,
"C_GetSlotInfo" );
248 pkcs11Info->pC_GetSlotList = ( CK_C_GetSlotList ) DynamicBind( pkcs11Info->hPKCS11,
"C_GetSlotList" );
249 pkcs11Info->pC_GetTokenInfo = ( CK_C_GetTokenInfo ) DynamicBind( pkcs11Info->hPKCS11,
"C_GetTokenInfo" );
250 pkcs11Info->pC_InitPIN = ( CK_C_InitPIN ) DynamicBind( pkcs11Info->hPKCS11,
"C_InitPIN" );
251 pkcs11Info->pC_InitToken = ( CK_C_InitToken ) DynamicBind( pkcs11Info->hPKCS11,
"C_InitToken" );
252 pkcs11Info->pC_Login = ( CK_C_Login ) DynamicBind( pkcs11Info->hPKCS11,
"C_Login" );
253 pkcs11Info->pC_Logout = ( CK_C_Logout ) DynamicBind( pkcs11Info->hPKCS11,
"C_Logout" );
254 pkcs11Info->pC_OpenSession = ( CK_C_OpenSession ) DynamicBind( pkcs11Info->hPKCS11,
"C_OpenSession" );
255 pkcs11Info->pC_SetAttributeValue = ( CK_C_SetAttributeValue ) DynamicBind( pkcs11Info->hPKCS11,
"C_SetAttributeValue" );
256 pkcs11Info->pC_SetPIN = ( CK_C_SetPIN ) DynamicBind( pkcs11Info->hPKCS11,
"C_SetPIN" );
257 pkcs11Info->pC_SignFinal = ( CK_C_SignFinal ) DynamicBind( pkcs11Info->hPKCS11,
"C_SignFinal" );
258 pkcs11Info->pC_SignInit = ( CK_C_SignInit ) DynamicBind( pkcs11Info->hPKCS11,
"C_SignInit" );
259 pkcs11Info->pC_SignUpdate = ( CK_C_SignUpdate ) DynamicBind( pkcs11Info->hPKCS11,
"C_SignUpdate" );
260 pkcs11Info->pC_Verify = ( CK_C_Verify ) DynamicBind( pkcs11Info->hPKCS11,
"C_Verify" );
261 pkcs11Info->pC_VerifyInit = ( CK_C_VerifyInit ) DynamicBind( pkcs11Info->hPKCS11,
"C_VerifyInit" );
262 pkcs11Info->pC_WrapKey = ( CK_C_WrapKey ) DynamicBind( pkcs11Info->hPKCS11,
"C_WrapKey" );
263 pkcs11Info->pC_UnwrapKey = ( CK_C_UnwrapKey ) DynamicBind( pkcs11Info->hPKCS11,
"C_UnwrapKey" );
270 if( pC_GetInfo == NULL || pC_Initialize == NULL ||
271 pkcs11Info->pC_CloseSession == NULL ||
272 pkcs11Info->pC_CreateObject == NULL ||
273 pkcs11Info->pC_Decrypt == NULL ||
274 pkcs11Info->pC_DecryptInit == NULL ||
275 pkcs11Info->pC_DestroyObject == NULL ||
276 pkcs11Info->pC_Digest == NULL ||
277 pkcs11Info->pC_DigestInit == NULL ||
278 pkcs11Info->pC_Encrypt == NULL ||
279 pkcs11Info->pC_EncryptInit == NULL ||
280 pkcs11Info->pC_Finalize == NULL ||
281 pkcs11Info->pC_FindObjects == NULL ||
282 pkcs11Info->pC_FindObjectsInit == NULL ||
283 pkcs11Info->pC_GenerateRandom == NULL ||
284 pkcs11Info->pC_GenerateKey == NULL ||
285 pkcs11Info->pC_GenerateKeyPair == NULL ||
286 pkcs11Info->pC_GetAttributeValue == NULL ||
287 pkcs11Info->pC_GetMechanismInfo == NULL ||
288 pkcs11Info->pC_GetSlotInfo == NULL ||
289 pkcs11Info->pC_GetSlotList == NULL ||
290 pkcs11Info->pC_GetTokenInfo == NULL ||
291 pkcs11Info->pC_InitPIN == NULL ||
292 pkcs11Info->pC_InitToken == NULL || pkcs11Info->pC_Login == NULL ||
293 pkcs11Info->pC_Logout == NULL || pkcs11Info->pC_OpenSession == NULL ||
294 pkcs11Info->pC_SetAttributeValue == NULL ||
295 pkcs11Info->pC_SetPIN == NULL || pkcs11Info->pC_Sign == NULL ||
296 pkcs11Info->pC_SignFinal == NULL || pkcs11Info->pC_SignInit == NULL ||
297 pkcs11Info->pC_SignUpdate == NULL ||
298 pkcs11Info->pC_WrapKey == NULL || pkcs11Info->pC_UnwrapKey == NULL ||
299 pkcs11Info->pC_Verify == NULL || pkcs11Info->pC_VerifyInit == NULL )
302 DynamicUnload( pkcs11Info->hPKCS11 );
303 memset( pkcs11Info, 0,
sizeof( PKCS11_DRIVER_INFO ) );
317 status = pC_Initialize( NULL_PTR ) & 0xFFFF;
320 isInitialised =
TRUE;
321 status = pC_GetInfo( &info ) & 0xFFFF;
331 pkcs11Info->pC_Finalize( NULL_PTR );
332 DynamicUnload( pkcs11Info->hPKCS11 );
333 memset( pkcs11Info, 0,
sizeof( PKCS11_DRIVER_INFO ) );
347 pC_GetFunctionList = ( CK_C_GetFunctionList ) \
348 DynamicBind( pkcs11Info->hPKCS11,
"C_GetFunctionList" );
349 if( pC_GetFunctionList == NULL )
359 status = pC_GetFunctionList( &functionListPtr ) & 0xFFFF;
361 pkcs11Info->functionListPtr = functionListPtr;
366 DynamicUnload( pkcs11Info->hPKCS11 );
367 memset( pkcs11Info, 0,
sizeof( PKCS11_DRIVER_INFO ) );
370 status = C_Initialize( NULL_PTR ) & 0xFFFF;
373 isInitialised =
TRUE;
374 status = C_GetInfo( &info ) & 0xFFFF;
384 C_Finalize( NULL_PTR );
385 DynamicUnload( pkcs11Info->hPKCS11 );
386 memset( pkcs11Info, 0,
sizeof( PKCS11_DRIVER_INFO ) );
395 while( i > 0 && ( pkcs11Info->name[ i - 1 ] ==
' ' || \
396 !pkcs11Info->name[ i - 1 ] ) )
398 pkcs11Info->name[ i ] =
'\0';
407 if( pkcs11Initialised )
409 for( i = 0; i < MAX_PKCS11_DRIVERS; i++ )
411 if( pkcs11InfoTbl[ i ].hPKCS11 != NULL_INSTANCE )
413 #ifdef USE_EXPLICIT_LINKING
414 pkcs11InfoTbl[ i ].pC_Finalize( NULL_PTR );
416 PKCS11_DRIVER_INFO *pkcs11Info = &pkcs11InfoTbl[ i ];
418 C_Finalize( NULL_PTR );
420 DynamicUnload( pkcs11InfoTbl[ i ].hPKCS11 );
422 pkcs11InfoTbl[ i ].hPKCS11 = NULL_INSTANCE;
425 pkcs11Initialised =
FALSE;
430 int tblIndex = 0, optionIndex,
status;
434 if( pkcs11Initialised )
436 memset( pkcs11InfoTbl, 0,
sizeof( pkcs11InfoTbl ) );
441 for( optionIndex = 0; optionIndex < MAX_PKCS11_DRIVERS; optionIndex++ )
444 char deviceDriverName[ MAX_PATH_LENGTH + 1 + 8 ];
452 deviceDriverName[ msgData.
length ] =
'\0';
453 status = loadPKCS11driver( &pkcs11InfoTbl[ tblIndex ],
458 pkcs11Initialised =
TRUE;
479 if( !pkcs11Initialised )
481 status = loadPKCS11driver( &pkcs11InfoTbl[ tblIndex ],
484 pkcs11Initialised =
TRUE;
486 if( !pkcs11Initialised )
488 status = loadPKCS11driver( &pkcs11InfoTbl[ tblIndex ],
489 "/opt/nfast/toolkits/pkcs11/libcknfast.so" );
491 pkcs11Initialised =
TRUE;
506 if( pkcs11Initialised )
509 status = C_Initialize( NULL_PTR );
512 pkcs11Initialised =
TRUE;
518 if( pkcs11Initialised )
519 C_Finalize( NULL_PTR );
520 pkcs11Initialised =
FALSE;
535 #define keysizeValid( algo ) \
536 ( ( algo ) == CRYPT_ALGO_DH || ( algo ) == CRYPT_ALGO_RSA || \
537 ( algo ) == CRYPT_ALGO_DSA || ( algo ) == CRYPT_ALGO_RC2 || \
538 ( algo ) == CRYPT_ALGO_RC4 || ( algo ) == CRYPT_ALGO_RC5 )
539 #define keysizeInBytes( algo ) \
540 ( ( algo ) == CRYPT_ALGO_RC5 )
590 #ifdef USE_HMAC_RIPEMD160
619 const PKCS11_MECHANISM_INFO *mechanismInfoPtr,
620 const int maxMechanisms )
628 PKCS11_INFO *pkcs11Info = deviceInfo->devicePKCS11;
629 int hardwareOnly, i, iterationCount;
633 maxMechanisms *
sizeof( PKCS11_MECHANISM_INFO ) ) );
639 status = C_GetMechanismInfo( pkcs11Info->
slotID,
640 mechanismInfoPtr->mechanism,
646 if( hardwareOnly && !( pMechanism.
flags &
CKF_HW ) )
650 if( ( capabilityInfo =
clAlloc(
"getCapability", \
653 for( i = 0; capabilityTemplates[ i ].cryptAlgo != cryptAlgo && \
654 capabilityTemplates[ i ].cryptAlgo !=
CRYPT_ERROR && \
658 capabilityTemplates[ i ].cryptAlgo ==
CRYPT_ERROR )
660 memcpy( capabilityInfo, &capabilityTemplates[ i ],
664 if( keysizeValid( cryptAlgo ) )
676 if( !keysizeInBytes( cryptAlgo ) )
691 DEBUG_DIAG((
"Maximum key size < minimum key size" ));
695 isDlpAlgo( cryptAlgo ) ) ? 128 : 16;
699 capabilityInfo->endFunction = genericEndFunction;
704 capabilityInfo->getInfoFunction = getDefaultInfo;
707 capabilityInfo->initParamsFunction = initGenericParams;
708 capabilityInfo->endFunction = mechanismInfoPtr->endFunction;
709 capabilityInfo->initKeyFunction = mechanismInfoPtr->initKeyFunction;
710 if( pMechanism.
flags & keyGenFlag )
711 capabilityInfo->generateKeyFunction = \
712 mechanismInfoPtr->generateKeyFunction;
720 capabilityInfo->signFunction = mechanismInfoPtr->signFunction;
722 capabilityInfo->encryptFunction = mechanismInfoPtr->encryptFunction;
728 capabilityInfo->sigCheckFunction = mechanismInfoPtr->sigCheckFunction;
730 capabilityInfo->decryptFunction = mechanismInfoPtr->decryptFunction;
736 switch( mechanismInfoPtr->cryptMode )
739 capabilityInfo->encryptCBCFunction = mechanismInfoPtr->encryptFunction;
743 capabilityInfo->encryptCFBFunction = mechanismInfoPtr->encryptFunction;
747 capabilityInfo->encryptOFBFunction = mechanismInfoPtr->encryptFunction;
751 capabilityInfo->encryptGCMFunction = mechanismInfoPtr->encryptFunction;
755 capabilityInfo->encryptFunction = mechanismInfoPtr->encryptFunction;
763 switch( mechanismInfoPtr->cryptMode )
766 capabilityInfo->decryptCBCFunction = mechanismInfoPtr->decryptFunction;
770 capabilityInfo->decryptCFBFunction = mechanismInfoPtr->decryptFunction;
774 capabilityInfo->decryptOFBFunction = mechanismInfoPtr->decryptFunction;
778 capabilityInfo->decryptGCMFunction = mechanismInfoPtr->decryptFunction;
782 capabilityInfo->decryptFunction = mechanismInfoPtr->decryptFunction;
790 capabilityInfo->encryptFunction = mechanismInfoPtr->encryptFunction;
791 capabilityInfo->decryptFunction = mechanismInfoPtr->decryptFunction;
798 if( !( pMechanism.
flags & keyGenFlag ) && \
799 ( mechanismInfoPtr->keygenMechanism != CKM_NONE ) )
801 status = C_GetMechanismInfo( pkcs11Info->
slotID,
802 mechanismInfoPtr->keygenMechanism,
804 if( status ==
CKR_OK && ( pMechanism.
flags & keyGenFlag ) && \
805 ( !hardwareOnly || ( pMechanism.
flags &
CKF_HW ) ) )
811 capabilityInfo->generateKeyFunction = \
812 mechanismInfoPtr->generateKeyFunction;
819 capabilityInfo->paramKeyType = mechanismInfoPtr->keyType;
820 capabilityInfo->paramKeyGen = mechanismInfoPtr->keygenMechanism;
821 capabilityInfo->paramDefaultMech = mechanismInfoPtr->defaultMechanism;
832 for( mechanismInfoPtr++, iterationCount = 0;
833 mechanismInfoPtr->cryptAlgo == cryptAlgo && \
834 iterationCount < maxMechanisms;
835 mechanismInfoPtr++, iterationCount++ )
839 status = C_GetMechanismInfo( pkcs11Info->
slotID,
840 mechanismInfoPtr->mechanism,
846 switch( mechanismInfoPtr->cryptMode )
850 capabilityInfo->encryptCBCFunction = \
851 mechanismInfoPtr->encryptFunction;
853 capabilityInfo->decryptCBCFunction = \
854 mechanismInfoPtr->decryptFunction;
858 capabilityInfo->encryptCFBFunction = \
859 mechanismInfoPtr->encryptFunction;
861 capabilityInfo->decryptCFBFunction = \
862 mechanismInfoPtr->decryptFunction;
866 capabilityInfo->encryptOFBFunction = \
867 mechanismInfoPtr->encryptFunction;
869 capabilityInfo->decryptOFBFunction = \
870 mechanismInfoPtr->decryptFunction;
874 capabilityInfo->encryptGCMFunction = \
875 mechanismInfoPtr->encryptFunction;
877 capabilityInfo->decryptGCMFunction = \
878 mechanismInfoPtr->decryptFunction;
885 if( iterationCount >= maxMechanisms )
896 static void freeCapabilities(
DEVICE_INFO *deviceInfo )
904 if( capabilityInfoListPtr == NULL )
908 while( capabilityInfoListPtr != NULL )
913 capabilityInfoListPtr = capabilityInfoListPtr->
next;
915 clFree(
"freeCapabilities", itemToFree );
917 clFree(
"freeCapabilities", listItemToFree );
921 static int getCapabilities(
DEVICE_INFO *deviceInfo,
922 const PKCS11_MECHANISM_INFO *mechanismInfoPtr,
923 const int maxMechanisms )
931 maxMechanisms *
sizeof( PKCS11_MECHANISM_INFO ) ) );
934 "Variable capability-info-struct" );
937 if( capabilityInfoListTail != NULL )
939 while( capabilityInfoListTail->
next != NULL )
940 capabilityInfoListTail = capabilityInfoListTail->
next;
944 for( i = 0; i < maxMechanisms && \
945 mechanismInfoPtr[ i ].mechanism != CKM_NONE; i++ )
961 newCapability = getCapability( deviceInfo, &mechanismInfoPtr[ i ],
963 if( newCapability == NULL )
965 REQUIRES( sanityCheckCapability( newCapability,
968 if( ( newCapabilityList = \
972 clFree(
"getCapabilities", newCapability );
975 newCapabilityList->
info = newCapability;
976 newCapabilityList->
next = NULL;
980 capabilityInfoListTail->
next = newCapabilityList;
981 capabilityInfoListTail = newCapabilityList;
986 while( mechanismInfoPtr[ i + 1 ].cryptAlgo == cryptAlgo && \
989 if( i >= maxMechanisms )
992 if( i >= maxMechanisms )
1008 static void shutdownFunction(
DEVICE_INFO *deviceInfo )
1010 PKCS11_INFO *pkcs11Info = deviceInfo->devicePKCS11;
1017 C_CloseSession( pkcs11Info->
hSession );
1018 pkcs11Info->
hSession = CK_OBJECT_NONE;
1022 freeCapabilities( deviceInfo );
1028 const int nameLength )
1032 CK_ULONG slotCount = MAX_PKCS11_SLOTS;
1036 PKCS11_INFO *pkcs11Info = deviceInfo->devicePKCS11;
1037 const PKCS11_MECHANISM_INFO *mechanismInfoPtr;
1039 int tokenSlot = DEFAULT_SLOT, i, labelLength, mechanismInfoSize;
1040 int cryptStatus, cryptStatus2;
1043 assert(
isReadPtr( name, nameLength ) );
1046 memset( slotList, 0,
sizeof( slotList ) );
1047 status = C_GetSlotList(
TRUE, slotList, &slotCount );
1050 if( slotCount <= 0 )
1058 for( i = 1; i < nameLength - 1; i++ )
1060 if( name[ i ] ==
':' && name[ i + 1 ] ==
':' )
1063 if( i < nameLength - 1 )
1065 const char *tokenName = name + i + 2;
1066 const int tokenNameLength = nameLength - ( i + 2 );
1068 if( tokenNameLength <= 0 )
1073 if( tokenNameLength == 1 &&
isDigit( *tokenName ) )
1075 tokenSlot = *tokenName -
'0';
1076 if( tokenSlot < 0 || tokenSlot > 9 )
1078 if( tokenSlot > slotCount - 1 )
1080 status = C_GetTokenInfo( slotList[ tokenSlot ], &tokenInfo );
1087 for( tokenSlot = 0; tokenSlot < slotCount && \
1091 status = C_GetTokenInfo( slotList[ tokenSlot ], &tokenInfo );
1092 if( status ==
CKR_OK && \
1096 if( tokenSlot >= FAILSAFE_ITERATIONS_MED )
1098 if( tokenSlot >= slotCount )
1102 pkcs11Info->
slotID = slotList[ tokenSlot ];
1105 status = C_GetSlotInfo( pkcs11Info->
slotID, &slotInfo );
1108 shutdownFunction( deviceInfo );
1116 status = C_GetTokenInfo( pkcs11Info->
slotID, &tokenInfo );
1119 shutdownFunction( deviceInfo );
1125 deviceInfo->getRandomFunction = getRandomFunction;
1148 const time_t theTime = getTokenTime( &tokenInfo );
1149 const time_t currentTime =
getTime();
1168 theTime >= currentTime - 86400 && \
1169 theTime <= currentTime + 86400 )
1176 assert( ( pkcs11InfoTbl[ pkcs11Info->
deviceNo ].name[ 0 ] && \
1178 "Software", 8 ) ) || \
1179 theTime < currentTime - 1 || theTime > currentTime + 1 );
1218 labelPtr = (
char * ) tokenInfo.
label;
1219 for( labelLength = 32;
1220 labelLength > 0 && \
1221 ( labelPtr[ labelLength - 1 ] ==
' ' || \
1222 !labelPtr[ labelLength - 1 ] );
1224 while( labelLength > 0 && *labelPtr ==
' ' )
1230 if( labelLength > 0 )
1232 memcpy( pkcs11Info->label, labelPtr, labelLength );
1233 pkcs11Info->
labelLen = labelLength;
1240 if( pkcs11InfoTbl[ pkcs11Info->
deviceNo ].name[ 0 ] )
1243 min( strlen( pkcs11InfoTbl[ pkcs11Info->
deviceNo ].name ),
1245 memcpy( pkcs11Info->label,
1246 pkcs11InfoTbl[ pkcs11Info->
deviceNo ].name, labelLength );
1250 deviceInfo->label = pkcs11Info->label;
1258 status = C_OpenSession( pkcs11Info->
slotID,
1260 NULL_PTR, &hSession );
1262 status = C_OpenSession( pkcs11Info->
slotID,
1285 return( cryptStatus );
1287 ENSURES( hSession != CK_OBJECT_NONE );
1295 mechanismInfoPtr = getMechanismInfoPKC( &mechanismInfoSize );
1296 cryptStatus = getCapabilities( deviceInfo, mechanismInfoPtr,
1297 mechanismInfoSize );
1298 mechanismInfoPtr = getMechanismInfoConv( &mechanismInfoSize );
1299 cryptStatus2 = getCapabilities( deviceInfo, mechanismInfoPtr,
1300 mechanismInfoSize );
1303 shutdownFunction( deviceInfo );
1313 int initPKCS11Init(
DEVICE_INFO *deviceInfo,
const char *name,
1314 const int nameLength )
1316 PKCS11_INFO *pkcs11Info = deviceInfo->devicePKCS11;
1318 int i, driverNameLength = nameLength;
1324 assert(
isReadPtr( name, nameLength ) );
1327 if( !pkcs11Initialised )
1332 for( i = 1; i < nameLength - 1; i++ )
1334 if( name[ i ] ==
':' && name[ i + 1 ] ==
':' )
1336 driverNameLength = i;
1352 if( driverNameLength == 12 && \
1353 !strnicmp(
"[Autodetect]", name, driverNameLength ) )
1355 if( !pkcs11InfoTbl[ 0 ].name[ 0 ] )
1362 for( i = 0; i < MAX_PKCS11_DRIVERS; i++ )
1364 if( !strnicmp( pkcs11InfoTbl[ i ].name, name, driverNameLength ) )
1367 if( i >= MAX_PKCS11_DRIVERS )
1374 deviceInfo->initFunction = initFunction;
1375 deviceInfo->shutdownFunction = shutdownFunction;
1376 deviceInfo->devicePKCS11->functionListPtr = \
1377 pkcs11InfoTbl[ pkcs11Info->
deviceNo ].functionListPtr;