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