summaryrefslogtreecommitdiff
path: root/security/tee_client_api/tee_client_api_linux_driver.c
blob: 08a82101825f6d11eb5940308415396ba51e3101 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
/**
 * Copyright(c) 2011 Trusted Logic.   All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *  * Neither the name Trusted Logic nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "tee_client_api.h"
#include "schannel6_protocol.h"
#include "s_error.h"
#include "s_version.h"

#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdarg.h>
#include <assert.h>

#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <linux/limits.h>
#include <time.h>
#include <sys/time.h>

/*
 * SCX_VERSION_INFORMATION_BUFFER structure description
 * Description of the sVersionBuffer handed over from user space to kernel space
 * This field is filled after an IOCTL call and handed back to user space
 */
typedef struct
{
   uint8_t sDriverDescription[65];
   uint8_t sSecureWorldDescription[65];
} SCX_VERSION_INFORMATION_BUFFER;


/* The IOCTLs to the driver */
#define IOCTL_SCX_GET_VERSION \
         _IO('z', 0)

#define IOCTL_SCX_EXCHANGE \
         _IOWR('z', 1, SCHANNEL6_COMMAND)

#define IOCTL_SCX_GET_DESCRIPTION \
         _IOR('z', 2, SCX_VERSION_INFORMATION_BUFFER)


/* Expected driver interface version. */
#define SM_DRIVER_VERSION 0x04000000

#define SCX_DEFAULT_DEVICE_NAME "tf_driver"

#define SCX_PARAM_TYPE_GET(nParamTypes, i) (((nParamTypes) >> (4*i)) & 0xF)

#define VAR_NOT_USED(variable)  do{(void)(variable);}while(0);

#define SIZE_4KB  0x1000
#define SIZE_1MB  0x100000

/* ------------------------------------------------------------------------ */
/*    UTILS                                                                 */
/* ------------------------------------------------------------------------ */
#ifdef NDEBUG
/* Compile-out the traces */
#define TRACE_ERROR(...)
#define TRACE_WARNING(...)
#define TRACE_INFO(...)
#else
static void TRACE_ERROR(const char* format, ...)
{
   va_list ap;
   va_start(ap, format);
   fprintf(stderr, "TRACE: ERROR: ");
   vfprintf(stderr, format, ap);
   fprintf(stderr, "\n");
   va_end(ap);
}

static void TRACE_WARNING(const char* format, ...)
{
   va_list ap;
   va_start(ap, format);
   fprintf(stderr, "TRACE: WARNING: ");
   vfprintf(stderr, format, ap);
   fprintf(stderr, "\n");
   va_end(ap);
}

static void TRACE_INFO(const char* format, ...)
{
   va_list ap;
   va_start(ap, format);
   fprintf(stderr, "TRACE: ");
   vfprintf(stderr, format, ap);
   fprintf(stderr, "\n");
   va_end(ap);
}
#endif /* NDEBUG */


/*
 * ====================================================
 *                 Internal functions
 * =====================================================
*/

static void scxYield(void)
{
   sleep(0);
}

/* ------------------------------------------------------------------------ */


/*
 * Exchange a message with the Secure World
 * by calling the ioctl command of the linux driver
 *
 * @param pContext
 * @param pCommand a SChannel command message that must have been filled except for the operation parameters
 * @param pAnswer  a placeholder for the SChannel answer
 * @param pOperation a TEEC_Operation structure that contains the operation parameters (and types)
 *                   and is updated with the SChannel answer data as appropriate. This parameter is
 *                   used only for the open and invoke operations
 */
static TEEC_Result scxExchangeMessage(
   IN  TEEC_Context*     pContext,
   IN  SCHANNEL6_COMMAND* pCommand,
   OUT SCHANNEL6_ANSWER*  pAnswer,
   IN TEEC_Operation* pOperation)
{
   TEEC_Result nResult = TEEC_SUCCESS;

   TRACE_INFO("scxExchangeMessage[0x%X]\n",pContext);

   if (pOperation != NULL)
   {
      /* Determine message parameters from operation parameters */
      uint32_t i;
      SCHANNEL6_COMMAND_PARAM* pSCXParams;

      /* Note that nParamType is at the same position in an open and an invoke message */
      pCommand->sHeader.nMessageInfo = pOperation->paramTypes;

      if (pCommand->sHeader.nMessageType == SCX_OPEN_CLIENT_SESSION)
      {
         pSCXParams = pCommand->sOpenClientSession.sParams;
      }
      else
      {
         /* An invoke-command */
         pSCXParams = pCommand->sInvokeClientCommand.sParams;
      }

      for (i = 0; i < 4; i++)
      {
         uint32_t nTEECParamType = SCX_PARAM_TYPE_GET(pOperation->paramTypes, i);
         TEEC_Parameter*  pTEECParam = &pOperation->params[i];
         SCHANNEL6_COMMAND_PARAM* pSCXParam = &pSCXParams[i];

         if (nTEECParamType & SCX_PARAM_TYPE_MEMREF_FLAG)
         {
            if (nTEECParamType & SCX_PARAM_TYPE_REGISTERED_MEMREF_FLAG)
            {
               /* A registered memref */
               pSCXParam->sMemref.hBlock  = pTEECParam->memref.parent->imp._hBlock;
               if (nTEECParamType == TEEC_MEMREF_WHOLE)
               {
                  /* A memref on the whole shared memory */
                  /* Set the direction from the shared memory flags */
                  pCommand->sInvokeClientCommand.nParamTypes |=
                     (pTEECParam->memref.parent->flags & (SCX_PARAM_TYPE_INPUT_FLAG | SCX_PARAM_TYPE_OUTPUT_FLAG))
                     << (4*i);
                  pSCXParam->sMemref.nSize   = pTEECParam->memref.parent->size;
                  pSCXParam->sMemref.nOffset = 0;
               }
               else
               {
                  /* A partial memref */
                  pSCXParam->sMemref.nSize   = pTEECParam->memref.size;
                  pSCXParam->sMemref.nOffset = pTEECParam->memref.offset;
               }
            }
            else
            {
               /* A temporary memref */
               /* Set nOffset to the address in the client. This allows the server
                 to allocate a block with the same alignment and also to
                 detect a NULL tmpref.
               */
               pSCXParam->sTempMemref.nOffset = (uint32_t)pTEECParam->tmpref.buffer;
               pSCXParam->sTempMemref.nDescriptor = (uint32_t)pTEECParam->tmpref.buffer;
               pSCXParam->sTempMemref.nSize = pTEECParam->tmpref.size;
            }
         }
         else if (nTEECParamType & SCX_PARAM_TYPE_INPUT_FLAG)
         {
            /* An input value */
            pSCXParam->sValue.a = pTEECParam->value.a;
            pSCXParam->sValue.b = pTEECParam->value.b;
         }
      }
   }

   pCommand->sHeader.nOperationID = (uint32_t)pAnswer;

   nResult = ioctl((S_HANDLE)pContext->imp._hConnection, IOCTL_SCX_EXCHANGE, pCommand);
   if (nResult != S_SUCCESS)
   {
      TRACE_INFO("scxExchangeMessage[0x%X]: Ioctl returned error: 0x%x (0x%x - %d)\n",pContext,nResult,errno,errno);
      switch(errno)
      {
         case ENOMEM:
            nResult=TEEC_ERROR_OUT_OF_MEMORY;
            break;
         case EACCES:
            nResult=TEEC_ERROR_ACCESS_DENIED;
            break;
         default:
            nResult=TEEC_ERROR_COMMUNICATION;
            break;
      }
   }

   if (pOperation != NULL)
   {
      /* Update the operation parameters from the answer message */
      uint32_t   i;
      SCHANNEL6_ANSWER_PARAM *  pSCXAnswers;
      if (pAnswer->sHeader.nMessageType == SCX_OPEN_CLIENT_SESSION)
      {
         /* Open session */
         pSCXAnswers = pAnswer->sOpenClientSession.sAnswers;
      }
      else
      {
         /* Invoke case */
         pSCXAnswers = pAnswer->sInvokeClientCommand.sAnswers;
      }

      for (i = 0; i < 4; i++)
      {
         uint32_t   nSCXParamType;
         nSCXParamType = SCX_GET_PARAM_TYPE(pCommand->sHeader.nMessageInfo, i);
         if (nSCXParamType & SCX_PARAM_TYPE_OUTPUT_FLAG)
         {
            if (nSCXParamType & SCX_PARAM_TYPE_MEMREF_FLAG)
            {
               /* Trick: the size field is at the same position in a memref or a tmpref */
               pOperation->params[i].memref.size = pSCXAnswers[i].sSize.nSize;
            }
            else
            {
               /* An output value */
               pOperation->params[i].value.a = pSCXAnswers[i].sValue.a;
               pOperation->params[i].value.b = pSCXAnswers[i].sValue.b;
            }
         }
      }
   }

   return nResult;
}

/* ------------------------------------------------------------------------ */

static void* scxAllocateSharedMemory(
   IN uint32_t nLength)
{
   if (nLength == 0)
   {
      /* This is valid, although we don't want to call mmap.
         Just return a dummy non-NULL pointer */
      return (void*)0x10;
   }
   else
   {
      return mmap(
         0,nLength,
         PROT_READ | PROT_WRITE,
         MAP_SHARED | MAP_ANONYMOUS,
         0,0);
   }
}

/* ------------------------------------------------------------------------ */

static void scxReleaseSharedMemory(IN void* pBuffer,
                                   IN uint32_t nLength)
{
   if (nLength == 0)
   {
      return;
   }
   if (munmap(pBuffer, nLength)!= 0)
   {
       TRACE_WARNING("scxReleaseSharedMemory returned 0x%x \n",errno);
   }
}
/* ------------------------------------------------------------------------ */

uint64_t scxGetCurrentTime(void)
{
   uint64_t currentTime = 0;
   struct timeval now;

   gettimeofday(&now,NULL);
   currentTime = now.tv_sec;
   currentTime = (currentTime * 1000) + (now.tv_usec / 1000);

   return currentTime;
}
/* ------------------------------------------------------------------------ */

/*
 * ====================================================
 *                TEE Client API
 * =====================================================
*/

/**
 * Get a time-limit equal to now + relative timeout expressed in milliseconds.
 **/
void TEEC_GetTimeLimit(
    TEEC_Context*    sContext,
    uint32_t         nTimeout,
    TEEC_TimeLimit*  sTimeLimit)
{
   uint64_t nTimeLimit = 0;
   VAR_NOT_USED(sContext);

   TRACE_INFO("TEEC_GetTimeLimit(0x%X, %u ms)", sContext, nTimeout);

   if (nTimeout == 0xFFFFFFFF )
   {
      /* Infinite timeout */
      nTimeLimit = SCTIME_INFINITE;
   }
   else
   {
       nTimeLimit = scxGetCurrentTime() + nTimeout;
   }
   TRACE_INFO("GetTimeLimit %ld\n",nTimeLimit);
   memcpy(sTimeLimit, &nTimeLimit, sizeof(TEEC_TimeLimit));
}

//-----------------------------------------------------------------------------------------------------
TEEC_Result TEEC_InitializeContext(
    const char*   pDeviceName,
    TEEC_Context* pContext)
{

  TEEC_Result nError = TEEC_SUCCESS;
   S_HANDLE hDriver   = S_HANDLE_NULL;
   char sFullDeviceName[PATH_MAX];
   uint32_t nVersion;

   if(pDeviceName == NULL)
   {
      pDeviceName = SCX_DEFAULT_DEVICE_NAME;
   }
   strcpy(sFullDeviceName, "/dev/");
   strcat(sFullDeviceName, pDeviceName);

   hDriver = open(sFullDeviceName, O_RDWR, 0);

   if (hDriver == (uint32_t)-1)
   {
      TRACE_ERROR("scxOpen: open() failed 0x%x\n", errno);
      switch(errno)
      {
         case ENOMEM:
            nError = TEEC_ERROR_OUT_OF_MEMORY;
            goto error;
         case EINTR:
             break;
         default:
            nError = TEEC_ERROR_COMMUNICATION;
            goto error;
      }
   }
   fcntl(hDriver, F_SETFD, FD_CLOEXEC);
   nVersion = ioctl(hDriver, IOCTL_SCX_GET_VERSION);
   if (nVersion != SM_DRIVER_VERSION)
   {
      TRACE_ERROR("scxOpen: Not expected driver version: 0x%x instead of 0x%x\n", nVersion,SM_DRIVER_VERSION);
      switch(errno)
      {
         case ENOMEM:
            nError=TEEC_ERROR_OUT_OF_MEMORY;
            break;
         default:
            nError=TEEC_ERROR_COMMUNICATION;
            break;
      }
      close(hDriver);
   }
error:
   if(nError == TEEC_SUCCESS)
   {
       pContext->imp._hConnection = hDriver;
   }
   else
   {
      TRACE_ERROR("scxOpen failed 0x%x\n", nError);
      pContext->imp._hConnection = 0;
   }

   return nError;
}

//-----------------------------------------------------------------------------------------------------
void TEEC_FinalizeContext(TEEC_Context* pContext)
{
   TRACE_INFO("TEEC_FinalizeContext[0x%X]", pContext);

   if (pContext == NULL) return;

   close(pContext->imp._hConnection);
   pContext->imp._hConnection = 0;
}

//-----------------------------------------------------------------------------------------------------
TEEC_Result TEEC_OpenSession (
    TEEC_Context*    context,
    TEEC_Session*    session,      /* OUT */
    const TEEC_UUID* destination,  /* The trusted application UUID we want to open the session with */
    uint32_t         connectionMethod, /* LoginType*/
    void*            connectionData,  /* LoginData */
    TEEC_Operation*  operation,    /* payload. If operation is NULL then no data buffers are exchanged with the Trusted Application, and the operation cannot be cancelled by the Client Application */
    uint32_t*        errorOrigin)
{
  return TEEC_OpenSessionEx(context,
                            session,
                            NULL,
                            destination,
                            connectionMethod,
                            connectionData,
                            operation,
                            errorOrigin);
}

//-----------------------------------------------------------------------------------------------------
void TEEC_CloseSession (TEEC_Session* session)
{
   TEEC_Context*     context;
   SCHANNEL6_ANSWER  sAnswer;
   SCHANNEL6_COMMAND sCommand;
   if (session == NULL) return;
   context = session->imp._pContext;
   memset(&sCommand,0,sizeof(sCommand));
   sCommand.sHeader.nMessageType = SCX_CLOSE_CLIENT_SESSION;
   sCommand.sHeader.nMessageSize = (sizeof(SCHANNEL6_CLOSE_CLIENT_SESSION_COMMAND)
                                         - sizeof(SCHANNEL6_COMMAND_HEADER))/sizeof(uint32_t);
   sCommand.sCloseClientSession.hClientSession = session->imp._hClientSession;
   scxExchangeMessage(context, &sCommand, &sAnswer, NULL);
   /* we ignore the error code of scxExchangeMessage */
   session->imp._hClientSession = S_HANDLE_NULL;
   session->imp._pContext = NULL;
}

//-----------------------------------------------------------------------------------------------------
TEEC_Result TEEC_InvokeCommand(
    TEEC_Session*     session,
    uint32_t          commandID,
    TEEC_Operation*   operation,
    uint32_t*         errorOrigin)
{
    return TEEC_InvokeCommandEx(session,
                            NULL,
                            commandID,
                            operation,
                            errorOrigin);
}


//-----------------------------------------------------------------------------------------------------
/* Used to implement both register and allocate */
static TEEC_Result TEEC_RegisterSharedMemory0(
    TEEC_Context*      context,
    TEEC_SharedMemory* sharedMem)
{
   TEEC_Result nResult;
   SCHANNEL6_COMMAND sCommand;
   SCHANNEL6_ANSWER  sAnswer;

   TRACE_INFO("TEEC_RegisterSharedMemory0 (%p, %p)",context, sharedMem);
   memset(&sCommand, 0, sizeof(sCommand));

   sCommand.sRegisterSharedMemory.nMessageSize = (sizeof(SCHANNEL6_REGISTER_SHARED_MEMORY_COMMAND) - sizeof(SCHANNEL6_COMMAND_HEADER))/4;
   sCommand.sRegisterSharedMemory.nMessageType             = SCX_REGISTER_SHARED_MEMORY;
   sCommand.sRegisterSharedMemory.nMemoryFlags             = sharedMem->flags;
   sCommand.sRegisterSharedMemory.nSharedMemSize           = sharedMem->size;
   sCommand.sRegisterSharedMemory.nSharedMemStartOffset    = 0;
   sCommand.sRegisterSharedMemory.nSharedMemDescriptors[0] = (uint32_t)sharedMem->buffer;
   nResult = scxExchangeMessage(context,
                &sCommand,
                &sAnswer,
                NULL);
   if (nResult == TEEC_SUCCESS)
   {
       nResult = sAnswer.sRegisterSharedMemory.nErrorCode;
   }
   if (nResult == TEEC_SUCCESS)
   {
      sharedMem->imp._pContext = context;
      sharedMem->imp._hBlock = sAnswer.sRegisterSharedMemory.hBlock;
   }
   return nResult;
}


//-----------------------------------------------------------------------------------------------------
TEEC_Result TEEC_RegisterSharedMemory(
    TEEC_Context*      context,
    TEEC_SharedMemory* sharedMem)
{
   TRACE_INFO("TEEC_RegisterSharedMemory (%p)",context);
   sharedMem->imp._pContext = NULL;
   sharedMem->imp._hBlock = S_HANDLE_NULL;
   sharedMem->imp._bAllocated = false;
   return TEEC_RegisterSharedMemory0(context, sharedMem);
}

//-----------------------------------------------------------------------------------------------------
TEEC_Result TEEC_AllocateSharedMemory(
    TEEC_Context*      context,
    TEEC_SharedMemory* sharedMem)
{
   TEEC_Result nResult;
   TRACE_INFO("TEEC_AllocateSharedMemory (%p)",context);

   sharedMem->imp._pContext = NULL;
   sharedMem->imp._hBlock = S_HANDLE_NULL;
   sharedMem->buffer = scxAllocateSharedMemory(sharedMem->size);
   if (sharedMem->buffer == NULL)
   {
      return TEEC_ERROR_OUT_OF_MEMORY;
   }
   sharedMem->imp._bAllocated = true;
   nResult = TEEC_RegisterSharedMemory0(context, sharedMem);
   if (nResult != TEEC_SUCCESS)
   {
      scxReleaseSharedMemory(sharedMem->buffer,sharedMem->size);
      sharedMem->buffer = NULL;
   }
   return nResult;
}

//-----------------------------------------------------------------------------------------------------
void TEEC_ReleaseSharedMemory (
    TEEC_SharedMemory* sharedMem)
{
   SCHANNEL6_ANSWER  sAnswer;
   SCHANNEL6_COMMAND sMessage;
   TEEC_Context* context;

   context = (TEEC_Context *)sharedMem->imp._pContext;
   memset(&sMessage, 0, sizeof(SCHANNEL6_COMMAND));
   sMessage.sReleaseSharedMemory.nMessageType = SCX_RELEASE_SHARED_MEMORY;
   sMessage.sReleaseSharedMemory.nMessageSize = (sizeof(SCHANNEL6_RELEASE_SHARED_MEMORY_COMMAND)
                                    - sizeof(SCHANNEL6_COMMAND_HEADER))/sizeof(uint32_t);
   sMessage.sReleaseSharedMemory.hBlock = sharedMem->imp._hBlock;
   scxExchangeMessage(context,&sMessage, &sAnswer, NULL);
   if (sharedMem->imp._bAllocated)
   {
       scxReleaseSharedMemory(sharedMem->buffer,sharedMem->size);
       /* Update parameters:
       * In this case the Implementation MUST set the buffer and size fields of the sharedMem structure
       * to NULL and 0 respectively before returning.
       */
       sharedMem->buffer = NULL;
       sharedMem->size = 0;
   }
   sharedMem->imp._pContext = NULL;
   sharedMem->imp._hBlock = S_HANDLE_NULL;
}

//-----------------------------------------------------------------------------------------------------
void TEEC_RequestCancellation(TEEC_Operation* operation)
{
   uint32_t nOperationState;
   TEEC_Result       nResult;

   if (operation == NULL) return;

retry:
   nOperationState = operation->started;
   if (nOperationState == 2)
    {
      /* Operation already finished. Return immediately */
      return;
   }
   else if (nOperationState == 1)
   {
       /* Operation is in progress */
       TEEC_Context*     context;
       SCHANNEL6_ANSWER  sAnswer;
       SCHANNEL6_COMMAND sMessage;

       context = operation->imp._pContext;

       memset(&sMessage,0,sizeof(sMessage));
       sMessage.sHeader.nMessageType = SCX_CANCEL_CLIENT_OPERATION;
       sMessage.sHeader.nMessageSize = (sizeof(SCHANNEL6_CANCEL_CLIENT_OPERATION_COMMAND) - sizeof(SCHANNEL6_COMMAND_HEADER))/4;
       sMessage.sCancelClientOperation.hClientSession = operation->imp._hSession;
       sMessage.sCancelClientOperation.nCancellationID = (uint32_t)operation;
       nResult = scxExchangeMessage(context,&sMessage, &sAnswer, NULL);

       if (nResult != TEEC_SUCCESS)
       {
           /* Communication failure. Ignore the error: the operation is already cancelled anyway */
           return;
       }
       if (sAnswer.sCancelClientOperation.nErrorCode == S_SUCCESS)
       {
           /* Command was successfully cancelled */
           return;
       }
       /* Otherwise, the command has not yet reached the secure world or has already finished and we must retry */
   }
   /* This applies as well when nOperationState == 0. In this case, the operation has not yet
      started yet and we don't even have a pointer to the context */
   scxYield();
   goto retry;
}


//-----------------------------------------------------------------------------------------------------
TEEC_Result TEEC_ReadSignatureFile(
                                   void**    ppSignatureFile,
                                   uint32_t* pnSignatureFileLength)
{
   TEEC_Result nErrorCode = TEEC_SUCCESS;

   uint32_t      nBytesRead;
   uint32_t      nSignatureSize = 0;
   uint8_t*     pSignature = NULL;
   FILE*    pSignatureFile = NULL;
   char     sFileName[PATH_MAX + 1 + 5];  /* Allocate room for the signature extension */
   long     nFileSize;

   *pnSignatureFileLength = 0;
   *ppSignatureFile = NULL;

   if (realpath("/proc/self/exe", sFileName) == NULL)
   {
      TRACE_ERROR("TEEC_ReadSignatureFile: realpath failed [%d]", errno);
      return TEEC_ERROR_OS;
   }

   /* Work out the signature file name */
   strcat(sFileName, ".ssig");

   pSignatureFile = fopen(sFileName, "rb");
   if (pSignatureFile == NULL)
   {
      /* Signature doesn't exist */
       return TEEC_ERROR_ITEM_NOT_FOUND;
   }

   if (fseek(pSignatureFile, 0, SEEK_END) != 0)
   {
      TRACE_ERROR("TEEC_ReadSignatureFile: fseek(%s) failed [%d]",
                     sFileName, errno);
      nErrorCode = TEEC_ERROR_OS;
      goto error;
   }

   nFileSize = ftell(pSignatureFile);
   if (nFileSize < 0)
   {
      TRACE_ERROR("TEEC_ReadSignatureFile: ftell(%s) failed [%d]",
                     sFileName, errno);
      nErrorCode = TEEC_ERROR_OS;
      goto error;
   }

   nSignatureSize = (uint32_t)nFileSize;

   if (nSignatureSize != 0)
   {
      pSignature = malloc(nSignatureSize);
      if (pSignature == NULL)
      {
         TRACE_ERROR("TEEC_ReadSignatureFile: Heap - Out of memory for %u bytes",
                        nSignatureSize);
         nErrorCode = TEEC_ERROR_OUT_OF_MEMORY;
         goto error;
      }

      rewind(pSignatureFile);

      nBytesRead = fread(pSignature, 1, nSignatureSize, pSignatureFile);
      if (nBytesRead < nSignatureSize)
      {
         TRACE_ERROR("TEEC_ReadSignatureFile: fread failed [%d]", errno);
         nErrorCode = TEEC_ERROR_OS;
         goto error;
      }
   }

   fclose(pSignatureFile);

   *pnSignatureFileLength = nSignatureSize;
   *ppSignatureFile = pSignature;

   return S_SUCCESS;

error:
   fclose(pSignatureFile);
   free(pSignature);

   return nErrorCode;
}

//-----------------------------------------------------------------------------------------------------
TEEC_Result TEEC_OpenSessionEx (
    TEEC_Context*         context,
    TEEC_Session*         session,      /* OUT */
    const TEEC_TimeLimit* timeLimit,
    const TEEC_UUID*      destination,  /* The trusted application UUID we want to open the session with */
    uint32_t              connectionMethod, /* LoginType*/
    void*                 connectionData,  /* LoginData */
    TEEC_Operation*       operation,    /* payload. If operation is NULL then no data buffers are exchanged with the Trusted Application, and the operation cannot be cancelled by the Client Application */
    uint32_t*             returnOrigin)
{
   TEEC_Result nError;
   uint32_t nReturnOrigin;
   SCHANNEL6_ANSWER  sAnswer;
   SCHANNEL6_COMMAND sCommand;

   memset(&sCommand, 0, sizeof(SCHANNEL6_COMMAND));

   sCommand.sHeader.nMessageType = SCX_OPEN_CLIENT_SESSION;
   sCommand.sHeader.nMessageSize = (sizeof(SCHANNEL6_OPEN_CLIENT_SESSION_COMMAND) - 20 -sizeof(SCHANNEL6_COMMAND_HEADER))/sizeof(uint32_t);
   if (timeLimit == NULL)
   {
      sCommand.sOpenClientSession.sTimeout = SCTIME_INFINITE;
   }
   else
   {
      sCommand.sOpenClientSession.sTimeout = *(uint64_t*)timeLimit;
   }
   sCommand.sOpenClientSession.sDestinationUUID   = *((S_UUID*)destination);
   sCommand.sOpenClientSession.nLoginType         = connectionMethod;
   if ((connectionMethod == TEEC_LOGIN_GROUP)||(connectionMethod == TEEC_LOGIN_GROUP_APPLICATION))
   {
       /* connectionData MUST point to a uint32_t which contains the group
       * which this Client Application wants to connect as. The Linux Driver
       * is responsible for securely ensuring that the Client Application
       * instance is actually a member of this group.
       */
       if (connectionData != NULL)
       {
           *(uint32_t*)sCommand.sOpenClientSession.sLoginData = *(uint32_t*)connectionData;
           sCommand.sHeader.nMessageSize += sizeof(uint32_t);
       }
   }
   sCommand.sOpenClientSession.nCancellationID    = (uint32_t)operation; // used for TEEC_RequestCancellation

   if (operation != NULL)
   {
       operation->imp._pContext = context;
       operation->imp._hSession = S_HANDLE_NULL;
       operation->started = 1;
   }

   nError = scxExchangeMessage(context, &sCommand, &sAnswer, operation);

   if (operation != NULL) operation->started = 2;

   if (nError != TEEC_SUCCESS)
   {
       nReturnOrigin = TEEC_ORIGIN_COMMS;
   }
   else
   {
       nError = sAnswer.sOpenClientSession.nErrorCode;
       nReturnOrigin = sAnswer.sOpenClientSession.nReturnOrigin;
   }

   if (returnOrigin != NULL) *returnOrigin = nReturnOrigin;

   if (nError == S_SUCCESS)
   {
       session->imp._hClientSession = sAnswer.sOpenClientSession.hClientSession;
       session->imp._pContext       = context;
   }

   return nError;
}

//-----------------------------------------------------------------------------------------------------
TEEC_Result TEEC_InvokeCommandEx(
    TEEC_Session*         session,
    const TEEC_TimeLimit* timeLimit,
    uint32_t              commandID,
    TEEC_Operation*       operation,
    uint32_t*             returnOrigin)
{
   TEEC_Result nError;
   SCHANNEL6_ANSWER  sAnswer;
   SCHANNEL6_COMMAND sCommand;
   uint32_t    nReturnOrigin;
   TEEC_Context * context;

   context = (TEEC_Context *)session->imp._pContext;
   memset(&sCommand, 0, sizeof(SCHANNEL6_COMMAND));

   sCommand.sHeader.nMessageType = SCX_INVOKE_CLIENT_COMMAND;
   sCommand.sHeader.nMessageSize = (sizeof(SCHANNEL6_INVOKE_CLIENT_COMMAND_COMMAND) - sizeof(SCHANNEL6_COMMAND_HEADER))/sizeof(uint32_t);
   sCommand.sInvokeClientCommand.nClientCommandIdentifier = commandID;
    if (timeLimit == NULL)
   {
      sCommand.sInvokeClientCommand.sTimeout = SCTIME_INFINITE;
   }
   else
   {
      sCommand.sInvokeClientCommand.sTimeout = *(uint64_t*)timeLimit;
   }
   sCommand.sInvokeClientCommand.hClientSession     = session->imp._hClientSession;
   sCommand.sInvokeClientCommand.nCancellationID = (uint32_t)operation; // used for TEEC_RequestCancellation

   if (operation != NULL)
   {
      operation->imp._pContext = session->imp._pContext;
      operation->imp._hSession = session->imp._hClientSession;
      operation->started = 1;
   }

   nError = scxExchangeMessage(context, &sCommand, &sAnswer, operation);

   if (operation != NULL)
   {
      operation->started = 2;
      operation->imp._hSession = S_HANDLE_NULL;
      operation->imp._pContext = NULL;
   }

   if (nError != TEEC_SUCCESS)
   {
      nReturnOrigin = TEEC_ORIGIN_COMMS;
   }
   else
   {
       nError = sAnswer.sInvokeClientCommand.nErrorCode;
       nReturnOrigin = sAnswer.sInvokeClientCommand.nReturnOrigin;
    }

   if (returnOrigin != NULL) *returnOrigin = nReturnOrigin;

   return nError;

}

//-----------------------------------------------------------------------------------------------------
/*
 * Retrieves information about the implementation
 */
void TEEC_GetImplementationInfo(
                                TEEC_Context*            context,
                                TEEC_ImplementationInfo* description)
{
   TRACE_INFO("TEEC_GetImplementationInfo");

   memset(description, 0, sizeof(TEEC_ImplementationInfo));

   strcpy(description->apiDescription, S_VERSION_STRING);

   if (context != NULL)
   {
      SCX_VERSION_INFORMATION_BUFFER sInfoBuffer;
      uint32_t nResult;

      nResult = ioctl((S_HANDLE)context->imp._hConnection, IOCTL_SCX_GET_DESCRIPTION, &sInfoBuffer);
      if (nResult != S_SUCCESS)
      {
         TRACE_ERROR("TEEC_GetImplementationInfo[0x%X]: ioctl returned error: 0x%x ( %d)\n",context, nResult, errno);
         return;
      }

      memcpy(description->commsDescription, sInfoBuffer.sDriverDescription, 64);
      description->commsDescription[64] = 0;
      memcpy(description->TEEDescription, sInfoBuffer.sSecureWorldDescription, 64);
      description->TEEDescription[64] = 0;
   }
}

void TEEC_GetImplementationLimits(
   TEEC_ImplementationLimits* limits)
{
   memset(limits, 0, sizeof(TEEC_ImplementationLimits));

   /* A temp mem ref can not be mapped on more than 1Mb */
   limits->pageSize = SIZE_4KB;
   limits->tmprefMaxSize = SIZE_1MB;
   limits->sharedMemMaxSize = SIZE_1MB * 8;
 }