Merge "[ElmSharp] Fixed the crash issue of Calendar"
[platform/core/csapi/tizenfx.git] / src / Tizen.Security.TEEC / Tizen.Security.TEEC / Libteec.cs
1 /*
2  *  Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License
15  */
16
17 using System;
18 using System.Collections.Generic;
19 using System.Runtime.InteropServices;
20 using System.Threading;
21 using System.Threading.Tasks;
22
23 namespace Tizen.Security.TEEC
24 {
25     /// <summary>
26     /// This type denotes the Session Login Method used in OpenSession.
27     /// </summary>
28     /// <since_tizen> 3 </since_tizen>
29     public class LoginMethod
30     {
31         /// <summary>No login data is provided.</summary>
32         /// <since_tizen> 3 </since_tizen>
33         public const uint Public      = 0x00000000;
34         /// <summary>Login data about the user running the client application process is provided.</summary>
35         /// <since_tizen> 3 </since_tizen>
36         public const uint User        = 0x00000001;
37         /// <summary>Login data about the group running the client application process is provided.</summary>
38         /// <since_tizen> 3 </since_tizen>
39         public const uint Group       = 0x00000002;
40         /// <summary>Login data about the running client application itself is provided.</summary>
41         /// <since_tizen> 3 </since_tizen>
42         public const uint Application = 0x00000003;
43     }
44
45     /// <summary>
46     /// This type denotes the Value parameter.
47     /// </summary>
48     /// <since_tizen> 3 </since_tizen>
49     public enum TEFValueType : UInt32
50     {
51         /// <summary>The parameter is a ValueType tagged as the input.</summary>
52         Input  = 0x00000001,
53         /// <summary>The parameter is a ValueType tagged as the output.</summary>
54         Output = 0x00000002,
55         /// <summary>The parameter is a ValueType tagged as both the input and the output.</summary>
56         InOut  = 0x00000003,
57     }
58
59     /// <summary>
60     /// This type denotes the TempMemoryReference parameter
61     /// describing a region of memory which needs to be temporarily registered for the duration of the operation.
62     /// </summary>
63     /// <since_tizen> 3 </since_tizen>
64     public enum TEFTempMemoryType : UInt32
65     {
66         /// <summary>The parameter is a TempMemoryType and is tagged as the input.</summary>
67         Input  = 0x00000005,
68         /// <summary>Same as the input, but the Memory Reference is tagged as the output.</summary>
69         Output = 0x00000006,
70         /// <summary>A Temporary Memory Reference tagged as both the input and the output.</summary>
71         InOut  = 0x00000007,
72     }
73
74     /// <summary>
75     /// This type denotes the SharedMemoryReference parameter.
76     /// </summary>
77     /// <since_tizen> 3 </since_tizen>
78     public enum TEFRegisteredMemoryType : UInt32
79     {
80         /// <summary>The parameter is a registered memory reference that refers to the entirety of its parent shared memory block.</summary>
81         Whole         = 0x0000000C,
82         /// <summary>A registered memory reference structure that refers to a partial region of its parent shared mMemory block and is tagged as the input.</summary>
83         PartialInput  = 0x0000000D,
84         /// <summary>A registered memory reference structure that refers to a partial region of its parent shared memory block and is tagged as the output.</summary>
85         PartialOutput = 0x0000000E,
86         /// <summary>A registered memory reference structure that refers to a partial region of its parent shared memory block and is tagged as both the input and the output.</summary>
87         PartialInOut  = 0x0000000F,
88     }
89
90     /// <summary>
91     /// This type denotes the SharedMemory access direction.
92     /// </summary>
93     /// <since_tizen> 3 </since_tizen>
94     [Flags]
95     public enum SharedMemoryFlags : UInt32
96     {
97         /// <summary>A flag indicates the shared memory can be read.</summary>
98         Input  = 0x00000001,
99         /// <summary>A flag indicates the shared memory can be written.</summary>
100         Output = 0x00000002,
101         /// <summary>A flag indicates the shared memory can be read and written.</summary>
102         InOut = Input | Output,
103     }
104
105     /// <summary>
106     /// This type denotes a shared memory block which has been either registered
107     /// with the implementation or allocated by it.
108     /// </summary>
109     /// <since_tizen> 3 </since_tizen>
110     public sealed class SharedMemory
111     {
112         internal Interop.TEEC_SharedMemory shm;
113         internal IntPtr shmptr;
114         internal SharedMemory(ref Interop.TEEC_SharedMemory shm)
115         {
116             this.shm=shm;
117             shmptr = Marshal.AllocHGlobal(Marshal.SizeOf(shm));
118             Marshal.StructureToPtr(shm, shmptr, false);
119         }
120         ~SharedMemory()
121         {
122             Marshal.FreeHGlobal(shmptr);
123         }
124         /// <summary>
125         /// This property represents the shared memory size in bytes.
126         /// </summary>
127         /// <since_tizen> 3 </since_tizen>
128         /// <privilege>http://tizen.org/privilege/tee.client</privilege>
129         /// <privlevel>partner</privlevel>
130         public UInt32 Size
131         {
132             get { return shm.size.ToUInt32(); }
133         }
134         /// <summary>
135         /// This property represents the start address of the shared memory block.
136         /// </summary>
137         /// <since_tizen> 3 </since_tizen>
138         /// <privilege>http://tizen.org/privilege/tee.client</privilege>
139         /// <privlevel>partner</privlevel>
140         public IntPtr Address
141         {
142             get { return shm.buffer; }
143         }
144
145         /// <summary>
146         /// This function makes a copy and is designed for convenient operations on small buffers.
147         /// For large buffers, the direct address should be used.
148         /// </summary>
149         /// <since_tizen> 3 </since_tizen>
150         /// <privilege>http://tizen.org/privilege/tee.client</privilege>
151         /// <privlevel>partner</privlevel>
152         /// <param name="data">The source data buffer to copy data from.</param>
153         /// <param name="dstOffs">The starting offset in the destination shared memory.</param>
154         /// <exception cref="InvalidOperationException">The operation is invalid.</exception>
155         public void SetData(byte[] data, int dstOffs)
156         {
157             if ((shm.flags & (uint)SharedMemoryFlags.Output) == 0) throw new InvalidOperationException("No write access");
158             Marshal.Copy(data, 0, shm.buffer + dstOffs, data.Length);
159         }
160         /// <summary>
161         /// This function makes a copy and is designed for convenient operations on small buffers.
162         /// For large buffers, the direct address should be used.
163         /// </summary>
164         /// <since_tizen> 3 </since_tizen>
165         /// <privilege>http://tizen.org/privilege/tee.client</privilege>
166         /// <privlevel>partner</privlevel>
167         /// <param name="data">The destination data buffer to copy data into.</param>
168         /// <param name="srcOffs">The starting offset in the source shared memory.</param>
169         /// <exception cref="InvalidOperationException">The operation is invalid.</exception>
170         public void GetData(byte[] data, int srcOffs)
171         {
172             if ((shm.flags & (uint)SharedMemoryFlags.Input) == 0) throw new InvalidOperationException("No read access");
173             Marshal.Copy(shm.buffer + srcOffs, data, 0, data.Length);
174         }
175     };
176
177     /// <summary>
178     /// This type defines the payload of either an open session operation or an invoke command operation. It is
179     /// also used for cancelation of operations, which may be desirable even if no payload is passed.
180     /// Parameters are used to exchange data between CA and TA.
181     /// </summary>
182     /// <since_tizen> 3 </since_tizen>
183     public abstract class Parameter
184     {
185         internal Parameter(uint t)
186         {
187             this.NativeType = t;
188         }
189         internal uint NativeType { get; }
190     };
191
192     /// <summary>
193     /// This type defines a template for the parameter types.
194     /// </summary>
195     /// <since_tizen> 3 </since_tizen>
196     public abstract class BaseParameter<TEnum> : Parameter where TEnum : struct, IComparable, IFormattable, IConvertible // as close to Enum as possible
197     {
198         internal BaseParameter(TEnum t) : base((uint)(object)t)
199         {
200             Type = t;
201         }
202
203         /// <summary>
204         /// This property represents the access type to this parameter.
205         /// </summary>
206         /// <since_tizen> 3 </since_tizen>
207         public TEnum Type { get; }
208     }
209
210     /// <summary>
211     /// This type defines a temporary memory reference.
212     /// </summary>
213     /// <since_tizen> 3 </since_tizen>
214     public sealed class TempMemoryReference : BaseParameter<TEFTempMemoryType>
215     {
216         /// <summary>
217         /// Constructs a parameter object which holds information about the temporary memory copied to or from TA.
218         /// </summary>
219         /// <since_tizen> 3 </since_tizen>
220         /// <param name="buffer">The address of the allocated memory buffer.</param>
221         /// <param name="size">The size of the buffer.</param>
222         /// <param name="type">The kind of access allowed for TA <see cref="TEFTempMemoryType"/>.</param>
223         public TempMemoryReference(IntPtr buffer, uint size, TEFTempMemoryType type) :
224                 base(type)
225         {
226             this.Buffer = buffer;
227             this.Size = size;
228         }
229         /// <summary>
230         /// This property represents the memory address of the buffer.
231         /// </summary>
232         /// <since_tizen> 3 </since_tizen>
233         public IntPtr Buffer { get; }
234         /// <summary>
235         /// This property represents the size of the buffer.
236         /// </summary>
237         /// <since_tizen> 3 </since_tizen>
238         public uint Size { get; internal set; }
239     };
240
241     /// <summary>
242     /// This type defines a memory reference that uses a pre-registered or pre-allocated shared memory block.
243     /// </summary>
244     /// <since_tizen> 3 </since_tizen>
245     public sealed class RegisteredMemoryReference : BaseParameter<TEFRegisteredMemoryType>
246     {
247         /// <summary>
248         /// Constructs a parameter object which holds information about the registered memory shared with TA.
249         /// </summary>
250         /// <since_tizen> 3 </since_tizen>
251         /// <privilege>http://tizen.org/privilege/tee.client</privilege>
252         /// <privlevel>partner</privlevel>
253         /// <param name="parent">The shared memory - registered or allocated.</param>
254         /// <param name="size">The size of the buffer part.</param>
255         /// <param name="offset">The offset of the buffer in the shared memory.</param>
256         /// <param name="type">The kind of access allowed for TA <see cref="TEFRegisteredMemoryType"/>.</param>
257         public RegisteredMemoryReference(SharedMemory parent, uint size, uint offset, TEFRegisteredMemoryType type) :
258                 base(type)
259         {
260             this.Parent = parent;
261             if (type == TEFRegisteredMemoryType.Whole) {
262                 this.Size = parent.Size;
263                 this.Offset = 0;
264             }
265             else {
266                 this.Size = size;
267                 this.Offset = offset;
268             }
269         }
270
271         /// <summary>
272         /// This property represents the shared memory that is referred to.
273         /// </summary>
274         /// <since_tizen> 3 </since_tizen>
275         /// <privilege>http://tizen.org/privilege/tee.client</privilege>
276         /// <privlevel>partner</privlevel>
277         public SharedMemory Parent { get; }
278         /// <summary>
279         /// This property represents the size (in bytes) of the shared memory.
280         /// </summary>
281         /// <since_tizen> 3 </since_tizen>
282         /// <privilege>http://tizen.org/privilege/tee.client</privilege>
283         /// <privlevel>partner</privlevel>
284         public uint Size { get; internal set; }
285         /// <summary>
286         /// This property represents the offset (in bytes) from the start of the shared memory.
287         /// </summary>
288         /// <since_tizen> 3 </since_tizen>
289         /// <privilege>http://tizen.org/privilege/tee.client</privilege>
290         /// <privlevel>partner</privlevel>
291         public uint Offset { get; }
292     };
293
294     /// <summary>
295     /// This type defines a parameter that is not referencing the shared memory, but carries instead small raw data
296     /// passed by a value.
297     /// </summary>
298     /// <since_tizen> 3 </since_tizen>
299     public sealed class Value : BaseParameter<TEFValueType>
300     {
301         /// <summary>
302         /// Constructs a parameter object which holds information about integer values copied to or from TA.
303         /// </summary>
304         /// <since_tizen> 3 </since_tizen>
305         /// <param name="a">User paramter A.</param>
306         /// <param name="b">User paramter B.</param>
307         /// <param name="type">The kind of access allowed for TA <see cref="TEFValueType"/>.</param>
308         public Value(uint a, uint b, TEFValueType type) :
309                 base(type)
310         {
311             this.A = a;
312             this.B = b;
313         }
314         /// <summary>
315         /// This property represents an unsigned integer A.
316         /// </summary>
317         /// <since_tizen> 3 </since_tizen>
318         public uint A { get; internal set; }
319         /// <summary>
320         /// This property represents an unsigned integer B.
321         /// </summary>
322         /// <since_tizen> 3 </since_tizen>
323         public uint B { get; internal set; }
324     };
325
326
327     /// <summary>
328     /// This type denotes a TEE Session, the logical container linking a client application with a particular trusted application.
329     /// </summary>
330     /// <since_tizen> 3 </since_tizen>
331     public sealed class Session
332     {
333         private Interop.TEEC_Context context;
334         private Interop.TEEC_Session session;
335
336         internal Session(Interop.TEEC_Context context) {
337             this.context = context;
338             this.session = new Interop.TEEC_Session();
339         }
340
341         /// <summary>
342         /// Destructor of the class.
343         /// </summary>
344         ~Session()
345         {
346             Close();
347         }
348
349         static internal UInt32 InitParam(ref Interop.TEEC_Parameter[] dst, int i, Parameter src)
350         {
351             switch (src.NativeType) {
352                 case (int)TEFValueType.Input:
353                 case (int)TEFValueType.Output:
354                 case (int)TEFValueType.InOut:
355                     dst[i].value.a = ((Value)src).A;
356                     dst[i].value.b = ((Value)src).B;
357                     break;
358
359                 case (int)TEFTempMemoryType.Input:
360                 case (int)TEFTempMemoryType.Output:
361                 case (int)TEFTempMemoryType.InOut:
362                     dst[i].tmpref.buffer = ((TempMemoryReference)src).Buffer;
363                     dst[i].tmpref.size = new UIntPtr(((TempMemoryReference)src).Size);
364                     break;
365
366                 case (int)TEFRegisteredMemoryType.Whole:
367                 case (int)TEFRegisteredMemoryType.PartialInput:
368                 case (int)TEFRegisteredMemoryType.PartialOutput:
369                 case (int)TEFRegisteredMemoryType.PartialInOut:
370                     RegisteredMemoryReference rmr = (RegisteredMemoryReference)src;
371                     dst[i].memref.parent = rmr.Parent.shmptr;
372                     dst[i].memref.size = new UIntPtr(rmr.Size);
373                     dst[i].memref.offset = new UIntPtr(rmr.Offset);
374                     break;
375
376                 default: return 0;
377             }
378             return src.NativeType;
379         }
380
381         static internal void UpdateParam(Interop.TEEC_Parameter src, ref Parameter[] dst, int i)
382         {
383             switch (dst[i].NativeType) {
384                case (int)TEFValueType.Input:
385                case (int)TEFValueType.Output:
386                case (int)TEFValueType.InOut:
387                    ((Value)dst[i]).A = src.value.a;
388                    ((Value)dst[i]).B = src.value.b;
389                    break;
390
391                case (int)TEFTempMemoryType.Input:
392                case (int)TEFTempMemoryType.Output:
393                case (int)TEFTempMemoryType.InOut:
394                    ((TempMemoryReference)dst[i]).Size = src.tmpref.size.ToUInt32();
395                    break;
396
397                case (int)TEFRegisteredMemoryType.Whole:
398                case (int)TEFRegisteredMemoryType.PartialInput:
399                case (int)TEFRegisteredMemoryType.PartialOutput:
400                case (int)TEFRegisteredMemoryType.PartialInOut:
401                    ((RegisteredMemoryReference)dst[i]).Size = src.memref.size.ToUInt32();
402                    break;
403
404                default: break;
405             }
406         }
407
408         static internal Interop.TEEC_Operation Create_TEEC_Operation() {
409             Interop.TEEC_Operation op = new Interop.TEEC_Operation();
410             op.started=0;
411             op.paramTypes=0;
412             op.paramlist = new Interop.TEEC_Parameter[4];
413             return op;
414         }
415
416         internal void Open(Guid destination, uint loginMethod, byte[] connectionData, Parameter[] paramlist)
417         {
418             Interop.TEEC_UUID uuid = Interop.TEEC_UUID.ToTeecUuid(destination);
419
420             int ret;
421             uint ro;
422             if (paramlist != null) {
423                 Interop.TEEC_Operation op = Create_TEEC_Operation();
424                 for (int i=0; i < 4 && i < paramlist.Length; ++i) {
425                     op.paramTypes |= InitParam(ref op.paramlist, i, paramlist[i]) << (4*i);
426                 }
427                 ret = Interop.Libteec.OpenSession(ref context, ref session, ref uuid, loginMethod, connectionData, ref op, out ro);
428                 for (int i=0; i < 4 && i < paramlist.Length; ++i) {
429                     UpdateParam(op.paramlist[i], ref paramlist, i);
430                 }
431             }
432             else {
433                 ret = Interop.Libteec.OpenSession(ref context, ref session, ref uuid, loginMethod, connectionData, IntPtr.Zero, out ro);
434             }
435
436             //MAYBE map origin of return code to specyfic Exception
437             Interop.CheckNThrowException(ret, string.Format("OpenSession('{0}')", destination));
438         }
439
440         /// <summary>
441         /// This function closes a session which has been opened with a trusted application.
442         /// All commands within the session must be completed before this function can be called.
443         /// </summary>
444         /// <since_tizen> 3 </since_tizen>
445         /// <privilege>http://tizen.org/privilege/tee.client</privilege>
446         /// <privlevel>partner</privlevel>
447         /// <feature>http://tizen.org/feature/security.tee</feature>
448         /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
449         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
450         /// <exception cref="InvalidOperationException">The operation is invalid.</exception>
451         public void Close() {
452             Interop.Libteec.CloseSession(ref session);
453         }
454
455         /// <summary>
456         /// This function invokes a command within the specified session.
457         /// The parameter commandID is an identifier that is used to indicate which of the exposed trusted
458         /// application functions should be invoked. The supported command identifier values are defined by the
459         /// trusted application's protocol.
460         /// There can be up to four parameter objects given in the <paramref name="paramlist"/> array.
461         /// </summary>
462         /// <since_tizen> 3 </since_tizen>
463         /// <param name="commandID">The command.</param>
464         /// <param name="paramlist">The array of parameters.</param>
465         /// <privilege>http://tizen.org/privilege/tee.client</privilege>
466         /// <privlevel>partner</privlevel>
467         /// <feature>http://tizen.org/feature/security.tee</feature>
468         /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
469         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
470         /// <exception cref="InvalidOperationException">The operation is invalid.</exception>
471         /// <exception cref="ArgumentException">The argument <paramref name="paramlist"/> is wrong.</exception>
472         public void InvokeCommand(uint commandID, Parameter[] paramlist)
473         {
474             int ret;
475             uint ro;
476             if (paramlist != null) {
477                 Interop.TEEC_Operation op = Create_TEEC_Operation();
478                 for (int i=0; i < 4 && i < paramlist.Length; ++i) {
479                     op.paramTypes |= InitParam(ref op.paramlist, i, paramlist[i]) << (4*i);
480                 }
481                 ret = Interop.Libteec.InvokeCommand(ref session, commandID, ref op, out ro);
482                 for (int i=0; i < 4 && i < paramlist.Length; ++i) {
483                     UpdateParam(op.paramlist[i], ref paramlist, i);
484                 }
485             }
486             else {
487                 ret = Interop.Libteec.InvokeCommand(ref session, commandID, IntPtr.Zero, out ro);
488             }
489
490             //MAYBE map origin of return code to specific Exception
491             Interop.CheckNThrowException(ret, string.Format("InvokeCommand({0})", commandID));
492         }
493
494         /// <summary>
495         /// The asynchronous version of the InvokeCommand.
496         /// </summary>
497         /// <since_tizen> 3 </since_tizen>
498         /// <param name="commandID">The command.</param>
499         /// <param name="paramlist">The array of parameters.</param>
500         /// <param name="token">The token for task manipulation.</param>
501         /// <returns>Returns a task executing an invoke command in the background.</returns>
502         /// <privilege>http://tizen.org/privilege/tee.client</privilege>
503         /// <privlevel>partner</privlevel>
504         /// <feature>http://tizen.org/feature/security.tee</feature>
505         /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
506         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
507         /// <exception cref="InvalidOperationException">The operation is invalid.</exception>
508         /// <exception cref="ArgumentException">One of the arguments is wrong.</exception>
509         public async Task InvokeCommandAsync(uint commandID, Parameter[] paramlist, CancellationToken token = default(CancellationToken))
510         {
511             await Task.Factory.StartNew(() => InvokeCommand(commandID, paramlist));
512         }
513
514     };
515
516     /// <summary>
517     /// This type denotes a TEE Context, the main logical container linking a Client Application with a particular TEE.
518     /// </summary>
519     /// <since_tizen> 3 </since_tizen>
520     public sealed class Context : IDisposable
521     {
522         private Interop.TEEC_Context context;
523
524         /// <summary>
525         /// This function (constructor) initializes a new TEE Context, forming a connection between this client application and the
526         /// TEE identified by the string identifier name (empty or null name denotes a default TEE).
527         /// </summary>
528         /// <since_tizen> 3 </since_tizen>
529         /// <param name="name">The TEE name.</param>
530         /// <privilege>http://tizen.org/privilege/tee.client</privilege>
531         /// <privlevel>partner</privlevel>
532         /// <feature>http://tizen.org/feature/security.tee</feature>
533         /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
534         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
535         /// <exception cref="InvalidOperationException">The operation is invalid.</exception>
536         public Context(string name)
537         {
538             context = new Interop.TEEC_Context();
539             if (name != null && name.Length == 0)
540                 name = null;
541             try {
542                 int ret = Interop.Libteec.InitializeContext(name, ref context);
543                 Interop.CheckNThrowException(ret, string.Format("InititalizeContext('{0}')", name));
544             }
545             catch (global::System.DllNotFoundException e)
546             {
547                 unchecked {
548                     Interop.CheckNThrowException((int)Interop.LibteecError.NotImplemented, "Not found: " + e.Message);
549                 }
550             }
551         }
552
553         /// <summary>
554         /// Destructor of the class.
555         /// </summary>
556         ~Context()
557         {
558             Dispose();
559         }
560
561         /// <summary>
562         /// This function implements the IDisposable interface.
563         /// </summary>
564         /// <since_tizen> 3 </since_tizen>
565         /// <privilege>http://tizen.org/privilege/tee.client</privilege>
566         /// <privlevel>partner</privlevel>
567         /// <feature>http://tizen.org/feature/security.tee</feature>
568         public void Dispose() {
569             try {
570                 Interop.Libteec.FinalizeContext(ref context);
571             }
572             catch (global::System.DllNotFoundException) { }
573         }
574
575         /// <summary>
576         /// This function opens a new session between the client application and the specified trusted application.
577         /// The target trusted application is identified by an UUID passed in the parameter destination.
578         /// There can be up to four parameter objects given in the <paramref name="paramlist"/> array.
579         /// </summary>
580         /// <since_tizen> 3 </since_tizen>
581         /// <param name="destination">The UUID of the destination TA.</param>
582         /// <param name="loginMethod">The authentication algorithm <see cref="LoginMethod" />.</param>
583         /// <param name="connectionData">The data to be verified by a given login method.</param>
584         /// <param name="paramlist">The parameters to be passed to TA open-session-callback.</param>
585         /// <returns>Returns opened session.</returns>
586         /// <privilege>http://tizen.org/privilege/tee.client</privilege>
587         /// <privlevel>partner</privlevel>
588         /// <feature>http://tizen.org/feature/security.tee</feature>
589         /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
590         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
591         /// <exception cref="InvalidOperationException">The operation is invalid.</exception>
592         /// <exception cref="ArgumentException">One of the arguments is wrong.</exception>
593         public Session OpenSession(Guid destination, uint loginMethod, byte[] connectionData, Parameter[] paramlist)
594         {
595             Session ses = new Session(context);
596             ses.Open(destination, loginMethod, connectionData, paramlist);
597             return ses;
598         }
599         /// <summary>
600         /// @see OpenSession (Guid destination, uint connectionMethod, byte[] connectionData, Parameter[] paramlist, CancellationToken token).
601         /// </summary>
602         /// <since_tizen> 3 </since_tizen>
603         /// <param name="destination">The UUID of the destination TA.</param>
604         /// <returns>Returns opened session.</returns>
605         /// <privilege>http://tizen.org/privilege/tee.client</privilege>
606         /// <privlevel>partner</privlevel>
607         /// <feature>http://tizen.org/feature/security.tee</feature>
608         /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
609         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
610         /// <exception cref="InvalidOperationException">The operation is invalid.</exception>
611         public Session OpenSession(Guid destination)
612         {
613             Session ses = new Session(context);
614             ses.Open(destination, LoginMethod.Public, null, null);
615             return ses;
616         }
617
618         /// <summary>
619         /// The asynchronous version of the OpenSession.
620         /// @see OpenSession (Guid destination, uint connectionMethod, byte[] connectionData, Parameter[] paramlist, CancellationToken token).
621         /// </summary>
622         /// <since_tizen> 3 </since_tizen>
623         /// <param name="destination">The UUID of the destination TA.</param>
624         /// <param name="loginMethod">The authentication algorithm <see cref="LoginMethod" />.</param>
625         /// <param name="connectionData">The data to be verified by a given login method.</param>
626         /// <param name="paramlist">The parameters to be passed to the TA open-session-callback.</param>
627         /// <param name="token">The token for the task manipulation.</param>
628         /// <returns>Returns a Task executing the session open in the background.</returns>
629         /// <privilege>http://tizen.org/privilege/tee.client</privilege>
630         /// <privlevel>partner</privlevel>
631         /// <feature>http://tizen.org/feature/security.tee</feature>
632         /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
633         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
634         /// <exception cref="InvalidOperationException">The operation is invalid.</exception>
635         /// <exception cref="ArgumentException">One of the arguments is wrong.</exception>
636         public async Task<Session> OpenSessionAsync(Guid destination, uint loginMethod, byte[] connectionData, Parameter[] paramlist, CancellationToken token = default(CancellationToken))
637         {
638             Task<Session> task = Task<Session>.Factory.StartNew(() =>
639             {
640                 return OpenSession(destination, loginMethod, connectionData, paramlist);
641             });
642             return await task;
643         }
644         /// <summary>
645         /// The asynchronous version of the OpenSession.
646         /// @see OpenSession (Guid destination, uint connectionMethod, byte[] connectionData, Parameter[] paramlist, CancellationToken token).
647         /// </summary>
648         /// <since_tizen> 3 </since_tizen>
649         /// <param name="destination">The UUID of the destination TA.</param>
650         /// <param name="token">The token for a task manipulation.</param>
651         /// <returns>Returns a task executing session open in the background.</returns>
652         /// <privilege>http://tizen.org/privilege/tee.client</privilege>
653         /// <privlevel>partner</privlevel>
654         /// <feature>http://tizen.org/feature/security.tee</feature>
655         /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
656         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
657         /// <exception cref="InvalidOperationException">The operation is invalid.</exception>
658         public async Task<Session> OpenSessionAsync(Guid destination, CancellationToken token = default(CancellationToken))
659         {
660             Task<Session> task = Task<Session>.Factory.StartNew(() =>
661             {
662                 return OpenSession(destination);
663             });
664             return await task;
665         }
666
667         /// <summary>
668         /// This function registers a block of the existing client application memory as a block of shared memory within
669         /// the scope of the specified context, in accordance with the parameters.
670         /// The input <paramref name="memaddr"/> must point to the shared memory region to register.
671         /// </summary>
672         /// <since_tizen> 3 </since_tizen>
673         /// <param name="memaddr">The address of the shared memory.</param>
674         /// <param name="size">The size of the shared memory.</param>
675         /// <param name="flags">The flags describing the access modes (Input and/or Output).</param>
676         /// <returns>Returns the SharedMemory handler.</returns>
677         /// <privilege>http://tizen.org/privilege/tee.client</privilege>
678         /// <privlevel>partner</privlevel>
679         /// <feature>http://tizen.org/feature/security.tee</feature>
680         /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
681         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
682         /// <exception cref="InvalidOperationException">The operation is invalid.</exception>
683         /// <exception cref="ArgumentException">The argument <paramref name="memaddr"/> is wrong.</exception>
684         public SharedMemory RegisterSharedMemory(IntPtr memaddr, UInt32 size, SharedMemoryFlags flags)
685         {
686             Interop.TEEC_SharedMemory shm = new Interop.TEEC_SharedMemory();
687             shm.buffer = memaddr;
688             shm.size = new UIntPtr(size);
689             shm.flags = (UInt32)flags;
690             int ret = Interop.Libteec.RegisterSharedMemory(ref context, ref shm);
691             Interop.CheckNThrowException(ret, "RegisterSharedMemory");
692             return new SharedMemory(ref shm);
693         }
694
695         /// <summary>
696         /// This function allocates a new block of memory as a block of shared memory within the scope of the
697         /// specified context, in accordance with the parameters.
698         /// </summary>
699         /// <since_tizen> 3 </since_tizen>
700         /// <param name="size">The size of shared memory.</param>
701         /// <param name="flags">The flags describing access modes (Input and/or Output).</param>
702         /// <returns>Returns the Shared Memory handler.</returns>
703         /// <privilege>http://tizen.org/privilege/tee.client</privilege>
704         /// <privlevel>partner</privlevel>
705         /// <feature>http://tizen.org/feature/security.tee</feature>
706         /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
707         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
708         /// <exception cref="InvalidOperationException">The operation is invalid.</exception>
709         public SharedMemory AllocateSharedMemory(UInt32 size, SharedMemoryFlags flags)
710         {
711             Interop.TEEC_SharedMemory shm = new Interop.TEEC_SharedMemory();
712             shm.size = new UIntPtr(size);
713             shm.flags = (UInt32)flags;
714             int ret = Interop.Libteec.AllocateSharedMemory(ref context, ref shm);
715             Interop.CheckNThrowException(ret, "AllocateSharedMemory");
716             return new SharedMemory(ref shm);
717         }
718
719         /// <summary>
720         /// This function deregisters or deallocates a previously initialized block of the shared memory.
721         /// </summary>
722         /// <remarks>
723         /// For a memory buffer allocated using AllocateSharedMemory, the implementation must free the
724         /// underlying memory and the client application must not access this region after this function has been
725         /// called. In this case, the implementation must clear the buffer and size fields of the shared memory
726         /// structure before returning.
727         /// For memory registered using RegisterSharedMemory, the implementation must deregister the
728         /// underlying memory from the TEE, but the memory region will stay available to the client application for
729         /// other purposes as the memory is owned by it.
730         /// </remarks>
731         /// <since_tizen> 3 </since_tizen>
732         /// <param name="shm">The shared memory object returned by RegisterSharedMemory or AllocateSharedMemory.</param>
733         /// <privilege>http://tizen.org/privilege/tee.client</privilege>
734         /// <privlevel>partner</privlevel>
735         /// <feature>http://tizen.org/feature/security.tee</feature>
736         /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
737         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
738         /// <exception cref="InvalidOperationException">The operation is invalid.</exception>
739         /// <exception cref="ArgumentException">The argument is wrong.</exception>
740         public void ReleaseSharedMemory(SharedMemory shm)
741         {
742             Interop.Libteec.ReleaseSharedMemory(ref shm.shm);
743         }
744     };
745 }