Home | History | Annotate | Download | only in tf_crypto_sst
      1 /**
      2  * Copyright(c) 2011 Trusted Logic.   All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  *
      8  *  * Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  *  * Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in
     12  *    the documentation and/or other materials provided with the
     13  *    distribution.
     14  *  * Neither the name Trusted Logic nor the names of its
     15  *    contributors may be used to endorse or promote products derived
     16  *    from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #ifndef __PKCS11_INTERNAL_H__
     32 #define __PKCS11_INTERNAL_H__
     33 
     34 #define CRYPTOKI_EXPORTS
     35 #include "cryptoki.h"
     36 #include "service_system_protocol.h"
     37 
     38 #include "lib_object.h"
     39 #include "lib_mutex.h"
     40 #include "tee_client_api.h"
     41 
     42 #include <stdlib.h>
     43 #include <string.h>
     44 
     45 
     46 /**
     47  * The magic word.
     48  */
     49 #define PKCS11_SESSION_MAGIC  ( (uint32_t)0x45EF683B )
     50 
     51 /**
     52  * Computes required size to fit in a 4-bytes aligned buffer (at the end)
     53  * If the size is a multiple of 4, just returns the size
     54  * Otherwise, return the size so that the (end of the buffer)+1 is 4-bytes aligned.
     55  */
     56 #define PKCS11_GET_SIZE_WITH_ALIGNMENT(a)  (uint32_t)(((uint32_t)a+3) & ~3)
     57 
     58 
     59 /**
     60  * The System Service UUID used by the library
     61  */
     62 extern const TEEC_UUID SERVICE_UUID;
     63 
     64 /**
     65  * g_sContext: the global TEEC context used by the library
     66  */
     67 extern TEEC_Context  g_sContext;
     68 
     69 void stubMutexLock(void);
     70 void stubMutexUnlock(void);
     71 TEEC_Result stubInitializeContext(void);
     72 void stubFinalizeContext(void);
     73 
     74 /** Whether the cryptoki library is initialized or not */
     75 extern bool g_bCryptokiInitialized;
     76 
     77 CK_RV ckInternalTeeErrorToCKError(TEEC_Result nError);
     78 
     79 #define PKCS11_PRIMARY_SESSION_TAG    1
     80 #define PKCS11_SECONDARY_SESSION_TAG  2
     81 
     82 typedef struct
     83 {
     84    /*
     85     * Magic word, must be set to {PKCS11_SESSION_MAGIC}.
     86     */
     87    uint32_t    nMagicWord;
     88 
     89    /* nSessionTag must be set to {PKCS11_PRIMARY_SESSION_TAG} for primary session
     90    *                          to {PKCS11_SECONDARY_SESSION_TAG} for secondary session */
     91    uint32_t    nSessionTag;
     92 
     93 }PKCS11_SESSION_CONTEXT_HEADER, * PPKCS11_SESSION_CONTEXT_HEADER;
     94 
     95 /**
     96  * The PKCS11 Primary session context
     97  */
     98 typedef struct
     99 {
    100    /* sHeader must be the first field of this structure */
    101    PKCS11_SESSION_CONTEXT_HEADER sHeader;
    102 
    103    /* TEEC session used for this cryptoki primary session.
    104       Each primary session has its own TEEC session */
    105    TEEC_Session sSession;
    106    uint32_t     hCryptoSession;
    107 
    108    /* Mutex to protect the table of secondary sessions */
    109    LIB_MUTEX sSecondarySessionTableMutex;
    110 
    111    /* Table of secondary sessions */
    112    LIB_OBJECT_TABLE_HANDLE16 sSecondarySessionTable;
    113 
    114 } PKCS11_PRIMARY_SESSION_CONTEXT, * PPKCS11_PRIMARY_SESSION_CONTEXT;
    115 
    116 /**
    117  * The PKCS11 Secondary session context
    118  */
    119 typedef struct
    120 {
    121    /* sHeader must be the first field of this structure */
    122    PKCS11_SESSION_CONTEXT_HEADER sHeader;
    123 
    124    /* Secondary session handle as returned by pkcs11 */
    125    uint32_t       hSecondaryCryptoSession;
    126 
    127    /* A node of the table of secondary sessions */
    128    LIB_OBJECT_NODE_HANDLE16 sSecondarySessionNode;
    129 
    130    /* pointer to the primary session */
    131    PKCS11_PRIMARY_SESSION_CONTEXT* pPrimarySession;
    132 
    133 } PKCS11_SECONDARY_SESSION_CONTEXT, *PPKCS11_SECONDARY_SESSION_CONTEXT;
    134 
    135 bool ckInternalSessionIsOpenedEx(S_HANDLE hSession, bool* pBoolIsPrimarySession);
    136 
    137 #endif /* __PKCS11_INTERNAL_H__ */
    138