corePKCS11  v3.2.0
PKCS #11 Cryptoki Library
C_OpenSession

Opens a connection between an application and a particular token or sets up an application callback for token insertion.

CK_DECLARE_FUNCTION( CK_RV, C_OpenSession )( CK_SLOT_ID slotID,
CK_FLAGS flags,
CK_VOID_PTR pApplication,
CK_NOTIFY Notify,
CK_SESSION_HANDLE_PTR phSession )
{
CK_RV xResult = CKR_OK;
P11Session_t * pxSessionObj = NULL;
uint32_t ulSessionCount = 0;
( void ) ( slotID );
( void ) ( pApplication );
/* Allow unused parameters to be cast to void to silence compiler warnings.
* Even if they are a function pointer. */
/* coverity[misra_c_2012_rule_11_1_violation] */
( void ) Notify;
/* Check that the PKCS #11 module is initialized. */
/* See explanation in prvCheckValidSessionAndModule for this exception. */
/* coverity[misra_c_2012_rule_10_5_violation] */
if( xP11Context.xIsInitialized != ( CK_BBOOL ) CK_TRUE )
{
xResult = CKR_CRYPTOKI_NOT_INITIALIZED;
LogError( ( "Could not open a session. PKCS #11 must be initialized "
"before any operations." ) );
}
/* Check arguments. */
if( NULL == phSession )
{
xResult = CKR_ARGUMENTS_BAD;
LogError( ( "Could not open a session. phSession cannot be a NULL pointer." ) );
}
/* For legacy reasons, the CKF_SERIAL_SESSION bit MUST always be set. */
if( ( CKR_OK == xResult ) && ( 0UL == ( CKF_SERIAL_SESSION & flags ) ) )
{
xResult = CKR_SESSION_PARALLEL_NOT_SUPPORTED;
LogError( ( "Could not open a session. CKR_SESSION_PARALLEL_NOT_SUPPORTED "
"must always be a set flag." ) );
}
/*
* Make space for the context.
*/
if( CKR_OK == xResult )
{
/* Get next open session slot. */
if( mbedtls_mutex_lock( &xP11Context.xSessionMutex ) == 0 )
{
for( ulSessionCount = 0; ulSessionCount < pkcs11configMAX_SESSIONS; ++ulSessionCount )
{
/* coverity[misra_c_2012_rule_10_5_violation] */
if( pxP11Sessions[ ulSessionCount ].xOpened == ( CK_BBOOL ) CK_FALSE )
{
xResult = CKR_OK;
pxSessionObj = &pxP11Sessions[ ulSessionCount ];
/* coverity[misra_c_2012_rule_10_5_violation] */
pxSessionObj->xOpened = ( CK_BBOOL ) CK_TRUE;
break;
}
else
{
xResult = CKR_SESSION_COUNT;
}
}
( void ) mbedtls_mutex_unlock( &xP11Context.xSessionMutex );
}
else
{
xResult = CKR_FUNCTION_FAILED;
LogError( ( "Could not open a session. Unsuccessful in taking xSessionMutex." ) );
}
if( CKR_OK == xResult )
{
mbedtls_mutex_init( &pxSessionObj->xSignMutex );
mbedtls_mutex_init( &pxSessionObj->xVerifyMutex );
}
}
if( CKR_OK == xResult )
{
/*
* Assign the session.
*/
pxSessionObj->ulState =
( 0UL != ( flags & CKF_RW_SESSION ) ) ? CKS_RW_PUBLIC_SESSION : CKS_RO_PUBLIC_SESSION;
LogDebug( ( "Assigned a 0x%0lX Type Session.", ( unsigned long int ) pxSessionObj->ulState ) );
}
/*
* Initialize the operation in progress.
*/
if( CKR_OK == xResult )
{
LogDebug( ( "Assigned Mechanisms to no operation in progress." ) );
}
if( xResult == CKR_SESSION_COUNT )
{
/* No available session. */
LogError( ( "Could not open a session. All sessions have "
"been taken. Consider increasing value of "
"pkcs11configMAX_SESSIONS." ) );
}
if( CKR_OK == xResult )
{
/* Increment by one, as invalid handles in PKCS #11 are 0. */
++ulSessionCount;
*phSession = ulSessionCount;
LogDebug( ( "Current session count at %lu", ( unsigned long int ) ( ulSessionCount - 1UL ) ) );
}
return xResult;
}
Note
PKCS #11 module must have been previously initialized with a call to C_Initialize() before calling C_OpenSession().
Parameters
[in]slotIDThis parameter is unused in this port.
[in]flagsSession flags - CKF_SERIAL_SESSION is a mandatory flag.
[in]pApplicationThis parameter is unused in this port.
[in]NotifyThis parameter is unused in this port.
[in]phSessionPointer to the location that the created session's handle will be placed.
Returns
CKR_OK if successful.
P11Session_t::xOperationVerifyMechanism
CK_MECHANISM_TYPE xOperationVerifyMechanism
The mechanism of verify operation in progress. Set during C_VerifyInit.
Definition: core_pkcs11_mbedtls.c:292
P11Session_t::xOperationDigestMechanism
CK_MECHANISM_TYPE xOperationDigestMechanism
Indicates if a digest operation is in progress.
Definition: core_pkcs11_mbedtls.c:289
P11Session_t::xOperationSignMechanism
CK_MECHANISM_TYPE xOperationSignMechanism
Mechanism of the sign operation in progress. Set during C_SignInit.
Definition: core_pkcs11_mbedtls.c:296
P11Session_t::xVerifyMutex
mbedtls_threading_mutex_t xVerifyMutex
Protects the verification key from being modified while in use.
Definition: core_pkcs11_mbedtls.c:293
P11Struct_t::xIsInitialized
CK_BBOOL xIsInitialized
Indicates whether PKCS #11 module has been initialized with a call to C_Initialize.
Definition: core_pkcs11_mbedtls.c:272
xP11Context
static P11Struct_t xP11Context
The global PKCS #11 module object. Entropy/randomness and object lists are shared across PKCS #11 ses...
Definition: core_pkcs11_mbedtls.c:313
LogDebug
#define LogDebug(message)
Macro that is called in the corePKCS11 library for logging "Debug" level messages.
Definition: core_pkcs11_config_defaults.h:375
C_OpenSession
CK_RV C_OpenSession(CK_SLOT_ID slotID, CK_FLAGS flags, CK_VOID_PTR pApplication, CK_NOTIFY Notify, CK_SESSION_HANDLE_PTR phSession)
Opens a connection between an application and a particular token or sets up an application callback f...
Definition: core_pkcs11_mbedtls.c:1762
P11Struct_t::xSessionMutex
mbedtls_threading_mutex_t xSessionMutex
Mutex that protects write operations to the pxSession array.
Definition: core_pkcs11_mbedtls.c:275
P11Session_t::xSignMutex
mbedtls_threading_mutex_t xSignMutex
Protects the signing key from being modified while in use.
Definition: core_pkcs11_mbedtls.c:297
pxP11Sessions
static P11Session_t pxP11Sessions[pkcs11configMAX_SESSIONS]
The global PKCS #11 session list.
Definition: core_pkcs11_mbedtls.c:318
CK_DECLARE_FUNCTION
#define CK_DECLARE_FUNCTION(returnType, name)
Macro for defining a PKCS #11 functions.
Definition: core_pkcs11.h:75
pkcs11configMAX_SESSIONS
#define pkcs11configMAX_SESSIONS
Maximum number of sessions that can be stored by the PKCS #11 module.
Definition: core_pkcs11_config_defaults.h:129
P11Session_t
Session structure.
Definition: core_pkcs11_mbedtls.c:286
P11Session_t::ulState
CK_ULONG ulState
Stores the session flags.
Definition: core_pkcs11_mbedtls.c:287
P11Session_t::xOpened
CK_BBOOL xOpened
Set to CK_TRUE upon opening PKCS #11 session.
Definition: core_pkcs11_mbedtls.c:288
pkcs11NO_OPERATION
#define pkcs11NO_OPERATION
Indicates that no PKCS #11 operation is underway for given session.
Definition: core_pkcs11_mbedtls.c:106
LogError
#define LogError(message)
Macro that is called in the corePKCS11 library for logging "Error" level messages.
Definition: core_pkcs11_config_defaults.h:315