108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic/**
208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic * Copyright(c) 2011 Trusted Logic.   All rights reserved.
308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic *
408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic * Redistribution and use in source and binary forms, with or without
508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic * modification, are permitted provided that the following conditions
608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic * are met:
708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic *
808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic *  * Redistributions of source code must retain the above copyright
908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic *    notice, this list of conditions and the following disclaimer.
1008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic *  * Redistributions in binary form must reproduce the above copyright
1108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic *    notice, this list of conditions and the following disclaimer in
1208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic *    the documentation and/or other materials provided with the
1308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic *    distribution.
1408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic *  * Neither the name Trusted Logic nor the names of its
1508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic *    contributors may be used to endorse or promote products derived
1608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic *    from this software without specific prior written permission.
1708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic *
1808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic */
3008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
3108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic#include "pkcs11_internal.h"
3208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
3308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic/* ------------------------------------------------------------------------
3408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted LogicInternal Functions
3508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic------------------------------------------------------------------------- */
3608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic/**
3708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic* Checks the following pre-conditions:
3808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic* - cryptoki is initialized,
3908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic* - hSession is valid (primary and/or secondary),
4008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic* - the user is logged in.
4108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic*
4208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic* And updates handle values:
4308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic*  IN/OUT : phSession
4408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic*           IN  = Cryptoki external handle
4508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic*           OUT = TFAPI handle = primary cryptoki session handle
4608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic*  OUT    : phSecSession16Msb
4708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic*           OUT = 0 for a primary session or
4808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic*                 the secondary cryptoki session handle in the 16 MSB bits
4908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic*/
5008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logicstatic CK_RV static_checkPreConditionsAndUpdateHandles(
5108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         CK_SESSION_HANDLE*   phSession,
5208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         uint32_t*            phCommandIDAndSession,
5308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         PPKCS11_PRIMARY_SESSION_CONTEXT* ppSession)
5408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic{
5508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   bool  bIsPrimarySession;
5608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
5708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   /* Check Cryptoki is initialized */
5808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (!g_bCryptokiInitialized)
5908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
6008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      return CKR_CRYPTOKI_NOT_INITIALIZED;
6108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
6208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
6308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (phSession == NULL)
6408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
6508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      return CKR_SESSION_HANDLE_INVALID;
6608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
6708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
6808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   /* Check that the session is valid */
6908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (!ckInternalSessionIsOpenedEx(*phSession, &bIsPrimarySession))
7008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
7108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      return CKR_SESSION_HANDLE_INVALID;
7208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
7308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   /* previous check is fine, then update session handles */
7408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (bIsPrimarySession)
7508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
7608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      PPKCS11_PRIMARY_SESSION_CONTEXT pSession =
7708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         (PPKCS11_PRIMARY_SESSION_CONTEXT)(*phSession);
7808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
7908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      *phSession = pSession->hCryptoSession;
8008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      *phCommandIDAndSession = (pSession->hCryptoSession<<16)|(*phCommandIDAndSession&0x00007FFF);
8108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      *ppSession = pSession;
8208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
8308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   else
8408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
8508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      PPKCS11_SECONDARY_SESSION_CONTEXT pSecSession =
8608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         (PPKCS11_SECONDARY_SESSION_CONTEXT)(*phSession);
8708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
8808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      *phSession = pSecSession->pPrimarySession->hCryptoSession;
8908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      *phCommandIDAndSession = (pSecSession->hSecondaryCryptoSession<<16)|(1<<15)|(*phCommandIDAndSession&0x00007FFF);
9008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      *ppSession = pSecSession->pPrimarySession;
9108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
9208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
9308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   return CKR_OK;
9408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic}
9508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
9608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic/******************************************/
9708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic/* The buffer must be freed by the caller */
9808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic/******************************************/
9908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logicstatic CK_RV static_encodeTwoTemplates(
10008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint8_t**         ppBuffer,
10108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t *        pBufferSize,
10208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   const uint32_t    nParamIndex,
10308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_ATTRIBUTE*     pTemplate1,
10408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_ULONG          ulCount1,
10508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_ATTRIBUTE*     pTemplate2,
10608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_ULONG          ulCount2)
10708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic{
10808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   INPUT_TEMPLATE_ITEM  sItem;
10908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
11008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t i;
11108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t nDataOffset    = 0;
11208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t nBufferIndex   = 0;
11308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t nBufferSize    = 0;
11408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint8_t* pBuffer = NULL;
11508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_RV    nErrorCode = CKR_OK;
11608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
11708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (ulCount1 == 0)
11808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
11908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      /* Nothing to do */
12008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      return CKR_OK;
12108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
12208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (pTemplate1 == NULL)
12308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
12408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      /* Nothing to do */
12508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      return CKR_OK;
12608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
12708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
12808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   /* First compute the total required buffer size that
12908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic    * will contain the full templates (for the template 1 AND 2)
13008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic    */
13108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   nBufferSize =  4 +                                    /* Nb Attributes */
13208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                  sizeof(INPUT_TEMPLATE_ITEM)*ulCount1;  /* The attributes items */
13308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (pTemplate2 != NULL)
13408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
13508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      nBufferSize += 4 +                                    /* Nb Attributes */
13608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                     sizeof(INPUT_TEMPLATE_ITEM)*ulCount2;  /* The attributes items */
13708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
13808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
13908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   /* First data (attribute values) on either template 1 or 2 will just be after the last item */
14008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   nDataOffset = nBufferSize;
14108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
14208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   for (i = 0; i < ulCount1; i++)
14308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
14408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      /* Each value will be aligned on 4 bytes.
14508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         This computation includes the spare bytes. */
14608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      nBufferSize += PKCS11_GET_SIZE_WITH_ALIGNMENT(pTemplate1[i].ulValueLen);
14708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
14808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (pTemplate2 != NULL)
14908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
15008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      for (i = 0; i < ulCount2; i++)
15108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      {
15208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         /* Each value will be aligned on 4 bytes.
15308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic            This computation includes the spare bytes. */
15408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         nBufferSize += PKCS11_GET_SIZE_WITH_ALIGNMENT(pTemplate2[i].ulValueLen);
15508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      }
15608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
15708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
15808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   pBuffer = (uint8_t*)malloc(nBufferSize);
15908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (pBuffer == NULL)
16008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
16108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      /* Not enough memory */
16208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      return CKR_DEVICE_MEMORY;
16308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
16408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
16508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   memset(pBuffer, 0, nBufferSize);
16608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
16708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   /*
16808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic    * First template
16908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic    */
17008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   *(uint32_t*)(pBuffer + nBufferIndex) = ulCount1;
17108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   nBufferIndex += 4;
17208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   for (i = 0; i < ulCount1; i++)
17308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
17408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      sItem.attributeType     = (uint32_t)pTemplate1[i].type;
17508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      /* dataOffset = 0 means NULL buffer */
17608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      sItem.dataOffset        = ((pTemplate1[i].pValue == NULL) ? 0 : nDataOffset);
17708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      sItem.dataParamIndex    = nParamIndex; /* The parameter where we store the data (0 to 3) */
17808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      sItem.dataValueLen      = (uint32_t)pTemplate1[i].ulValueLen;
17908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      /* Copy the item */
18008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      memcpy(pBuffer + nBufferIndex, &sItem, sizeof(INPUT_TEMPLATE_ITEM));
18108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      nBufferIndex += sizeof(INPUT_TEMPLATE_ITEM);
18208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      if (pTemplate1[i].pValue != NULL)
18308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      {
18408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         /* Copy the data */
18508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         memcpy(pBuffer + nDataOffset, (uint8_t*)pTemplate1[i].pValue, (uint32_t)pTemplate1[i].ulValueLen);
18608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         /* Next data will be stored just after the previous one but aligned on 4 bytes */
18708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         nDataOffset += PKCS11_GET_SIZE_WITH_ALIGNMENT(pTemplate1[i].ulValueLen);
18808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         if ((nDataOffset & 0xC0000000) != 0)
18908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         {
19008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic            /* We whould never go in this case, that means the dataOffset will not be able to store the offset correctly */
19108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic            nErrorCode = CKR_DEVICE_ERROR;
19208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic            goto error;
19308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         }
19408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      }
19508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
19608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
19708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   /*
19808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic    * Second template
19908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic    */
20008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (pTemplate2 != NULL)
20108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
20208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      *(uint32_t*)(pBuffer + nBufferIndex) = ulCount2;
20308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      nBufferIndex += 4;
20408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      for (i = 0; i < ulCount2; i++)
20508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      {
20608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         sItem.attributeType     = (uint32_t)pTemplate2[i].type;
20708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         /* dataOffset = 0 means NULL buffer */
20808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         sItem.dataOffset        = ((pTemplate2[i].pValue == NULL) ? 0 : nDataOffset);
20908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         sItem.dataParamIndex    = nParamIndex; /* The parameter where we store the data (0..3) */
21008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         sItem.dataValueLen      = (uint32_t)pTemplate2[i].ulValueLen;
21108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         /* Copy the item */
21208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         memcpy(pBuffer + nBufferIndex, &sItem, sizeof(INPUT_TEMPLATE_ITEM));
21308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         nBufferIndex += sizeof(INPUT_TEMPLATE_ITEM);
21408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         if (pTemplate2[i].pValue != NULL)
21508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         {
21608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic            /* Copy the data */
21708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic            memcpy(pBuffer + nDataOffset, (uint8_t*)pTemplate2[i].pValue, (uint32_t)pTemplate2[i].ulValueLen);
21808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic            /* Next data will be stored just after the previous one but aligned on 4 bytes */
21908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic            nDataOffset += PKCS11_GET_SIZE_WITH_ALIGNMENT(pTemplate2[i].ulValueLen);
22008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic            if ((nDataOffset & 0xC0000000) != 0)
22108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic            {
22208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic               /* We whould never go in this case, that means the dataOffset will not be able to store the offset correctly */
22308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic               nErrorCode = CKR_DEVICE_ERROR;
22408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic               goto error;
22508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic            }
22608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         }
22708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      }
22808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
22908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
23008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   *ppBuffer      = pBuffer;
23108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   *pBufferSize   = nBufferSize;
23208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
23308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   return CKR_OK;
23408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
23508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logicerror:
23608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   free(pBuffer);
23708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   return nErrorCode;
23808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic}
23908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
24008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic/******************************************/
24108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic/* The buffer must be freed by the caller */
24208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic/******************************************/
24308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logicstatic CK_RV static_encodeTemplate(
24408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint8_t**         ppBuffer,
24508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t*         pBufferSize,
24608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   const uint32_t    nParamIndex,
24708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_ATTRIBUTE*     pTemplate,
24808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_ULONG          ulCount)
24908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic{
25008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   return static_encodeTwoTemplates(ppBuffer, pBufferSize, nParamIndex, pTemplate, ulCount, NULL, 0);
25108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic}
25208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic/* ----------------------------------------------------------------------- */
25308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
25408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logicstatic CK_RV static_C_CallInit(
25508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t            nCommandID,
25608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_SESSION_HANDLE   hSession,
25708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   const CK_MECHANISM* pMechanism,
25808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_OBJECT_HANDLE    hKey)
25908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic{
26008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   TEEC_Result    teeErr;
26108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t       nErrorOrigin;
26208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   TEEC_Operation sOperation;
26308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_RV          nErrorCode = CKR_OK;
26408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t       nCommandIDAndSession = nCommandID;
26508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t       nParamType2 = TEEC_NONE;
26608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   PPKCS11_PRIMARY_SESSION_CONTEXT pSession;
26708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
26808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   nErrorCode = static_checkPreConditionsAndUpdateHandles(&hSession, &nCommandIDAndSession, &pSession);
26908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (nErrorCode != CKR_OK)
27008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
27108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      return nErrorCode;
27208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
27308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (pMechanism == NULL)
27408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
27508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      return CKR_ARGUMENTS_BAD;
27608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
27708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
27808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   memset(&sOperation, 0, sizeof(TEEC_Operation));
27908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   sOperation.params[0].value.a = (uint32_t)pMechanism->mechanism;
28008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (nCommandID != SERVICE_SYSTEM_PKCS11_C_DIGESTINIT_COMMAND_ID)
28108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
28208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      sOperation.params[0].value.b = (uint32_t)hKey;
28308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
28408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
28508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   sOperation.params[1].tmpref.buffer = (uint8_t*)pMechanism->pParameter;
28608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   sOperation.params[1].tmpref.size   = (uint32_t)pMechanism->ulParameterLen;
28708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
28808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   /* Specific case of RSA OAEP */
28908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (((nCommandID == SERVICE_SYSTEM_PKCS11_C_ENCRYPTINIT_COMMAND_ID)
29008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      ||(nCommandID == SERVICE_SYSTEM_PKCS11_C_DECRYPTINIT_COMMAND_ID))
29108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      && (pMechanism->mechanism == CKM_RSA_PKCS_OAEP)
29208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      && (pMechanism->pParameter != NULL))
29308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
29408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      /* Add the source buffer of the RSA OAEP mechanism parameters */
29508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      nParamType2 = TEEC_MEMREF_TEMP_INPUT;
29608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      sOperation.params[2].tmpref.buffer = (uint8_t*)((CK_RSA_PKCS_OAEP_PARAMS_PTR)(pMechanism->pParameter))->pSourceData;
29708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      sOperation.params[2].tmpref.size   = (uint32_t) ((CK_RSA_PKCS_OAEP_PARAMS_PTR)(pMechanism->pParameter))->ulSourceDataLen;
29808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
29908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   sOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_MEMREF_TEMP_INPUT, nParamType2, TEEC_NONE);
30008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   teeErr = TEEC_InvokeCommand(   &pSession->sSession,
30108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                  nCommandIDAndSession,        /* commandID */
30208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                  &sOperation,                 /* IN OUT operation */
30308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                  &nErrorOrigin                /* OUT returnOrigin, optional */
30408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                 );
30508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   nErrorCode = (nErrorOrigin == TEEC_ORIGIN_TRUSTED_APP ?
30608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                  teeErr :
30708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                  ckInternalTeeErrorToCKError(teeErr));
30808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
30908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   return nErrorCode;
31008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic}
31108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
31208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic/* ----------------------------------------------------------------------- */
31308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic/**
31408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic* If bSend, the pData buffer is sent to the service.
31508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic* If bResult, a buffer is received, the convention described in
31608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic* PKCS11 Section 11.2 applies for pResult and pulResultLen.
31708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic* Specific function used for single operation
31808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic*/
31908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logicstatic CK_RV static_C_CallForSingle(
32008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t             nCommandID,
32108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_SESSION_HANDLE    hSession,
32208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   const CK_BYTE*       pData,
32308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_ULONG             ulDataLen,
32408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_BYTE*             pResult,
32508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_ULONG*            pulResultLen,
32608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   bool                 bSend,
32708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   bool                 bReceive)
32808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic{
32908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   TEEC_Result       teeErr;
33008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t          nErrorOrigin;
33108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   TEEC_Operation    sOperation;
33208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_RV             nErrorCode = CKR_OK;
33308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t          nCommandIDAndSession = nCommandID;
33408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t          nParamType0 = TEEC_NONE;
33508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t          nParamType1 = TEEC_NONE;
33608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   PPKCS11_PRIMARY_SESSION_CONTEXT pSession;
33708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
33808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   nErrorCode = static_checkPreConditionsAndUpdateHandles(&hSession, &nCommandIDAndSession, &pSession);
33908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (nErrorCode != CKR_OK)
34008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
34108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      return nErrorCode;
34208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
34308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
34408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   memset(&sOperation, 0, sizeof(TEEC_Operation));
34508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
34608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (bSend)
34708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
34808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      nParamType0 = TEEC_MEMREF_TEMP_INPUT;
34908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      sOperation.params[0].tmpref.buffer = (uint8_t*)pData;
35008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      sOperation.params[0].tmpref.size   = (uint32_t)ulDataLen;
35108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
35208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
35308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (bReceive)
35408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
35508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      if (pulResultLen == NULL)
35608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      {
35708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         /* The P11 API Spec states that, in this case, the operation must be
35808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic            aborted and the error code CKR_ARGUMENTS_BAD must be returned. We
35908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic            achieve this result by sending an invalid parameter type */
36008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         nParamType1 = TEEC_NONE;
36108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      }
36208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      else if (pResult == NULL)
36308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      {
36408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         /* If pResult is NULL, the caller only wants the buffer length.
36508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic            Send a NULL output memref */
36608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         nParamType1 = TEEC_MEMREF_TEMP_OUTPUT;
36708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         sOperation.params[1].tmpref.buffer = (uint8_t*)NULL;
36808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      }
36908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      else
37008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      {
37108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         /* send the result buffer information */
37208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         nParamType1 = TEEC_MEMREF_TEMP_OUTPUT;
37308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         sOperation.params[1].tmpref.buffer = (uint8_t*)pResult;
37408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         sOperation.params[1].tmpref.size   = (uint32_t)*pulResultLen;
37508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      }
37608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
37708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
37808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   sOperation.paramTypes = TEEC_PARAM_TYPES(nParamType0, nParamType1, TEEC_NONE, TEEC_NONE);
37908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   teeErr = TEEC_InvokeCommand(&pSession->sSession,
38008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                               nCommandIDAndSession,     /* commandID */
38108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                               &sOperation,              /* IN OUT operation */
38208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                               &nErrorOrigin             /* OUT returnOrigin, optional */
38308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                              );
38408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (teeErr != TEEC_SUCCESS)
38508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
38608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      nErrorCode = (nErrorOrigin == TEEC_ORIGIN_TRUSTED_APP ?
38708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                     teeErr :
38808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                     ckInternalTeeErrorToCKError(teeErr));
38908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      goto end;
39008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
39108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
39208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   /* Success */
39308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   nErrorCode = CKR_OK;
39408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
39508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic end:
39608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (bReceive)
39708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
39808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      if ((nErrorCode == CKR_OK) || (nErrorCode == CKR_BUFFER_TOO_SMALL))
39908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      {
40008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         /* The service has returned the actual result */
40108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         /* The data is already in pResult, we get the returned length */
40208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         *pulResultLen = sOperation.params[1].tmpref.size;
40308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      }
40408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
40508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
40608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   return nErrorCode;
40708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic}
40808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
40908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic/* ----------------------------------------------------------------------- */
41008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic/**
41108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic* If bSend, the pData buffer is sent to the service.
41208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic* If bResult, a buffer is received, the convention described in
41308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic* PKCS11 Section 11.2 applies for pResult and pulResultLen.
41408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic* Specific function only used for update operations
41508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic*/
41608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logicstatic CK_RV static_C_CallUpdate(
41708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t             nCommandID,
41808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_SESSION_HANDLE    hSession,
41908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   const CK_BYTE*       pData,
42008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_ULONG             ulDataLen,
42108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_BYTE*             pResult,
42208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_ULONG*            pulResultLen,
42308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   bool                 bSend,
42408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   bool                 bReceive)
42508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic{
42608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   TEEC_Result       teeErr;
42708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t          nErrorOrigin;
42808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   TEEC_Operation    sOperation;
42908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_RV             nErrorCode = CKR_OK;
43008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t          nResultLen = 0;
43108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t          nCommandIDAndSession = nCommandID;
43208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t          nParamType0 = TEEC_NONE;
43308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t          nParamType1 = TEEC_NONE;
43408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   PPKCS11_PRIMARY_SESSION_CONTEXT pSession;
43508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
43608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   nErrorCode = static_checkPreConditionsAndUpdateHandles(&hSession, &nCommandIDAndSession, &pSession);
43708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (nErrorCode != CKR_OK)
43808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
43908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      return nErrorCode;
44008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
44108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
44208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (pulResultLen != NULL)
44308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
44408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      nResultLen = *pulResultLen;
44508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
44608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
44708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   memset(&sOperation, 0, sizeof(TEEC_Operation));
44808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
44908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (bSend)
45008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
45108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      nParamType0 = TEEC_MEMREF_TEMP_INPUT;
45208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      sOperation.params[0].tmpref.buffer = (void*)pData;
45308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      sOperation.params[0].tmpref.size   = ulDataLen;
45408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
45508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
45608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (bReceive)
45708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
45808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      if (pulResultLen == NULL)
45908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      {
46008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         /* The P11 API Spec states that, in this case, the operation must be
46108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic            aborted and the error code CKR_ARGUMENTS_BAD must be returned. We
46208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic            achieve this result by setting an invalid parameter type */
46308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         nParamType1 = TEEC_NONE;
46408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      }
46508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      else if (pResult == NULL)
46608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      {
46708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         /* If pResult is NULL, the caller only wants the output buffer length.
46808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic            Pass a NULL output ref */
46908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         nParamType1 = TEEC_MEMREF_TEMP_OUTPUT;
47008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         sOperation.params[1].tmpref.buffer = NULL;
47108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      }
47208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      else
47308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      {
47408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         /* send the result buffer information */
47508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         nParamType1 = TEEC_MEMREF_TEMP_OUTPUT;
47608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         sOperation.params[1].tmpref.buffer = pResult;
47708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         sOperation.params[1].tmpref.size   = (uint32_t)*pulResultLen;
47808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      }
47908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
48008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
48108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   sOperation.paramTypes = TEEC_PARAM_TYPES(nParamType0, nParamType1, TEEC_NONE, TEEC_NONE);
48208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   teeErr = TEEC_InvokeCommand(   &pSession->sSession,
48308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                  nCommandIDAndSession,        /* commandID */
48408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                  &sOperation,                 /* IN OUT operation */
48508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                  &nErrorOrigin                /* OUT returnOrigin, optional */
48608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                 );
48708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (teeErr != TEEC_SUCCESS)
48808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
48908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      nErrorCode = (nErrorOrigin == TEEC_ORIGIN_TRUSTED_APP ?
49008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                     teeErr :
49108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                     ckInternalTeeErrorToCKError(teeErr));
49208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      goto end;
49308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
49408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
49508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   /* Success */
49608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   nErrorCode = CKR_OK;
49708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
49808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic end:
49908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (bReceive)
50008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
50108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      if ((nErrorCode == CKR_OK) || (nErrorCode == CKR_BUFFER_TOO_SMALL))
50208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      {
50308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         /* The service has returned the actual result */
50408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         /* The data is already in pResult, we get the returned length */
50508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         *pulResultLen = sOperation.params[1].tmpref.size;
50608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      }
50708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
50808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
50908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   return nErrorCode;
51008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic}
51108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
51208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic/* Splits the buffer pData in chunks of nChunkSize size
51308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic * and calls static_C_CallUpdate for each chunk
51408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic */
51508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logicstatic CK_RV static_C_CallSplitUpdate(
51608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                           uint32_t           nCommandID,
51708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                           CK_SESSION_HANDLE  hSession,
51808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                           const CK_BYTE*     pData,
51908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                           CK_ULONG           ulDataLen,
52008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                           CK_BYTE*           pResult,
52108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                           CK_ULONG*          pulResultLen,
52208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                           bool               bSend,
52308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                           bool               bReceive,
52408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                           uint32_t           nChunkSize)
52508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic{
52608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_RV nErrorCode;
52708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_ULONG nPartDataLen;
52808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_ULONG nPartResultLen = 0;
52908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_ULONG ulResultLen = 0;
53008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   bool bIsSymOperation = false;
53108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
53208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (pulResultLen != NULL)
53308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
53408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      ulResultLen = *pulResultLen;
53508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      /* Check wether the operation is a symetrical or asymetrical */
53608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      if (*pulResultLen == ulDataLen)
53708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      {
53808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         bIsSymOperation = true;
53908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      }
54008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      *pulResultLen = 0;
54108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
54208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
54308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   while (ulDataLen > 0)
54408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
54508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      nPartDataLen = (ulDataLen <= nChunkSize ?
54608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                        ulDataLen : nChunkSize);
54708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      if (bIsSymOperation)
54808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      {
54908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         /* update the result only if it is a symetric operation */
55008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         nPartResultLen = (ulResultLen <= nChunkSize ?
55108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                               ulResultLen : nChunkSize);
55208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      }
55308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      else
55408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      {
55508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         nPartResultLen = ulResultLen;
55608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      }
55708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
55808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      nErrorCode =  static_C_CallUpdate(
55908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                 nCommandID,
56008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                 hSession,
56108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                 pData,
56208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                 nPartDataLen,
56308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                 pResult,
56408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                 &nPartResultLen,
56508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                 bSend,
56608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                 bReceive);
56708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      if (nErrorCode != CKR_OK)
56808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      {
56908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         return nErrorCode;
57008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      }
57108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
57208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      ulDataLen -= nPartDataLen;
57308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      pData += nPartDataLen;
57408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
57508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      if (pResult != NULL)
57608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      {
57708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         ulResultLen -= nPartResultLen;
57808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         pResult += nPartResultLen;
57908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      }
58008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
58108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      if ((pulResultLen != NULL) && (bIsSymOperation))
58208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      {
58308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         *pulResultLen += nPartResultLen;
58408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      }
58508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
58608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   return CKR_OK;
58708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic}
58808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
58908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic/* Decides whether to split or not the inout/output buffer into chunks
59008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic*/
59108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logicstatic CK_RV static_C_Call_CallForUpdate(
59208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                           uint32_t           nCommandID,
59308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                           CK_SESSION_HANDLE  hSession,
59408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                           const CK_BYTE*     pData,
59508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                           CK_ULONG           ulDataLen,
59608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                           CK_BYTE*           pResult,
59708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                           CK_ULONG*          pulResultLen,
59808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                           bool               bSend,
59908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                           bool               bReceive)
60008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic{
60108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_RV                   nErrorCode;
60208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t                nChunkSize;
60308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
60408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   TEEC_ImplementationLimits  limits;
60508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
60608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (!g_bCryptokiInitialized)
60708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
60808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      return CKR_CRYPTOKI_NOT_INITIALIZED;
60908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
61008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
61108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   TEEC_GetImplementationLimits(&limits);
61208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
61308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   /* We can split the buffer in chunks of fixed size.
61408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      No matter of the start address of the buffer,
61508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      a safe size would be TotalNumberOfPages - 1
61608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   */
61708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   nChunkSize = limits.tmprefMaxSize - limits.pageSize;
61808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
61908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (ulDataLen > nChunkSize)
62008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
62108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      /* inoutMaxSize = 0  means unlimited size */
62208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic       nErrorCode = static_C_CallSplitUpdate(nCommandID,
62308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                 hSession,
62408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                 pData,
62508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                 ulDataLen,
62608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                 pResult,
62708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                 pulResultLen,
62808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                 bSend,
62908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                 bReceive,
63008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                 nChunkSize);
63108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
63208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   else
63308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
63408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      nErrorCode = static_C_CallUpdate(nCommandID,
63508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                 hSession,
63608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                 pData,
63708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                 ulDataLen,
63808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                 pResult,
63908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                 pulResultLen,
64008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                 bSend,
64108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                 bReceive);
64208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
64308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   return nErrorCode;
64408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
64508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic}
64608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
64708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic/* ------------------------------------------------------------------------
64808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted LogicPublic Functions
64908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic------------------------------------------------------------------------- */
65008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
65108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted LogicCK_RV PKCS11_EXPORT C_CreateObject(
65208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_SESSION_HANDLE    hSession,    /* the session's handle */
65308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   const CK_ATTRIBUTE*  pTemplate,   /* the object's template */
65408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_ULONG             ulCount,     /* attributes in template */
65508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_OBJECT_HANDLE*    phObject)    /* receives new object's handle. */
65608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic{
65708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   TEEC_Result          teeErr;
65808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t             nErrorOrigin;
65908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   TEEC_Operation       sOperation;
66008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_RV                nErrorCode = CKR_OK;
66108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   PPKCS11_PRIMARY_SESSION_CONTEXT pSession;
66208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t             nCommandIDAndSession = SERVICE_SYSTEM_PKCS11_C_CREATEOBJECT_COMMAND_ID;
66308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint8_t*             pBuffer = NULL;
66408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t             nBufferSize = 0;
66508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
66608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if ( pTemplate == NULL || phObject == NULL )
66708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
66808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      return CKR_ARGUMENTS_BAD;
66908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
67008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
67108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   nErrorCode = static_checkPreConditionsAndUpdateHandles(&hSession, &nCommandIDAndSession, &pSession);
67208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (nErrorCode != CKR_OK)
67308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
67408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      return nErrorCode;
67508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
67608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
67708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   nErrorCode = static_encodeTemplate(&pBuffer, &nBufferSize, 0, (CK_ATTRIBUTE*)pTemplate, ulCount); /* Sets the template on the param 0 */
67808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (nErrorCode != CKR_OK)
67908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
68008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      return nErrorCode;
68108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
68208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
68308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   memset(&sOperation, 0, sizeof(TEEC_Operation));
68408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   sOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, TEEC_VALUE_OUTPUT, TEEC_NONE, TEEC_NONE);
68508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   sOperation.params[0].tmpref.buffer = pBuffer;
68608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   sOperation.params[0].tmpref.size   = nBufferSize;
68708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   teeErr = TEEC_InvokeCommand(   &pSession->sSession,
68808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                  nCommandIDAndSession,        /* commandID */
68908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                  &sOperation,                 /* IN OUT operation */
69008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                  &nErrorOrigin                /* OUT returnOrigin, optional */
69108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                 );
69208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   free(pBuffer);
69308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
69408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (teeErr != TEEC_SUCCESS)
69508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
69608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      nErrorCode = (nErrorOrigin == TEEC_ORIGIN_TRUSTED_APP ?
69708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                     teeErr :
69808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                     ckInternalTeeErrorToCKError(teeErr));
69908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      goto end;
70008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
70108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
70208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   *phObject = sOperation.params[1].value.a;
70308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
70408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   /* Success */
70508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   nErrorCode = CKR_OK;
70608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
70708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logicend:
70808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   return nErrorCode;
70908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic}
71008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
71108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted LogicCK_RV PKCS11_EXPORT C_DestroyObject(
71208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_SESSION_HANDLE hSession,  /* the session's handle */
71308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_OBJECT_HANDLE  hObject)   /* the object's handle */
71408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic{
71508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   TEEC_Result    teeErr;
71608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t       nErrorOrigin;
71708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   TEEC_Operation sOperation;
71808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_RV       nErrorCode = CKR_OK;
71908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t    nCommandIDAndSession = SERVICE_SYSTEM_PKCS11_C_DESTROYOBJECT_COMMAND_ID;
72008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   PPKCS11_PRIMARY_SESSION_CONTEXT pSession;
72108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
72208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   nErrorCode = static_checkPreConditionsAndUpdateHandles(&hSession, &nCommandIDAndSession, &pSession);
72308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (nErrorCode != CKR_OK)
72408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
72508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      return nErrorCode;
72608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
72708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
72808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   memset(&sOperation, 0, sizeof(TEEC_Operation));
72908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   sOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE, TEEC_NONE);
73008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   sOperation.params[0].value.a = (uint32_t)hObject;
73108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   teeErr = TEEC_InvokeCommand(   &pSession->sSession,
73208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                  nCommandIDAndSession,        /* commandID */
73308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                  &sOperation,                 /* IN OUT operation */
73408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                  &nErrorOrigin                /* OUT returnOrigin, optional */
73508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                 );
73608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   nErrorCode = (nErrorOrigin == TEEC_ORIGIN_TRUSTED_APP ?
73708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                  teeErr :
73808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                  ckInternalTeeErrorToCKError(teeErr));
73908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   return nErrorCode;
74008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic}
74108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
74208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted LogicCK_RV PKCS11_EXPORT C_GetAttributeValue(
74308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_SESSION_HANDLE hSession,   /* the session's handle */
74408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_OBJECT_HANDLE  hObject,    /* the object's handle */
74508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_ATTRIBUTE*     pTemplate,  /* specifies attributes, gets values */
74608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_ULONG          ulCount)    /* attributes in template */
74708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic{
74808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   TEEC_Result       teeErr;
74908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t          nErrorOrigin;
75008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   TEEC_Operation    sOperation;
75108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_RV             nErrorCode = CKR_OK;
75208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_RV             nFinalErrorCode = CKR_OK;
75308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t          i = 0;
75408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t          nCommandIDAndSession = SERVICE_SYSTEM_PKCS11_C_GETATTRIBUTEVALUE_COMMAND_ID;
75508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   PPKCS11_PRIMARY_SESSION_CONTEXT pSession;
75608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
75708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   nErrorCode = static_checkPreConditionsAndUpdateHandles(&hSession, &nCommandIDAndSession, &pSession);
75808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (nErrorCode != CKR_OK)
75908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
76008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      return nErrorCode;
76108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
76208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
76308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (pTemplate == NULL)
76408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
76508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      return CKR_ARGUMENTS_BAD;
76608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
76708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
76808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (ulCount == 0)
76908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
77008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      return CKR_OK;
77108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
77208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
77308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   for (i = 0; i < ulCount; i++)
77408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
77508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      memset(&sOperation, 0, sizeof(TEEC_Operation));
77608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      sOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_MEMREF_TEMP_OUTPUT, TEEC_NONE, TEEC_NONE);
77708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      sOperation.params[0].value.a = (uint32_t)hObject;
77808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      sOperation.params[0].value.b = (uint32_t)pTemplate[i].type;
77908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      sOperation.params[1].tmpref.buffer = pTemplate[i].pValue;
78008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      sOperation.params[1].tmpref.size   = pTemplate[i].ulValueLen;
78108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      teeErr = TEEC_InvokeCommand(   &pSession->sSession,
78208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                     nCommandIDAndSession,        /* commandID */
78308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                     &sOperation,                 /* IN OUT operation */
78408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                     &nErrorOrigin                /* OUT returnOrigin, optional */
78508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                    );
78608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      nErrorCode = (nErrorOrigin == TEEC_ORIGIN_TRUSTED_APP ?
78708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                     teeErr :
78808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                     ckInternalTeeErrorToCKError(teeErr));
78908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      if (nErrorCode != CKR_OK)
79008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      {
79108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         if (  (nErrorCode == CKR_ATTRIBUTE_SENSITIVE) ||
79208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic               (nErrorCode == CKR_ATTRIBUTE_TYPE_INVALID) ||
79308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic               (nErrorCode == CKR_BUFFER_TOO_SMALL))
79408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         {
79508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic            nFinalErrorCode = nErrorCode;
79608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         }
79708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         else
79808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         {
79908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic            /* Not some of the special error codes: this is fatal */
80008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic            return nErrorCode;
80108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         }
80208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      }
80308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
80408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      pTemplate[i].ulValueLen = sOperation.params[1].tmpref.size;
80508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
80608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
80708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   return nFinalErrorCode;
80808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic}
80908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
81008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted LogicCK_RV PKCS11_EXPORT C_FindObjectsInit(
81108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_SESSION_HANDLE    hSession,   /* the session's handle */
81208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   const CK_ATTRIBUTE*  pTemplate,  /* attribute values to match */
81308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_ULONG             ulCount)    /* attributes in search template */
81408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic{
81508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   TEEC_Result    teeErr;
81608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t       nErrorOrigin;
81708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   TEEC_Operation sOperation;
81808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_RV       nErrorCode = CKR_OK;
81908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   PPKCS11_PRIMARY_SESSION_CONTEXT pSession;
82008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t    nCommandIDAndSession = SERVICE_SYSTEM_PKCS11_C_FINDOBJECTSINIT_COMMAND_ID;
82108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint8_t*    pBuffer     = NULL;
82208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t    nBufferSize = 0;
82308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
82408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   nErrorCode = static_checkPreConditionsAndUpdateHandles(&hSession, &nCommandIDAndSession, &pSession);
82508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (nErrorCode != CKR_OK)
82608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
82708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      return nErrorCode;
82808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
82908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
83008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   nErrorCode = static_encodeTemplate(&pBuffer, &nBufferSize, 0, (CK_ATTRIBUTE*)pTemplate, ulCount); /* Sets the template on the param 0 */
83108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (nErrorCode != CKR_OK)
83208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
83308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      return nErrorCode;
83408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
83508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
83608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   memset(&sOperation, 0, sizeof(TEEC_Operation));
83708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   sOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, TEEC_NONE, TEEC_NONE, TEEC_NONE);
83808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   sOperation.params[0].tmpref.buffer = pBuffer;
83908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   sOperation.params[0].tmpref.size   = nBufferSize;
84008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   teeErr = TEEC_InvokeCommand(   &pSession->sSession,
84108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                  nCommandIDAndSession,        /* commandID */
84208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                  &sOperation,                 /* IN OUT operation */
84308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                  &nErrorOrigin                /* OUT returnOrigin, optional */
84408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                 );
84508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   free(pBuffer);
84608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
84708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   nErrorCode = (nErrorOrigin == TEEC_ORIGIN_TRUSTED_APP ?
84808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                  teeErr :
84908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                  ckInternalTeeErrorToCKError(teeErr));
85008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   return nErrorCode;
85108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic}
85208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
85308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
85408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted LogicCK_RV PKCS11_EXPORT C_FindObjects(
85508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_SESSION_HANDLE hSession,          /* the session's handle */
85608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_OBJECT_HANDLE* phObject,          /* receives object handle array */
85708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_ULONG          ulMaxObjectCount,  /* max handles to be returned */
85808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_ULONG*         pulObjectCount)    /* actual number returned */
85908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic{
86008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   TEEC_Result       teeErr;
86108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t          nErrorOrigin;
86208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   TEEC_Operation    sOperation;
86308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_RV             nErrorCode = CKR_OK;
86408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   PPKCS11_PRIMARY_SESSION_CONTEXT pSession;
86508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t          nCommandIDAndSession = SERVICE_SYSTEM_PKCS11_C_FINDOBJECTS_COMMAND_ID;
86608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
86708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if ( (phObject == NULL) || (pulObjectCount == NULL))
86808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
86908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      return CKR_ARGUMENTS_BAD;
87008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
87108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
87208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   *pulObjectCount = 0;
87308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
87408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   nErrorCode = static_checkPreConditionsAndUpdateHandles(&hSession, &nCommandIDAndSession, &pSession);
87508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (nErrorCode != CKR_OK)
87608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
87708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      return nErrorCode;
87808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
87908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
88008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   memset(&sOperation, 0, sizeof(TEEC_Operation));
88108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   sOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_OUTPUT, TEEC_NONE, TEEC_NONE, TEEC_NONE);
88208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   sOperation.params[0].tmpref.buffer = (uint8_t*)phObject;
88308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   sOperation.params[0].tmpref.size   = (uint32_t)ulMaxObjectCount * sizeof(uint32_t);
88408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
88508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   teeErr = TEEC_InvokeCommand(   &pSession->sSession,
88608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                  nCommandIDAndSession,        /* commandID */
88708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                  &sOperation,                 /* IN OUT operation */
88808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                  &nErrorOrigin                /* OUT returnOrigin, optional */
88908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                 );
89008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
89108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (teeErr != TEEC_SUCCESS)
89208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
89308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      nErrorCode = (nErrorOrigin == TEEC_ORIGIN_TRUSTED_APP ?
89408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                     teeErr :
89508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                     ckInternalTeeErrorToCKError(teeErr));
89608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      return nErrorCode;
89708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
89808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
89908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   *pulObjectCount = sOperation.params[0].tmpref.size / sizeof(uint32_t);
90008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
90108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   return CKR_OK;
90208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic}
90308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
90408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted LogicCK_RV PKCS11_EXPORT C_FindObjectsFinal(CK_SESSION_HANDLE hSession) /* the session's handle */
90508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic{
90608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   TEEC_Result    teeErr;
90708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t       nErrorOrigin;
90808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   TEEC_Operation sOperation;
90908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_RV          nErrorCode = CKR_OK;
91008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t       nCommandIDAndSession = SERVICE_SYSTEM_PKCS11_C_FINDOBJECTSFINAL_COMMAND_ID;
91108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   PPKCS11_PRIMARY_SESSION_CONTEXT pSession;
91208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
91308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   nErrorCode = static_checkPreConditionsAndUpdateHandles(&hSession, &nCommandIDAndSession, &pSession);
91408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (nErrorCode != CKR_OK)
91508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
91608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      return nErrorCode;
91708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
91808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
91908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   memset(&sOperation, 0, sizeof(TEEC_Operation));
92008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   teeErr = TEEC_InvokeCommand(   &pSession->sSession,
92108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                  nCommandIDAndSession,        /* commandID */
92208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                  &sOperation,                 /* IN OUT operation */
92308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                  &nErrorOrigin                /* OUT returnOrigin, optional */
92408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                 );
92508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
92608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   nErrorCode = (nErrorOrigin == TEEC_ORIGIN_TRUSTED_APP ?
92708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                  teeErr :
92808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                  ckInternalTeeErrorToCKError(teeErr));
92908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   return nErrorCode;
93008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic}
93108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
93208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
93308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted LogicCK_RV PKCS11_EXPORT C_DigestInit(
93408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_SESSION_HANDLE   hSession,   /* the session's handle */
93508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   const CK_MECHANISM* pMechanism) /* the digesting mechanism */
93608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic{
93708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   return static_C_CallInit(
93808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      SERVICE_SYSTEM_PKCS11_C_DIGESTINIT_COMMAND_ID,
93908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      hSession,
94008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      pMechanism,
94108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      CK_INVALID_HANDLE);
94208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic}
94308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
94408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted LogicCK_RV PKCS11_EXPORT C_Digest(
94508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_SESSION_HANDLE hSession,     /* the session's handle */
94608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   const CK_BYTE*    pData,        /* data to be digested */
94708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_ULONG          ulDataLen,    /* bytes of data to be digested */
94808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_BYTE*          pDigest,      /* receives the message digest */
94908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_ULONG*         pulDigestLen) /* receives byte length of digest */
95008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic{
95108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   return static_C_CallForSingle(
95208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      SERVICE_SYSTEM_PKCS11_C_DIGEST_COMMAND_ID,
95308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      hSession,
95408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      pData,
95508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      ulDataLen,
95608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      pDigest,
95708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      pulDigestLen,
95808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      TRUE,
95908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      TRUE);
96008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic}
96108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
96208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted LogicCK_RV PKCS11_EXPORT C_DigestUpdate(
96308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_SESSION_HANDLE hSession,  /* the session's handle */
96408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   const CK_BYTE*    pPart,     /* data to be digested */
96508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_ULONG          ulPartLen) /* bytes of data to be digested */
96608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic{
96708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   return static_C_Call_CallForUpdate(
96808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      SERVICE_SYSTEM_PKCS11_C_DIGESTUPDATE_COMMAND_ID,
96908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      hSession,
97008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      pPart,
97108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      ulPartLen,
97208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      NULL,
97308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      NULL,
97408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      TRUE,
97508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      FALSE);
97608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic}
97708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
97808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted LogicCK_RV PKCS11_EXPORT C_DigestFinal(
97908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_SESSION_HANDLE hSession,  /* the session's handle */
98008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_BYTE*       pDigest,      /* receives the message digest */
98108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_ULONG*      pulDigestLen) /* receives byte count of digest */
98208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic{
98308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   return static_C_Call_CallForUpdate(
98408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      SERVICE_SYSTEM_PKCS11_C_DIGESTFINAL_COMMAND_ID,
98508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      hSession,
98608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      NULL,
98708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      0,
98808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      pDigest,
98908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      pulDigestLen,
99008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      FALSE,
99108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      TRUE);
99208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic}
99308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
99408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
99508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted LogicCK_RV PKCS11_EXPORT C_SignInit(
99608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_SESSION_HANDLE    hSession,    /* the session's handle */
99708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   const CK_MECHANISM*  pMechanism,  /* the signature mechanism */
99808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_OBJECT_HANDLE     hKey)        /* handle of the signature key */
99908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic{
100008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   return static_C_CallInit(
100108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      SERVICE_SYSTEM_PKCS11_C_SIGNINIT_COMMAND_ID,
100208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      hSession,
100308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      pMechanism,
100408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      hKey);
100508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic}
100608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
100708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted LogicCK_RV PKCS11_EXPORT C_Sign(
100808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_SESSION_HANDLE hSession,        /* the session's handle */
100908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   const CK_BYTE*    pData,           /* the data (digest) to be signed */
101008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_ULONG          ulDataLen,       /* count of bytes to be signed */
101108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_BYTE*          pSignature,      /* receives the signature */
101208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_ULONG*         pulSignatureLen) /* receives byte count of signature */
101308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic{
101408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   return static_C_CallForSingle(
101508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      SERVICE_SYSTEM_PKCS11_C_SIGN_COMMAND_ID,
101608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      hSession,
101708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      pData,
101808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      ulDataLen,
101908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      pSignature,
102008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      pulSignatureLen,
102108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      TRUE,
102208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      TRUE);
102308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic}
102408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
102508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted LogicCK_RV PKCS11_EXPORT C_SignUpdate(
102608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_SESSION_HANDLE hSession,  /* the session's handle */
102708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   const CK_BYTE*    pPart,     /* the data (digest) to be signed */
102808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_ULONG          ulPartLen) /* count of bytes to be signed */
102908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic{
103008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   return static_C_Call_CallForUpdate(
103108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      SERVICE_SYSTEM_PKCS11_C_SIGNUPDATE_COMMAND_ID,
103208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      hSession,
103308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      pPart,
103408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      ulPartLen,
103508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      NULL,
103608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      NULL,
103708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      TRUE,
103808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      FALSE);
103908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic}
104008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
104108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted LogicCK_RV PKCS11_EXPORT C_SignFinal(
104208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_SESSION_HANDLE hSession,     /* the session's handle */
104308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_BYTE*       pSignature,      /* receives the signature */
104408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_ULONG*      pulSignatureLen) /* receives byte count of signature */
104508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic{
104608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   return static_C_Call_CallForUpdate(
104708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      SERVICE_SYSTEM_PKCS11_C_SIGNFINAL_COMMAND_ID,
104808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      hSession,
104908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      NULL,
105008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      0,
105108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      pSignature,
105208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      pulSignatureLen,
105308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      FALSE,
105408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      TRUE);
105508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic}
105608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
105708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted LogicCK_RV PKCS11_EXPORT C_EncryptInit(
105808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_SESSION_HANDLE   hSession,    /* the session's handle */
105908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   const CK_MECHANISM* pMechanism,  /* the encryption mechanism */
106008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_OBJECT_HANDLE    hKey)        /* handle of encryption key */
106108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic{
106208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   return static_C_CallInit(
106308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      SERVICE_SYSTEM_PKCS11_C_ENCRYPTINIT_COMMAND_ID,
106408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      hSession,
106508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      pMechanism,
106608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      hKey);
106708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic}
106808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
106908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted LogicCK_RV PKCS11_EXPORT C_Encrypt(
107008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_SESSION_HANDLE hSession,            /* the session's handle */
107108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   const CK_BYTE*    pData,               /* the plaintext data */
107208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_ULONG          ulDataLen,           /* bytes of plaintext data */
107308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_BYTE*          pEncryptedData,      /* receives encrypted data */
107408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_ULONG*         pulEncryptedDataLen) /* receives encrypted byte count */
107508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic{
107608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
107708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   return static_C_CallForSingle(
107808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      SERVICE_SYSTEM_PKCS11_C_ENCRYPT_COMMAND_ID,
107908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      hSession,
108008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      pData,
108108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      ulDataLen,
108208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      pEncryptedData,
108308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      pulEncryptedDataLen,
108408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      TRUE,
108508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      TRUE);
108608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic}
108708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
108808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
108908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
109008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted LogicCK_RV PKCS11_EXPORT C_EncryptUpdate(
109108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_SESSION_HANDLE hSession,           /* the session's handle */
109208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   const CK_BYTE*    pPart,              /* the plaintext data */
109308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_ULONG          ulPartLen,          /* bytes of plaintext data */
109408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_BYTE*          pEncryptedPart,     /* receives encrypted data */
109508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_ULONG*         pulEncryptedPartLen)/* receives encrypted byte count */
109608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic{
109708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   return static_C_Call_CallForUpdate(
109808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      SERVICE_SYSTEM_PKCS11_C_ENCRYPTUPDATE_COMMAND_ID,
109908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      hSession,
110008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      pPart,
110108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      ulPartLen,
110208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      pEncryptedPart,
110308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      pulEncryptedPartLen,
110408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      TRUE,
110508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      TRUE);
110608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic}
110708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
110808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted LogicCK_RV PKCS11_EXPORT C_EncryptFinal(
110908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_SESSION_HANDLE hSession,             /* the session's handle */
111008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_BYTE*       pLastEncryptedPart,      /* receives encrypted last part */
111108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_ULONG*      pulLastEncryptedPartLen) /* receives byte count */
111208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic{
111308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   return static_C_Call_CallForUpdate(
111408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      SERVICE_SYSTEM_PKCS11_C_ENCRYPTFINAL_COMMAND_ID,
111508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      hSession,
111608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      NULL,
111708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      0,
111808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      pLastEncryptedPart,
111908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      pulLastEncryptedPartLen,
112008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      FALSE,
112108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      TRUE);
112208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic}
112308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
112408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted LogicCK_RV PKCS11_EXPORT C_DecryptInit(
112508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_SESSION_HANDLE   hSession,    /* the session's handle */
112608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   const CK_MECHANISM* pMechanism,  /* the decryption mechanism */
112708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_OBJECT_HANDLE    hKey)        /* handle of the decryption key */
112808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic{
112908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   return static_C_CallInit(
113008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      SERVICE_SYSTEM_PKCS11_C_DECRYPTINIT_COMMAND_ID,
113108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      hSession,
113208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      pMechanism,
113308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      hKey);
113408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic}
113508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
113608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted LogicCK_RV PKCS11_EXPORT C_Decrypt(
113708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_SESSION_HANDLE hSession,           /* the session's handle */
113808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   const CK_BYTE*    pEncryptedData,     /* input encrypted data */
113908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_ULONG          ulEncryptedDataLen, /* count of bytes of input */
114008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_BYTE*          pData,              /* receives decrypted output */
114108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_ULONG*         pulDataLen)         /* receives decrypted byte count */
114208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic{
114308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
114408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   return static_C_CallForSingle(
114508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      SERVICE_SYSTEM_PKCS11_C_DECRYPT_COMMAND_ID,
114608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      hSession,
114708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      pEncryptedData,
114808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      ulEncryptedDataLen,
114908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      pData,
115008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      pulDataLen,
115108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      TRUE,
115208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      TRUE);
115308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic}
115408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
115508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted LogicCK_RV PKCS11_EXPORT C_DecryptUpdate(
115608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_SESSION_HANDLE hSession,            /* the session's handle */
115708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   const CK_BYTE*    pEncryptedPart,      /* input encrypted data */
115808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_ULONG          ulEncryptedPartLen,  /* count of bytes of input */
115908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_BYTE*          pPart,               /* receives decrypted output */
116008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_ULONG*         pulPartLen)          /* receives decrypted byte count */
116108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic{
116208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   return static_C_Call_CallForUpdate(
116308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      SERVICE_SYSTEM_PKCS11_C_DECRYPTUPDATE_COMMAND_ID,
116408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      hSession,
116508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      pEncryptedPart,
116608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      ulEncryptedPartLen,
116708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      pPart,
116808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      pulPartLen,
116908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      TRUE,
117008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      TRUE);
117108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic}
117208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
117308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted LogicCK_RV PKCS11_EXPORT C_DecryptFinal(
117408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_SESSION_HANDLE hSession,    /* the session's handle */
117508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_BYTE*       pLastPart,      /* receives decrypted output */
117608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_ULONG*      pulLastPartLen) /* receives decrypted byte count */
117708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic{
117808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   return static_C_Call_CallForUpdate(
117908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      SERVICE_SYSTEM_PKCS11_C_DECRYPTFINAL_COMMAND_ID,
118008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      hSession,
118108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      NULL,
118208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      0,
118308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      pLastPart,
118408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      pulLastPartLen,
118508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      FALSE,
118608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      TRUE);
118708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic}
118808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
118908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
119008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted LogicCK_RV PKCS11_EXPORT C_GenerateKey(
119108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_SESSION_HANDLE    hSession,    /* the session's handle */
119208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   const CK_MECHANISM*  pMechanism,  /* the key generation mechanism */
119308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   const CK_ATTRIBUTE*  pTemplate,   /* template for the new key */
119408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_ULONG             ulCount,     /* number of attributes in template */
119508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_OBJECT_HANDLE*    phKey)       /* receives handle of new key */
119608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic{
119708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   TEEC_Result    teeErr;
119808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t       nErrorOrigin;
119908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   TEEC_Operation sOperation;
120008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_RV       nErrorCode = CKR_OK;
120108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t    nCommandIDAndSession = SERVICE_SYSTEM_PKCS11_C_GENERATEKEY_COMMAND_ID;
120208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint8_t*    pBuffer     = NULL;
120308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t    nBufferSize = 0;
120408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   PPKCS11_PRIMARY_SESSION_CONTEXT pSession;
120508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
120608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if ((pMechanism == NULL) || (phKey == NULL) || (pTemplate == NULL))
120708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
120808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      return CKR_ARGUMENTS_BAD;
120908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
121008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
121108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   nErrorCode = static_checkPreConditionsAndUpdateHandles(&hSession, &nCommandIDAndSession, &pSession);
121208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (nErrorCode != CKR_OK)
121308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
121408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      return nErrorCode;
121508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
121608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
121708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   nErrorCode = static_encodeTemplate(&pBuffer, &nBufferSize, 2, (CK_ATTRIBUTE*)pTemplate, ulCount);
121808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (nErrorCode != CKR_OK)
121908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
122008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      return nErrorCode;
122108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
122208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
122308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   memset(&sOperation, 0, sizeof(TEEC_Operation));
122408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   sOperation.params[0].value.a = (uint32_t)pMechanism->mechanism;
122508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   sOperation.params[0].value.b = 0;
122608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   sOperation.params[1].tmpref.buffer = pMechanism->pParameter;
122708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   sOperation.params[1].tmpref.size = (uint32_t)pMechanism->ulParameterLen;
122808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   sOperation.params[2].tmpref.buffer = pBuffer;
122908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   sOperation.params[2].tmpref.size = nBufferSize;
123008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   sOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INOUT, TEEC_MEMREF_TEMP_INPUT, TEEC_MEMREF_TEMP_INPUT, TEEC_NONE);
123108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   teeErr = TEEC_InvokeCommand(   &pSession->sSession,
123208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                  nCommandIDAndSession,        /* commandID */
123308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                  &sOperation,                 /* IN OUT operation */
123408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                  &nErrorOrigin                /* OUT returnOrigin, optional */
123508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                 );
123608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   free(pBuffer);
123708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
123808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (teeErr != TEEC_SUCCESS)
123908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
124008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      nErrorCode = (nErrorOrigin == TEEC_ORIGIN_TRUSTED_APP ?
124108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                     teeErr :
124208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                     ckInternalTeeErrorToCKError(teeErr));
124308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      return nErrorCode;
124408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
124508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
124608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   *phKey = sOperation.params[0].value.a;
124708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
124808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   return CKR_OK;
124908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic}
125008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
125108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted LogicCK_RV PKCS11_EXPORT C_GenerateKeyPair(
125208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_SESSION_HANDLE    hSession,                    /* the session's handle */
125308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   const CK_MECHANISM*  pMechanism,                  /* the key gen. mech. */
125408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   const CK_ATTRIBUTE*  pPublicKeyTemplate,          /* pub. attr. template */
125508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_ULONG             ulPublicKeyAttributeCount,   /* # of pub. attrs. */
125608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   const CK_ATTRIBUTE*  pPrivateKeyTemplate,         /* priv. attr. template */
125708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_ULONG             ulPrivateKeyAttributeCount,  /* # of priv. attrs. */
125808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_OBJECT_HANDLE*    phPublicKey,                 /* gets pub. key handle */
125908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_OBJECT_HANDLE*    phPrivateKey)                /* gets priv. key handle */
126008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic{
126108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   TEEC_Result    teeErr;
126208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t       nErrorOrigin;
126308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   TEEC_Operation sOperation;
126408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_RV       nErrorCode = CKR_OK;
126508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t    nCommandIDAndSession = SERVICE_SYSTEM_PKCS11_C_GENERATEKEYPAIR_COMMAND_ID;
126608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint8_t*    pBuffer     = NULL;
126708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t    nBufferSize = 0;
126808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   PPKCS11_PRIMARY_SESSION_CONTEXT pSession;
126908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
127008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (  (pMechanism == NULL) ||
127108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         (pPublicKeyTemplate == NULL) || (pPrivateKeyTemplate == NULL) ||
127208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         (phPublicKey== NULL) || (phPrivateKey== NULL))
127308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
127408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      return CKR_ARGUMENTS_BAD;
127508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
127608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
127708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   nErrorCode = static_checkPreConditionsAndUpdateHandles(&hSession, &nCommandIDAndSession, &pSession);
127808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (nErrorCode != CKR_OK)
127908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
128008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      return nErrorCode;
128108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
128208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
128308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   nErrorCode = static_encodeTwoTemplates(&pBuffer, &nBufferSize, 2, (CK_ATTRIBUTE*)pPublicKeyTemplate, ulPublicKeyAttributeCount, (CK_ATTRIBUTE*)pPrivateKeyTemplate, ulPrivateKeyAttributeCount);
128408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (nErrorCode != CKR_OK)
128508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
128608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      return nErrorCode;
128708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
128808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
128908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   memset(&sOperation, 0, sizeof(TEEC_Operation));
129008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   sOperation.params[0].value.a = (uint32_t)pMechanism->mechanism;
129108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   sOperation.params[0].value.b = 0;
129208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   sOperation.params[1].tmpref.buffer = (uint8_t*)pMechanism->pParameter;
129308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   sOperation.params[1].tmpref.size = (uint32_t)pMechanism->ulParameterLen;
129408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   sOperation.params[2].tmpref.buffer = pBuffer;
129508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   sOperation.params[2].tmpref.size = nBufferSize;
129608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   sOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INOUT, TEEC_MEMREF_TEMP_INPUT, TEEC_MEMREF_TEMP_INPUT, TEEC_NONE);
129708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   teeErr = TEEC_InvokeCommand(   &pSession->sSession,
129808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                  nCommandIDAndSession,        /* commandID */
129908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                  &sOperation,                 /* IN OUT operation */
130008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                  &nErrorOrigin                /* OUT returnOrigin, optional */
130108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                 );
130208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   free(pBuffer);
130308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
130408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (teeErr != TEEC_SUCCESS)
130508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
130608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      nErrorCode = (nErrorOrigin == TEEC_ORIGIN_TRUSTED_APP ?
130708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                     teeErr :
130808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                     ckInternalTeeErrorToCKError(teeErr));
130908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      return nErrorCode;
131008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
131108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
131208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   *phPublicKey  = sOperation.params[0].value.a;
131308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   *phPrivateKey = sOperation.params[0].value.b;
131408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
131508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   return CKR_OK;
131608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic}
131708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
131808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted LogicCK_RV PKCS11_EXPORT C_DeriveKey(
131908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_SESSION_HANDLE    hSession,          /* session's handle */
132008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   const CK_MECHANISM*  pMechanism,        /* key deriv. mech. */
132108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_OBJECT_HANDLE     hBaseKey,          /* base key */
132208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   const CK_ATTRIBUTE*  pTemplate,         /* new key template */
132308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_ULONG             ulAttributeCount,  /* template length */
132408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_OBJECT_HANDLE*    phKey)             /* gets new handle */
132508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic{
132608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   TEEC_Result    teeErr;
132708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t       nErrorOrigin;
132808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   TEEC_Operation sOperation;
132908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_RV       nErrorCode = CKR_OK;
133008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t    nCommandIDAndSession = SERVICE_SYSTEM_PKCS11_C_DERIVEKEY_COMMAND_ID;
133108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint8_t*    pBuffer     = NULL;
133208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t    nBufferSize = 0;
133308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   PPKCS11_PRIMARY_SESSION_CONTEXT pSession;
133408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
133508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if ((pMechanism == NULL) || (pTemplate == NULL) || (phKey == NULL))
133608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
133708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      return CKR_ARGUMENTS_BAD;
133808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
133908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
134008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   nErrorCode = static_checkPreConditionsAndUpdateHandles(&hSession, &nCommandIDAndSession, &pSession);
134108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (nErrorCode != CKR_OK)
134208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
134308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      return nErrorCode;
134408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
134508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
134608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   nErrorCode = static_encodeTemplate(&pBuffer, &nBufferSize, 2, (CK_ATTRIBUTE*)pTemplate, ulAttributeCount);
134708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (nErrorCode != CKR_OK)
134808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
134908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      return nErrorCode;
135008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
135108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
135208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   memset(&sOperation, 0, sizeof(TEEC_Operation));
135308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   sOperation.params[0].value.a = (uint32_t)pMechanism->mechanism;
135408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   sOperation.params[0].value.b = (uint32_t)hBaseKey;
135508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   sOperation.params[1].tmpref.buffer = (uint8_t*)pMechanism->pParameter;
135608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   sOperation.params[1].tmpref.size = (uint32_t)pMechanism->ulParameterLen;
135708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   sOperation.params[2].tmpref.buffer = pBuffer;
135808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   sOperation.params[2].tmpref.size = nBufferSize;
135908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   sOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INOUT, TEEC_MEMREF_TEMP_INPUT, TEEC_MEMREF_TEMP_INPUT, TEEC_NONE);
136008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   teeErr = TEEC_InvokeCommand(   &pSession->sSession,
136108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                  nCommandIDAndSession,        /* commandID */
136208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                  &sOperation,                 /* IN OUT operation */
136308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                  &nErrorOrigin                /* OUT returnOrigin, optional */
136408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                 );
136508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   free(pBuffer);
136608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
136708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (teeErr != TEEC_SUCCESS)
136808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
136908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      nErrorCode = (nErrorOrigin == TEEC_ORIGIN_TRUSTED_APP ?
137008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                     teeErr :
137108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                     ckInternalTeeErrorToCKError(teeErr));
137208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      return nErrorCode;
137308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
137408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
137508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   *phKey = sOperation.params[0].value.a;
137608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
137708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   return CKR_OK;
137808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic}
137908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
138008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted LogicCK_RV PKCS11_EXPORT C_SeedRandom(
138108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_SESSION_HANDLE hSession,  /* the session's handle */
138208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   const CK_BYTE*    pSeed,     /* the seed material */
138308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_ULONG          ulSeedLen) /* count of bytes of seed material */
138408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic{
138508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   TEEC_Result    teeErr;
138608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t       nErrorOrigin;
138708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   TEEC_Operation sOperation;
138808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_RV       nErrorCode = CKR_OK;
138908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t    nCommandIDAndSession = SERVICE_SYSTEM_PKCS11_C_SEEDRANDOM_COMMAND_ID;
139008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   PPKCS11_PRIMARY_SESSION_CONTEXT pSession;
139108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
139208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   nErrorCode = static_checkPreConditionsAndUpdateHandles(&hSession, &nCommandIDAndSession, &pSession);
139308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (nErrorCode != CKR_OK)
139408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
139508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      return nErrorCode;
139608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
139708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   memset(&sOperation, 0, sizeof(TEEC_Operation));
139808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   sOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, TEEC_NONE, TEEC_NONE, TEEC_NONE);
139908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   sOperation.params[0].tmpref.buffer = (uint8_t*)pSeed;
140008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   sOperation.params[0].tmpref.size   = (uint32_t)ulSeedLen;
140108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   teeErr = TEEC_InvokeCommand(   &pSession->sSession,
140208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                  nCommandIDAndSession,        /* commandID */
140308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                  &sOperation,                 /* IN OUT operation */
140408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                  &nErrorOrigin                /* OUT returnOrigin, optional */
140508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                 );
140608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
140708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   nErrorCode = (nErrorOrigin == TEEC_ORIGIN_TRUSTED_APP ?
140808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                  teeErr :
140908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                  ckInternalTeeErrorToCKError(teeErr));
141008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   return nErrorCode;
141108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic}
141208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
141308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted LogicCK_RV PKCS11_EXPORT C_GenerateRandom(
141408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_SESSION_HANDLE hSession,    /* the session's handle */
141508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_BYTE*          pRandomData,  /* receives the random data */
141608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_ULONG          ulRandomLen) /* number of bytes to be generated */
141708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic{
141808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   TEEC_Result    teeErr;
141908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t       nErrorOrigin;
142008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   TEEC_Operation sOperation;
142108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_RV       nErrorCode = CKR_OK;
142208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t    nCommandIDAndSession = SERVICE_SYSTEM_PKCS11_C_GENERATERANDOM_COMMAND_ID;
142308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   PPKCS11_PRIMARY_SESSION_CONTEXT pSession;
142408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
142508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   nErrorCode = static_checkPreConditionsAndUpdateHandles(&hSession, &nCommandIDAndSession, &pSession);
142608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (nErrorCode != CKR_OK)
142708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
142808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      return nErrorCode;
142908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
143008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
143108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   do
143208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
143308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      CK_ULONG nArrayLength;
143408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      nArrayLength = 1024;
143508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      if (ulRandomLen < nArrayLength)
143608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      {
143708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         nArrayLength = ulRandomLen;
143808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      }
143908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      memset(&sOperation, 0, sizeof(TEEC_Operation));
144008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      sOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_OUTPUT, TEEC_NONE, TEEC_NONE, TEEC_NONE);
144108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      sOperation.params[0].tmpref.buffer = (uint8_t*)pRandomData;
144208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      sOperation.params[0].tmpref.size   = (uint32_t)nArrayLength;
144308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      teeErr = TEEC_InvokeCommand(   &pSession->sSession,
144408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                     nCommandIDAndSession,        /* commandID */
144508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                     &sOperation,                 /* IN OUT operation */
144608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                     &nErrorOrigin                /* OUT returnOrigin, optional */
144708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                    );
144808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      if (teeErr != TEEC_SUCCESS)
144908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      {
145008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         nErrorCode = (nErrorOrigin == TEEC_ORIGIN_TRUSTED_APP ?
145108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                        teeErr :
145208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                        ckInternalTeeErrorToCKError(teeErr));
145308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         return nErrorCode;
145408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      }
145508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
145608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      ulRandomLen -= nArrayLength;
145708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      pRandomData += nArrayLength;
145808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      if (ulRandomLen == 0)
145908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      {
146008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         break;
146108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      }
146208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
146308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   while(1);
146408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
146508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   return CKR_OK;
146608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic}
146708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
146808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted LogicCK_RV PKCS11_EXPORT C_VerifyInit(
146908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_SESSION_HANDLE   hSession,    /* the session's handle */
147008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   const CK_MECHANISM* pMechanism,  /* the verification mechanism */
147108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_OBJECT_HANDLE    hKey)        /* handle of the verification key */
147208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic{
147308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   return static_C_CallInit(
147408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      SERVICE_SYSTEM_PKCS11_C_VERIFYINIT_COMMAND_ID,
147508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      hSession,
147608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      pMechanism,
147708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      hKey);
147808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic}
147908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
148008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted LogicCK_RV PKCS11_EXPORT C_Verify(
148108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_SESSION_HANDLE hSession,       /* the session's handle */
148208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   const CK_BYTE*    pData,          /* plaintext data (digest) to compare */
148308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_ULONG          ulDataLen,      /* length of data (digest) in bytes */
148408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_BYTE*          pSignature,     /* the signature to be verified */
148508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_ULONG          ulSignatureLen) /* count of bytes of signature */
148608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic{
148708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   TEEC_Result    teeErr;
148808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t       nErrorOrigin;
148908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   TEEC_Operation sOperation;
149008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_RV       nErrorCode = CKR_OK;
149108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t    nCommandIDAndSession = SERVICE_SYSTEM_PKCS11_C_VERIFY_COMMAND_ID;
149208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   PPKCS11_PRIMARY_SESSION_CONTEXT pSession;
149308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
149408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   nErrorCode = static_checkPreConditionsAndUpdateHandles(&hSession, &nCommandIDAndSession, &pSession);
149508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (nErrorCode != CKR_OK)
149608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
149708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      return nErrorCode;
149808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
149908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
150008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   memset(&sOperation, 0, sizeof(TEEC_Operation));
150108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   sOperation.params[0].tmpref.buffer = (uint8_t*)pData;
150208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   sOperation.params[0].tmpref.size   = (uint32_t)ulDataLen;
150308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   sOperation.params[1].tmpref.buffer = (uint8_t*)pSignature;
150408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   sOperation.params[1].tmpref.size   = (uint32_t)ulSignatureLen;
150508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   sOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, TEEC_MEMREF_TEMP_INPUT, TEEC_NONE, TEEC_NONE);
150608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   teeErr = TEEC_InvokeCommand(   &pSession->sSession,
150708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                  nCommandIDAndSession,        /* commandID */
150808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                  &sOperation,                 /* IN OUT operation */
150908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                  &nErrorOrigin                /* OUT returnOrigin, optional */
151008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                 );
151108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   nErrorCode = (nErrorOrigin == TEEC_ORIGIN_TRUSTED_APP ?
151208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                  teeErr :
151308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                  ckInternalTeeErrorToCKError(teeErr));
151408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   return nErrorCode;
151508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic}
151608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
151708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted LogicCK_RV PKCS11_EXPORT C_VerifyUpdate(
151808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_SESSION_HANDLE hSession,  /* the session's handle */
151908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   const CK_BYTE*    pPart,     /* plaintext data (digest) to compare */
152008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_ULONG          ulPartLen) /* length of data (digest) in bytes */
152108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic{
152208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   return static_C_Call_CallForUpdate(
152308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      SERVICE_SYSTEM_PKCS11_C_VERIFYUPDATE_COMMAND_ID,
152408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      hSession,
152508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      pPart,
152608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      ulPartLen,
152708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      NULL,
152808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      NULL,
152908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      TRUE,
153008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      FALSE);
153108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic}
153208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
153308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted LogicCK_RV PKCS11_EXPORT C_VerifyFinal(
153408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_SESSION_HANDLE hSession,       /* the session's handle */
153508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   const CK_BYTE*    pSignature,     /* the signature to be verified */
153608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_ULONG          ulSignatureLen) /* count of bytes of signature */
153708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic{
153808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   return static_C_Call_CallForUpdate(
153908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      SERVICE_SYSTEM_PKCS11_C_VERIFYFINAL_COMMAND_ID,
154008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      hSession,
154108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      pSignature,
154208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      ulSignatureLen,
154308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      NULL,
154408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      NULL,
154508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      TRUE,
154608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      FALSE);
154708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic}
154808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
154908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted LogicCK_RV PKCS11_EXPORT C_CloseObjectHandle(
155008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_SESSION_HANDLE hSession,  /* the session's handle */
155108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_OBJECT_HANDLE  hObject)   /* the object's handle */
155208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic{
155308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   TEEC_Result    teeErr;
155408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t       nErrorOrigin;
155508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   TEEC_Operation sOperation;
155608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_RV       nErrorCode = CKR_OK;
155708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t    nCommandIDAndSession = SERVICE_SYSTEM_PKCS11_C_CLOSEOBJECTHANDLE_COMMAND_ID;
155808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   PPKCS11_PRIMARY_SESSION_CONTEXT pSession;
155908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
156008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   nErrorCode = static_checkPreConditionsAndUpdateHandles(&hSession, &nCommandIDAndSession, &pSession);
156108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (nErrorCode != CKR_OK)
156208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
156308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      return nErrorCode;
156408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
156508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   memset(&sOperation, 0, sizeof(TEEC_Operation));
156608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   sOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE, TEEC_NONE);
156708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   sOperation.params[0].value.a = (uint32_t)hObject;
156808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   sOperation.params[0].value.b = 0;
156908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   teeErr = TEEC_InvokeCommand(   &pSession->sSession,
157008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                  nCommandIDAndSession,        /* commandID */
157108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                  &sOperation,                 /* IN OUT operation */
157208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                  &nErrorOrigin                /* OUT returnOrigin, optional */
157308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                 );
157408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   nErrorCode = (nErrorOrigin == TEEC_ORIGIN_TRUSTED_APP ?
157508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                  teeErr :
157608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                  ckInternalTeeErrorToCKError(teeErr));
157708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   return nErrorCode;
157808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic}
157908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
158008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted LogicCK_RV PKCS11_EXPORT C_CopyObject(
158108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         CK_SESSION_HANDLE    hSession,    /* the session's handle */
158208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         CK_OBJECT_HANDLE     hObject,     /* the source object's handle */
158308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   const CK_ATTRIBUTE*        pTemplate,   /* the template of the copied object */
158408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         CK_ULONG             ulCount,     /* the number of attributes of the template*/
158508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic         CK_OBJECT_HANDLE*    phNewObject) /* the copied object's handle */
158608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic{
158708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   TEEC_Result    teeErr;
158808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t       nErrorOrigin;
158908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   TEEC_Operation sOperation;
159008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   CK_RV       nErrorCode = CKR_OK;
159108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t    nCommandIDAndSession = SERVICE_SYSTEM_PKCS11_C_COPYOBJECT_COMMAND_ID;
159208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint8_t*    pBuffer     = NULL;
159308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   uint32_t    nBufferSize = 0;
159408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   PPKCS11_PRIMARY_SESSION_CONTEXT pSession;
159508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
159608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if ((pTemplate == NULL) || (phNewObject == NULL))
159708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
159808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      return CKR_ARGUMENTS_BAD;
159908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
160008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
160108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   nErrorCode = static_checkPreConditionsAndUpdateHandles(&hSession, &nCommandIDAndSession, &pSession);
160208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (nErrorCode != CKR_OK)
160308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
160408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      return nErrorCode;
160508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
160608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
160708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   nErrorCode = static_encodeTemplate(&pBuffer, &nBufferSize, 1, (CK_ATTRIBUTE*)pTemplate, ulCount);
160808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (nErrorCode != CKR_OK)
160908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
161008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      return nErrorCode;
161108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
161208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
161308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   memset(&sOperation, 0, sizeof(TEEC_Operation));
161408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   sOperation.params[0].value.a = (uint32_t)hObject;
161508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   sOperation.params[0].value.b = 0;
161608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   sOperation.params[1].tmpref.buffer = pBuffer;
161708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   sOperation.params[1].tmpref.size   = nBufferSize;
161808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   sOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INOUT, TEEC_MEMREF_TEMP_INPUT, TEEC_NONE, TEEC_NONE);
161908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   teeErr = TEEC_InvokeCommand(   &pSession->sSession,
162008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                  nCommandIDAndSession,        /* commandID */
162108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                  &sOperation,                 /* IN OUT operation */
162208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                  &nErrorOrigin                /* OUT returnOrigin, optional */
162308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                                 );
162408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   free(pBuffer);
162508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
162608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   if (teeErr != TEEC_SUCCESS)
162708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   {
162808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      nErrorCode = (nErrorOrigin == TEEC_ORIGIN_TRUSTED_APP ?
162908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                     teeErr :
163008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic                     ckInternalTeeErrorToCKError(teeErr));
163108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic      return nErrorCode;
163208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   }
163308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
163408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   *phNewObject = sOperation.params[0].value.a;
163508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic
163608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic   return CKR_OK;
163708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic}
1638